// JavaScript Document

var randomico = "&rnd=" + Math.random();


function AjaxRequest() {

	try	{
		// Firefox, Opera 8.0+, Safari
		Ajax=new XMLHttpRequest();
	}

	catch (e)
	{
		// Internet Explorer
		try
		{
		Ajax=new ActiveXObject("Msxml2.XMLHTTP");
		}
	catch (e)
		{
		try
			{
			Ajax=new ActiveXObject("Microsoft.XMLHTTP");
			}
		catch (e)
		{
			alert("Your browser does not support AJAX!");
			return false;
		}
	}
}
}


function focaCampo(objeto) {
	objeto.focus();
}


function alternaFormulario(tipo) {
	if (tipo == "Brasil") {
		document.getElementById("tbFormularioOutros").style.display = "none";
		document.getElementById("tbFormularioBrasil").style.display = "block";
		document.getElementById("nome").focus();
	} else {
		document.getElementById("tbFormularioOutros").style.display = "block";
		document.getElementById("tbFormularioBrasil").style.display = "none";
		document.getElementById("nome2").focus();
	}

}

document.onload=alternaFormulario(document.getElementById("pais").value);


function checaMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){
                    return true;
                }
    }else{
        return false;
        }
}


function alteraCor(objeto) {
	
	corAtual = objeto.style.color;
	fundoAtual = objeto.style.backgroundColor;
	
	objeto.style.color = "#FFFFFF";
	objeto.style.backgroundColor = "#FF0000";
	
	focaCampo(objeto);
	
	setTimeout(function(){						
						objeto.style.color = corAtual;
						objeto.style.backgroundColor = fundoAtual;						
						}, 2000);
	
}

function autoTab(campo, quantidade, proximo) {
	if (campo.value.length == quantidade) {
		document.getElementById(proximo).focus();
	}
}


function validaFormulario() {

	// apenas números;
	var regExp = new RegExp(/\d\b/);

	var pais = document.getElementById("pais");

	if(pais.value == "Brasil") {

	// nome
	var nome = document.getElementById("nome");
	
	if (nome.value.length < 2) {
		alert("Por favor, verifique a digitação de seu nome.");
		alteraCor(nome);
		return false;
	}
	
	if (nome.value.indexOf(" ") <= 0) {
		alert("Por favor, preencha com nome e sobrenome.");
		alteraCor(nome)
		return false;
	}
	
	if (nome.value.indexOf(" ") == nome.value.length) {
		alert("Por favor, preencha com nome e sobrenome.");
		alteraCor(nome)
		return false;
	}
	
	
	// email
	var email = document.getElementById("email");
	
	if (email.value.length == 0) {
		alert("Por favor, preencha com seu e-mail.");
		alteraCor(email);
		return false;
	}
	
	if(!checaMail(email)) {
		alert("Por favor, preencha com seu e-mail corretamente.");
		alteraCor(email);
		return false;
	}
	
	
	// endereço
	var endereco = document.getElementById("endereco");
	
	if (endereco.value.length < 2) {
		alert("Por favor, preencha seu endereço corretamente.");
		alteraCor(endereco);
		return false;
	}
	
	// bairro
	var bairro = document.getElementById("bairro");
	
	if (bairro.value.length < 2) {
		alert("Por favor, informe seu bairro.");
		alteraCor(bairro);
		return false;
	}
	
	// cep
	var cep = document.getElementById("cep");
	if (!regExp.test(cep.value)) {
		alert("Por favor, verifique a digitação de seu CEP.\nUtilize apenas números.");
		alteraCor(cep);
		return false;
	}
	
	if (cep.value.length < 8){
		alert("Por favor, verifique a digitação de seu CEP.");
		alteraCor(cep);
		return false;
	}
	
	// cidade
	var cidade = document.getElementById("cidade");
	if (cidade.value.length < 3) {
		alert("Por favor, informe corretamente o nome de sua cidade.");
		alteraCor(cidade);
		return false;
	}
	
	// ddd 
	var ddd = document.getElementById("ddd");
	if (!regExp.test(ddd.value)) {
		alert("Por favor, verifique a digitação de seu DDD");
		alteraCor(ddd);
		return false;
	}
	
	if (ddd.value.length < 2) {
		alert("Por favor, digite o seu DDD com 2 dígitos.");
		alteraCor(ddd);
		return false;
	}
	
	// telefone
	var tel = document.getElementById("tel");
	
	if (tel.value.length < 6) {
		alert("Por favor, preencha corretamente o número de seu telefone/celular.");
		alteraCor(tel);
		return false;
	}
	
	if(!regExp.test(tel.value)) {
		alert("Por favor, preencha corretamente o número de seu telefone/celular.\nUtilize apenas números.");
		alteraCor(tel);
		return false;
	}
	
	// formação
	var formacao = document.getElementById("formacao");
	if (formacao.value == 0) {
		alert("Por favor, selecione sua formação.");
		alteraCor(formacao);
		return false;
	}
	
	} else {
		
	// VALIDAÇÃO PARA OS QUE ESTÃO FORA DO BRASIL.	
	// nome
	var nome = document.getElementById("nome2");
	
	if (nome.value.length < 2) {
		alert("Por favor, verifique a digitação de seu nome.");
		alteraCor(nome);
		return false;
	}
	
	if (nome.value.indexOf(" ") <= 0) {
		alert("Por favor, preencha com nome e sobrenome.");
		alteraCor(nome)
		return false;
	}
	
	
	// email
	var email = document.getElementById("email2");
	
	if (email.value.length == 0) {
		alert("Por favor, preencha com seu e-mail.");
		alteraCor(email);
		return false;
	}
	
	if(!checaMail(email)) {
		alert("Por favor, preencha com seu e-mail corretamente.");
		alteraCor(email);
		return false;
	}
		
	// cidade
	var cidade = document.getElementById("cidade2");
	if (cidade.value.length < 3) {
		alert("Por favor, informe corretamente o nome de sua cidade.");
		alteraCor(cidade);
		return false;
	}

	// estado
	var estado = document.getElementById("estado2");
	if (estado.value.length < 2) {
		alert("Por favor, informe o seu Estado/Província.");
		alteraCor(estado);
		return false;
	}
	
	}
}


function montaEstado(padrao, callback) {
	var retorno = document.getElementById("campoEstado");
	retorno.innerHTML = "<center><img src='/guia/interfaces/img/loader.gif'></center>"

	AjaxRequest();

	Ajax.onreadystatechange=function(){
		
		if (Ajax.readyState == 4) {
			retorno.innerHTML = Ajax.responseText;
			if(callback){ eval(callback); }
		}
		
	};
		
	
	var url;
	url = "/guia/interfaces/montaLocalidades.asp?f=e";
	if (padrao) {
		url += "&p=" + padrao;
	}
		
	Ajax.open("GET", url + randomico, true)
	Ajax.send(null);	
}

function montaCidade(estado) {
	var retorno = document.getElementById("campoCidade");
	retorno.innerHTML = "<center><img src='/guia/interfaces/img/loader.gif'></center>"
	
	AjaxRequest();
	
	Ajax.onreadystatechange=function() {
		if (Ajax.readyState == 4) {
			retorno.innerHTML = Ajax.responseText;
		}
	}
	
	var url;
	url = "/guia/interfaces/montaLocalidades.asp?f=c&uf=" + estado;	
	Ajax.open("GET", url + randomico, true);
	Ajax.send(null)
	
}