// -- AJAX -------------------------------------------------------------------------------
function include(url, destino, parametros){
    var req = null;
    if (!parametros) 
        parametros = '';
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        requesita();
    }
    else 
        if (window.ActiveXObject) {
            req = new ActiveXObject("Microsoft.XMLHTTP");
            requesita();
        }
    
    function requesita(){
        req.open("POST", url, true);
        req.onreadystatechange = processa_pagina;
        req.setRequestHeader('Content-Type', "application/x-www-form-urlencoded; charset=iso-8859-1");
        req.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        req.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
        req.setRequestHeader("Pragma", "no-cache");
        req.setRequestHeader("expires", "0");
        req.send(parametros);
    }
    
    function processa_pagina(){
		document.getElementById(destino).innerHTML = "<div style='text-align:center; border: 0px solid; width: 100%;'><br><br><img src='imagens/aguarde.gif'><br>"+lang[idioma]['carregando']+"...</div>";
        if (req.readyState == 4) {
            if (req.status == 200) {
				var temp = req.responseText;
                $("#"+destino).html(temp);
                ScriptOnload(req.responseText);
            }
            else {
                alert(req.statusText + "\n" + url);
            }
        }
    }
}

function ScriptOnload(texto){
	var Emular = document.createElement('script');
	var encontrou = texto.indexOf('onload', "0");
	if (encontrou != -1) {
		dividir = texto.split('onload="');
		fim = dividir[1].split('"');
		Emular.setAttribute('language', 'Javascript');
		Emular.text = fim[0];
		document.body.appendChild(Emular);
	}
}

// EFEITOS NAS PÁGINAS
function abreMenu(id){
	$('#'+id).toggle('fast');
	if ($("#sinal"+id).html() == "+"){
		$("#sinal"+id).html("- &nbsp;");
		$("#salvaCategoriaAberta").load("catalogo/salvaCategoriaAberta.php?id="+id+"&ativa=1");
	}else{
		$("#sinal"+id).html("+");
		$("#salvaCategoriaAberta").load("catalogo/salvaCategoriaAberta.php?id="+id+"&ativa=0");
	};
}

function extrusao(){
	$("#extrusao").toggle();
}

// VALIDAÇÕES DE FORMULÁRIO ///////////////////////////////////////////////////////////

function validaContato(form){
    if (!form.nome.value) {
        alert(lang[idioma]['validaNome']);
        form.nome.focus();
        return false;
    }
    
    if (!validaEmail(form.email, 1)) {
        alert(lang[idioma]['validaEmail']);
        form.email.focus();
        return false;
    }
    
    if (!form.cidade.value) {
        alert(lang[idioma]['validaCidade']);
        form.cidade.focus();
        return false;
    }

    if (form.uf.value == -1) {
        alert(lang[idioma]['validaUf']);
        form.uf.focus();
        return false;
    }

    if (form.departamento.value == -1) {
        alert(lang[idioma]['validaDepartamento']);
        form.departamento.focus();
        return false;
    }
	    
    if (!form.mensagem.value) {
        alert(lang[idioma]['validaMensagem']);
        form.mensagem.focus();
        return false;
    }
}

function gravaContato(enderecoArquivo, form){
	var parametros = "nome="+form.nome.value+"&fone1="+form.ddd1.value+"%20"+form.fone1.value+"&fone2="+form.ddd2.value+"%20"+form.fone2.value+"&email="+form.email.value+"&cidade="+form.cidade.value+"&uf="+form.uf.value+"&departamento="+form.departamento.value+"&mensagem="+form.mensagem.value;
	include(enderecoArquivo,'contato',parametros);
}

// -- FUNÇÕES DE VALIDAÇÃO E MASCARAS ----------------------------------------------------
function mascaraInteiros(campo){
	valor = campo.value.replace(/\D/g,"");
	campo.value = valor;
}

function arredonda(valor, casas){
   var novo = Math.round(valor * Math.pow(10,casas)) / Math.pow(10,casas);
   return(novo);
}

function validaEmail(mail, obrigatorio){
    if ((obrigatorio == 1) || (obrigatorio == 0 && mail.value != "")) {
        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)) {
                return true;
            }
        }
        else 
            if (typeof(mail) == "object") {
                if (er.test(mail.value)) {
                    return true;
                }
            }
            else {
                return false;
            }
    }
    else {
        return true;
    }
}

function moedaToFloat(valor){
		if(valor === ""){
			valor = 0;
		}else{
			valor = valor.replace(".","");
			valor = valor.replace(",",".");
			valor = parseFloat(valor);
		}
	return valor;
}