/* [inicia variáveis] */
var ajaxRetErro;
var ie = navigator.appName=="Microsoft Internet Explorer";
String.prototype.getID = function(){return document.getElementById(this)};
/* [inicia Ajax] */
function fncAjax(){
	try{
		xmlhttp = new XMLHttpRequest();
	}catch(ee){
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				xmlhttp = false;
			}
		}
	}
}
/* [abre popup centralizado] */
function fncPopUp(pg, w, h){
	var l = (screen.width/2) - (w/2);
	var t = (screen.height/2) - (h/2);
	var param = "width="+w+",height="+h+",left="+l+",top="+t;
	window.open(pg, "_popUp", param);
	return;
}
/*	[valida campos do formulário]
	vars	= array com os nomes dos campos a serem pesquisados
	info	= mostra (ou não) mensagem de 'campos obrigatórios'
			  para campos de e-mail, colocar 'campo:1' para validação do e-mail */
function fncCampos(vars, info){
	for(var f=0; f<vars.length; f++){
		var campo = vars[f].split(":");
		fncRetSpc(document.getElementById(campo[0]));
		if(!document.getElementById(campo[0]).value){
			if(info) window.alert("Todos os campos marcados são de preenchimento obrigatório.");
			document.getElementById(campo[0]).focus();
			return false;
		}else{
			if(campo[1] && !fncValidaMail(document.getElementById(campo[0]))){
				if(info) window.alert("E-mail incorreto.");
				document.getElementById(campo[0]).select();
				return false;
			}
		}
	}
	return true;
}
/* [retira espaços do inicio] */
function fncRetSpc(obj){
	var ret = obj.value;
	/* [início] */
	if(ret.substr(0, 1)==" "){
		while(ret.substr(0, 1)==" ") ret = ret.substr(1);
		obj.value = ret;
	}
	/* [fim] */
	if(ret.substr(-1)==" "){
		while(ret.substr(-1)==" ") ret = ret.substr(0, ret.length-1);
		obj.value = ret;
	}
	return;
}
/* [valida e-mail] */
function fncValidaMail(obj){
	var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/; 
	if (!re_mail.test(obj.value)) return false;
	return true;
}
/* [campos apenas com números -> onKeyPress="return fncNumeros(event);"] */
function fncNumeros(e){
	var whichCode = (window.Event)? e.which : e.keyCode;
	if(whichCode>=48 && whichCode<=57 || whichCode==8 ||whichCode==32) return true;
	return false;
}
/* ---------------------- [Exclusivo da Comunidade] ---------------------- */
/*	[envia dados via 'HttpRequest']
	vars	: array com o nome das váriáveis a serem passadas
	pagina	: nome da página de destino
	info	: tipo de informação a ser mostrada:
			  false  - sem informação
			  alerta - window.alert()
			  obs: para mensagem em div, colocar o nome do div
	redirect: página de redirect após 'sucesso na transação' (erro 0) ou false (sem redirect)
	exec	: ação a ser executada após o processamento (erro 1 ou erro 0) ou false (sem ação) */
function fncEnviaAjax(vars, pagina, info, redirect, exec){
	if(info && info!='alerta' && !document.getElementById(info)){
		window.alert("Objeto '"+info+"' não encontrado.");
		return;
	}
	var pg = "comunidade_"+pagina+".php";
	var data = "?";
	for(var f=0; f<vars.length; f++){
		switch(document.getElementById(vars[f]).type){
			case("checkbox"):
			case("radio"):
				var valor = document.getElementById(vars[f]).checked;
			break;
			default:
				var valor = document.getElementById(vars[f]).value;
			break;
		}
		data += vars[f]+"="+valor;
		if(f<(vars.length-1)) data +="&";
	}
	fncAjax();
	xmlhttp.open("GET", pg+data, true);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4){
			/* msg = erro|texto */
			var msg = xmlhttp.responseText.split("|");
			if(info){
				switch(info){
					case("alerta"):
						window.alert(msg[1]);
					break;
					default:
						with(document.getElementById(info)){
							style.color = msg[0]=="1" ? "#FF9900" : "#0099FF";
							innerHTML = msg[1];
						}
					break;
				}
			}
			ajaxRetErro = msg[0]=="1" ? true : false;
			if(exec) eval(exec);
			if(redirect && msg[0]=="0") top.location.replace("comunidade_index.php?pgID="+redirect);
		}
	}
	xmlhttp.send(null);
}
/* [executa url pelo ID da página] */
function fncURL(url){
	top.location.href = "comunidade_index.php?pgID="+url;
	return;
}
/* [verifica campos preenchidos no formulário de login] */
function fncLoginFormOk(email, senha, btn){
	var btn = document.getElementById(btn);
	var email = document.getElementById(email);
	btn.disabled = true;
	fncLimpaDados(email);
	if(!email.value){
		email.focus();
		btn.disabled = false;
		return false;
	}
	var senha = document.getElementById(senha);
	fncLimpaDados(senha);
	if(!senha.value){
		senha.focus();
		btn.disabled = false;
		return false;
	}
	return true;
}
/* [limpa dados de formulário] */
function fncLimpaDados(Obj){
	fncRetSpc(Obj);
	var chars = /%|#|&|\$|\*|<|>|!|=|'|\"|\.\.|\||--|\\/g;
	var result = Obj.value.replace(chars, "");
	Obj.value = result;
	return;
}
/* [máximo de caracteres em campos textarea] */
function fncTextMax(Obj, tMax){
	document.getElementById('maxChar').innerHTML = Obj.value.length<tMax ? tMax - Obj.value.length : 0;
	if(Obj.value.length>tMax){
		Obj.value = Obj.value.substr(0, tMax);
	}
	return;
}
/* [painel de inserção de mensagens/comentários] */
var varMsgPainel = false;
function fncMsgPainel(){
	with(document.getElementById('divMsgPainel')){
		if(!varMsgPainel){
			/* [abre painel] */
			style.display = "block";
			document.getElementById('txtMsg').focus();
			varMsgPainel = true;
			return;
		}
		/* [fecha painel] */
		style.display = "none";
		varMsgPainel = false;
	}
	return;
}
/* [muda cor de link de botão] */
function fncHover(Obj, Over){
	with(Obj.style){
		backgroundImage = Over ? "url('imagens/menu_fundo_laranja.png')" : "url('imagens/menu_fundo.png')";
	}
	return;
}