function $(id)
{
    return document.getElementById(id);
}

function RLCalc()
{
    var RLC = new Object();
    RLC={
        objId : null,
        //////////////////////
        //////Константы///////
        //////////////////////
        accommodationType1Text : "",
        accommodationType2Text : "",
        accommodationType3Text : "",
        autumnSemesterTill : "",
        costAccommodation1 : 0,
        costAccommodation2 : 0,
        costAutumnSemester : 0,
        costAutumnSemesterSTP : 0,
        costCourseType1 : 0,
        costCourseType1ILIM : 0, //для I live in Moscow другая цена
        costCourseType2 : 0,
        costCourseType3 : 0,
        costCourseType4 : 0,
        costCourseType5 : 0,
        costCourseType6 : 0,
        costCourseType6ILIM : 0, //для I live in Moscow другая цена        
        costCourseType7 : 0,
        costCourseType7ILIM : 0, //для I live in Moscow другая цена        
        costCourseType8 : 0,
        costCourseType9 : 0,
        costCourseType10 : 0,
        costSem3Weeks : 0,
        costSem4Weeks : 0,
        costSem5Weeks : 0,
        costSem6Weeks : 0,
        costSemInc : 0,
        costSP3Weeks : 0,
        costSP4Weeks : 0,
        costSP5Weeks : 0,
        costSP6Weeks : 0,
        costSPInc : 0,
        costSPInd : 0,
        costSpringSemester : 0,
        costSpringSemesterSTP : 0,
        costTransferIn : 0,
        costTransferTo : 0,
        costVisa1 : 0,
        costVisa2 : 0,
        costVisaInc : 0,
        costYear : 0,
        costYearSTP : 0,
        CoursesType1Text : "", //текст группы 20ч/нед
        CoursesType2Text : "",
        CoursesType3Text : "",
        CoursesType4Text : "",
        CoursesType5Text : "",
        CoursesType6Text : "",
        CoursesType7Text : "",
        CoursesType8Text : "", //текст утренние программы
        CoursesType9Text : "", //текст вечерние программы
        CoursesType10Text : "", //текст выходные программы
        datesText : "", //текст для i live in moscow и my own dates
        datesText2 : "", //текст для семестров и академ года
        springSemesterFrom : "",
        summerFrom : "",
        summerTill : "",
        thisAutumnSemesterTill : "",
        thisSpringSemesterFrom : "",
        thisYearFrom : "",
        thisYearTill : "",
        totalCommentText : "",
        totalCommentILIMText : "",
        VisaType1Text : "",  //текст "Стандартная виза"
        VisaType2Text : "",  //текст "Срочная виза"
        VisaType3Text : "",  //текст "Не требуется"
        visaInc : 0,
        visaDec : 0,
        yearFrom : "",
        yearTill : "",
        //////////////////////
        //////Переменные//////
        //////////////////////
        courseId : "0",
        maxWeeksCount : null, //максимальное кол-во недель для летних программ или текущего весеннего/осеннего семестров
        programId : null,
        weeksCount : null,      
        //////////////////////
        ///////Контролы///////
        //////////////////////
        ddlAccommodation : null,
        ddlCoursesType : null,
        ddlVisa : null,
        ddlWeeksCount : null,
        hfAccommodation : null,
        hfCostCourses : null,
        hfCostVisa : null,
        hfCostAccommodation : null,
        hfCostTransferIn : null,
        hfCostTransferTo : null,
        hfCostTotal : null,
        hfCoursesType : null,
        hfCoursesTypeValue : null,
        hfDISelectedIndex : null,
        hfWCSelectedIndex : null,
        hfDISelectedText : null,
        hfVisa : null,
        hwWeeksCount : null,
        iVisa : null,
        iAccommodation : null,
        iCoursesType : null,
        iTransferIn : null,
        iTransferTo : null,
        lDates : null,
        lDiscountText : null,
        lProgramComment : null,
        lTotalComment : null,
        lbDatesIntervals : null,
        lbProgramComment : null,
        lbProgramType : null,
        rbTransferInNo : null,
        rbTransferInYes : null,
        rbTransferToNo : null,
        rbTransferToYes : null,
        sAccommodationDate : null,
        sVisaDate : null,
        sWeeksCount : null,
        tbAccommodationFrom : null,
        tbAccommodationTill : null,
        tbFrom : null,
        tbTill : null,
        tbTotal : null,
        tbVisaFrom : null,
        tbVisaTill : null,
        trAccommodation : null,
        trCoursesType : null,
        trDates : null,
        trDatesIntervalsHead : null,
        trDatesIntervalsBody : null,
        trEnroll : null,
        trTotal : null,
        trTransferIn : null,
        trTransferTo : null,
        trWeeksCount : null,
        trVisa : null,
        //////////////////////
        ///Иницианализация////
        //////////////////////
        Init : function()
        {
            RLC.lbProgramType.onchange = function()
            {
                RLC.programId = this.options[this.selectedIndex].value;
                RLC.SelectCourseType();
            }
            RLC.ddlCoursesType.onchange = function()
            {
                RLC.courseId = this.options[this.selectedIndex].value;
                RLC.hfCoursesType.value = this.selectedIndex;
                RLC.hfCoursesTypeValue.value = RLC.courseId;
                RLC.ShowWeeksCount(); //перерасчет, тк можно выбрать "weekend group"
                RLC.CalcCost();
            }
            RLC.ddlVisa.onchange = function()
            {
                RLC.hfVisa.value = this.options[this.selectedIndex].value;
                if ((RLC.programId == "3") || (RLC.programId == "4") || (RLC.programId == "5")) //для года, осеннего, весенного семестра убираем проживание
                {
                    RLC.SetAccommodationType(2);
                }
                RLC.CalcCost();
            }
            RLC.ddlAccommodation.onchange = function()
            {
                RLC.hfAccommodation.value = this.options[this.selectedIndex].value;
                if ((RLC.programId == "3") || (RLC.programId == "4") || (RLC.programId == "5")) //для года, осеннего, весенного семестра убираем проживание
                {
                    RLC.SetVisaType(0);
                }
                RLC.CalcCost();
            }
            RLC.rbTransferInNo.onclick = function() { RLC.CalcCost(); }
            RLC.rbTransferInYes.onclick = function() { RLC.CalcCost(); }
            RLC.rbTransferToNo.onclick = function() { RLC.CalcCost(); }
            RLC.rbTransferToYes.onclick = function() { RLC.CalcCost(); }
            RLC.ddlWeeksCount.onchange = function()
            {
                RLC.hfWCSelectedIndex.value = this.selectedIndex;
                RLC.FillDatesIntervals();
                RLC.CalcCost();
            }
            RLC.lbDatesIntervals.onchange = function()
            {
                if (this.options.length == 0) return;
                RLC.hfDISelectedIndex.value = this.selectedIndex;
                RLC.hfDISelectedText.value = this.options[this.selectedIndex].text;
                
                var datesText = this.options[this.selectedIndex].text;
                RLC.SetDates(datesText.substring(0, 10), datesText.substring(13));
            }
            
            RLC.tbFrom.readOnly = "readonly";
            RLC.tbTill.readOnly = "readonly";
            
            RLC.iAccommodation = document.getElementById("iAccommodation");
            RLC.iCoursesType = document.getElementById("iCoursesType");
            RLC.iTransferIn = document.getElementById("iTransferIn");
            RLC.iTransferTo = document.getElementById("iTransferTo");
            RLC.iVisa = document.getElementById("iVisa");
            
            RLC.ResetState();
        },
        //////////////////////
        //Глобальные функции//
        //////////////////////
        SelectCourseType : function()
        {
            RLC.ResetState();
            
            RLC.trCoursesType.style.display = "";
            RLC.trTotal.style.display = "";
            RLC.trEnroll.style.display = "";
            
            if ((RLC.programId == "3") || (RLC.programId == "4") || (RLC.programId == "5")) //для года, осеннего и весеннего семестра отличаются только даты
            {
                RLC.lDates.innerText = RLC.datesText2;
                RLC.lDates.textContent = RLC.datesText2;
            
                RLC.trDates.style.display = "";
                RLC.SetDatesEnabled(false);
                
                RLC.ddlCoursesType.options[0].selected = true;
                RLC.ddlCoursesType.disabled = true;
                
                RLC.trVisa.style.display = "";
                //RLC.ddlVisa.disabled = true;
                RLC.SetVisaType(0);
                RLC.SetVisaDatesVisible(false);
                
                RLC.trAccommodation.style.display = "";
                //RLC.ddlAccommodation.disabled = true;
                RLC.SetAccommodationType(0);
                
                RLC.trTransferIn.style.display = "";
                    
                RLC.trTransferTo.style.display = "";
            }
            else
            {
                RLC.lDates.innerText = RLC.datesText;
                RLC.lDates.textContent = RLC.datesText;
            }
            
            if ((RLC.programId == "2") || (RLC.programId == "6") || (RLC.programId == "7"))
            {
                RLC.lProgramComment.style.display = "";
                RLC.lbProgramComment.style.display = "";
            
                RLC.trDatesIntervalsHead.style.display = "";
                RLC.trDatesIntervalsBody.style.display = "";
                
                RLC.ddlCoursesType.options[0].selected = true;
                RLC.ddlCoursesType.disabled = false;
                
                RLC.trVisa.style.display = "";
                RLC.ddlVisa.disabled = false;
                RLC.SetVisaType(0);
                RLC.SetVisaDatesVisible(false);
                
                RLC.trAccommodation.style.display = "";
                RLC.ddlAccommodation.disabled = false;
                RLC.SetAccommodationType(0);
                
                RLC.trTransferIn.style.display = "";
                
                RLC.trTransferTo.style.display = "";
            }
            
            switch(RLC.programId)
            {
                case "0" :
                    RLC.ddlCoursesType.options[0].selected = true;
                    RLC.ddlCoursesType.onchange();
                    RLC.trDates.style.display = "";
                    
                    RLC.lTotalComment.innerText = RLC.totalCommentILIMText;
                    RLC.lTotalComment.textContent = RLC.totalCommentILIMText;
                    
                    if (typeof DisableControls == 'function') DisableControls(true); //внешняя функция
                break;
                case "1" :
                    RLC.trDates.style.display = "";
                    
                    RLC.trVisa.style.display = "";
                    if (RLC.tbFrom.value != "")
                    {
                        RLC.tbVisaFrom.value = getTextFromDate(getDateFromText(RLC.tbFrom.value).addDay(-RLC.visaDec))
                    }
                    if (RLC.tbTill.value != "")
                    {
                        RLC.tbVisaTill.value = getTextFromDate(getDateFromText(RLC.tbTill.value).addDay(RLC.visaInc))
                    }
                    RLC.SetVisaDatesVisible(false);
                    
                    RLC.trAccommodation.style.display = "";
                    RLC.tbAccommodationFrom.value = RLC.tbFrom.value;
                    RLC.tbAccommodationTill.value = RLC.tbTill.value;
                    
                    RLC.trTransferIn.style.display = "";
                    
                    RLC.trTransferTo.style.display = "";
                    
                    RLC.iVisa.style.display = "";
                    RLC.iAccommodation.style.display = "";
                    RLC.iCoursesType.style.display = "";
                break;
                case "2" : 
                    RLC.FillWeeksCount(RLC.summerFrom, RLC.summerTill);
                    RLC.FillDatesIntervals();
                    
                    //RLC.SetAccommodationType(2);
                    
                    //RLC.lDiscountText.style.display = "";
                    
                    RLC.lProgramComment.style.display = "none";
                    RLC.lbProgramComment.style.display = "none";
                break;
                case "3" :
                    RLC.SetDates(RLC.yearFrom, RLC.yearTill);
                    
                    RLC.SetVisaDates(getTextFromDate(getDateFromText(RLC.yearFrom).addDay(-RLC.visaDec)), getTextFromDate(getDateFromText(RLC.yearTill).addDay(RLC.visaInc)));

                    RLC.SetAccommodationDates(RLC.yearFrom, RLC.yearTill);
                break;
                case "4" :
                    RLC.SetDates(RLC.yearFrom, RLC.autumnSemesterTill);
                    
                    RLC.SetVisaDates(getTextFromDate(getDateFromText(RLC.yearFrom).addDay(-RLC.visaDec)), getTextFromDate(getDateFromText(RLC.autumnSemesterTill).addDay(RLC.visaInc)));
                    
                    RLC.SetAccommodationDates(RLC.yearFrom, RLC.autumnSemesterTill);
                break;
                case "5" :
                    RLC.SetDates(RLC.springSemesterFrom, RLC.yearTill);
                    
                    RLC.SetVisaDates(getTextFromDate(getDateFromText(RLC.springSemesterFrom).addDay(-RLC.visaDec)), getTextFromDate(getDateFromText(RLC.yearTill).addDay(RLC.visaInc)));
                    
                    RLC.SetAccommodationDates(RLC.springSemesterFrom, RLC.yearTill);
                break;
                case "6" :
                    RLC.FillWeeksCount(RLC.thisYearFrom, RLC.thisAutumnSemesterTill);
                    RLC.FillDatesIntervals();
                    RLC.SetVisaType(2);
                    RLC.SetAccommodationType(2);
                    
                    RLC.iVisa.style.display = "";
                    RLC.iAccommodation.style.display = "";
                    RLC.iCoursesType.style.display = "";
                break;
                case "7" :
                    RLC.FillWeeksCount(RLC.thisSpringSemesterFrom, RLC.thisYearTill);
                    RLC.FillDatesIntervals();
                    RLC.SetVisaType(2);
                    RLC.SetAccommodationType(2);
                    
                    RLC.iVisa.style.display = "";
                    RLC.iAccommodation.style.display = "";
                    RLC.iCoursesType.style.display = "";
                break;
                case "8" :
                    RLC.trDates.style.display = "";
                    
                    RLC.trCoursesType.style.display = "none";
                break;
            }
            
            if ((RLC.programId != 2) && (RLC.programId != 6) && (RLC.programId != 7))
            {
                RLC.trWeeksCount.style.display = "";
                RLC.ShowWeeksCount();
            }
            
            RLC.CalcCost();
        },
        ResetState : function() //возвращает калькулятор в первоначальное состояние
        {
            if (typeof DisableControls == 'function') DisableControls(false); //внешняя функция
        
            RLC.lProgramComment.style.display = "none";
            RLC.lbProgramComment.style.display = "none";
            RLC.lDiscountText.style.display = "none";
        
            RLC.trDates.style.display = "none";
            RLC.SetDatesEnabled(true);
            RLC.tbFrom.readOnly = "readonly";
            RLC.tbTill.readOnly = "readonly";
            
            RLC.trDatesIntervalsHead.style.display = "none";
            RLC.trDatesIntervalsBody.style.display = "none";
            
            RLC.trWeeksCount.style.display = "none";
            
            RLC.trCoursesType.style.display = "none";
            RLC.ddlCoursesType.disabled = false;
            
            RLC.trVisa.style.display = "none";
            RLC.ddlVisa.disabled = false;
            RLC.SetVisaDatesVisible(true);
            RLC.tbVisaFrom.readOnly = "readonly";
            RLC.tbVisaTill.readOnly = "readonly";
            RLC.SetVisaType(2);
            
            RLC.trAccommodation.style.display = "none";
            RLC.tbAccommodationFrom.readOnly = "readonly";
            RLC.tbAccommodationTill.readOnly = "readonly";
            RLC.SetAccommodationDatesVisible(false);
            RLC.tbAccommodationFrom.disabled = false;
            RLC.tbAccommodationTill.disabled = false;
            RLC.ddlAccommodation.disabled = false;
            RLC.SetAccommodationType(2);

            RLC.trTransferIn.style.display = "none";
            RLC.rbTransferInNo.checked = true;
            
            RLC.trTransferTo.style.display = "none";
            RLC.rbTransferToNo.checked = true;
            
            RLC.lTotalComment.innerText = RLC.totalCommentText;
            RLC.lTotalComment.textContent = RLC.totalCommentText;
            
            RLC.trTotal.style.display = "none";
            RLC.tbTotal.value = "";
            
            RLC.trEnroll.style.display = "none";
            
            RLC.iVisa.style.display = "none";
            RLC.iAccommodation.style.display = "none";
            RLC.iCoursesType.style.display = "none";
            
            RLC.LoadCoursesType();
            RLC.LoadVisaType();
            RLC.LoadAccommodationType();
        },
        CalcCost : function()
        {
            RLC.iVisa.value = "";
            RLC.iAccommodation.value = "";
            RLC.iTransferIn.value = "";
            RLC.iTransferTo.value = "";
            RLC.trTotal.value = "";
            
            RLC.hfCostCourses.value = "";
            RLC.hfCostVisa.value = "";
            RLC.hfCostAccommodation.value = "";
            RLC.hfCostTransferIn.value = "";
            RLC.hfCostTransferTo.value = "";
            RLC.hfCostTotal.value = "";
        
            var transfersCost = 0;
            if (RLC.programId != "0")
            {
                if (RLC.rbTransferInYes.checked) 
                {
                    transfersCost += RLC.costTransferIn;
                    RLC.iTransferIn.value = RLC.costTransferIn;
                    RLC.hfCostTransferIn.value = RLC.costTransferIn;
                }
                if (RLC.rbTransferToYes.checked)
                {
                    transfersCost += RLC.costTransferTo;
                    RLC.iTransferTo.value = RLC.costTransferTo;
                    RLC.hfCostTransferTo.value = RLC.costTransferTo;
                }
            }
            
            switch(RLC.programId)
            {
                case "0" :
                    if (RLC.weeksCount == 0) return; 

                    switch(RLC.courseId)
                    {
                        //case "0" : RLC.tbTotal.value = RLC.weeksCount * RLC.costCourseType1ILIM; //* RLC.costCourseType1
                        //break;
                        case "1" : RLC.tbTotal.value = RLC.weeksCount * RLC.costCourseType2;
                        break;
                        case "2" : RLC.tbTotal.value = RLC.weeksCount * RLC.costCourseType3;
                        break;
                        case "3" : RLC.tbTotal.value = RLC.weeksCount * RLC.costCourseType4;
                        break;
                        case "4" : RLC.tbTotal.value = RLC.weeksCount * RLC.costCourseType5;
                        break;
                        case "5" : RLC.tbTotal.value = RLC.weeksCount * RLC.costCourseType6ILIM; //RLC.tbTotal.value = RLC.weeksCount * RLC.costCourseType6;
                        break;
                        case "6" : RLC.tbTotal.value = RLC.weeksCount * RLC.costCourseType7ILIM; //RLC.tbTotal.value = RLC.weeksCount * RLC.costCourseType7;
                        break;
                        case "7" : RLC.tbTotal.value = RLC.weeksCount * RLC.costCourseType8;
                        break;
                        case "8" : RLC.tbTotal.value = RLC.weeksCount * RLC.costCourseType9;
                        break;
                        case "9" : RLC.tbTotal.value = RLC.weeksCount * RLC.costCourseType10;
                        break;
                    }
                break;
                case "1" :
                    if (RLC.weeksCount == 0) return; 
                    var totalCost = 0;
                    
                    switch(RLC.courseId)
                    {
                        case "0" : 
                            if ((RLC.ddlVisa.selectedIndex == 2) && (RLC.ddlAccommodation.selectedIndex == 2)) //если не нужно проживание и виза, то расчет как в I live in Moscow
                            {
                                totalCost += RLC.weeksCount * RLC.costCourseType1ILIM;
                            }
                            else
                            {
                                if (RLC.weeksCount > 11) //если кол-во недель больше 11, то одна неделя бесплатная
                                {
                                    totalCost += (RLC.weeksCount - 1) * RLC.costCourseType1;
                                }
                                else
                                {
                                    totalCost += RLC.weeksCount * RLC.costCourseType1;
                                }
                            }
                        break;
                        case "1" : totalCost += RLC.weeksCount * RLC.costCourseType2;
                        break;
                        case "2" : totalCost += RLC.weeksCount * RLC.costCourseType3;
                        break;
                        case "3" : totalCost += RLC.weeksCount * RLC.costCourseType4;
                        break;
                        case "4" : totalCost += RLC.weeksCount * RLC.costCourseType5;
                        break;
                        case "5" : totalCost += RLC.weeksCount * RLC.costCourseType6;
                        break;
                        case "6" : totalCost += RLC.weeksCount * RLC.costCourseType7;
                        break;
                    }
                    
                    RLC.iCoursesType.value = totalCost;
                    RLC.hfCostCourses.value = totalCost;
                    
                    if (RLC.ddlVisa.selectedIndex != 2)
                    {
                        var visaMonth = Math.ceil(getDateFromText(RLC.tbVisaFrom.value).getMonthsBetween(getDateFromText(RLC.tbVisaTill.value)));
                        var visaCost = 0;
                        if (visaMonth > 0)
                        {
                            switch(RLC.ddlVisa.selectedIndex)
                            {
                                case 0 : visaCost += RLC.costVisa1;
                                break;
                                case 1 : visaCost += RLC.costVisa2;
                                break;
                            }
                            visaMonth -= 1;
                            if (visaMonth > 0) visaCost += visaMonth * RLC.costVisaInc;
                        }
                        if (visaCost != 0) RLC.iVisa.value = visaCost;
                        RLC.hfCostVisa.value = visaCost;
                        totalCost += visaCost;
                    }
                    if (RLC.ddlAccommodation.selectedIndex != 2)
                    {
                        var accommodationCost = 0;
                        var accommodationDay = getDatesDiff(getDateFromText(RLC.tbAccommodationFrom.value), getDateFromText(RLC.tbAccommodationTill.value)) + 1;
                        var accommodationWeek = Math.ceil(accommodationDay / 7);
                        if ((accommodationDay % 7 <= 3) && ((accommodationDay % 7 != 0))) accommodationWeek -= 1;
                        if (accommodationDay <= 7) accommodationWeek = 1;
                        var accommodationMonth = Math.ceil(getDateFromText(RLC.tbAccommodationFrom.value).getMonthsBetween(getDateFromText(RLC.tbAccommodationTill.value)));
                        var additionalWeeks = 0;
                        if (accommodationMonth > 1)
                        {
                            additionalWeeks = Math.ceil((getDatesDiff(getDateFromText(RLC.tbAccommodationFrom.value).addMonth(1), getDateFromText(RLC.tbAccommodationTill.value)) + 1) / 7);
                        }
                        switch(RLC.ddlAccommodation.selectedIndex)
                        {
                            case 0: accommodationCost += accommodationWeek * RLC.costAccommodation1;
                            break;
                            case 1: if (accommodationMonth > 1) { accommodationCost += RLC.costAccommodation2 + additionalWeeks * 215; } else { accommodationCost += accommodationMonth * RLC.costAccommodation2; }
                            break;
                        }
                        
                        if (accommodationCost != 0) RLC.iAccommodation.value = accommodationCost;
                        RLC.hfCostAccommodation.value = accommodationCost;
                        totalCost += accommodationCost;
                    }
                    

                    RLC.tbTotal.value = totalCost + transfersCost;
                break;
                case "2" :
                    var totalCost = 0;
                    var studyWeek = parseInt(RLC.ddlWeeksCount.options[RLC.ddlWeeksCount.selectedIndex].text);
                    var studyMonth = Math.ceil(studyWeek / 4);
                    
                    switch(RLC.courseId)
                    {
                        case "0" : 
                            if ((RLC.ddlVisa.selectedIndex == 1) && (RLC.ddlAccommodation.selectedIndex == 2)) //если не нужно проживание и виза, то расчет как в I live in Moscow
                            {
                                totalCost += RLC.costCourseType1ILIM * studyWeek;
                            }
                            else
                            {
                                totalCost += RLC.costCourseType1 * studyWeek;
                            }
                        break;
                        case "5" : totalCost += RLC.costCourseType6 * studyWeek;
                        break;
                        case "6" : totalCost += RLC.costCourseType7 * studyWeek;
                        break;
                    }
                    
                    RLC.hfCostCourses.value = totalCost;
                    
                    //РАСЧЕТ ВИЗЫ
                    var visaCost = 0;
                    var visaMonth = studyMonth;
                    if (visaMonth > 0)
                    {
                        switch(RLC.ddlVisa.selectedIndex)
                        {
                            case 0 : visaCost += RLC.costVisa1;
                                     visaMonth -= 1;
                                     if (visaMonth > 0) visaCost += visaMonth * RLC.costVisaInc;
                            break;
                        }
                        
                    }
                    if (visaCost != 0) RLC.iVisa.value = visaCost;
                    RLC.hfCostVisa.value = visaCost;
                    totalCost += visaCost;
                    
                    //РАСЧЕТ ПРОЖИВАНИЯ
                    var accommodationCost = 0;
                    var accommodationWeek = studyWeek;
                    var accommodationMonth = studyMonth;
                    var additionalWeeks;
                    if (accommodationWeek > 4)
                    {
                        additionalWeeks = accommodationWeek - 4;
                    }
                    
                    switch(RLC.ddlAccommodation.selectedIndex)
                    {
                        case 0: accommodationCost += accommodationWeek * RLC.costAccommodation1;
                        break;
                        case 1: if (accommodationMonth > 1) { accommodationCost += RLC.costAccommodation2 + additionalWeeks * 215; } else { accommodationCost += accommodationMonth * RLC.costAccommodation2; }
                        break;
                    }
                    
                    if (accommodationCost != 0) RLC.iAccommodation.value = accommodationCost;
                    RLC.hfCostAccommodation.value = accommodationCost;
                    totalCost += accommodationCost;
                    
                    RLC.tbTotal.value = totalCost + transfersCost;
                break;
                case "3" : //стоимость константна + трансферы
                    if ((RLC.ddlVisa.selectedIndex != 0) || (RLC.ddlAccommodation.selectedIndex != 0))
                    {
                        var totalCost = RLC.costYearSTP + transfersCost;
                        if (RLC.ddlVisa.selectedIndex != 1)
                        {
                            totalCost += 146;
                        }
                        RLC.tbTotal.value = totalCost;
                    }
                    else
                    {
                        RLC.tbTotal.value = RLC.costYear + transfersCost;
                    }
                break;
                case "4" :
                    if ((RLC.ddlVisa.selectedIndex != 0) || (RLC.ddlAccommodation.selectedIndex != 0))
                    {
                        var totalCost = RLC.costAutumnSemesterSTP + transfersCost;
                        if (RLC.ddlVisa.selectedIndex != 1)
                        {
                            totalCost += 98;
                        }
                        RLC.tbTotal.value = totalCost;
                    }
                    else
                    {
                        RLC.tbTotal.value = RLC.costAutumnSemester + transfersCost;
                    }
                break;
                case "5" :
                    if ((RLC.ddlVisa.selectedIndex != 0) || (RLC.ddlAccommodation.selectedIndex != 0))
                    {
                        var totalCost = RLC.costSpringSemesterSTP + transfersCost;
                        if (RLC.ddlVisa.selectedIndex != 1)
                        {
                            totalCost += 98;
                        }
                        RLC.tbTotal.value = totalCost;
                    }
                    else
                    {
                        RLC.tbTotal.value = RLC.costSpringSemester + transfersCost;
                    }
                break;
                case "6" :
                case "7" :
                    var totalCost = 0;
                    var studyWeek = parseInt(RLC.ddlWeeksCount.options[RLC.ddlWeeksCount.selectedIndex].text);
                    var studyMonth = Math.ceil(studyWeek / 4);
                    
                    switch(RLC.courseId)
                    {
                        case "0" : 
                            if ((RLC.ddlVisa.selectedIndex == 2) && (RLC.ddlAccommodation.selectedIndex == 2)) //если не нужно проживание и виза, то расчет как в I live in Moscow
                            {
                                totalCost += studyWeek * RLC.costCourseType1ILIM;
                            }
                            else
                            {
                                if (studyWeek > 11) //если кол-во недель больше 11, то одна неделя бесплатная
                                {
                                    totalCost += (studyWeek - 1) * RLC.costCourseType1;
                                }
                                else
                                {
                                    totalCost += studyWeek * RLC.costCourseType1;
                                }
                            }
                        break;
                        case "1" : totalCost += studyWeek * RLC.costCourseType2;
                        break;
                        case "2" : totalCost += studyWeek * RLC.costCourseType3;
                        break;
                        case "3" : totalCost += studyWeek * RLC.costCourseType4;
                        break;
                        case "4" : totalCost += studyWeek * RLC.costCourseType5;
                        break;
                        case "5" : totalCost += studyWeek * RLC.costCourseType6;
                        break;
                        case "6" : totalCost += studyWeek * RLC.costCourseType7;
                        break;
                    }
                    
                    RLC.iCoursesType.value = totalCost;
                    RLC.hfCostCourses.value = totalCost;
                    
                    if (RLC.ddlVisa.selectedIndex != 2)
                    {
                        var visaMonth = studyMonth;
                        var visaCost = 0;
                        if (visaMonth > 0)
                        {
                            switch(RLC.ddlVisa.selectedIndex)
                            {
                                case 0 : visaCost += RLC.costVisa1;
                                break;
                                case 1 : visaCost += RLC.costVisa2;
                                break;
                            }
                            visaMonth -= 1;
                            if (visaMonth > 0) visaCost += visaMonth * RLC.costVisaInc;
                        }
                        if (visaCost != 0) RLC.iVisa.value = visaCost;
                        RLC.hfCostVisa.value = visaCost;
                        totalCost += visaCost;
                    }
                    if (RLC.ddlAccommodation.selectedIndex != 2)
                    {
                        var accommodationCost = 0;
                        var accommodationWeek = studyWeek;
                        var accommodationMonth = studyMonth;
                        var additionalWeeks;
                        if (accommodationWeek > 4)
                        {
                            additionalWeeks = accommodationWeek - 4;
                        }
                        switch(RLC.ddlAccommodation.selectedIndex)
                        {
                            case 0: accommodationCost += accommodationWeek * RLC.costAccommodation1;
                            break;
                            case 1: if (accommodationMonth > 1) { accommodationCost += RLC.costAccommodation2 + additionalWeeks * 215; } else { accommodationCost += accommodationMonth * RLC.costAccommodation2; }
                            break;
                        }
                        
                        if (accommodationCost != 0) RLC.iAccommodation.value = accommodationCost;
                        RLC.hfCostAccommodation.value = accommodationCost;
                        totalCost += accommodationCost;
                    }
                    
                    RLC.tbTotal.value = totalCost + transfersCost;
                break;
                case "8" :
                    if (RLC.weeksCount == 0) return; 

                    RLC.tbTotal.value = RLC.weeksCount * RLC.costCourseType9;
                break;
            }
            RLC.hfCostTotal.value = RLC.tbTotal.value;
        },
        tbFrom_OnChange : function()
        {
            if (RLC.programId == "1")
            {
                if (RLC.tbFrom.value != "")
                {
                    RLC.tbVisaFrom.value = getTextFromDate(getDateFromText(RLC.tbFrom.value).addDay(-RLC.visaDec))
                }
                RLC.tbAccommodationFrom.value = RLC.tbFrom.value;
            }
            if (RLC.programId != "2") RLC.ShowWeeksCount();
            RLC.CalcCost();
        },
        tbTill_OnChange : function()
        {
            if (RLC.programId == "1")
            {
                if (RLC.tbTill.value != "")
                {
                    RLC.tbVisaTill.value = getTextFromDate(getDateFromText(RLC.tbTill.value).addDay(RLC.visaInc))
                }
                RLC.tbAccommodationTill.value = RLC.tbTill.value;
            }
            if (RLC.programId != "2") RLC.ShowWeeksCount();            
            RLC.CalcCost();
        },
        //////////////////////
        //Локальные функции///
        //////////////////////
        SetDatesEnabled : function(enable)
        {
            RLC.tbFrom.disabled = !enable;
            RLC.tbTill.disabled = !enable;
        },
        SetDates : function(from, till)
        {
            RLC.tbFrom.value = from;
            RLC.tbTill.value = till;
        },
        SetVisaDates : function(from, till)
        {
            RLC.tbVisaFrom.value = from;
            RLC.tbVisaTill.value = till;
        },
        SetVisaDatesVisible : function(visible)
        {
            var vis = visible ? "" : "none";
            RLC.tbVisaFrom.style.display = vis;
            RLC.sVisaDate.style.display = vis;
            RLC.tbVisaTill.style.display = vis;
        },
        SetAccommodationDates : function(from, till)
        {
            RLC.tbAccommodationFrom.value = from;
            RLC.tbAccommodationTill.value = till;
        },
        SetAccommodationDatesVisible : function(visible)
        {
            var vis = visible ? "" : "none";
            RLC.tbAccommodationFrom.style.display = vis;
            RLC.sAccommodationDate.style.display = vis;
            RLC.tbAccommodationTill.style.display = vis;
        },
        SetVisaType : function(visaType) //выбор типа виза по значению(!)
        {
            selectOptionByValue(RLC.ddlVisa, visaType);
            //RLC.ddlVisa.options[visaType].selected = true;
            RLC.hfVisa.value = visaType;
        },
        SetAccommodationType : function(accType) //выбор типа проживания по значению(!)
        {
            selectOptionByValue(RLC.ddlAccommodation, accType);
            //RLC.ddlAccommodation.options[accType].selected = true;
            RLC.hfAccommodation.value = accType;
        },
        FillWeeksCount : function(fromDate, tillDate) //заполнение кол-ва недель для летних программ, программ в течение текущего осеннего/весеннего семестра
        {
            tillDate = getDateFromText(tillDate);
            today() > getDateFromText(fromDate) ? fromDate = today().getNextMonday() : fromDate = getDateFromText(fromDate);
            var weeksCount = Math.ceil((getDatesDiff(fromDate, tillDate) + 1) / 7);
            RLC.maxWeeksCount = weeksCount;
            RLC.ddlWeeksCount.options.length = 0;
            for (var i = 0; i < weeksCount - 2; i++)
            {
                RLC.ddlWeeksCount.options[i] = new Option(i + 3, i + 3);
            }
            
            RLC.hfWCSelectedIndex.value = 0;
            if (RLC.programId == "2") 
            {
                RLC.ddlWeeksCount.options[1].selected = true; //для летних программ по умолчанию кол-во недель 4
                RLC.hfWCSelectedIndex.value = 1;
            }
        },
        FillDatesIntervals : function()
        {
            RLC.lbDatesIntervals.options.length = 0;
            if (RLC.ddlWeeksCount.options.length == 0) return;
            var startDate;
            switch (RLC.programId)
            {
                case "2": startDate = getDateFromText(RLC.summerFrom);
                break;
                case "6": startDate = getDateFromText(RLC.thisYearFrom);
                break;
                case "7": startDate = getDateFromText(RLC.thisSpringSemesterFrom);
                break;
                default: return;
                break;
            }
            if (today() > startDate) startDate = today().getNextMonday();
            var weeksCount = parseInt(RLC.ddlWeeksCount.options[RLC.ddlWeeksCount.selectedIndex].text);
            var maxWeeksCount = RLC.maxWeeksCount;
            for(var i = 0; i < maxWeeksCount + 1 - weeksCount; i++)
            {
                if (RLC.programId != "2")
                {
                    RLC.lbDatesIntervals.options[i] = new Option(getTextFromDate(startDate.addDay(i * 7).getNextWorkDay()) + " - " + getTextFromDate(startDate.addDay((i + weeksCount) * 7 - 1).getPrevWorkDay()), i);
                }
                else // в летних программах интервал с воскресенья по субботу
                {
                    RLC.lbDatesIntervals.options[i] = new Option(getTextFromDate(startDate.addDay(i * 7).getNextWorkDay().addDay(-1)) + " - " + getTextFromDate(startDate.addDay((i + weeksCount) * 7 - 2).getPrevWorkDay().addDay(2)), i);
                }
            }
            
            //запоминаем кол-во недель!
            RLC.hwWeeksCount.value = weeksCount;
        },
        ShowWeeksCount : function()
        {
            if ((RLC.tbFrom.value != "") && (RLC.tbTill.value != "")) 
            {
                var datesDiff;
                if (RLC.courseId == "9") //если занятия по субботам, то считать без смещения дат
                {
                    datesDiff = getDatesDiff(getDateFromText(RLC.tbFrom.value), getDateFromText(RLC.tbTill.value));
                    RLC.weeksCount = Math.ceil((datesDiff) / 7);
                    if (datesDiff % 7 == 0) RLC.weeksCount++;
                }
                else
                {
                    datesDiff = getDatesDiff(getDateFromText(RLC.tbFrom.value).getNextWorkDay(), getDateFromText(RLC.tbTill.value).getPrevWorkDay());
                    RLC.weeksCount = Math.ceil((datesDiff) / 7);
                    if ((datesDiff % 7 <= 2) && (datesDiff % 7 != 0)) RLC.weeksCount -= 1;
                    if (datesDiff <= 7) RLC.weeksCount = 1;
                }
                
                if (RLC.programId == 3) RLC.weeksCount -= 2;
                RLC.sWeeksCount.innerText = RLC.weeksCount;
                RLC.sWeeksCount.textContent = RLC.weeksCount;
                RLC.hwWeeksCount.value = RLC.weeksCount;
            }
            else
            {
                RLC.sWeeksCount.innerText = "-";
                RLC.sWeeksCount.textContent = "-";
                RLC.hwWeeksCount.value = "-";
                RLC.weeksCount = 0;
            }
        },
        LoadCoursesType : function() //меняет значения контрола ddlCoursesType
        {
            RLC.ddlCoursesType.options.length = 0;
            
            switch (RLC.programId)
            {
                case "0":
                    RLC.ddlCoursesType.options[0] = new Option(RLC.CoursesType8Text, 7);
                    RLC.ddlCoursesType.options[1] = new Option(RLC.CoursesType9Text, 8);
                    RLC.ddlCoursesType.options[2] = new Option(RLC.CoursesType10Text, 9);
                    RLC.ddlCoursesType.options[3] = new Option(RLC.CoursesType3Text, 1);
                    RLC.ddlCoursesType.options[4] = new Option(RLC.CoursesType2Text, 2);
                    RLC.ddlCoursesType.options[5] = new Option(RLC.CoursesType6Text, 3);
                    RLC.ddlCoursesType.options[6] = new Option(RLC.CoursesType7Text, 4);
                    RLC.ddlCoursesType.options[7] = new Option(RLC.CoursesType4Text, 5);
                    RLC.ddlCoursesType.options[8] = new Option(RLC.CoursesType5Text, 6);
                break;
                case "2":
                    RLC.ddlCoursesType.options[0] = new Option(RLC.CoursesType1Text, 0);
                    RLC.ddlCoursesType.options[1] = new Option(RLC.CoursesType4Text, 5);
                    RLC.ddlCoursesType.options[2] = new Option(RLC.CoursesType5Text, 6);
                break;
                default:
                    RLC.ddlCoursesType.options[0] = new Option(RLC.CoursesType1Text, 0);
                    RLC.ddlCoursesType.options[1] = new Option(RLC.CoursesType3Text, 1);
                    RLC.ddlCoursesType.options[2] = new Option(RLC.CoursesType2Text, 2);
                    RLC.ddlCoursesType.options[3] = new Option(RLC.CoursesType6Text, 3);
                    RLC.ddlCoursesType.options[4] = new Option(RLC.CoursesType7Text, 4);
                    RLC.ddlCoursesType.options[5] = new Option(RLC.CoursesType4Text, 5);
                    RLC.ddlCoursesType.options[6] = new Option(RLC.CoursesType5Text, 6);
                break;
            }
            
            //всегда при загрузке выбираем первый элемент
            RLC.ddlCoursesType.options[0].selected = true;
            RLC.ddlCoursesType.onchange();
        },
        LoadVisaType : function() //меняет значение контрола ddlVisa
        {
            RLC.ddlVisa.options.length = 0;
        
            switch(RLC.programId)
            {
                case "2":
                case "3":
                case "4":
                case "5":
                    RLC.ddlVisa.options[0] = new Option(RLC.VisaType1Text, 0);
                    RLC.ddlVisa.options[1] = new Option(RLC.VisaType3Text, 2);
                break;
                default:
                    RLC.ddlVisa.options[0] = new Option(RLC.VisaType1Text, 0);
                    RLC.ddlVisa.options[1] = new Option(RLC.VisaType2Text, 1);
                    RLC.ddlVisa.options[2] = new Option(RLC.VisaType3Text, 2);
                break;
            }
            
            RLC.ddlVisa.options[0].selected = true;
            RLC.ddlVisa.onchange();
        },
        LoadAccommodationType : function() //меняет значение контрола ddlAccommodation
        {
            RLC.ddlAccommodation.options.length = 0;
            switch (RLC.programId)
            {
                case "3":
                case "4":
                case "5":
                    RLC.ddlAccommodation.options[0] = new Option(RLC.accommodationType1Text, 0);
                    RLC.ddlAccommodation.options[1] = new Option(RLC.accommodationType3Text, 2);
                break;
                default:
                    RLC.ddlAccommodation.options[0] = new Option(RLC.accommodationType1Text, 0);
                    RLC.ddlAccommodation.options[1] = new Option(RLC.accommodationType2Text, 1);
                    RLC.ddlAccommodation.options[2] = new Option(RLC.accommodationType3Text, 2);
                break;
            }
        }
    }
    return RLC;
}

////////////////////
////////даты////////
////////////////////

Date.prototype.addDay = function (days) 
{
    var d = new Date(this.getTime())
    d.setDate (d.getDate() + days)
    return d
}

Date.prototype.addMonth = function (months)
{
    var d = new Date(this);
    d.setMonth(d.getMonth() + months);
    return d;
}

Date.prototype.lastday = function() {
  var d = new Date(this.getFullYear(), this.getMonth() + 1, 0);
  return d.getDate();
};

Date.prototype.getNextWorkDay = function() //если день выходной, то возвращает понедельник
{
    var dayOfWeek = this.getDay();
    if (dayOfWeek == 0) return this.addDay(1);
    if (dayOfWeek == 6) return this.addDay(2);
    return this;
};

Date.prototype.getNextMonday = function() //возвращает дату следующего понедельника
{
    var dayOfWeek = this.getDay();
    if (dayOfWeek == 1) return this;
    if (dayOfWeek == 0) return this.addDay(1);
    return this.addDay(8 - dayOfWeek);
}

Date.prototype.getPrevWorkDay = function() //если день выходной, то возвращает пятницу
{
    var dayOfWeek = this.getDay();
    if (dayOfWeek == 0) return this.addDay(-2);
    if (dayOfWeek == 6) return this.addDay(-1);
    return this;
};

Date.prototype.getMonthsBetween = function(d) 
{
    var sDate, eDate;
    var d1 = this.getFullYear() * 12 + this.getMonth();
    var d2 = d.getFullYear() * 12 + d.getMonth();
    var sign;
    var months = 0;

    if (this == d) 
    {
        months = 0;
    }
    else if (d1 == d2) //тот же год и месяц
    {
        months = (d.getDate() - this.getDate()) / this.lastday();
    } 
    else 
    {
        if (d1 <  d2) 
        {
            sDate = this;
            eDate = d;
            sign = 1;
        } 
        else 
        {
            sDate = d;
            eDate = this;
            sign = -1;
        }

        var sAdj = sDate.lastday() - sDate.getDate();
        var eAdj = eDate.getDate();
        var adj = (sAdj + eAdj) / sDate.lastday() - 1; 
        months = Math.abs(d2 - d1) + adj;
        months = (months * sign)
    }
    return months;
}

function getDatesDiff(fromDate, tillDate)
{
    return tillDate > fromDate ? Math.ceil((tillDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24)) : null;
}

function getTextFromDate(parDate)
{
    var Day = parDate.getDate();
    var Month = parDate.getMonth() + 1;
    var Year = parDate.getFullYear();
    
    var Res = "";
    for (var i = 0; i < 2 - Day.toString().length; i++) Res += "0";
    Res += Day + ".";
    for (var i = 0; i < 2 - Month.toString().length; i++) Res += "0";
    Res += Month + ".";
    for (var i = 0; i < 4 - Year.toString().length; i++) Res += "0";
    Res += Year;
    
    return Res;
}

function getDateFromText(parString)
{
    var Month;
    if (parString.charAt(3) != "0")
    {
        Month = parseInt(parString.substring(3, 5)) - 1;
    }
    else
    {
        Month = parseInt(parString.substring(4, 5)) - 1;
    }
    return new Date(parString.substring(6, 10), Month, parString.substring(0, 2));
}

function today()
{
    return new Date();
}

//выбор option в select по значению
function selectOptionByValue(selObj, val){
    var a = selObj.options, l = a.length;
    while(l){
        if (a[--l].value == val){
            selObj.selectedIndex = l;
            l = 0;
        }
    }
}