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);
}
$(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 :selected').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();
		var start = hotelId == '22215' ? "" : 1;
		
		//generate URL string
		url += "?chain="+chainId
			  +"&hotel="+hotelId
			  +"&arrive="+arrivalDate
			  +"&depart="+departDate
			  +"&adult="+adults
			  +"&child="+kids
			  +"&"+codeType+"="+code
		  	+"&start="+start+""
			  +"&lang=1";
		
		
		switch(hotelId)
		{
		case "1922": //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"
				;
		  break;
		case "24803":
			url += "&shell=viceroy";
			break;
			//viceroy hotel that isn't snowmass
		default:
				url += "&shell=viceroy" + "&template=viceroy";
		}
		//send url data to synxis booking engine
		window.open(url);
		return false;
	});
	
	// Update Drop Down Lists
		adultsKids();
	
		// when the location changes
		$('#destination').bind('change', function() {
	        setTimeout("adultsKids()", 250);
		});
	
		// when the adults ddl changes
		$('#adults').bind('change', function() {
	        setTimeout("adultsKids()", 250);
		});
	
		// when the kids ddl changes
		$('#kids').bind('change', function() {
	        setTimeout("adultsKids()", 250);
		});
});

function adultsKids () {

	// get the number of adults and then set this value if still exists.
	var adultsVal = $('#adults').val();
	
	// get the number of kids and then set this value if still exists.
	var kidsVal = $('#kids').val();

	// set the max value for the ddl add 1 to max number
	var kidsMaxVal = 13 - adultsVal ;
	var adultsMaxVal = 13 - kidsVal;
	
	
	// if Anguilla
	if ( $('#destination').val()=='19595' ) {

 		$("#adults").html( genDD( '1', adultsMaxVal, adultsVal ) );
 		$("#kids").html( genDD( '0', kidsMaxVal, kidsVal ) );
		
	} else {
		$("#adults").html( genDD( '0', 5, adultsVal ) );
		$("#kids").html( genDD( '0', 5, kidsVal ) );
	}
}

function genDD( startWith, numTo, selectedNum ) {
	var ddval="";
	for ( i=startWith; i< numTo; i++) {
		ddval += "<option value=\"" + i + "\"";
		if ( i == selectedNum ) {
			ddval += " selected ";
		}
		ddval += ">" + i + "</option>";
	}
	return( ddval );
}
