// JavaScript Document

/* FUNCOES */
function redirect(pag){ location.replace(pag); }
function mostraMsg(msg){ window.attachEvent("onload", alert(msg) ); }
function maximaExtensao (campo, maximo){ if( campo.value.length>maximo ){ campo.value=campo.value.substring(0, maximo-1);} }
function showHide(id,mostra){
	i=0;
	while ( document.getElementById(id+i) ){ document.getElementById(id+i).style.display = "none"; i++; }
	document.getElementById(id+mostra).style.display = "block";
}
function openWin(url,valor){ 
	janela = window.open(url,'_blank','location=no,directories=no,status=no,menubar=no'+valor);
	janela.focus();
}
function getBusca(id){ sendRequest('busca.php','conteudo','q='+document.getElementById(id).value); }
/**/

/* NEWSLETTER */
function addNewsletter(){
	var poststr = "nome=" + encodeURI ( document.getElementById("nome").value ) +
				"&email=" + encodeURI ( document.getElementById("email").value );
	//alert (poststr);
	sendRequest('post.php?tipo=newsletter&'+poststr,'newsletter');
}

function getNewsletter(){
	if (document.getElementById("nome").value==""){
		alert("Informe seu nome!");
		document.getElementById("nome").focus();
		return false;
	} else {
		var txt = strlen(document.getElementById("nome").value);
		if ( txt <= 5 ){
			alert("Preencha o nome completo!");
			document.getElementById("nome").focus();
			return false;
		}
	}
	if (document.getElementById('email').value==""){
		alert("Informe seu e-mail!");
		document.getElementById('email').focus();
		return false;
	} else {
		mail = document.getElementById('email');
		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)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else if(typeof(mail) == "object"){
			if(!er.test(mail.value)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else{
			alert('E-mail inválido!'); 
			mail.focus();
			return false;
		}
	}
	
	return true;
}

/* CONTATO */
function addContato(){
	var poststr = "empresa=" + encodeURI ( document.getElementById("empresa").value ) +
				"&nome=" + encodeURI ( document.getElementById("nome").value ) +
				"&email=" + encodeURI ( document.getElementById("email").value ) +
				"&telefone=" + encodeURI ( document.getElementById("telefone").value ) +
				"&bairro=" + encodeURI ( document.getElementById("bairro").value ) +
				"&cidade=" + encodeURI ( document.getElementById("cidade").value ) +
				"&estado=" + encodeURI ( document.getElementById("estado").value ) +
				"&cep=" + encodeURI ( document.getElementById("cep").value ) +
				"&comentarios=" + encodeURI ( document.getElementById("comentarios").value );
	//alert (poststr);
	sendRequest('post.php?tipo=contato&'+poststr , 'contato');
}
function getContato(){
	if (document.getElementById("nome").value==""){
		alert("Informe seu nome!");
		document.getElementById("nome").focus();
		return false;
	} else {
		var txt = strlen(document.getElementById("nome").value);
		if ( txt <= 5 ){
			alert("Preencha o nome completo!");
			document.getElementById("nome").focus();
			return false;
		}
	}
	if (document.getElementById('email').value==""){
		alert("Informe seu e-mail!");
		document.getElementById('email').focus();
		return false;
	} else {
		mail = document.getElementById('email');
		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)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else if(typeof(mail) == "object"){
			if(!er.test(mail.value)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else{
			alert('E-mail inválido!'); 
			mail.focus();
			return false;
		}
	}
	if (document.getElementById("telefone").value==""){
		alert("Informe seu telefone!");
		document.getElementById("telefone").focus();
		return false;
	} else {
		var txt = strlen(document.getElementById("telefone").value);
		if ( txt <= 7 ){
			alert("Preencha o telefone corretamente!");
			document.getElementById("telefone").focus();
			return false;
		}
	}
	if (document.getElementById("bairro").value==""){
		alert("Informe o bairro!");
		document.getElementById("bairro").focus();
		return false;
	}
	if (document.getElementById("cidade").value==""){
		alert("Informe a cidade!");
		document.getElementById("cidade").focus();
		return false;
	}
	if (document.getElementById("estado").value==""){
		alert("Informe o estado!");
		document.getElementById("estado").focus();
		return false;
	}
	if (document.getElementById("cep").value==""){
		alert("Informe o CEP!");
		document.getElementById("cep").focus();
		return false;
	} else {
		var txt = strlen(document.getElementById("cep").value);
		if ( txt <= 7 ){
			alert("Preencha o CEP corretamente!");
			document.getElementById("cep").focus();
			return false;
		}
	}
	if (document.getElementById("comentarios").value==""){
		alert("Preencha o comentario!");
		document.getElementById("comentarios").focus();
		return false;
	}
	return true;
}

/* TRABALHE CONOSCO */
function addTrabalhe(){
	var poststr = "nome=" + encodeURI ( document.getElementById("nome").value ) +
				"&idade=" + encodeURI ( document.getElementById("idade").value ) +
				"&email=" + encodeURI ( document.getElementById("email").value ) +
				"&cep=" + encodeURI ( document.getElementById("cep").value ) +
				"&endereco=" + encodeURI ( document.getElementById("endereco").value ) +
				"&cidade=" + encodeURI ( document.getElementById("cidade").value ) +
				"&estado=" + encodeURI ( document.getElementById("estado").value ) +
				"&telefone=" + encodeURI ( document.getElementById("telefone").value ) +
				"&profissional=" + encodeURI ( document.getElementById("profissional").value );
	//alert (poststr);
	sendRequest('post.php?tipo=trabalhe&'+poststr , 'trabalhe');
}
function getTrabalhe(){
	if (document.getElementById("nome").value==""){
		alert("Informe seu nome!");
		document.getElementById("nome").focus();
		return false;
	} else {
		var txt = strlen(document.getElementById("nome").value);
		if ( txt <= 5 ){
			alert("Preencha o nome completo!");
			document.getElementById("nome").focus();
			return false;
		}
	}
	if (document.getElementById("idade").value==""){
		alert("Informe sua idade!");
		document.getElementById("idade").focus();
		return false;
	} else {
		var txt = strlen(document.getElementById("idade").value);
		if ( txt == 1 ){
			alert("Preencha a idade corretamente!");
			document.getElementById("idade").focus();
			return false;
		}
	}
	if (document.getElementById('email').value==""){
		alert("Informe seu e-mail!");
		document.getElementById('email').focus();
		return false;
	} else {
		mail = document.getElementById('email');
		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)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else if(typeof(mail) == "object"){
			if(!er.test(mail.value)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else{
			alert('E-mail inválido!'); 
			mail.focus();
			return false;
		}
	}
	if (document.getElementById("cep").value==""){
		alert("Informe o CEP!");
		document.getElementById("cep").focus();
		return false;
	} else {
		var txt = strlen(document.getElementById("cep").value);
		if ( txt <= 7 ){
			alert("Preencha o CEP corretamente!");
			document.getElementById("cep").focus();
			return false;
		}
	}
	if (document.getElementById("endereco").value==""){
		alert("Informe o endereco!");
		document.getElementById("endereco").focus();
		return false;
	}
	if (document.getElementById("cidade").value==""){
		alert("Informe a cidade!");
		document.getElementById("cidade").focus();
		return false;
	}
	if (document.getElementById("estado").value==""){
		alert("Informe o estado!");
		document.getElementById("estado").focus();
		return false;
	}
	if (document.getElementById("telefone").value==""){
		alert("Informe seu telefone!");
		document.getElementById("telefone").focus();
		return false;
	} else {
		var txt = strlen(document.getElementById("telefone").value);
		if ( txt <= 7 ){
			alert("Preencha o telefone corretamente!");
			document.getElementById("telefone").focus();
			return false;
		}
	}
	if (document.getElementById("profissional").value==""){
		alert("Informe sua area de interesse!");
		document.getElementById("profissional").focus();
		return false;
	}
	return true;
}
/**/

/* ENVIE PARA UM AMIGO */

function addEnvieAmigo(){
	var poststr = "nomeAmigo=" + encodeURI ( document.getElementById("nomeAmigo").value ) +
				"&emailAmigo=" + encodeURI ( document.getElementById("emailAmigo").value ) +
				"&nome=" + encodeURI ( document.getElementById("nome").value ) +
				"&email=" + encodeURI ( document.getElementById("email").value ) +
				"&cat=" + encodeURI ( document.getElementById("cat").value ) +
				"&id=" + encodeURI ( document.getElementById("id").value );
	//alert (poststr);
	sendRequest('post.php?tipo=envieAmigo&'+poststr,'envieAmigo');
}

function getEnvieAmigo(){
	if (document.getElementById("nomeAmigo").value==""){
		alert("Informe o nome de seu amigo!");
		document.getElementById("nomeAmigo").focus();
		return false;
	}
	if (document.getElementById('emailAmigo').value==""){
		alert("Informe o e-mail de seu amigo!");
		document.getElementById('emailAmigo').focus();
		return false;
	} else {
		mail = document.getElementById('emailAmigo');
		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)){ 
				alert('E-mail do amigo inválido!'); 
				mail.focus();
				return false; 
			}
		}else if(typeof(mail) == "object"){
			if(!er.test(mail.value)){ 
				alert('E-mail do amigo inválido!'); 
				mail.focus();
				return false; 
			}
		}else{
			alert('E-mail do amigo inválido!'); 
			mail.focus();
			return false;
		}
	}
	if (document.getElementById("nome").value==""){
		alert("Informe seu nome!");
		document.getElementById("nome").focus();
		return false;
	}
	if (document.getElementById('email').value==""){
		alert("Informe seu e-mail!");
		document.getElementById('email').focus();
		return false;
	} else {
		mail = document.getElementById('email');
		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)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else if(typeof(mail) == "object"){
			if(!er.test(mail.value)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else{
			alert('E-mail inválido!'); 
			mail.focus();
			return false;
		}
	}
	
	return true;
}

/* REPRESENTANTE */
function sigla(sigla){ sendRequest('mudaRep.php?est='+sigla,'rep-estado'); }
/**/

/* VALIDA FORMULARIOS */
function validaContato(){
	if (document.getElementById("nome").value==""){
		alert("O campo Nome deve ser preenchido!");
		document.getElementById("nome").focus();
		return false;
	}	
	if (document.getElementById("email").value==""){
		alert("O campo E-mail deve ser preenchido!");
		document.getElementById("email").focus();
		return false;
	}	
	if (document.getElementById("area").value==""){
		alert("O campo Área deve ser selecionado!");
		document.getElementById("area").focus();
		return false;
	}	
	if (document.getElementById("assunto").value==""){
		alert("O campo Assunto deve ser preenchido!");
		document.getElementById("assunto").focus();
		return false;
	}	
	if (document.getElementById("mensagem").value==""){
		alert("O campo Mensagem deve ser preenchido!");
		document.getElementById("mensagem").focus();
		return false;
	}	
	return true;
}

function validaTrabalhe(){
	if (document.getElementById("area").value==""){
		alert("O campo Área deve ser preenchido!");
		document.getElementById("area").focus();
		return false;
	}	
	if (document.getElementById("nome").value==""){
		alert("O campo Nome deve ser preenchido!");
		document.getElementById("nome").focus();
		return false;
	}	
	if (document.getElementById("email").value==""){
		alert("O campo E-mail deve ser preenchido!");
		document.getElementById("email").focus();
		return false;
	}	
	if (document.getElementById("telefone").value==""){
		alert("O campo Telefone deve ser preenchido!");
		document.getElementById("telefone").focus();
		return false;
	}	
	if (document.getElementById("nacionalidade").value==""){
		alert("O campo Nacionalidade deve ser preenchido!");
		document.getElementById("nacionalidade").focus();
		return false;
	}	
	if (document.getElementById("estadocivil").value==""){
		alert("O campo Estado civil deve ser selecionado!");
		document.getElementById("estadocivil").focus();
		return false;
	}	
	if (document.getElementById("nascimento").value==""){
		alert("O campo Nascimento deve ser preenchido!");
		document.getElementById("nascimento").focus();
		return false;
	}	
	if (document.getElementById("endereco").value==""){
		alert("O campo Endereço deve ser preenchido!");
		document.getElementById("endereco").focus();
		return false;
	}
	/*
	if (document.getElementById("complemento").value==""){
		alert("O campo Complemento deve ser preenchido!");
		document.getElementById("complemento").focus();
		return false;
	}	// */
	if (document.getElementById("cep").value==""){
		alert("O campo CEP deve ser preenchido!");
		document.getElementById("cep").focus();
		return false;
	}	
	if (document.getElementById("bairro").value==""){
		alert("O campo Bairro deve ser preenchido!");
		document.getElementById("bairro").focus();
		return false;
	}	
	if (document.getElementById("cidade").value==""){
		alert("O campo Cidade deve ser preenchido!");
		document.getElementById("cidade").focus();
		return false;
	}	
	if (document.getElementById("uf").value==""){
		alert("O campo UF deve ser selecionado!");
		document.getElementById("uf").focus();
		return false;
	}	
	if (document.getElementById("formacao").value==""){
		alert("O campo Formação acadêmica deve ser preenchido!");
		document.getElementById("formacao").focus();
		return false;
	}	
	if (document.getElementById("experiencia").value==""){
		alert("O campo Experiência profissional deve ser preenchido!");
		document.getElementById("experiencia").focus();
		return false;
	}	
	if (document.getElementById("cursos").value==""){
		alert("O campo Cursos extra-curriculares deve ser preenchido!");
		document.getElementById("cursos").focus();
		return false;
	}	
	if (document.getElementById("qualificacoes").value==""){
		alert("O campo Qualificações deve ser preenchido!");
		document.getElementById("qualificacoes").focus();
		return false;
	}	
	if (document.getElementById("observacoes").value==""){
		alert("O campo Observações deve ser preenchido!");
		document.getElementById("observacoes").focus();
		return false;
	}	
	return true;
}

/**/

/* AJAX */
function sendRequest(url,id,postData){
	document.getElementById(id).innerHTML = "<center><img src='./css/img/loading.gif' alt='carregando...' /></center>";
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (req.overrideMimeType) req.overrideMimeType('text/html');
	if (postData) req.setRequestHeader('Content-type','application/x-www-form-urlencoded');

	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
			alert('HTTP error ' + req.status);
			return;
		}
		document.getElementById(id).innerHTML = req.responseText; 
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try { xmlhttp = XMLHttpFactories[i](); } catch (e) { continue; }
		break;
	}
	return xmlhttp;
}
/**/

/*
 *  onblur="tiramascara(this,'_999.999.999-99');" 
 *  onkeyup="mascara(this,event,'_999.999.999-99');"
 */
function mascara(campo,e,mask){  //*
  if (e.keyCode>30 || e.keyCode==8){
    dado1=campo.value;
		completar=' ';
		dado2='';
		if (mask.charAt(0)=='_') { completar='_'; mask=mask.substr(1); }
    while (dado1.search(/[^0-9]/)>=0) {dado1=dado1.replace(/[^0-9]/,'');}
    while (completar==' ' && dado1.charAt(0)=='0') {dado1=dado1.substr(1);}

		for (i1=0,i2=0;i1<mask.length;i1++) {
			maskch=mask.charAt(mask.length-i1-1);
			if (maskch=='9' || maskch=='0') {
				if (i2<dado1.length) { 
					dado2=dado1.charAt(dado1.length-(i2++)-1)+dado2;
				}	else {
					if (maskch=='9') {
						dado2=completar+dado2;
					} else {
						dado2='0'+dado2;
					}
				}
			} else {
				if (i2<dado1.length || mask.substr(0,mask.length-i1).search(/[0]/)>=0 || completar!=' ') {
					dado2=maskch+dado2;
				} else {
					dado2=completar+dado2;
				}
			}
		}
    campo.value=dado2;
  }

}

function tiramascara(campo,mask){  //*

	dado1=campo.value; completar=' '; dado2='';
	if (mask.charAt(0)=='_') {completar='_';mask=mask.substr(1);}
	while (dado1.search(/[^0-9]/)>=0) {dado1=dado1.replace(/[^0-9]/,'');}
	while (completar==' ' && dado1.charAt(0)=='0') {dado1=dado1.substr(1);}

	for (i1=0,i2=0;i1<mask.length;i1++) {
		maskch=mask.charAt(mask.length-i1-1);
		if (maskch=='9' || maskch=='0') {
			if (i2<dado1.length) {
				dado2=dado1.charAt(dado1.length-(i2++)-1)+dado2;
			} else {
				if (maskch=='9') {dado2=dado2;} else {dado2='0'+dado2;}
			}
		} else {
			if ( i2<dado1.length || mask.substr(0,mask.length-i1).search(/[0]/)>=0 ){
				dado2=maskch+dado2;
			} else {
				dado2=dado2;
			}
		}
	}
	campo.value="";
	campo.value=dado2;
}
/**/
