/*
 * core.js
 * Oscar Medina
 */


/*
 *          CORE
 */


function nuevo_xmlhttp(){
    var xmlhttp=false;
    
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(E) {
            xmlhttp = false;
        }
    }
    //
    if(!xmlhttp && typeof XMLHttpRequest!='undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}


function carga_bloque_dinamico(xmlhttp, id_frame, url, accion) {
    var contenido;

    contenido = document.getElementById(id_frame);
    //
    xmlhttp.open("GET", url, true);
    xmlhttp.onreadystatechange=function() {
        if(xmlhttp.readyState==1) {
            contenido.style.background = "url('imagenes/loading.gif') no-repeat";
            contenido.style.backgroundPosition = "center";
        } else if(xmlhttp.readyState==4) {
            if(xmlhttp.status==200) {
                //mostramos los datos dentro del div
                contenido.style.background = "none";
                contenido.innerHTML = xmlhttp.responseText;
            } else if(xmlhttp.status==404) {
                contenido.style.background = "none";
                contenido.innerHTML = "<div align='center'>La pagina no existe</div>";
            } else {
                //mostramos el posible error
                contenido.style.background = "none";
                contenido.innerHTML = "Error: "+xmlhttp.status;
            }
        }
    };
    xmlhttp.send(null);
    if (accion != "") {
        setTimeout(accion, 3000);
    }
}

function envia_formulario(id_formulario) {
    document.getElementById(id_formulario).submit();
}

function envia_form_xmlhttp(id_formulario, id_frame, accion) {
    /*
     * envia un form que necesite recargar un frame con ajax
     */

    var valores = "";
    var destino = "";

    destino = document.getElementById(id_formulario).action;
    valores = coge_valores_del_form(id_formulario);

    carga_bloque_dinamico(nuevo_xmlhttp(), id_frame, destino+"?"+valores, accion);
}

function coge_valores_del_form(id_formulario) {
    /*
     * recoge los valores de un formulario dado y forma una cadena del tipo get
     * no se añade el ? inicial
     */

    var elementos;
    var conector = ""
    var cadena = "";

    elementos = document.getElementById(id_formulario).elements;
    for (n=0; n < elementos.length; n++)
    {
        if (n > 0) {
            conector = "&";
        }
        if (elementos[n].type == "radio") {
            if (elementos[n].checked) {
                cadena += conector+elementos[n].name+"="+elementos[n].value;
            }
        } else {
            cadena += conector+elementos[n].name+"="+elementos[n].value;
        }
    }
    //alert(cadena);
    return cadena;
}


/*
 *          MRT
 */


function cierra_edicion() {
    document.getElementById("form_edicion").style.display="none";
}

function muestra_tab(objeto, tab) {
    var elementos;

    document.getElementById("tab_panel_1").className="tab_off";
    document.getElementById("tab_panel_2").className="tab_off";
    document.getElementById("tab_panel_3").className="tab_off";
    document.getElementById("tab_panel_4").className="tab_off";
    document.getElementById("tab_panel_5").className="tab_off";
    document.getElementById("tab_panel_6").className="tab_off";
    document.getElementById(tab).className="tab_on";
    elementos = document.getElementsByTagName("span");
    for (n=0; n < elementos.length; n++)
    {
        if (elementos[n].className == "tab_title_on")
        {
            elementos[n].className="tab_title_off";
        }
    }
    objeto.className="tab_title_on";
}

function edita_contenido(referencia) {
    document.getElementById("form_edicion").style.display="block";
    blanket_size();
    carga_bloque_dinamico(nuevo_xmlhttp(), "form_edicion", "ad._contenido.part.php?r="+referencia, "");
}

function edita_etiqueta(referencia) {
    document.getElementById("form_edicion").style.display="block";
    blanket_size();
    carga_bloque_dinamico(nuevo_xmlhttp(), "form_edicion", "ad._etiqueta.part.php?r="+referencia, "");
}

/*
 * redimensiona la capa transparente de los popups para que ocupe toda la
 * altura de la pagina
 */
function blanket_size() {
    if (typeof window.innerWidth != 'undefined') {
        viewportheight = window.innerHeight;
    } else {
        viewportheight = document.documentElement.clientHeight;
    }
    if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
        blanket_height = viewportheight;
    } else {
        if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
            blanket_height = document.body.parentNode.clientHeight;
        } else {
            blanket_height = document.body.parentNode.scrollHeight;
        }
    }
    var blanket = document.getElementById('form_edicion');
    blanket.style.height = blanket_height + 'px';
}


/*
 *          MAR
 */

function pago_tpv() {
    envia_formulario("tpv");
}

function carga_booking_form() {
    carga_bloque_dinamico(nuevo_xmlhttp(), "frame_form_reserva", "form.part.php", "");
}

function muestra_imagen(url) {
    // oculta el flash
    var flash = document.getElementById("1001");
    if (flash) {
        flash.style.display="none";
    }
    // muestra la capa con la imagen
    document.getElementById("form_edicion").style.display="block";
    blanket_size();
    carga_bloque_dinamico(nuevo_xmlhttp(), "form_edicion", "imagen.part.php?u="+url, "");
}

function muestra_mapa(url) {
    document.getElementById("form_edicion").style.display="block";
    blanket_size();
    carga_bloque_dinamico(nuevo_xmlhttp(), "form_edicion", "mapa.part.php?u="+url, "");
}

function oculta_imagen() {
    // muestra el flash
    var flash = document.getElementById("1001");
    if (flash) {
        flash.style.display="block";
    }
    // oculta la capa con la imagen
    document.getElementById("form_edicion").style.display="none";
}

function imagen_siguiente() {
    var imagenes = new Array(
        "imagenes/f-home-gr-1.jpg",
        "imagenes/f-home-gr-2.jpg",
        "imagenes/f-home-gr-3.jpg",
        "imagenes/f-home-gr-4.jpg",
        "imagenes/f-home-gr-5.jpg",
        "imagenes/f-home-gr-6.jpg",
        "imagenes/f-home-gr-7.jpg",
        "imagenes/f-home-gr-8.jpg"
    );
    var siguiente = parseInt(document.getElementById("img").alt) + 1;
    if (siguiente > 7) {
        siguiente = 0;
    }
    document.getElementById("img").src = imagenes[siguiente];
    document.getElementById("img").alt = siguiente;
}

function imagen_anterior() {
    var imagenes = new Array(
        "imagenes/f-home-gr-1.jpg",
        "imagenes/f-home-gr-2.jpg",
        "imagenes/f-home-gr-3.jpg",
        "imagenes/f-home-gr-4.jpg",
        "imagenes/f-home-gr-5.jpg",
        "imagenes/f-home-gr-6.jpg",
        "imagenes/f-home-gr-7.jpg",
        "imagenes/f-home-gr-8.jpg"
    );
    var siguiente = parseInt(document.getElementById("img").alt) - 1;
    if (siguiente < 0) {
        siguiente = 7;
    }
    document.getElementById("img").src = imagenes[siguiente];
    document.getElementById("img").alt = siguiente;
}


/*
 * selecciona la excursion 'numero' como la seleccionada y recarga el frame
 */
function marca_excursion(numero) {
    document.getElementById("item_excursion").value=numero;
    envia_form_xmlhttp("form_reserva", "frame_form_reserva", "");
}

/*
 * selecciona la salida 'numero' y recarga el frame
 */
function marca_salida(numero) {
    document.getElementById("item_salida").value=numero;
    envia_form_xmlhttp("form_reserva", "frame_form_reserva", "");
}

function recarga_reserva() {
    envia_form_xmlhttp("form_reserva", "frame_form_reserva", "");
}

function edita_reserva(referencia) {
    document.getElementById("form_edicion").style.display="block";
    blanket_size();
    carga_bloque_dinamico(nuevo_xmlhttp(), "form_edicion", "ad._reserva.part.php?r="+referencia, "");
}

/*
 * carga la siguiente pagina del formulario de reserva
 */
function siguiente_formulario(numero) {
    var paso = document.getElementById("item_pagina").value;
    var valida = true;
    if (paso == 0) {
        valida = valida_formulario();
    }
    if (valida == true) {
        document.getElementById("item_pagina").value=numero;        
        envia_form_xmlhttp("form_reserva", "frame_form_reserva", "");
        carga_bloque_dinamico(nuevo_xmlhttp(), "res_pasos", "pasos.part.php?p="+numero, "");
    } else {
        carga_bloque_dinamico(nuevo_xmlhttp(), "res_form_error", "form_er.part.php?e="+valida, "");
        document.getElementById("res_form_error").style.display="block";
    }
}

/*
 * comprueba que se hayan asignado todos los valores requeridos
 */
function valida_formulario() {
    var nombre = document.getElementById("item_nom").value;
    var mail = document.getElementById("item_mail").value;
    var direccion = document.getElementById("item_direccio").value;
    var telefono = document.getElementById("item_telefon").value;
    var salida = document.getElementById("item_salida").value;
    var excursion = document.getElementById("item_excursion").value;
    var error = true;
    if (nombre == "") {
        error = "NOMBRE";
    } else if (mail == "") {
        error = "EMAIL";
    } else if (direccion == "") {
        error = "DIRECCION";
    } else if (telefono == "") {
        error = "TELEFONO";
    } else if (salida == "") {
        error = "SALIDA";
    } else if (excursion == "") {
        error = "EXCURSION";
    }
    return error;
}

/*
 * oculta el aviso de campo requerido
 */
function cierra_aviso() {
    document.getElementById("res_form_error").style.display="none";
}

function resfreca_edicion_reserva() {
    envia_form_xmlhttp("form_reserva", "form_edicion", "");
}

function guarda_reserva(id) {
    var formulario = document.getElementById("form_reserva");

    formulario.method="POST";
    formulario.action="?r="+id;
    formulario.submit();
}

function edita_articulo(id) {
    document.getElementById("form_edicion").style.display="block";
    blanket_size();
    carga_bloque_dinamico(nuevo_xmlhttp(), "form_edicion", "ad._blog.part.php?r="+id, "");
}

function resfreca_planning() {
    envia_formulario("form_planning");
}

function imprime_planning(fecha) {
    window.open("ad.impreso_planning.php?f="+fecha, "Planning", "width=850,height=800,scrollbars=YES");
}
