function gotoAPage(aURL, width, height)
{	
	window.open(aURL,'mywin', 'left=20,top=20,width=' + width + ',height=' + height + ',toolbar=0,resizable=0,scrollbars=yes');
}

function setCity(aListControl)
{
	if ( (aListControl.id == "lstCities") && (document.getElementById("hidCity") != null) )
	{
		document.getElementById("hidCity").value = aListControl.selectedIndex;
	}
	
	getLocationsInACity(aListControl);
}

function setLocation(aListControl)
{
	if ( (aListControl.id == "lstLocations") && (document.getElementById("hidLocation") != null) )
	{
		document.getElementById("hidLocation").value = aListControl.selectedIndex;
	}
}

function setupForm()
{
	if ( (document.getElementById("hidCityArray") != null) && (document.getElementById("hidCityArray").value != '') )
	{
	
		var cities= document.getElementById("hidCityArray").value.split(",");

		// set city list
		var sel;
		sel = document.getElementById("lstCities");
		
		if (sel == null)
			return;
			
		for(i=sel.options.length-1; i>=0; i--)
		{
			sel.options[i] = null;
		}

		element = document.getElementById("lstCountries");
		if (element == null)
			return;
		
		var opt = new Option(' - please select a city in ' + element[element.selectedIndex].text + ' - ', '-1');
		sel.options[sel.options.length] = opt;

		for (i=0; i<cities.length; i++)
		{
			opt = new Option(cities[i], cities[i]);
			sel.options[sel.options.length] = opt;
		}
		
		if (document.getElementById("hidCity").value != '')
			sel.selectedIndex = document.getElementById("hidCity").value;
		
		sel.disabled = false;
	}
	
	if ( (document.getElementById("hidLocationArray") != null) && (document.getElementById("hidLocationArray").value != '') )
	{
	
		var locations= document.getElementById("hidLocationArray").value.split(",");

		// set location list
		var sel;
		sel = document.getElementById("lstLocations");
		
		if (sel == null)
			return;
			
		for(i=sel.options.length-1; i>=0; i--)
		{
			sel.options[i] = null;
		}

		element = document.getElementById("lstCities");
		if (element == null)
			return;
		
		var opt = new Option(' - please select a location in ' + element[element.selectedIndex].text + ' - ', '-1');
		sel.options[sel.options.length] = opt;

		for (i=0; i<locations.length; i++)
		{
			opt = new Option(locations[i], locations[i]);
			sel.options[sel.options.length] = opt;
		}
		
		if (document.getElementById("hidLocation").value != '')
			sel.selectedIndex = document.getElementById("hidLocation").value;
		
		sel.disabled = false;
	}
}

var submitInProgress = false;

function addingDays(myDate,days) {
    return new Date(myDate.getTime() + days*24*60*60*1000);
}

function checkValid(aBtnControl)
{
	if (submitInProgress)
		return false;

	// check if all mandatory input is supplied
	var todayDate = new Date();
	var chkInDate = new Date();
	var chkOutDate = new Date();
	var aYearLaterDate = addingDays(todayDate, 365);
	
	var element;

	with(document.forms[0]){
		chkInDate.setFullYear(parseInt(arrival3.value),parseInt(arrival2.value),parseInt(arrival1.value)+1);
		chkOutDate.setFullYear(parseInt(departure3.value),parseInt(departure2.value),parseInt(departure1.value)+1);
		if (chkInDate<todayDate)
		{
			alert("Check in date is not valid. Please enter a date equal to or later than today");
			return false;
		}

		if (chkOutDate<todayDate)
		{
			alert("Check out date is not a valid. Please enter a date equal to or later than today");
			return false;
		}

		if (chkInDate>=aYearLaterDate)
		{
			alert("You can only make reservation of up to one year in advance.  Please change your date and try again.");
			return false;
		}

		if (chkOutDate>aYearLaterDate)
		{
			alert("You can only make reservation of up to one year in advance. Please change the date and try again.");
			return false;
		}
		
		if (chkOutDate<=chkInDate)
		{
			alert("Invalid date entries.  Check in date must be earlier than check out date.");
			return false;
		}

		element = document.getElementById("lstCountries");
		if (element.selectedIndex < 1)
		{
			alert("You must select a country first.");
			return false;
		}

		element = document.getElementById("lstCities");
		if (element.selectedIndex < 1)
		{
			alert("You must select a city first.");
			return false;
		}

		element = document.getElementById("lstLocations");
		if (element.selectedIndex < 1)
		{
			alert("You must select a location or all location first.");
			return false;
		}
	}
	
	
	aBtnControl.value = "please wait";
	submitInProgress = true;
	return true;
}

function displayRoomsNeeded()
{
	var roomsNeeded = "";
	var separator = " <b>and</b> ";
	var totalRooms = 0;
	
	with(document.forms[0]){
		if (chkBoxTwin.checked) 
		{				
			if (lstNumTwinRooms.selectedIndex>0)
				roomsNeeded += lstNumTwinRooms.options[lstNumTwinRooms.selectedIndex].value + " twin rooms";
			else
				roomsNeeded += lstNumTwinRooms.options[lstNumTwinRooms.selectedIndex].value + " twin room";

			totalRooms += parseInt(lstNumTwinRooms.options[lstNumTwinRooms.selectedIndex].value);
		}
		
		if (chkBoxTwinPlusChild.checked) 
		{
			if (roomsNeeded != "")
				roomsNeeded += separator;

			if (lstNumTwinPlusChildRooms.selectedIndex>0)
				roomsNeeded += lstNumTwinPlusChildRooms.options[lstNumTwinPlusChildRooms.selectedIndex].value + " twin plus child rooms";
			else
				roomsNeeded += lstNumTwinPlusChildRooms.options[lstNumTwinPlusChildRooms.selectedIndex].value + " twin plus child room";

			totalRooms += parseInt(lstNumTwinPlusChildRooms.options[lstNumTwinPlusChildRooms.selectedIndex].value);
		}
		
		if (chkBoxDouble.checked)
		{
			if (roomsNeeded != "")
				roomsNeeded += separator;

			if (lstNumDoubleRooms.selectedIndex>0)
				roomsNeeded += lstNumDoubleRooms.options[lstNumDoubleRooms.selectedIndex].value + " double rooms";
			else
				roomsNeeded += lstNumDoubleRooms.options[lstNumDoubleRooms.selectedIndex].value + " double room";

			totalRooms += parseInt(lstNumDoubleRooms.options[lstNumDoubleRooms.selectedIndex].value);
		}
		
		if (chkBoxDoublePlusChild.checked) 
		{
			if (roomsNeeded != "")
				roomsNeeded += separator;
		
			if (lstNumDoublePlusChildRooms.selectedIndex>0)
				roomsNeeded += lstNumDoublePlusChildRooms.options[lstNumDoublePlusChildRooms.selectedIndex].value + " double plus child rooms";
			else
				roomsNeeded += lstNumDoublePlusChildRooms.options[lstNumDoublePlusChildRooms.selectedIndex].value + " double plus child room";

			totalRooms += parseInt(lstNumDoublePlusChildRooms.options[lstNumDoublePlusChildRooms.selectedIndex].value);
		}
		
		if (chkBoxSingle.checked) 
		{
			if (roomsNeeded != "")
				roomsNeeded += separator;

			if (lstNumSingleRooms.selectedIndex>0)
				roomsNeeded += lstNumSingleRooms.options[lstNumSingleRooms.selectedIndex].value + " single rooms";
			else
				roomsNeeded += lstNumSingleRooms.options[lstNumSingleRooms.selectedIndex].value + " single room";

			totalRooms += parseInt(lstNumSingleRooms.options[lstNumSingleRooms.selectedIndex].value);
		}
		
		if (chkBoxTripple.checked)
		{
			if (roomsNeeded != "")
				roomsNeeded += separator;

			if (lstNumTrippleRooms.selectedIndex>0)
				roomsNeeded += lstNumTrippleRooms.options[lstNumTrippleRooms.selectedIndex].value + " tripple rooms";
			else
				roomsNeeded += lstNumTrippleRooms.options[lstNumTrippleRooms.selectedIndex].value + " tripple room";

			totalRooms += parseInt(lstNumTrippleRooms.options[lstNumTrippleRooms.selectedIndex].value);
		}
		
		if (chkBoxTwinForSoleUse.checked)
		{
			if (roomsNeeded != "")
				roomsNeeded += separator;

			if (lstNumTwinForSoleUseRooms.selectedIndex>0)
				roomsNeeded += lstNumTwinForSoleUseRooms.options[lstNumTwinForSoleUseRooms.selectedIndex].value + " twin for sole use rooms";
			else
				roomsNeeded += lstNumTwinForSoleUseRooms.options[lstNumTwinForSoleUseRooms.selectedIndex].value + " twin for sole use room";

			totalRooms += parseInt(lstNumTwinForSoleUseRooms.options[lstNumTwinForSoleUseRooms.selectedIndex].value);
		}
		
		if (chkBoxQuad.checked)
		{
			if (roomsNeeded != "")
				roomsNeeded += separator;

			if (lstNumQuadRooms.selectedIndex>0)
				roomsNeeded += lstNumQuadRooms.options[lstNumQuadRooms.selectedIndex].value + " quad rooms";
			else
				roomsNeeded += lstNumQuadRooms.options[lstNumQuadRooms.selectedIndex].value + " quad room";

			totalRooms += parseInt(lstNumQuadRooms.options[lstNumQuadRooms.selectedIndex].value);
		}
		
		numberOfRooms.innerHTML = "<b>" + totalRooms + "</b>";
		
		if (roomsNeeded != "")
		{
			roomsSelected.innerHTML = "You need:<br>" + roomsNeeded;
		}
		else
		{
			roomsSelected.innerHTML = "";
		}
	}
}


function getLocationsInACity(aListControl)
{
	// disabled button and wait for location data to be available	
	var element = document.getElementById("btnSearchForHotel");
	if (element != null)
		element.disabled = true;

	// disabled list and wait for location data to be available 
	element = document.getElementById("lstLocations");
	if (element != null)
		element.disabled = true;

	// if a city is selected, send request to server
	var countryLst = document.getElementById("lstCountries");
	if (countryLst == null)
		return;

	if (aListControl.selectedIndex != 0)
	{
		sendDataToServer("exchangerGetLocation.aspx?" , "cityName=" + aListControl[aListControl.selectedIndex].value + "&countryCode=" + countryLst[countryLst.selectedIndex].value);
	}
	else // no city being selected
	{
		// enable search button
		element = document.getElementById("btnSearchForHotel");
		if (element != null)
			element.disabled = false;
		
		// empty city select control
		element = document.getElementById("lstLocations");
		emptyControlList(element);
		var opt = new Option(' - please select a country first and a city first- ', '-1');
		element.options[element.options.length] = opt;
		
		// reset hidCityArray
		element = document.getElementById("hidLocationArray");
		if (element != null)
			element.value = '';
				
		// reset hidCity
		element = document.getElementById("hidLocation");
		if (element != null)
			element.value = '';
	}
}

function getDataForLocationList()
{
	var element;

	// get locations found from server
	var locations = showReturnData("myLocationArray");
	
	if ((locations == null) || (locations.length == 0))
		return;
		
	if (locations.length == 1)
	{
		if (locations[0] == "no hotel found")
		{
			alert("We have no hotel in this city");
			
			// empty city select control
			element = document.getElementById("lstLocations");
			emptyControlList(element);
			var opt = new Option(' - please select a country first and a city first - ', '-1');
			element.options[element.options.length] = opt;
			
			// reset hidCityArray
			element = document.getElementById("hidLocationArray");
			element.value = '';
			
			// reset hidCity
			element = document.getElementById("hidLocation");
			element.value = '';
			return;
		}
	}
	

	// enabled button
	element = document.getElementById("btnSearchForHotel");
	if (element != null)
		element.disabled = false;

	// enabled list 
	element = document.getElementById("lstLocations");
	if (element != null)
		element.disabled = false;

	// set locations list
	var sel;
	sel = document.getElementById("lstLocations");
	
	if (sel == null)
		return;
		
	for(i=sel.options.length-1; i>=0; i--)
	{
		sel.options[i] = null;
	}
	
	element = document.getElementById("lstCities");
	if (element == null)
		return;

	var opt = new Option(' - please select a location in ' + element[element.selectedIndex].text + ' - ', '-1');
	sel.options[sel.options.length] = opt;
	var hidLocationArrayControl = document.getElementById("hidLocationArray");
	hidLocationArrayControl.value="";
	for (i=0; i<locations.length; i++)
	{
		opt = new Option(locations[i], locations[i]);
		hidLocationArrayControl.value += locations[i] + ",";
		sel.options[sel.options.length] = opt;
	}
	hidLocationArrayControl.value = hidLocationArrayControl.value.substring(0,hidLocationArrayControl.value.length - 1);
}	


function getCitiesInACountry(aListControl)
{
	var element;
	
	// disabled button and wait for cities data to be available	
	element = document.getElementById("btnSearchForHotel");
	if (element != null)
		element.disabled = true;

	// disabled list and wait for cities data to be available 
	element = document.getElementById("lstCities");
	if (element != null)
		element.disabled = true;

	// if a country is selected, send request to server
	if (aListControl.selectedIndex != 0)
	{
		sendDataToServer("exchangerGetCity.aspx?" , "countryCode=" + aListControl[aListControl.selectedIndex].value);
	}
	else // no country being selected
	{
		// enable search button
		element = document.getElementById("btnSearchForHotel");
		if (element != null)
			element.disabled = false;
		
		// empty city select control
		element = document.getElementById("lstCities");
		emptyControlList(element);
		var opt = new Option(' - please select a country first - ', '-1');
		element.options[element.options.length] = opt;
		
		// reset hidCityArray
		element = document.getElementById("hidCityArray");
		element.value = '';
			
		// reset hidCity
		element = document.getElementById("hidCity");
		element.value = '';
	}
}

function emptyControlList(aListControl)
{
	if (aListControl == null)
		return;
	
	if (aListControl.options.length == 0)
		return;
		
	for(i=aListControl.options.length-1; i>=0; i--)
	{
		aListControl.options[i] = null;
	}
}

function getDataForCityList()
{
	var element;

	// get cities found from server
	var cities = showReturnData("myCityArray");
	
	if ((cities == null) || (cities.length == 0))
		return;
		
	if (cities.length == 1)
	{
		if (cities[0] == "no hotel found")
		{
			alert("We have no hotel in this country");
			
			// empty city select control
			element = document.getElementById("lstCities");
			emptyControlList(element);
			var opt = new Option(' - please select a country first - ', '-1');
			element.options[element.options.length] = opt;
			
			// reset hidCityArray
			element = document.getElementById("hidCityArray");
			element.value = '';
			
			// reset hidCity
			element = document.getElementById("hidCity");
			element.value = '';
			return;
		}
	}
	

	// enabled button and wait for cities data to be available	
	element = document.getElementById("btnSearchForHotel");
	if (element != null)
		element.disabled = false;

	// enabled list and wait for cities data to be available 
	element = document.getElementById("lstCities");
	if (element != null)
		element.disabled = false;

	// set city list
	var sel;
	sel = document.getElementById("lstCities");
	
	if (sel == null)
		return;
		
	for(i=sel.options.length-1; i>=0; i--)
	{
		sel.options[i] = null;
	}
	
	element = document.getElementById("lstCountries");
	if (element == null)
		return;

	var opt = new Option(' - please select a city in ' + element[element.selectedIndex].text + ' - ', '-1');
	sel.options[sel.options.length] = opt;
	var hidCityArrayControl = document.getElementById("hidCityArray");
	hidCityArrayControl.value="";
	for (i=0; i<cities.length; i++)
	{
		opt = new Option(cities[i], cities[i]);
		hidCityArrayControl.value += cities[i] + ",";
		sel.options[sel.options.length] = opt;
	}
	hidCityArrayControl.value = hidCityArrayControl.value.substring(0,hidCityArrayControl.value.length - 1);
}	

// for iframe, get city

var theBuffer;

//call this function in page onload event handler
function initialize()
{
	theBuffer = new exchanger("myframe");
}

//call this function when data needs to be sent to server
function sendDataToServer(pageURL, data)
{
	theBuffer.sendData(pageURL + data);
}

//call this function to check what the server returns.
function showReturnData(bufferName)
{
	return theBuffer.retrieveData(bufferName);
}
