$(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";
		
		// Snowmass
		if (hotelId == "24803") {
				url += "&shell=viceroy";
		}
		//viceroy hotel that isn't snowmass
		//(hotelID == ("19595"||hotelID == "23399"||hotelID == "1921"||hotelID == "1922")
		if ((hotelId == "19595")||(hotelId == "23399")||(hotelId == "1921")||(hotelId == "1922")) {
			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 );
}