function testaBranco(campo, msg){
	var aux=document.getElementById(campo).value;
	
	if (aux==''){
		document.getElementById(msg).style.display = 'block';
	}else{
		document.getElementById(msg).style.display = 'none';
	}
	liberasubmit();
}

function testaEmail(campo, msg){	
	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	var aux=document.getElementById(campo).value;
    
	if(er.test(aux)){ 
		document.getElementById(msg).style.display = 'none';
	}else{
        document.getElementById(msg).style.display = 'block';
    }
	liberasubmit();
}

function testaFone(campo,msg){
    var aux=document.getElementById(campo).value;
	exp = /\(\d{2}\)\ \d{4}\-\d{4}/
    if(!exp.test(aux)){
        document.getElementById(msg).style.display = 'block';
	}else{
		document.getElementById(msg).style.display = 'none';
	}
	liberasubmit();
}

function validaDat(valor,msg) {
	var date=valor;
	var ardt=new Array;
	var ExpReg=new RegExp("(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/[12][0-9]{3}");
	ardt=date.split("/");
	erro=false;
	if ( date.search(ExpReg)==-1){
		erro = true;
		}
	else if (((ardt[1]==4)||(ardt[1]==6)||(ardt[1]==9)||(ardt[1]==11))&&(ardt[0]>30))
		erro = true;
	else if ( ardt[1]==2) {
		if ((ardt[0]>28)&&((ardt[2]%4)!=0))
			erro = true;
		if ((ardt[0]>29)&&((ardt[2]%4)==0))
			erro = true;
	}
	if (erro){
		document.getElementById(msg).style.display = 'block';
	}else{
		document.getElementById(msg).style.display = 'none';	
	}
}

function testaData2(pObj,msg) {
  var expReg = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[1-9]|1[0-2])|31\/(0[13578]|1[02]))\/(19|20)?\d{2}$/;
  var aRet = true;
  if ((pObj) && (pObj.value.match(expReg)) && (pObj.value != '')) {
    var dia = pObj.value.substring(0,2);
    var mes = pObj.value.substring(3,5);
    var ano = pObj.value.substring(6,10);
    if (mes == 4 || mes == 6 || mes == 9 || mes == 11 && dia > 30) 
      aRet = false;
    else 
      if ((ano % 4) != 0 && mes == 2 && dia > 28) 
        aRet = false;
      else
        if ((ano%4) == 0 && mes == 2 && dia > 29)
          aRet = false;
  }  else 
    aRet = false;  

  if(aRet){
	document.getElementById(msg).style.display = 'none';
  }else{
	document.getElementById(msg).style.display = 'block';
  }
}

function testaData(campo,msg){
	var aux=document.getElementById(campo).value;
    exp = /\d{2}\/\d{2}\/\d{4}/    
	if(!exp.test(aux)){
        document.getElementById(msg).style.display = 'block';
	}else{
		document.getElementById(msg).style.display = 'none';
	}
	liberasubmit();
}

function testaSenha(campo,msg){
	var aux=document.getElementById(campo).value;
	if(aux.length<6 || aux.length>32){
		document.getElementById(msg).style.display = 'block';
	}else{
		document.getElementById(msg).style.display = 'none';
	}
	liberasubmit();
}

function testaSenhaIgual(campo1,campo2,msg){	
	var aux=document.getElementById(campo1).value;
	var aux2=document.getElementById(campo2).value;
	if(aux==aux2){
        document.getElementById(msg).style.display = 'none';
	}else{
		document.getElementById(msg).style.display = 'block';
	}
	liberasubmit();
}

//valida o CPF digitado
function testaCPF(Objcpf,msg){
    var cpf = Objcpf.value;
    exp = /\.|\-/g
    cpf = cpf.toString().replace( exp, "" ); 
    var digitoDigitado = eval(cpf.charAt(9)+cpf.charAt(10));
    var soma1=0, soma2=0;
    var vlr =11;
    
    for(i=0;i<9;i++){
        soma1+=eval(cpf.charAt(i)*(vlr-1));
        soma2+=eval(cpf.charAt(i)*vlr);
        vlr--;
    }    
    soma1 = (((soma1*10)%11)==10 ? 0:((soma1*10)%11));
    soma2=(((soma2+(2*soma1))*10)%11);
    
    var digitoGerado=(soma1*10)+soma2;
    if(digitoGerado!=digitoDigitado){
		document.getElementById(msg).style.display = 'block';
	}else{
		document.getElementById(msg).style.display = 'none';
	}
}

function testaCPF_v2(Objcpf){
    var aRet = true;
	var cpf = Objcpf.value;
    exp = /\.|\-/g
    cpf = cpf.toString().replace( exp, "" ); 
    var digitoDigitado = eval(cpf.charAt(9)+cpf.charAt(10));
    var soma1=0, soma2=0;
    var vlr =11;
    
    for(i=0;i<9;i++){
        soma1+=eval(cpf.charAt(i)*(vlr-1));
        soma2+=eval(cpf.charAt(i)*vlr);
        vlr--;
    }    
    soma1 = (((soma1*10)%11)==10 ? 0:((soma1*10)%11));
    soma2=(((soma2+(2*soma1))*10)%11);
    
    var digitoGerado=(soma1*10)+soma2;
    if(digitoGerado!=digitoDigitado){
		aRet=false;
	}
	return aRet;
}

function testaCPF_v3(Objcpf,msg){
	var cpf = Objcpf.value;
	var numeros, digitos, soma, i, resultado, digitos_iguais;
	var aRet = true;
	exp = /\.|\-/g
    cpf = cpf.toString().replace( exp, "" );
	digitos_iguais = 1;
	if (cpf.length < 11)
		aRet=false;
	for (i = 0; i < cpf.length - 1; i++)
		if (cpf.charAt(i) != cpf.charAt(i + 1)){
			digitos_iguais = 0;
			break;
		}
	if (!digitos_iguais){
		numeros = cpf.substring(0,9);
		digitos = cpf.substring(9);
		soma = 0;
		for (i = 10; i > 1; i--)
			soma += numeros.charAt(10 - i) * i;
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(0))
			aRet=false;
		numeros = cpf.substring(0,10);
		soma = 0;
		for (i = 11; i > 1; i--)
			soma += numeros.charAt(11 - i) * i;
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(1))
			aRet=false;
	}else{
		aRet=false;
	}
	if(aRet==true){
		document.getElementById(msg).style.display = 'none';
	}else{
		document.getElementById(msg).style.display = 'block';
	}
	
	return aRet;
}

function testaCPF_v4(Objcpf){
	var cpf = Objcpf.value;
	var numeros, digitos, soma, i, resultado, digitos_iguais;
	var aRet = true;
	exp = /\.|\-/g
    cpf = cpf.toString().replace( exp, "" );
	digitos_iguais = 1;
	if (cpf.length < 11)
		aRet=false;
	for (i = 0; i < cpf.length - 1; i++)
		if (cpf.charAt(i) != cpf.charAt(i + 1)){
			digitos_iguais = 0;
			break;
		}
	if (!digitos_iguais){
		numeros = cpf.substring(0,9);
		digitos = cpf.substring(9);
		soma = 0;
		for (i = 10; i > 1; i--)
			soma += numeros.charAt(10 - i) * i;
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(0))
			aRet=false;
		numeros = cpf.substring(0,10);
		soma = 0;
		for (i = 11; i > 1; i--)
			soma += numeros.charAt(11 - i) * i;
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		if (resultado != digitos.charAt(1))
			aRet=false;
	}else{
		aRet=false;
	}
	return aRet;
}

function MascaraTelefone(tel){
    return formataCampo(tel, '(00) 0000-0000', event);
}

function MascaraData(data){   
    return formataCampo(data, '00/00/0000', event);
}

function MascaraCep(cep){  
    return formataCampo(cep, '00.000-000', event);
}

function MascaraCPF(cpf){   
    return formataCampo(cpf, '000.000.000-00', event);
}

function MascaraContrato(contrato){
    return formataCampo(contrato, '00000000-0', event);
}

function MascaraCNPJ(cnpj){      
    return formataCampo(cnpj, '00.000.000/0000-00', event);
}

function formataCampo(campo, Mascara, evento) { 
    var boleanoMascara; 
    
    var Digitato = evento.keyCode;
    exp = /\-|\.|\/|\(|\)| /g
    campoSoNumeros = campo.value.toString().replace( exp, "" ); 
   
    var posicaoCampo = 0;     
    var NovoValorCampo="";
    var TamanhoMascara = campoSoNumeros.length;; 
    
    if (Digitato != 8) { // backspace 
        for(i=0; i<= TamanhoMascara; i++) { 
            boleanoMascara  = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".")
                                || (Mascara.charAt(i) == "/")) 
            boleanoMascara  = boleanoMascara || ((Mascara.charAt(i) == "(") 
                                || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " ")) 
            if (boleanoMascara) { 
                NovoValorCampo += Mascara.charAt(i); 
                  TamanhoMascara++;
            }else { 
                NovoValorCampo += campoSoNumeros.charAt(posicaoCampo); 
                posicaoCampo++; 
              }            
          }     
        campo.value = NovoValorCampo;
          return true; 
    }else { 
        return true; 
    }
}

function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

function formataMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    //var whichCode = (window.Event) ? e.which : e.keyCode;
	var whichCode = (window.addEventListener) ? e.which : e.keyCode;
    // 13=enter, 8=backspace as demais retornam 0(zero)
    // whichCode==0 faz com que seja possivel usar todas as teclas como delete, setas, etc    
    if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8))
    	return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
 
 
    if (strCheck.indexOf(key) == -1) 
    	return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) 
        	break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) 
        	aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) 
    	objTextBox.value = '';
    if (len == 1) 
    	objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) 
    	objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        	objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

/*Máscaras Novas Abaixo*/

documentall = document.all;
function formataDinheiro(c) {
    var t = this;
	if(c == undefined) c = 2;
    	var p, d = (t=t.split("."))[1].substr(0, c);
	    for(p = (t=t[0]).length; (p-=3) >= 1;)
		{
           t = t.substr(0,p) + "." + t.substr(p);
	    }
    return t+","+d+Array(c+1-d.length).join(0);
}
 
String.prototype.formatCurrency=formataDinheiro

function demaskvalue(valor, currency){
	var val2 = '';
	var strCheck = '0123456789';
	var len = valor.length;
    	if (len== 0){
	      return 0.00;
   		}
  
   if (currency ==true){
      for(var i = 0; i < len; i++)
         if ((valor.charAt(i) != '0') && (valor.charAt(i) != ',')) break;
		 
      for(; i < len; i++){
         if (strCheck.indexOf(valor.charAt(i))!=-1) val2+= valor.charAt(i);  
      }  
  
      if (val2.length==0) return "0.00";  
      if (val2.length==1)return "0.0" + val2;  
      if (val2.length==2)return "0." + val2;  
        
      var parte1 = val2.substring(0,val2.length-2);  
      var parte2 = val2.substring(val2.length-2);  
      var returnvalue = parte1 + "." + parte2;  
      return returnvalue;  
        
   }  
   else{  

         val3 ="";  
         for(var k=0; k < len; k++){  
            if (strCheck.indexOf(valor.charAt(k))!=-1) val3+= valor.charAt(k);  
         }           
   return val3;  
   }  
}  
  
function reais(obj,event){  
  
var whichCode = (window.Event.keyCode) ? event.which : event.keyCode;  
if (whichCode == 8 && !documentall) {     
   if (event.preventDefault){
         event.preventDefault();  
      }else{
         event.returnValue = false;  
   }  
   var valor = obj.value;  
   var x = valor.substring(0,valor.length-1);  
   obj.value= demaskvalue(x,true).formatCurrency();  
   return false;  
}  
	FormataReais(obj,'.',',',event);  
}
  
  
function backspace(obj,event){

var whichCode = (window.addEventListener) ? event.which : event.keyCode;  
if (whichCode == 8 && documentall) {     
   var valor = obj.value;  
   var x = valor.substring(0,valor.length-1);  
   var y = demaskvalue(x,true).formatCurrency();  

   obj.value ="";
   obj.value += y;
   
   if (event.preventDefault){
         event.preventDefault();
      }else{
         event.returnValue = false;
   }
   return false;

   }
}
  
function FormataReais(fld, milSep, decSep, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.addEventListener) ? e.which : e.keyCode;

if (whichCode == 0 ) return true;  
if (whichCode == 9 ) return true;
if (whichCode == 13) return true;
if (whichCode == 16) return true;
if (whichCode == 17) return true;
if (whichCode == 27 ) return true;
if (whichCode == 34 ) return true;
if (whichCode == 35 ) return true;
if (whichCode == 36 ) return true;  
  
if (e.preventDefault){
      e.preventDefault()  
    }else{  
       e.returnValue = false  
}  
  
var key = String.fromCharCode(whichCode);
if (strCheck.indexOf(key) == -1) return false;
	fld.value += key;
  
var len = fld.value.length;
var bodeaux = demaskvalue(fld.value,true).formatCurrency();
fld.value=bodeaux;

  if (fld.createTextRange) {
    var range = fld.createTextRange();
    range.collapse(false);
    range.select();
  }
  else if (fld.setSelectionRange) {
    fld.focus();
    var length = fld.value.length;
    fld.setSelectionRange(length, length);
  }
  return false;
}
