// JavaScript Document

/* FUNCOES */
function redirect(pag){ location.href=pag; }
function mostraMsg(msg){ window.attachEvent("onload", alert(msg) ); }
function mudaImg(campo,valor){ id = (campo.id) ? campo.id : campo; document.getElementById(id).src = "css/img/"+valor; }

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 sobre(el){
		document.getElementById(el).style.filter='alpha(opacity=50)';
		document.getElementById(el).style.opacity='0.5';
	}
	function fora(el){
		document.getElementById(el).style.filter='alpha(opacity=100)';
		document.getElementById(el).style.opacity='1';
	}

function mudaBg(desc,id){
		var i=0;
		while(e = document.getElementById(desc+i)){
			e.style.backgroundImage = "url(css/img/seta1.gif)";
			i++;
		}
		document.getElementById(desc+id).style.backgroundImage="url(css/img/seta2.gif)";
}

function mudaCor(id){
		var i=1;
		while(e = document.getElementById('lnk'+i)){
			e.style.color = "#FFF";
			i++;
		}
		document.getElementById('lnk'+id).style.color="#000";
}

function seta(id){
	var i=0;
	while(v = document.getElementById('link'+i)){
		v.style.backgroundImage = "";
		i++;	
	}
	document.getElementById('link'+id).style.backgroundImage = "url(css/img/seta.gif)";
}

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 M(campo,valor){ id = (campo.id) ? campo.id : campo; document.getElementById(id).src = "css/img/menu/"+valor; }

function mudaBt(id, img){
	if(document.getElementById(id).src!='css/img/'+img){
		var i=1; var z=0;
		var arr = new Array();
		arr[0] = 'btCompA.png';
		arr[1] = 'btRecA.png';
		arr[2] = 'btSimbA.png';
		arr[3] = 'btSabA.png';
		while(obj=document.getElementById('bt'+i)){
			obj.src='css/img/'+arr[z];
			i++;
			z++;
		}
		document.getElementById(id).src='css/img/'+img
	}
}

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"), "");
}

function abrirGaleria(image){
	window.parent.Shadowbox.open({
		player: 'img',
		content: 'images/'+image
	});
};

function abrirSB(pg, titulo, larg, alt){
	Shadowbox.open({
		player: 'iframe',
		content: pg,
		height: alt,
		width: larg,
		title: titulo
	});
};

function abrirPai(pg, titulo, larg, alt){
	window.parent.Shadowbox.open({
		player: 'iframe',
		content: pg,
		height: alt,
		width: larg,
		title: titulo
	});
};

function openWin(url,valor){ 
	janela = window.open(url,'_blank','location=no,directories=no,status=no,menubar=no,'+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('representantesDetalhe.php?uf='+uf,'container'); }

/* 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 getNews(){ 
	if ( trim(document.getElementById('nomeNews').value)=="" || trim(document.getElementById('nomeNews').value)=="Nome" ){
		alert("Informe seu Nome!");
		document.getElementById('nomeNews').focus();
		return false;
	}
	if ( trim(document.getElementById('emailNews').value)=="" || trim(document.getElementById('emailNews').value)=="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;
		}
	}
}
/**/

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

/*ENQUETE*/
function getEnquete(){
	var i=1;
	var aux = false;
	while(el = document.getElementById('a'+i)){
		if(el.checked==true) aux = true;
		i++;
	}
	if(!aux){
		alert("Selecione uma opcao!");
		return false;
	}
}

/*CADASTRO*/

function getCadastro(){
	if (trim(document.getElementById("nome").value)==""){
		alert("Informe o seu nome!");
		document.getElementById("nome").focus();
		return false;
	}
	if (trim(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;
		}
	}
	if (trim(document.getElementById("nascimento").value)==""){
		alert("Informe a data de nascimento!");
		document.getElementById("nascimento").focus();
		return false;
	}
	if (trim(document.getElementById("cidade").value)==""){
		alert("Informe a cidade!");
		document.getElementById("cidade").focus();
		return false;
	}
	if (trim(document.getElementById("estado").value)==""){
		alert("Informe o estado!");
		document.getElementById("estado").focus();
		return false;
	}
}
/**/

/*FORM (PLANTA)*/

function getForm(){
	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;
		}
	}
	if (document.getElementById("telefone").value==""){
		alert("Informe o seu telefone!");
		document.getElementById("telefone").focus();
		return false;
	}
	if (document.getElementById("comentario").value==""){
		alert("Informe o seu Comentário!");
		document.getElementById("comentario").focus();
		return false;
	}
}
/**/

/* CONTATO */

function getContato(){
	if ( trim(document.getElementById('nome').value)=="" || trim(document.getElementById('nome').value)=="Nome" ){
		alert("Informe seu Nome!");
		document.getElementById('nome').focus();
		return false;
	}
	if ( trim(document.getElementById('email').value)=="" || trim(document.getElementById('email').value)=="E-mail" ){
		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("mensagem").value)=="" || trim(document.getElementById("mensagem").value)=="Mensagem" ){
		alert("Informe sua mensagem!");
		document.getElementById("mensagem").focus();
		return false;
	}
}

/* TREPRESENTANTE */

function getRepresentantes(){
	if ( trim(document.getElementById("nome").value)=="" || trim(document.getElementById("nome").value)=="Nome e Sobrenome"){
		alert("Informe seu nome completo!");
		document.getElementById("nome").focus();
		return false;
	}
	if ( trim(document.getElementById("cidade").value)=="" || trim(document.getElementById("cidade").value)=="Cidade"){
		alert("Preencha a Cidade!");
		document.getElementById("cidade").focus();
		return false;
	}
	if ( trim(document.getElementById("estado").value)=="" || trim(document.getElementById("estado").value)=="Estado"){
		alert("Preencha o Estado!");
		document.getElementById("estado").focus();
		return false;
	}
	if ( trim(document.getElementById('email').value)=="" || trim(document.getElementById('email').value)=="E-mail"){
		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)=="" || trim(document.getElementById("telefone").value)=="Telefone"){
		alert("Informe seu telefone!");
		document.getElementById("telefone").focus();
		return false;
	}
	if ( trim(document.getElementById("celular").value)=="" || trim(document.getElementById("celular").value)=="Celular"){
		alert("Informe seu celular!");
		document.getElementById("celular").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;
}
/**/
