try {
    xmlhttp = new XMLHttpRequest();
} catch(ee) {
    try{
        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
        try{
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        } catch(E) {
            xmlhttp = false;
        }
    }
}

function extraiScript(texto){
    // inicializa o inicio ><
    var ini = 0;
    // loop enquanto achar um script
    while (ini!=-1){
        // procura uma tag de script
        ini = texto.indexOf('<script', ini);
        // se encontrar
        if (ini >=0){
            // define o inicio para depois do fechamento dessa tag
            ini = texto.indexOf('>', ini) + 1;
            // procura o final do script
            var fim = texto.indexOf('</script' + '>', ini);
            // extrai apenas o script
            codigo = texto.substring(ini,fim);
            // executa o script
            //eval(codigo);
            novo = document.createElement("script")
            novo.text = codigo;
            document.body.appendChild(novo);
        }
    }
}

function carrega_params(_idContainer, _endereco, method, params){

    if (_idContainer != '') {
        document.getElementById(_idContainer).innerHTML = "<img src='_images/preload.gif'> Carregando";
    }

    if (method == 'POST') {
        xmlhttp.open(method,_endereco,true);
    } else {
        xmlhttp.open(method,_endereco + '?' + params,true);
    }
    

    if (method == 'POST') {
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", params.length);
        xmlhttp.setRequestHeader("Connection", "close");
    }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            retorno=xmlhttp.responseText;
            if (_idContainer != '') {
                var tag_container = document.getElementById(_idContainer);
                tag_container.innerHTML=retorno;
            }
            extraiScript(retorno);
        }
    }

    if (method == 'POST') {
        xmlhttp.send(params);
    } else {
        xmlhttp.send(null);
    }
}


function carrega(_idContainer, _endereco){

    //document.getElementById(_idContainer).innerHTML = "<img src='_images/preload.gif'> Carregando";
    //document.getElementById("preload").style.display = "";
    showPreload();

    xmlhttp.open('POST',_endereco,true);

    xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState==4){
            retorno=xmlhttp.responseText;
            var tag_container = document.getElementById(_idContainer);
            tag_container.innerHTML=retorno;
            extraiScript(retorno);
            hidePreload();
        }
    }

    xmlhttp.send(null);
}

function carregaConteudo(_idContainer, _endereco){

    document.getElementById(_idContainer).innerHTML = "<img src='_images/preload.gif'> Carregando";
    //document.getElementById("preload").style.display = "";

    //showPreload();

    xmlhttp.open('POST',_endereco,true);

    xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState==4){
            retorno=xmlhttp.responseText;
            var tag_container = document.getElementById(_idContainer);
            tag_container.innerHTML=retorno;
            extraiScript(retorno);
            hidePreload();
        }
    }

    xmlhttp.send(null);
}

function carregaAjax(_endereco, params){
    
    xmlhttp.open('POST', _endereco, true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");

    xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState==4){
            retorno=xmlhttp.responseText;
            //var tag_container = document.getElementById("conteudo");
            //tag_container.innerHTML=retorno;
            extraiScript(retorno);
        }
        
    }
    xmlhttp.send(params);
    
}

function carrega_cidades(_idContainer, _endereco, method, params){

    document.getElementById("lista").innerHTML = '<table width="237" cellpadding="0" cellspacing="0" border="0"><tr height="279"><td width="237" valign="top" align="center" background="_images/img_fixa.jpg"></td></tr></table>';
    document.getElementById(_idContainer).innerHTML = "Carregando";

    if (method == 'POST') {
        xmlhttp.open(method,_endereco,true);
    } else {
        xmlhttp.open(method,_endereco + '?' + params,true);
    }


    if (method == 'POST') {
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", params.length);
        xmlhttp.setRequestHeader("Connection", "close");
    }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            retorno=xmlhttp.responseText;
            if (_idContainer != '') {
                var tag_container = document.getElementById(_idContainer);
                tag_container.innerHTML=retorno;
            }
            extraiScript(retorno);
        }
    }

    if (method == 'POST') {
        xmlhttp.send(params);
    } else {
        xmlhttp.send(null);
    }
}

function showPreload() {
    document.getElementById("preload").style.display = "";
}

function hidePreload() {
    document.getElementById("preload").style.display = "none";
}

function alerta(msg) {
    new WindowAlert(msg);
}

function enviaContato() {
    if (validaFormContato()) {
        carrega_params('', '_mvc/controllers/AtendimentoController.php', 'POST', 'mode=SAVE&nome='+document.form.nome.value+'&email='+document.form.email.value+'&telefone='+document.form.telefone.value+'&assunto='+document.form.assunto.value+'&mensagem='+document.form.msg.value);
    }
}

function validaFormContato() {
    if (document.form.nome.value == "" || document.form.nome.lenght < 3)  {
        new WindowAlert("O campo <STRONG>Nome</STRONG> não por ser vazio e nem ter menos de 3 caracteres!");
        return false;
    } else if (document.form.email.value == "" || document.form.email.lenght < 3)  {
        new WindowAlert("O campo <STRONG>E-mail</STRONG> não por ser vazio!");
        return false;
    } else if (document.form.assunto.value == "" || document.form.assunto.lenght < 3)  {
        new WindowAlert("O campo <STRONG>Assunto</STRONG> não por ser vazio e nem ter menos de 3 caracteres!");
        return false;
    } else if (document.form.msg.value == "" || document.form.msg.lenght < 3)  {
        new WindowAlert("O campo <STRONG>Mensagem</STRONG> não por ser vazio e nem ter menos de 3 caracteres!");
        return false;
    } else {
        return true;
    }
}

function webmail() {
    showWebmail();
}

function posiciona() {
    var de = document.documentElement;
    var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
    var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
    document.getElementById('webmail').style.left = (w/2 + 150)+"px";
}

function MM_jumpMenuGo(objId,targ,restore){ //v9.0
    var selObj = null;
    with (document) {
        if (getElementById) selObj = getElementById(objId);
        if (selObj) eval("window.open('"+selObj.options[selObj.selectedIndex].value+"')");
        if (restore) selObj.selectedIndex=0;

        hideWebmail();

        }
}

function hideWebmail(){
    document.getElementById('webmail').style.display = 'none';
}

function showWebmail(){
    document.getElementById('webmail').style.display = 'block';
    document.getElementById("webmail").style.height = getPageSize()[1]+"px";
    carrega('webmail', '_mvc/views/webmail.php');
}

function getPageSize() {

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;

    if (self.innerHeight) {	// all except Explorer
        if(document.documentElement.clientWidth){
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }

    return [pageWidth,pageHeight];
}