
// LANGUAGE
function updateEngLinks() {
//    var cCurLang = $.query.get("lang");
//    if (cCurLang != '' && typeof(cCurLang)!='boolean') {
//        $('a:not(a[href*="lang"])').each(function () {
//            var cLink = $(this).attr('href');
//            if (cLink && cLink.indexOf('aspx') >= 0) {
//                if (cLink.indexOf('?') > 0) {
//                    cLink += '&lang=' + cCurLang;
//                } else {
//                    cLink += '?lang=' + +cCurLang;
//                }
//                $(this).attr('href', cLink);
////                cLink = cLink.replace(/&{2,}/g, "&");
//            }
//        });
//    }
}

function ToggleLanguage(cChangeToLang) {
    var s = window.location.href.replace(/[\?&]+lang=[^&]*/gi, '');
    if (cChangeToLang) {
        if (s.indexOf('?') >= 0) {
            s = s + ("&lang=" + cChangeToLang);
        } else {
            s = s + ("?lang=" + cChangeToLang);
        }
    }
    window.location.href = s;
    return false;
    //window.location.href = $.query.REMOVE("lang").set("lang", cChangeToLang);
}

// HELP
function doPopup(href) {
    w = 600;
    h = 500;
    w = (w ? w : parseInt((screen.availWidth) / 2)) + 20
    h = (h ? h : parseInt((screen.availHeight) / 2)) + 30
    l = parseInt((screen.availWidth - w) / 2),
t = parseInt((screen.availHeight - h) / 2) - 10;
    p = window.open(href, "pop", 'resizable=yes,scrollbars=yes,location=no,menubar=no,resizable=no,status=no,toolbar=no,width=' + w + ',height=' + h + ',left=' + l + ',top=' + t);
    p.focus();
}
function InitDays() {
    
    var cOldVal = $('.ddlDay').val();
    var cVal = $('.ddlYearMonth').val();
    
    if (!cVal) {
        return;
    }

    
    var aVals = cVal.split('_');
    if (aVals.length == 2) {

        var nDayInMonth = daysInMonth(aVals[0], aVals[1]);

        // CLEAR DAYS
        var oDdl = $('.ddlDay').get(0);
        oDdl.options.length = 0;


        var dCurDate = new Date();
        var nCurMonth = dCurDate.getMonth()+1;
        var nCurDay = dCurDate.getDate();
        //alert(nCurDay);
        for (var i = 1; i <= nDayInMonth; i++) {

            // DON'T BOOK TO THE PAST DATE
            
            if (nCurMonth == parseInt(aVals[0])
            && nCurDay >= i) {
                continue;
            }
        
            var oOption = document.createElement('OPTION');


            if (cOldVal == i) {

                oOption.selected = true;
            }
            oDdl.options.add(oOption);
            oOption.value = i;
            oOption.innerHTML = i;
        }
    }
}
//    function daysInMonth(iMonth, iYear) {
//        return 32 - new Date(iYear, iMonth, 32).getDate();
//    }
function daysInMonth(month, year) {
    var dd = new Date(year, month, 0);
    return dd.getDate();
}
function GetDate() {
    var cDayVal = $('.ddlDay').val();
    var cMonthYear = $('.ddlYearMonth').val();
    
    var aVals = cMonthYear.split('_');
    if (aVals.length == 2) {
        var cMonth = aVals[0];
        var cYear = aVals[1];
        return new Date(cDayVal, cMonth, cYear);
    }
}

// ORDER FORM
function CalcTotalPrice() {
    var nQtyVal = parseInt($('.nqty').val());
    var nRoomId = parseInt($('#ddlRoom').val());
//    var nDaysVal=parseInt($('#ddlNights').val());

    if (isNaN(nRoomId)) {
        return;
    }

    var nRoomPrice = 0;
    var nDiscount = 0;
    
    // PRICE
    for (var i = 0; i < dPrices.Keys.length; i++) {
        if (dPrices.Keys[i] == nRoomId) {
            nRoomPrice = dPrices.Lookup(dPrices.Keys[i]);
            break;
        }
    }
    // DISCOUNT
    for (var i = 0; i < dDiscounts.Keys.length; i++) {
        if (dDiscounts.Keys[i] == nRoomId) {
            nDiscount = dDiscounts.Lookup(dDiscounts.Keys[i]);
            break;
        }
    }

    
    // PRICE
    if (nRoomPrice > 0) {
//        $('#nTotal').html(nRoomPrice * nQtyVal);
        $('#nTotal').html(nRoomPrice * nQtyVal * nDaysVal);
    } 
    

    // DISCOUNT
    if (nDiscount > 0) {
        nRoomPrice = nRoomPrice - (nRoomPrice * (nDiscount / 100)); // CHANGE THE MEANING TO THE DISCOUNTED PRICE
        

        $('#nPercent').html(nDiscount);
        $('#nNewTotal').html(nRoomPrice * nQtyVal);
        $('#oDiscount').show();
        $('#oTotal').removeClass('red');

    } else {

        $('#oDiscount').hide();
        if (!$('#oTotal').hasClass('red')) {
            $('#oTotal').addClass('red');
        }
    }


}
function ToggleUpload(cVal) {

    if (cVal == 'безналичная') {
        $('#trUpload').css('visibility', 'visible');
    } else {
        $('#trUpload').css('visibility', 'hidden');
    }
}

// DICTIONARY
function Lookup(key) {
    return (this[key]);
}
function Delete() {
    for (c = 0; c < Delete.arguments.length; c++) {
        this[Delete.arguments[c]] = null;
    }
    // Adjust the keys (not terribly efficient)
    var keys = new Array()
    for (var i = 0; i < this.Keys.length; i++) {
        if (this[this.Keys[i]] != null)
            keys[keys.length] = this.Keys[i];
    }
    this.Keys = keys;
}

function Add() {
    for (c = 0; c < Add.arguments.length; c += 2) {
        // Add the property
        this[Add.arguments[c]] = Add.arguments[c + 1];
        // And add it to the keys array
        this.Keys[this.Keys.length] = Add.arguments[c];
    }
}

function Dictionary() {
    this.Add = Add;
    this.Lookup = Lookup;
    this.Delete = Delete;
    this.Keys = new Array();
}

