
$().ready(function() {
	// Set autocomplete for dropoff
	$("#dropoff").autocomplete("/cars/get_locations.php", {
		width: 400,
		cacheLength: 1,
		max: 20,
		minChar: 3,
		focus:false,
		extraParams: { id: function() { return $('#country_id').val(); } 	}
	});
	// Set autocomplete for pickup
	$("#pickup").autocomplete("/cars/get_locations.php", {
		width: 400,
		cacheLength: 1,
		max: 20,
		minChar: 2,
		focus:true,
		extraParams: { id: "*" }
	});

	// What to do when we get results for pickups autocomplete
	$("#pickup").result(function(event, data, formatted) {
		if (data)
		{
			$("#ploc_id").val(data[1]);
			$("#country_id").val(data[2]);
			$("#pickup").val(data[3]);

			$("#dloc_id").val(data[1]);
			$("#dropoff").val(data[3]);
		}
	});


	// What to do when we get results for dropoffs autocomplete
	$("#dropoff").result(function(event, data, formatted) {
		if (data)
		{
			$("#dloc_id").val(data[1]);
			$("#dropoff").val(data[3]);
		}
	});

	// Set calendar to date inputs
    $('#pDate').datepicker({ minDate: 0, dateFormat: 'dd/mm/yy', firstDay: 1, numberOfMonths: 1 });
    $('#dDate').datepicker({ minDate: 0, dateFormat: 'dd/mm/yy', firstDay: 1, numberOfMonths: 1 });

	// When a pickup date is selected make the minDate for dropoff this date
    $('#pDate').change(function () {
      var date_tmp = $('#pDate').val().split("/");
	  var date_help = $('#dDate').val();
      $('#dDate').datepicker('option', { minDate: new Date(date_tmp[2], date_tmp[1]-1, date_tmp[0]) });
	  $('#dDate').val(date_help);
    });

	// When a dropoff date is selected make the maxDate for pickup this date
    $('#dDate').change(function () {
      var date_tmp = $('#dDate').val().split("/");
	  var date_help = $('#pDate').val();
      $('#pDate').datepicker('option', { maxDate: new Date(date_tmp[2], date_tmp[1]-1, date_tmp[0]) });
	  $('#pDate').val(date_help);
    });
  //all hover and click logic for buttons
		$(".fg-button:not(.ui-state-disabled)")
		.hover(
			function(){
				$(this).addClass("ui-state-hover");
			},
			function(){
				$(this).removeClass("ui-state-hover");
			}
		)
		.mousedown(function(){
				$(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
				if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
				else { $(this).addClass("ui-state-active"); }
		})
		.mouseup(function(){
			if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
				$(this).removeClass("ui-state-active");
			}
		});
});

function clearBox(el)
{
	if (el.value == "Type an airport, city or town.")
	  $(el).val('');
}

function setLocation(str, postForm)
{
  var loc = new Array();
  loc = str.split('#');

  $("#pickup").val(loc[2]);
  $("#dropoff").val(loc[2]);
  $("#ploc_id").val(loc[1]);
  $("#dloc_id").val(loc[1]);
  $("#country_id").val(loc[0]);

  $("#dialog1").dialog('close');
}

function checkEnteredLocation()
{
  if (parseInt($("#age").val()) != $("#age").val() )
  {
    alert("Please enter a valid age.")
	return false;
  }

  //alert($("#ploc_id").val());
  if ($("#pickup").val() != "" && $("#ploc_id").val() == 0)
  {
    $("#dialog").html("<div id='dialog1' title='Select Pick Up Location'><span></span></div>");

	$("#dialog1").dialog({
		bgiframe: true, resizable: false,
		height: 340,
		width: 440,
		modal: true,
		overlay:
		{
		  backgroundColor: '#000',
		  opacity: 0.5
		},
		buttons:
		{
		  Cancel: function()
		  {
		  	$(this).dialog('close');
		  }
		}
	});

	$.get(
		"/cars/get_locations.php",
		"&type=overlay&q=" + $("#pickup").val()+"&id=*",

		function(data)
		{
		  $("#dialog1 span").html("<div>"+data+"</div>");
		  $('#dialog1').dialog('show');
		}
	);

	return false;
  }

  if($('#pDate').val() == "Click to choose")
  {
    alert("Please enter a pick up date.");
    $("#pDate").datepicker('show');
	return false;
  }

  if($('#dDate').val() == "Click to choose")
  {
    alert("Please enter a drop off date.");
    $("#dDate").datepicker('show');
	return false;
  }

  return true;
}


