/*
This file provides basic form validation for the store, tyre and vehicle searches.
*/



///////////////
// FUNCTIONS //
///////////////

// This function validates the tyre search form (and submits it if it is valid).
function validation_tyreSearch() {
	// Initialise vars
	//var $type = "";
	var $width = "";
	var $ratio = "";
	var $rim = "";
	var $invReasons = new Array;
	var $message = "";
	var $curReason = "";
	
	// Get handles to fields
	//$type = document.getElementById("type");
	$form 	= document.getElementById("searchForm");
	$width = document.getElementById("width");
	$ratio = document.getElementById("ratio");
	$rim = document.getElementById("rim");

	// If a type has NOT been selected...
	/*
	if ($type.value == "") {
		// Add to invReasons
		$invReasons[$invReasons.length] = "Please select a vehicle type";
	}
	*/
	// If a width has NOT been selected...
	if (($width.value == "")||($width.value == "- Select -")) {
		// Add to invReasons
		$invReasons[$invReasons.length] = "Please select a width";
	}
	// If a ratio has NOT been selected...
	if (($ratio.value == "")||($ratio.value == "- Select -")) {
		// Add to invReasons
		$invReasons[$invReasons.length] = "Please select a ratio";
	}
	// If a rim has NOT been selected...
	if (($rim.value == "")||($rim.value == "- Select -")) {
		// Add to invReasons
		$invReasons[$invReasons.length] = "Please select a rim";
	}
	
	// Process (display message or submit)
	$success = validation_process($invReasons, $form);
}
// This function validates the quote request search form (and submits it if it is valid).
function validation_quoteSearch(e) {
	if ($("input:checked", e).length == 0) { 
		alert("Please select at least one tyre to request a quote for"); 
		return false; 
	} else { return true; }
}

// This function accepts a list of invReasons (may be empty) and displays an error message containing all reasons.
// Or, if there are no reasons, it submits the form.
function validation_process($invReasons, $form) {
	// If there are any invReasons...
	if ($invReasons.length > 0) {
		// Construct the message
//		$message = "You have not selected all required fields:";
		$message = "";
		for ($i=0; $i<$invReasons.length; $i++) {
			$curReason = $invReasons[$i];
//			$message = $message + "\n       - " + $curReason;
			$message +=  $curReason + "\n";
		}
		// Display the message
		alert($message);
		$("#txtSearch").keydown();
	// If there are NOT any invReasons
	} else {
		// Submit the form
		$form.submit();
	}
}
