var abaSelecionada = "";

function SelecionaAba(aba) {
	if (aba == abaSelecionada)
		return;
		
	obj = document.getElementById("divAba"+aba);
	obj.style.display = 'inline';
	
	obj = document.getElementById("tdAba"+aba);
	obj.className = 'AbaSelecionada'

	if (abaSelecionada!="") {
		obj = document.getElementById("divAba"+abaSelecionada);
		obj.style.display = 'none';

		obj = document.getElementById("tdAba"+abaSelecionada);
		obj.className = 'AbaNaoSelecionada'
	}
	
	abaSelecionada = aba;
}
	
function ValidaForm(frm) {
	for (x=0; x<frm.elements.length; x++) {
		if (frm.elements[x].value.length==0) {
			if (frm.elements[x].getAttribute("required") == 'true') {
				try {
					pai = frm.elements[x].parentElement;
					while(pai.id.indexOf("divAba")<0) {
						pai = pai.parentElement;
					}
					SelecionaAba(pai.id.substring(6, pai.id.length));
				}
				catch (e){
				}
				alert(frm.elements[x].getAttribute("errorMessage"));
				frm.elements[x].focus();
				return false;
			}
		}
		else {
			if (frm.elements[x].getAttribute("typeField")) {
				switch (frm.elements[x].getAttribute("typeField")) {
					case "number" : 
						ret = ValidaNumero(frm.elements[x].value);
						break;
					case "email" : 
						ret = ValidaEmail(frm.elements[x].value);
						break;
					case "telefone" : 
						ret = ValidaTel(frm.elements[x].value);
						break;
					case "CPF" : 
						ret = ValidaCPF(frm.elements[x].value);
						break;
					case "CNPJ" : 
						//ret = ValidaCNPJ(frm.elements[x].value);
						ret = true;
						break;
					case "data" : 
						ret = ValidaData(frm.elements[x].value);
						break;
					default :
						ret = true;
				}
				if (!ret) {
					try {
						pai = frm.elements[x].parentElement;
						while(pai.id.indexOf("divAba")<0) {
							pai = pai.parentElement;
						}
						SelecionaAba(pai.id.substring(6, pai.id.length));
					}
					catch (e){
					}
					alert(frm.elements[x].getAttribute("errorMessageType"));
					frm.elements[x].focus();
					return false;
				}
			}
		}		
	}
	return true;
}

function ValidaNumero(num)
{
	for (var i=0;i<num.length;i++) {
		if ((num.charAt(i) < "0") || (num.charAt(i) > "9")) {
			return false;
		}
	}
	return true;
}


function ValidaTel(num) {
 	num = num.replace('-','');
	if (ValidaNumero(num)) {
		if (num.length < 7 || num.length > 8) {
			return false
		}
		else {
			return true ;
		}
	}
	else {
		return false;
	}
}

function ValidaEmail(email) {
        var achou_ponto=false;
        var achou_arroba=false;
        var achou_caracter=false;
        for (var i=0; i<email.length; i++) {
                if (email.charAt(i)=="@") achou_arroba=true;
                else if (email.charAt(i)==".") achou_ponto=true;
                else if (email.charAt(i)!=" ") achou_caracter=true;
        }
        return (achou_ponto & achou_arroba & achou_caracter);
}

function ValidaCPF(strCpf)
{
	var varFirstChr = strCpf.charAt(0);	
	var vaCharCPF = false;
	for(var i=0;i<=10;i++){
		var c = strCpf.charAt(i);             
		if(!(c>='0')&&(c<='9')){
			return false;
	    }              
	    if(c!=varFirstChr)
			vaCharCPF = true;
	}
	if(!vaCharCPF){
		return false;
	}
	soma=0;	
	for(i=0;i<9; i++){ 
		soma += (10-i) * ( eval(strCpf.charAt(i)) );
	}
	digito_verificador = 11-(soma % 11);
	if((soma % 11) < 2)
		digito_verificador = 0;	
		if (eval(strCpf.charAt(9)) != digito_verificador){
			return false;
		}
		soma=0;	
		for(i=0;i<9; i++){
			soma += (11-i)*(eval(strCpf.charAt(i)));
		}
		soma += 2*(eval(strCpf.charAt(9)));
		digito_verificador = 11-(soma % 11);
		if((soma % 11)<2) 
			digito_verificador = 0;
		if(eval(strCpf.charAt(10)) != digito_verificador){ 
			return false;
		}
	return true;
}

function ValidaData (data) {
	separador = '/';
	aux = data;
	barras = 0;
	erro = 0;
	dia = 0;
	mes = 0;
	ano = 0;
	eh_barra = 1;

	meses = new Array(12);
	meses = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	
	for (var i=0; (i<aux.length) && (erro==0); i++) {
		if (aux.charAt(i)==separador) {
			if (eh_barra==0) {
				barras++;
				eh_barra = 1;
			}
			else erro = 1;
		}
		else {
			a = parseInt(aux.charAt(i), 10);
			if (isNaN(a)) erro = 1;
			eh_barra = 0;
		}
	}
	if (barras != 2) erro = 1;

	if (erro==0) {
		pos = aux.indexOf(separador);
		pos2 = aux.indexOf(separador, pos+1);
		dia = parseInt(aux.substring(0, pos), 10);
		mes = parseInt(aux.substring(pos+1, pos2), 10);
		ano = parseInt(aux.substring(pos2+1, aux.length), 10);
		if (ano<1900 || ano>2050) erro = 1;
		else {
			if (eh_bissexto(ano)) meses[1] = 29;
			else meses[1] = 28;
			if (mes<1 || mes>12) erro = 1;
			else if (dia<1 || dia>meses[mes-1]) erro = 1;
		}
	}
	if (erro==1) return false;
	return true;
}


function eh_bissexto(ano)
{
	if ( ((ano - 1996) % 4) == 0) return true;
	return false;
}

function CriaArray(n) {
	this.length=n
	for (var i=1;i<=n;i++)
		{this[i]=""}
}

function ValidaCNPJ(campo) {
 with (Math) {
	 w = 0;
	 Resp1 = "";
	 Resp2 = "";
 	
 	 campo = campo.replace('/','');
 	 campo = campo.replace('-','');
 	 campo = campo.replace('.','');
 	 campo = campo.replace('.','');
	 CGC = campo;

	 if (CGC.length != 14) {
	 	return false;
	 }
 
	 if (!ValidaNumero(CGC)) { 
	 	return false;
	 }
 
	 VtCGC = new CriaArray(CGC.length);

	 for (var i=0;i < CGC.length;i++) {
 		if ((CGC.charAt(i) == "0") || (CGC.charAt(i) == "1") || (CGC.charAt(i) == "2") || (CGC.charAt(i) == "3") || (CGC.charAt(i) == "4") || (CGC.charAt(i) == "5") || (CGC.charAt(i) == "6") || (CGC.charAt(i) == "7") || (CGC.charAt(i) == "8") || (CGC.charAt(i) == "9")) {
	 		VtCGC[w]=parseFloat(CGC.charAt(i));	
	 		w++;
		}
 	}


 	Soma1 = (VtCGC[0]*5)+(VtCGC[1]*4)+(VtCGC[2]*3)+(VtCGC[3]*2)+(VtCGC[4]*9)+(VtCGC[5]*8)+(VtCGC[6]*7)+(VtCGC[7]*6)+(VtCGC[8]*5)+(VtCGC[9]*4)+(VtCGC[10]*3)+(VtCGC[11]*2)+0.0001;
 	Divisao1 = Soma1 / 11; 
 	RestoParc1 = (Divisao1 - floor(Divisao1))*11;
 	Resto1 = floor(RestoParc1);
 
 	Soma2 = (VtCGC[0]*6)+(VtCGC[1]*5)+(VtCGC[2]*4)+(VtCGC[3]*3)+(VtCGC[4]*2)+(VtCGC[5]*9)+(VtCGC[6]*8)+(VtCGC[7]*7)+(VtCGC[8]*6)+(VtCGC[9]*5)+(VtCGC[10]*4)+(VtCGC[11]*3)+(VtCGC[12]*2)+0.0001;
 	Divisao2 = Soma2 / 11; 
 	RestoParc2 = (Divisao2 - floor(Divisao2))*11;
 	Resto2 = floor(RestoParc2);


 	if (((Resto1 == 0) || (Resto1 == 1)) && (VtCGC[12] == 0)) {
  		Resp1 = "V";
 	} else {
  		Digito1 = 11 - Resto1;
  		if ((Digito1 == VtCGC[12]) && (Resto1 > 1)) {
			Resp1 = "V";
  		}
 	}
 
 	if (((Resto2 == 0) || (Resto2 == 1)) && (VtCGC[13] == 0)) {
  		Resp2 = "V";
 	} else {
  		Digito2 = 11 - Resto2;
  		if ((Digito2 == VtCGC[13]) && (Resto2 > 1)) {
   			Resp2 = "V";
  		}
 	}
 
 	if ((Resp1 == "V") && (Resp2 == "V")) 
 	{
   		return true;
 	} else {
  		return false;   
 	} 
 }
}


function formatarCPF(campo,teclaPres)
{
	var tecla=teclaPres.keyCode;
	vr = "";
	for(i=0;i<campo.value.length;i++)
		if(campo.value.charAt(i)!="." && campo.value.charAt(i)!="-" && campo.value.charAt(i)!="/")
			vr=vr + campo.value.charAt(i);
	tam = vr.length ;
	if (tam < 15 && tecla != 8){tam = vr.length + 1 ;}
	if (tecla == 8){ tam = tam - 1 ;}
	if (tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
		if (tam <= 2) { campo.value = vr ;}
		if ((tam > 2) && (tam <= 5) ){ campo.value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ) ; }
		if ((tam >= 6) && (tam <= 8) ){ campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ; }
		if ((tam >= 9) && (tam <= 11) ){campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ; }
	}
}

function formatarCNPJ(campo,teclaPres)
{
	var tecla=teclaPres.keyCode;
	vr = "";
	for(i=0;i<campo.value.length;i++)
		if(campo.value.charAt(i)!="." && campo.value.charAt(i)!="-" && campo.value.charAt(i)!="/")
			vr=vr + campo.value.charAt(i);
	tam = vr.length ;

	if (tam < 14 && tecla != 8){tam = vr.length + 1;}
	if (tecla == 8){ tam = tam - 1 ;}
	if (tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
		if (tam <= 2) { campo.value = vr ;}
		if ((tam > 2) && (tam <= 5) ) { campo.value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ) ; }
		if ((tam >= 6) && (tam <= 8) ) { campo.value = vr.substr( 0, tam - 5 ) + '/' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ; }
		if ((tam >= 9) && (tam <= 11) ) {campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '/' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ; }
		if (tam >=12) { campo.value = vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
		if ((tam >= 13) && (tam <= 15) ) { campo.value = vr.substr( 0, tam - 12 ) + '.' +vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam );}
	}
}

function formatarTelefone(campo,teclaPres)
{
	var tecla=teclaPres.keyCode;
	vr = "";
	for(i=0;i<campo.value.length;i++)
		if(campo.value.charAt(i)!="-")
			vr=vr + campo.value.charAt(i);
	tam = vr.length ;
	if (tecla == 8){ tam = tam - 1 ;}
	if (tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
		if (tam < 3) { campo.value = vr ;}
		if ((tam >= 3) && (tam <= 6) ) { 
			campo.value = vr.substr(0,3) + '-' + vr.substr(3,tam-3);
		}
		if (tam > 6) { 
			campo.value = vr.substr(0,4) + '-' + vr.substr(4,tam-4);
		}
	}
}




