function sendVacancy(cId) {
    var $s = $('select[id$="vac1"]');
    if ($s.length>0) {
        $s.val(cId);
        location.href = "#vac";
        $('input[id$="fio"]').focus();
    }
}

// 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('_');
    var cMonth = aVals[0];
    var cYear = aVals[1];
    var Ret = new Date(parseFloat(cYear), parseFloat(cMonth) - 1, parseFloat(cDayVal));
    return Ret;
}

// ORDER FORM
function CalcTotalPrice() {
    var nQtyVal = 1;
//    var nQtyVal = parseFloat($('.nqty').val());
    var nRoomId = parseFloat($('#ddlRoom').val());
    var nDaysVal = parseFloat($('select[id$=ddlNights]').val());
    var DiscountDays = 0;
    var sDate = GetDate();
    if($('#ddlRoom').length==0) return;



    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;
        }
    }
    //Number of fridays and saturdays
    if ((/люкс/i).test($("#ddlRoom option:selected").text())) {
        var TestDay = sDate;
        for (var i = 0; i < nDaysVal; i++) {
            TestDay.setDate(TestDay.getDate() + i);
            if (TestDay.getDay() >= 5) {
                DiscountDays++;
            }
        }
    }
    // PRICE
        if (nRoomPrice > 0) {
            //        $('#nTotal').html(nRoomPrice * nQtyVal);
            
            $('#nTotal').html(nRoomPrice * nQtyVal * nDaysVal - nRoomPrice * 0.2 * DiscountDays);
        } 
    

    // 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();
}

function ClearValidation(cVal) {
    $('input[name$="hType"]').val(cVal);
}

function ShowNotFoundMsg(cMsg) {
    $('#trBookDetails1').hide();
    $('#trBookDetails2').hide();
    $('#trNotFound').show();
    $('#trNotFound td #err' + cMsg).show();
}

function RemoveRoomParam() {
    window.location.href = $.query.remove('roomid');
}


$(function () {
    //$(".ddlDay").change(CalcTotalPrice);
    //$(".ddlYearMonth").change(CalcTotalPrice);
    //$(".ddlNights").change(CalcTotalPrice);
        changeAttr(0);
    $(".chlist").change(function () {
        changeAttr(0);
    });
    $("#ddlRoom").change(function () {
        changeAttr(1);
    });

});

function changeAttr(i) {
    var cVal = $("select[name$='ddlYearMonth']").val();
    if (cVal) {
        var day = $("select[name$='ddlDay']").val();
        var month = ""; var year = "";
        var aVals = cVal.split('_');
        if (aVals.length == 2) {
            month = aVals[0];
            year = aVals[1];
        }
        var night = $("select[name$='ddlNights']").val();
        var accom = $("select[name$='ddlAccom']").val();
        var room = $("select[id='ddlRoom']").val();
        if (!room) room = $('input[name$="hType"]').val();

        if (i == 0) {
            $.ajax({
                type: "POST",
                url: "getList.aspx",
                data: "day=" + day + "&month=" + month + "&year=" + year + "&night=" + night + "&accom=" + accom + "&room=" + room,
                success: function (data) {
                    $("#ddlRoom").html(data);
                    var s = $("#ddlRoom").val();
                    setStat(s);
                    if (s!="") {
                        $.ajax({
                            type: "POST",
                            url: "getList.aspx",
                            data: "day=" + day + "&month=" + month + "&year=" + year + "&night=" + night + "&accom=" + accom + "&room=" + s + "&calc=1",
                            success: function (data) {
                                $("#tot").html(data);
                            }
                        });
                    }
                }
            });
        } else {
            var s = $("#ddlRoom").val();
            setStat(s);
            if (s) {
                $.ajax({
                    type: "POST",
                    url: "getList.aspx",
                    data: "day=" + day + "&month=" + month + "&year=" + year + "&night=" + night + "&accom=" + accom + "&room=" + s + "&calc=1",
                    success: function (data) {
                        $("#tot").html(data);
                    }
                });
            }
        }

    }
}

function setStat(s) {
    if (s) {
        $("#ddlRoom").attr("style", "display:block");
        $('input[name$="hType"]').val(s);
        $("#err55").attr("style", "display:none");
    } else {
        $('input[name$="hType"]').val("");
        $("#tot").html("");
        if ($("#ddlRoom").get(0).options.length == 0) {
            $("#ddlRoom").attr("style", "display:none");
            $("#err55").attr("style", "display:block");
        }
    }

}

