$().ready(function() {
	// Set autocomplete for dropoff
	$("#dropoff").autocomplete("/post/get_locations.php", {
		width: 300,
		cacheLength: 1,
		max: 20,
		minChar: 3,
		focus:false,
		extraParams: { id: function() { return $('#country_id').val(); } 	}
	});
	// Set autocomplete for pickup
	$("#pickup").autocomplete("/post/get_locations.php", {
		width: 300,
		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 });
    $('#dDate').datepicker({ minDate: 0, dateFormat: 'dd/mm/yy', firstDay: 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);
    });
	$("#pickup, #dropoff").tooltip({ 
		track: true, 
		delay: 0, 
		showURL: false, 
		opacity: 1, 
		fixPNG: true, 
		showBody: " - ", 
		extraClass: "pretty fancy", 
		top: -15, 
		left: 5 
	}); 
});

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($('#pDate').val() != "Click to choose")
  {
       var pData = $('#pDate').val().split("/");
       var pTData = $('#pTime').val().split(":");

    sel_time = new Date();
       sel_time.setYear(pData[2])
       sel_time.setMonth(pData[1]-1);
       sel_time.setDate(pData[0]);
       sel_time.setHours(pTData[0]*1-2);
    sel_time.setMinutes(pTData[1]);

    now = new Date();

       if (sel_time < now)
       {
         alert("Pick up time must be at least 2 hours in advance.");
         return false;
       }
  }



  if (parseInt($("#age").val()) != $("#age").val() )
  {
    alert("Please enter a valid age.")
	return false;
  }

  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(
		"/post/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;
  }

  if($('#pDate').val() == $('#dDate').val() && $("#pTime").val() == $("#dTime").val())
  {
    alert("Drop off date and time are the same as pick up date and time.");
    return false;
  }
  
  return true;
}

