<!-- Arquivo de Validações em Javascript -->
<!-- Versão 1.0 -->
<!-- Criado por Alexandre Verta Eiras - alexandre.verta@gmail.com -->
<!-- Data de criação: 17/07/2006 -->
<!-- Modificado por: -->
<!-- Data de modificação -->

function ValidaCPF(input){
	var CPF = input.value; // Recebe o valor digitado no campo

	out = "."; // replace this
	add = ""; // with this
	temp = "" + CPF; // temporary holder
		
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}

	CPF = temp;
		
	out = "-"; // replace this
	add = ""; // with this
	temp = "" + CPF; // temporary holder
		
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}

	CPF = temp;

	// Aqui começa a checagem do CPF
	var POSICAO, I, SOMA, DV, DV_INFORMADO;
	var DIGITO = new Array(10);
	DV_INFORMADO = CPF.substr(9, 2); // Retira os dois últimos dígitos do número informado

	// Desemembra o número do CPF na array DIGITO
	for (I=0; I<=8; I++) {
	  DIGITO[I] = CPF.substr( I, 1);
	}

	// Calcula o valor do 10º dígito da verificação
	POSICAO = 10;
	SOMA = 0;
	for (I=0; I<=8; I++) {
		SOMA = SOMA + DIGITO[I] * POSICAO;
		POSICAO = POSICAO - 1;
	}
	DIGITO[9] = SOMA % 11;
	if (DIGITO[9] < 2) {
		DIGITO[9] = 0;
	}
	else{
		DIGITO[9] = 11 - DIGITO[9];
	}

	// Calcula o valor do 11º dígito da verificação
	POSICAO = 11;
	SOMA = 0;
	for (I=0; I<=9; I++) {
		SOMA = SOMA + DIGITO[I] * POSICAO;
		POSICAO = POSICAO - 1;
	}
	DIGITO[10] = SOMA % 11;
	if (DIGITO[10] < 2) {
       	DIGITO[10] = 0;
	}
	else {
		DIGITO[10] = 11 - DIGITO[10];
	}

	// Verifica se os valores dos dígitos verificadores conferem
	DV = DIGITO[9] * 10 + DIGITO[10];
	if (DV != DV_INFORMADO) {
		return false;
	} 
		
	return true;	
}

function validaHora(str){
	reTime2 = /^([0-1]\d|2[0-3]):[0-5]\d$/;
	return reTime2.test(str)
}

// Verifica se data2 é maior que data1
function verificaDataMaiorQueOutra(data1, data2){
	if ( parseInt( data2.split( "/" )[2].toString() + data2.split( "/" )[1].toString() + data2.split( "/" )[0].toString() ) > parseInt( data1.split( "/" )[2].toString() + data1.split( "/" )[1].toString() + data1.split( "/" )[0].toString() ) )
		return true;
	else
		return false;
}

function ValidaData(input) {
	var data 	= input.value;
	var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})\2(\d{4})$/;
	var datadiv = data.match(datePat);
	 
	if( datadiv==null ) return false;
	 
	var dia = datadiv[1];
	var mes = datadiv[3];
	var ano = datadiv[4];
	 
	if(  dia<1 || dia>31 || mes<1 || mes>12 ) return false;
	if((mes==4 || mes==6 || mes==9 || mes==11) && dia>30) return false;
	 
	if( mes==2 )
		if(dia>29) 
			return false;
		else
			if( dia==29 && !ano_bi(ano)) return false
	
	return true;
}

function ValidaDataMenor(input){
	var data 		= input.value;
	var dataHoje	= new Date();
	var datePat 	= /^(\d{1,2})(\/|-|.)(\d{1,2})\2(\d{4})$/;
	var datadiv 	= data.match(datePat);
	 
	if( datadiv==null ) return false;
	 
	var dia = datadiv[1];
	var mes = datadiv[3];
	var ano = datadiv[4];
	
	dataValida = new Date(ano,mes,dia);
	dataValida.setMonth(dataValida.getMonth() - 1);
	
	if (dataValida > dataHoje){
		return false;
	} else {
		return true;
	}
}

function ValidaDataMaior(input){
	var data 		= input.value;
	var dataHoje	= new Date();
	var datePat 	= /^(\d{1,2})(\/|-|.)(\d{1,2})\2(\d{4})$/;
	var datadiv 	= data.match(datePat);
	 
	if( datadiv==null ) return false;
	 
	var dia = datadiv[1];
	var mes = datadiv[3];
	var ano = datadiv[4];
	
	dataValida = new Date(ano,mes,dia);
	dataValida.setMonth(dataValida.getMonth() - 1);
	
	if (dataValida < dataHoje){
		return false;
	} else {
		return true;
	}
}
 
function ano_bi(ano) {
	if( ano % 100 == 0 ) { if( ano % 400 == 0 ) return true; } else { if(( ano % 4 ) == 0 ) return true; }
	return false;
}

function ValidaEmail(str){
	reEmail = new RegExp(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/);
	return reEmail.test(str);
}

function validaTelefone(str){
    reTelefone = new RegExp(/^\([1-9]\d{1}\)\s?\d{4}\-\d{4}$/);
    return reTelefone.test(str)
}

function ValidaCNPJ(input) {	
	var cnpj = input.value;
	var c, j, i, dv;
	var aux_cnpj = "";
	var dl = 0;
	
	out = "."; // replace this
	add = ""; // with this
	temp = "" + cnpj; // temporary holder
		
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}

	cnpj = temp;
		
	out = "-"; // replace this
	add = ""; // with this
	temp = "" + cnpj; // temporary holder
		
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}

	cnpj = temp;
	
	out = "/"; // replace this
	add = ""; // with this
	temp = "" + cnpj; // temporary holder
		
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}

	cnpj = temp;

	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;
	}
}
