$(document).ready(function() {
	$("input#citypu,input#citydo")
		.autocomplete(
			"/ajax/get-dropdown-results.php",{
			delay:0,
			minChars:3,
			matchContains:true,
			matchSubset:false,
			max:100,
			scrollHeight:175,
			width:200,
			formatItem:function(row,current_row,row_count) {
				if (row[0].substr(0,2) == "a^")
				{
					return '<img src="/images/airport-icon.gif" alt="" /> ' + row[0].substr(2);
				}
				else
				{
					return row[0].substr(2);
				}
			},
			formatMatch:function(row,current_row,row_count) {
				return row[0].substr(2);
			},
			formatResult:function(row) {
				return row[0].substr(2);
			}
		})
		.result(function(event,item) {
			var type_value;

			if (item[0].substr(0,2) == "a^")
			{
				type_value = "air";
			}
			else
			{
				type_value = "city";
			}

			if ($(this).attr("id") == "citypu")
			{
				$("input#putype").val(type_value);
			}
			else if ($(this).attr("id") == "citydo")
			{
				$("input#dotype").val(type_value);
			}
		});

	$("select#rental-car-company").change(function() {
		if ($(this).parent().parent().parent().parent().parent().parent().get(0).tagName.toLowerCase() == "td" || $(this).parent().parent().parent().parent().parent().parent().parent().get(0).id.toLowerCase() == "about-search-bottom-right")
		{
			load_discount_code_fields();
		}
		else
		{
			load_discount_code_fields(false,true);
		}
	});

	$("a#search-rental-cars").click(function() {
		if ($("input#citypu").val().length == 0)
		{
			alert("You must specify a location");
			$("input#citypu").focus();
			return false;
		}

		if ($("input#return-at-different-location").attr("checked") && $("input#citydo").val().length == 0)
		{
			alert("You must specify a dropoff location");
			$("input#citydo").focus();
			return false;
		}

		if ($("input#rs_chk_in_vis").val() == "mm/dd/yyyy")
		{
			alert("You must specify a pick-up date");
			$("input#rs_chk_in_vis").focus();
			return false;
		}

		if ($("select#pickup-time").val() == "any")
		{
			alert("You must specify a pick-up time");
			$("select#pickup-time").focus();
			return false;
		}

		if ($("input#rs_chk_out_vis").val() == "mm/dd/yyyy")
		{
			alert("You must specify a drop-off date");
			$("input#rs_chk_out_vis").focus();
			return false;
		}

		if ($("select#dropoff-time").val() == "any")
		{
			alert("You must specify a drop-off time");
			$("select#dropoff-time").focus();
			return false;
		}

		if (Math.abs(new Date($("input#rs_chk_in").val() + " " + $("select#pickup-time").val()).getTime() - new Date($("input#rs_chk_out").val() + " " + $("select#dropoff-time").val()).getTime()) > 1000 * 60 * 60 * 24 * 310)
		{
			alert("That is an invalid drop-off time, it exceeds our limit of 310 days for a reservation");
			$("select#pickup-time").focus();
			return false;
		}

		$(this).addClass("searching");

		$("form.search-form-to-submit").submit();

		// THE NEXT TWO LINES ARE A HACK TO ENSURE THE GIF KEEPS ROTATING EVEN AFTER THE PAGE CHANGE HAS BEGUN (works in ff3, ie6/7 & chrome)
		var image = document.getElementById("loader-img");
		setTimeout(function() { image.src = image.src; },50);

		return false;
	});
});