/* ***************** FUNCAO *************************** */
function checkMail(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 isNum(event)
{
  var Key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  if(Key==13 || (Key >= 48 && Key <=57)) return true;
  else return false;
}

function onTopPage() {
	document.body.scrollTop=0;
}

function AbrirGaleria(galeria){
    day = new Date();
    id = day.getTime();
    pG = 0;
    eval("page" + id + " = window.open('galeria.aspx?pG=" + galeria + "', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=1,menubar=0,resizable=0,width=700,height=565,left=440,top=312');");
}

function erro_galeria() {
	alert("Não existe imagem cadastrada!");
	return false;
}


/* *******************  MENU  ********************* */

var menu_ativo = null;
var submenu_ativo = null;

function selecionar_menu(menu, submenu, seletor){
    if(document.getElementById(submenu).style.display == 'none'){
        if(submenu_ativo != null){ $("#"+submenu_ativo).hide("slow"); }
		if(menu_ativo != null){ document.getElementById(menu_ativo).className = "menu_item"; }
		if(seletor == 1){
		    if($("#"+submenu+" div").size() > 0){
		        $("#"+submenu).show("slow");
		    } else {
		        return true;
		    }
		} else {
		    if($("#"+submenu+" div").size() > 0){
		        $("#"+submenu).show();
		    }
		}
		$("#"+menu).toggleClass("menu_item_over");
        menu_ativo = menu;
        submenu_ativo = submenu;
	}
	return false;
}

function configurar_menu() {
	var id = document.getElementById('h_menu');
	if(id.value!=0) {
		var menu = document.getElementById("menu_" + id.value).id;
		var submenu = document.getElementById("submenu_" + id.value).id;
		selecionar_menu(menu,submenu,0);
	}
}

/* ***************** DEFAULT ************************** */

function changeInputType(
  oldElm, // a reference to the input element
  iType, // value of the type property: 'text' or 'password'
  iValue, // the default value, set to 'password' in the demo
  blankValue, // true if the value should be empty, false otherwise
  noFocus) {  // set to true if the element should not be given focus
  if(!oldElm || !oldElm.parentNode || (iType.length<4) || 
    !document.getElementById || !document.createElement) return;
  var newElm = document.createElement('input');
  newElm.type = iType;
  if(oldElm.name) newElm.name = oldElm.name;
  if(oldElm.id) newElm.id = oldElm.id;
  if(oldElm.className) newElm.className = oldElm.className;
  if(oldElm.style.width) newElm.style.width = oldElm.style.width;
  if(oldElm.size) newElm.size = oldElm.size;
  if(oldElm.tabIndex) newElm.tabIndex = oldElm.tabIndex;
  if(oldElm.accessKey) newElm.accessKey = oldElm.accessKey;
  newElm.onfocus = function(){return function(){
    if(this.hasFocus) return;
    var newElm = changeInputType(this,'password',iValue,
      (this.value.toLowerCase()==iValue.toLowerCase())?true:false);
    if(newElm) newElm.hasFocus=true;
  }}();
  newElm.onblur = function(){return function(){
    if(this.hasFocus)
    if(this.value=='' || (this.value.toLowerCase()==iValue.toLowerCase())) {
      changeInputType(this,'text',iValue,false,true);
    }
  }}();
 // hasFocus is to prevent a loop where onfocus is triggered over and over again
  newElm.hasFocus=false;
  oldElm.parentNode.replaceChild(newElm,oldElm);
  if(!blankValue) newElm.value = iValue;
  if(!noFocus || typeof(noFocus)=='undefined') {
    window.tempElm = newElm;
    setTimeout("tempElm.hasFocus=true;tempElm.focus();",1);
  }
  return newElm;
}

function validaCPF(cpf) {
    var nonNumbers = /\D/;
    if (nonNumbers.test(cpf)) {
        alert("Use apenas números no campo CPF! \n\n");
        return false;
    }
    if (cpf.length < 11) {
        alert("CPF inválido. Tente novamente!");
        return false;
    }    
    if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
        alert("CPF inválido. Tente novamente!");
        return false;
    }
    var a = [];
    var b = new Number;
    var c = 11;
    for (i=0; i<11; i++){
        a[i] = cpf.charAt(i);
        if (i < 9) b += (a[i] * --c);
    }
    if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
    b = 0;
    c = 11;
    for (y=0; y<10; y++) b += (a[y] * c--);
        if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
        if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
        alert("CPF inválido. Tente Novamente!");
        return false;
    }
    return true;
}

function configurar_nome(oElem,iValue,blankValue) {
	if(oElem.value==iValue || oElem.value=='') {
		oElem.value = (blankValue)?'':iValue;
	}
}

function configurar_nome_senha() {
	if(document.getElementById("pS").value=='senha') {
		changeInputType(document.getElementById("pS"),'password','senha',true,false)
	}
}

function configurar_comosoube() {		
	if(document.getElementById('d_como_soube').value == 99) { 
		document.getElementById('linha_como_soube').style.display = 'block';
	} else { 
		document.getElementById('linha_como_soube').style.display = 'none';
	}		
}

function configurar_newsletter() {		
	if(document.getElementById('cb_newsletter').checked == true) { 
		document.getElementById('linha_idioma').style.display = 'block';
	} else { 
		document.getElementById('linha_idioma').style.display = 'none';
	}		
}	

function validar_login() {
	if(document.getElementById('pL').value=='login' || document.getElementById('pL').value=='') { 
		alert('O campo login é obrigatório!');
		document.getElementById('pL').focus();
		return false; 
	}
	if (document.getElementById('pS').value=='senha' || document.getElementById('pS').value=='') { 
		alert('O campo senha é obrigatório!');
		document.getElementById('pS').focus();
		return false;
	}
	return true;
}	
	
function validar_newsletter() {
	if(document.getElementById('t_nome_news').value=='nome' || document.getElementById('t_nome_news').value=='') { 
		alert('O campo nome é obrigatório!');
		document.getElementById('t_nome_news').focus();
		return false;
	}
	if (document.getElementById('t_email_news').value=='email') { 
		alert('O campo email é obrigatório!');
		document.getElementById('t_email_news').focus();
		return false;
	}
	if(!checkMail(document.getElementById('t_email_news').value)) {
		alert('O e-mail é inválido!');
		document.getElementById('t_email_news').focus();	
		return false;
	}
	return true;
}

/* ******************* CONTATO ****************************** */

function validar_contato() {
	if(document.getElementById('t_nome').value=='') {
		alert('O campo nome é obrigatório!');
		document.getElementById('t_nome').focus();
		return false;
	}
	if(document.getElementById('t_email').value=='') {
		alert('O campo e-mail é obrigatório!');
		document.getElementById('t_email').focus();
		return false;
	}
	if(!checkMail(document.getElementById("t_email").value)) {
		alert('O e-mail é inválido!');
		document.getElementById("t_email").focus();	
		return false;
	}
	if(document.getElementById('t_empresa').value=='') {
		alert('O campo empresa é obrigatório!');
		document.getElementById('t_empresa').focus();
		return false;
	}
	if(document.getElementById('t_cargo').value=='') {
		alert('O campo cargo é obrigatório!');
		document.getElementById('t_cargo').focus();
		return false;
	}	
	if(document.getElementById('t_endereco').value=='') {
		alert('O campo endereço é obrigatório!');
		document.getElementById('t_endereco').focus();
		return false;
	}
	if(document.getElementById('t_cidade').value=='') {
		alert('O campo cidade é obrigatório!');
		document.getElementById('t_cidade').focus();
		return false;
	}
	if(document.getElementById('t_bairro').value=='') {
		alert('O campo bairro é obrigatório!');
		document.getElementById('t_bairro').focus();
		return false;
	}
	if(document.getElementById('t_estado').value=='') {
		alert('O campo estado é obrigatório!');
		document.getElementById('t_estado').focus();
		return false;
	}
	if(document.getElementById('t_pais').value=='') {
		alert('O campo país é obrigatório!');
		document.getElementById('t_pais').focus();
		return false;
	}
	if(document.getElementById('t_cep').value=='') {
		alert('O campo cep é obrigatório!');
		document.getElementById('t_cep').focus();
		return false;
	}
	if(document.getElementById('t_telefone').value=='') {
		alert('O campo telefone é obrigatório!');
		document.getElementById('t_telefone').focus();
		return false;
	}	
	if(document.getElementById('d_como_soube').value==0) { 
		alert('O campo como soube é obrigatório!');
		document.getElementById('d_como_soube').focus();
		return false 
	}
	if(document.getElementById('d_como_soube').value==99 && document.getElementById('t_como_soube').value=='') {
		alert('O campo como soube - outros é obrigatório!');
		document.getElementById('t_como_soube').focus();
		return false;
	}
	if(document.getElementById('t_atividade').value=='') { 
		alert('O campo atividade da empresa é obrigatório!');
		document.getElementById('t_atividade').focus();
		return false;
	}
	if($('input:checkbox[id*=cb_interesses]:checked').size() == 0){
	    alert('Selecione pelo menos uma área de interesse!');
		return false;
	}
	if(document.getElementById('t_mensagem').value=='') { 
		alert('O campo mensagem é obrigatório!');
		document.getElementById('t_mensagem').focus();
		return false 
	}		
	return true;
}

/* ******************* CREDENCIAMENTO ****************************** */
function validar_credencial() {
	if(document.getElementById('t_nome_completo').value=='') {
		alert('O campo nome completo é obrigatório!');
		document.getElementById('t_nome_completo').focus();
		return false;
	}
	if(document.getElementById('t_nome_cracha').value=='') {
		alert('O campo nome pra crachá é obrigatório!');
		document.getElementById('t_nome_cracha').focus();
		return false;
	}
	if(document.getElementById('t_cpf').value=='') {
		alert('O campo cpf é obrigatório!');
		document.getElementById('t_cpf').focus();
		return false;
	}
	if(validaCPF(document.getElementById('t_cpf').value) == false){
	    document.getElementById('t_cpf').focus();
	    return false;
	}
	if(document.getElementById('t_email').value=='') {
		alert('O campo e-mail é obrigatório!');
		document.getElementById('t_email').focus();
		return false;
	}
	if(!checkMail(document.getElementById("t_email").value)) {
		alert('O e-mail é inválido!');
		document.getElementById("t_email").focus();	
		return false;
	}	
	if(document.getElementById('t_empresa').value=='') {
		alert('O campo empresa é obrigatório!');
		document.getElementById('t_empresa').focus();
		return false;
	}
	if(document.getElementById('t_cargo').value=='') {
		alert('O campo cargo é obrigatório!');
		document.getElementById('t_cargo').focus();
		return false;
	}
	if(document.getElementById('t_endereco').value=='') {
		alert('O campo endereço é obrigatório!');
		document.getElementById('t_endereco').focus();
		return false;
	}
	if(document.getElementById('t_cidade').value=='') {
		alert('O campo cidade é obrigatório!');
		document.getElementById('t_cidade').focus();
		return false;
	}
	if(document.getElementById('t_bairro').value=='') {
		alert('O campo bairro é obrigatório!');
		document.getElementById('t_bairro').focus();
		return false;
	}
	if(document.getElementById('t_estado').value=='') {
		alert('O campo estado é obrigatório!');
		document.getElementById('t_estado').focus();
		return false;
	}
	if(document.getElementById('t_pais').value=='') {
		alert('O campo país é obrigatório!');
		document.getElementById('t_pais').focus();
		return false;
	}						
	if(document.getElementById('t_cep').value=='') {
		alert('O campo CEP é obrigatório!');
		document.getElementById('t_cep').focus();
		return false;
	}
	if(document.getElementById('t_telefone').value=='') {
		alert('O campo telefone é obrigatório!');
		document.getElementById('t_telefone').focus();
		return false;
    }
	return true;
}

/* ******************  NEWSLETTER *********************** */

function validar_assinante() {
	if(document.getElementById("t_nome").value=='') {
		alert('O campo nome é obrigatório!');
		document.getElementById("t_nome").focus();
		return false;
	}
	if(document.getElementById("t_email").value=='') {
		alert('O campo e-mail é obrigatório!');
		document.getElementById("t_email").focus();
		return false;
	}
	if(!checkMail(document.getElementById("t_email").value)) {
		alert('O e-mail é inválido!');
		document.getElementById("t_email").focus();	
		return false;
	}	
	if(document.getElementById("t_empresa").value=='') {
		alert('O campo empresa é obrigatório!');
		document.getElementById("t_empresa").focus();
		return false;
	}		
	if(document.getElementById("d_idioma").value==0) {
		alert('O campo idioma é obrigatório!');
		document.getElementById("d_idioma").focus();
		return false;
	}
	return true;
}


/* ******************  BUSCA *********************** */

function validar_pesquisa(){
    if(document.getElementById('buscar').value=='pesquisar' || document.getElementById('buscar').value=='') {
        alert('Digite o termo a ser pesquisado!');
		document.getElementById('buscar').focus();	
		return false;
    }
    return true;
}