// JavaScript Document
function callIRM(){
    //var url = "reservations.html?";  - Lou
    var url = "https://irm.ochotels.com/irmnet/Res/ResMain.aspx?PropertyCode=01&";

    var arrival = document.form1.wdcArrival_input.value;
    var departure = document.form1.wdcDeparture_input.value;
	var nights = document.form1.wcNights_input.value;
	var adults = document.form1.ddAdults.value;
	var child = document.form1.ddChildren.value;
	
	
	
	if (arrival != "Select Date"){
		if (nights <= 0 ){
       alert("Departure date must be later than arrival date.\nPlease change your calendar dates and try again.")
       return false;
       }
    //additional checking
		if (nights > 10){  // for example enforce a maximum stay
		   alert("Online Reservations cannot be made for more than a 10 night stay.\nPlease change your calendar dates, or call us about your extended stay.\nThank You");
		   return false;
		   }
		url = url + "Arrival=" + arrival;
		if (!departure) {
			url = url + "&Nights=" + nights;
			}
		else{
			url = url + "&Departure=" + departure;
			}
		}
	else {
		arrival = "03/01/1998";
		departure = "03/03/1998";
		url = url + "Arrival=" + arrival;
		url = url + "&Departure=" + departure;		
		} //if arrival does equal select date then set the dates to default arrival and departure dates.
	url = url + "&People1=" + adults;
	url = url + "&People3=" + child;
	window.location.assign(url);	
}//end callIRM()

function callIRM_All(){
	var url = "reservations.html?&People1=0&People3=0";
	window.location.assign(url);
}//end callIRM_All()

//////////////////////////////////////////
// Supporting date variables and functions
//////////////////////////////////////////

// Array list of days. //only needed if day of week is desired for display
//var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

// Array list of months.
var months = new Array('01','02','03','04','05','06','07','08','09','10','11','12');

// Calculate four digit year.
function fourdigits(number)	{
	return (number < 1000) ? number + 1900 : number;
	}
	

//Get actual values for date and month from DOM, For security purposes of demo year is not set to current//
var nowdate = 01;   
var nowmonth = 2;  // 2 = March see months array above//
var nowyear = 1998; 

//For use with demo -will set the default departure date to x number of days ahead of arrival - remove or comment out for current date functionality;
var arr = 0;
var dep = 2;

var arrivalDate = new Date(nowyear, nowmonth, nowdate);
arrivalDate.setDate(arrivalDate.getDate() + arr);
var departureDate = new Date(nowyear, nowmonth, nowdate);
departureDate.setDate(departureDate.getDate() + dep);

var arrivalInit = (months[arrivalDate.getMonth()]) + "/" +
								   ((arrivalDate.getDate()<10) ? "0" : "")+ arrivalDate.getDate() + "/" +    
								   fourdigits(arrivalDate.getFullYear());
								   
var departureInit = (months[departureDate.getMonth()]) + "/" +
								   ((departureDate.getDate()<10) ? "0" : "")+ departureDate.getDate() + "/" +    
								   fourdigits(departureDate.getFullYear());
								   
var numberNights = Math.round(departureDate - arrivalDate)/86400000;



function calcDates2() {
    var nights = parseInt(document.form1.wcNights_input.value);
    var departs = document.form1.wdcDeparture_input.value;
    var arrivals = document.form1.wdcArrival_input.value;

    if ((departs != "Select Date") && (departs != "")) {
        departs = departs.split("/");
    }
    else {
        var thetime = new Date();

        departs = (thetime.getMonth() + 1) + "/" + thetime.getDate() + "/" + thetime.getYear();
        departs = departs.split("/");
    }

    var newDeparture = new Date(departs[0] + "/" + departs[1] + "/" + departs[2]);
    var newArrival = new Date();


    if (!nights) {
        nights = 1;
    }
    if (nights == 0) {
        nights = 1;
    }
    if (nights > 10) {
        nights = 10;
    }
    if ((nights < 10) && (arrivals != "Select Date") && (arrivals != "")) {
        nights = days_between(document.form1.wdcArrival_input.value, document.form1.wdcDeparture_input.value);
    }

    newDeparture.setDate(newDeparture.getDate());
    newArrival.setDate(newDeparture.getDate() - nights);
    
    var departField = (months[newDeparture.getMonth()]) + "/" +
							   ((newDeparture.getDate() < 10) ? "0" : "") + newDeparture.getDate() + "/" +
							   fourdigits(newDeparture.getFullYear());

    var arrivalField = (months[newArrival.getMonth()]) + "/" +
							   ((newArrival.getDate() < 10) ? "0" : "") + newArrival.getDate() + "/" +
							   fourdigits(newArrival.getFullYear());

   //document.form1.departDefault.value = departField;
    document.form1.wdcArrival_input.value = arrivalField;
    document.form1.wcNights_input.value = nights;
    document.form1.nightsDefault.value = nights;

} //end calcDates()



function calcDates(){
        var nights = parseInt(document.form1.wcNights_input.value);
        var arrivals = document.form1.wdcArrival_input.value;

        //alert(arrivals);

        if ((arrivals != "Select Date") && (arrivals != "")) {
          		arrivals = arrivals.split("/");
        }
            else
            {
                var thetime = new Date();
                //alert(thetime)
                //alert(thetime.getMonth()+1)
                //alert(thetime.getDate())
                //alert(thetime.getYear())
                //var date = thetime.getDay() + "/" + thetime.getMonth() + "/" + thetime.getYear();
                
                
                
                arrivals = (thetime.getMonth()+1) + "/" + thetime.getDate() + "/" + thetime.getYear();
                //alert(arrivals)
                arrivals = arrivals.split("/"); 
            }

		var newArrival = new Date(arrivals[0]+"/"+arrivals[1]+"/"+arrivals[2]);
		var newDeparture = new Date(arrivals[0] + "/" + arrivals[1] + "/" + arrivals[2]);

		
		if (!nights){
		    nights = 1;
			}
		if (nights == 0){
		    nights = 1;
			}
		if (nights > 10){
			nights = 10;
		}
		
		newDeparture.setDate(newDeparture.getDate() + nights);
		var departField = (months[newDeparture.getMonth()]) + "/" +
							   ((newDeparture.getDate()<10) ? "0" : "")+ newDeparture.getDate() + "/" +
							   fourdigits(newDeparture.getFullYear());
							   
		document.form1.departDefault.value = departField;
		document.form1.wdcDeparture_input.value = departField;
		document.form1.wcNights_input.value = nights;
		document.form1.nightsDefault.value = nights;
		
		} //end calcDates()
		
		
function days_between(date1, date2) {

var datea = new Date(date1);
var dateb = new Date(date2);

    
    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24

    // Convert both dates to milliseconds
    var date1_ms = datea.getTime()
    var date2_ms = dateb.getTime()

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms)
    
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY)

}

