﻿var _idCtrlPobOf = "";
var _idCtrlPobOf = "";
var _idHidPodOf = "";
var _idHotel = "";
var resul = "";

window.onload = iniciaOfertas;

function iniciaOfertas() {
    CalendarioLib.GestionPet.InitFechas();
    getCtl("hidParamsAjax").value = "ofertas" + sepGra + "inicio" + sepBloc + _idHotel;
    getCtl("hidFunctionAjax").value = "inicio";
    petiAjaxMain(getCtl("hidParamsAjax").value, iniciaOfertasVuelta);
}

function BucarOfertas() {
    ocultaCtl("dvCont");
    verCtl("divLoadingOfer");
    var selHoteles = $("selHoteles");
    var idhotel = [];
    _idHotel = "";

    if ($F("txtPoblacion") != "" && $F("selHoteles") == "") {
        for (var i = 0; i < selHoteles.length; i++) {
            _idHotel += selHoteles.options[i].value + sepGra;
        }
    } else if (selHoteles.selectedIndex != 0 || selHoteles.options[selHoteles.selectedIndex].value != "") {
        if (selHoteles.options[selHoteles.selectedIndex].value == "undefined")
            _idHotel = "-1";
        else _idHotel = sepGra + $F("selHoteles");
    } else {
        _idHotel = "";
    }
    iniciaOfertas();
}

function iniciaOfertasVuelta(res) {
    var resul = res.split(sepBloc);
    //alert(resul[1]);
    getCtl("dvCont").innerHTML = resul[0];
    ocultaCtl("divLoading");
    verCtl("tblDatos");
    verCtl("dvCont");
    if (_idHotel == "") {
        GetAutoCompleteOfertas("ListaDestinosOfertas", resul[1], null, "txtPoblacion", "selHoteles", "hidCodPobla");
    }
    ocultaCtl("divLoadingOfer");
    iniciaLoginWeb();
}

function cambioIdiomaOfertas() {
    switch (getCtl("hidFunctionAjax").value) {
        case "inicio":
            petiAjaxMain(getCtl("hidParamsAjax").value, iniciaOfertasVuelta);
            break;
    }
}

function verOfertas(codigoHot) {
    var href = "";
    var hidServer = getCtl(idCabecera + "hidServer").value;
    var hidEsReal = getCtl(idCabecera + "hidEsReal").value;

    if (hidEsReal == "1") {
        hidServer = hidServer.substring(hidServer.indexOf("//") + 2, hidServer.length);
        href = "https://" + hidServer + Path;
    }

    window.location.href = href + "oferta.aspx?idhotel=" + codigoHot;
}

function GetAutoCompleteOfertas(opcion, idioma, cod, idCtrlPob, idCtrlHot, idHidPod) {
    var operacion = "";
    var mensaje = "";
    var parametros = "";

    if (idCtrlPob != null) _idCtrlPobOf = idCtrlPob;
    if (idCtrlHot != null) _idCtrlHotOf = idCtrlHot;
    if (idHidPod != null) _idHidPodOf = idHidPod;

    // Deshabilitamos la lista de hoteles mientras se procesa la petición
    var selHoteles = document.getElementById(_idCtrlHotOf);
    if (selHoteles != null) {
        selHoteles.disabled = true;
    }
    switch (opcion) {
        case "ListaDestinosOfertas":
        case "ListaHotelesOfertas":
            parametros = "opcion=" + opcion;
            break;

        case "ListaHotelesFiltradaOfertas":
            var poblacion = document.getElementById("poblacion-form");
            parametros = "opcion=" + opcion + "&cod=" + cod;
            break;
    }
    sendCall("getPost.aspx", parametros);
}

function AutoCompleteProvOfertas(res) {
    var resul = res.split(sepBloc);
    var wordHotel = resul[0].split(sepElem);
    var arrAux = new Array();
    var customarray = resul[1].split("|"); // Andorra(23#14)
    for (var i = 0; i < customarray.length; i++) {
        arrAux = customarray[i].split("#");
        arrAux[0] += " ";
        if (parseInt(customarray[i].substring(customarray[i].indexOf("(") + 1, customarray[i].indexOf("#"))) > 1)
            arrAux[0] += wordHotel[1];
        else
            arrAux[0] += wordHotel[0];
        customarray[i] = arrAux[0] + "#" + arrAux[1];
    }
    //var vElements = new Array(customarray.length);
    var obj = new actb(_idCtrlPobOf, customarray, null, _idHidPodOf);
    GetAutoCompleteOfertas("ListaHotelesOfertas", "es", null);
}

function AutoCompleteHot(res) {
    var hidHoteles = document.getElementById("hidHoteles");
}

function AutoCompleteSelHotOfertas(res) {
    var selHot = document.getElementById(_idCtrlHotOf);
    selHot.options.length = 0;
    var result = "";
    var txtVal;
    var hayResult = false;

    var bloque = res.split(sepGra);
    var hoteles = bloque[0].split("|");
    if (hoteles.length > 1) result += "" + sepVal + "" + sepElem;
    //if(res == "") selHot.style.width = "150px";
    //else selHot.style.width = "";

    for (var i = 0; i < hoteles.length; i++) {
        txtVal = hoteles[i].split("#");
        result += txtVal[0] + sepVal + txtVal[1] + sepElem;
    }

    if (result.length > 0) {
        result = result.substring(0, result.length - sepElem.length);
        cargaSel(_idCtrlHotOf, result);
    }

    // Y volvemos a habilitar el desplegable
    selHot.disabled = false;
}