// update ie ---------------------------------------------------------------------------
theObjects = document.getElementsByTagName("object");
for (var i = 0; i < theObjects.length; i++) {
theObjects[i].outerHTML = theObjects[i].outerHTML;
}
// --------------------------------------------------------------------------------------

// GERAR COOKIE -------------------------------------------------------------------------
/*
    Exemplo:
    - Cria o cookie 'CookieTeste' com o valor 'HellowWorld!' que irá expirar quando o browser for fechado.
    GerarCookie('CookieTeste', 'HellowWorld!', 0);
    - Lê o conteúdo armazenado no cookie.
    LerCookie('CookieTeste');
    - Exclúi o cookie.
    ExcluirCookie('CookieTeste');
*/

// Função para criar o cookie.
// Para que o cookie seja destruído quando o brawser for fechado, basta passar 0 no parametro lngDias.
function GerarCookie(strCookie, strValor, lngDias)
{
    var dtmData = new Date();

    if(lngDias)
    {
        dtmData.setTime(dtmData.getTime() + (lngDias * 24 * 60 * 60 * 1000));
        var strExpires = "; expires=" + dtmData.toGMTString();
    }
    else
    {
        var strExpires = "";
    }
    document.cookie = strCookie + "=" + strValor + strExpires + "; path=/";
}

// Função para ler o cookie.
function LerCookie(strCookie)
{
    var strNomeIgual = strCookie + "=";
    var arrCookies = document.cookie.split(';');

    for(var i = 0; i < arrCookies.length; i++)
    {
        var strValorCookie = arrCookies[i];
        while(strValorCookie.charAt(0) == ' ')
        {
            strValorCookie = strValorCookie.substring(1, strValorCookie.length);
        }
        if(strValorCookie.indexOf(strNomeIgual) == 0)
        {
            return strValorCookie.substring(strNomeIgual.length, strValorCookie.length);
        }
    }
    return null;
}

// Função para excluir o cookie desejado.
function ExcluirCookie(strCookie)
{
    GerarCookie(strCookie, '', -1);
} // ------------------------------------------------------------------------------------


function clearBrowserCache() {
/*    header("Pragma: no-cache");
    header("Cache: no-cache");
    header("Cache-Control: no-cache, must-revalidate");
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); */
}


// ajax ---------------------------------------------------------------------------------
function GetXMLHttp() {
    var xmlHttp;
    try {
        xmlHttp = new XMLHttpRequest();
    }
    catch(ee) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e) {
                xmlHttp = false;
            }
        }
    }
    return xmlHttp;
}

var xmlRequest = GetXMLHttp();
// --------------------------------------------------------------------------------------

// carregar paginas em div --------------------------------------------------------------
function abrirPag(valor){
    var url = valor;
    
    GerarCookie('pag', valor, 1);
    
//    clearBrowserCache();
    
    revela_comp();

    if (url == "intro.php") {
       window.location.href = "index.php";
    }

    xmlRequest.open("GET",url,true);
    xmlRequest.setRequestHeader("Content-Type", "text/html;");
    xmlRequest.onreadystatechange = mudancaEstado;
    xmlRequest.send(null);

        if (xmlRequest.readyState == 1) {
            document.getElementById("conteudo").innerHTML = "<center><img src='imagens/carregando.gif'></center>";
        }

    return url;
}

function mudancaEstado(){
    if (xmlRequest.readyState == 4){
        document.getElementById("conteudo").innerHTML = xmlRequest.responseText;
    }
}// -------------------------------------------------------------------------------------

// alterar imagem menu ------------------------------------------------------------------
function menu(M){
   document.getElementById("menu_inic").innerHTML = "<a href=\"#\" onclick=\"abrirPag('intro.php'); menu('menu_inic');\"><img border=\"0\" name=\"topo_r2_c2\" src=\"imagens/topo_r2_c2.jpg\" width=\"130\" height=\"30\" border=\"0\" alt=\"\"></a>";
   document.getElementById("menu_hist").innerHTML = "<a href=\"#\" onclick=\"abrirPag('historia.php'); menu('menu_hist');\"><img border=\"0\" name=\"topo_r2_c4\" src=\"imagens/topo_r2_c4.jpg\" width=\"130\" height=\"30\" border=\"0\" alt=\"\"></a>";
   document.getElementById("menu_prod").innerHTML = "<img border=\"0\" name=\"topo_r2_c7\" src=\"imagens/topo_r2_c7.jpg\" width=\"130\" height=\"30\" border=\"0\" alt=\"\">";
   document.getElementById("menu_curs").innerHTML = "<a href=\"#\" onclick=\"abrirPag('cursos.php'); menu('menu_curs');\"><img border=\"0\" name=\"topo_r2_c9\" src=\"imagens/topo_r2_c9.jpg\" width=\"130\" height=\"30\" border=\"0\" alt=\"\"></a>";
   document.getElementById("menu_const").innerHTML = "<img border=\"0\" name=\"topo_r2_c12\" src=\"imagens/topo_r2_c12.jpg\" width=\"130\" height=\"30\" border=\"0\" alt=\"\">";
   document.getElementById("menu_lab").innerHTML = "<img border=\"0\" name=\"topo_r2_c14\" src=\"imagens/topo_r2_c14.jpg\" width=\"130\" height=\"30\" border=\"0\" alt=\"\">";
   document.getElementById("menu_cont").innerHTML = "<a href=\"#\" onclick=\"abrirPag('falecon.php'); menu('menu_cont');\"><img border=\"0\" name=\"topo_r2_c17\" src=\"imagens/topo_r2_c17.jpg\" width=\"130\" height=\"30\" border=\"0\" alt=\"\"></a>";
   switch (M) {
      case "menu_inic": { document.getElementById(M).innerHTML = "<img src='imagens/2topo_r2_c2.jpg'>"; break;}
      case "menu_hist": { document.getElementById(M).innerHTML = "<img src='imagens/2topo_r2_c4.jpg'>"; break;}
      case "menu_prod": { document.getElementById(M).innerHTML = "<img src='imagens/2topo_r2_c7.jpg'>"; break;}
      case "menu_curs": { document.getElementById(M).innerHTML = "<img src='imagens/2topo_r2_c9.jpg'>"; break;}
      case "menu_const": { document.getElementById(M).innerHTML = "<img src='imagens/2topo_r2_c12.jpg'>"; break;}
      case "menu_lab": { document.getElementById(M).innerHTML = "<img src='imagens/2topo_r2_c14.jpg'>"; break;}
      case "menu_cont": { document.getElementById(M).innerHTML = "<img src='imagens/2topo_r2_c17.jpg'>"; break;} }
}
// ---------------------------------------------------------------------------------------

// MENUS - aparece e some ----------------------------------------------------------------
function apar(M){
   document.getElementById(M).style.visibility = "visible"; }
   
function some(M){
   document.getElementById(M).style.visibility = "hidden"; }
// ---------------------------------------------------------------------------------------

// AJUSTE de MENUS SUPENSOS --------------------------------------------------------------
function ajust_top(T,D) {
   parent.document.getElementById(D).style.top = (document.getElementById(T).scrollHeight * -1) -1;
} // -------------------------------------------------------------------------------------

// constr.php - MOSTRAR FOTO MAIOR -------------------------------------------------------
function amplia(END,NOME){
   document.getElementById('imag').innerHTML = "<a href=\"#\" onClick=zoomfoto('../"+END+"','"+NOME+"') title='Zoom'><img src='../imagemdim4.php?imagem="+END+"' border='0'></a>"; }
// ---------------------------------------------------------------------------------------

// contato.php ----------------------------------------------------------------------------
function valida_contato() {
    var nome = document.contato.nome;
    var email = document.contato.email;
    var tel = document.contato.tel;
    var assunto = document.contato.assunto;
    var descr = document.contato.descr;
	if (nome.value == "") {
		alert("Seu nome é necessário...");
		nome.focus();
		return false; }

	if (email.value == "") {
		alert("Informe seu e-mail.");
		email.focus();
		return false; }
    else {
		prim = email.value.indexOf("@")
		if(prim < 2) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("@",prim + 1) != -1) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf(".") < 1) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf(" ") != -1) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("zipmeil.com") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("gmail.com.br") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("hotmail.com.br") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("hotmial.com") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("hotmal.com") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("htmail.com") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("hotmeil.com") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf(".@") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("@.") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf(".com.br.") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("/") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("[") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("]") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("(") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf(")") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("..") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; } }

	if (tel.value == "") {
		alert("Digite seu telefone...");
		tel.focus();
		return false; }

	if (assunto.value == "- - - - - - - - - - - - - - -") {
		alert("Escolha um assunto...");
		assunto.focus();
		return false; }

	if (descr.value == "") {
		alert("Preencha a descrição de seu contato...");
		descr.focus();
		return false; }

    return true; }

function gmap() {
     window.open("falecon.php?gmap=TRUE","gmap","alwaysRaised=no, status=no, scrollbars=no, width=660px, height=650px, top=30, left=20");
}

// --------------------------------------------------------------------------------------

// carrinho de compras ----------------------------------------------------------------------------
function pegacompra() {
   var compras = document.contato.compras;
   compras.value = document.getElementById("compra_finalizada").innerHTML;
}

function valida2_contato() {

    pegacompra();

    var nome = document.contato.nome;
    var sobnome = document.contato.sobnome;
    var email = document.contato.email;
    var confemail = document.contato.confemail;
    var tel = document.contato.tel;
    var end = document.contato.end;
    var num = document.contato.num;
    var bairro = document.contato.bairro;
    var cid = document.contato.cid;
    var uf = document.contato.uf;
    var cep = document.contato.cep;

	if (nome.value == "") {
		alert("Seu nome é necessário...");
		nome.focus();
		return false; }
		
	if (sobnome.value == "") {
		alert("Seu sobrenome é necessário...");
		sobnome.focus();
		return false; }

	if (email.value == "") {
		alert("Informe seu e-mail.");
		email.focus();
		return false; }
    else {
		prim = email.value.indexOf("@")
		if(prim < 2) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("@",prim + 1) != -1) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf(".") < 1) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf(" ") != -1) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("zipmeil.com") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("gmail.com.br") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("hotmail.com.br") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("hotmial.com") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("hotmal.com") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("htmail.com") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("hotmeil.com") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf(".@") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("@.") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf(".com.br.") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("/") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("[") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("]") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("(") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf(")") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; }
		if(email.value.indexOf("..") > 0) {
			alert("O e-mail informado parece não estar correto.");
			email.focus();
			return false; } }
			
 	if (confemail.value != email.value) {
		alert("Confirmação de e-mail inválida.");
		confemail.focus();
		return false; }

	if (tel.value == "") {
		alert("Digite seu telefone...");
		tel.focus();
		return false; }

    if (end.value == "") {
		alert("Digite seu endereço...");
		end.focus();
		return false; }
		
    if (num.value == "") {
		alert("Digite o número da sua casa...");
		num.focus();
		return false; }

    if (bairro.value == "") {
		alert("Digite seu bairro...");
		bairro.focus();
		return false; }

    if (cid.value == "") {
		alert("Digite sua cidade...");
		cid.focus();
		return false; }

    if (uf.value == "") {
		alert("Escolha seu estado...");
		uf.focus();
		return false; }

    if (cep.value == "") {
		alert("Importante! Preencha seu CEP para o cálculo do frete...");
		cep.focus();
		return false; }

    newwindow = window.open('', 'compra', 'height=220,width=500,scrollbars=no,top=210,left=180');
    newwindow.focus();
    document.contato.target = 'compra';
    ExcluirCookie('comprando');
    window.location = "./";
    return true;
}
// --------------------------------------------------------------------------------------

// TRAVAR TECLA F5 ----------------------------------------------------------------------
function Verificar(e){
    var e=(e)?e:window.event; //IE Problemas!
    var tecla = (e.keyCode)?e.keyCode:e.which; //Problemas!
    if (tecla == 116){
        try{e.preventDefault();}
        catch(ex){//IE Problemas!
            e.returnValue=false;
            e.keyCode=0;
        }
        var refr = LerCookie('pag');
        abrirPag(refr);
        return false;
    }
}
document.onkeydown = Verificar;
// ---------------------------------------------------------------------------------------

// zoom fotos ----------------------------------------------------------------------------
function zoomfoto(URL,NAMME) {
     void window.open("popup.php?img="+URL,NAMME,"alwaysRaised=yes, status=yes, scrollbars=no, width=300, height=300, top=20, left=20");
}
// ---------------------------------------------------------------------------------------


// carrinho de compras -------------------------------------------------------------------
// -- registra a compra
function cook_comp(id_ml) {
    var atual = LerCookie('comprando');

    if (atual != null) {
    var quebra = atual.split("/");
         var i; for (i=0; i<(quebra.length); i++) {
             var quebra2 = quebra[i].split("-");
             if (quebra2[0] == id_ml) {
                var algum = "ok";
                var quant = parseInt(quebra2[1]);
                quebra[i] = quebra2[0]+"-"+(quant+1); }
         }

         var j; for (j=0; j<(quebra.length); j++) {
             var monta;
             if ((monta != null)&&(monta!="undefined")) { monta = monta+"/"+quebra[j]; } else { monta = quebra[j]; }
         }
    }

    if (algum != "ok") {
       var novo;
       novo = id_ml+"-1";
       var monta;
       if ((monta != null)&&(monta!="undefined")) {monta = monta+"/"+novo; } else { monta = novo; }
    }

    GerarCookie('comprando', monta, 1);
}

// -- altera a qtde.
function altera_comp(qtde,id_ml) {
    var atual = LerCookie('comprando');

    if (qtde > 0 ) {
    var quebra = atual.split("/");
         var i; for (i=0; i<(quebra.length); i++) {
             var quebra2 = quebra[i].split("-");
             if (quebra2[0] == id_ml) {
                var quant = parseInt(quebra2[1]);
                quebra[i] = quebra2[0]+"-"+qtde;
             }
         }

         var j; for (j=0; j<(quebra.length); j++) {
             var monta;
             if ((monta != null)&&(monta!="undefined")) { monta = monta+"/"+quebra[j]; } else { monta = quebra[j]; }
         }

    GerarCookie('comprando', monta, 1);
    }
    
    var refr = LerCookie('pag');
//    alert('Alterado...');
    abrirPag(refr);
}

// -- remove produto.
function remove_comp(id_ml) {
    var atual = LerCookie('comprando');

    var quebra = atual.split("/");
         var i; for (i=0; i<(quebra.length); i++) {
             var quebra2 = quebra[i].split("-");
             if (quebra2[0] != id_ml) {
                var monta;
                if ((monta != null)&&(monta!="undefined")) { monta = monta+"/"+quebra[i]; } else { monta = quebra[i]; }
             }
         }

    GerarCookie('comprando', monta, 1);
    var refr = LerCookie('pag');
    abrirPag(refr);
}

// -- mostra quadro do carrinho
function revela_comp() {
    if ( (LerCookie('comprando')!=null) && (LerCookie('comprando')!="undefined") ) {
           var atual = LerCookie('comprando');

           var quebra = atual.split("/");
           var conta = 0;
           var i; for (i=0; i<(quebra.length); i++) {
                var quebra2 = quebra[i].split("-");
                var quant = parseInt(quebra2[1]);
                conta = conta + quant;
           }
           if (conta > 1) {qnt = " itens"; } else {qnt = " item"; }
           document.getElementById("div_compra").innerHTML = "<br><br><br><center><a href=\"#\" class=\"prod\" onclick='abrirPag(\"prods.php?id_ml=x&comp=TRUE&tp=1\"); menu(\"menu_prod\");'><b>" + conta + qnt + "</a>";
       apar("div_compra")
       document.getElementById("div_compra").style.height = '100px';
        }
    else {
         document.getElementById("div_compra").style.height = '0px';
         some("div_compra"); }
}
