var defaultCityText = null;
var noCityError = 'Please enter a city.';

function parseCityStateCountry(radiobutton, widgetId) {
	var cityStateCountry = radiobutton.value;
	var city = '';	
	var state = '';	
	var country = '';	
	var destId = '';
	
	//Parse out the four values.
	// The current format is city,state,country,(destId optional)
	try {
		city = cityStateCountry.split(',')[0];
		state = cityStateCountry.split(',')[1];
		country = cityStateCountry.split(',')[2];
		destId = cityStateCountry.split(',')[3];
	}
	catch (e) {
		//Do nothing
	}
	
	setFormValuesByElementName('city', city);
	setFormValuesByElementName('stateProvince', state);
	setFormValuesByElementName('country', country);
	if (destId) setFormValuesByElementName('destId', destId);
	else setFormValuesByElementName('destId', '');
	
	// Set the free-form city box with the city state country value
	if ($('cityText')) {
		prepareForEntry($('cityText'), widgetId + "-free-form");

		var newCityText = city;
		
		if (state && state.length > 0) newCityText +=  ", "+state;
		if (country && country.length > 0) newCityText +=  ", "+country;
		
		$('cityText').value = newCityText;
	}
}

function prepareDataForPost() {
	
	if (validateDates()) {
		setFormValuesByElementName('arrivalMonth', getFormData('checkInMonth'));
		setFormValuesByElementName('arrivalDay', getFormData('checkInDay'));
		setFormValuesByElementName('departureMonth', getFormData('checkOutMonth'));
		setFormValuesByElementName('departureDay', getFormData('checkOutDay'));

		//Build the adult and children room count parameter-value pairs
		// Stash the selected information for possible back-button use
		createCookie('checkIn', startDate, 1);
		createCookie('checkOut', endDate, 1);
		createCookie('rooms', currentRoomCount, 1);

		for(var i=1; i <=currentRoomCount; i++) {			
			createCookie('adultsRoom_'+i, roomsArray[i]['adult'], 1);
			createCookie('childrenRoom_'+i, roomsArray[i]['child'], 1);
		}
		
	 	//Handle the custom-entered city name
		if ((getFormData('cityRadio') == "" || getFormData('cityRadio') == null) && getFormData("cityText") != "" && getFormData("cityText") != defaultCityText) {			
			setFormValuesByElementName('city', getFormData('cityText'));
			setFormValuesByElementName('stateProvince', '');
			setFormValuesByElementName('country', '');
			setFormValuesByElementName('destId', '');
	    }

		// If no city has been provided still... request one
		if (getFormData('city') == '') {
			alert(noCityError)
		}
		else {
			document.hotelSearch.submit(); 	
		}
	}
}

function handleRoomsData() {
	var hiddenInputHTML = '';
	
	//Build the adult and children room count parameter-value pairs
	for(var i=1; i <=currentRoomCount; i++) {	
		hiddenInputHTML += '<input type="hidden" name="room-'+ (i-1)+'-adult-total" value="'+ roomsArray[i]['adult'] +'"/>\n';
		hiddenInputHTML += '<input type="hidden" name="room-'+ (i-1)+'-child-total" value="'+ roomsArray[i]['child'] +'"/>\n';
		
		//Are there children in this room, if so, add their ages
		if (roomsArray[i]['child'] > 0) {
			for(var j=1; j <= roomsArray[i]['child']; j++) {
				hiddenInputHTML += '<input type="hidden" name="room-'+ (i-1)+'-child-'+ (j-1)+'-age" value="10"/>\n';				
			}
		}
	}
		
	//Now find its spot and dump the HTML in
	document.getElementById("hotel-search-params").innerHTML = hiddenInputHTML;	
	
}
