﻿/*Setdate*///////////////////////////////////////////////////////////////
function AddItems(dropList, Text, Value) {
    var myOption;
    newOption = document.createElement("Option");
    newOption.text = Text;
    newOption.Value = Value;
    newOption.value = Value;
    dropList.options.add(newOption);
}
//-----------------------------------------------------------------
function daysInMonth(month, year) {
    month++;
    var dd = new Date(year, month, 0);
    return dd.getDate();

}
//-----------------------------------------------------------------
function SetNightNo(e) {
    var Checkin_month = drpCheckinMounth.options[drpCheckinMounth.selectedIndex].value;
    var Checkin_day = drpCheckinDay.options[drpCheckinDay.selectedIndex].value;
    var Checkin_Yeartt = drpCheckinMounth.options[drpCheckinMounth.selectedIndex].text;
    var Checkin_Year = Checkin_Yeartt.substring(Checkin_Yeartt.indexOf(" ") + 1);

    var Checkout_month = drpCheckoutMounth.options[drpCheckoutMounth.selectedIndex].value;
    var Checkout_day = drpCheckoutDay.options[drpCheckoutDay.selectedIndex].value;
    var Checkout_Yeartt = drpCheckoutMounth.options[drpCheckoutMounth.selectedIndex].text;
    var Checkout_Year = Checkout_Yeartt.substring(Checkout_Yeartt.indexOf(" ") + 1);

    var CheckinDate = new Date(Checkin_Year, Checkin_month, Checkin_day);
    var CheckoutDate = new Date(Checkout_Year, Checkout_month, Checkout_day);


    var CheckinDate1 = Date.parse(CheckinDate);
    var CheckoutDate1 = Date.parse(CheckoutDate);
    var datedif = (CheckoutDate1 - CheckinDate1) / (24 * 60 * 60 * 1000);



    if (datedif <= 0) {//end CheckoutDate is smaller than CheckinDate
        alert('تاریخ خروج از هتل نمی‌تماند قبل از تاریخ ورود به هتل باشد')
        //DDLNight[0].selected = true;
        FillAllDate();
    }
    else if (datedif <= 15) {
        DDLNight[datedif - 1].selected = true;
        SaveInTextBox(Checkin_Year, Checkin_month, Checkin_day, datedif);
    }
    else {
        alert('برای اقامت بیشتر از 15 شب لطفا با ما تماس بگیرید')
        DDLNight[14].selected = true;
        FillAllDate();
    }

}
//-----------------------------------------------------------------
function FillMonth(drp, SelectDateChekin, NumberOfNights) {
    var m_names = new Array("January", "February", "March",
                "April", "May", "June", "July", "August", "September",
                "October", "November", "December");

    //find CheckIn Date
    var Chekin_month = SelectDateChekin.getMonth();
    var Chekin_day = SelectDateChekin.getDate();
    var Chekin_Year = SelectDateChekin.getFullYear();
    //find Selected Date (baraye chekinDDL NumberOfNights=0 ast)
    var Select_date = new Date(Chekin_Year, Chekin_month, Chekin_day);
    Select_date.setDate(Select_date.getDate() + NumberOfNights);

    //var Start_date = new Date();
    var Start_date = new Date(CurrentDate_Year, CurrentDate_Month, 1);
    var startMonth = Start_date.getMonth();
    var Select_index;


    drp.options.length = 0;
    for (i = 1; i < 13; i++) {

        var curr_month = Start_date.getMonth();
        var Text = m_names[curr_month] + '  ' + Start_date.getFullYear();
        var Value = curr_month;


        AddItems(drp, Text, Value);

        if (curr_month == Select_date.getMonth()) { Select_index = i - 1; } //find selected index in dropdown


        var t = startMonth + i;
        if (t > 12) { t = t - 12; }
       // alert(Select_index + "," + i + "," + t + "," + curr_month + "," + Select_date.getMonth())
        Start_date.setMonth(t); //dafeh aval set nemishe

        //Start_date.setMonth(t); //dafeh dovom set mishe
    }
    drp[Select_index].selected = true;
}

//-----------------------------------------------------------------
function FillDay(drp, SelectDateChekin, NumberOfNights) {
    var First_day;
    var End_days;
    var Year;
    var Month;
    var SelectDay;

    //var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    var dayNames = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

    //find Selected Date
    var Chekin_month = SelectDateChekin.getMonth();
    var Chekin_day = SelectDateChekin.getDate();
    var Chekin_Year = SelectDateChekin.getFullYear();
    var Chekin_date = new Date(Chekin_Year, Chekin_month, Chekin_day);
    var ChekOut_date = new Date(Chekin_Year, Chekin_month, Chekin_day);
    //age darim drop downl avalo por mikonim
    if (NumberOfNights == 0) {
        End_days = daysInMonth(Chekin_month, Chekin_Year);
        Year = Chekin_Year;
        Month = Chekin_month;
        SelectDay = Chekin_day;
        //age chekinmonth ba mahe current yeki nabashe rooz az 1 bashe
        //if (Chekin_month != new Date().getMonth())
        if (Chekin_month != new Date(CurrentDate_Year, CurrentDate_Month, CurrentDate_Day).getMonth())
        { First_day = 1 }
        else
        { First_day = new Date(CurrentDate_Year, CurrentDate_Month, CurrentDate_Day).getDate(); }
        //{ First_day = new Date().getDate(); }
    }
    else//drpchekout ast
    {
        ChekOut_date.setDate(Chekin_date.getDate() + NumberOfNights);
        //bayad tarikhe select shodeh ro rozaye mahesho biareh
        var ChekOut_month = ChekOut_date.getMonth();
        var ChekOut_Year = ChekOut_date.getFullYear();
        Year = ChekOut_Year;
        Month = ChekOut_month;
        SelectDay = ChekOut_date.getDate();
        End_days = daysInMonth(ChekOut_month, ChekOut_Year);
        //age mahe checkin ba mahe checkout yeki ast 
        //az roze (checkin be bad ro neshon bede vagarna az avale mah
        if (ChekOut_month != Chekin_month)
        { First_day = 1 }
        else
        { First_day = Chekin_day + 1; }
    }

    if (SelectDay < First_day)//age cookie ye rozi ro set kardeh bashe ke gozashteh
    { SelectDay = First_day } //First_day ro select koneh

    //alert(First_day + ',' + End_days + ',' + SelectDay + ',')
    var Select_index;
    drp.options.length = 0;
    var j = 0;
    for (i = First_day; i <= End_days; i++) {
        var now = new Date(Year, Month, i);
        var Text = dayNames[now.getDay()] + '  ' + i;
        var Value = i;
        AddItems(drp, Text, Value);
        if (i == SelectDay) { Select_index = j; } //find selected index in dropdown
        j++;
    }
    drp[Select_index].selected = true;
}
//-----------------------------------------------------------------
function FillAllDate(Command) {
    var curr_month = drpCheckinMounth.options[drpCheckinMounth.selectedIndex].value;

    var tt = drpCheckinMounth.options[drpCheckinMounth.selectedIndex].text;
    var curr_Year = tt.substring(tt.indexOf(" ") + 1);

    if (Command == 'FillCheckInDay') {
        //age valin mah entekhab shode bayad avalin rooz to dropdown emrooz bashe
        var CurrentDate;
        if (drpCheckinMounth.selectedIndex == 0) {
            CurrentDate = new Date(curr_Year, curr_month, new Date(CurrentDate_Year, CurrentDate_Month, CurrentDate_Day).getDate());
        }
        else {
            CurrentDate = new Date(curr_Year, curr_month, 1);
        }
        FillDay(drpCheckinDay, CurrentDate, 0)
    }
    //FillMonth(drpCheckinMounth, CurrentDate,0)


    var curr_day = drpCheckinDay.options[drpCheckinDay.selectedIndex].value;
    var StartDate = new Date(curr_Year, curr_month, curr_day);

    NumberOfNights = DDLNight.selectedIndex + 1;

    FillMonth(drpCheckoutMounth, StartDate, NumberOfNights);

    FillDay(drpCheckoutDay, StartDate, NumberOfNights);

    SaveInTextBox(curr_Year, curr_month, curr_day, NumberOfNights);

}
//-----------------------------------------------------------------
function FillAllDateOnStartUp(Select_Year, Select_month, Select_day, NumberOfNights) {



    var SelectDate;
    //    if (Select_Year == undefined) {
    if (NumberOfNights == undefined) {
            //SelectDate = new Date();
            SelectDate = new Date(CurrentDate_Year, CurrentDate_Month, CurrentDate_Day);
            SelectDate.setDate(SelectDate.getDate() + 2);
            NumberOfNights = 1;
        }
        else {
            SelectDate = new Date(Select_Year, Select_month, Select_day);
            
        }


    

    FillDay(drpCheckinDay, SelectDate, 0)
    FillMonth(drpCheckinMounth, SelectDate, 0)
    FillMonth(drpCheckoutMounth, SelectDate, NumberOfNights);
    FillDay(drpCheckoutDay, SelectDate, NumberOfNights);
    SaveInTextBox(SelectDate.getFullYear(), SelectDate.getMonth(), SelectDate.getDate(), NumberOfNights);
    //TODO:Farhad-Mehdi: Farhad jan. man ino khodam ezafe kardam. bebin doroste?
    DDLNight.value = NumberOfNights;


    //inital First Hotel room to be double
    //if ((Select_Year == '' && Select_month == '') || (Select_Year == undefined && Select_month == undefined)) {
    if (NumberOfNights == undefined) {
        //alert(Select_Year)
        //alert(Select_month)

        SetRoomTypeDDLSelected(2, DDLRomeType1)
        SetRoomTypeDDLSelected(2, DDLRomeType2)
        SetRoomTypeDDLSelected(2, DDLRomeType3)
        SetRoomTypeDDLSelected(2, DDLRomeType4)
        SetRoomTypeDDLSelected(2, DDLRomeType5)
    }
}



/*EndSetdate*///////////////////////////////////////////////////////////////




















/*Public*///////////////////////////////////////////////////////////////

function SaveInTextBox(curr_Year, curr_month, curr_day, NumberOfNights) {
    var CheckOutDate = new Date(curr_Year, curr_month, curr_day);
    CheckOutDate.setDate(CheckOutDate.getDate() + NumberOfNights);


    if (curr_day.toString().length == 1) { curr_day = "0" + curr_day; }
    curr_month = parseInt(curr_month) + 1;
    if (curr_month.toString().length == 1) { curr_month = "0" + curr_month; }

    HFCheckIn.value = curr_Year + "/" + curr_month + "/" + curr_day;


    curr_day = CheckOutDate.getDate();
    if (curr_day.toString().length == 1) { curr_day = "0" + curr_day; }
    curr_month = parseInt(CheckOutDate.getMonth()) + 1;
    if (curr_month.toString().length == 1) { curr_month = "0" + curr_month; }

    HFCheckOut.value = CheckOutDate.getFullYear() + "/" + curr_month + "/" + curr_day;
}
//-----------------------------------------------------------------
function SetRoomTypeDDLSelected(Value, drp, number) {

    if (Value == 1) { drp[0].selected = true; }
    if (Value == 2) { drp[1].selected = true; }
    if (Value == 6) { drp[2].selected = true; }
    if (Value == 3) { drp[3].selected = true; }
    if (Value == 4) { drp[4].selected = true; }


}
//-----------------------------------------------------------------
function Set_ExtraBed_Child_Visibility(number, val) {

    ex1 = 'ctl00_ContentPlaceHolder_ForControls_SearchHotelControl1_chExtraBed' + number;
    child = 'ctl00_ContentPlaceHolder_ForControls_SearchHotelControl1_DDLChildrenCount' + number;
    //Otage 1 nafareh extrabed nadare
    if (val == 1) {
        $get(ex1).parentNode.disabled = true;
        $get(ex1).disabled = true;
    }
    else {
        $get(ex1).parentNode.disabled = false;
        $get(ex1).disabled = false;

    }

    //Otaghe Single bache nadare
    if (val == 1) {
        $get(child).parentNode.disabled = true;
        $get(child).disabled = true;
    }
    else {
        $get(child).parentNode.disabled = false;
        $get(child).disabled = false;
    }
}
/*EndPublic*///////////////////////////////////////////////////////////////
















//-----------------------------------------------------------------
function RoomNumberChanged() {
    drpRoomNumber = $get('ctl00$ContentPlaceHolder_ForControls$SearchHotelControl1$DDLRoomNo');
    drpRoomNumber = $get('ctl00_ContentPlaceHolder_ForControls_SearchHotelControl1_DDLRoomNo');
    val = drpRoomNumber.options[drpRoomNumber.selectedIndex].value;
    switch (val) {
        case '1':
            Utility.show1('PanelRooms1');
            Utility.hide1('PanelRooms2');
            Utility.hide1('PanelRooms3');
            Utility.hide1('PanelRooms4');
            Utility.hide1('PanelRooms5');
            break;
        case '2':
            Utility.show1('PanelRooms1');
            Utility.show1('PanelRooms2');
            Utility.hide1('PanelRooms3');
            Utility.hide1('PanelRooms4');
            Utility.hide1('PanelRooms5');
            break;
        case '3':
            Utility.show1('PanelRooms1');
            Utility.show1('PanelRooms2');
            Utility.show1('PanelRooms3');
            Utility.hide1('PanelRooms4');
            Utility.hide1('PanelRooms5');
            break;
        case '4':
            Utility.show1('PanelRooms1');
            Utility.show1('PanelRooms2');
            Utility.show1('PanelRooms3');
            Utility.show1('PanelRooms4');
            Utility.hide1('PanelRooms5');
            break;
        case '5':
            Utility.show1('PanelRooms1');
            Utility.show1('PanelRooms2');
            Utility.show1('PanelRooms3');
            Utility.show1('PanelRooms4');
            Utility.show1('PanelRooms5');
            break;


    }
    //vase inke bar asase serch type hotel ya apartment help ro show hide koneh
    PrepareSearchType()
}
//-----------------------------------------------------------------
function NumberofChildrenChanged(drpID, drpChild1ID, drpChild2ID, lblChild1ID, lblChild2ID) {
    drpChildNumber = $get(drpID)
    val = drpChildNumber.options[drpChildNumber.selectedIndex].value;

    switch (val) {
        case '0':
            Utility.hide3(drpChild1ID);
            Utility.hide3(lblChild1ID);
            Utility.hide3(drpChild2ID);
            Utility.hide3(lblChild2ID);
            break;
        case '1':
            Utility.show3(drpChild1ID);
            Utility.show3(lblChild1ID);
            Utility.hide3(drpChild2ID);
            Utility.hide3(lblChild2ID);
            break;
        case '2':
            Utility.show3(drpChild1ID);
            Utility.show3(lblChild1ID);
            Utility.show3(drpChild2ID);
            Utility.show3(lblChild2ID);
            break;
    }

}
//-----------------------------------------------------------------
function PrepareSearchType() {
    return;
        Utility.show1('ctl00_ContentPlaceHolder_ForControls_SearchHotelControl1_lnkClose1');

        //alert(Utility.IsHidden('PanelRooms2'))
        if (Utility.IsHidden('PanelRooms2') == false)
            Utility.show1('ctl00_ContentPlaceHolder_ForControls_SearchHotelControl1_lnkClose2');
        else
            Utility.hide1('ctl00_ContentPlaceHolder_ForControls_SearchHotelControl1_lnkClose2');

        if (Utility.IsHidden('PanelRooms3') == false)
            Utility.show1('ctl00_ContentPlaceHolder_ForControls_SearchHotelControl1_lnkClose3');
        else
            Utility.hide1('ctl00_ContentPlaceHolder_ForControls_SearchHotelControl1_lnkClose3');

        if (Utility.IsHidden('PanelRooms4') == false)
            Utility.show1('ctl00_ContentPlaceHolder_ForControls_SearchHotelControl1_lnkClose4');
        else
            Utility.hide1('ctl00_ContentPlaceHolder_ForControls_SearchHotelControl1_lnkClose4');

        if (Utility.IsHidden('PanelRooms5') == false)
            Utility.show1('ctl00_ContentPlaceHolder_ForControls_SearchHotelControl1_lnkClose5');
        else
            Utility.hide1('ctl00_ContentPlaceHolder_ForControls_SearchHotelControl1_lnkClose5');

}


//-------------------------------------------------------------------------
function CitySelect() {
    Utility.hide(lblErrorMessageID);
    Utility.hide(panelHelpID);
    DDCityID = DDLcity.options[DDLcity.selectedIndex].value;
    HFCity.value = DDCityID;
}

var xmlhttpGetCity
function FillCityDate() {

    DDCountry = $get('ctl00_ContentPlaceHolder_ForControls_SearchHotelControl1_DDLcountry');
    var CountryID = DDCountry.options[DDCountry.selectedIndex].value;
    if (DDCountry.selectedIndex == 0) {
        DDLcity.options.length = 0;
        AddItems(DDLcity, 'ابتدا کشور مقصد را انتخاب نمایید', 0);
        return;
    }

     Utility.hide(lblErrorMessageID);
     Utility.hide(panelHelpID);
     DDLcity.disabled = true;
     AddItems(DDLcity, 'در حال بارگذاری شهر‌ها', 0);
     DDLcity.options.length = 0;
       
    xmlhttpGetCity = GetXmlHttpObject();
    if (xmlhttpGetCity == null) {
        alert("Your browser does not support XMLHTTP!");
        return;
    }
    var url = ResolveUrl + "AA/GetCity.aspx";
    url = url + "?sid=" + Math.random();
    url = url + "&CountryCode=" + CountryID;
    xmlhttpGetCity.onreadystatechange = GetCityChanged;
    xmlhttpGetCity.open("GET", url, true);
    xmlhttpGetCity.send(null);


}




function GetCityChanged() {
    DDCity = DDLcity;
    //alert(xmlhttp1.readyState);
     
    if (xmlhttpGetCity.readyState == 4) {
        var res = xmlhttpGetCity.responseText;
        //alert(res);
        DDCity.options.length = 0;

        if (res != 'Error') {
            var temp1 = new Array();
            temp1 = res.split(';');
            for (i = 0; i < temp1.length; i++) {
                var temp2 = new Array();
                temp2 = temp1[i].split(',');
                
                AddItems(DDCity, temp2[1], temp2[0]);
                }

            DDCity.disabled = false; 
        } else {
             AddItems(DDCity, "خطا", -1);
//            var innerHTML = "<p>در هنگام ارسال مشکلی به وجو آمد. لطفا آدرس ایمیل را بررسی کنید ومجددا سعی کنید<br /><a href='javascript:ShowSendPanel();'><br />تلاش دوباره</a></p>";
//            obj1.innerHTML = innerHTML;
//            $get('ImgWaitInvitation').src = 'Images/Failed.gif';
        }
    }
}