// JavaScript Document
var valorCookie, idiomaAtual;
valorCookie = getCookie("OtorrinoUsp");
//Teste de alteração
if(valorCookie == null)
	valorCookie = "";
	
if( valorCookie.indexOf("idioma=Ingles") != -1 ) { // Inglês selecionado
	idiomaAtual = "Ingles"

	txt_branco = "The field ";
	txt_branco2 = " is required.";
	txt_email = "The e-mail address is invalid.";
	txt_numeroTelefone = "Invalid phone number. \nEnter the area code and phone number.\nExample: (12) 1234-1234"
	
} else if( valorCookie.indexOf("idioma=Espanhol") != -1 ) { // Espanhol selecionado
	idiomaAtual = "Espanhol"

	txt_branco = "El campo ";
	txt_branco2 = " debe llenarse.";
} else {
	idiomaAtual = "Portugues"
	
	txt_branco = "O campo ";
	txt_branco2 = " dever ser preenchido.";
	txt_email = "O e-mail deve ser um endereço de e-mail válido.";
	txt_numeroTelefone = "Número de telefone inválido. \nEntre com o DDD e o número do telefone.\nExemplo: (12) 1234-1234"
}


function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function checkInput(){
	var idObj = this.id;
	if(document.getElementById(idObj).value){
		document.getElementById(idObj).style.backgroundColor = "#fff";
	} else document.getElementById(idObj).style.backgroundColor = "#ffe200";
}

function checkSelect(){
	var idObj = this.id;
	if(document.getElementById(idObj).selectedIndex != 0){
		document.getElementById(idObj).style.backgroundColor = "#fff";
	} else document.getElementById(idObj).style.backgroundColor = "#ffe200";
}

function valida_tel(campo) {
   var MensIdioma = txt_numeroTelefone;
   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 (MensIdioma);
      campo.value = '';
      campo.focus();
      }
	  else
	  {
		  if (idiomaAtual == "Portugues"){		  
			  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;
		  }
	  }
   }
}

function valida_email(campoform)
{
	var MensIdioma = txt_email;
	var email = campoform.value;
	
	if (email != ""){	
		if (email) {
			p=email.indexOf('@');
			pont=email.indexOf('.');
			if (p<1 || p==(email.length-1) || pont<1 || pont==(email.length-1))
			{
				alert (MensIdioma);
				campoform.value = '';
				//campoform.focus();
				return false;
			} else return true;
		} else {
			alert (MensIdioma);
			campoform.value = '';
			//campoform.focus();
			return false;
		}
	}
}


// 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( txt_branco + nome.toUpperCase() + txt_branco2 );
		else
			alert( txt_branco + campo.name.toUpperCase() + txt_branco2 );
		
		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;
}

// Valida um URL
function validaURL( campo ){
	campo = document.getElementById( campo );

	if( campo.value.substr(0, 7) != "http://" ){
		alert( "O campo " + campo.name.toUpperCase() + " não contém um endereço válido." );
		campo.focus();
		return false;
	}
	return true;
}
