// Generic form functions / events / behaviors
$(document).ready(function() {

	var el=$(".text_default:first").parents('form');
	$(el).bind('submit', function(){ return check_fields() });

	update_fields();
	
	$('#search_ride_date').attr('autocomplete','off');
	
	$('a.tip.clicktip').click(function() {
      $(this).removeClass('clicktip');
      $(this).blur( function() { $(this).addClass('clicktip')});
      return false;
    });

});

// This makes this global and callable from other functions
var update_fields = function(){
	// Look for any text fields that should maintain some default value
	$(".text_default").each(function(){
		// This is a weird check because if we use override, then we could have an empty 
		// field (in the case of an error on a reload)..
		if($(this).val() == ''){
			$(this).addClass('text-input-help');
			$(this).val($(this).attr('refid'));
		}
	});
	$(".text_default").blur(function(){
		if($(this).val() == ''){
			$(this).addClass('text-input-help');
			$(this).val($(this).attr('refid'));
		}
	});
	$(".text_default").bind('focus', function(){
		if($(this).val() == $(this).attr('refid')){
			$(this).removeClass('text-input-help');
			$(this).val('');
		}
		return true;
	});
	// Hook up the text_default form button to clear the text_default fields so the vals don't get submitted
	var el=$(".text_default:first").parents('form');
	

	/* NC: rather than put function with the submit button, bind to onSubmit event for the form, which is more reliable
	el.find('input').each( function(n,i){
		if(jQuery.trim($(i).attr('type'))=='submit'){
			$(i).click( function(){
				clear_all_text_default_fields();
			});
		}
	});
	*/
	$(el).bind('submit', function(){ clear_all_text_default_fields() });

}

// Goes through all the text_default input boxes and clears their default text, if it exists.
function clear_all_text_default_fields(){
	$(".text_default").each(function(){
		if($(this).val() == $(this).attr('refid')){$(this).val('');}
	});
}
	
	
function check_fields(){

  document.getElementById('search_submit').disabled=false;


/*
  if (document.getElementById('service_type').value == "")
  {
    alert("Please Select Service Type");
	document.getElementById('service_type').focus();
    return (false);
  }

  if (document.getElementById('search_pax').value == "")
  {
    alert("Please enter the number of passengers");
	document.getElementById('search_pax').focus();
    return (false);
  }

  if (document.getElementById('search_ride_date').value == "")
  {
    alert("Please enter a Date");
	document.getElementById('search_ride_date').focus();
    return (false);
  }

  if (document.getElementById('search_pickup_time').value == "")
  {
    alert("Please enter a Date");
	document.getElementById('search_pickup_time').focus();
    return (false);
  }
*/	

	var alertmsg = '';
	var errfields = new Array();
	var extraerr = '';
	
	svctype = $('#service_type').val();
	sdop = $('#search_drop_off_place').val();
	spup = $('#search_pickup_place').val();
	if (svctype=='')
	{ 
		alertmsg += " 'Type of Service'\n";
		errfields.push('#service_type');
    }
	if ($('#search_pax').val()=='')
	{
	    alertmsg += " 'Passengers'\n";
		errfields.push('#search_pax');
	}
	if ($('#search_ride_date').val()=='' || $('#search_ride_date').val()=='Select Date')
	{
		alertmsg += " 'Date of service'\n";
		errfields.push('#search_ride_date');
	}
	if ($('#search_pickup_time').val()=='')
	{
		alertmsg += " 'Time of Pickup'\n";
		errfields.push('#search_pickup_time');
	}
	if ( $('#search_drop_off_time').val()=='' && (svctype!="99" && svctype!="100" && svctype!="101" && svctype!="102") )
	{
		alertmsg += " 'Expected drop-off Time'\n";
		errfields.push('#search_drop_off_time');
		
	}

	if (spup==''||spup=='Address, City, or Zip'||spup=='Airport Name, Code or City')
	{
		alertmsg += " 'Pick-up address'\n";
		errfields.push('#search_pickup_place');
	}
	if ((svctype=="99"||svctype=="100"||svctype=="101"||svctype=="102"||svctype=="131072") && (sdop==''||sdop=='Address, City, or Zip'||sdop=='Airport Name, Code or City')) 
	{
		alertmsg += " 'Drop-off address'\n";
		errfields.push('#search_drop_off_place');
	}

	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 && !confirm( 'You have selected a ride duration of '+timediff+' hours. Is this correct?\n\nClick OK to continue.' ) )
		{
	 	   alertmsg += " 'Time of Pickup'\n";
		   errfields.push('#search_pickup_time');
		   extraerr = "<br/>Please select a valid time span."
		}
	}
   
	if (alertmsg!='' && ucsv == 2 ) {
		$('.formError').remove(); /* clear out old errors */
		$('.errorRed').removeClass('errorRed');
		
		msg = "<div class='formError'><strong>Please provide all information below for accurate results.</strong>"+extraerr+"</div>";
		$(msg).prependTo('#homeFormTop');
		$(errfields.toString()).each( function(){ 
			$(this).parent().addClass('errorRed'); 
			$(this).change(function(){
				$(this).parent().removeClass('errorRed');
			});
		});
		return false;
	}
	else if (alertmsg!='') {
		alert('Please complete all of the following fields:\n\n'+alertmsg);
		return false;
	}

  return true;

}
	
	
