//onkeypress='evita_letra(event)' onKeyDown='FormataData(this.name, this.form.name, event);' onchange="testa_data(this.value, this.form.name, this.name);" maxlength="10"
function testa_data(dta, formname, campo) { // função complementar para testar a validade da data
	if(!isdate(dta)) {
		alert('Digite uma data válida');
		document[formname][campo].value = '';
		document[formname][campo].focus();
		return false;
	}
}

function FormataData(campo,formname,teclapres) { // Máscara para os campos de data
	var tecla = teclapres.keyCode;
	vr = document[formname][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 )
			document[formname][campo].value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		if ( tam >= 5 && tam <= 10 )
			document[formname][campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 
	}
}

function isdate2(obj){
var mes, ano;
var retval = false;
ano=obj.substring(3,7);
//alert(obj);
if (obj != ""){
   if (obj.length != 7 || obj.substring(2,3)!="/") {         
   		retval = false;
   }
   else {     
      mes = obj.substring(0,2);
      if (nochars(mes) && parseInt(mes,10) > 0 && parseInt(mes,10) <= 12){
         mes = parseInt(mes,10);
         if (nochars(ano) && parseInt(ano,10) >= 0000) {
            if ((parseInt(ano,10) % 4) == 0) {
               bissexto = 1;
            }
            else {
               bissexto = 0;
            }
			retval = true;
         } else {
		 	retval = false;
		 }
      } else {
	  		retval = false;
	  }
   }
}
else {
     retval = true;
}
//alert(retval);
return retval;
}

//Verifica se é uma data válida
function isdate(obj){
var mes, dia, ano;
var retval = false;
ano=obj.substring(6,10);
//alert(obj);
if (obj != ""){
   if (obj.length != 10 || obj.substring(2,3)!="/" || obj.substring(5,6)!="/") {         
   }
   else {     
      mes = obj.substring(3,5);
      if (nochars(mes) && parseInt(mes,10) > 0 && parseInt(mes,10) <= 12){
         mes = parseInt(mes,10);
         if (nochars(ano) && parseInt(ano,10) >= 0000) {
            if ((parseInt(ano,10) % 4) == 0) {
               bissexto = 1;
            }
            else {
               bissexto = 0;
            }
            dia = obj.substring(0,2);
            if (nochars(dia)){
               dia = parseInt(dia,10);
               if (((mes==1 || mes==3 || mes==5 || mes==7 || mes==8 || mes==10 || mes==12) && (dia >= 1 && dia <= 31)) ||
                  ((mes==4 || mes==6 || mes==9 || mes==11) && (dia >= 1 && dia <= 30)) ||
                  (mes==2 && dia >= 1 && dia <= (28 + bissexto))) {
                  retval=true;
               }
            }
         }
      }
   }
}
else {
     retval = true;
}
//alert(retval);
return retval;
}

// Verifica se existem letras na variavel
function nochars(strval){
  var retval = true;
  for (var i = 0; i < strval.length; i++){
     if (strval.substring(i,i+1) < "0" || strval.substring(i,i+1) > "9"){
        retval=false;
        break;
     }
  }
  return retval;
}

// Não permite digitar letras em um input type=text
function evita_letra(tecla) {
	if (tecla.keyCode < 45 || tecla.keyCode > 57) 
		tecla.returnValue = false;
}

function FormataValor(tammax,teclapres, tf) { // formata os campos com a máscara valor
	var tecla = teclapres.keyCode;
	vr = tf.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 ){ 
	 		tf.value = vr ; }
	 	if ( (tam > 2) && (tam <= 5) ){
	 		tf.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		tf.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		tf.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) ){
	 		tf.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) ){
	 		tf.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 ) ;}
	}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function isANumber(number) {
  answer = 1;

  if (!parseFloat(number)) {
    //the first digit wasn't numeric
    answer = 0;
  } else {
    //the first digit was numeric, so check the rest
    for (var i=0; i<number.length; i++) {
      if ((number.charAt(i) != "0")
        && (!parseFloat(number.charAt(i)))) {
          answer = 0;
          break;
      }
    }
  }
  if (answer == 1) {
    orderPlaced = true;
  }
  if (number == 0) answer = 1;
  return answer;
}

function pega_valor(campo) {
			var ret = 0;
    		for (i = 0; i < document.cadastro_projeto.elements.length; i++) {
    			if (document.cadastro_projeto.elements[i].name == campo && document.cadastro_projeto.elements[i].value != "" && document.cadastro_projeto.elements[i].checked) {
    				ret = document.cadastro_projeto.elements[i].value;
    				break;
    			} else {
    				ret = 0;
    			}
    		}
    	return ret;
}

function ToggleAll(tf,checked) {
    //alert(document.forms["cadastro_regiao"].action);
	len = document.forms[tf].elements.length;
    var i = 0;
    for(i = 0; i < len; i++) {
        document.forms[tf].elements[i].checked = checked;
    }
}

function fixWin() {
			 var x = 400;
			 var y = 150;
			 window.moveTo(parseInt((screen.availWidth - x) / 2),parseInt((screen.availHeight - y) / 2));
		}

function verifica_mail(valor, formname, campo){
   if (valor != "")
        {
         if (valor.indexOf("@") == -1)
        {
                    //erro(campo, "email", "e-mail inválido.");
                        alert("E-Mail inválido!");
                        document[formname][campo].focus();
                        document[formname][campo].value = "";
                        return false;
            //returnVal = false;
         }
         else
         {
            if (valor.indexOf(".") == -1)
         {
                     //erro(campo, "email", "e-mail inválido.");
                         alert("E-Mail inválido!");
                         document[formname][campo].focus();
                         document[formname][campo].value = "";
             return false;
            //returnVal = false;
          }
                }
        }
return true;
}

function val_cnpj(valor,formname,campo) {
	numero = valor;
	dig_1 = 0;
	dig_2 = 0;
	controle_1 = 5;
	controle_2 = 6;
	
	if ((numero.length != 18)  || (numero.substring(2, 3) != ".") || (numero.substring(6, 7) != ".") || (numero.substring(10, 11) != "/") || (numero.substring(15, 16) != "-")) {
	     alert("CNPJ inválido ! Formato: xx.xxx.xxx/xxxx-xx");
	     	document[formname][campo].focus();
			document[formname][campo].value = "";
	     return false;
	}
	else { 
	numero = (numero.substring(0, 2)) + "" + (numero.substring(3, 6)) + "" + (numero.substring(7, 10))+ "" + (numero.substring(11, 15)) + "" + (numero.substring(16, 18)) 
	   for ( i=0 ; i < 12 ; i++) {
	        dig_1 = dig_1 + parseFloat(numero.substring(i, i+1) * controle_1);
	          controle_1 = controle_1 - 1;
	        if (i == 3) {
	           controle_1 = 9;
	        }
	     }
	   resto = dig_1 % 11;
	   dig_1 = 11 - resto;
	   if ((resto == 0) || (resto == 1)){
	        dig_1 = 0;
	   }
	   for ( i=0 ; i < 12 ; i++) {
	        dig_2 = dig_2 + parseInt(numero.substring(i, i+1) * controle_2);
	          controle_2 = controle_2 - 1;
	        if (i == 4) {
	           controle_2 = 9;
	        }
	   }
	   dig_2 = dig_2 + (2 * dig_1);
	   resto = dig_2 %11;
	   dig_2 = 11 - resto;
	   if ((resto == 0) || (resto == 1)){
	        dig_2 = 0;
	   }
	   dig_ver = (dig_1 * 10) + dig_2;
	   if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))) {
	          alert("CNPJ inválido ! Formato: xx.xxx.xxx/xxxx-xx");
	          document[formname][campo].focus();
			  document[formname][campo].value = "";
	          return false;
	   }
	  }
	 return true;
}


//Valida o cpf 
function val_cpf(valor, formname, campo) {
	dig_1 = 0;  dig_2 = 0;  controle_1 = 10;
	controle_2 = 11; lsucesso = 1; numero = valor;
	if ((numero.length != 12) || (numero.substring(9, 10) != "-")) {
		alert("CPF inválido! Formato: 000000000-00) ");
		document[formname][campo].focus();
		document[formname][campo].value = "";
		return false;
	}
	else 
	{
		for ( i=0 ; i < 9 ; i++) 
		{
			dig_1 = dig_1 + parseInt(numero.substring(i, i+1) * controle_1);
			controle_1 = controle_1 - 1;
		}
		
		resto = dig_1 % 11;
		dig_1 = 11 - resto;
		if ((resto == 0) || (resto == 1)) {
			dig_1 = 0;
		}
		for ( i=0 ; i < 9 ; i++) {
			dig_2 = dig_2 + parseInt(numero.substring(i, i + 1) * controle_2);
			controle_2 = controle_2 - 1;
		}
		dig_2 = dig_2 + 2 * dig_1;
		resto = dig_2 % 11;
		dig_2 = 11 - resto;
		if ((resto == 0) || (resto == 1)) {
			dig_2 = 0;
		}
		dig_ver = (dig_1 * 10) + dig_2;
		if (dig_ver != parseFloat(numero.substring(numero.length-2, numero.length))) 
		{
			alert("CPF inválido !");
			document[formname][campo].focus();
			document[formname][campo].value = "";
			return false;
		}
	}

	return true;
}

// função para abrir uma janela Pop-UP
function abre_pop(turl,twindow,tparams) {
	window.open(turl,twindow,tparams);
}

function somenteNumeros(tecla) {
// Deixa tbm teclas backSpace, delete e setinhas.
	if((tecla.keyCode < 48 || tecla.keyCode > 57) && (tecla.keyCode > 105 || tecla.keyCode < 96) && (tecla.keyCode < 37 || tecla.keyCode > 41) && tecla.keyCode != 8 && tecla.keyCode != 46)
		tecla.returnValue = false;
}

function FormataCPF(Campo, teclapres){
	var tecla = teclapres.keyCode;
	
	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1;
	
	if (tecla != 9 && tecla != 8 && tecla != 46){
		if (tam > 3 && tam < 7)
			Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
		if (tam >= 7 && tam <10)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);
		if (tam >= 10 && tam < 12)
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,tam-9);
		}
}