 		var d;
		var ds;
		var PROTECT_CLASS = "protected";
		var PROTECT_TIMEOUT = 100;
		
		$(document).ready(function() {
			
			if (typeof(pickupTime) != 'undefined') {
			  if (pickupTime != "") {
			    $("#pick-up-time").val(pickupTime);
			  } else {
			    $("#pick-up-time").val("10:00AM");
			  }
			}
			if (typeof(dropoffTime) != 'undefined') {
			  if (dropoffTime != "") {
			    $("#drop-off-time").val(dropoffTime);
			  } else {
			    $("#drop-off-time").val("10:00AM");
			  }
			}
			
			$(document).click(function() {
			    $('.popup:not(.' + PROTECT_CLASS + ')').hide();
			});
			
			var autocomplete_options = {
			  width: 280,
			  selectFirst: true,
			  matchContains: true,
			  minChars: 0,
			  mustMatch: false,
			  max: 1000,
				formatMatch: function(row, i, max) {
          return (row[0].match('AIRPORT$')) ? (row[0].replace(' AIRPORT', '')) : row[0];
        },
        sort: function (term, matches) {
          return matches.sort(function(a, b) {
            // Normalize strings.
            a = a.value.toLowerCase();
            b = b.value.toLowerCase();

            // If terms match start, order by alpha.
            if (a.indexOf(term) == 0 && b.indexOf(term) == 0)
              return a - b;

            // Order by location of match if location of a != b
            if (a.indexOf(term) == 0)
              return -1;
            if (b.indexOf(term) == 0)
              return 1;

            // Match is not at the start, so just order by alpha.
            if (a > b)
              return 1;
            if (a < b)
              return -1;
            return 0;
          });
        },
        formatResult: function(row, i, num) {
          return row[0];
        }
			};
			
			if(typeof(locationNames) != 'undefined') {
				$("#pick-up-location").autocomplete(locationNames, autocomplete_options).result(function(){
		      $("#drop-off-location").val($("#pick-up-location").val());
		      $("#drop-off-location").effect('highlight', 500);
				  $("#drop-off-location").trigger('change');
				});
				$("#pick-up-time").change(function(){
			    $("#drop-off-time").val($("#pick-up-time").val());
			    $("#drop-off-time").effect('highlight', 500);
			    $("#drop-off-time").change();
				});
				$("#drop-off-time").change(function(){
					if ($("#pick-up-date").val() == '' || $("#drop-off-date").val() == '') return false;
					if ($("#pick-up-time").val() == '' || $("#drop-off-time").val() == '') return false;
					
 					var dod = $("#drop-off-date").val().split('/');
				  var pud = $("#pick-up-date").val().split('/');
				  dod[0] = parseInt(dod[0],10);
				  dod[1] = parseInt(dod[1],10);
				  pud[0] = parseInt(pud[0],10);
				  pud[1] = parseInt(pud[1],10);
				  var dot = $('#drop-off-time').val().split(':');
				  var put = $('#pick-up-time').val().split(':');
				  
				  if (dot[1].substr(2,2)=='PM' && dot[0] != "12") dot[0] = parseInt(dot[0],10)+12;
				  else if (dot[1].substr(2,2)=='AM' && dot[0] == "12") dot[0] = 0;
				  else dot[0] = parseInt(dot[0],10);
				  dot[1] = parseInt(dot[1].substr(0,2),10);
				  
				  if (put[1].substr(2,2)=='PM' && put[0] != "12") put[0] = parseInt(put[0],10)+12;
				  else if (put[1].substr(2,2)=='AM' && put[0] == "12") put[0] = 0;
				  else put[0] = parseInt(put[0],10);
				  
				  put[1] = parseInt(put[1].substr(0,2),10);
          pickup = new Date();
					pickup.setYear(parseInt(pud[2],10));
					pickup.setDate(parseInt(pud[0],10));
					pickup.setMonth(parseInt(pud[1],10) - 1);
					pickup.setHours(put[0]);
					pickup.setMinutes(put[1]);
					// console.log(pickup);
         	dropoff = new Date();
					dropoff.setYear(parseInt(dod[2],10));
					dropoff.setDate(parseInt(dod[0],10));
					dropoff.setMonth(parseInt(dod[1],10) - 1);
					dropoff.setHours(dot[0]);
					dropoff.setMinutes(dot[1]);
					// console.log(dropoff);
					
          //If they are booking at a funny time warn them
          if (pickup.getHours()<=6||pickup.getHours()>=22 || dropoff.getHours()<=6||dropoff.getHours()>=22) {
            $('#put').html('Note that booking at this time may yield less results');
            $('#put').addClass(PROTECT_CLASS).hide().css({
             top: $(this).offset().top,
             left: $(this).offset().left + $(this).width(),
             position: 'absolute',
             display: 'block'
            }).show('fadeIn');
            setTimeout(function(){
             $("#put").removeClass(PROTECT_CLASS);
            },PROTECT_TIMEOUT);
          }
          
          // if (dropoff.getHours()<=6||dropoff.getHours()>=22) {
          //   $('#ptt').html('Note that booking at this time may yield less results');
          //   $('#ptt').addClass(PROTECT_CLASS).hide().css({
          //     top: $(this).offset().top,
          //     left: $(this).offset().left + $(this).width(),
          //     position: 'absolute',
          //     display: 'block'
          //   }).show('fadeIn');
          //   setTimeout(function(){
          //    $("#ptt").removeClass(PROTECT_CLASS);
          //   },PROTECT_TIMEOUT);
          // }
					
					// If booking greater than 4 days, and airport pickup, show them a 
					// friendly tip.
					$("#drop-off-location").each(function(){
            trip_over_four_days = ((dropoff.getTime() - pickup.getTime()) / 86400000) > 4;
            if ($("#pick-up-location").val().match(/AIRPORT/) && trip_over_four_days){
              popup = $("#drop-off-location").next('.popup');
              popup.addClass(PROTECT_CLASS).hide().css({
                top: $(this).offset().top,
                left: $(this).offset().left + $(this).width(),
                position: 'absolute',
                display: 'block'
              }).show('fadeIn');
              setTimeout(function(){
              popup.removeClass(PROTECT_CLASS);
              }, PROTECT_TIMEOUT);
            }
					});
					
					// If they are booking between 0 and 3 hours more than a 24 hour period show time tip
          // remainder = (dropoff.getTime() - pickup.getTime()) % 86400000;
          // if($("#drop-off-location").next('.popup').is('.' + PROTECT_CLASS)) {
          //  remainder = 0; // disable this popup if we're already showing the drop off tip.
          // }
          // if (remainder > 1000 && remainder < (86400000 - 10800000)) {
          //    $('#ptt').html($('#pttgcb').html());
          //    $('#ptt').addClass(PROTECT_CLASS).hide().css({
          //          top: $(this).offset().top,
          //          left: $(this).offset().left + $(this).width(),
          //          position: 'absolute',
          //          display: 'block'
          //      }).show('fadeIn');
          //  setTimeout(function(){
          //    $('#ptt').removeClass(PROTECT_CLASS);
          //  },PROTECT_TIMEOUT);
          // }
					
					// If they choose a finish date that is before the start date show error
					if (dropoff.getTime() < pickup.getTime()) {
						$('#dod').html('The dropoff date you have chosen is before the pickup day');
						$('#dod').addClass(PROTECT_CLASS).hide().css({
						      top: $(this).offset().top,
						      left: $(this).offset().left + $(this).width(),
						      position: 'absolute',
						      display: 'block'
					    }).show('fadeIn');
						setTimeout(function(){
							$('#dod').removeClass(PROTECT_CLASS);
						}, PROTECT_TIMEOUT);
					}
					
					// If they choose a start date/time which is in the past show an error
					today = new Date();
					
					//if (pickup.getTime() < today.getTime() || dropoff.getTime() < today.getTime()) {
					if (pickup < today || dropoff < today) {
						$('#pud').html('Please choose a pickup day which is in the future');
						$('#pud').addClass(PROTECT_CLASS).hide().css({
						      top: $(this).offset().top,
						      left: $(this).offset().left + $(this).width(),
						      position: 'absolute',
						      display: 'block'
					    }).show('fadeIn');
						setTimeout(function(){
							$('#pud').removeClass(PROTECT_CLASS);
						}, PROTECT_TIMEOUT);
					}
					return true;
				});
				$("#drop-off-location").autocomplete(locationNames, autocomplete_options);
	      $("#live-in").click(function(){
	        if ($("#live-in").val() == '') {
	          $("#live-in").val('mAustralia');
	          $("#live-in").focus();
	          $("#live-in").select();
	        }
	      });
			}
			
			if(typeof(countryNames) != 'undefined') {
				$("#live-in").autocomplete(countryNames, {
				  width: 280,
				  selectFirst: true,
				  matchContains: true,
				  minChars: 0,
				  mustMatch: true
				});
			}
			
			$("#compare").click(function() {
			  
			  if (jQuery.inArray($('#pick-up-location').val(), locationNames) == -1) {
			    $('#pick-up-location').val("");
			  }
			  if (jQuery.inArray($('#drop-off-location').val(), locationNames) == -1) {
			    $('#drop-off-location').val("");
			  }
			  
				if (error_field('#pick-up-location', 'Please enter a pickup location')
				   && error_field('#drop-off-location', 'Please enter a dropoff location')
				   && error_field('#pick-up-date', 'Please enter a pickup date')
				   && error_field('#pick-up-time', 'Please enter a pickup time')
				   && error_field('#drop-off-date', 'Please enter a dropoff date')
				   && error_field('#drop-off-time', 'Please enter a dropoff time')
				   && error_field('#live-in', 'Please enter your country')
				) 	      $("#resform").submit();  
			});
			
			function error_field(selector, tip) {
				if ($(selector).val() == '') {
					$(selector).effect('highlight', {}, 1500);
				  $(selector).after('<span class="popup">'+tip+'</span>');
				  $(selector).focus();
				  return false;
				}
				return true;
			}
			
			$("#live-in").change(function() {
				if ($("#live-in").val()!='Australia'&&$('#live-in').val()!=''){
				  $("#live-in").addClass(PROTECT_CLASS).next('.popup').hide().css({
				      top: $(this).offset().top,
				      left: $(this).offset().left + $(this).width(),
				      position: 'absolute',
				      display: 'block'
				    }).show('fadeIn');
					setTimeout(function(){
						$('#live-in').next('popup').removeClass(PROTECT_CLASS);
					}, PROTECT_TIMEOUT);
				}
			});
			
			$("#over-25").click(function() {
				el = this;
				if (!$(el).is(':checked')){
					popup = $(el).next('.popup');
				  popup.addClass(PROTECT_CLASS).hide().css({
				      top: $(el).offset().top,
				      left: $(el).offset().left + $(el).width(),
				      position: 'absolute',
				      display: 'block'
				    }).show();
					setTimeout(function(){
						popup.removeClass(PROTECT_CLASS);
					},PROTECT_TIMEOUT);
				} else {
					$(el).next('.popup').stop().hide();
				}
			});
			
			$('#pick-up-location').focus();
			
			function navigate(e) {
			  
        if(e && e.keyCode && e.keyCode == 13) $("#compare").click();
      }
      document.onkeypress=navigate;
    
		});
