document.title = "AMPAC - " + document.title + " "; 
function ValidarValor( e ){
    if( !e ) {
    //if the browser did not pass the event information to the
    //function, we will have to obtain it from the event register
    
        if( window.event ) {
    //Internet Explorer
            e = window.event;
        } else {
    //total failure, we have no way of referencing the event
            return;
        }
    }
    e = getKeyCode(e);
//alert(e);
//    if( e > 31 && e < 48 || e > 57 && e < 96 || e > 105 )
    if( e > 31 && e < 48 || e > 57 && e < 96 || e > 105 || e == 15 || e == 14 )
    {
        return false;
    }
    else{
        if (e == 13 && arguments.length > 1 && arguments[1] && typeof(arguments[1]) == 'function' ) {
			eval('arguments[1]()');	
		}
		return true;
    }
}

function ValidarValorNumerico( e ){
    if( !e ) {
    //if the browser did not pass the event information to the
    //function, we will have to obtain it from the event register
    
        if( window.event ) {
    //Internet Explorer
            e = window.event;
        } else {
    //total failure, we have no way of referencing the event
            return;
        }
    }
	e = getKeyCode(e);

//    if( e > 31 && e < 48 || e > 57 && e < 96 || e > 105 )
	
    if ( ( e >= 48 && e <=57 ) || ( e == 8)){
        return true;
    } else if (e == 13 && arguments.length > 1 && arguments[1] && typeof(arguments[1]) == 'function' ) {
		eval('arguments[1]()');	
		return true;
	} else {
        return false;
    }
}

function FormataValorReal(campo,tammax,e) {
	if (!ValidarValorNumerico( e )){
		return false;	
	}
	if( !e ) {
    //if the browser did not pass the event information to the
    //function, we will have to obtain it from the event register
    
        if( window.event ) {
    //Internet Explorer
            e = window.event;
        } else {
    //total failure, we have no way of referencing the event
            return;
        }
    }
	var tecla = getKeyCode(e);

	vr = campo.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

	if (tecla == 8 ){	tam = tam - 1 ; }
		
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 2 ){ 
	 		campo.value = "0," + vr ; } 
		/*else {
				if (vr.substring(0,1) == "0"){
					vr = vr.substring(1,vr.length+1);
					alert("val:"+vr)
				}
			} */
			
		if (vr.substring(0,1) == "0"){
			vr = vr.substring(1,vr.length+1);
			tam = tam - 1;
			
		}
	 	if ( (tam > 2) && (tam <= 5) ){
	 		campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 12) && (tam <= 14) ){
	 		campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 15) && (tam <= 17) ){
	 		campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;}
	}
		/*
	for (var ct = 0; ct < document.form.elements.length; ct++) {
		if (document.form.elements[ct].name == document.form.elements[campo].name) {
			if ( !teclapres.shiftKey && tecla == 9 && document.form.elements[ct+1] && document.form.elements[ct+1].name == "senhaConta" && document.applets['tclJava'] ){
				//document.applets['tclJava'].setFocus();
			}	
		}
	}*/
}

function ajustaMoedaBanco(vr,tipo){
	if (vr != ""){
		//alert("inicial: " + vr);
		if (tipo == 0) {
			vr = replaceChars(vr,".","");
			vr = replaceChars(vr,",",".");
		
		} else if(tipo == 1){
	//		vr = replaceChars(vr,".","");
	//		vr = replaceChars(vr,",","");

			if (vr.indexOf(",")>-1) {
				out = ",";

			} else if (vr.indexOf(".")>-1) {
				out = ".";
			} else {
				vr = vr + ",00";
				out = ",";
			}
				//alert(out)	
	
			vrA = vr.split(out);
			//alert("meso: " + vrA);
			if (vrA.length != 1 && vrA[1].length == 1) {
				vrA[1] = vrA[1] + "0";
			}
			vr = vrA.join(",");
			vr = replaceChars(vr,",","");
			//alert("meso2: " + vr);
			tam = vr.length;
			if ( tam <= 2 ){ 
				vr = "0," + vr ; }
			if ( (tam > 2) && (tam <= 5) ){
				vr = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
			if ( (tam >= 6) && (tam <= 8) ){
				vr = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
			if ( (tam >= 9) && (tam <= 11) ){
				vr = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
			if ( (tam >= 12) && (tam <= 14) ){
				vr = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
			if ( (tam >= 15) && (tam <= 17) ){
				vr = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;}
			
		}
	
		//alert("final: " + vr);
	} // fim do if...
	return vr;
}


function ajustaMoedaBancoSemPonto(vr,tipo){
	if (vr != ""){
		//alert("inicial: " + vr);
		if (tipo == 0) {
			vr = replaceChars(vr,".","");
			vr = replaceChars(vr,",",".");
		
		} else if(tipo == 1){
	//		vr = replaceChars(vr,".","");
	//		vr = replaceChars(vr,",","");

			if (vr.indexOf(",")>-1) {
				out = ",";

			} else if (vr.indexOf(".")>-1) {
				out = ".";
			} else {
				vr = vr + ",00";
				out = ",";
			}
				//alert(out)	
	
			vrA = vr.split(out);
			//alert("meso: " + vrA);
			if (vrA.length != 1) {
				if (vrA[1].length == 1){
					vrA[1] = vrA[1] + "0";
				}
				
			}
			vr = vrA.join(",");
			vr = replaceChars(vr,",","");
			//alert("meso2: " + vr);
			tam = vr.length;
			if ( tam <= 2 ){ 
				vr = "0," + vr ; }
				else {
				vr = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }			
		}
	
		//alert("final: " + vr);
	} // fim do if...
	return vr;
}



function validaNum(val){
var er;
	er = /[^0-9],.+/;
	if ( er.test(val) ){
		return false;
	} else {
		return true;
	}
}

// #######################################adaptado de diego nunes - dnunes[em]gmail.com##########
function gE(tI) { /* theID */
  if (document.getElementById) { return document.getElementById(tI); }
  else if (document.all) { return document.all[tI]; } else return false;
}

function gEv(tI) { /* theID */
  if (document.getElementById) { return document.getElementById(tI).value; }
  else if (document.all) { return document.all[tI].value; } else return false;
}

function wgE(tI) { /* theID */
  if (document.getElementById) { return window.opener.document.getElementById(tI); }
  else if (document.all) { return window.opener.document.all[tI]; } else return false;
}

function wgEv(tI) { /* theID */
  if (document.getElementById) { return window.opener.document.getElementById(tI).value; }
  else if (document.all) { return window.opener.document.all[tI].value; } else return false;
}

function cE(tN, tId, tCN, tIH) { /* tagName, id, className, innerHTML */ if (!document.createElement) return false;
  var nE, tER; nE=document.createElement(tN); if (valorValido(tId)) { nE.id=tId; }
  if (valorValido(tCN)) { nE.className=tCN; } if (valorValido(tIH)) { nE.innerHTML=tIH; } return nE;
}
function cTN(tV) { /* theValue */ return (tV && document.createTextNode) ? document.createTextNode(tV) : false; }
function rEs(tO) { var i, n; //removeElements. Recebe um objeto ou uma coleção deles, para remover do documento
  if (!tO) { return false; } if (!tO.nodeName) { if (tO.length) for (n=tO.length; n--;) rEs(tO[n]); }
  else tO.parentNode.removeChild(tO);
}
function adEvento(tO, tE, tF) { //Adaptado da função de Scott Andrew - scottandrew.com
  if (tO.addEventListener) { tO.addEventListener(tE, tF, true); return true; }
  else if (tO.attachEvent) { return tO.attachEvent('on'+tE, tF); }
  else { alert('Erro!'); return false; }
}
function remEvento(tO, tE, tF) { //Adaptado da função de Scott Andrew - scottandrew.com
  if (tO.removeEventListener) { tO.removeEventListener(tE, tF, false); return true; }
  else if (tO.detachEvent) { return tO.detachEvent("on"+tE, tF); }
  else { alert('Erro!'); return false; }
}
function getSrc(e) { /* event */ return (e.target) ? e.target : (window.event && event.srcElement) ? event.srcElement : false; }
function getKeyCode(e) { /* event */ return (e.which) ? e.which : (window.event && event.keyCode) ? event.keyCode : false; }
function valorValido(tV) { /* theValue */ var tER=/[a-z0-9]/gi; return tER.test(tV); }
function insertAfter(tN, nN) { /* tNode, newNode */ var tP=tN.parentNode;
  while ((tN=tN.nextSibling) && tN.nodeType != 3) { /* ... */ }
  if (tN) { tP.insertBefore(tN, nN); } else { tP.appenChild(nN); } return tP;
}
function firstOfType(tO, tT) { /* theObject, theTag */ var i, n, tCs;
  for (i=0, n=(tCs=tO.childNodes).length; i<n; i++) {
    if (tCs[i].nodeType==1 && tCs[i].nodeName.toLowerCase()==tT.toLowerCase()) return tCs[i];
  } return false;
}
function lastOfType(tO, tT) { /* theObject, theTag */ var i, tCs;
  for (i=(tCs=tO.childNodes).length; (i--)>0; ) {
    if (tCs[i].nodeType==1 && tCs[i].nodeName.toLowerCase()==tT.toLowerCase()) return tCs[i];
  } return false;
}
function nextOfType(tO, tT) { /* theObject, theTag */
  while (tO=tO.nextSibling) { if (tO.nodeType==1 && tO.nodeName.toLowerCase()==tT.toLowerCase()) return tO; }
  return false;
}
function previousOfType(tO, tT) { /* theObject, theTag */
  while (tO=tO.previousSibling) { if (tO.nodeType==1 && tO.nodeName.toLowerCase()==tT.toLowerCase()) return tO; }
  return false;
}
function sW(tS, sS) { /* [Bool] StartsWith: theString, SubString */
  return (tS && sS && tS.substring(0,sS.toString().length) == sS.toString()) ? true : false;
}
function getTok(tokList, tIndex, tDiv, tPattern) { /* tokList, theIndex, tDivisor, tPattern (Filtro [RegExp]) */
  if (!tPattern) { tPattern='.'; } var tER, tL, i, n, tMs=0; tER=new RegExp(tPattern); tL=tokList.split(tDiv);
  for (i=0, n=tL.length; i<n; i++) { if (tER.test(tL[i])) tMs++; if (tMs==tIndex) { return tL[i].toString(); } }
  return false;
}

function limpaFilhosElemento(obj){

	while (obj.childNodes.length > 0){
		//remove o primeiro no dos resultados antigos
		obj.removeChild(obj.childNodes[0]);
	}
		
}
// #######################################ate aqui.##########


function selectObj(idObj){
	
	if ( isVazio(idObj)){
		return false;
	}
	if ( typeof(idObj) == "string" ) {
		gE(idObj).select();
	} else if ( typeof(idObj) == "object" ) {
		idObj.select();
	}
	
}

function focusObj(idObj){
	
	if ( isVazio(idObj)){
		return false;
	}
	if ( typeof(idObj) == "string" ) {
		gE(idObj).focus();
	} else if ( typeof(idObj) == "object" ) {
		idObj.focus();
	}
	
}

function isVazio(prm){
	return (prm == "");
}

/* 
 * Esta função faz a limpeza do objeto informado.
 * O idObj pode ser uma string ou array de strings.
 * No primeiro caso, ele age especificamente para 
 * cada um dos casos: select, input(text,hidden,radio,checkbox)
 * e div. No segundo caso, ele percorre o array informado 
 * e chama esta mesma função repassando cada posição do array
 * de cada vez para executar a tarefa específica.
 * 
 *  By Jorge Oliveira em Jan/2007
 *  
 */
function clearValObj(idObj){
	var obj;
	
	if ( isVazio(idObj)){ //Critica se está vazio.
		return false;
	}
	
	if ((idObj instanceof Array)){ //Verifica se o prm é uma instância do objeto Array
		
		for(var i=0;i<idObj.length;i++){
				
			clearValObj(idObj[i]);
			
		}
		return true;
	}
	
	if ( typeof(idObj) == "string" ) {
			
		obj = gE(idObj);
		
	} else if ( typeof(idObj) == "object" ) {
		
		obj = idObj;
		
	} // Fim do If...
	
	if (obj.tagName == "SELECT") {
	
		obj.selectedIndex = 0;	
	
	} else if (obj.tagName == "INPUT") {
		
		var tipoObj = obj.type.toLowerCase();
		
		if ( (tipoObj == "text") || ( tipoObj == "hidden" ) || ( tipoObj == "password" ) ) {
			
			obj.value = "";
	
		} else if ( (tipoObj == "radio") || tipoObj == "checkbox" ){
	
			obj.checked = false;			
	
		} // Fim do If...
		
	} else if (obj.tagName == "DIV"){
	
		obj.innerHTML = "";
		
	} else if (obj.tagName == "TEXTAREA"){
	
		obj.value = "";
	
	} // Fim do If...
	
} // Fim da Função clearValObj


//-------------------BEGIN AJAX-----------------------
var req; 
var acaoajax;
var action;
var retorno;


document.write("<div id=\"divCarreg\"  " + 
			   "style=\"position:absolute;left:668px;top:30px;width:87px; " + 
			   "height:23px;z-index:1; " + 
			   "font-family: Arial, Helvetica, sans-serif;font-size: 12px; " + 
			   "font-weight: bold;color: #FFFFFF;background-color: #FF0000; " + 
			   "visibility: hidden;text-align:center;vertical-align:middle; " + 
			   "\" >Carregando...</div>");


/*document.write("<div id=\"divCarreg\"  " + 
			   "style=\"position:absolute;left:668px;top:30px;width:87px; " + 
			   "height:23px;z-index:100; " +  
			   "visibility: hidden;text-align:center;vertical-align:middle; " + 
			   "\" ><img src=\"ajxLoad.gif\" alt=\"\" width=\"16\" height=\"16\" /></div>");
*/
// Funçao para iniciarmos o Ajax no browser do cliente.
function openAjax() {

		var ajax;
		
		try{
			ajax = new XMLHttpRequest(); // XMLHttpRequest para browsers decentes, como: Firefox, Safari, dentre outros.
		}catch(ee){
			try{
				ajax = new ActiveXObject("Msxml2.XMLHTTP"); // Para o IE da MS
			}catch(e){
				try{
					ajax = new ActiveXObject("Microsoft.XMLHTTP"); // Para o IE da MS
				}catch(E){
					ajax = false;
				}
			}
		}
		return ajax;
}

function execAJAX(url,acao) {
			var ajax = openAjax(); // Inicia o Ajax.
			ajax.open("GET", url, true); // Envia a página que realizará a consulta propriamente dita
			/* verifica se o terceiro argumento [2] existe e é uma funçao. */
			if ( (arguments.length > 2) && (arguments[2]) && (typeof(arguments[2]) == 'function') ){
				var func = arguments[2];
			}
			
			ajax.onreadystatechange = function() {
				if(ajax.readyState == 1) { // Quando estiver carregando, exibe uma camada: Carregando...
					gE('divCarreg').style.visibility = "visible";
				}
				if(ajax.readyState == 4) { // Quando estiver tudo pronto.
					
					if(ajax.status == 200) {
						var resultado = ajax.responseText; // Coloca o resultado (da busca) retornado pelo Ajax nessa variável (var resultado).
						resultado = resultado.replace(/\+/g," "); // Resolve o problema dos acentos (saiba mais aqui: http://www.plugsites.net/leandro/?p=4)
						resultado = unescape(resultado); // Resolve o problema dos acentos
						gE('divCarreg').style.visibility = "hidden"; //Oculta o "Carregando..."
						if (func){

							eval('func(resultado)');
							
						} else {
							
							retornaAJAX(resultado,acao); // Manda o resultado para uma funç?o de retorno.
						}
					} else {
						
						gE('divCarreg').innerHTML = "Erro no AJAX.";
						alert("Ocorreu um erro no AJAX ("+ ajax.statusText + ")"); //Caso ocorram erros, exibe um aviso.
						gE('divCarreg').style.visibility = "hidden"; //Oculta o "Carregando..."
					}
				}
			} 
			ajax.send(null); // submete
}
//-----------------------------END AJAX--------------------------

var comboGLOBAL;
function preencheCbo(retorno){
	var Line = retorno.split("||");
	
	var combo = comboGLOBAL;
				combo.length = 0;
				combo.length = 1;
				
				
				for (iLine = 1; iLine < Line.length; iLine++){
					var arrLine = Line[iLine].split(";;");

					if (arrLine[0] != '') {
						var i = combo.length;
						combo.length = i + 1;
						combo.options[i].value = arrLine[0];
						combo.options[i].text =  arrLine[1];
					} //fim do if...
				} //fim do for...
				
				combo.options[0].text = ("-- Selecione --");	
				combo.options[0].value = "";
				combo.selectedIndex = 0;
}

function preencheCampo(retorno){
	if (comboGLOBAL.type == "text"){
		comboGLOBAL.value = retorno;
	} else {
		comboGLOBAL.innerHTML = retorno;
	}
}

function preencheCboInfo(val,idCbo,selec){
	var Line = val
	
	var combo = gE(idCbo);
				combo.length = 0;
				combo.length = 1;
				
				
				for (iLine = 0; iLine < Line.length; iLine++){
					var arrLine = Line[iLine].split(";;");

					if (arrLine[0] != '') {
						var i = combo.length;
						combo.length = i + 1;
						combo.options[i].value = arrLine[0];
//						combo.options[i].text =  arrLine[0] + " - " + arrLine[1];
						combo.options[i].text =  arrLine[1];
					} //fim do if...
				} //fim do for...
				
				combo.options[0].text = ("-- Escolha --");	
				combo.options[0].value = "";
				if (selec != "") {
					combo.selectedIndex = selec;
				} else {
					combo.selectedIndex = 0;
				} 
}

function cadEmailNewsletter(){
	var txtEmail = gEv('txtEmailNewsletter');
	
	var acao = "cadEmailNewsletter";
	var url = "ac.asp?a=" + acao;
		url += "&txtEmail="+txtEmail;
//	window.open(url)
	execAJAX(url,acao,retornaCadEmailNewsletter)
}

function retornaCadEmailNewsletter(ret){
		
	alert(ret);
	clearValObj("txtEmailNewsletter");
}

function confereUS(){

	var us = gEv('txtUs');
	var ps = gEv('txtPs');

	if (isVazio(us)){
		alert("Favor preencher o campo Usuário para continuar.");
		focusObj('txtUs');
		return false;
	} else if (isVazio(ps)){
		alert("Favor preencher o campo Senha para continuar.");
		focusObj('txtPs');
		return false;
	}

	var acao = "confereUS";
	var url = "ac.asp?a=" + acao;
		url += "&us="+us;
		url += "&ps="+ps;
//	window.open(url)
//	execAJAX(url,acao,retornaConfereUS);
	gE('formLog').submit();
}

function retornaConfereUS(ret){
	ret1 = ret.split("|||");
	if (ret1[0] == "True"){
		alert(ret1[1]);
		clearValObj(["txtUs","txtPs"]);
	} else {
		clearValObj(["txtUs","txtPs"]);
		focusObj('txtUs');
		alert(ret1[1]);
		
	}
	
}