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){

    scroll(0, 0);

    if (_idContainer != "") {
        document.getElementById(_idContainer).innerHTML = "<table width='100%' height='100%'> <tr height='100%'><td width='100%' align='center' style='font-size: 14px; font-family: Tahoma;'> <img src='imagens/preload.gif'/><br/><i>CARREGANDO</i></td></tr></table>";
    }

    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){
    scroll(0, 0);
    
    document.getElementById(_idContainer).innerHTML = "<table width='100%' height='100%'> <tr height='100%'><td width='100%' align='center' style='font-size: 14px; font-family: Tahoma;'> <img src='imagens/preload.gif'/><br/><i>CARREGANDO</i> </td></tr></table>";

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

    xmlhttp.send(null);
}

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

function outNews(src) {
    if (src.value == '') {
        src.value = 'Assine nosso newsletter digite seu email.';
    }
}

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

function showForm(){
    document.getElementById('webmail').style.display = 'block';
}

function validaFormFoto() {    
    if (document.formFoto.nomeC.value != '' && document.formFoto.raca.value != "")  {
        document.formFoto.submit();
    } else {
        top.alerta('É necessário preencher todos os campos.<BR><BR>');
    }
}

function validaFoto() {
    if (document.formFoto.arq.value != "")  {
        document.getElementById("btnOk").style.display = 'none';
        document.getElementById("preload").style.display = 'block';
        document.formFoto.submit();
    } else {
        top.alerta('É necessário escolher um arquivo.<BR><BR>');
    }
}

function abreModulo(end) {    
    if (end == "views/guiaRaca.php") {
        abriu = true;
        carrega('principal', end);
    } else if (end == "views/caes.php") {
        end = "views/produtos.php";
        carrega_params('principal', end, 'POST', 'c=1&l=0&p=0');
    } else if (end == "views/gatos.php") {
        end = "views/produtos.php";
        carrega_params('principal', end, 'POST', 'c=2&l=0&p=0');
    } else if (end == "views/insumos.php") {
        end = "views/produtos.php";
        carrega_params('principal', end, 'POST', 'c=3&l=0&p=0');
    } else {
        carrega('principal', end);
    }
}

function carregaRaca(container, pagina, id) {
    abriu = true;
    carrega_params(container, pagina,'POST','id='+id);
}

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];
}

var regexNumeros = /^\d+$/;
function checaNumber(src) {
    if ( regexNumeros.test( src.value )) {
        return true;
    } else {
        src.value = '';
        src.focus();
        return false;
    }
}

var regexEmail = /^(([^<>()[\]\\.,;áàãâäéèêëíìîïóòõôöúùûüç:\s@\""]+(\.[^<>()[\]\\.,;áàãâäéèêëíìîïóòõôöúùûüç:\s@\""]+)*)|(\"".+\""))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$$/;
function soEmail( campo ){
    if ( regexEmail.test( campo.value )) {
        return true;
    } else{
        campo.value = '';
        campo.focus();
        return false;
    }
}

