// JAVASCRIPT for GLOBAL RES reservation system
// 10 FEB 2010


// Converts 1 into 01 and 9 into 09 and so on

function leadingZero(zahl) {
	if (zahl < 10)  { 
	   zahl = "0" + zahl;
	}  
	return zahl
}


// I turn a date into 2010-12-31

function assembleDate(Year, Month, Day) {
    var zahl
    zahl = Year;
	zahl += "-";
	zahl += leadingZero(Month+1);
    zahl += "-";
	zahl += leadingZero(Day);
	
	return zahl;
}


function createToday(f) {

	var today = new Date();
    var Month = today.getMonth();
	var Day = today.getDate();	

    // I only need to set the month and the day ... the year is automatically generated starting today.
	f.elements["month"].selectedIndex = Month;
	f.elements["day"].selectedIndex = Day - 1;
}	


function convertDates(f)  {
	

// Arrival Date

	var Day = (f.elements["day"].selectedIndex + 1);
	var Month = (f.elements["month"].selectedIndex);
	var Year = (f.elements["year"].value);
	
	var today = new Date();
	today = today.getTime()
	
	var arrDate = new Date(Year, Month, Day,23,59); // need this as I will compare it with today
	arrDate = arrDate.getTime();
	
	if (today > arrDate) {
		alert("Please choose a date from today onwards.")
	}
	else { // start to assemble the rest
	
	// Assemble Date
	    arrDate = assembleDate(Year,Month,Day)	
	
	// Departure Date ( Arrival Date + Nights)	
	
		var depDate = new Date(Year, Month, Day);
		depDate = depDate.getTime();
	
	// Add Nights
	
		var dayToAdd = (f.elements["nights"].selectedIndex +1);
		depDate += dayToAdd * 86400000; //// Day in Milliseconds : 86400000
	
		depDate = new Date(depDate);

		var Year = depDate.getFullYear();	
	    var Month = depDate.getMonth();
		var Day = depDate.getDate();	

	// Assemble date

	    depDate = assembleDate(Year,Month,Day)		
	
    // Retrieve number of adults
	
		var adults = (f.elements["adults"].selectedIndex +1);
	 
	
	// Assemble link to booking site
		var booking;
	
	
	// System does not accept a date before today!!!
		booking='https://www.xnglobalres.com/EclecticHotels/DeepLink.aspx';  // LIVE site 
		//booking='https://www.xnglobalres.com/EclecticHotelsStaging/DeepLink.aspx';  // Testing site
	    booking += "?cmd=stayCriteria";
		booking += "&hotel=" + 	f.elements["hotel"].value ;
		booking += "&arrDate=" + arrDate;
		booking += "&depDate=" + depDate;
		booking += "&numRooms=1";
		booking += "&adult1=" + adults;
		booking += "&child1=0";

		window.open(booking,'','');
		//location.href=booking;
		
	} // date choosen correctly
	
	return false;

} // end of convert dates


 function check31(day) {
     var add = 0;
     if (day > 30) add = 1;
	 return add;
  }
  
 function checkLeapYear(year) {
	 var leap = 28; // assume Feb has 28 days unless its a leap year
         switch(year) {
		  // below are leap years - 29 days in February
		  case "2012": 
		    leap = 29;
			break;
		  case "2016": 
		    leap = 29;
			break;
		  case "2020": 
		    leap = 29;
			break;		
		 }
     return leap;
  }
  


 function checkDates(f){
	 var addDay=0;
	 var day =  f.elements["day"].selectedIndex + 1;
 	 var month = f.elements["month"].selectedIndex + 1;
 	 var year = f.elements["year"].value;
	 
	 //alert("Day: " + day + " Month: " + month + " Year " + year);
	 
	 
	 switch(month) {
		 case 2: 
			//alert("Feb");	
            // every Feb has 28 days... so I do nothing
			// if a Feb has 29 days, it could be a leap year and I add 3 DAYS
			if (day > 28 ) addDay = day - checkLeapYear(year) ; // check for leapyear 
			//alert(addDay)
			break;
		 case 4: 
		    //alert("April");
			addDay = check31(day);
			break;
		 case 6:
		   // alert("June");
			addDay = check31(day);			
			break;
		 case 9:
		    //alert("Sept");
			addDay = check31(day);			
			break;
		 case 11:
		    // alert("Nov")
			addDay = check31(day);
			break;
	 }
	 
 	 
		 
// I have my days I have to add to the next month, which I only do if there is a problem

 if(addDay>0) {
	  //(alert(addDay));
	  f.elements["day"].selectedIndex = addDay -1; // -1 is translating the days back to the index starting at 0
      f.elements["month"].selectedIndex = month;		
 }
	
} 

