function TestarVazio(a) {
a = LimparEspacos(a);
if (a=="") {
    return true;}
else {
   return false;}    
}

function LimparEspacos(s) 
{
  while(s.charAt(0)==' ')   
     s=s.substring(1,s.length);  
return (s);
}

function isdigit (c)
{
 return ((c >= "0") && (c <= "9"))}


function ValidaEmail(email) {
        var achou_ponto=false;
        var achou_arroba=false;
        var achou_caracter=false;

        for (var i=0; i<email.length; i++) {
                if (email.charAt(i)=="@")
                { 
                  if (email.charAt(i+1)==".")
                  	achou_arroba=false;
                  else
                    achou_arroba=true;
                  
                }
                else if (email.charAt(i)==".") achou_ponto=true;
                else if (email.charAt(i)!=" ") achou_caracter=true;
        }
        if (email.charAt(0)=="@") {
           achou_arroba=false;
        }

        if((email.charAt(0)=="W" || email.charAt(0)=="w") &&
           (email.charAt(1)=="W" || email.charAt(1)=="w") &&
           (email.charAt(2)=="W" || email.charAt(2)=="w") &&
           (email.charAt(3)=="."))
        {
            achou_ponto=false;
            achou_caracter=false;
        }
        if(email.charAt(email.length-1)==".")
        {
            achou_ponto=false;
        }	
        return (achou_ponto && achou_arroba && achou_caracter);
}



function ValidaEmail2(email) {
        var achou_ponto=false;
        var achou_arroba=false;
        var achou_caracter=false;
        var achou_dominio=false;
        var sDominio;

        for (var i=0; i<email.length; i++) {
                if (email.charAt(i)=="@")
                { 
                  if (email.charAt(i+1)==".")
                  	achou_arroba=false;
                  else
                    achou_arroba=true;
                  sDominio = LimparEspacos(email.substring(i+1,email.length)) ; 
                  if (sDominio.toLowerCase() != "fusan.com.br" && sDominio.toLowerCase() != "fundacaosanepar.org.br")
                      achou_dominio = false;
                  else
                      achou_dominio = true;                  
                }
                else if (email.charAt(i)==".") achou_ponto=true;
                else if (email.charAt(i)!=" ") achou_caracter=true;
        }

        if((email.charAt(0)=="W" || email.charAt(0)=="w") &&
           (email.charAt(1)=="W" || email.charAt(1)=="w") &&
           (email.charAt(2)=="W" || email.charAt(2)=="w") &&
           (email.charAt(3)=="."))
        {
            achou_ponto=false;
            achou_caracter=false;
        }
        if(email.charAt(email.length-1)==".")
        {
            achou_ponto=false;
        }	
        return (achou_ponto && achou_arroba && achou_caracter && achou_dominio);
}


// Aqui inicia as funções de teste do CNPJ

function isNUMB(c)
	{
	if((cx=c.indexOf(","))!=-1)
		{		
		c = c.substring(0,cx)+"."+c.substring(cx+1);
		}
	if((parseFloat(c) / c != 1))
		{
		if(parseFloat(c) * c == 0)
			{
			return(1);
			}
		else
			{
			return(0);
			}
		}
	else
		{
		return(1);
		}
	}

function LIMP(c)
	{
	while((cx=c.indexOf("-"))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf("/"))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf(","))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf("."))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf("("))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf(")"))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf(" "))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	return(c);
	}


function isalpha(c)
{
 if (((c >= 'a') && (c <= 'z')) ||
     ((c >= 'A') && (c <= 'Z')))
 	 return true;
 else  
 	 return false;
}

function SoNumerosLetras(s)
{
 for (var i=0;i<s.length;i++) { 
 	  c = s.substring(i,i+1); 
	  if (!isdigit(c) && !isalpha(c)) 
	  	  return false;	  
 }
 return true;
}

function ValidaValor(s, prec)
{
 if (s.length - s.lastIndexOf(",") != (prec + 1)) 
 	 return false;
 return true;
}


function ValidaNum(s)
{
 var c;
 for (var i=0;i<s.length;i++) { 
 	  c = s.substring(i,i+1); 
          if (c != ",") {
	     if (!isdigit(c) || c == ".") 
             {
	  	     return false;	  
             }
          }
 }
 return true;
}

function Trocavir(s)
{
 var c;
 var numero="";
 for (var i=0;i<s.length;i++) { 
   c = s.substring(i,i+1); 
   if (c == ",") {
      c = ".";
   }
   numero = numero + c;
 }
 return numero;
}

function strDateParse(strData)
{
//Retorna em milisegundos desde 01/01/1970. Se a data for antes de 1970, retorna um negativo.
//Serve para comparar datas. 
// Ex.: Comparar 1 data com a data atual:   if strDateParse(txtData.value)<>strDateparse(Date())....
  return Date.parse(strData.split("/").reverse().join("/"));
}

function ValidaData(s)
{
 var i;
 var c;
 var n_barras;
 var data;
 
 n_barras = 0;        
 if (s.length != 10)
   return false; 
 for(i=0; i<s.length; i++) {
   c = s.substring(i,i+1);
   if (c == "/") 
  n_barras++;
   if (n_barras > 2)  
  return false;  
   if (!isdigit(c) && (c != "/"))  
  return false;          
 }
 if (n_barras != 2)  
   return false;
 
 if ( (s.indexOf("/") != 2) || (s.lastIndexOf("/") != 5) )  
   return false;
  
 d = s.substring(0, 2)// dia
 m = s.substring(3, 5)// mes
 a = s.substring(6, 11)// ano
 if (m<1 || m>12) 
   return false;
 if (d<1 || d>31) 
  return false;
 
 if (a<1900 || a>2050) 
  return false;
 if (m==4 || m==6 || m==9 || m==11) {
   if (d==31) 
      return false;
 }
/* Bissexto
 Se for divisível por 4
   Se for divisível por 100
     Se for divisível por 400 é bissexto
     Senão não é
   Senão é bissexto
 Senão não é bissexto
Ex. de ano que é divisível por 4, é divisível por 100,
mas não é divisível por 400 é o ano 1000
*/
 if (m==2){
   var g=parseInt(a/4)
   if (isNaN(g))
     return false;
   if (d>29) return false;
   if (d==29) {
      if ((a/4)==parseInt(a/4)) {
        if ((a/100)==parseInt(a/100))
          if ((a/400)!=parseInt(a/400))
             return false;
      }
      else 
         return false; 
   }      
 }
 
 return true;
}



function SoNumeros(s)
{
 for (var i=0;i<s.length;i++) { 
    c = s.substring(i,i+1); 
   if (!isdigit(c)) 
      return false;   
 }
 return true;
}


function Pressionou_Enter(form, nome_prox_ctrl) {
//	alert(this.value);
//	netscape = (ver.charAt(iln+1).toUpperCase() != "C");
//	k = (netscape) ? DnEvents.which : window.event.keyCode;
	k = window.event.keyCode;
	if (k == 13) { // preciona tecla enter
		eval('document.' + form.name + '.' + nome_prox_ctrl + '.focus();');
//           if (ProxCampo.type=="text") {
//                eval("document." + form.name + "." + nome_prox_ctrl + ".select();");
//           }
//		eval('document.' + form.name + '.' + nome_prox_ctrl + '.click();');
	}
}


function openNewWindow(winURL,winWidth,winHeight,winConfig,adjust){
adjust = adjust || 0;
if(adjust == "1")
{
winWidth = window.screen.width-10;
winHeight = window.screen.height-56;
}
mmConfig=""
mmConfig="width="+winWidth+","
mmConfig+="height="+winHeight+","
mmConfig+=winConfig
var mmWin=open(winURL,"",mmConfig);
mmWin.focus();
}

function lnk(param)
{
  if(param !=""){
    location.href = param;
  }  
}


function Numero_KeyPress(form, CampoAtual, ProxCampo){
// Função Para aceitar somente numeros. Não aceita decimal
// Exemplo de uso: Em um formulário de nome "formulario"
//   e o campo atual for "campo1" e o foco vai para "campo2"
// onkeypress=""Numero_KeyPress(formulario, campo1, campo2);""
 k = event.keyCode;
 if ( k==44 || k==46 || k==13 || (k>=48 && k<=57) ) {
  //Digitou um "." ou uma ","
  if (k==44 || k==46) {
   k=0;
  }
  // pressionou tecla enter
  if (k == 13) {
   k=0;
   eval("document." + form.name + "." + ProxCampo.name + ".focus();");
   eval("document." + form.name + "." + ProxCampo.name + ".click();");
   if (ProxCampo.type=="text") {
      eval("document." + form.name + "." + ProxCampo.name + ".select();");
   }
  }
 } else{
  k=0;
 }
 event.keyCode=k;
}
 
function Valor_KeyPress(form, CampoAtual, ProxCampo){
// Função Para aceitar somente valores. Aceita decimal(com 2 casas)
// Exemplo de uso: Em um formulário de nome "formulario"
//   e o campo atual for "campo1" e o foco vai para "campo2"
// onkeypress=""Valor_KeyPress(formulario, campo1, campo2);""
 k = event.keyCode;

// alert(CampoAtual.defaultValue);
 if ( k==44 || k==46 || k==13 || (k>=48 && k<=57) ) {
  virgula = CampoAtual.value.indexOf(",");
  //Digitou um "." ou uma ","
  if (k==44 || k==46) {
   // Já tem um "," digitado. Não permite deixar mais de uma vírgula
   if (virgula>=0){
    k=0;
   } else {
    // Não tem nada no campo, coloca um "0" antes da virgula.
    if (CampoAtual.value.length < 1){
     CampoAtual.value = "0";
    }
    k=44;
   }
  }
  //  0 a 9 ou "," ou "enter" */
  if (k>=48 && k<=57) {
   // (length - 1) é a última posição do último caracter(Ex.: "1234" -> length = 4, porém a última posição é 3)
   // (virgula + 2) é a posição dos centavos. Só pode ter 2 casas decimais
   // Então vai entrar aqui quando já tiver 2 ou mais casas decimais.
   if ( (virgula >=0) && ((CampoAtual.value.length-1) >= (virgula + 2)) ){
     if (CampoAtual.value != CampoAtual.defaultValue) {
       k=0;
     }
   }
  }
  // pressionou tecla enter
  if (k == 13) {
   k=0;
   if (virgula >=0){
    //O último caracter digitado foi o virgula
    if ( (CampoAtual.value.length-1) == virgula ) {
     CampoAtual.value = CampoAtual.value + "00";
    } else{
     if ( (CampoAtual.value.length-1) == (virgula+1) ){
      CampoAtual.value = CampoAtual.value + "0";
     }
    }
   }else {
    CampoAtual.value = CampoAtual.value + ",00";
   }
   eval("document." + form.name + "." + ProxCampo.name + ".focus();");
   eval("document." + form.name + "." + ProxCampo.name + ".click();");
   if (ProxCampo.type=="text") {
      eval("document." + form.name + "." + ProxCampo.name + ".select();");
   }
  }
 } else{
  k=0;
 }
 event.keyCode=k;
}

function Pula_Proximo_Campo(CampoAtual) {
   if (event.keyCode==13) {
      CampoAtual.form[(getIndex(CampoAtual)+1) % CampoAtual.form.length].focus();
      if (CampoAtual.form[(getIndex(CampoAtual)+1) % CampoAtual.form.length].type=="text") {
         CampoAtual.form[(getIndex(CampoAtual)+1) % CampoAtual.form.length].select();
      }
   }

   function getIndex(CampoAtual) {
      var index = -1, i = 0, found = false;
      while (i < CampoAtual.form.length && index == -1)
         if (CampoAtual.form[i] == CampoAtual) {
            index = i;
         }
         else {
            i++;
         }
      return index;
   }
   return true;
}

// Tiradas de: http://elcio.com.br/ajax/mascara/
function mascara(funcao, CampoAtual){
    CampoAtual.value=funcao(CampoAtual.value);
    Pula_Proximo_Campo(CampoAtual);
//    setTimeout("execmascara()",1)
}

//function execmascara(){
//    v_obj.value=v_fun(v_obj.value)
//}

function leech(v){
    v=v.replace(/o/gi,"0")
    v=v.replace(/i/gi,"1")
    v=v.replace(/z/gi,"2")
    v=v.replace(/e/gi,"3")
    v=v.replace(/a/gi,"4")
    v=v.replace(/s/gi,"5")
    v=v.replace(/t/gi,"7")
    return v
}

//function soNumeros(v){
//    return v.replace(/\D/g,"")
//}

function telefone_com_ddd(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que não é dígito
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2") //Coloca parênteses em volta dos dois primeiros dígitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2")    //Coloca hífen entre o quarto e o quinto dígitos
    return v
}

function telefone(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que não é dígito
    v=v.replace(/(\d{4})(\d)/,"$1-$2")    //Coloca hífen entre o quarto e o quinto dígitos
    return v
}

function data(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que não é dígito
    v=v.replace(/(\d{2})(\d)/,"$1/$2")    //Coloca hífen entre o segundo e o terceiro dígitos
    v=v.replace(/(\d{2})(\d)/,"$1/$2")    //Coloca hífen entre o segundo e o terceiro dígitos
    return v
}

function cpf(v){
    v=v.replace(/\D/g,"")                    //Remove tudo o que não é dígito
    v=v.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dígitos
    v=v.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dígitos
                                             //de novo (para o segundo bloco de números)
    v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2") //Coloca um hífen entre o terceiro e o quarto dígitos
    return v
}

function cep(v){
    v=v.replace(/D/g,"")                //Remove tudo o que não é dígito
    v=v.replace(/^(\d{5})(\d)/,"$1-$2") //Esse é tão fácil que não merece explicações
    return v
}

function cnpj(v){
    v=v.replace(/\D/g,"")                           //Remove tudo o que não é dígito
    v=v.replace(/^(\d{2})(\d)/,"$1.$2")             //Coloca ponto entre o segundo e o terceiro dígitos
    v=v.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3") //Coloca ponto entre o quinto e o sexto dígitos
    v=v.replace(/\.(\d{3})(\d)/,".$1/$2")           //Coloca uma barra entre o oitavo e o nono dígitos
    v=v.replace(/(\d{4})(\d)/,"$1-$2")              //Coloca um hífen depois do bloco de quatro dígitos
    return v
}

