// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function setText(id,txt) {
    document.getElementById(id).innerHTML = txt;
}
function clearText(thefield){
	if (thefield.defaultValue==thefield.value)
		thefield.value = ""
}

// ==================================================
// Function: validateSearch()
// Input: errorField - div to display error
// Purpose: validates search fields
// ==================================================
function validateSearch(form,errorField) {
	// Default values for Destination field: "Landmark or Place"
	// Default values for Address field: "City, State or Zip"
	var destination_label = "";
	var address_label     = "Enter a city, state or zipcode";

	form = "#" + form;
	
  // Clear default labels before validation
	if ($(form + ' #search_dest_name').val() == destination_label) {
		$(form + ' #search_dest_name').val('');
	}
	/*
	if ($(form + ' #search_dest_address').val() == address_label) {
		$(form + ' #search_dest_address').val('');
	}
	*/
	
	// Validate city selection
	if ( $(form + ' #search_city_id').val() == '') {
	   setText(errorField,'Please select a City');
	   return false;		
	}
	
	// If fields are empty, then reset initial values
	if ($(form + ' #search_dest_name').length > 0) {
		//if ( $(form + ' #search_dest_name').val() == '' && $(form + ' #search_dest_address').val() == '') {
		if ( $(form + ' #search_dest_name').val() == '' ) {	
			 //$(form + ' #search_dest_name').val(destination_label);
			 //$(form + ' #search_dest_address').val(address_label);
			 //$(form + ' #search_dest_name').css('color','#aaa');
			 //$(form + ' #search_dest_address').css('color','#aaa');
		   setText(errorField,'Where do you want to park?');
		   return false;		
		}
	}
	// Validate arrival day/time
	if ( $(form + ' #arrivalDay').val() == '') {
	   setText(errorField,'Please enter Arrival Day');
	   return false;		
	}
	if ( $(form + ' #search_arrival_hour').val() == '') {
	   setText(errorField,'Please enter Arrival Time');
	   return false;
	}
	// validate departure day/time
	if ( $(form + ' #departureDay').val() == '') {
	   setText(errorField,'Please enter Departure Day');
	   return false;		
	}
	if ( $(form + ' #search_departure_hour').val() == '') {
	   setText(errorField,'Please enter Departure Time');
	   return false;
	}
	
	// Validate Arrival is less than Departure date
	var arrivalDate   = new Date($(form + ' #arrivalDay').val());
	var departureDate = new Date($(form + ' #departureDay').val());
	
	arrivalDate = arrivalDate.getTime();
	departureDate = departureDate.getTime();
	
	if (arrivalDate > departureDate) {
		setText(errorField,'Departure cannot be less than Arrival');
		return false;
	}
	if(arrivalDate == departureDate) {
		arrival_time   = $(form + ' #search_arrival_time').val();
		departure_time = $(form + ' #search_departure_time').val();
		
		a = parseInt(arrival_time.split(":")[0]);
		d = parseInt(departure_time.split(":")[0]);
		
		if ( a > d ) {
			setText(errorField,'Departure cannot be less than Arrival');
			return false;
		}
		if ( a == d ) {
			// hour is equal, check minutes
			a = parseInt(arrival_time.split(":")[1]);
			d = parseInt(departure_time.split(":")[1]);
			if ( a >= d ) {
				setText(errorField,'Departure cannot be less than Arrival');
				return false;
			}
		}
	}
	setText(errorField,'');
	return true;
}