﻿
var addEvent = function(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {
        elm.addEventListener(evType, fn, useCapture);
        return true;
    } else if (elm.attachEvent) {
        var r = elm.attachEvent('on' + evType, fn);
        return r;
    } else {
        elm['on' + evType] = fn;
    }
};

var _$ = function(idObj) {
    return document.getElementById(idObj);
};



/*Constants elements*/
var searchType = 'H';
var flightType = 'Rt';
var bkCookie = 'uv-bk-search';

addEvent(_$('rooms'), 'change', function() {
    cambiocuartos(_$('rooms'));

}, false);

addEvent(_$('childrenR1'), 'change', function() {
    cambioninios(_$('childrenR1'), '1');
}, false);

addEvent(_$('childrenR2'), 'change', function() {
    cambioninios(_$('childrenR2'), '2');
}, false);

addEvent(_$('childrenR3'), 'change', function() {
    cambioninios(_$('childrenR3'), '3');

}, false);





addEvent(_$('btnBookingSearch'), 'click', function() {

    if (AgesValid() == false) {
        alert('Especifique las edades de los menores');
        return 0;
    }

    var bk;
    var copyhtml = 0;

    bk = document.forms["FrmBookingDNNTrick"];
    if (!bk) {
        bk = document.forms[0];
    }
    else
    { copyhtml = 1; }

    var item = -1;
    if (_$('hotel').value.split(',').length > 0) {
        for (i = 0; i < dnnUrlHotels.length; i++) {
            if (dnnUrlHotels[i].Id == _$('hotel').value.split(',')[0]) { item = i; break; }
        }
    }


    if (item >= 0) {
        _$('iProperty').value = dnnUrlHotels[i].Name, _$('hotel').value.split(',')[1];
        _$('iProvider').value = "0";
        _$('iChainCode').value = "UV";
        _$('iAccessCode').value = _$('promo').value;
        _$('iCurrency').value = _$('currency').value;
        _$('iTab').value = "Rates";
        _$('iStartDate').value = _$('checkin').value;
        _$('iEndDate').value = _$('checkout').value;
        _$('iAges').value = "";
        _$('iChildren').value = "";
        _$('iAdults').value = "";


        var ad = 0;
        var ch = 0;

        /*  for (var idx = 0; idx < _$('rooms').value; idx++) {

	            if (_$('iChildren').value.split(',').length > idx) {

	                ch = eval(ch) + eval(_$('iChildren').value.split(',')[idx]);

	            }
        if (_$('iAdults').value.split(',').length > idx) {
        ad = eval(ad) + eval(_$('iAdults').value.split(',')[idx]);
        }
        }

	        if (_$('children').value != ch) {

	            _$('iChildren').value = _$('children').value;
        _$('iAges').value = "";
        }
        if (_$('adults').value != ad) {

	            _$('iAdults').value = _$('adults').value;
        }*/

        for (var idxR = 1; idxR <= 3; idxR++) {

            var Ch = _$('childrenR' + idxR).value;
            if (_$('iChildren').value != '') {
                _$('iChildren').value = _$('iChildren').value + ',';
            }

            var Ad = _$('adultsR' + idxR).value;
            if (_$('iAdults').value != '') {
                _$('iAdults').value = _$('iAdults').value + ',';
            }


            _$('iChildren').value = _$('iChildren').value + Ch;
            _$('iAdults').value = _$('iAdults').value + Ad;
            for (var idx = 1; idx <= 3; idx++) {
                var IDInput = 'ChildR' + idxR + 'Age' + idx;
                _$('iAges').value = _$('iAges').value + _$(IDInput).value
                if (_$('iAges').value != '') {
                    _$('iAges').value = _$('iAges').value + ',';
                }


            }


        }


        _$('iRooms').value = _$('rooms').value;

    }


    if (copyhtml == 1 && _$('dvBookingData') && _$('bookingGrid')) {
        if (document.all)
            _$('dvBookingData').innerHTML = _$('bookingGrid').innerHTML;
        else
            bk.appendChild(_$('bookingGrid'));
    }

    if (item >= 0) {

        bk.action = dnnUrlHotels[i].Url + RatesPage;
        bk.method = 'Post';


        bk.submit();
    }
    //return false;

}, false);

function isMatch(e1, e2) {
    try {
        if (e1.toLowerCase().trim() == e2.toLowerCase().trim())
        { return 1; }
    } catch (err) {
        if (e1.trim() == e2.trim())
        { return 1; }
    }
    return 0;
}


function ActiveDropDownList() {
    for (var i = 1; i <= 3; i++) {
        for (var j = 1; j <= 3; j++) {
            _$('dvCA' + i + j).style.visibility = 'visible';
        }
    }
}
function cambiocuartos(obj) {
    ActiveDropDownList();
    var oculta = 0;
    var c = obj.value;
    var o;
    if (c) {
        for (var j = 2; ; j++) {
            o = _$("rowR" + j);
            if (o) {
                o.style.display = "none";
                o = _$("RowAges" + j);
                o.style.display = "none";
            }
            else break;
        }
        for (var i = 1; eval(i) <= eval(c); i++) {
            o = _$("rowR" + i);
            if (o) o.style.display = '';
            o = _$("RowAges" + i);
            var strcmb = '';

            if (i == 1) { strcmb = 'childrenR1'; }
            if (i == 2) { strcmb = 'childrenR2'; }
            if (i == 3) { strcmb = 'childrenR3'; }

            if (o && eval(_$(strcmb).value) > 0)
                o.style.display = '';
            else o.style.display = 'none';
            o = _$(strcmb);
            if (eval(o.value) > 0) oculta = 1;
        }
    }

}
function cambioninios(obj, c) {
    ActiveDropDownList();
    var v = obj.value;
    var o;
    var oc;
    var i;
    var oculta = 0;
    for (i = 1; ; i++) {
        o = _$('dvCA' + c + i);

        if (o) { o.style.display = 'none'; }
        else break;
    }

    o = _$("RowAges" + c);

    if (eval(v) > 0) {
        o.style.display = '';
        for (i = 0; eval(i) <= eval(v); i++) {

            o = _$("dvCA" + c + i);
            if (o) { o.style.display = ''; }
        }
        if (o) o.style.display = "";
        oculta = 1;
    }
    else {
        o.style.display = 'none';

        for (i = 1; i <= 3; i++) {
            o = null;
            if (i == 1) { o = _$('childrenR1'); }
            if (i == 2) { o = _$('childrenR2'); }
            if (i == 3) { o = _$('childrenR3'); }


            oc = _$("RowAges" + i);
            if (o) {
                if (eval(o.value) > 0 && oc.style.display == '') {
                    oculta = 1;
                }
            }
            else break;
        }
    }

}
function Cambio() {
    var ocuartos = _$('rooms');
    cambiocuartos(ocuartos);
    var cuartos = ocuartos.value;
    for (i = 1; eval(i) <= eval(cuartos); i++) {

        if (i == 1) { obj = _$('childrenR1'); }
        if (i == 2) { obj = _$('childrenR2'); }
        if (i == 3) { obj = _$('childrenR3'); }

        if (obj) cambioninios(obj, '' + i + '');
    }
}



function AgesValid() {
    var Rooms = _$('rooms').value;

    for (var idxR = 1; idxR <= Rooms; idxR++) {

        var Ch = _$('childrenR' + idxR).value;
        if (Ch > 0) {
            for (var idx = 1; idx <= Ch; idx++) {
                var IDInput = 'ChildR' + idxR + 'Age' + idx;
                if (_$(IDInput).value == '') {
                    return false;
                }

            }
        }


    }

    return true;
}



var locations = "";
$(document).ready(function() {

    /* $("select#neighborhood").change(function() {
    $("select#hotel").hide();

	        var optionsCities = '';
    var currentCountry = this.value;
    $.each(locations.area, function(i, itemCountry) {
    if (itemCountry.value == currentCountry)
    $.each(itemCountry.city, function(j, itemCity) {
    optionsCities += '<option value="' + itemCity.value + '">' + itemCity.text + '</option>';
    });
    });

	        $("select#hotel").html(optionsCities).fadeIn("fast");
    });*/

    //Show loading.. msg
    //$("select#neighborhood").html("<option>Loading...</option>");
    $("select#hotel").html("<option>Loading...</option>");

    //Fill Combo Cities 
    $.getJSON(VarUrlSite + "GetHotels.aspx", function(data) {
        locations = data;
        var locationsaux = data;
        var optionsCountries = '';
        $.each(locations.area, function(i, itemCountry) {
            //optionsCountries += '<option value="' + itemCountry.value

            optionsCountries += '<optgroup label="' + itemCountry.text + '">;'


              $.each(itemCountry.city, function(j, itemCity) {
                if (vrIdProperty == itemCity.propertyNumber) {
                    optionsCountries += '<option SELECTED value="' + itemCity.propertyNumber + '">' + itemCity.text + '</option>';
                }
                else {
                    optionsCountries += '<option value="' + itemCity.propertyNumber + '">' + itemCity.text + '</option>';
                }

            });
            optionsCountries + '</optgroup>'

        });
        $("select#hotel").html(optionsCountries).trigger("change");
        Cambio();
    });
});

