

//---------------variaveis Globais------------------------

var gProcessando = 0;



function validateObrigatorio_form1(){
return 1;
}



/**
* Returns the value of the selected radio button in the radio group, null if
* none are selected, and false if the button group doesn't exist
*
* @param {radio Object} or {radio id} el
* OR
* @param {form Object} or {form id} el
* @param {radio group name} radioGroup
*/
function $RF(el, radioGroup) {

	if($(el).type && $(el).type.toLowerCase() == "radio") {

		var radioGroup = $(el).name;

		var el = $(el).form;
	} else 
		if ($(el).tagName.toLowerCase() != 'form') {
			return false;
		}

	var checked = $(el).getInputs("radio", radioGroup).find( function(re) {return re.checked;} );
	return (checked) ? $F(checked) : null;
}


//-------------------------------------------------------------------------
//
// FUNCOES DE REQUEST
//
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
//---Funcao requestError--------------------------------------------------
//-------------------------------------------------------------------------
function requestError(){	

	alert("Erro de Request!" );

}

//-------------------------------------------------------------------------
//---Funcao trataProcessamento---------------------------------------------
//-------------------------------------------------------------------------
function trataProcessamento(pVal){



}


//-------------------------------------------------------------------------
//---Funcao requestReturn--------------------------------------------------
//-------------------------------------------------------------------------
function requestReturn(pObj){	

	if(document.getElementById('tempoID')!=null){
		$('tempoID').hide();
	}
	
	gProcessando = 0;	


	var root = pObj.responseXML.getElementsByTagName('actions')[0];

	var lActionsToDoLst = root.getElementsByTagName("actiontodo");

	for (var i = 0 ; i < lActionsToDoLst.length ; i++) {
	
		var lActionDoTo = lActionsToDoLst[i];
	
		var lAct = lActionDoTo.getElementsByTagName("actionoption")[0].firstChild.nodeValue;

		if(lAct == 'cACT_EXECFUNCTION'){
			lFunc = lActionDoTo.getElementsByTagName("func")[0].firstChild.nodeValue;
			eval( lFunc );
		}
		
		if(lAct == 'cACT_SHOW'){
			lId = lActionDoTo.getElementsByTagName("id")[0].firstChild.nodeValue;
			showHide(lId,true);
		}

		if(lAct == 'cACT_HIDE'){
			lId = lActionDoTo.getElementsByTagName("id")[0].firstChild.nodeValue;
			showHide(lId,false);
		}
		
		if(lAct == 'cACT_FIELDFOCUS'){
			lId = lActionDoTo.getElementsByTagName("id")[0].firstChild.nodeValue;
			lField = $( lId );
			lField.focus();
		}		

		if(lAct == 'cACT_SETCHECKRADIO'){
		
			lFormName = lActionDoTo.getElementsByTagName("formname")[0].firstChild.nodeValue;
			lId = lActionDoTo.getElementsByTagName("id")[0].firstChild.nodeValue;
			lVal = lActionDoTo.getElementsByTagName("val")[0].firstChild.nodeValue;
			setCheckRadioValue(lFormName,lId,lVal);
		}


		if(lAct == 'cACT_FIELDREADONLY'){
		
			lId = lActionDoTo.getElementsByTagName("id")[0].firstChild.nodeValue;
			lVal = lActionDoTo.getElementsByTagName("val")[0].firstChild.nodeValue;
	
			lField = $(lId);
			
			if( lVal == 0)
				lField.readOnly = false;
			else	
				lField.readOnly = true;
			
		}
		
		
		if(lAct == 'cACT_SETCSS'){
		
			lId = lActionDoTo.getElementsByTagName("id")[0].firstChild.nodeValue;
			lName = lActionDoTo.getElementsByTagName("name")[0].firstChild.nodeValue;
			lField = $(lId);
			lField.className  = lName;
		}		
		
	
		if(lAct == 'cACT_SETOPTIONLIST'){
		
			lId = lActionDoTo.getElementsByTagName("id")[0].firstChild.nodeValue;
			
			//Quando no retorno estoura o tamanho do nodo, o browser divide em mais de um nodo, por isso este laço
			var numNodos = lActionDoTo.getElementsByTagName("data").item(0).childNodes.length;
			var dados = "";
			var lDadosLst = lActionDoTo.getElementsByTagName("data");

			for(var j=0; j < numNodos;j++) {
				var lDado = lActionDoTo.getElementsByTagName("data").item(0).childNodes.item(j).nodeValue;
				dados = dados + lDado;
			}
			
			setFormOptionListField( lId,dados );	
		}
		
	
		if(lAct == 'cACT_SETHTMLDATA'){
			d = $(lActionDoTo.getElementsByTagName("id")[0].firstChild.nodeValue);
		
		
			//Quando no retorno estoura o tamanho do nodo, o browser divide em mais de um nodo, por isso este laço
			var numNodos = lActionDoTo.getElementsByTagName("data").item(0).childNodes.length;
			var dados = "";
			var lDadosLst = lActionDoTo.getElementsByTagName("data");
			for(var j=0; j < numNodos;j++) {
				var lDado = lActionDoTo.getElementsByTagName("data").item(0).childNodes.item(j).nodeValue;
				dados = dados + lDado;
			}
		
			d.innerHTML=dados;
			
		}



		if(lAct == 'cACT_RESETFIELDVALUES'){
			//Busca o nome do form
			lFormName = lActionDoTo.getElementsByTagName("formname")[0].firstChild.nodeValue;
			Form.reset(lFormName);
		}	
	

		if(lAct == 'cACT_SETCHECKBOX'){
			lAux = lActionDoTo.getElementsByTagName("valuelist")[0].firstChild.nodeValue;

			//separa campo valor
			lFieldNameValue = lAux.split("=");

			lFieldID = lFieldNameValue[0];
			lFieldValue = lFieldNameValue[1];

			lField = $(lFieldID);
			if(lFieldValue==0)
				lField.checked = false;
			else		
				lField.checked = true;
		}
		
		if(lAct == 'cACT_SETFIELDVALUES'){
			lValueList = lActionDoTo.getElementsByTagName("valuelist")[0].firstChild.nodeValue;
			setFormFieldsValue( lValueList );	
		}

		if(lAct == 'cACT_SETALERT'){
			lMsg = lActionDoTo.getElementsByTagName("message")[0].firstChild.nodeValue;
			alert( lMsg );	
		}

		if(lAct == 'cACT_SETBROWSERURL'){
			lUrl = lActionDoTo.getElementsByTagName("url")[0].firstChild.nodeValue;
			window.location= lUrl;	
		}

		if(lAct == 'cACT_SETBROWSERURLPOPUP'){
		
		

			lUrl = lActionDoTo.getElementsByTagName("url")[0].firstChild.nodeValue;	

			lLeft = lActionDoTo.getElementsByTagName("left")[0].firstChild.nodeValue;	

			lTop = lActionDoTo.getElementsByTagName("top")[0].firstChild.nodeValue;	

			lW = lActionDoTo.getElementsByTagName("w")[0].firstChild.nodeValue;	

			lH = lActionDoTo.getElementsByTagName("h")[0].firstChild.nodeValue;
			
			lmenuBar = lActionDoTo.getElementsByTagName("mb")[0].firstChild.nodeValue;
		
			lScrollBar = lActionDoTo.getElementsByTagName("sb")[0].firstChild.nodeValue;
	
			lResize = lActionDoTo.getElementsByTagName("rz")[0].firstChild.nodeValue;			


		
			var lParams='width='+lW+',height='+lH+',left='+lLeft+',top='+lTop + ',menubar='+ lmenuBar + ',scrollbars='+ lScrollBar + ',resizable='+ lResize ;
			newWindow=window.open(lUrl, 'Janela', lParams); 
		}
			
			
	}	


}

//-------------------------------------------------------------------------
//---Funcao makeRequestParams----------------------------------------------------
//-------------------------------------------------------------------------
function makeRequestParams(pRequestPath,pFormName,pControlEventName,pID){	
/*
	if(gProcessando==1){
	
		//Se o usuário clica no check, e está em processamento, não executa nenhuma ação, porém o check permanece
		//marcado. Sendo assim, esta condição visa desmarcar ou marcar o check, pois a operação não foi bem sucedida.
		lField = $(pID);
		switch(lField.type){
	
			case "checkbox":	
							
							if( lField.checked ) 
								lField.checked = false;
							else	
								lField.checked = true;
	
							break;
 		} 
 	
//		alert("Aguarde....em processamento!");	
		return;
	} 	
	gProcessando = 1;
*/
	
	if(document.getElementById('tempoID')!=null){
		l = document.body.clientWidth / 2;
		t = document.body.clientHeight /2;
	
		$('tempoID').show(); 
		$('tempoID').style.left = l;//screen.width/2; 
		$('tempoID').style.top = t;//screen.height/2;
	}
	

	var lFormFields = '';

	//Entra aqui caso seja um campo de formulário
	if(document.getElementById(pID)!=null){

		lField = $(pID);
		switch(lField.type){
	
			case "checkbox":
							if( lField.checked ) 
								lValue = "1";
							else	
								lValue = "0";
								
							//O ID é composto pelo ID +  '_' + o número da linha	
							lRow = pID.split("_");	
			
							lFormFields = "&pControlName=" + pID + "&pControlRow=" + lRow[1] + "&pControlValue=" + lValue;
						break;
						
						
			case "select-one":   
						
						lFormFields = "&pControlName=" + pID + "&pControlValue=" + lField.options[lField.selectedIndex].value;
						break;
						
						
			case "radio":
					
						//O ID é composto pelo ID +  '_' + o valor do radio	
						lData = pID.split("_");	
		
						lFormFields = "&pControlName=" + lData[0] + "&pControlValue=" + lData[1];
						break;									
	
	
		}
	}else{
	

		//Entra aqui caso seja um clique em uma imagem ,etc.

		//O ID é composto pelo ID +  '_' + params - Por enquanto trata um só	
		lControlVal = pID.split("_");
	
	
		if(lControlVal.length>1) 
			lFormFields = "&pControlValue=" + lControlVal[1];
		else	
			lFormFields = "&pControlValue=" + lControlVal[0];
	
	
	
	}
	
	var lRequestUrl = pRequestPath;
    var lRequestParams = "&pFormName=" + pFormName + "&pControlEventName=" + pControlEventName + lFormFields;
	
	//Pra nao dar erro no explorer, que coloca em cache a URl, adiciona um 
  	//campo data, isso faz com que cada vez que a  URL for chamada seja renovada	
	lRequestParams += '&ms='+ new Date().getTime();
	
	var myAjax = new Ajax.Request(lRequestUrl, {method: "post",encoding: "ISO-8859-1",parameters: lRequestParams,	onComplete: requestReturn , onFailure: requestError});
}


//-------------------------------------------------------------------------
//---Funcao makeRequest----------------------------------------------------
//-------------------------------------------------------------------------
function makeRequest(pRequestPath,pFormName,pControlEventName,pFunctionResultName,pSendFormFields){	
	/*		
	if(gProcessando==1){
		alert("Aguarde....em processamento!");	
		return;
	} 	
	gProcessando = 1;			
	
	if(document.getElementById('tempoID')!=null){
	
		l = document.body.clientWidth / 2;
		t = document.body.clientHeight /2;
	
		$('tempoID').show(); 
		$('tempoID').style.left = l;//screen.width/2; 
		$('tempoID').style.top = t;//screen.height/2;
	}
*/	
			
	var lFormFields = '';		
			
	if( pFunctionResultName == '')
		pFunctionResultName = 'requestReturn';
	
	if( pSendFormFields ){ 
		lFormFields = '&' + Form.serialize(pFormName);
	}		

	var lRequestUrl = pRequestPath;
    var lRequestParams = "&pFormName=" + pFormName + "&pControlEventName=" + pControlEventName + lFormFields;
	
	
	
	//Pra nao dar erro no explorer, que coloca em cache a URl, adiciona um 
  	//campo data, isso faz com que cada vez que a  URL for chamada seja renovada	
	lRequestParams += '&ms='+ new Date().getTime();
	
	var myAjax = new Ajax.Request(lRequestUrl, {method: "post",encoding: "ISO-8859-1",parameters: lRequestParams,	onComplete: requestReturn , onFailure: requestError});
}

//-------------------------------------------------------------------------
//---Funcao makeRequestParams----------------------------------------------------
//-------------------------------------------------------------------------
function makeSimpleRequestParams(pRequestPath,pControlEventName,pParams){	

	var lRequestUrl = pRequestPath;
    var lRequestParams = "&pControlEventName=" + pControlEventName + pParams;
	
	//Pra nao dar erro no explorer, que coloca em cache a URl, adiciona um 
  	//campo data, isso faz com que cada vez que a  URL for chamada seja renovada	
	lRequestParams += '&ms='+ new Date().getTime();

	var myAjax = new Ajax.Request(lRequestUrl, {method: "post",encoding: "ISO-8859-1",parameters: lRequestParams,	onComplete: requestReturn , onFailure: requestError});
}

//-------------------------------------------------------------------------
//---Funcao setDataBrowser-------------------------------------------------
//-------------------------------------------------------------------------
function setDataBrowser(pRequestPath,pComponentNameToFill){

	var lRequestUrl = pRequestPath;
    var lRequestParams = "&pComponentNameToFill=" + pComponentNameToFill;

	//Pra nao dar erro no explorer, que coloca em cache a URl, adiciona um 
  	//campo data, isso faz com que cada vez que a  URL for chamada seja renovada	
	lRequestParams += '&ms='+ new Date().getTime();
	
	var myAjax = new Ajax.Request(lRequestUrl, {method: "post",encoding: "ISO-8859-1",parameters: lRequestParams,	onComplete: requestReturn , onFailure: requestError});
}

//-------------------------------------------------------------------------
//---Funcao showHideDIV----------------------------------------------------
//-------------------------------------------------------------------------
function showHide(pID,pVal){

	if(document.getElementById(pID)!=null){
		
		if(pVal)
			$(pID).show();
		else	
			$(pID).hide();
	}
}

//-------------------------------------------------------------------------
//---Funcao setFormFieldsValue---------------------------------------------
//-------------------------------------------------------------------------
function setFormFieldsValue(pList){

	//separa campos
	lFieldList = pList.split("&");
	for(i=0;i < lFieldList.length ;i++){
		
		lAux = lFieldList[i];
//		alert( lAux );
		
		//separa campo valor
		lFieldNameValue = lAux.split("=");

		lFieldID = lFieldNameValue[0];
		lFieldValue = unescape( lFieldNameValue[1] );
		
//		alert(lFieldID + '-->' + lFieldValue);
		setFieldValue(lFieldID,lFieldValue);
	}	
	
}


//-------------------------------------------------------------------------
//---Funcao setFormListFieldValue---------------------------------------------
//-------------------------------------------------------------------------
function setFormOptionListField(pID,pData){

	var lFieldList =$(pID);


	//Formato:  texto||Valor||texto||valor


	// clear the did list 
	for (var count = lFieldList.length-1; count >-1; count--)
	{
		lFieldList.options[count] = null;
	}

	pData = unescape( pData );

	//separa campos
	lFieldData = pData.split("||");
	for(i=0;i < lFieldData.length ;i+=2){
		
		lFieldTexto = unescape( lFieldData[i] );
		lFieldValue = unescape( lFieldData[i+1] );
		
		lOptionItem = new Option( lFieldTexto, lFieldValue,  false, false);
		lFieldList.options[lFieldList.length] = lOptionItem;
	}	
	
}

//-------------------------------------------------------------------------
//---Funcao setFieldValue--------------------------------------------------
//-------------------------------------------------------------------------
function setFieldValue(pID,pValue){


	if(document.getElementById('pID')==null)
		 lField = parent.document.getElementById(pID);
	else
		 lField = $(pID);

	switch(lField.type){
		case "text": 
		case "textarea": 
		case "hidden": 
		case "checkbox": 
						lField.value = pValue;
						break;
						
		case "select-one": 							
						for(var i=0;i<lField.options.length;i++)
							if(lField.options[i].value == pValue){
								lField.options[i].selected = true;
								return;
							}
	}
}
	
//-------------------------------------------------------------------------
//---Funcao getFieldValue--------------------------------------------------
//-------------------------------------------------------------------------
function getFieldValue(pID,pValue){

	lField = $(pID);
	switch(lField.type){
		case "text": 
		case "textarea": 
		case "hidden": 
		case "checkbox": 
						return lField.value;
						break;
						
		case "select-one": 							
						for(var i=0;i<lField.options.length;i++)
							if(lField.options[i].value == pValue){
								return lField.options[lField.selectedIndex].value;
							}
	}
}

//-------------------------------------------------------------------------
//---Funcao setCheckRadioValue--------------------------------------------------
//-------------------------------------------------------------------------
function setCheckRadioValue(pFormName,pID,pValue){


	var lFieldList = Form.getInputs(pFormName,'radio',pID);


	for(var i=0;i<lFieldList.length ;i++){
		if ( lFieldList[i].value == pValue){
			lFieldList[i].checked = true;
			return;	
		}
	}		
	
}


//-------------------------------------------------------------------------
//
// FUNCOES DE MANIP. TABELAS
//
//-------------------------------------------------------------------------

var gCurrRowID = '';
var gCurrCelssClassName;


function rowDeselect(pID){

	if(document.getElementById(pID)==null)
		return;

	lRow = document.getElementById(pID); 
	
	for (i=0;i<lRow.cells.length;i++)
   	 	lRow.cells[i].className =gCurrCelssClassName;
}

function rowSelect(pID,pClassName){

	rowDeselect(gCurrRowID);
	gCurrRowID = pID;

	lRow = document.getElementById(pID);
	  
	
	gCurrCelssClassName = lRow.cells[0].className;
	
	
	for (i=0;i<lRow.cells.length;i++){
	    gCurrCelssClassName = lRow.cells[i].className;
 	 	lRow.cells[i].className =pClassName;
   }

}

//-------------------------------------------------------------------------
//
// FUNCOES DE POPUP DIV, IFRAME, etc..
//
//-------------------------------------------------------------------------

function deleteIframe(pID,pParent){

	if(pParent != null){	
		var ifr = pParent.document.getElementById(pID);//.style.height = 50;
   		ifr.parentNode.removeChild(ifr);
	}else{
	    var ifr = document.getElementById(pID);
	    ifr.parentNode.removeChild(ifr);
	}
}	


function createIframe (pID, width, height,url) {

	if(document.getElementById(pID)!=null){
		return;
	}

	var iframe;
	if (document.createElement && (iframe = document.createElement('iframe')) ) {
		iframe.name = iframe.id = pID;
		iframe.width = width;
		iframe.height = height;
//		iframe.style.position="absolute";
//		iframe.style.left="100";


		if(url!='')
			iframe.src = url;
		else	
			iframe.src = 'about:blank';

		document.body.appendChild(iframe);
	}
	return iframe;
}


function createUrlIframeCenter (pID, width, height,url,pOffSetX,pOffSetY) {

	if(document.getElementById(pID)!=null){
		return;
	}

	lAreaBrowser = 120;//100;


	var iframe;
	if (document.createElement && (iframe = document.createElement('iframe')) ) {

		iframe.name = iframe.id = pID;
		iframe.width = width;
		iframe.height = height;
		iframe.style.position="absolute";
		iframe.style.left= screen.width/2 - width/2 + pOffSetX;

		if (navigator.appName == "Microsoft Internet Explorer")
			iframe.style.top= eval( ( (screen.height/2 - height/2) + document.body.scrollTop)  - lAreaBrowser + pOffSetY) ;
		else
			iframe.style.top= eval( ( (screen.height/2 - height/2) + window.pageYOffset)  - lAreaBrowser + pOffSetY) ;

		if(url!='')
			iframe.src = url;
		else	
			iframe.src = 'about:blank';




		document.body.appendChild(iframe);
	}
	return iframe;
}






