﻿$(document).ready(function () {

    $("#localizacao_mudar").click(function () {
        mudar_localizacao("texto");
    });

});

function checkMail(mail) {
    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;
    }
}


function isNum(event) {
    var Key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (Key == 8 || Key == 13 || Key == 127 || (Key >= 48 && Key <= 57)) {
        return true;
    } else {
        return false;
    }
}

function replaceAll(string, token, newtoken) {
    while (string.indexOf(token) != -1) {
        string = string.replace(token, newtoken);
    }
    return string;
}

function configurar_nome(oElem, iValue, blankValue) {
    if (oElem.value == iValue || oElem.value == '') {
        oElem.value = (blankValue) ? '' : iValue;
    }
}


function carregar_estados() {
    id = "tipo=estados&estado=";
    $.ajax({
        type: "GET", url: "carregar_cidades_estados.aspx", data: id, cache: false,
        success: function (msg) {

            //$("#localizacao_texto").hide();
            //$("#localizacao_mudar").hide();

            $("#localizacao_select_txt").text("Estado:");
            $("#t_local_cidade").hide();
            $("#t_local_estado").show();
            $("#t_local_estado").empty();
            $("#t_local_estado").append(msg);
            $("#t_local_estado").unbind().change(function() {
                carregar_cidades();
            });
        }
    });
}

function carregar_cidades() {
    id = "tipo=cidades&estado=" + $("#t_local_estado").val();
    $.ajax({
        type: "GET", url: "carregar_cidades_estados.aspx", data: id, cache: false,
        success: function(msg) {
            $("#localizacao_select_txt").text("Cidade:");
            $("#t_local_estado").hide();
            $("#t_local_cidade").show();
            $("#t_local_cidade").empty();
            $("#t_local_cidade").append(msg);
            $("#t_local_cidade").unbind().change(function() {
                setar_cidade();
            });
        }
    });
}

function setar_cidade() {
    id = "tipo=setar&estado=" + $("#t_local_estado").val() + "&cidade=" + $("#t_local_cidade").val();
    $.ajax({
        type: "GET", url: "carregar_cidades_estados.aspx", data: id, cache: false,
        success: function (msg) {
            window.location = window.location
        }
    });
}

function mudar_localizacao(tipo) {
    if (tipo == "select") {
        carregar_estados();
        $("#localizacao_texto").hide();
        $("#localizacao_select").show();
        $("#localizacao_mudar").text("cancelar");
        $("#localizacao_mudar").unbind().click(function() {
            mudar_localizacao('texto');
      });
  } else if (tipo == "texto") {
        $("#t_local_estado_cidade").empty();
        $("#localizacao_select").hide();
        $("#localizacao_texto").show();
        $("#localizacao_mudar").text("mudar localização");
        $("#localizacao_mudar").unbind().click(function() {
            mudar_localizacao('select');
        });
    }
}


function add_filtro(filtro) {
    window.location = window.location + "?aF=" + filtro;
    return false;
}

function rem_filtro(filtro) {
    window.location = window.location + "?rF=" + filtro;
    return false;
}

var shownWindow;
var contentLoaded = true;

function openWindow(url, filtro, valor) {

    if (!contentLoaded) return;
    if (shownWindow != null) hideWindow();

    var loadingDiv = document.getElementById("pop_FILTRO");
    var screenSize = { screenW: 0, screenH: 0 };
    if (window.innerWidth) {
        screenSize.screenW = window.innerWidth;
        screenSize.screenH = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        screenSize.screenW = document.documentElement.clientWidth;
        screenSize.screenH = document.documentElement.clientHeight;
    } else if (document.body) {
        screenSize.screenW = document.body.clientWidth;
        screenSize.screenH = document.body.clientHeight;
    }

    cleft = (screenSize.screenW / 2) - 405 / 2;
    ctop = $("#filtro-busca").offset().top + 30;

    $.ajax({
        type: "GET", url: url, data: "filtro=" + filtro + "&valor=" + valor, cache: false,
        success: function(msg) {
            $("#pop_FILTRO").html(msg);
            if (loadingDiv.style.setAttribute) { //IE
                loadingDiv.style.setAttribute('left', parseInt(cleft));
                loadingDiv.style.setAttribute('top', parseInt(ctop));
                loadingDiv.style.setAttribute('visibility', 'visible');
            } else {
                loadingDiv.setAttribute('style', "position:absolute;left:" + cleft + "px;top:" + ctop + "px;visibility:visible");
            }
            shownWindow = loadingDiv;
        }
    });
}

function hideWindow() {
    if (shownWindow == null) return;
    if (shownWindow.style.setAttribute) {//IE
        shownWindow.style.setAttribute("visibility", "hidden");
    } else {
        shownWindow.setAttribute("style", "visibility:hidden;position:absolute");
    }
    shownWindow = null;
}

function imprimir_boleto(codigo_prefixo,codigo_anunciante) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('imprimir_boleto.aspx?pPfx=" + codigo_prefixo + "&pAct=" + codigo_anunciante + "', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=660,height=632');");
}

function imprimir_detalhe_costura_externa(indice_pesquisa) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('imprime-detalhe-costura-externa.aspx?pIdx=" + indice_pesquisa + "', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=660,height=632');");
}

function imprimir_detalhe_servicos(indice_pesquisa) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('imprime-detalhe-servicos.aspx?pIdx=" + indice_pesquisa + "', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=660,height=632');");
}

function imprimir_detalhe_maquinario(indice_pesquisa) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('imprime-detalhe-maquinario.aspx?pIdx=" + indice_pesquisa + "', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=660,height=632');");
}

function imprimir_detalhe_empregos(indice_pesquisa) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('imprime-detalhe-empregos.aspx?pIdx=" + indice_pesquisa + "', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=660,height=632');");
}

function imprimir_detalhe_fornecedor(indice_pesquisa) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('imprime-detalhe-fornecedor.aspx?pIdx=" + indice_pesquisa + "', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=660,height=632');");
}

/* -- Copiado do site da SBGG - By Mark - 28/06/10 -- */
function carregar_efeito() {
    $('#geral #conteudo #meio #conteudo-faq div.tblAT').parent().children("a").click(function() {
        /*if (tblAT_selec != "" && $(this).parent().parent().children("div.tblAT").attr("id") != tblAT_selec) {
        $('#' + tblAT_selec).slideUp(500);
        }*/
        $(this).parent().parent().children("div.tblAT").slideToggle(500);
        tblAT_selec = $(this).parent().parent().children("div.tblAT").attr("id");
        return false;
    });

    $('#geral #conteudo #meio #conteudo-faq div.tblAT').parent().parent().children("td.pergunta").children("a").click(function() {
        /*if (tblAT_selec != "" && $(this).parent().parent().children("td.info").children("div.tblAT").attr("id") != tblAT_selec) {
        $('#' + tblAT_selec).slideUp(500);
        }*/
        $(this).parent().parent().children("td.resposta").children("div.tblAT").slideToggle(500);
        tblAT_selec = $(this).parent().parent().children("td.resposta").children("div.tblAT").attr("id");
        return false;
    });
}


function fechaModalPopup() {
    $("#popup").hide();
}

