// JavaScript Document
// Validação de campo vazio. Param: campo, nome do campo para exibição
function validaBranco( campo, nome ){
	campo = document.getElementById( campo );

	if( campo.value == "" ) {
		if( nome != null )
			alert( "O campo " + nome.toUpperCase() + " dever ser preenchido." );
		else
			alert( "O campo " + campo.name.toUpperCase() + " deve ser preenchido." );
		
		campo.focus();
		return false;
	}
	return true;
}

// Validação de campo vazio sem exibir alert
function validaBranco2( campo ){
	campo = document.getElementById( campo );

	if( campo.value == "" ) 
		return false;
	return true;
}

//Validação de email
function validaEmail( campo )
{
	campoform = document.getElementById( campo );
	email = campoform.value;
    if ( email )
	{
		p = email.indexOf('@');
		pont = email.indexOf('.');
		if ( p < 1 || p == (email.length - 1) || pont < 1 || pont == (email.length - 1) )
		{
				alert ('O e-mail deve ser um endereço de e-mail válido.');
	    		campoform.value = '';
	    		campoform.focus();
				return false;
		}
	} else {
		alert ('O e-mail deve ser um endereço de e-mail válido.');
  		campoform.value = '';
	    campoform.focus();
		return false;
	}
	return true;
}

//Validação de Telefone
function validaTelefone( campoPar ) {
	campo = document.getElementById( campoPar );
	var valor='';
	var digito = false;
	a = campo.value;

	if ( a != "" ) {
		num = a.length;
		for (f=0;f<num;f++)
		{
			if (parseInt(a.substr(f,1)) || a.substr(f,1)=='0') 
			if ((a.substr(f,1) != '0') || digito) valor = valor + '' + a.substr(f,1);
			if (parseInt(a.substr(f,1)) && a.substr(f,1) != '0') digito = true;
		};
		num = valor.length;
		if (num < 9 || num > 10)
		{
			alert ('- Número de telefone inválido. \nEntre com o DDD e o número do telefone.\nExemplo: (11) 1234-5678');
			campo.focus();
			return false;
		}
		else
		{
			if (num == 9) valor = '(' + valor.substr(0, 2) + ') ' + valor.substr(num-7, 3) + '-' + valor.substr(num-4, 4);
			if (num == 10) valor = '(' + valor.substr(0, 2) + ') ' + valor.substr(num-8, 4) + '-' + valor.substr(num-4, 4);
			campo.value = valor;
		}
	}
	else{
		alert ('- Número de telefone inválido. \nEntre com o DDD e o número do telefone.\nExemplo: (11) 1234-5678');
		campo.focus();
		return false;
	}
	return true;
}

// validação de data
function validaData( campo ){
	var data = document.getElementById(campo);

	hoje = new Date();
    anoAtual = hoje.getFullYear();
   	barras = data.value.split("/");
    if (barras.length == 3){
		dia = barras[0];
        mes = barras[1];
   	    ano = barras[2];
        resultado = (!isNaN(dia) && (dia > 0) && (dia < 32)) && (!isNaN(mes) && (mes > 0) && (mes < 13)) && (!isNaN(ano) && (ano.length == 4) );
   	    if (!resultado) {
			alert("O formato da data é invalido!");
            data.focus();
            return false;
		}
	}
	else {
		alert("O formato da data é invalido!");
		data.focus();
        return false;
   	}
	return true;
}

//validação de números
function validaNumero( campo ){
	var numero = document.getElementById( campo );
	if ( isNaN( numero.value ) ){
		alert( "O campo " + numero.name.toUpperCase() + " deve conter apenas números." );
		numero.focus();
		return false;
	}
	return true;
}

// comparação de 2 datas
function comparaData( data1, data2 ){
	var data1 = document.getElementById( data1 );
	var data2 = document.getElementById( data2 );
		
	var dataArray1 = data1.value.split( "/" );
	var dataArray2 = data2.value.split( "/" );
	
	var dData1 = new Date( dataArray1[2], dataArray1[1] - 1, dataArray1[0] );
	var dData2 = new Date( dataArray2[2], dataArray2[1] - 1, dataArray2[0] );

	if ( dData1.getTime() > dData2.getTime() )
		return 1;
	else if ( dData1.getTime() < dData2.getTime() )
		return -1;
	else
		return 0;
}