// JavaScript Document

//-------------------------------------------------------------------------------------------------------------------------------
// w3cookies
// Forma de uso:
// Criar: w3cookies.create('nome_do_cookie', 'valor', dias_para_expirar);
// Ler: w3cookies.read('nome_do_cookie');
// Deletar: w3cookies.erase('nome_do_cookie');
var w3cookies = {
	date: new Date(),
	create: function(strName, strValue, intDays) {
		if ( intDays ) {
			this.date.setTime(this.date.getTime()+(intDays*24*60*60*1000));
			var expires = "; expires=" + this.date.toGMTString();
		} else {
			var expires = "";
		}
		document.cookie = strName + "=" + strValue + expires + "; path=/";
	},

	read: function(strName) {
		var strNameIgual = strName + "=";
		var arrCookies = document.cookie.split(";");
		for ( var i = 0, strCookie; strCookie = arrCookies[i]; i++ ) {
			while ( strCookie.charAt(0) == " ") {
				strCookie = strCookie.substring(1,strCookie.length);
			}
			if ( strCookie.indexOf(strNameIgual) == 0 ) {
				return strCookie.substring(strNameIgual.length,strCookie.length);
			}
		}
		return null;
	},
	erase: function(strName) {
		this.create(strName,"",-1);
	}
}

//validaData(dt)
function validaData(dt){
	if(dt.length != 0){
		var dia = dt.substr(0, dt.indexOf('/'));
		var mes = dt.substr(dt.indexOf('/')+1, (dt.lastIndexOf('/')-1) - dt.indexOf('/'))
		var ano = dt.substr(dt.lastIndexOf('/')+1);
		
		if(dia == '' || isNaN(dia) || dia > 31 || dia < 1 || mes == '' || isNaN(mes) || mes < 1 || mes > 12 || ano == '' || isNaN(ano) || ano < 1900){
			return false;	
		}
	}
	
	return true;
}

//function validaEmail(campo)
//Função que valida o e-mail informado
function validaEmail(campo) {
    var A 					= campo.indexOf('@');
    var AA 					= campo.indexOf('@', A + 1) != -1;
    var ponto 				= campo.indexOf(".") < 1;
    var espaco 				= campo.indexOf(" ") != -1;
    var pontoAntesDoA 		= campo.indexOf(".@") > 0;
    var pontoDepoisDoA 		= campo.indexOf("@.") > 0;
    var pontoDepoisDoBr 	= campo.indexOf(".com.br.") > 0;
    var barra 				= campo.indexOf("/") > 0;
    var abreColchete 		= campo.indexOf("[") > 0;
    var fechaColchete 		= campo.indexOf("]") > 0;
    var abreParentese 		= campo.indexOf("(") > 0;
    var fechaParentese 		= campo.indexOf(")") > 0;
    var pontoConsecutivo 	= campo.indexOf("..") > 0;
    var aspas				= campo.indexOf('"') > 0;
	
    if(A < 2 || AA || ponto || espaco || pontoAntesDoA || pontoDepoisDoA || pontoDepoisDoBr || barra || abreColchete || fechaColchete || abreParentese || fechaParentese || pontoConsecutivo || aspas) {
        return false;
    }
	
    return true;
}	

//menuLateral(subMenu)
function menuLateral(idMenu, url, nmCookie){
	var menu = w3cookies.read(nmCookie);
	
	if (menu == null) menu = '';
	
	if(idMenu.length > 0){
	
		if (document.getElementById(idMenu).style.display == 'none'){
			document.getElementById(idMenu).style.display = 'block';
			if ((menu != '') && (document.getElementById(menu) != null)){
				document.getElementById(menu).style.display = 'none';
			}
			menu = idMenu;
	
		}else{
			document.getElementById(idMenu).style.display = 'none';	
			
			menu = menu.replace(idMenu, '');
			
		}

	}else{
		menu = '';	
	}
	
	w3cookies.create(nmCookie, menu, 1);
	
	if(url.length > 0) window.location.href = url;
}

//download(pasta, arquivo)
function download(pasta, arquivo){
	document.form.acao.value = 'download';
	document.form.pasta.value = pasta;
	document.form.arquivo.value = arquivo;
	document.form.action = '/funcao/asp/download.asp';
	document.form.submit();
}

//mudaEvento(mes)
var mesAnterior = '';
function mudaEvento(mes){
	if (mesAnterior != ''){
		document.getElementById('mes'+mesAnterior).style.display = 'none';
	}else{
		document.getElementById('mes3').style.display = 'none';
	}
	
	document.getElementById('mes'+mes).style.display = 'block';
	mesAnterior = mes;
}

//validaSolicitacao(frm)
function validaSolicitacao(frm){
	//alert(frm);
	
	var msg = "Preencha corretamente o(s) campo(s):\n";
	var mandaMsg = false;

	if(frm.nome.value == ""){
		msg += " - Nome\n";
		mandaMsg = true;
	}
	if(frm.email.value == "" || !(validaEmail(frm.email.value)) ){
		msg += " - Email\n";
		mandaMsg = true;
	}
	
	if(frm.mensagem.value == ""){
		msg += " - Mensagem\n";
		mandaMsg = true;
	}
	
	if (mandaMsg){
		alert(msg);
		return false;
	}else{
		document.getElementById('form').action = 'procDados.asp';
		document.getElementById('acao').value = 'enviaEmail';
		return true;
	}

}

//selecionaMesEvento(mes)
function selecionaMesEvento(mes){
	document.getElementById('mes').value = mes;
	document.getElementById('form').action = '';
	document.getElementById('form').submit();
}

//selecionaTipoEvento(tipoEs)
function selecionaTipoEvento(tipoEs){
	document.getElementById('tipoEs').value = tipoEs;
	document.getElementById('form').action = '';
	document.getElementById('form').submit();
}

//fBalao(elem, idBalao)
function fBalao(elem, idBalao){
	var divBalao = document.getElementById(idBalao);
	var divBalaoContWidth;
	var divBalaoContHeight;
	
	//Verifica se o display do balão é none
	if(divBalao.style.display == 'none'){
		divBalao.style.display = 'block';

		divBalao.style.top = (getElementPosition(elem.id).top - getElementPosition(idBalao).height) + 'px';
		divBalao.style.left = (getElementPosition(elem.id).left - (getElementPosition(idBalao).width/2) + (getElementPosition(elem.id).width/2)) + 'px';

		innerDivBalao = divBalao.getElementsByTagName('div');
		
		divBalaoContWidth = getElementPosition(innerDivBalao[5].id).width;
		divBalaoContHeight = getElementPosition(innerDivBalao[5].id).height;
		
		innerDivBalao[1].style.width = divBalaoContWidth + 'px';
		innerDivBalao[3].style.width = (divBalaoContWidth - 20) + 'px';
	
		innerDivBalao[7].style.width = divBalaoContWidth + 'px';
		innerDivBalao[9].style.width = (divBalaoContWidth - 20) + 'px';
	
		innerDivBalao[11].style.width = divBalaoContWidth + 'px';
		innerDivBalao[12].style.top = (divBalaoContHeight+18) + 'px';
		innerDivBalao[12].style.left = (getElementPosition(idBalao).width - (getElementPosition(idBalao).width/2)) + 'px';
	}else{
		divBalao.style.display = 'none';
	}
}

//getPosicaoElemento(elementId) - Retorna a posição do elemento
function getElementPosition(elementId){
	var offsetTrail = document.getElementById(elementId);
	var offsetLeft = 0;
	var offsetTop = 0;
	var offsetHeight = 0;
	var offsetWidth = 0;
	
	while(offsetTrail){
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	
	if(navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined"){
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
	
	offsetHeight = document.getElementById(elementId).offsetHeight; 
	offsetWidth = document.getElementById(elementId).offsetWidth; 
	
	return {left:offsetLeft, top:offsetTop, height:offsetHeight, width:offsetWidth};
}