var srcRoot="";
if(top.location.href.indexOf("local.breezenet")==-1&&top.location.href.indexOf("dev.breezenet")==-1&&top.location.href.indexOf("qa.breezenet")==-1&&top.location.href.indexOf("secure.breezenet")==-1&&top.location.href.indexOf("rentalcars")==-1)srcRoot="http://secure.breezenet.com";

$(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="' + srcRoot + '/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).hasClass("bnm-gray"))
		{
			load_discount_code_fields(false,true);
		}
		else 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() {
		// ensure there is a three hour difference between pick-up and drop-off on same day rentals
		if ($("input#rs_chk_in").val() == $("input#rs_chk_out").val())
		{
			var hours_difference = (parseInt($("select#dropoff-time").val().replace("30","50").replace(":",""),10) - parseInt($("select#pickup-time").val().replace("30","50").replace(":",""),10)) / 100;
			if (hours_difference < 3)
			{
				alert("There must be a three hour difference between the pick-up and drop-off times");
				$("select#dropoff-time").focus();
				return false;
			}
		}

		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;
		}

		if ($("select#rental-car-company").val() != "" && $("select#rental-car-company").val() != undefined)
		{
			var blnCoupon=false;
			
			if($("input#cd").val()!=undefined&&$("input#cd").val()!="")blnCoupon=true;
			if($("input#pc").val()!=undefined&&$("input#pc").val()!="")blnCoupon=true;
			if($("input#rc").val()!=undefined&&$("input#rc").val()!="")blnCoupon=true;
			if($("input#cd2").val()!=undefined&&$("input#cd2").val()!="")blnCoupon=true;
			
			if(!blnCoupon)
			{
				alert("Please enter your coupon code.");
				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;
	});
});