$(document).ready(function() {
	//initialise date picker(s)
	$('.datepicker').datepicker({
		duration: "",
		minDate: '0',
		maxDate: '1Y'
	});
	
	$('#arrive').bind('change', function() {
		//grab date string and parse
		var departDate = $.datepicker.parseDate('mm/dd/yy', $('#arrive').val());
		
		//split date for creation of new Date()
		var month = departDate.getMonth();
		var date = departDate.getDate();
		var year = departDate.getFullYear();
		
		//assure check-out date is > check-in date
		$('#depart').datepicker('option', 'minDate', new Date(year, month, date + 1));
	});
	
	//submit reservation form
	$('#vhg-rese-widget').submit(function() {
		//constants
		domain = "https://gc.synxis.com";
		chainId = "1003";
		
		//gather reservation form data
		var url = domain;
		var hotelId = $('#destination').val();
		var arrivalDate = $('#arrive').val();
		var departDate = $('#depart').val();
		var adults = $('#adults :selected').val();
		var kids = $('#kids :selected').val();
		var codeType = $('#code-type :selected').val();
		var code = $('#code').val();
		
//		case "1135": //Sheraton Delfina has a seperate Booking Engine
			/*
			This is an example of a booking URL.
			http://www.starwoodhotels.com/sheraton/search/ratelist.html?propertyID=1135&roomOccupancyTotal=1&departureDate=2010-12-10&arrivalDate=2010-12-09&lengthOfStay=1&numberOfRooms=1&numberOfAdults=1&language=en_US&ES=Custom_Tracking_Code
			Please note that the only field that is mandatory is the prop id. Everything else is optional.
	        Here's an explanation of the parameters used:

			propertyID=1135  this defines the property that will be pulled on the booking page. For Sheraton Delfina Santa Monica Hotel this will always be 1135. This is a mandatory field
			roomOccupancyTotal=1  this defines the total room occupancy (includes adults + kids) and they may use a javascript to calculate that using a dynamic form
			departureDate=2010-12-10  this is the departure date. This should pull from the form
			arrivalDate=2010-12-09  this is the arrival date. This should pull from the form
			lengthOfStay=1  this is calculated by javascript automatically and it can be optional.
			numberOfRooms=1  this defines the number of room (it can only be up to 9 rooms). This info should pull from the form
			numberOfAdults=1  this is defines the number of adults and pulls from form used on the website
			language=en_US  this is the language variable. This defines in what language the booking page will appear. (other values are: fr_FR for French, es_ES for Spanish, de_DE for German, it_IT for Italian, ja_JP for Japanese and zh_CN for Chinese)
			ES=Custom_Tracking_Code  this can be anything they want as long as it includes the brand id and the prop id. Heres an example they can use: ES= sheratondelfina.com_SI_1135
			*/
			url = "http://www.starwoodhotels.com/sheraton/search/ratelist.html?propertyID=1135"
				+ "&departureDate="+StarwoodDate(departDate)
				+ "&arrivalDate="+StarwoodDate(arrivalDate)
				+ "&roomOccupancyTotal=" + (parseInt(adults,10)+parseInt(kids,10)).toString()
				+ "&numberOfAdults="+adults
				+ "&promoCode="+code
				+ "&language=en_US"
				+ "&ES=UrbanQuickRes_SI_1135"
				;

			//send url data to synxis booking engine
			window.open(url);
			
			return false;
		});
	});

function StarwoodDate (theDate){
	var d = new Date(theDate);
	var curr_date = d.getDate();
	if (curr_date < 10){curr_date = "0" + curr_date;}
	var curr_month = d.getMonth();
	curr_month++;
	if (curr_month < 10){curr_month = "0" + curr_month;}
	var curr_year = d.getFullYear();
	return (curr_year + "-" + curr_month + "-" + curr_date);
}
