// JavaScript Document
//adiciona mascara de cnpj
function MascaraCNPJ(cnpj,event){
	if(mascaraInteiro(event)==false){
		event.returnValue = false;
	}
	return formataCampo(cnpj, '00.000.000/0000-00', event);
}

//adiciona mascara de cep
function MascaraCep(cep,event){
	if(mascaraInteiro(event)==false){
		event.returnValue = false;
	}
	return formataCampo(cep, '00.000-000', event);
}

//adiciona mascara de data
function MascaraData(data,event){
	if(mascaraInteiro(event)==false){
		event.returnValue = false;
	}
	return formataCampo(data, '00/00/0000', event);
}

//adiciona mascara ao telefone
function MascaraTelefone(tel,event){
	if(mascaraInteiro(event)==false){
		event.returnValue = false;
	}
	return formataCampo(tel, '(00) 0000-0000', event);
}

//adiciona mascara ao CPF
function MascaraCPF(cpf,event){
	if(mascaraInteiro(event)==false){
		event.returnValue = false;
	}
	return formataCampo(cpf, '000.000.000-00', event);
}

//valida numero inteiro com mascara
function mascaraInteiro(event){
	//alert(event.keyCode)
	if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 37 || event.keyCode == 39 || 
			event.keyCode == 9 || event.keyCode == 13 || event.keyCode == 116 || event.keyCode == 36 ||
			event.keyCode == 35 ||
			(event.keyCode > 47 && event.keyCode < 58) ||
			(event.keyCode > 95 && event.keyCode < 106)){
		// 08 - backspace / 46 - delete / 37 - seta esquerda / 39 - seta direita / 9 - tab / 13 - enter / 116 - F5 / 36 - Home / 35 - End
		return true;
	}
	var UA = navigator.userAgent;
	if (UA.indexOf('Firefox') > -1){
		event.preventDefault();
	} else {
		event.returnValue = false;
	}
	return false;
}

//valida telefone
function ValidaTelefone(tel){
    exp = /\(\d{2}\)\ \d{4}\-\d{4}/;
    if(!exp.test(tel.value)){
			if (tel.value != ""){
        alert('Numero de Telefone Invalido!');
				tel.focus();
				return false;
			}
		}
		return true;
}

//valida CEP
function ValidaCep(cep){
    exp = /\d{2}\.\d{3}\-\d{3}/;
    if(!exp.test(cep.value)){
			if (cep.value != ""){
        alert('Numero de Cep Invalido!');
				cep.focus();
				return false;
			}
		}
		return true;
}

//valida data
function ValidaData(data){
    exp = /\d{2}\/\d{2}\/\d{4}/
    if(!exp.test(data.value)){
			if (data.value != ""){
				return false;
			}
		}
		return true;
}

//valida o CPF digitado
function ValidarCPF(Objcpf){
    var cpf = Objcpf.value;
		
		if (cpf == "")
			return true;
		
    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){
        alert('CPF Invalido!');
				Objcpf.focus();
				return false;
		}
		return true;
}

//valida o CNPJ digitado
function ValidarCNPJ(ObjCnpj){
    var cnpj = ObjCnpj.value;
		
		if (cnpj == "")
			return true;
		
    var valida = new Array(6,5,4,3,2,9,8,7,6,5,4,3,2);
    var dig1= new Number;
    var dig2= new Number;
    
    exp = /\.|\-|\//g
    cnpj = cnpj.toString().replace( exp, "" );
    var digito = new Number(eval(cnpj.charAt(12)+cnpj.charAt(13)));
        
    for(i = 0; i<valida.length; i++){
        dig1 += (i>0? (cnpj.charAt(i-1)*valida[i]):0);    
        dig2 += cnpj.charAt(i)*valida[i];    
    }
    dig1 = (((dig1%11)<2)? 0:(11-(dig1%11)));
    dig2 = (((dig2%11)<2)? 0:(11-(dig2%11)));
    
    if(((dig1*10)+dig2) != digito){    
        alert('CNPJ Invalido!');
				ObjCnpj.focus();
				return false;
		}
		return true;
}

//formata de forma generica os campos
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 Validate_String(string, return_invalid_chars){
	valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	invalid_chars = '';
	
	if (string == null || string == ''){
		return(true);
	}
	
	//For every character on the string.
	for (index = 0; index < string.length; index++){
		char = string.substr(index, 1);
		
		//Is it a valid character?
		if (valid_chars.indexOf(char) == -1){
			//If not, is it already on the list of invalid characters?
			if (invalid_chars.indexOf(char) == -1){
				//If it's not, add it.
				if (invalid_chars == ''){
					invalid_chars += char;
				} else {
					invalid_chars += ', ' + char;
				}
			}
		}
	}
	
	//If the string does not contain invalid characters, the function will return true.
	//If it does, it will either return false or a list of the invalid characters used
	//in the string, depending on the value of the second parameter.
	if (return_invalid_chars == true && invalid_chars != ''){
		last_comma = invalid_chars.lastIndexOf(',');
		if (last_comma != -1){
			invalid_chars = invalid_chars.substr(0, $last_comma) + ' and ' + 
											invalid_chars.substr(last_comma + 1, invalid_chars.length);
		}
		return(invalid_chars);
	} else {
		return(invalid_chars == '');
	}
}

function Validate_Email_Address(email_address){
	//Assumes that valid email addresses consist of user_name@domain.tld
	at = email_address.indexOf('@');
	at2 = email_address.lastIndexOf('@');
	dot = email_address.indexOf('.');
	dot2 = email_address.lastIndexOf('.');
	
	//Verifica se possui algum . ou @ ou se possui mais de um @ no email
	if (at == -1 || at == 0 || at == email_address.length - 1 || at != at2 ||
			dot == -1 || dot == 0 || dot2 == email_address.length - 1){
		return(false);
	}
	
	user_name = email_address.substr(0, at);
	domain_name = email_address.substr(at + 1, email_address.length);
	
	if (Validate_String(user_name) === false || 
		  Validate_String(domain_name) === false){
		return(false);
	}
	
	return(true);
}

//valida Email
function ValidaEmail(email){
	if (email.value == "")
		return true;
	if (!Validate_Email_Address(email.value)){
		alert('Email Invalido!');
		email.focus();
		return false;
	}
	return true;
}

//Transformar DD/MM/AAAA para AAAAMMDD.
function desformataData(data){
	if (data != ""){
		data = data.substr(6,4) + data.substr(3,2) + data.substr(0,2);
		return data;
	}
}
