// Função para abrir os links marcados com o atributo rel='link-externo' em nova janela
function externalLinks() {
 if (!document.getElementsByTagName) return;

var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
	  var anchor = anchors[i];
	  if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "link-externo")
		  anchor.target = "_blank";
 }
}
window.onload = externalLinks;


//===========================================================================
// FUNÇÕES GENÉRICAS
//===========================================================================
function validaVazio(obj, msgErro) {
    if (obj.value == "") {
	   alert(msgErro);
	   obj.focus();
	   return true;
    }
    return false;
}
function valida_mail(valor) {
	   prim = valor.indexOf("@")
	   if(prim < 1) return false;
	   if(valor.indexOf("@",prim + 1) != -1) return false;
	   if(valor.indexOf(".") < 1) return false;
	   if(valor.indexOf("zipmeil.com") > 0) return false;
	   if(valor.indexOf("hotmeil.com") > 0) return false;
	   if(valor.indexOf(".@") > 0) return false;
	   if(valor.indexOf("@.") > 0) return false;
	   if(valor.indexOf(".com.br.") > 0) return false;
	   if(valor.indexOf("/") > 0) return false;
	   if(valor.indexOf("[") > 0) return false;
	   if(valor.indexOf("]") > 0) return false;
	   if(valor.indexOf("(") > 0) return false;
	   if(valor.indexOf(")") > 0) return false;
	   if(valor.indexOf("..") > 0) return false;
	   if(valor.indexOf(",") > 0) return false;
	   return true;
}

function corrigeAcentosAjax(texto) {
	if (texto != "") {
		texto = texto.replace(/\+/g, "");
		texto = unescape(texto);
		return texto;		
	}
}

function formata(campo, mask, evt) { 

    if(document.all) { // Internet Explorer 
	  key = evt.keyCode; } 
	  else{ // Nestcape 
           key = evt.which; 
    } 

    teclasPermitidas = Array(0, 8, 13);
    for (i=0; i < teclasPermitidas.length; i++) {
		if (key == teclasPermitidas[i]) return true;
    }

    string = campo.value;  
    i	   = string.length;

    if (i < mask.length) {
    	if (mask.charAt(i) == '§') {

    return (key > 47 && key < 58);

    } else {

    if (mask.charAt(i) == '!') {  return true;  }

    for (c = i; c < mask.length; c++) {

    if (mask.charAt(c) != '§' && mask.charAt(c) != '!')

    campo.value = campo.value + mask.charAt(c);

    else if (mask.charAt(c) == '!'){

    return true;

    } else {

    return (key > 47 && key < 58);

    }

    }

    }

    } else return false;

}

//===========================================================================

//===========================================================================
// FUNÇÕES  DA NEWSLETTER
//===========================================================================
function enviaNews(form) {
	
	if (validaVazio(form.nome, "Campo nome é obrigatório"))
		return false;	
		
	if (!valida_mail(form.email.value)) {
		alert("O e-mail informado não parece ser válido");
		form.email.focus();
		return false;		
	}

	url = "../home/gravaNewsLetter.php"
	tipo = "mostraRetornoNews";	
	tipo_envio = "POST";
	form_envio = form;
	loadXMLDoc(url);
	return false;		
}

function mostraRetornoNews() {
	texto = req.responseText;
	if (req.responseText != "") {
		document.getElementById("newsRetorno").innerHTML = "<br>" + texto;
	}
}
//===========================================================================


//===========================================================================
// FUNÇÕES DOS CASES DA INDEX
//===========================================================================
function mudaPaginaCase(pg) {
	if (pg != "") {
		url  = "../home/mostraCases.php?pg=" + pg;
		tipo = "mostraCases";
		loadXMLDoc(url);
	}
}

function mostraCases() {
	if (req.responseText != "") {
		document.getElementById("case").innerHTML = corrigeAcentosAjax(req.responseText);
	}
}
//===========================================================================


//===========================================================================
// FUNÇÕES DE CONTROLE DO LOGIN
//===========================================================================
function validaLogin (form) {
	
	var erro 	  = false;
	
	var cont_erro = 0;
		
	if (form.login.value == "") {
		cont_erro++;
		msg  = "<li>" + cont_erro + "- Campo login é <strong>obrigatório</strong></li>";
		erro = true;
	}
	
	if (form.senha.value == "") {
		cont_erro++;
		msg  += "<li>" + cont_erro + "- Campo senha é <strong>obrigatório</strong></li>";
		erro = true;
	}
	
	if (erro) {
		document.getElementById("erro").style.display = "block";
		document.getElementById("ulErro").innerHTML   = msg;
	} else {
		url = "login.php";
		tipo = "checaUsuario";	
		tipo_envio = "POST";
		form_envio = form;
		loadXMLDoc(url);		
	}
	
	return false;

}

function checaUsuario() {
	texto = req.responseText;
	if (texto == "ok") {
		location.href = "clientes_exclusiva.php";
	} else {
		document.getElementById("erro").style.display = "block";
		document.getElementById("ulErro").innerHTML   = texto;
	}
}
//===========================================================================

//===========================================================================
// FORMULÁRIO DE CONTATO
//===========================================================================
function validaContato (form) {

	var erro 	  = false;
	var cont_erro = 0;	
	
	if (form.nome.value == "") {
		cont_erro++;
		msg  = "<li>" + cont_erro + "- Campo nome é <strong>obrigatório</strong></li>";
		erro = true;
	}

	if (form.sobrenome.value == "") {
		cont_erro++;
		msg  += "<li>" + cont_erro + "- Campo sobrenome é <strong>obrigatório</strong></li>";
		erro = true;
	}
	
	if (form.email.value == "") {
		cont_erro++;
		msg  += "<li>" + cont_erro + "- Campo email é <strong>obrigatório</strong></li>";
		erro = true;
	}else if (!valida_mail(form.email.value)) {
		  cont_erro++;
		  msg += "<li>" + cont_erro + "- Campo email parece ser <strong>inválido</strong></li>";
		  erro = true;
	}
	
	if (form.mensagem.value == "") {
		cont_erro++;
		msg  += "<li>" + cont_erro + "- Campo mensagem é <strong>obrigatório</strong></li>";
		erro = true;
	}	

	if (erro) {
		document.getElementById("erro").style.display = "block";
		document.getElementById("ulErro").innerHTML   = msg;
	} else {
		/*url = "login.php";
		tipo = "checaUsuario";
		tipo_envio = "POST";
		form_envio = form;
		loadXMLDoc(url);*/
	
		return true;
	}

}
//===========================================================================

function enviaSenha() {
	if (!validaVazio(document.frmLogin.login, "Por favor, informe o login de acesso.")) {
		url = "envia_senha.php?login=" + document.frmLogin.login.value;
		tipo = "retornoEnviaSenha";
		tipo_envio = "GET";
		loadXMLDoc(url);
	}
}

function retornoEnviaSenha() {
	var texto = req.responseText;
	if (texto != "") {
		document.getElementById("senha-retorno").innerHTML = "<p>"+texto+"</p>";
	}
}

function controlaArea(link, area) {
	
	var busca;
	
	if (link.className == "" || link.className == "areaOff") {
		link.className = "areaOn";
		busca = true;
	} else {
		link.className = "areaOff";
		busca = false;
	}
	
	if (busca) {
		document.frmBuscaAvancada.elements[area].value = "sim";
	} else {
		document.frmBuscaAvancada.elements[area].value = "";
	}
	
}
