$(function() {

	if (!($.browser.safari && $.browser.version < 500))
      $('.form-date').datepicker();

	// Disable drop off times and locations for air/cruiseports, enable only for hourly
	$('#service_type').change(function(){
		show_hide_drop_offs();
		show_hide_drop_location();
		change_labels();
	});
	show_hide_drop_offs();
	show_hide_drop_location();
	change_labels();
	
	$('#search_user_tz').val((new Date()).getTimezoneOffset()/60);   // submit user's time zone

  $('#search_pickup_time').change(function() {
   changeTimeSlot();
  });

    $('#search_form').submit(function(){
      formvalid = validate_form();
	  if(formvalid==false) {
	    return false;	
   	    $('#search_submit').attr('enabled', 'true');
	  }
   	  $('#search_submit').attr('disabled', 'true');
	  $('#search_submit').attr('value','Now Searching...');
    });

    $('#contact_information').submit(function(){
      formvalid = validate_contact();
	    if(formvalid==false) {
     	  $('#contact_submit').attr('enabled', 'true');
	      return false;	
	    }
   	  $('#contact_submit').attr('disabled', 'true');
	    $('#contact_submit').attr('value','Please Wait...');
    });

    $('#search_limos').submit(function(){
  	  formvalid = validate_search();
	    if(formvalid==false) {
  	    $('#search_submit').attr('enabled', 'true');
	      return false;	
	    }
   	  $('#search_submit').attr('disabled', 'true');
	    $('#search_submit').attr('value','Now Searching...');
    });

});

$(window).unload(function() {
    $('#search_submit').removeAttr('disabled');
    $('#search_submit').removeAttr('disabled');
    $('#contact_submit').removeAttr('disabled');
});

function show_hide_drop_offs() {
	var val=$('#service_type :selected').val();
	if(val == 99 || val == 100 || val == 101 || val == 102 || val==''){
	  $('#search_between_time_div').hide();
      $('#search_dropoff_time_div').hide();
	}else{
	  $('#search_between_time_div').show();
      $('#search_dropoff_time_div').show();
	}
}

function show_hide_drop_location() {
	var val=$('#service_type :selected').val();
	if(val == 99 || val == 100 || val == 101 || val == 102 || val == 131072){
		$('#search_dropoff_location_div').show();
	}else{	
		$('#search_dropoff_location_div').hide();
	}
}

function change_labels(only_labels) {
	// Any of the direct clearing of the value attributed on the drop off and pickup places 
	// will erase anything the user has added.
	var val=$('#service_type :selected').val();
	// All the undefined checks allow us to segment off the onload vs a event call. When onload
	// happens (form submit or page load), we only want to change the text labels - this will 
	// allow and Airport To to have the labels updated correctly. Direct calls do not pass in 
	// the only_labels flag and therefore, blow away the values in the text field(s) directly.
	// Note: Don't forget that this works in conjunction with forms:update_fields
	if(val == 100) {
		// to airport
		$('#pickup_location_label').text('Pick-up Zip Code');
		$('#drop_off_location_label').text('Airport Code');
	} else if ( val == 99 ) {
		// from airport
		$('#pickup_location_label').text('Airport Code');
		$('#drop_off_location_label').text('Drop-off Zip Code');
	} else if (val == 102) {
		// to cruise port
		$('#pickup_location_label').text('Pick-up Zip Code');
		$('#drop_off_location_label').text('Cruiseport');
	} else if (val == 101) {
		// from cruise port
		$('#pickup_location_label').text('Cruiseport');
		$('#drop_off_location_label').text('Drop-off Zip Code');
	} else {
		$('#pickup_location_label').text('Pick-up Zip Code');
		$('#drop_off_location_label').text('Drop-off Zip Code');
	}
}

//If first time is set, change second time to an hour later
function changeTimeSlot() {
  var index_offset = 4; // hour increments
  
  // put 6 hours for prom
  if( $('#service_type').val() == '16384' ){
    index_offset = 6;
  }
  var hs = $('#search_pickup_time').attr('selectedIndex');
  var et = $('#search_drop_off_time').attr('selectedIndex');
  if (hs <= 48) {
  var do_time_idx = hs+index_offset;
  if (do_time_idx>48) {do_time_idx=48;}
  $('#search_drop_off_time').attr('selectedIndex', do_time_idx);
  } else {
  $('#search_drop_off_time').attr('selectedIndex', hs-48);
  }
}

function validate_form() {
	var alertmsg = '';
	if ($('#search_pax').val()=='') alertmsg += "* Number of Passengers \n";
	if ($('#search_ride_date').val()=='') alertmsg += "* Date of Service \n";
	if ($('#search_pickup_time').val()=='') alertmsg += "* Pick-up Time \n";
	if ($('#search_pickup_place').val()=='') alertmsg += "* Pick-up Location \n";

	if (alertmsg!='') {
		alert('Please complete all fields: \n\n'+alertmsg);
        return false;
	}
    return true;
}

function validate_search() {
	var alertmsg = '';
  	svctype = $('#service_type').val();
	sdop = $('#search_drop_off_place').val();
	spup = $('#search_pickup_place').val();
	if (svctype=='') alertmsg += "* Type of Service \n";
	if ($('#search_pax').val()=='') alertmsg += "* Number of Passengers \n";
	if ($('#search_ride_date').val()=='') alertmsg += "* Date of Service \n";
	if ($('#search_pickup_time').val()=='') alertmsg += "* Pickup Time \n";
	if ($('#search_drop_off_time').val()=='' && svctype!="99" && svctype!="100" && svctype!="101" && svctype!="102") alertmsg += "* Drop-off Time \n";
	if (spup=='') alertmsg += "* Pick-up Location \n";
	if ((svctype=="99"||svctype=="100"||svctype=="101"||svctype=="102"||svctype=="131072") && (sdop=='')) alertmsg += "* Drop-off Location \n";
  if ( alertmsg=='' && svctype!="99" && svctype!="100" && svctype!="101" && svctype!="102" ) {
    var drophour = parseInt($('#search_drop_off_time').val().substring(0,2),10);
    if( $('#search_drop_off_time').val().substring(3)=="30" ) drophour += 0.5;
      var pickhour = parseInt($('#search_pickup_time').val().substring(0,2),10);
    if( $('#search_pickup_time').val().substring(3)=="30" ) pickhour += 0.5;
      var timediff = drophour - pickhour;
    if( timediff < 0 ){ timediff += 24; /* account for times spanning midnight */ }
    if( timediff >= 12 ) {
      alertmsg += " 'Time of Pickup'\n";
    }
  }


	if (alertmsg!='') {
		alert('Please complete all fields: \n\n'+alertmsg);
        return false;
	}
    return true;
}

function validate_contact() {
	var alertmsg = '';
	if ($('#first_name').val()=='') alertmsg += "* First Name \n";
	if ($('#last_name').val()=='') alertmsg += "* Last Name \n";
	if ($('#email_address').val()=='') alertmsg += "* Email Address \n";

	if (alertmsg!='') {
		alert('Please complete all fields: \n\n'+alertmsg);
        return false;
	}
    return true;
}

