/* funções:
	valida_data(campo) -> utilizada na validação de campos tipo date 
	maximo(campo,length) -> valida se o 'campo' passado é >= ao 'length' e retorna false
	valida_campo(campo,teste,msg) -> valida se o 'campo' passado é igual ao 'teste' e devolve uma 'msg' 
	valida_campo_duplo(campo1,teste1,campo2,teste2,msg) -> valida se o 'campo1' é igual ao 'teste1' e o 'campo2' é igual ao 'teste2' e devolve um alert() e false 
	valida_cpf(campo) -> valida o valor 'campo' passado é um cpf válido
	validaEmail(campo) -> valida o email atrave´s do 'campo' passado
*/

function valida_data(campo){ 
	var checkstr = "0123456789"; 
	var DateField = campo; 
	var Datevalue = ""; 
	var DateTemp = ""; 
	var seperator = "/"; 
	var day; 
	var month; 
	var year; 
	var leap = 0; 
	var err = 0; 
	var i; 
	err = 0; 

	DateValue = DateField.value; 
	/* Deleta todos os caracteres exceto 0..9 */ 
	for (i = 0; i < DateValue.length; i++) { 
		if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) { 
			DateTemp = DateTemp + DateValue.substr(i,1); 
		} 
	} 

	DateValue = DateTemp; 

	/* Sempre modifica a data para 8 digitos*/ 

	/* Se o ano for digitado com 2 digitos assume 20xx */ 

	if (DateValue.length == 6) { 
		DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); 
	} 

	if (DateValue.length != 8) { 
		err = 1;
	} 

/* ano é considerado inválido se for = 0000 */ 

	year = DateValue.substr(4,4); 
	if (year == 0) { 
		err = 1; 
	} 

/* Validação do Mês */ 

	month = DateValue.substr(2,2); 
	if ((month < 1) || (month > 12)) { 
		err = 1; 
	} 

/* Validação do Dia */ 
	day = DateValue.substr(0,2); 
	if (day < 1) { 
		err = 1; 
	} 

/* Validação do ano bissexto referente ao mês de fevereiro */ 

	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) { 
		leap = 1; 
	} 

	if ((month == 2) && (leap == 1) && (day > 29)) { 
		err = 1; 
	} 

	if ((month == 2) && (leap != 1) && (day > 28)) { 
		err = 1; 
	} 

/* Validação dos outros meses */ 

	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) { 
		err = 1; 
	} 

	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) { 
		err = 1; 

	} 
/* Se não houver erro escreve a data completa no campo input com os separadores (ex. 07/01/2004) */ 

	if (err == 0) { 
		DateField.value = day + seperator + month + seperator + year; 
	} 
/* Escreve mensagem de erro se err != 0 */ 
	else { 
		alert("Data Incorreta!!!"); 
		DateField.select(); 
		DateField.focus();
		return false;
	} 
	return (true);
}

function maximo(campo,length){
 	if (campo.value.length >= length)
		return false;
}

function validar_campo(campo,valor,msg){
	switch(campo.type){
		case 'text':
			if(campo.value == valor){
				alert(msg);
				campo.focus();
				return false;
			}else
				return true;				
			break;
		case 'password':
			if(campo.value == valor){
				alert(msg);
				campo.focus();
				return false;
			}else
				return true;				
			break;
		case 'select-one':
			if(campo.value == valor){
				alert(msg);
				campo.focus();
				return false;
			}else
				return true;
			break;
		case 'textarea':
			if(campo.value == valor){
				alert(msg);
				campo.focus();
				return false;
			}else
				return true;				
			break;
	}
}


function valida_campo_duplo(campo1,teste1,campo2,teste2,msg){
	if( (campo1.value == teste1) && (campo2.value == teste2) ){
		alert(msg);		
		return false;
	}
	else
		return true;
}

function valida_cpf(campo) {
	numero  =   campo.value.replace(/\.|\-/gi,'');
	cpf 	= new Array(11); 
	
	if (numero.length != 11 || numero == "00000000000" || numero == "11111111111" || numero == "22222222222" ||	numero == "33333333333" || numero == "44444444444" || numero == "55555555555" || numero == "66666666666" || numero == "77777777777" || numero == "88888888888" || numero == "99999999999")
		return false;
		
	cpf[0]  = numero.substr(0, 1);
	cpf[1]  = numero.substr(1, 1);
	cpf[2]  = numero.substr(2, 1);
	cpf[3]  = numero.substr(3, 1);
	cpf[4]  = numero.substr(4, 1);
	cpf[5]  = numero.substr(5, 1);
	cpf[6]  = numero.substr(6, 1);
	cpf[7]  = numero.substr(7, 1);
	cpf[8]  = numero.substr(8, 1);
	cpf[9]  = numero.substr(9, 1);
	cpf[10] = numero.substr(10, 1);

	soma = 0;	
	for (i=0; i < 9; i ++)
		soma += parseInt(cpf[i]) * (10 - i);
	
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
				
	if (resto != parseInt(cpf[9]))
		return false;	
		
	soma = 0;	
	for (i = 0; i < 10; i ++)
		soma += parseInt(cpf[i]) * (11 - i);
			
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
		
	if (resto != parseInt(cpf[10]))
		return false;
		
	return (true);	
 }

function validaEmail(campo){
	var string = campo.value;
	if (string.indexOf('@')>0 && string.indexOf('[')<0  && string.indexOf(']')<0 && string.indexOf('(')<0 && string.indexOf(')')<0  && string.indexOf('/')<0 && string.indexOf(' ')<0){
		string = string.split("@")		 
		if(string.length <=2 && string[0].length > 1){
			 partes = string[1].split(".");
			if(partes.length >= 2 && partes.length <=3){
				if(partes[0].length > 1 && (partes[1].length >= 2 && partes[1].length <= 3) && ((partes.length==3 && partes[2].length == 2) || (partes.length == 2))){			
					return true;
				}		
			}
		}
	}
	alert("Endereço de email inválido!");
	return false;
}

function validaCNPJ(CNPJ) {
	erro = new String;
		if (CNPJ == "00.000.000/0000-00") erro += "CNPJ inválido!";
		if (CNPJ.length < 18) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
		if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
		if (erro.length == 0) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
		}

		//substituir os caracteres que não são números
		if(document.layers && parseInt(navigator.appVersion) == 4){
			x = CNPJ.substring(0,2);
			x += CNPJ. substring (3,6);
			x += CNPJ. substring (7,10);
			x += CNPJ. substring (11,15);
			x += CNPJ. substring (16,18);
			CNPJ = x; 
		} else {
			CNPJ = CNPJ. replace (".","");
			CNPJ = CNPJ. replace (".","");
			CNPJ = CNPJ. replace ("-","");
			CNPJ = CNPJ. replace ("/","");
		}
		var nonNumbers = /\D/;
		if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! \n\n"; 
			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])){
		erro +="CNPJ inválido!";
		}
		if (erro.length > 0){
			alert(erro);
			return false;
		} 
	return true;
}
