function carsSwitchSearchNear(element)
{
	if (element == 'cars_switch_city')
	{
		$('cars_switch_airport').style.display = 'none';
		$('cars_switch_city').style.display = '';
	}
	else
	{
		$('cars_switch_city').style.display = 'none';
		$('cars_switch_airport').style.display = '';
	}
}

function setInMotion(inMotionValue)
{
	inMotion = inMotionValue;
}

/* 
| Slide elements of the packages and incredideals
*/
function slideCompareElements(elementID)
{
	if (inMotion == 'y')
	{
		return false;
	}

	if ($(elementID) && $(elementID).style.display == '')
	{
		return false;
	}
	
	if ($(elementID) && $(elementID).style.display == 'none')
	{
		if ($('slide_me_flights') && $('slide_me_flights').style.display == '')
		{
			slideUpID = 'slide_me_flights_body';
		}
		else if ($('slide_me_cars') && $('slide_me_cars').style.display == '')
		{
			slideUpID = 'slide_me_cars_body';
		}
		else if ($('slide_me_lodging') && $('slide_me_lodging').style.display == '')
		{
			slideUpID = 'slide_me_lodging_body';
		}
		else
		{
			return false;
		}

		setInMotion('y');
		
		new Effect.Parallel(
			[
				Effect.BlindDown(elementID,
					{
						delay: .1,
						afterFinish: function() { setInMotion('n'); }
					}
				),
				Effect.BlindUp(slideUpID)
			],
			{
				duration: .3
			}
		);

		return true;
	}	
}


function slideCompareElements2(elementID)
	{
		if (inMotion == 'y')
		{
			return false;
		}
	
		if ($(elementID+'_body') && $(elementID+'_body').style.display == '')
		{
			return false;
		}
		
		if ($(elementID+'_body') && $(elementID+'_body').style.display == 'none')
		{
			if ($('slide_me_flights_body') && $('slide_me_flights_body').style.display == '')
			{
				slideUpID = 'slide_me_flights_body';
				imgId = 'slide_me_flights_arrow';
			}
			else if ($('slide_me_cars_body') && $('slide_me_cars_body').style.display == '')
			{
				slideUpID = 'slide_me_cars_body';
				imgId = 'slide_me_cars_arrow';
			}
			else if ($('slide_me_lodging_body') && $('slide_me_lodging_body').style.display == '')
			{
				slideUpID = 'slide_me_lodging_body';
				imgId = 'slide_me_lodging_arrow';
			}
			else
			{
				return false;
			}
	
			setInMotion('y');
			$(imgId).src = '/images/theme4/top10/deals_head_arr_down.gif';
			$(elementID+'_arrow').src = '/images/theme4/top10/deals_head_arr_up.gif';
			
			new Effect.Parallel(
				[
					Effect.BlindDown(elementID+'_body',
						{
							sync:true,
							afterFinish: function() { setInMotion('n');}
						}
					),
					Effect.BlindUp(slideUpID, {sync:true})
				],
				{
					duration: .5
				}
			);
	
			return true;
		}	
	}

function compareRates(sectionName, isRegionPage, locationName)
{	
	var section = sectionName;
	var regionPage = isRegionPage;
	var url = '/util/generateCompareUrls.php';
	var compareSites = '';
	var uniqueIDs = '';
	var separator = '';
	var separator2 = '';
	var elem = $(section + 'Form').elements;
	var left = 0;
	var top = 0;

	if (section == 'flights')
	{
		var fromAirport = $('from_airport').value;
		var toAirport = $('to_airport').value;
		var departDate = $('depart_date').value;
		var returnDate = $('return_date').value;
		var numberAdults = $('flights_number_adults').value;
	}
	else if (section == 'cars')
	{
		var searchNear = $('search_near').value;
		var carsCity = $('cars_city').value;
		var carsCityLocation = $('cars_city_location').value;
		var carsAirport = $('cars_airport').value;
		var pickupDate = $('pickup_date').value;
		var pickupTime = $('pickup_time').value;
		var dropoffDate = $('dropoff_date').value;
		var dropoffTime = $('dropoff_time').value;
	}
	else if (section == 'lodging')
	{
		if (regionPage == 'y')
		{
			var lodgingCity = $('lodging_city').value;
			var lodgingCityLocation = $('lodging_city_location').value;
		}
		
		var arrivalDate = $('arrival_date').value;
		var departureDate = $('departure_date').value;
		var numberAdults = $('number_adults').value;
		var numberChildren = $('number_children').value;
	}
	else
	{
		return false;
	}

	for (var i = 0; i < elem.length; i++)
	{
		if (elem[i].type == 'checkbox' && elem[i].checked == true)
		{
			compareSites += separator + elem[i].name.substring(5 + section.length);
			separator = ',';
		}
		
		if (section == 'lodging' && regionPage == 'n')
		{
			if (elem[i].type == 'hidden')
			{
				var name = elem[i].name;
				var parentOtaElement = 'ota_lodging_' + name.substring(14);

				if ($(parentOtaElement).checked == true)
				{
					uniqueIDs += separator2 + elem[i].value;
					separator2 = ',';
				}
			}
		}
	}

	if (section == 'flights')
	{
		if ($('from_airport').value == '' && $('to_airport').value == '' && !compareSites)
		{
			alert('You must select a From, To, and at least one website.');

			return false;
		}
		
		if ($('from_airport').value == '' && $('to_airport').value == '')
		{
			alert('You must select a From and a To.');

			return false;
		}
		
		if ($('from_airport').value == '' && !compareSites)
		{
			alert('You must select a From and at least one website.');

			return false;
		}
		
		if ($('to_airport').value == '' && !compareSites)
		{
			alert('You must select a To and at least one website.');

			return false;
		}
		
		if ($('from_airport').value == '')
		{
			alert('You must select a From.');

			return false;
		}
		
		if ($('to_airport').value == '')
		{
			alert('You must select a To.');

			return false;
		}
		
		if ($('depart_date').value == '' && $('return_date').value == '' && !compareSites)
		{
			alert('You must select a Depart Date, Return Date, and at least one website.');

			return false;
		}

		if ($('depart_date').value == '' && $('return_date').value == '')
		{
			alert('You must select a Depart Date and Return Date.');

			return false;
		}

		if ($('depart_date').value == '' && !compareSites)
		{
			alert('You must select a Depart Date and at least one website.');

			return false;
		}

		if ($('return_date').value == '' && !compareSites)
		{
			alert('You must select a Return Date and at least one website.');

			return false;
		}

		if ($('depart_date').value == '')
		{
			alert('You must select a Depart Date.');

			return false;
		}

		if ($('return_date').value == '')
		{
			alert('You must select a Return Date.');

			return false;
		}
	}
	else if (section == 'cars')
	{
		if ($('search_near').value == 'cars_switch_city')
		{
			var elementTempName = 'cars_city';
			var valueTempName = 'a City';
		}
		else if ($('search_near').value == 'cars_switch_airport')
		{
			var elementTempName = 'cars_airport';
			var valueTempName = 'an Airport';
		}
		else
		{
			return false;
		}

		if ( $(elementTempName).value == '' && $('pickup_date').value == '' && $('dropoff_date').value == '' && !compareSites)
		{
			alert('You must select ' + valueTempName + ', Pick-Up Date, Drop-Off Date, and at least one website.');

			return false;
		}
	
		if ($(elementTempName).value == '' && $('pickup_date').value == '' && !compareSites)
		{
			alert('You must select ' + valueTempName + ', Pick-Up Date, and at least one website.');

			return false;
		}
	
		if ($(elementTempName).value == '' && $('dropoff_date').value == '' && !compareSites)
		{
			alert('You must select ' + valueTempName + ', Drop-Off Date, and at least one website.');

			return false;
		}
	
		if ($(elementTempName).value == '' && !compareSites)
		{
			alert('You must select ' + valueTempName + ' and at least one website.');

			return false;
		}
	
		if ($(elementTempName).value == '')
		{
			alert('You must select ' + valueTempName + '.');

			return false;
		}

		/*
		| No pickup date, dropoff date, or websites selected
		*/
		if ($('pickup_date').value == '' && $('dropoff_date').value == '' && !compareSites)
		{
			alert('You must select a Pick-Up Date, Drop-Off Date, and at least one website.');

			return false;
		}

		/*
		| No pickup date or dropoff date
		*/
		if ($('pickup_date').value == '' && $('dropoff_date').value == '')
		{
			alert('You must select a Pick-Up Date and Drop-Off Date.');

			return false;
		}

		/*
		| No pickup off date or websites selected
		*/
		if ($('pickup_date').value == '' && !compareSites)
		{
			alert('You must select a Pick-Up Date and at least one website.');

			return false;
		}

		/*
		| No drop off date or websites selected
		*/
		if ($('dropoff_date').value == '' && !compareSites)
		{
			alert('You must select a Drop-Off Date and at least one website.');

			return false;
		}

		/*
		| No pickup date
		*/
		if ($('pickup_date').value == '')
		{
			alert('You must select a Pick-Up Date.');

			return false;
		}

		/*
		| No drop off date
		*/
		if ($('dropoff_date').value == '')
		{
			alert('You must select a Drop-Off Date.');

			return false;
		}
	}
	else if (section == 'lodging')
	{
		if (regionPage == 'y')
		{
			if ( $('lodging_city').value == '' && $('arrival_date').value == '' && $('departure_date').value == '' && !compareSites)
			{
				alert('You must select a City, Arrival Date, Departure Date, and at least one website.');

				return false;
			}
	
			if ($('lodging_city').value == '' && $('arrival_date').value == '' && !compareSites)
			{
				alert('You must select a City, Arrival Date, and at least one website.');

				return false;
			}
	
			if ($('lodging_city').value == '' && $('departure_date').value == '' && !compareSites)
			{
				alert('You must select a City, Departure Date, and at least one website.');

				return false;
			}
	
			if ($('lodging_city').value == '' && !compareSites)
			{
				alert('You must select a City and at least one website.');

				return false;
			}
	
			if ($('lodging_city').value == '')
			{
				alert('You must select a City.');

				return false;
			}
		}

		if ($('arrival_date').value == '' && $('departure_date').value == '' && !compareSites)
		{
			alert('You must select an Arrival Date, Departure Date, and at least one website.');

			return false;
		}

		if ($('arrival_date').value == '' && $('departure_date').value == '')
		{
			alert('You must select an Arrival Date and Departure Date.');

			return false;
		}

		if ($('arrival_date').value == '' && !compareSites)
		{
			alert('You must select an Arrival Date and at least one website.');

			return false;
		}

		if ($('departure_date').value == '' && !compareSites)
		{
			alert('You must select a Departure Date and at least one website.');

			return false;
		}

		if ($('arrival_date').value == '')
		{
			alert('You must select an Arrival Date.');

			return false;
		}

		if ($('departure_date').value == '')
		{
			alert('You must select a Departure Date.');

			return false;
		}
	}
	else
	{
		return false;
	}

	if (!compareSites)
	{
		alert('You must select at least one website');
		
		return false;
	}

	if (section == 'flights')
	{	
		new Ajax.Request(url, {
			method: 'post',
			parameters: {
				ota_ids: compareSites,
				from_airport: fromAirport,
				to_airport: toAirport,
				depart_date: departDate,
				return_date: returnDate,
				number_adults: numberAdults,
				region_page: regionPage,
				compare_section: section
			},
			onException: function(request, exception)
			{
				//alert("JS Exception: " + exception);
			},
			onComplete: function (transport)
			{
				/*
				| Use json for this to return an array and loop through it.
				*/
				var data = transport.responseText.evalJSON();

				for (var i = 0; i < data.tracking.length; i++)
				{
					linkCode(false, 'ota-flights-' + locationName + '-compare', data.tracking[i]['account_name'], data.tracking[i]['link_name'], data.tracking[i]['value']);
				}

				for (var i = 0; i < data.urls.length; i++)
				{
					left = (i * 30) + 100;
					top = (i * 50) + 50;

					var settings = 'height=500,width=500,resizable=1,toolbar=1,location=yes,status=yes,menubar=yes,scrollbars=yes,left=' + left + ',top=' + top;

					window.open(data.urls[i], i + '_flights_compare', settings);
				}
			}
		});
	}
	else if (section == 'cars')
	{	
		new Ajax.Request(url, {
			method: 'post',
			parameters: {
				ota_ids: compareSites,
				search_near: searchNear,
				cars_city: carsCity,
				cars_city_location: carsCityLocation,
				cars_airport: carsAirport,
				pickup_date: pickupDate,
				pickup_time: pickupTime,
				dropoff_date: dropoffDate,
				dropoff_time: dropoffTime,
				region_page: regionPage,
				compare_section: section
			},
			onException: function(request, exception)
			{
				//alert("JS Exception: " + exception);
			},
			onComplete: function (transport)
			{	
				/*
				| Use json for this to return an array and loop through it.
				*/
				var data = transport.responseText.evalJSON();

				for (var i = 0; i < data.tracking.length; i++)
				{
					linkCode(false, 'ota-cars-' + locationName + '-compare', data.tracking[i]['account_name'], data.tracking[i]['link_name'], data.tracking[i]['value']);
				}

				for (var i = 0; i < data.urls.length; i++)
				{
					left = (i * 30) + 100;
					top = (i * 50) + 50;

					var settings = 'height=500,width=500,resizable=1,toolbar=1,location=yes,status=yes,menubar=yes,scrollbars=yes,left=' + left + ',top=' + top;

					window.open(data.urls[i], i + '_cars_compare', settings);
				}
			}
		});
	}
	else if (section == 'lodging')
	{
		if (regionPage == 'y')
		{	
			new Ajax.Request(url, {
				method: 'post',
				parameters: {
					ota_ids: compareSites,
					lodging_city: lodgingCity,
					lodging_city_location: lodgingCityLocation,
					arrival_date: arrivalDate,
					departure_date: departureDate,
					number_adults: numberAdults,
					number_children: numberChildren,
					region_page: regionPage,
					compare_section: section
				},
				onException: function(request, exception)
				{
					//alert("JS Exception: " + exception);
				},
				onComplete: function (transport)
				{
					/*
					| Use json for this to return an array and loop through it.
					*/
					var data = transport.responseText.evalJSON();

					for (var i = 0; i < data.tracking.length; i++)
					{
						linkCode(false, 'ota-lodging-' + locationName + '-compare', data.tracking[i]['account_name'], data.tracking[i]['link_name'], data.tracking[i]['value']);
					}

					for (var i = 0; i < data.urls.length; i++)
					{
						left = (i * 30) + 100;
						top = (i * 50) + 50;

						var settings = 'height=500,width=500,resizable=1,toolbar=1,location=yes,status=yes,menubar=yes,scrollbars=yes,left=' + left + ',top=' + top;

						window.open(data.urls[i], i + '_lodging_compare', settings);
					}
				}
			});
		}
		else
		{
			new Ajax.Request(url, {
				method: 'post',
				parameters: {
					ota_ids: compareSites,
					ota_unique_ids: uniqueIDs,
					arrival_date: arrivalDate,
					departure_date: departureDate,
					number_adults: numberAdults,
					number_children: numberChildren,
					region_page: regionPage,
					compare_section: section
				},
				onComplete: function (transport)
				{		
					/*
					| Use json for this to return an array and loop through it.
					*/
					var data = transport.responseText.evalJSON();

					for (var i = 0; i < data.tracking.length; i++)
					{
						linkCode(false, 'ota-lodging-' + locationName + '-compare', data.tracking[i]['account_name'], data.tracking[i]['link_name'], data.tracking[i]['value']);
					}

					for (var i = 0; i < data.urls.length; i++)
					{
						left = (i * 30) + 100;
						top = (i * 50) + 50;

						var settings = 'height=500,width=500,resizable=1,toolbar=1,location=yes,status=yes,menubar=yes,scrollbars=yes,left=' + left + ',top=' + top;

						window.open(data.urls[i], i + '_lodging_compare', settings);
					}
				}
			});
		}
	}
	else
	{
		return false;
	}
}