/***************************************************
* Criado por: Fábio Emerson - www.fabioemerson.com *
* Cliente: www.netuai.net               		   *
* Data: março 2009                      		   *
****************************************************/

/*PEGA O ENDEREÇO DO SITE*/
var urlsite = window.location.href.toLowerCase();
urlsite = urlsite.split("netuai.net",1);
urlsite = urlsite[0]+"netuai.net";

function preCarrega(img){
	var imagem = new Image();
	imagem.src = img;
}

function saltaCampo (campo,prox,tammax,teclapres){//onkeyup="saltaCampo(this, document.FORM.CAMPO, 2, event);"
   var tecla = teclapres.keyCode;
   vr = campo.value;
   tam = vr.length;
   if (tecla != 0 && tecla != 10 && tecla != 24)
      if (tam == tammax)
          prox.focus();
}

function soNumeros(event){/*onkeypress="return soNumeros(event)"*/
    var kCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    var caract = new RegExp(/^[0-9\b]+$/i);
    var caract = caract.test(String.fromCharCode(kCode));
    if(!caract)
        return false;
}

function urlEncode(sStr) {
	return escape(sStr).replace(/\+/g, '%2B').replace(/\”/g,'%22').replace(/\’/g, '%27').replace(/\//g,'%2F');
}

function display(id){
	if(document.getElementById(id).style.display == 'none')
		document.getElementById(id).style.display = 'block';
	else document.getElementById(id).style.display = 'none';
}

function showHideDiv(div,lista,valor){
	if(document.getElementById(lista).value == valor)
		document.getElementById(div).style.display = 'block';
	else document.getElementById(div).style.display = 'none';
}

/*FORMATA DATA*/
function formataData(campo){/* onkeyup="formataData(this);*/
	if (campo.value.length == 2 || campo.value.length == 5)
		campo.value = campo.value+"/";
}

/*MUDA PESSOA JURÍDICA E FÍSICA*/
function mudaPessoa(tipo){
	if(tipo == 'fisica'){
		display('pessoa_fisica');
		display('pessoa_juridica');
		document.getElementById('tipopessoa').value = 'fisica';
	}
	else{
		display('pessoa_fisica');
		display('pessoa_juridica');
		document.getElementById('tipopessoa').value = 'juridica';
	}
}

/*VALIDA CPF*/
function validaCPF(cpf){
    var i;
    var soma = soma2 = 0;
    if (!(cpf.match(/^[0-9]{3,3}[.]{0,1}[0-9]{3,3}[.]{0,1}[0-9]{3,3}[-]{0,1}[0-9]{2,2}$/))) 
        return false;
	if(cpf == '00000000000')
		return false;
    for(i=0;i<10;i++) {
        if (i<9)
            soma += cpf.charAt(i) * (10 - i);
        soma2 += cpf.charAt(i) * (11 - i);
    }
    if(((soma % 11) < 2 ? 0 : 11 - (soma  % 11 )) != cpf.charAt(9))
        return false;
    return (((soma2 % 11 ) < 2 ? 0 : 11 - (soma2 % 11 )) != cpf.charAt(10)) ? false : true;
}

/*VALIDA CNPJ*/
function validaCNPJ(cnpj) {
	if (cnpj.length < 14)
		return false;
	if(cnpj == '00000000000000')
		return false;
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	for (i=0; i<12; i++){
		a[i] = cnpj.charAt(i);
		b += a[i] * c[i+1];
	}
	if ((x = b % 11) < 2)
		a[12] = 0;
	else a[12] = 11-x;
	b = 0;
	for (y=0; y<13; y++)
		b += (a[y] * c[y]); 
	if ((x = b % 11) < 2)
		a[13] = 0;
	else a[13] = 11-x;
	if ((cnpj.charAt(12) != a[12]) || (cnpj.charAt(13) != a[13]))
		return false;
	else return true;
}

/*ELIMINA CARACTERES*/
function retiraCaracter(string, caracter) {
    var i = 0;
    var final = '';
    while (i < string.length) {
        if (string.charAt(i) == caracter) {
            final += string.substr(0, i);
            string = string.substr(i+1, string.length - (i+1));
            i = 0;
        }
        else {
            i++;
        }
    }
    return final + string;
}

//VALIDA DATA
function validaData(data){
	data = data.replace(/[\/]+/g,'');
	if(!(/^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/.test(data)))
		return false;
	else return true;
}

