/* #################### Funções Java ################### */
/* Criado por Anderson Böger Roveda                      */
/* Data: 01/08/2006                                      */
/* ##################################################### */

// Open Window
function OpenWindow (URL,Name,W,H,Scroll) {
  var WinLeft = (screen.width - W) / 2;
  var WinTop = (screen.height - H) / 2;
  WinProps = 'width='+W+',height='+H+',top='+WinTop+',left='+WinLeft+',scrollbars='+Scroll+',toolbar=0,location=0,directories=0,status=0,menuBar=0,resizable=0';
  Win = window.open(URL,Name,WinProps); 
  if (parseInt(navigator.appVersion) >= 4) {
    Win.window.focus();
  }
}

function OpenWindow2 (URL) {
  Win = window.open(URL,"");
  if (parseInt(navigator.appVersion) >= 4) {
    Win.window.focus();
  }
}
// -----

// Login do Index
function login () {
  var S = 'conteudo/login.php?login='+document.Form_Login.login.value+'&pass='+document.Form_Login.pass.value+'&uf='+document.Form_Login.uf.value;
  loadXMLDoc(S);
}
// -----

// Formatar Telefone
function Formatar_Telefone (Campo,e) {
  var key = '';
  var len = 0;
  var strCheck = '0123456789';
  var aux = '';
  var whichCode = (window.Event) ? e.which : e.keyCode;
  if (whichCode == 13 || whichCode == 8 || whichCode == 0) {
    return true; /*Enter backspace ou FN qualquer um que não seja alfa numerico*/
  }
  key = String.fromCharCode(whichCode);
  if (strCheck.indexOf(key) == -1) {
    return false; /*não é válido*/
  }
  aux =  Formatar_Telefone_Remove(Campo.value);
  len = aux.length;
  if (len >= 10) {
    return false; /*impede de digitar um telefone maior que 10*/
  }
  aux += key;
  Campo.value = Formatar_Telefone_Mont(aux);
  return false;
}

function Formatar_Telefone_Mont (Telefone) {
  var aux = len = '';
  len = Telefone.length;
  if (len <= 9) { tmp = 5; }
  else { tmp = 6; }
  aux = '';
  for(i = 0; i < len; i++) {
    if (i == 0) { aux = '('; }
    aux += Telefone.charAt(i);
    if (i+1 == 2) { aux += ')'; }
    if (i+1 == tmp) { aux += '-'; }
  }
  return aux;
}

function Formatar_Telefone_Remove (Telefone) {
  var strCheck = '0123456789';
  var len = i = aux = '';
  len = Telefone.length;
  for (i = 0; i < len; i++) {
    if (strCheck.indexOf(Telefone.charAt(i))!=-1) {
      aux += Telefone.charAt(i);
    }
  }
  return aux;
}
// -----

// Formatação para qualquer máscara 
function Formatar (src,mask) {
  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i);
  if (texto.substring(0,1) != saida) {
    src.value += texto.substring(0,1);
  }
}
// -----

// Formatar Data
function Formatar_Data(campo,teclapress) {
  var tecla = teclapress.keyCode;
  vr = campo.value;
  vr = vr.replace(".","");
  vr = vr.replace("/","");
  vr = vr.replace("/","");
  tam = vr.length + 1;
  if (tecla != 9 && tecla != 8) {
    if (tam > 2 && tam < 5) {
      campo.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
    }
    if (tam >= 5 && tam <= 10) {
      campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
    }
  }
}
// -----

// Função que Formata Dado
function FormataDado(campo,tammax,pos,teclapres){
	var tecla = teclapres.keyCode;
	vr = event.srcElement.value;
	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 == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 2 ){ event.srcElement.value = vr; }
		if ( tam > pos && tam <= tammax ){ event.srcElement.value = vr.substr( 0, tam - pos ) + '-' + vr.substr( tam - pos, tam ); }
	}
}

// Verificar se é numérico
function isNumeric (valor) {
	for( var i = 0 ; i < valor.length ; i++ )
		if( (valor.charAt( i ) < "0") || (valor.charAt( i ) > "9") )
			return false;
	return true;
}
// -----

// Função que Formata Valor
// Modificado por Anderson Böger Roveda - Testando no IE e FF
// Uso: onKeyDown="FormataValor(this,20,event);"
function FormataValor(valor,tammax,event) {
  var tecla = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  vr = valor.value;
  vr = vr.replace("/","");
  vr = vr.replace("/","");
  vr = vr.replace(",","");
  vr = vr.replace(".","");
  vr = vr.replace(".","");
  vr = vr.replace(".","");
  vr = vr.replace(".","");
  if (vr.length > 0) { vr = parseInt(vr,10); }
  vr = '' + vr;
  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 ) {
      if (tam == 1) { valor.value = "0,0" + vr ; }
      else if (tam == 2) { valor.value = "0," + vr ; }
      else { valor.value = "" ; }
    }
    if ((tam > 2) && (tam <= 5)) { valor.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); }
    if ((tam >= 6) && (tam <= 8)) { valor.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
    if ((tam >= 9) && (tam <= 11)) { valor.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)) { valor.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)) { valor.value = vr.substr( 0, tam - 15 ) + '.' + vr.substr( tam - 15, 3 ) + '.' + vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + ',' + vr.substr( tam - 8, 0 ) ; }
  }
}
// -----

function SaltaCampo (campo, prox, tammax, teclapres){
	var tecla = teclapres.keyCode;
	vr = event.srcElement.value;
	if( tecla == 109 || tecla == 188 || tecla == 110 || tecla == 111 || tecla == 223 || tecla == 108 || tecla == 194){		
		recuperaPai(event.srcElement.parentElement, "FORM")[campo].value = vr.substr( 0, vr.length - 1 ); 
	} else {
	
	 	vr = vr.replace( "-", "" );
	 	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 (tecla != 0 && tecla != 9 && tecla != 16 ){			
			if ( tam == tammax ){
				//VERIFICA SE O PROXIMO ELEMENTO EXISTE	
				if( typeof recuperaPai(event.srcElement.parentElement, "form")[prox] == "undefined"){
					//SELECIONA ELE MESMO
					recuperaPai(event.srcElement.parentElement, "form")[prox-1].focus();	
				} else {
					//SELECIONA O PROXIMO
					recuperaPai(event.srcElement.parentElement, "form")[prox].focus();					
				}
				
			}
		}
	}
}

function recuperaPai(elemento, nomeDoPai) {
   while (elemento.tagName != "FORM")
	   elemento = elemento.parentElement;
   return elemento;
}
// Salta Campo
function SaltaCampoFalse (campo,prox,tammax,teclapres){
	var tecla = teclapres.keyCode;
	vr = event.srcElement.value;
	if( tecla == 109 || tecla == 188 || tecla == 110 || tecla == 111 || tecla == 223 || tecla == 108 ){
		recuperaPai(event.srcElement.parentElement, "FORM")[campo].value = vr.substr( 0, vr.length - 1 ); }
	else{
	 	vr = vr.replace( "-", "" );
	 	vr = vr.replace( "/", "" );
	 	vr = vr.replace( "/", "" );
	 	vr = vr.replace( ",", "" );
	 	vr = vr.replace( ".", "" );
	 	vr = vr.replace( ".", "" );
	 	vr = vr.replace( ".", "" );
	 	vr = vr.replace( ".", "" );
	 	tam = vr.length;
	}
}
// -----

// Formatar
function Formatar_CPF(campo,tammax,teclapress) {
  var tecla = teclapress.keyCode;
  vr = campo.value;
  vr = vr.replace( "/", "" );
  vr = vr.replace( "/", "" );
  vr = vr.replace( ",", "" );
  vr = vr.replace( ".", "" );
  vr = vr.replace( ".", "" );
  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 = vr ; }
    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 ) ; }
  }
}
// -----

// AJAX
var Ajax_Req; /* Requisitado */
var Ajax_Res; /* Resposta    */
var Ajax_Car; /* Carregando  */

function loadXMLDoc (url,objRes,objCar) {
  Ajax_Req = null;
  Ajax_Res = objRes;
  Ajax_Car = objCar;
  if (window.XMLHttpRequest) {
    Ajax_Req = new XMLHttpRequest();
    Ajax_Req.onreadystatechange = processReqChange;
    Ajax_Req.open("GET",url,true);
    Ajax_Req.send(null);
  }
  else if (window.ActiveXObject) {
    try { Ajax_Req = new ActiveXObject("Msxml2.XMLHTTP.4.0"); }
	catch (e) {
	  try { Ajax_Req = new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
	  catch (e) {
	    try { Ajax_Req = new ActiveXObject("Msxml2.XMLHTTP"); }
        catch (e) {
	      try { Ajax_Req = new ActiveXObject("Microsoft.XMLHTTP"); }
		  catch (e) { Ajax_Req = false; }
		}
      }
    }
    if (Ajax_Req) {
      Ajax_Req.onreadystatechange = processReqChange;
      Ajax_Req.open("GET",url,true);
      Ajax_Req.send();
    }
  }
}

function processReqChange () {
  if (Ajax_Req.readyState != 4) { 
    //if (Ajax_Car) {
    //  document.getElementById(Ajax_Car).innerHTML = "<b>Loading ...</b>";
    //}
  } 
  else if (Ajax_Req.readyState == 4) {
    if (Ajax_Req.status == 200) {             
      var retorno = Ajax_Req.responseText;             
      retorno = retorno.replace(/\+/g," ")
      retorno = unescape(retorno)
      if (retorno) {
        if (Ajax_Res) {
          document.getElementById(Ajax_Res).innerHTML = retorno;
        }
        else {
          eval(retorno);
        }
	  }
      //if (Ajax_Car) {
      //  document.getElementById(Ajax_Car).innerHTML = "";
      //}
    }
	else {
      //alert("Houve um problema ao obter os dados.\n Provavelmente sua autenticação expirou.");
    }
  }
}
// -----

// Enter funciona como TAB
// Uso: onKeyPress="return alterEnterAsTab(this,event);"
function alterEnterAsTab (field,event) {
  var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  if (keyCode == 13) {
    var i;
    for (i = 0; i < field.form.elements.length; i++)
    if (field == field.form.elements[i]) break;
    i = (i + 1) % field.form.elements.length;
    field.form.elements[i].focus();
    return false;
  }
  else return true;
}
// -----