﻿var menu_atual = "";
var prog_atual = "";

$(document).ready(function(){
    $('#menu .menu_item').mouseover(function(){
        var menu = $(this).attr('id');
        var sub = $('#'+menu+' div.submenu').attr('id');
        var iframe = $('#'+menu+' iframe').attr('id'); 

        if(menu != menu_atual && menu_atual != ""){
            var subatual = $('#'+menu_atual+' div.submenu').attr('id');
            $('#'+subatual).hide();
            var iframeatual = $('#'+menu_atual+' iframe').attr('id'); 
            $('#'+iframeatual).hide(); 
            //$('#'+menu_atual).removeClass('menu_item_over');
        }
        //$('#'+menu).addClass('menu_item_over');

        if($('#'+sub+' div').size() > 0){
            $('#'+sub).show();
            $('#'+iframe).show();
            $('#'+iframe).width($('#'+sub).width()+22);
            $('#'+iframe).height($('#'+sub).height());
        }    
        
        menu_atual = "menu_"+menu;
    });
    
    $('#menu .menu_item').mouseout(function(){
        var menu = $(this).attr('id');
        var sub = $('#'+menu+' > div').attr('id');
        $('#'+sub).hide();
        var iframe = $('#'+menu+' iframe').attr('id'); 
        $('#'+iframe).hide(); 
        //$('#'+menu).removeClass('menu_item_over');
        menu_atual = "";
    });
    
    //selecionarMenu();
    
    $(document).pngFix();
});

function selecionarMenu(){
    var sub = $('#menu_'+menu+' > div').attr('id');
    $('#menu_'+menu).addClass('menu_item_over');    
    menu_atual = "menu_"+menu;
}

function showTooltip(cat, tipo){
    $('a.tooltip').each(function(){
        var id = $(this).attr("id").replace("prog_","");

        if(tipo == "progexpo"){
            url = "getprogexpositores.aspx";
        } else if(tipo == "progcultural"){
            url = "getprogcultural.aspx";
        }

        $(this).simpletip('Carregando...', { 
            width: '400px',
            showOn: 'click',
            hideOn: { element: 'close', event: 'click' },
            delay: 200,
            position: 'bottom',
            hook: { tooltip: 'topRight', target: 'topLeft' }, 
            parentClass: 'tooltip-light', 
            onShow: function(){ this.load(url+'?pC='+cat+'&pP='+id); }
        });   
    });
    
    $('a.tooltip').bind("click", function(e){
        if(prog_atual != ""){
            $('div[class*=id-'+$('#'+prog_atual).attr("id")+']').hide();
        }
    });
}

/* ***************** 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=626,left=440,top=312');");
}

function AbrirPlantaFeira(){
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('imagem.aspx', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=1,menubar=0,resizable=0,width=665,height=498,top=20,left=20');");
}

function ImprimirProg(){
    expositor = $('#h_expositor').val();
    data = $('#h_data').val();
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('imprimir_prog.aspx?pE=" + expositor + "&pD=" + data + "', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=1,menubar=0,resizable=0,width=700,height=568,left=440,top=312');");
}

function ImprimirProgCult(){
    local = $('#h_local').val();
    data = $('#h_data').val();
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('imprimir_progcult.aspx?pL=" + local + "&pD=" + data + "', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=1,menubar=0,resizable=0,width=700,height=568,left=440,top=312');");
}

function Imprimir(escola){
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('visitacao_imprimir.aspx?pP=" + escola + "', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=700,height=568,left=440,top=312');");
}

function ImprimirExpositores(){
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('imprimir_expositores.aspx', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=700,height=568');");
}

function VerDados(expositor){
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('expositorform.aspx?expositor=" + expositor + "', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=700,height=450');");
}

function erro_galeria() {
	alert("Não existe imagem cadastrada!");
	return false;
}

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 validaCNPJ(CNPJ) {
    if (CNPJ.length < 18){ alert("CNPJ inválido!"); return false; }
    if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
        alert("CNPJ inválido!"); return false;
    }

    if(document.layers && parseInt(navigator.appVersion) == 4){
           x = CNPJ.substring(0,2);
           x += CNPJ. substring (3,6);
           x += CNPJ. substring (7,10);
           x += CNPJ. substring (11,15);
           x += CNPJ. substring (16,18);
           CNPJ = x;
    } else {
           CNPJ = CNPJ. replace (".","");
           CNPJ = CNPJ. replace (".","");
           CNPJ = CNPJ. replace ("-","");
           CNPJ = CNPJ. replace ("/","");
    }
    var nonNumbers = /\D/;
    if (nonNumbers.test(CNPJ)){ alert("CNPJ inválido!"); return false; }
    var a = [];
    var b = new Number;
    var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
    for (i=0; i<12; i++){
           a[i] = CNPJ.charAt(i);
           b += a[i] * c[i+1];
    }
    if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
    b = 0;
    for (y=0; y<13; y++) {
           b += (a[y] * c[y]);
    }
    if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
    if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
           alert("CNPJ inválido!"); return false;
    }
    return true;
}

function configurar_input(oElem,iValue,blankValue) {
	if(oElem.value==iValue || oElem.value=='') {
		oElem.value = (blankValue)?'':iValue;
	}
}

function configurar_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_area_interesse_outro(campo) {
	if(document.getElementById('cb_interesses_'+campo).checked) { 
		document.getElementById('linha_area_interesse_outro').style.display = 'block';
	} else { 
		document.getElementById('linha_area_interesse_outro').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';
	}		
}	


/* ******************* VALIDAÇÕES ****************************** */
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=='E-mail' || document.getElementById('t_email_news').value=='') { 
		alert('O campo e-mail é 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;
}

function validar_busca(){
    if(document.getElementById('t_busca').value=='Palavra-Chave' || document.getElementById('t_busca').value==''){
        alert("Digite o termo a ser pesquisado!");
        document.getElementById('t_busca').focus();
        return false;
    }
    return true;
}

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_bairro').value=='') {
		alert('O campo bairro é obrigatório!');
		document.getElementById('t_bairro').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_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_mensagem').value=='') { 
		alert('O campo mensagem é obrigatório!');
		document.getElementById('t_mensagem').focus();
		return false 
	}		
	return true;
}

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(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)) {
		document.getElementById('t_cpf').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_bairro').value=='') {
		alert('O campo bairro é obrigatório!');
		document.getElementById('t_bairro').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_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($('input:checkbox[id*=cb_interesses]:checked').size() == 0){
	    alert('Selecione pelo menos uma área de interesse!');
		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;
	}
	return true;
}

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("d_idioma").value==0) {
		alert('O campo idioma é obrigatório!');
		document.getElementById("d_idioma").focus();
		return false;
	}
	return true;
}

function validar_visitacao(){
    if(document.getElementById('ddlCategory').value==0) {
		alert('O campo categoria é obrigatório!');
		document.getElementById('ddlCategory').focus();
		return false;
	}
	if(document.getElementById('txtCNPJ').value!='' && !validaCNPJ(document.getElementById('txtCNPJ').value)) {
		document.getElementById('txtCNPJ').focus();
		return false;
	}
	if(document.getElementById('txtCENSO').value=='') {
		alert('O campo CENSO é obrigatório!');
		document.getElementById('txtCENSO').focus();
		return false;
	}
    if(document.getElementById('txtName').value=='') {
		alert('O campo nome da escola é obrigatório!');
		document.getElementById('txtName').focus();
		return false;
	}
	if(document.getElementById('txtPrincipal').value=='') {
		alert('O campo diretor é obrigatório!');
		document.getElementById('txtPrincipal').focus();
		return false;
	}
	if(document.getElementById('txtAddress').value=='') {
        alert('O campo endereço é obrigatório!');
		document.getElementById('txtAddress').focus();
		return false;
	}
	if(document.getElementById('txtAddressNumber').value=='') {
		alert('O campo número é obrigatório!');
		document.getElementById('txtAddressNumber').focus();
		return false;
	}
	if(document.getElementById('txtNeighborhood').value=='') {
		alert('O campo bairro é obrigatório!');
		document.getElementById('txtNeighborhood').focus();
		return false;
	}
	if(document.getElementById('txtCity').value=='') {
		alert('O campo cidade é obrigatório!');
		document.getElementById('txtCity').focus();
		return false;
	}
	if(document.getElementById('txtCEP').value=='') {
		alert('O campo cep é obrigatório!');
		document.getElementById('txtCEP').focus();
		return false;
	}	
	if(document.getElementById('txtPhone').value=='') {
		alert('O campo telefone é obrigatório!');
		document.getElementById('txtPhone').focus();
		return false;
	}
	if(document.getElementById('txtEmail').value=='') {
		alert('O campo e-mail é obrigatório!');
		document.getElementById('txtEmail').focus();
		return false;
	}	
	if(!checkMail(document.getElementById("txtEmail").value)) {
		alert('E-mail inválido!');
		document.getElementById("txtEmail").focus();	
		return false;
	}
	if (document.getElementById ("txtAnswerable1").value == "") {
	    alert ("O campo nome do primeiro responsável é obrigatório!");
	    document .getElementById ("txtAnswerable1").focus();
	    return false;
	}
	if (document.getElementById ("txtAnswerable1Job").value == "") {
	    alert ("O campo cargo do primeiro responsável é obrigatório!");
	    document .getElementById ("txtAnswerable1Job").focus();
	    return false;
	}
	if (document.getElementById ("txtAnswerable1Email").value == "") {
	    alert ("O campo e-mail do primeiro responsável é obrigatório!");
	    document .getElementById ("txtAnswerable1Email").focus();
	    return false;
	}
	if(!checkMail(document.getElementById("txtAnswerable1Email").value)) {
		alert('E-mail do primeiro responsável inválido!');
		document.getElementById("txtAnswerable1Email").focus();	
		return false;
	}
	if (document.getElementById ("txtAnswerable2").value == "") {
	    alert ("O campo nome do segundo responsável é obrigatório!");
	    document .getElementById ("txtAnswerable2").focus();
	    return false;
	}
	if (document.getElementById ("txtAnswerable2Job").value == "") {
	    alert ("O campo cargo do segundo responsável é obrigatório!");
	    document .getElementById ("txtAnswerable2Job").focus();
	    return false;
	}
	if (document.getElementById ("txtAnswerable2Email").value == "") {
	    alert ("O campo e-mail do segundo responsável é obrigatório!");
	    document .getElementById ("txtAnswerable2Email").focus();
	    return false;
	}
	if(!checkMail(document.getElementById("txtAnswerable2Email").value)) {
		alert('E-mail do segundo responsável inválido!');
		document.getElementById("txtAnswerable2Email").focus();	
		return false;
	}
	if(document.getElementById('txtDescricao').value=='') {
		alert('O campo projeto pedagógico é obrigatório!');
		document.getElementById('txtDescricao').focus();
		return false;
	}
	return true;
}

function validar_visitas(){
    if(document.getElementById('h_totalvisitas').value == 0){
        alert("Adicione uma visita para a escola!");
        return false;
    }
    return true;
}

function validar_add(){
    if(document.getElementById('d_data').value == 0){
        alert("Selecione uma data!");
        return false;        
    }
    if(document.getElementById('d_hora').value == 0){
        alert("Selecione uma hora!");
        return false;        
    }
    if(document.getElementById('t_qtdealunos').value == 0 || document.getElementById('t_qtdealunos').value == ""){
        alert("Digite a quantidade de alunos!");
        return false;        
    }
    if(document.getElementById('t_qtdeveiculos').value == 0 || document.getElementById('t_qtdeveiculos').value == ""){
        alert("Digite a quantidade de veículos!");
        return false;        
    }
    return true;
}