// JavaScript Document
function Init()
 {
   CreateDateCombo('checkinmonth','checkinday','checkinyear') // initial and create date drop down list from empty list.
   CreateDateCombo('checkoutmonth','checkoutday','checkoutyear') // initial and create date drop down list from empty list.
   SetCurrentDateToCombobox();
 }
function goahead()
{	
			document.aForm.submit();
}

function days_between(strdate1,strdate2)
 {
    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24
    
    var arrOfDate1 = strdate1.split("-")
    // change from DD-MM-YY to input format -> YYYY-MM-DD
    // Note: MM -> the based index is 0 that the valid value is 0 through 11
    var date1 = new Date(arrOfDate1[2],arrOfDate1[1]-1,arrOfDate1[0])
    //var date1 = new Date(arrOfDate1[2],arrOfDate1[1],"29")

    // change from DD-MM-YY to input format -> YYYY-MM-DD
    // Note: MM -> the based index is 0 that the valid value is 0 through 11
    var arrOfDate2 = strdate2.split("-")
    var date2 = new Date(arrOfDate2[2],arrOfDate2[1]-1,arrOfDate2[0])


    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()

    // Calculate the difference in milliseconds
    //var difference_ms = Math.abs(Math.ceil(date2_ms) - Math.ceil(date1_ms)) // check from checkout date - check in date
    var difference_ms = date2_ms - date1_ms; // check from checkout date - check in date
	return Math.ceil(difference_ms/ONE_DAY); // round value
    /*
    if (date2_ms >= date1_ms)
    {
       // Convert back to days and return
       return Math.ceil(difference_ms/ONE_DAY)
    } else
    {
       return "-" + Math.ceil(difference_ms/ONE_DAY); // show negative number;
    }
    */
 }

 function CreateDateCombo(strObjMonth,strObjDate,strObjYear)
 {
    var objSelect  = document.getElementById(strObjDate);
    var objMonthSelect = document.getElementById(strObjMonth);
    var objYearSelect = document.getElementById(strObjYear);

    var intDay = 0;
    var intMonth = parseInt(objMonthSelect.options[objMonthSelect.selectedIndex].value,10);
    var year = objYearSelect.value
    var intDayIdx = 0;
    var bIsFirstTime = true;
    var intDate = 0;
    if (objSelect.length > 0)
    { // there are any values in the drop down list.
       intDayIdx = objSelect.selectedIndex
       // remove all items
       do
       {
          objSelect.remove(0);
       }
       while (objSelect.length > 0);
       bIsFirstTime = false
    }
    for(intDay=1;intDay<=28;intDay++)
    {
		objSelect.options[objSelect.options.length] = new Option(intDay, intDay);
		/* var objOption = document.createElement("option");
         objSelect.options.add(objOption);
         objOption.innerText = intDay;
         objOption.value = intDay;
         delete objOption;	*/
         //alert("for day " + intDay);
    }
    var intAddedDay = 0
    //alert("index " + objMonthSelect.selectedIndex)
    //alert("value " + objMonthSelect.options[objMonthSelect.selectedIndex].value)
    //alert(intMonth)
    if (intMonth == 2)
    {
       //var currentTime = new Date()
       //var year = currentTime.getFullYear() //The four digit year (1970-9999)
       // check the year that has 28 or 29 days
       // year MOD 4
       if ((year % 4) == 0)
       { // there are 29 days.
             //intDay++;
			 objSelect.options[objSelect.options.length] = new Option(intDay, intDay);
			 /*var objOption = document.createElement("option");
             objSelect.options.add(objOption);
             objOption.innerText = intDay;
             objOption.value = intDay;
             delete objOption;*/
       }
    } else if( (intMonth == 4) || (intMonth == 6) || (intMonth == 9) ||  (intMonth == 11) )
             { //There are 30 days.

                for(var j=0;j<2;j++)
                {
					objSelect.options[objSelect.options.length] = new Option(intDay, intDay);
                   /*var objOption = document.createElement("option");
                   objSelect.options.add(objOption);
                   objOption.innerText = intDay;
                   objOption.value = intDay;*/
                   intDay++;
                   //delete objOption;
                }
             } else
             { //There are 31 days.
                for(var j=0;j<3;j++)
                {
					objSelect.options[objSelect.options.length] = new Option(intDay, intDay);
                   /*var objOption = document.createElement("option");
                   objSelect.options.add(objOption);
                   objOption.innerText = intDay;
                   objOption.value = intDay;*/
                   intDay++;
                   //delete objOption;
                }
             } // end of if statement.

      if (bIsFirstTime == false)
      {
             // set Index
             intDate = intDayIdx + 1
             // check whether it's the month number that is 1,3,5,7,8,10,12
             if ( ((intMonth == 1) || (intMonth == 3) || (intMonth == 5) ||(intMonth == 7) || (intMonth == 8) || (intMonth == 10) || (intMonth == 12)) && (intDate <= 31)  )
             {
                  objSelect.selectedIndex = intDayIdx
             } else if ( ((intMonth == 4) || (intMonth == 6) || (intMonth == 9) ||(intMonth == 11)) && (intDate <= 30)  )
                    { // check whether it's the month number that is 4,6,9,11
                       objSelect.selectedIndex = intDayIdx
                    } else if (intMonth == 2)
                      {  // check whether it's the month number that is 2
                         objSelect.selectedIndex = intDayIdx
                         if ((year % 4) == 0)
                         { // there are 29 days in febuary month.
                            if (intDate <= 29) objSelect.selectedIndex = intDayIdx
                         }else if (intDate <= 28)
                         {  // there are 28 days in febuary month.
                            objSelect.selectedIndex = intDayIdx
                         } else
                         {
                            objSelect.selectedIndex = 0
                         }
                      } else
                      {
                         objSelect.selectedIndex = 0
                      }

      } // end if statament

    delete objMonthSelect;
    delete objSelect;
    delete objYearSelect;
 }
 function SetCurrentDateToCombobox()
 {
    var currentTime = new Date()
    var month = currentTime.getMonth() + 1 //Number of month (0-11)
    var day = currentTime.getDate()  //Day of the month (0-31)
    var year = currentTime.getFullYear() //The four digit year (1970-9999)
    //document.write(month + "/" + day + "/" + year)
    //alert(currentTime.getMonth())
    // set to check out combobox
	
	if ( (month == 1) || (month == 3) || (month == 5) ||(month == 7) || (month == 8) || (month == 10) || (month == 12) )
	{
		if ((document.getElementById("checkinday").selectedIndex = day)==31)
		{
			if (month == 12)
			{
      			document.getElementById("checkoutday").selectedIndex = 0;
    			document.getElementById("checkoutmonth").selectedIndex = month-12;
				document.getElementById("checkoutyear").selectedIndex = 1;
			}
			else
			{
				document.getElementById("checkoutday").selectedIndex = 0;
    			document.getElementById("checkoutmonth").selectedIndex = month;
				
			}
		}
		else
		{
      		document.getElementById("checkoutday").selectedIndex = day;
    		document.getElementById("checkoutmonth").selectedIndex = month-1;
		}
		    
		document.getElementById("checkinday").selectedIndex = day-1;
		document.getElementById("checkinmonth").selectedIndex = month -1;
    } 
	else if ( (month == 4) || (month == 6) || (month == 9) || (month == 11) )
    { 
		if ((document.getElementById("checkinday").selectedIndex = day)==30)
		{
      		document.getElementById("checkoutday").selectedIndex = 0;
    		document.getElementById("checkoutmonth").selectedIndex = month;
		}
		else
		{
      		document.getElementById("checkoutday").selectedIndex = day;
    		document.getElementById("checkoutmonth").selectedIndex = month-1;
		}
		    
		document.getElementById("checkinday").selectedIndex = day-1;
		document.getElementById("checkinmonth").selectedIndex = month -1;
		
    }
	else if (month == 2)
	{
		if (((document.getElementById("checkinday").selectedIndex = day)==28)||((document.getElementById("checkinday").selectedIndex = day)==29))
		{
      		document.getElementById("checkoutday").selectedIndex = 0;
    		document.getElementById("checkoutmonth").selectedIndex = month;
		}
		else
		{
      		document.getElementById("checkoutday").selectedIndex = day;
    		document.getElementById("checkoutmonth").selectedIndex = month-1;
		}
		    
		document.getElementById("checkinday").selectedIndex = day-1;
		document.getElementById("checkinmonth").selectedIndex = month -1;
	}

    // set to arrival date combobox
    document.getElementById("aday").selectedIndex = day - 1 ;
    document.getElementById("amonth").selectedIndex = month - 1;

    // set to departure date combobox
    document.getElementById("dday").selectedIndex = day;
    document.getElementById("dmonth").selectedIndex = month - 1;

	CheckNumberOfNight();
 }
 function SetCheckOutMonthFollowToCheckInMonth()
 {
    document.getElementById("checkoutmonth").selectedIndex = document.getElementById("checkinmonth").selectedIndex
 }

 /*function CheckNumberOfNight()
 {
 	// check in date
	var checkinday = document.getElementById("checkinday").options[document.getElementById("checkinday").selectedIndex].value;
    var checkinmonth = document.getElementById("checkinmonth").options[document.getElementById("checkinmonth").selectedIndex].value;
    var checkinyear = document.getElementById("checkinyear").options[document.getElementById("checkinyear").selectedIndex].value;
    strdate1 = checkinday + "-" + checkinmonth + "-" + checkinyear;
    
    // check out date
    var checkoutday = document.getElementById("checkinday").options[document.getElementById("checkoutday").selectedIndex].value;
    var checkoutmonth = document.getElementById("checkinmonth").options[document.getElementById("checkoutmonth").selectedIndex].value;
    var checkoutyear = document.getElementById("checkinyear").options[document.getElementById("checkoutyear").selectedIndex].value;
    strdate2 = checkoutday + "-" + checkoutmonth + "-" + checkoutyear;
	var diffdt = days_between(strdate1,strdate2);
	//document.getElementById("txtnumday").value = diffdt;
    //alert (diffdt)
	$("#txtnumday").val(diffdt);
	var chknight = 1;//document.getElementById("txtnumday").value
	var month = parseInt(document.getElementById("checkinmonth").options[document.getElementById("checkinmonth").selectedIndex].value);
	
	if (chknight <= 0)
	{
		//alert (month);
		if ( (month == 1) || (month == 3) || (month == 5) ||(month == 7) || (month == 8) || (month == 10) || (month == 12) )
		{
			if ((document.getElementById("checkinday").options[document.getElementById("checkinday").selectedIndex].value)==31)
			{
					if (month == 12)
					{
						document.getElementById("checkoutday").selectedIndex = 0;
						document.getElementById("checkoutmonth").selectedIndex = month-12;
						document.getElementById("checkoutyear").selectedIndex = 1;
					}
					else
					{
						document.getElementById("checkoutday").selectedIndex = 0;
						document.getElementById("checkoutmonth").selectedIndex = month;
					}
			}
			else
			{
				document.getElementById("checkoutday").selectedIndex = parseInt(document.getElementById("checkinday").options[document.getElementById("checkinday").selectedIndex].value);
				document.getElementById("checkoutmonth").selectedIndex = month-1;
			}
		} 
		else if ( (month == 4) || (month == 6) || (month == 9) || (month == 11) )
		{ 
			if ((document.getElementById("checkinday").options[document.getElementById("checkinday").selectedIndex].value)==30)
			{
				document.getElementById("checkoutday").selectedIndex = 0;
				document.getElementById("checkoutmonth").selectedIndex = month;
			}
			else
			{
				document.getElementById("checkoutday").selectedIndex = parseInt(document.getElementById("checkinday").options[document.getElementById("checkinday").selectedIndex].value);
				document.getElementById("checkoutmonth").selectedIndex = month-1;
			}
		}
		else if (month == 2)
		{
			if (((document.getElementById("checkinday").options[document.getElementById("checkinday").selectedIndex].value)==28)||((document.getElementById("checkinday").options[document.getElementById("checkinday").selectedIndex].value)==29))
			{
				document.getElementById("checkoutday").selectedIndex = 0;
				document.getElementById("checkoutmonth").selectedIndex = month;
			}
			else
			{
				document.getElementById("checkoutday").selectedIndex = parseInt(document.getElementById("checkinday").options[document.getElementById("checkinday").selectedIndex].value);
				document.getElementById("checkoutmonth").selectedIndex = month-1;
			}
		}
		CheckNumberOfNight();
	}
 }
*/

    function Check_Key()
    {
       // check for press enter.
        if (event.keyCode == 13 || event.keyCode == 0)
        {
           CheckIt();
        }
    }
    function Check_Num()
    { // Allow only type number
        // event.keyCode = 46 (".")
            if (((event.keyCode < 48) || (event.keyCode > 57))&&(event.keyCode != 13) && (event.keyCode != 46)){
                    event.returnValue=false;
            }
    }
