// JavaScript Document

/* FUNCOES */
function redirect(pag){ location.href=pag; }
function mostraMsg(msg){ window.attachEvent("onload", 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/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 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();
}

/* BUSCA */
function getBusca(){
	if (document.getElementById('busca').value==""){
		alert("Informe a palavra-chave!");
		document.getElementById('busca').focus();
		return false;
	}
}
/**/

/* NEWSLETTER */
function addNews(){
	var poststr = "nome=" + encodeURI ( document.getElementById("nome").value ) +
	"&email=" + encodeURI ( document.getElementById("email").value );
	sendRequest('post.php?tipo=newsletter&'+poststr,'formNews');
}
function getNews(){
	if (document.getElementById('nome').value==""){
		alert("Informe seu nome!");
		document.getElementById('nome').focus();
		return false;
	} else{
		if (document.getElementById('nome').value.length<8){
			alert("Preencha o nome completo!");
			document.getElementById('nome').focus();
			return false;
		}
	}
	if (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;
		}
	}
	
	return true;
}
/**/

/* CONTATO */
function addContato(){
	var poststr = "nome=" + encodeURI ( document.getElementById("nome").value ) +
				"&telefone=" + encodeURI ( document.getElementById("telefone").value ) +
				"&email=" + encodeURI ( document.getElementById("email").value ) +
				"&mensagem=" + encodeURI ( document.getElementById("mensagem").value );
	//alert (poststr);
	sendRequest('post.php?tipo=contato&'+poststr , 'formulario');
}

function addContatoStevia(){
	var poststr = "nome=" + encodeURI ( document.getElementById("nome").value ) +
				"&telefone=" + encodeURI ( document.getElementById("telefone").value ) +
				"&email=" + encodeURI ( document.getElementById("email").value ) +
				"&mensagem=" + encodeURI ( document.getElementById("mensagem").value );
	//alert (poststr);
	sendRequest('../post.php?tipo=contato&'+poststr , 'formulario');
}
/**/

/* ENVIE PARA UM AMIGO */
function addEnvieAmigo(){
	var poststr = "nomeAmigo=" + encodeURI ( document.getElementById("nomeAmigo").value ) +
				"&emailAmigo=" + encodeURI ( document.getElementById("emailAmigo").value ) +
				"&nome=" + encodeURI ( document.getElementById("nome").value ) +
				"&email=" + encodeURI ( document.getElementById("email").value ) +
				"&url=" + encodeURI ( document.getElementById("url").value ) +
				"&pg=" + encodeURI ( document.getElementById("pg").value ) +
				"&id=" + encodeURI ( document.getElementById("id").value );
	//alert (poststr);
	sendRequest('post.php?tipo=envieAmigo&'+poststr,'envieAmigo');
}

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

/* FORM REPRESENTAR */
	function addRepresentar(){
	var poststr = "nome=" + encodeURI ( document.getElementById("nome").value ) +
		"&razao=" + encodeURI ( document.getElementById("razao").value ) +
		"&fantasia=" + encodeURI ( document.getElementById("fantasia").value ) +
		"&endereco=" + encodeURI ( document.getElementById("endereco").value ) +
		"&bairro=" + encodeURI ( document.getElementById("bairro").value ) +
		"&cidade=" + encodeURI ( document.getElementById("cidade").value ) +
		"&estado=" + encodeURI ( document.getElementById("estado").value ) +
		"&cnpj=" + encodeURI ( document.getElementById("cnpj").value ) +
		"&estadual=" + encodeURI ( document.getElementById("estadual").value ) +		
		"&telefone=" + encodeURI ( document.getElementById("fone").value ) +
		"&email=" + encodeURI ( document.getElementById("email").value ) +
		"&vendedores=" + encodeURI ( document.getElementById("vendedores").value ) +
		"&promotores=" + encodeURI ( document.getElementById("promotores").value ) +
		"&area=" + encodeURI ( document.getElementById("area").value ) +
		"&marca=" + encodeURI ( document.getElementById("marca").value ) + '&opcao=';
		if (document.getElementById("opcao").checked==true) poststr+= 'Supermercado-';
		if (document.getElementById("opcao1").checked==true) poststr+= 'LojasDeProdutosNaturais-';
		if (document.getElementById("opcao2").checked==true) poststr+= 'Farmacias-';
		if (document.getElementById("opcao3").checked==true) poststr+= 'Hoteis-Moteis-SPAs-';
		if (document.getElementById("opcao4").checked==true) poststr+= 'Restaurantes-Cafes-';
		if (document.getElementById("opcao5").checked==true) poststr+= 'Associacoes-';
		if (document.getElementById("opcao6").checked==true) poststr+= encodeURI ( document.getElementById("atuacaoOutros").value );
	//alert (poststr);
	sendRequest('post.php?tipo=representar&'+poststr , 'formUm');
}
/**/

/* FORM LOGISTA */
function addLogista(){
	var poststr = "nome=" + encodeURI ( document.getElementById("nome2").value ) +
		"&nomeE=" + encodeURI ( document.getElementById("nomeE").value ) +
		"&endereco=" + encodeURI ( document.getElementById("endereco2").value ) +
		"&bairro=" + encodeURI ( document.getElementById("bairro2").value ) +
		"&cidade=" + encodeURI ( document.getElementById("cidade2").value ) +
		"&estado=" + encodeURI ( document.getElementById("estado2").value ) +
		"&email=" + encodeURI ( document.getElementById("email2").value ) +		
		"&telefone=" + encodeURI ( document.getElementById("fone2").value );
	//alert (poststr);
	sendRequest('post.php?tipo=logista&'+poststr , 'formDois');
}
/**/

/* FORM CONSUMIDOR */
function addConsumidor(){
	var poststr = "nome=" + encodeURI ( document.getElementById("nome3").value ) +
		"&endereco=" + encodeURI ( document.getElementById("endereco3").value ) +
		"&bairro=" + encodeURI ( document.getElementById("bairro3").value ) +
		"&cidade=" + encodeURI ( document.getElementById("cidade3").value ) +
		"&estado=" + encodeURI ( document.getElementById("estado3").value ) +		
		"&telefone=" + encodeURI ( document.getElementById("fone3").value ) +
		"&email=" + encodeURI ( document.getElementById("email3").value ) +
		"&local=" + encodeURI ( document.getElementById("local2").value );
		if (document.getElementById("po50g").checked==true) poststr += '&prod1=S';
		if (document.getElementById("po120g").checked==true) poststr += '&prod2=S';
		if (document.getElementById("po220g").checked==true) poststr += '&prod3=S';
		if (document.getElementById("liq40ml").checked==true) poststr += '&prod4=S';
		if (document.getElementById("liq100ml").checked==true) poststr += '&prod5=S';
		if (document.getElementById("cul400g").checked==true) poststr += '&prod6=S';
		if (document.getElementById("poslac160g").checked==true) poststr += '&prod7=S';
		if (document.getElementById("choc120g").checked==true) poststr += '&prod8=S';
	//alert (poststr);
	sendRequest('post.php?tipo=consumidor&'+poststr , 'formTres');
}
/**/

/* AJAX */
function sendRequest(url,id,postData){
	document.getElementById(id).innerHTML = "<div style='text-align:center;width:auto;'><img src='./css/img/load.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;
}
/**/

/***  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;
}
/**/