var div = "";
var txt = "";
var url = document.URL;
var actualContHeight = "";
var prom_reg = "";

function getInfo(id) {
    div = "#sec" + id;
    txt = "#txt" + id;

    var height = $(txt).outerHeight();

    if ($(div).outerHeight() == 0) {
        $(div).animate({ "opacity": 1, "height": height + "px" }, "slow");
    }
    else {
        $(div).animate({ "height": 0 }, "slow");
    }
}

function setActive() {
    url = document.URL;
    var page = url.split("/");

    var ul = document.getElementById("ctl00_ContentPlaceHolder1_sMenu");
    var ul2 = ul.getElementsByTagName("ul");

    //alert(page[4]);

    for (var i = 0; i < ul2.length; i++) {
        var a = ul2[i].getElementsByTagName("a");
        for (var j = 0; j < a.length; j++) {
            if (a[j].getAttribute("href") == "/" + page[4] || a[j].getAttribute("href") == url) {
                ul2[i].style.height = "auto";
                $('li#' + ul2[i].parentNode.attributes.getNamedItem("id").value).mouseenter(function() { $(this).unbind('mouseleave mouseenter'); });
                $('li#' + ul2[i].parentNode.attributes.getNamedItem("id").value).mouseover(function() {
                    $(this).unbind('mouseleave mouseenter');
                });
            }
        }
    }
}

function clearCurrentLink() {
    var a = document.getElementsByTagName("A");
    for (var i = 0; i < a.length; i++)
        if (a[i].href == window.location.href.split("#")[0])
        removeNode(a[i]);
}

function removeNode(n) {
    if (n.hasChildNodes())
        for (var i = 0; i < n.childNodes.length; i++)
        n.parentNode.insertBefore(n.childNodes[i].cloneNode(true), n);
    n.parentNode.removeChild(n);
}

function headHover() {
    $(".capa").hover(function() {
        $(this).children("div.sectorInfo").fadeIn("normal");
    }, function() {
        $(this).children("div.sectorInfo").fadeOut("normal");
    });
}

function setMenu() {
    $("#ctl00_ContentPlaceHolder1_sMenu > li").mouseenter(function() {
        var hght = $(this).find("div").outerHeight();
        $(this).children("ul").stop().animate({ "height": hght + "px" }, "slow");
    });

    $("#ctl00_ContentPlaceHolder1_sMenu > li").mouseleave(function() {
        $(this).children("ul").stop().animate({ "height": 0 }, "slow");
    });
}

function init() {
    $('.image a').lightBox();
    setMenu()
    initPagination();
    headHover();
    showTrends();
    clearCurrentLink();
}

function showTrends() {
    $("#ul_paises > li > a").click(function(event) {
        event.preventDefault();
        $("#ul_paises ul").fadeOut("fast");
        $(this).next("ul").fadeIn();
    });
}

function sendSearch() {
    var result = document.getElementById("ctl00_txt1");
    if (result.value == "" || result.value == "Escribe algun parametro") {
        result.value = "Escribe algun parametro";
    }
    else {
        location.href = "Contenido.aspx?noc=51&string=" + result.value;
    }
}

/*// Funciones que validan diferentes formatos //*/

/*// Una cadena normal //*/
function validate(valor, leyenda) {

    if (valor == "") {
        alert(leyenda);
        return false;
    } else {
        if (!valor.match(/[a-zA-Z0-9]/)) {
            alert("Unicamente se acepta caracteres alfanumericos");
            return false;
        } else {
            return true
        }
    }
}

/*// Una cadena tipo email //*/
function validarMail(valor, leyenda) {
    var firstPoint = ".";

    if (valor == "") {
        alert(leyenda);
        return false;
    } else {
        if (!valor.match(/[a-zA-Z0-9]/)) {
            alert("Unicamente se aceptan caracteres alfanumericos");
            return false;
        } else {
            if (valor.indexOf("@") < 0) {
                alert("No esta bien escrito el correo electronico");
            } else {
                return true;
            }
        }
    }
}

/*// Una cadena numerica //*/
function soloNumeros(evento) {
    if (window.event) {
        keynum = evento.keyCode; // IE
    } else {
        keynum = evento.which; // Otros Navegadores
    }
    if (keynum > 47 && keynum < 58 || keynum == 8) {
        return true;
    } else {
        return false;
    }
}

function soloLetras(evento) {
    if (window.event) {
        keynum = evento.keyCode; // IE
    } else {
        keynum = evento.which; // Otros Navegadores
    }
    if (keynum > 96 && keynum < 123 || keynum == 32 || (keynum > 64 && keynum < 91) || keynum == 241 || keynum == 209 || keynum == 8) {
        return true;
    } else {
        return false;
    }
}

function soloAlfanumerico(evento) {
    if (window.event) {
        keynum = evento.keyCode; // IE
    } else {
        keynum = evento.which; // Otros Navegadores
    }
    if (keynum > 96 && keynum < 123 || keynum == 32 || (keynum > 64 && keynum < 91) || (keynum > 47 && keynum < 58) || keynum == 64 || keynum == 46 || keynum == 95 || keynum == 8) {
        return true;
    } else {
        return false;
    }
}

function validarTelefono(valor, leyenda) {
    if (valor == "") {
        alert(leyenda);
        return false;
    } else {
        if (!valor.match(/[0-9]/)) {
            alert("Unicamente se aceptan caracteres numericos");
            return false;
        } else {
            if (valor.length > 10) {
                alert("El maximo de numeros admitidos es de 10");
            } else {
                return true;
            }
        }
    }
}

/*// Un drop box //*/
function validarCombo(value, valor) {
    if (value == valor) {
        alert("Elige un" + valor);
        return false;
    } else {
        return true;
    }
}

function sendContacto(prom) {

    var seccion = document.getElementById("seccion").value;
    var nombre = document.getElementById("nombre").value;
    var email = document.getElementById("mail").value;
    var apellido = document.getElementById("apellidos").value;
    var telefono = document.getElementById("telefono").value;
    var comentarios = document.getElementById("comentarios").value;
    var estado = document.getElementById("estado").value;
    var ciudad = document.getElementById("ciudad").value;
    var medio = "";
    var empresa = "";

    if (seccion == "prensa") {
        prom_reg = 1;
    }
    else if (seccion == "rc_sostenibilidad") {
        prom_reg = 2;
    }
    else if (seccion == "patronicios") {
        prom_reg = 3;
    }
    else if (seccion == "Relaciones_Institucionales") {
        prom_reg = 4;
    }
    else if (seccion == "Capital_Humano") {
        prom_reg = 5;
    }
    else {
        prom_reg = 6;
    }
    if (validate(nombre, "El nombre es obligatorio")) {
        if (validate(apellido, "Los apllidos son obligatorios")) {
            if (validarMail(email, "El email es obligatorio")) {
                /*if(validate(telefono, "El telefono es obligatorio")) 
                {*/
                if (validate(estado, "Selecciona un estado")) {
                    if (validate(ciudad, "La ciudad es obligatoria")) {
                        if (validate(comentarios, "Por favor, escribe algun comentario")) {
                            PageMethods.sendRegistro(nombre, email, apellido, telefono, comentarios, medio, empresa, estado, ciudad, prom_reg, SiRegistraDatos, NoRegistraDatos);
                        }
                    }
                }
                /*}*/
            }
        }
    }
}

function SiRegistraDatos(message) {
    if (prom_reg == 3) {
        alert("Gracias por su propuesta, en caso de estar interesados, nos pondremos en contacto a la brevedad.");
    }
    else {
        alert(message);
    }
    var seccion = document.getElementById("seccion").value = "prensa";
    document.getElementById("nombre").value = "";
    document.getElementById("mail").value = "";
    document.getElementById("apellidos").value = "";
    document.getElementById("telefono").value = "";
    document.getElementById("comentarios").value = "";
    document.getElementById("estado").value = "Selecciona";
    document.getElementById("ciudad").value = "";
    document.getElementById("chkAcepto").checked = false;
    AceptoAviso(false);
}

function NoRegistraDatos(message) {
    alert(message);
}

function onChanged() {
    var seccion = document.getElementById("seccion").value;
    if (seccion == "prensa") {
        $("#selectedPrensa").slideDown("normal");
    }
    else {
        $("#selectedPrensa").slideUp("normal");
    }
}

function pageselectCallback(page_index, jq) {
    var new_content = jQuery('#hiddenresult div.result:eq(' + page_index + ')').clone();
    $('#Searchresult').empty().append(new_content);
    return false;
}

function initPagination() {
    var num_entries = jQuery('#hiddenresult div.result').length;
    $("#Pagination").add("#Pagination2").pagination(num_entries, {
        callback: pageselectCallback,
        items_per_page: 1
    });
}

function AceptoAviso(valor) {
    if (valor == true) {
        document.getElementById("Divenviar").style.display = "block";
    } else {
        document.getElementById("Divenviar").style.display = "none";
    }
}
function VerAviso() {
    var AviUna = document.getElementById("DivAviso").style.display;
    if (AviUna == "none") {
        document.getElementById("DivAviso").style.display = "block";
        document.getElementById("DivForm").style.display = "none";
    } else {
        document.getElementById("DivAviso").style.display = "none";
        document.getElementById("DivForm").style.display = "block";
    }
}

function sendResgistroFranquicias() {
    var nombre = document.getElementById("nombre").value;
    var apellidos = document.getElementById("apellidos").value;
    var email = document.getElementById("email").value;
    var telefono = document.getElementById("telefono").value;
    var movil = document.getElementById("movil").value;
    var direccion = document.getElementById("direccion").value;
    var exp_franquicias = document.getElementById("exp_franquicias").value;
    var Desc_franquicias = document.getElementById("Desc_franquicias").value;
    var capital_disponible = document.getElementById("capital_disponible").value;
    var apertura = document.getElementById("apertura").value;
    var estado_apertura = document.getElementById("estado_apertura").value;
    var local = document.getElementById("local").value;
    var metros_local = document.getElementById("metros_local").value;
    var direccion_local = document.getElementById("direccion_local").value;
    var colonia_local = document.getElementById("colonia_local").value;
    var ciudad_local = document.getElementById("ciudad_local").value;
    var estado_local = document.getElementById("estado_local").value;
    var codigo_postal = document.getElementById("codigo_postal").value;
    var telecomunicaciones = document.getElementById("telecomunicaciones").value;
    var telecom = document.getElementById("telecom").value;
    var descripcion = document.getElementById("descripcion").value;

    if (validate(nombre, "El nombre es obligatorio")) {
        if (validate(apellidos, "Los apellidos son obligatorios")) {
            if (validarMail(email, "El email es obligatorio")) {
                /*if (validate(telefono, "Solo el tel&eacute;fono") || validate(movil, "El m&oacute;vil es obligatorio")) {
                if (validate(movil, "El movil es obligatorio")) {
                if (validate(direccion, "La direccion es obligatoria es obligatorio")) {
                if (validate(exp_franquicias, "La experiencia en franquicias es obligatoria")) {
                if (validate(Desc_franquicias, "La descripción de experiencia en franquicias es obligatoria")) {
                if (validate(capital_disponible, "El capital disponible es obligatorio")) {
                if (validate(apertura, "El tiempo estimado de apertura es obligatorio")) {
                if (validate(estado_apertura, "El estado donde desea aperturar es obligatorio")) {
                if (validate(local, "El local disponible es obligatorio")) {
                if (validate(metros_local, "Los metros del local son obligatorios")) {
                if (validate(direccion_local, "La direccion del local disponible es obligatoria")) {
                if (validate(colonia_local, "La colonia es obligatoria")) {
                if (validate(ciudad_local, "La ciudad es obligatoria")) {
                if (validate(estado_local, "El estado es obligatorio")) {
                if (validate(codigo_postal, "El codigo postal es obligatorio")) {
                if (validate(telecomunicaciones, "La experiencia en sector telecomunicaciones es obligatoria")) {
                if (validate(telecom, "La descripción experiencia en telecom es obligatorio")) {
                if (validate(descripcion, "La descripción disponible es obligatoria")) {*/
                if (telefono != "" || movil != "") {
                    PageMethods.sendResgistroFranquicias(nombre, apellidos, email, telefono, movil, direccion, exp_franquicias, Desc_franquicias, capital_disponible, apertura, estado_apertura, local, metros_local, direccion_local, colonia_local, ciudad_local, estado_local, codigo_postal, telecomunicaciones, telecom, descripcion, SiRegistraDatosFranquicias, NoRegistraDatosFranquicias);
                } else {
                    alert("Se requiere al menos un número telefónico");
                }
                /*                                                            }
                }
                }
                }
                }
                }
                }
                }
                }
                }
                }
                }
                }
                }
                }
                }
                }
                }*/
            }
        }
    }
}

function SiRegistraDatosFranquicias(message) {
    alert(message);
    document.getElementById("nombre").value = "";
    document.getElementById("apellidos").value = "";
    document.getElementById("email").value = "";
    document.getElementById("telefono").value = "";
    document.getElementById("movil").value = "";
    document.getElementById("direccion").value = "";
    document.getElementById("exp_franquicias").value = "Selecciona";
    document.getElementById("Desc_franquicias").value = "";
    document.getElementById("capital_disponible").value = "Selecciona";
    document.getElementById("apertura").value = "Selecciona";
    document.getElementById("estado_apertura").value = "Selecciona";
    document.getElementById("local").value = "Selecciona";
    document.getElementById("metros_local").value = "";
    document.getElementById("direccion_local").value = "";
    document.getElementById("colonia_local").value = "";
    document.getElementById("ciudad_local").value = "";
    document.getElementById("estado_local").value = "Selecciona";
    document.getElementById("codigo_postal").value = "";
    document.getElementById("telecomunicaciones").value = "Selecciona";
    document.getElementById("telecom").value = "";
    document.getElementById("descripcion").value = "";

    document.getElementById("chkAcepto").checked = false;
    AceptoAviso(false);

}

function NoRegistraDatosFranquicias(message) {
    alert(message);
}

$(document).ready(function() {
    init();
});
