// JavaScript Document

function redirect(pag){ location.replace(pag); }
function mudaImg(campo,valor){ id = (campo.id) ? campo.id : campo; document.getElementById(id).src = valor; }
function mudaStyle(campo, valor){ id = (campo.id) ? campo.id : campo; document.getElementById(id).style.color = valor; }
function openWin(url,valor){ janela = window.open(url,'_blank','location=no,directories=no,status=no'+valor); janela.focus(); }
function showHide(div,num){
	i=0;
	while ( document.getElementById(div+i) ){
		document.getElementById(div+i).style.display = "none";
		i++;
	}
	document.getElementById(div+num).style.display = "block";
}
function closeBanner(){
	document.getElementById('ban').style.display="none";
	document.getElementById('bEntrada').style.display="none";
}


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

function addNews2(){
	var poststr = "email=" + encodeURI ( document.getElementById("email").value );
	sendRequest('post.php?tipo=newsletter&'+poststr,'msg',2);
}

function getNews(){
	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;
		}
	}
	
	document.getElementById('formNews').submit();
}
/**/

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

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

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

