//Ä caracter para forçar detecção UTF8

var isMSIE = (navigator.userAgent.toLowerCase().indexOf('msie') != -1) && (navigator.userAgent.toLowerCase().indexOf('opera') == -1);
var documentCompat = null;
var prevPic = 0;
var nextPic = 0;

if (!document.getElementById) {
	if (document.all) {
		document.getElementById = function(id) {
			return(document.all(id));
		}
		document.getElementsByTagName = function(id) {
			return(document.all.tags(id));
		}
	}

	if (document.layers) {
		document.getElementById = function(id) {
			return(document.layers[id]);
		}
	}
}

function getID(elem) {
	return document.getElementById(elem);
}

function handleKeys(e) {
	kc  = (window.event) ? event.keyCode : e.keyCode;
	esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
	
	if (nextPic > 0) {
		right = (window.event) ? 39 : e.DOM_VK_RIGHT;
		if (kc == right) {
			idfoto = nextPic;
			nextPic = 0;
			abrirCorpo('janela','pg=galeria&modo=verfoto&foto='+idfoto);
		}
	}
	
	if (prevPic > 0) {
		left = (window.event) ? 37 : e.DOM_VK_LEFT;
		if (kc == left) {
			idfoto = prevPic;
			prevPic = 0
			abrirCorpo('janela','pg=galeria&modo=verfoto&foto='+idfoto);
		}
	}
	
	if (kc == esc) fecharJanela();
}

function forceClick(e, botao) {
	kc  = (window.event) ? event.keyCode : e.keyCode;
	enter = (window.event) ? 13 : e.DOM_VK_ENTER;
	
	if (kc == enter) getID(botao).click();
}

function formQuery(campo) {
	var query = '';
	var objs;
	var obj;

	form = getID(campo);

	objs = form.getElementsByTagName('input');
	for (i=0; i<objs.length; i++) {
		obj = objs[i];

		if (obj.name != '') {
			if ((obj.type == "text") || (obj.type == "hidden") || (obj.type == "button") || (obj.type == "submit") || (obj.type == "password")) {
				query += "&" + obj.name + "=" + encodeURIComponent(obj.value);
			}
			else if ((obj.type == "checkbox") || (obj.type == "radio")) {
				if (obj.checked) {
					if (obj.value != 'on')
						query += "&" + obj.name + "=" + encodeURIComponent(obj.value);
					else
						query += "&" + obj.name + "=1";
				}
			}
		}
	}

	objs = form.getElementsByTagName('select');
	for (i=0; i<objs.length; i++) {
		obj = objs[i];

		if (obj.selectedIndex > -1)
			query += "&" + obj.name + "=" + encodeURIComponent(obj.options[obj.selectedIndex].value);
	}

	objs = form.getElementsByTagName('textarea');
	for (i=0; i<objs.length; i++) {
		obj = objs[i];
		query += "&" + obj.name + "=" + encodeURIComponent(obj.value);
	}
	return query;
}

function checkAll(campo) {
	var objs;
	var obj;

	form = getID(campo);

	try {
		if ((form.senha2.value != form.senha.value)) {
			alert("As senhas digitadas não conferem");
			return false;
		}
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		if(form.email.value != '' && !emailPattern.test(form.email.value)){
			alert("Email inválido");
			return false;
		}
	} catch (e) {}

	objs = form.getElementsByTagName('input');
	for (i=0; i<objs.length; i++) {
		obj = objs[i];

		if (obj.value == '' && obj.className != 'notob' && obj.name != 'conteudo' && obj.type != 'hidden') {
			alert("Favor preencher o campo "+obj.name);
			return false;
		}
	}
	return true;
}

function checkSelected(campo) {
	box = getID(campo);
	if(box.options.length > 0)
		return true;
	else
		alert("Favor inserir pelo um destinatário");
}

function showMenu() {
	box_menu = getID('menubar');

	try { box_menu.onselectstart = function() {return false}; } catch(e) {}
	try { box_menu.style.MozUserSelect = "none"; } catch(e) {}
	try { box_menu.onmousedown = function() {return false}; } catch(e) {}

	var lis = box_menu.getElementsByTagName('li');
	i = 0;
	total = lis.length;
	while (i < total) {
		objli = lis[i++];

		objli.onmouseover = function() {
			this.className = 'over';
		}

		objli.onmouseout = function() {
			this.className = '';
		}
	}
}

function hideMenu() {
	var lis = getID('menubar').getElementsByTagName('li');
	i = 0;
	total = lis.length;
	while (i < total) {
		objli = lis[i++];
		if (objli.className == 'over') {
			objli.className = '';
			break;
		}
	}
}

function show(id) {
	getID(id).style.display = 'block';
}

function showInline(id) {
	getID(id).style.display = 'inline';
}

function hide(id) {
	getID(id).style.display = 'none';
}

function addItem(campo, valor, name) {
	if ((valor) && (valor != '') && (name) && (name != '')) {
		box = getID(campo);
		
		for(i=0;i<box.length;i++)
			if(box.options[i].value == valor)
				return;
		
		box.options[box.length] = new Option(name, valor);
	}
}

function addItemFilho(campo, nome, aniversario) {
	if ((nome) && (nome != '') && (aniversario) && (aniversario != '')) {
		box = getID(campo);
		
		for(i=0;i<box.length;i++)
			if(box.options[i].text == nome)
				return;
		
		box.options[box.length] = new Option(nome, nome+":"+aniversario);
	}
}

function delItem(campo) {
	box = getID(campo);
	
	if (box.selectedIndex == -1)
		alert('Selecione um item primeiro');
	else {
		box.options[box.selectedIndex] = null;
		box.selectedIndex = -1;
	}
}

function convertSelect(campoSelect, campoInput) {
	itens = '';

	boxSelect = getID(campoSelect);

	for (i=0; i<boxSelect.length; i++) {
		if (i) itens += '|';
		itens += boxSelect.options[i].value;
	}

	getID(campoInput).value = itens;
}

function convertToSelect(campoInput, campoSelect) {
	itens = getID(campoInput).value.split('|');
	
	for (i=0; i<itens.length; i++)
		addItemFilho(campoSelect, itens[i]);
}

function convertToSelectFilhos(campoInput, campoSelect) {
	itens = getID(campoInput).value.split('|');
	
	for (i=0; i<itens.length; i++){
		valores = itens[i].split(':');
		addItemFilho(campoSelect, valores[0], valores[1]);
	}
}

function changeForm(tipo) {
	switch (tipo) {
		case 0: //Macon
			show('incluirMacon');
			hide('incluirDemolay');
			hide('incluirAcacia');
			hide('incluirAdmin');
			break;
		case 1: //DeMolay
			hide('incluirMacon');
			show('incluirDemolay');
			hide('incluirAcacia');
			hide('incluirAdmin');
			break;
		case 2: //Acacia
			hide('incluirMacon');
			hide('incluirDemolay');
			show('incluirAcacia');
			hide('incluirAdmin');
			break;
		case 3: //Admin
			hide('incluirMacon');
			hide('incluirDemolay');
			hide('incluirAcacia');
			show('incluirAdmin');
	}
}

function atualizar(endereco) {
	if (endereco.indexOf('index') != -1) {
		target = 'corpo';
		
		if (endereco.indexOf('=') != -1)
			str = 'pg=index&tipo='+endereco.substring(endereco.indexOf('=')+1);
		else
			str = 'pg=index';
	}
	else if ((endereco.indexOf('perfil') != -1) || (endereco.indexOf('fale_conosco') != -1) || (endereco.indexOf('endereco') != -1)) {
		target = 'corpo';
		str = 'pg='+endereco;
	}
	else if (endereco.indexOf('idPagina') != -1) {
		target = 'corpo';
		str = 'pg=exibir_pagina&' + endereco;
	}
	else if (endereco.indexOf('galeria') != -1) {
		target = 'corpo';
		
		if (endereco.indexOf('=') != -1)
			str = 'pg=galeria&abrirgaleria='+endereco.substring(endereco.indexOf('=')+1);
		else
			str = 'pg=galeria';
	}
	else if (endereco.indexOf('arquivos') != -1) {
		target = 'corpo';
		
		if (endereco.indexOf('=') != -1)
			str = 'pg=arquivos&modo=listagem&dir='+endereco.substring(endereco.indexOf('=')+1);
		else
			str = 'pg=arquivos';
	}
	else if (endereco.indexOf('evento') != -1) {
		abrirCorpoSinc('corpo','pg=index');
		
		dms = endereco.substring(endereco.indexOf('=')+1).split('-');
		
		target = 'content';
		str = 'pg=agenda&dia='+dms[0]+'&mes='+dms[1]+'&ano='+dms[2];
	}
	else if (endereco.indexOf('noticias') != -1) {
		target = 'corpo';
		str = 'pg=listar_noticia';
	}
	else if (endereco.indexOf('agenda') != -1) {
		target = 'corpo';
		str = 'pg=agenda';
	}
	
	if (str != '') {
		abrirCorpo(target,str);
		window.location.hash = endereco;
	}
}

function validar(campo) {
	if (campo.value == '')
		estado = 'check_falha';
	else
		estado = 'check_sucesso';
	
	campo.parentNode.className = estado;
}

function validarSenha(campo, campo2) {
	if (campo.value == '')
		estado = 'check_falha';
	else if (campo.value != campo2.value)
		estado = 'check_falha';
	else
		estado = 'check_sucesso';
	
	campo.parentNode.className = estado;
	campo2.parentNode.className = estado;
}

function validarEmail(campo){
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   if(campo.value == '' || Pattern.test(campo.value))
		campo.parentNode.className = 'check_sucesso';
	else
		campo.parentNode.className = 'check_falha';
}

function revalidar(campo) {
	formulario = getID(campo);
	inputs = formulario.getElementsByTagName('input');
	
	for (i=0;i<inputs.length;i++) {
		input = inputs[i];
		if ((input.type == 'text') && (input.className != 'notob'))
			validar(input);
	}
}
