/* Extended Javscript Functions */
/*override basic element functions so pNd can be array of strings or elements*/
function html_HideElement(pNd){
	if(pNd.constructor == Array){
		for(var i=0;i<pNd.length;i++){
			var node = html_GetElement(pNd[i]);
			if (node) {node.style.display = "none"};
		}
		return pNd;
	}else{
		var node = html_GetElement(pNd);
		if (node) {node.style.display = "none"};
		return node;
	}
}

function html_ShowElement(pNd){
	if(pNd.constructor == Array){
		for(var i=0;i<pNd.length;i++){
			var node = html_GetElement(pNd[i]);
			if (node) {node.style.display = ""};
		}
		return pNd;
	}else{
		var node = html_GetElement(pNd);
		if (node) {node.style.display = ""};
		return node;
	}
}


function html_ToggleElement(pNd){
	if(pNd.constructor == Array){
		for(var i=0;i<pNd.length;i++){
			var node = html_GetElement(pNd[i]);
			if(node){
				if(node.style.display == "none"){html_ShowElement(node)}
				else{html_HideElement(node)}
			}
		}
		return pNd;
	}else{
		var node = html_GetElement(pNd);
		if(node){
			if(node.style.display == "none"){html_ShowElement(node)}
			else{html_HideElement(node)}
		}
		return node;
	}
}

function html_HideItemRow(pNd){
	if(pNd.constructor == Array){
		for(var i=0;i<pNd.length;i++){
			var node = html_GetElement(pNd[i]);
			var lTr = html_CascadeUpTill(node,'TR');
			html_HideElement(lTr);
		}
		return;
	}else{
		var node = html_GetElement(pNd);
		var lTr = html_CascadeUpTill(node,'TR');
		html_HideElement(lTr);
		return;
	}
}

function html_ShowItemRow(pNd){
	if(pNd.constructor == Array){
		for(var i=0;i<pNd.length;i++){
			var node = html_GetElement(pNd[i]);
			var lTr = html_CascadeUpTill(node,'TR');
			html_ShowElement(lTr);
		}
		return;
	}else{
		var node = html_GetElement(pNd);
		var lTr = html_CascadeUpTill(pNd,'TR');
		html_ShowElement(lTr);
		return;
	}
}

function html_ToggleItemRow(pNd){
	if(pNd.constructor == Array){
		for(var i=0;i<pNd.length;i++){
			var node = html_GetElement(pNd[i]);
			var lTr = html_CascadeUpTill(node,'TR');
			html_ToggleElement(lTr);
		}
		return;
	}else{
		var node = html_GetElement(pNd);
		var lTr = html_CascadeUpTill(pNd,'TR');
		html_ToggleElement(lTr);
		return;
	}
}


function html_SetSelectValue(pId,pValue){
 var lSelect = html_GetElement(pId);
 if(lSelect.nodeName == 'SELECT'){  
  for(var i=0,l=lSelect.options.length;i<l;i++){
    if(lSelect.options[i].value == pValue){lSelect.options[i].selected=true;}
		else{lSelect.options[i].selected=false;}
  }
 }
}


/* New Javscript Functions */

function f_Hide_On_Value_Item(pThis,pThat,pValue){
	var l_Value;
	if(html_GetElement(pThat)){
		if(html_GetElement(pThis).nodeName=='SELECT'){
			l_Value = html_SelectValue(pThis);
		}else{
			l_Value = html_GetElement(pThis).value
		}
		if(l_Value==pValue){
			html_HideElement(pThat);
		}else{
			html_ShowElement(pThat)
		}

	}
}

function f_Hide_On_Value_Item_Row(pThis,pThat,pValue){
	var l_Value;
	if(html_GetElement(pThat)){
		if(html_GetElement(pThis).nodeName=='SELECT'){
			l_Value = html_SelectValue(pThis);
		}else{
			l_Value = html_GetElement(pThis).value
		}
		if(l_Value==pValue){
			html_HideItemRow(pThat);
		}else{
			html_ShowItemRow(pThat)
		}

	}
}

function html_DisableOnValue(pThis,pValue,pThat){
	var lTest;
  if(pThis.nodeName == 'SELECT'){
			lTest = html_SelectValue(pThis) == pValue;
	}else{
			lTest = html_GetElement(pThis).value == pValue;
	}
	if(pThat){
		 for (var i=2;i <= arguments.length; i++){
		 		 html_disableItem(arguments[i],lTest)
		}
  }
	return;
}

function html_AddTableRow(pThis,pThat,pNum){
        var tt = html_GetElement(pThat);
        var l_newRows = new Array();
        for(i=0;i<pNum;i++){
          var ogTR = tt.rows[tt.rows.length -1];
          var trClone = ogTR.cloneNode(true);
          if(document.all){
            myNewRow = tt.insertRow(tt.rows.length);
            oReplace = myNewRow.replaceNode(trClone);
          }else{
            tt.appendChild(trClone);
          }
          l_newRows[l_newRows.length] = trClone;
          
          var tSelects = trClone.getElementsByTagName('select');
          for(var iSelects=0;iSelects<tSelects.length;iSelects++){
              tSelects[iSelects].selectedIndex = 0;
              tSelects[iSelects].disabled="";
           }
          var tInputs = trClone.getElementsByTagName('input');
          for(var iInputs=0;iInputs<tInputs.length;iInputs++){
            if(tInputs[iInputs].type=="text"){
            tInputs[iInputs].value="";
            tInputs[iInputs].disabled="";
            }
          }
        }
  return l_newRows;
}




/* Extended Javscript Objects */
/* 
this adds better aysnc functionality 
to the htmldb_Get object 
pVar is the function that you want to call when the xmlhttp state changes
in the function specified by pVar the xmlhttp object can be referenced by the variable p
*/
htmldb_Get.prototype.GetAsync = function(pVar){
   try{
      p = new XMLHttpRequest();
    }catch(e){
      p = new ActiveXObject("Msxml2.XMLHTTP");
    }
    try {
    	var startTime = new Date();
			p.open("POST", this.base, true);
			if(p) {
					p.onreadystatechange = pVar;
					p.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					p.send(this.queryString == null ? this.params : this.queryString );
					return p;
	  	} 
		}catch(e){
      return false;
    }
 }


/* 
these two functions extend the
dhtml_ShuttleObject to add move_all 
and remove_all methods
*/
dhtml_ShuttleObject.prototype.move_all = function(){
	for (var i=0;i<this.Select1.options.length;i++){
			this.Select1.options[i].selected=true;
	}
	this.move()
}
dhtml_ShuttleObject.prototype.remove_all = function(){
	for (var i=0;i<this.Select2.options.length;i++){
			this.Select2.options[i].selected=true;
	}
	this.remove();
}

dhtml_ShuttleObject.prototype.sort1 = function(pDir){
	this.sort(this.Select1,pDir);
}

dhtml_ShuttleObject.prototype.sort2 = function(pDir){
	this.sort(this.Select2,pDir);
}

dhtml_ShuttleObject.prototype.sort = function(pShuttle,pDir){
	var nextOption;
	var lLength = pShuttle.options.length;
	if(pDir == 'U'){
		for (var i=0;i<pShuttle.options.length;i++){
			if(pShuttle.options[i].selected==true){
				if(pDir == 'U'){
					if(i!=0){pShuttle.insertBefore(pShuttle.options[i], pShuttle.options[i-1]);}
				}
			}
		}
	}else if(pDir == 'D'){
		for (var i=pShuttle.options.length-1;i>=0;i--){
			if(pShuttle.options[i].selected==true){
				if(pDir == 'D'){
					if(i!=pShuttle.options.length-1){pShuttle.insertBefore(pShuttle.options[i], pShuttle.options[i+2]);}
				}
			}
		}
	}else{
		var l_Opt = new Array();
			for (var i=0;i<pShuttle.options.length;i++){
				if(pShuttle.options[i].selected==true){
					l_Opt[l_Opt.length] = pShuttle.options[i];
				}
			}
		if(pDir == 'B'){
			for (var i=0;i<l_Opt.length;i++){
				pShuttle.appendChild(l_Opt[i]);
			}
		}else if(pDir == 'T'){
			for (var i=l_Opt.length-1;i>=0;i--){
				pShuttle.insertBefore(l_Opt[i],pShuttle.firstChild);
			}
		}
	}
}

/* New Javscript Objects */




/* Javascript para a função de hide/show no meio do relatório */
function showdetail(t,i,j,p,d){
  var g_LastNewRowOpened = false;
  var l_TR = html_CascadeUpTill(t,'TR');
  var l_ThisTable = html_CascadeUpTill(t,'TABLE');
  var comp1 = escape(t.innerHTML);
  var comp2 = '-%20'+escape(d);
  if(t.innerHTML.indexOf('- ') != -1){
    html_GetElement(g_LastNewRowOpened).parentNode//.removeChild(html_GetElement(g_LastNewRowOpened))
    t.innerHTML = '+ '+d;
    ShowHideDetailRow(t,l_ThisTable,l_TR,i,j,p);
    g_LastNewRowOpened = false;
  }else{
    if(html_GetElement(g_LastNewRowOpened)){
      html_GetElement(g_LastNewRowOpened).parentNode.removeChild(html_GetElement(g_LastNewRowOpened))
      l_ThisTable.normalize();
    }
    ShowHideDetailRow(t,l_ThisTable,l_TR,i,j,p);
    t.innerHTML = '- '+d;
  }
}

/* Javascript para a função de hide/show no meio do relatório */
function ShowHideDetailRow(pThis,pTable,pTR,pID,pIDNA, pidapl){
  var l_Detail = 'detail'+pID;
  var l_NumCells = pTR.cells.length;
  var g_LastNewRowOpened = false;
  if(pTR.rowIndex == pTable.rows.length-1 || pTable.rows[pTR.rowIndex+1].className != "detail"){
    var myNewRow = pTable.insertRow(pTR.rowIndex+1);
    g_LastNewRowOpened = myNewRow;
    myNewRow.className = "detail";
    var myNewCell = myNewRow.insertCell(0);
    myNewCell.className = "detail";
    myNewCell.style.padding = 0;
    myNewCell.colSpan=l_NumCells;
    gURL = 'f?p='+pidapl+':9035:'+html_GetElement('pInstance').value+'::NO:9035:P9035_ID_NIVEL_ACESSO,P9035_ID_GRUPO_ACESSO:'+pIDNA+','+pID;
    gReturn = '<iframe src="'+gURL+'" style="border:none;width:100%;height:250px;" frameborder="0" framespacing="0" marginheight="0" marginwidth="0"></iframe>';
    myNewCell.innerHTML = gReturn;
    html_ShowElement(l_Detail);
  }else{
    html_ToggleElement(pTable.rows[pTR.rowIndex+1]);
  }
}

