// JavaScript Document

/* FUNCOES */
function redirect(pag){ location.href=pag; }
function mostraMsg(msg){ window.onload = function(){ alert(msg); } }
function maximaExtensao (campo, maximo){ if( campo.value.length>maximo ){ campo.value=campo.value.substring(0, maximo-1);} }
function mudaImg(campo,valor){ id = (campo.id) ? campo.id : campo; document.getElementById(id).src = "css/menu/"+valor; }
function expert(id){
	document.getElementById('top'+id).style.background='url(css/img/bgTop2.gif)';
	document.getElementById('middle'+id).style.background='#41c5fa';
	document.getElementById('bottom'+id).style.background='url(css/img/bgBottom2.gif)';
}
function expert2(id){
	document.getElementById('top'+id).style.background='url(css/img/bgTop.gif)';
	document.getElementById('middle'+id).style.background='#8b8b8b';
	document.getElementById('bottom'+id).style.background='url(css/img/bgBottom.gif)';
}
function showHide(id,mostra){
	i=0;
	while ( document.getElementById(id+i) ){ document.getElementById(id+i).style.display = "none"; i++; }
	document.getElementById(id+mostra).style.display = (document.getElementById(id+mostra).style.display=="block") ? "none" : "block";
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

 // +/- FONTES
var tgs = new Array( 'div' );
var szs = new Array( 'xx-small','x-small','small','medium','large','x-large','xx-large' );
var startSz = 2;

function fonte( trgt,inc ) {
	if (!document.getElementById) return
	var d = document,cEl = null,sz = startSz,i,j,cTags;
	sz += inc;
	if ( sz < 1 ) sz = 1;
	if ( sz > 5 ) sz = 5;
	startSz = sz;
	if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];
	
	cEl.style.fontSize = szs[ sz ];
	
	for ( i = 0; i < tgs.length; i++ ) {
	cTags = cEl.getElementsByTagName( tgs[ i ] );
	for ( j = 0; j < cTags.length; j++ ) cTags[ j ].style.fontSize = szs[ sz ];
	}
}

function openWin(url,valor){ 
	janela = window.open(url,'_blank','location=no,directories=no,status=no,menubar=yes,scrollbars=yes'+valor);
	janela.focus();
}
function imprimir(url,valor){ 
	janela = window.open(url,'_blank','location=no,directories=no,status=no,menubar=no,'+valor);
	janela.focus();
	janela.print();
	janela.stop();
}
function sigla(uf){ sendRequest('consultoresDetalhes.php?est='+uf,'divCon'); }

/* AJAX */
function sendRequest(url,id,postData){
	document.getElementById(id).innerHTML = "<div style='text-align:center;width:auto;'><img src='./css/img/loading.gif' alt='carregando...' /></div>";
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (req.overrideMimeType) req.overrideMimeType('text/html');
	if (postData) req.setRequestHeader('Content-type','application/x-www-form-urlencoded');

	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
			alert('HTTP error ' + req.status);
			return;
		}
		document.getElementById(id).innerHTML = req.responseText; 
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try { xmlhttp = XMLHttpFactories[i](); } catch (e) { continue; }
		break;
	}
	return xmlhttp;
}
/**/


/*NEWSLETTER*/
function addNews(){
	var poststr = "email=" + encodeURI ( document.getElementById("emailNews").value );
	sendRequest('post.php?tipo=newsletter&'+poststr,'news');
}


function getBusca(){
	if (trim(document.getElementById('termo').value)==""){
		alert("Informe o termo da busca!");
		document.getElementById('termo').focus();
		return false;
	}
}

function getNews(){
	if (document.getElementById('emailNews').value=="" || document.getElementById('emailNews').value=="seu e-mail"){
		alert("Informe seu e-mail!");
		document.getElementById('emailNews').focus();
		return false;
	} else {
		mail = document.getElementById('emailNews');
		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)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else if(typeof(mail) == "object"){
			if(!er.test(mail.value)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else{
			alert('E-mail inválido!'); 
			mail.focus();
			return false;
		}
	}
}
/**/


/*ENVIE*/

function getEnvie(){
	if (document.getElementById("nomeAmigo").value==""){
		alert("Informe o nome de seu amigo!");
		document.getElementById("nomeAmigo").focus();
		return false;
	}
	if (document.getElementById('emailAmigo').value==""){
		alert("Informe o e-mail de seu amigo!");
		document.getElementById('emailAmigo').focus();
		return false;
	} else {
		mail = document.getElementById('emailAmigo');
		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)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else if(typeof(mail) == "object"){
			if(!er.test(mail.value)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else{
			alert('E-mail inválido!'); 
			mail.focus();
			return false;
		}
	}
	if (document.getElementById("nome").value==""){
		alert("Informe o seu nome!");
		document.getElementById("nome").focus();
		return false;
	}
	if (document.getElementById('email').value==""){
		alert("Informe o seu e-mail!");
		document.getElementById('email').focus();
		return false;
	} else {
		mail = document.getElementById('email');
		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)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else if(typeof(mail) == "object"){
			if(!er.test(mail.value)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else{
			alert('E-mail inválido!'); 
			mail.focus();
			return false;
		}
	}
}
/**/

/* CONTATO */

function getContato(){
	if (document.getElementById("nomec").value==""){
		alert("Informe seu nome!");
		document.getElementById("nomec").focus();
		return false;
	}
	if (document.getElementById('emailc').value==""){
		alert("Informe seu e-mail!");
		document.getElementById('emailc').focus();
		return false;
	} else {
		mail = document.getElementById('emailc');
		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)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else if(typeof(mail) == "object"){
			if(!er.test(mail.value)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else{
			alert('E-mail inválido!'); 
			mail.focus();
			return false;
		}
	}
	if (document.getElementById("telefonec").value==""){
		alert("Informe seu telefone!");
		document.getElementById("telefonec").focus();
		return false;
	}
	if (document.getElementById("mensagemc").value==""){
		alert("Informe sua mensagem!");
		document.getElementById("mensagemc").focus();
		return false;
	} else {
		var txt = document.getElementById("mensagemc").value.length;
		if ( txt < 10 ){
			alert("A mensagem deve conter pelo menos 10 caracteres!");
			document.getElementById("mensagemc").focus();
			return false;
		}
	}	
	return true;
}



/* AGENDA */

function addAgenda(){
	var poststr = "nome=" + encodeURI ( document.getElementById("nome").value ) +
				"&email=" + encodeURI ( document.getElementById("email").value ) +
				"&telefone=" + encodeURI ( document.getElementById("telefone").value ) +
				"&mensagem=" + encodeURI ( document.getElementById("mensagem").value ) +
				"&evento=" + encodeURI ( document.getElementById("evento").value ) +
				"&buffet=" + encodeURI ( document.getElementById("buffet").value ) +
				"&opcao1=" + encodeURI ( document.getElementById("opcao1").value ) +
				"&opcao2=" + encodeURI ( document.getElementById("opcao2").value ) +
				"&opcao3=" + encodeURI ( document.getElementById("opcao3").value ) +
				"&convidados=" + encodeURI ( document.getElementById("convidados").value ) +
				"&local=" + encodeURI ( document.getElementById("local").value );
	sendRequest('post.php?tipo=agenda&'+poststr , 'formItem');
}


/*AGENDA*/
function getCadastro(){
	if (trim(document.getElementById("nome").value)==""){
		alert("Informe seu nome!");
		document.getElementById("nome").focus();
		return false;
	}
	if (trim(document.getElementById("endereco").value)==""){
		alert("Informe seu endereco!");
		document.getElementById("endereco").focus();
		return false;
	}
	if (trim(document.getElementById("bairro").value)==""){
		alert("Informe seu bairro!");
		document.getElementById("bairro").focus();
		return false;
	}
	if (trim(document.getElementById("cep").value)==""){
		alert("Informe seu cep!");
		document.getElementById("cep").focus();
		return false;
	}
	if (trim(document.getElementById("cidade").value)==""){
		alert("Informe sua cidade!");
		document.getElementById("cidade").focus();
		return false;
	}
	if (trim(document.getElementById("estado").value)==""){
		alert("Informe seu estado!");
		document.getElementById("estado").focus();
		return false;
	}
	if (trim(document.getElementById('email').value)==""){
		alert("Informe seu e-mail!");
		document.getElementById('email').focus();
		return false;
	} else {
		mail = document.getElementById('email');
		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)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else if(typeof(mail) == "object"){
			if(!er.test(mail.value)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else{
			alert('E-mail inválido!'); 
			mail.focus();
			return false;
		}
	}
	if (trim(document.getElementById("telefone").value)==""){
		alert("Informe o seu telefone!");
		document.getElementById("telefone").focus();
		return false;
	}
	if (trim(document.getElementById("rg").value)==""){
		alert("Informe seu rg!");
		document.getElementById("rg").focus();
		return false;
	}
	if (trim(document.getElementById("cpf").value)==""){
		alert("Informe seu cpf!");
		document.getElementById("cpf").focus();
		return false;
	}
	if (trim(document.getElementById("nascimento").value)==""){
		alert("Informe sua data de nascimento!");
		document.getElementById("nascimento").focus();
		return false;
	}
	if (trim(document.getElementById("titulo").value)==""){
		alert("Informe o titulo do evento!");
		document.getElementById("titulo").focus();
		return false;
	}
	if (trim(document.getElementById("data1").value)==""){
		alert("Informe a data de inicio do evento!");
		document.getElementById("data1").focus();
		return false;
	}
	if (trim(document.getElementById("data2").value)==""){
		alert("Informe a data de termino do evento!");
		document.getElementById("data2").focus();
		return false;
	}
	if (trim(document.getElementById("descricao").value)==""){
		alert("Informe a descricao do evento!");
		document.getElementById("descricao").focus();
		return false;
	}
	if (trim(document.getElementById("cidade2").value)==""){
		alert("Informe a cidade do evento!");
		document.getElementById("cidade2").focus();
		return false;
	}
	if (trim(document.getElementById("email2").value)==""){
		alert("Informe o e-mail para contato do evento!");
		document.getElementById("email2").focus();
		return false;
	}else {
		mail = document.getElementById('email2');
		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)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else if(typeof(mail) == "object"){
			if(!er.test(mail.value)){ 
				alert('E-mail inválido!'); 
				mail.focus();
				return false; 
			}
		}else{
			alert('E-mail inválido!'); 
			mail.focus();
			return false;
		}
	}
	if (document.getElementById("aceite").checked==false){
		alert("Você precisa ler e concordar com o termo de adesão!");
		document.getElementById("aceite").focus();
		return false;
	}
	return true;
}

/**/


/***  onblur="tiramascara(this,'_999.999.999-99');" onkeyup="mascara(this,event,'_999.999.999-99');"   ***/
function mascara(campo,e,mask){  //*
	if (e.keyCode>30 || e.keyCode==8){
		dado1=campo.value;
		completar=' ';
		dado2='';
		if (mask.charAt(0)=='_') { completar='_'; mask=mask.substr(1); }
		while (dado1.search(/[^0-9]/)>=0) {dado1=dado1.replace(/[^0-9]/,'');}
		while (completar==' ' && dado1.charAt(0)=='0') {dado1=dado1.substr(1);}

		for (i1=0,i2=0;i1<mask.length;i1++) {
			maskch=mask.charAt(mask.length-i1-1);
			if (maskch=='9' || maskch=='0') {
				if (i2<dado1.length) { 
					dado2=dado1.charAt(dado1.length-(i2++)-1)+dado2;
				}	else {
					if (maskch=='9') { dado2=completar+dado2; } else { dado2='0'+dado2; }
				}
			} else {
				if (i2<dado1.length || mask.substr(0,mask.length-i1).search(/[0]/)>=0 || completar!=' ') {
					dado2=maskch+dado2;
				} else {
					dado2=completar+dado2;
				}
			}
		}
    campo.value=dado2;
  }

}

function tiramascara(campo,mask){  //*

	dado1=campo.value; completar=' '; dado2='';
	if (mask.charAt(0)=='_') {completar='_';mask=mask.substr(1);}
	while (dado1.search(/[^0-9]/)>=0) {dado1=dado1.replace(/[^0-9]/,'');}
	while (completar==' ' && dado1.charAt(0)=='0') {dado1=dado1.substr(1);}

	for (i1=0,i2=0;i1<mask.length;i1++) {
		maskch=mask.charAt(mask.length-i1-1);
		if (maskch=='9' || maskch=='0') {
			if (i2<dado1.length) {
				dado2=dado1.charAt(dado1.length-(i2++)-1)+dado2;
			} else {
				if (maskch=='9') {dado2=dado2;} else {dado2='0'+dado2;}
			}
		} else {
			if ( i2<dado1.length || mask.substr(0,mask.length-i1).search(/[0]/)>=0 ){
				dado2=maskch+dado2;
			} else {
				dado2=dado2;
			}
		}
	}
	campo.value="";
	campo.value=dado2;
}
/**/
