function FormatarCPF(campo,xEvent){
	
	if(navigator.appNome == "Microsoft Internet Explorer"){
		tecla = window.event.keyCode;
	} else{
		tecla = xEvent.which;
	}

	vr = campo.value;
	vr = vr.replace(/([^0-9])/g,'');
	tam = vr.length + 1;
	
	if(tecla != 8 && tecla != 0 && tecla != 127 && tecla != 9 && tecla != 13 && tecla != 37 && tecla != 39 && tecla != 46 ){
		if(tam <= 3)
			campo.value = vr;
		if((tam > 3) && (tam <= 6))
			campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
		if((tam > 6) && (tam <= 9))
			campo.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(6, tam);
		if(tam > 9)
			campo.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(6, 3) + '-' + vr.substr(9, 1);
	}

	return true;
}

function FormatarData(campo, vEvent) {

	isNav4 = navigator.appName == "Netscape" && navigator.appVersion < "5";
	strSeperator = '/';
	
	if(!isNav4){
		if(campo.value.length == 2 || campo.value.length == 5)
		if(vEvent.keyCode != 46 && vEvent.keyCode != 8) // Del && Back Space
			campo.value = campo.value + strSeperator;   
		else
			campo.value = campo.value.substr(0, campo.value.length-1);   
	} else if (campo.value.length == 8){
		campo.value = campo.value.substr(0, 2) + strSeperator + campo.value.substr(2, 2) + strSeperator + campo.value.substr(4, 4);
	}

	return true;
}

function FormatarTelefone(campo,vEvent){

	var separador = "-";
	var abre = "(";
	var fecha = ") ";
	var ddd = 2;
	var telefone = 9;
	
	if (campo.value.length == ddd)
		campo.value = abre + campo.value + fecha;

	if (campo.value.length == telefone)
		campo.value = campo.value + separador;
	
	return true;
}

function FormatarCEP(campo,xEvent) {
    
	var vr = campo.value;
	if (vr.length == 5)
		campo.value = vr + "-";

	return true;
}


// FUNÇÕES NÃO USADAS
function FormatarCNPJ(campo,xEvent) {
	if (navigator.appNome == "Microsoft Internet Explorer") {
		tecla = window.event.keyCode;
	} else {
		tecla = xEvent.which;
	}
	
	vr = campo.value;
	vr = vr.replace(/([^0-9])/g,'');
	tam = vr.length + 1;

	// Formata Mask de CNPJ
	if ( tecla != 9 && tecla != 0 && tecla != 8 && tecla != 13 && tecla != 37 && tecla != 39 && tecla != 46 && tecla != 58) {
		if ( tam <= 2 ) 
			campo.value = vr ;
		if ( (tam > 2) && (tam <= 5) ) 
			campo.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, tam ) ;
		if ( (tam > 5) && (tam <= 8) ) 
			campo.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, tam ) ;
		if ( (tam > 8) && (tam <= 12) ) 
			campo.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, 3 ) + '/' + vr.substr( 8, tam ) ;
		if ( (tam > 12)  ) 
			campo.value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, 3 ) + '.' + vr.substr( 5, 3 ) + '/' + vr.substr( 8, 4 ) + '-' + vr.substr( 12, 1) ;
	}
}

function ValidarCNPJ(source, arguments) {
	var cgc = source.Value;
	var n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14
	var d1,d2
	var digitado, calculado
	cgc = extrai_numeros(cgc)
	
	if(cgc.length < 14) {
		arguments.IsValid = false 
		return
	}
	
	n1 = cgc.substring(0,1)
	n2 = cgc.substring(1,2)
	n3 = cgc.substring(2,3)
	n4 = cgc.substring(3,4)
	n5 = cgc.substring(4,5)
	n6 = cgc.substring(5,6)
	n7 = cgc.substring(6,7)
	n8 = cgc.substring(7,8)
	n9 = cgc.substring(8,9)
	n10 = cgc.substring(9,10)
	n11 = cgc.substring(10,11)
	n12 = cgc.substring(11,12)
	n13 = cgc.substring(12,13)
	n14 = cgc.substring(13,14)
	d1 = n12*2+n11*3+n10*4+n9*5+n8*6+n7*7+n6*8+n5*9+n4*2+n3*3+n2*4+n1*5
	d1= 11-(d1 % 11)
	if (d1 >= 10) d1=0
	d2 = d1*2+n12*3+n11*4+n10*5+n9*6+n8*7+n7*8+n6*9+n5*2+n4*3+n3*4+n2*5+n1*6
	d2 = 11-(d2 % 11)
	if (d2 >= 10) d2=0
	calculado = d1 + d2
	digitado = n13*1 + n14*1
	if (calculado == digitado)
		arguments.IsValid = true
	else
		arguments.IsValid = false
}

function valida_CPFCNPJ(oSrc,args){
    
    if (args.Value.length == 11){
        valida_CPF(oSrc,args);
    }else if(args.Value.length == 11){
        valida_CNPJ(oSrc, args);
    }else{
        return args.IsValid = false;
    }
}

function valida_CPF(oSrc,args){
   
    var s = args.Value;
    s = s.replace(/[.]/g,'');
    s = s.replace('-','');
    
    //args.isValid = (s >= 3);
    //document.write(oSrc.Value + ',' + args.Value);
    if (isNaN(s)) {
        return args.IsValid = false;
    }
   
    var result = true
    
    for (i=1; i<s.length; i++)
	{
		result = result && (s.charAt(i-1) == s.charAt(i));
	}   
   
    var i;
    var c = s.substr(0,9);
    var dv = s.substr(9,2);
    var d1 = 0;
    
    for (i = 0; i < 9; i++) {
        d1 += c.charAt(i)*(10-i);
    }
    if (d1 == 0){
        return args.IsValid = false;
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(0) != d1) {
        return args.IsValid = false;
    }
    d1 *= 2;
    for (i = 0; i < 9; i++) {
        d1 += c.charAt(i)*(11-i);
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(1) != d1) {
        return args.IsValid = false;
    }
    return args.IsValid = (!result);
}

// Validação de CNPJ
function valida_CNPJ(oSrc, args){
    
    var s = args.Value;
    
    s = s.replace(/[.]/g,'');
    s = s.replace(/-/,'');
    s = s.replace('/','');
    
    if (isNaN(s)) {
        return args.IsValid = false;
    }
    
    var i;
    var c = s.substr(0,12);
    var dv = s.substr(12,2);
    var d1 = 0;
    
    for (i = 0; i <12; i++){
        d1 += c.charAt(11-i)*(2+(i % 8));

    }
    if (d1 == 0)
        return args.IsValid = false;

    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(0) != d1){
        return args.IsValid = false;
    }
    d1 *= 2;
    for (i = 0; i < 12; i++){
        d1 += c.charAt(11-i)*(2+((i+1) % 8));
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9)
        d1 = 0;
    if (dv.charAt(1) != d1){
        return args.IsValid = false;
    }
    return args.IsValid = true;
} 

function ValidarHORA(source, arguments) {
		var hora = arguments.Value;
		var formato = "[0-2][0-9]:[0-5][0-9]";
		var reg = new RegExp(formato);
		var formato_ok = reg.test(hora);
		var h = hora.substring(0,2);
		var m = hora.substring(3,5);
		var nova_hora = new Date(2000, 1, 1, h, m);
		var h_ok = (nova_hora.getHours() == h);
		var m_ok = (nova_hora.getMinutes() == m);
		arguments.IsValid = (formato_ok && h_ok && m_ok)
}

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 || whichCode == 8 || whichCode == 0) 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;
    if (len > 9) return false; 
     
    var num = objTextBox.value.replace(/\./g, "");
    num = num.replace(/\,/g, "");
    num += key;
      
    for (i = num.length -1 ; i >= 0; i--){
        j++
        aux += num.charAt(i);
        if (j == 3){ 
            
            aux += SeparadorMilesimo;
            j = 0;
        }
        
    }
    
    if (aux.charAt(aux.length - 1) == ".") aux = aux.substring(0, aux.length -1);
    
    for(i = aux.length; i >= 0; i--){
        aux2 += aux.charAt(i - 1); 
    }
    
    objTextBox.value = aux2;
    
    if (event.stopPropagation) {
        event.stopPropagation();
    }        
    event.cancelBubble = true; //para resolver o problema de duplicar o valor do campo, qdo tiver o atributo default button definido.
    
    return false;
}

function MascaraMixMoeda(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 || whichCode == 8 || whichCode == 0) 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;
    if (len >= 12) return false;
    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 MascaraNumero(objTextBox,e){
    var strCheck = '0123456789';
    var key = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida 
}


function ValidaTexto(sender,args){
    var ret = false
    
    //Telefone
    var x = /([(]?[0-9]?[0-9]{2}[)]?)?[ ]?[0-9]?[0-9]{3}[ -]?[0-9]{4}/
    //Email
    var y = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
    //URL
    var z = /((http(s)?:\/\/)|(www(2)?.))+([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/

    var txt = args.Value
    
    ret1 = x.test(txt) || y.test(txt) || z.test(txt)
  
    args.IsValid = !ret1
}

function MudaFoco(obj,objDestino,qtdChar){
    if (obj.value.length == qtdChar){
       document.getElementById(objDestino).focus();
    }
}

function ValidaTelefone(sender, args){
    var Tel = /^[0-9]{3,4}[-]?[0-9]{4}$/
    args.IsValid = Tel.test(args.Value);
}

function Trim(_sValue){
    return _sValue.replace(/^\s*/, "").replace(/\s*$/, "");
}

function VerificaData(Data) {
	        if (Data.length != 10) {
		        return false
	        }
	        dia = ''
	        for (i=0;i<=1; i++) {
		        val = Data.charAt(i)
		        dia = dia + Data.charAt(i)
		        if ((val!="9")&&(val!="0")&&(val!="1")&&(val!="2")&&(val!="3")&&(val!="4") &&
					        (val!="5")&&(val!="6")&&(val!="7")&&(val!="8")) {
			        return false
		        }			   
	        }
	        if (Data.charAt(2) != "/") {
		        return false
	        }
	        mes = ''
	        for (i=3;i<=4; i++) {
		        val = Data.charAt(i)
		        mes = mes + Data.charAt(i)
		        if ((val!="9")&&(val!="0")&&(val!="1")&&(val!="2")&&(val!="3")&&(val!="4") &&
					        (val!="5")&&(val!="6")&&(val!="7")&&(val!="8")) {
			        return false
		        }			   
	        }
	        if (Data.charAt(5) != "/") {
		        return false
	        }
	        ano = ''
	        for (i=6;i<=9; i++) {		
		        val = Data.charAt(i)
		        ano = ano + Data.charAt(i)
		        if ((val!="9")&&(val!="0")&&(val!="1")&&(val!="2")&&(val!="3")&&(val!="4") &&
					        (val!="5")&&(val!="6")&&(val!="7")&&(val!="8")) {
			        return false
		        }			   
	        }
	        if (mes <= 12) {
		        if (mes == 2) {
		            if (AnoBissexto(ano)) {
		                if (dia > 29) {
				            return false
			            }
			            else {
				            return true
			            }
		            }	
		            else {
		               if (dia > 28) {
				            return false
			            }
			            else {
				            return true
			            }
		            }		        
		        }
		        else {
			        if (mes == 4 || mes == 6 || mes == 9 || mes == 11) {
				        if (dia > 30) {
					        return false
				        }
				        else {
					        return true
				        }
			        }
			        else {
				        if (dia > 31) {
					        return false
				        }
				        else {
					        return true
				        }
			        }
		        }
	        }
	        else {
		        return false
	        }	
        }
       
        function AnoBissexto (Ano) {
            if (((Ano % 4)==0) && ((Ano % 100)!=0) || ((Ano % 400)==0))
                return true;
            else
                return false;
        }
        
function max(txarea, max, font) {  
    var total = max;     
    tam = txarea.value.length; 
    str=""; 
    str=str+tam;      
    if ((total - str) < 0){
        document.getElementById(font).innerHTML = 0;       
    } else {
        document.getElementById(font).innerHTML = total - str;       
    }
    if (tam > total){         
       aux = txarea.value;         
       if (total < 0){
            txarea.value = aux.substring(0,max);                    
       } else {
            txarea.value = aux.substring(0,total);                    
       }
       if ((total - str) < 0){
            document.getElementById(font).innerHTML = 0;       
       }else{
            document.getElementById(font).innerHTML = total - str;     
       }
    }
}