// JScript File

if (navigator.appName.indexOf('Microsoft') != -1){
    clientNavigator = "IE";
}else{
    clientNavigator = "Other";
}


//Se o parâmetro obrigatório for igual à zero, significa que elepode estar vazio, caso contrário, não
function Verifica_Data(data, obrigatorio){
 
    var data = document.getElementById(data);
 	var strdata = data.value;
 	if((obrigatorio == 1) || (obrigatorio == 0 && strdata != "")){
 		//Verifica a quantidade de digitos informada esta correta.
 		if (strdata.length != 10){
 			alert("Formato da data não é válido. Formato correto: - dd/mm/aaaa.");
 			data.focus();
 			return false
 		}
 		//Verifica máscara da data
 		if ("/" != strdata.substr(2,1) || "/" != strdata.substr(5,1)){
 			alert("Formato da data não é válido. Formato correto: - dd/mm/aaaa.");
 			data.focus();
 			return false
 		}
 		dia = strdata.substr(0,2)
 		mes = strdata.substr(3,2);
 		ano = strdata.substr(6,4);
 		//Verifica o dia
 		if (isNaN(dia) || dia > 31 || dia < 1){
 			alert("Formato do dia não é válido.");
 			data.focus();
 			return false
 		}
 		if (mes == 4 || mes == 6 || mes == 9 || mes == 11){
 			if (dia == "31"){
 				alert("O mês informado não possui 31 dias.");
 				data.focus();
 				return false
 			}
 		}
 		if (mes == "02"){
 			bissexto = ano % 4;
 			if (bissexto == 0){
 				if (dia > 29){
 					alert("O mês informado possui somente 29 dias.");
 					data.focus();
 					return false
 				}
 			}else{
 				if (dia > 28){
 					alert("O mês informado possui somente 28 dias.");
 					data.focus();
 					return false
 				}
 			}
 		}
 		//Verifica o mês
 		if (isNaN(mes) || mes > 12 || mes < 1){
 			alert("Formato do mês não é válido.");
 			data.focus();
 			return false
 		}
 		//Verifica o ano
 		if (isNaN(ano)){
 			alert("Formato do ano não é válido.");
 			data.focus();
 			return false
 		}
	}
}

//Se o parâmetro obrigatório for igual à zero, significa que ele pode estar vazio, caso contrário, não 
function Verifica_Email(email, obrigatorio){
 
 	var email = document.getElementById(email);
 	if((obrigatorio == 1) || (obrigatorio == 0 && email.value != "")){
 		//if( !email.value.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.[a-zA-Z0-9._-]+)/gi) ){
 		
 		if(!email.value.match(/^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/)){

 			alert("Informe um e-mail correto - Exemplo: usuario@dominio.com");
 			email.focus();
 			return false
 		}
 	}
 }
 
//Se o parâmetro obrigatório for igual à zero, significa que elepode estar vazio, caso contrário, não 
function Verifica_Cep(cep, obrigatorio){
 	var cep    = document.getElementById(cep);
 	var strcep = cep.value;
 	if((obrigatorio == 1) || (obrigatorio == 0 && strcep != "")){
 		if (strcep.length != 9){
 			alert("CEP informado inválido. - Exemplo: 00000-000");
 			cep.focus();
 			return false
 		}else{
 			if (strcep.indexOf("-") != 5){
 				alert("Formato de CEP informado inválido. - Exemplo: 00000-000");
 				cep.focus();
 				return false
 			}else{
 				if (isNaN(strcep.replace("-","0"))){
 					alert("CEP informado inválido. - Exemplo: 00000-000");
 					cep.focus();
 					return false
 				}
 			}
 		}
    }	  
}






function Verifica_Url(url, obrigatorio){
 
 	var url = document.getElementById(url);
 	if((obrigatorio == 1) || (obrigatorio == 0 && url.value != "")){
 		
 		if(!url.value.match(/http[s]?:\/\/\w[\.\w]+\w[\.\w]+\w[\w]$/)){

 			alert("Formato de Url inválida. Exemplo: http://dominio.com");
 			url.focus();
 			return false
 		}
 	}
 }



//Ajusta máscara de Data e só permite digitação de números
function Ajusta_Data(input, evnt){
    
    var data = document.getElementById(input);
 	var data = data.value;

 	if ((data.length == 2) || (data.length == 5)){
 		if(clientNavigator == "IE"){
 			data += "/";
 		}else{
 			if(evnt.keyCode == 0){
 				data += "/";
 			}
 		}
 	}
}

function alteraEstado(){

    if (document.formulario.cxpais.value == "BRA"){
        document.formulario.cxestado.disabled = false;
        document.formulario.cxestado.value = "XX";
    } else {
        document.formulario.cxestado.disabled = true;
        document.formulario.cxestado.value = "ZZ";
    }
}

//adiciona mascara ao telefone
function mascaraTelefone(varNumero){    
    if(mascaraInteiro(varNumero)==false){
        event.returnValue = false;
    }
}


function IsNumeric(sText){
   
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;
 
    for (i = 0; i < sText.length && IsNumber == true; i++){ 
    Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;   
}

function mascaraInteiro(varNumero){
    if (event.keyCode < 48 || event.keyCode > 57){
        event.returnValue = false;
        return false;
    }
    return true;
}