//////////////////////
// FILE DESCRIPTION //
//////////////////////

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



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

// This function validates the store search form (and submits it if it is valid).
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your Browser is not compatible!");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();

function validation_storeSearch() {
	var $region = ""
	
	$region = document.getElementById("txtSearch");
	
		if (searchReq.readyState == 4 || searchReq.readyState == 0) {
			var e = escape($region.value);
			searchReq.open("GET", "/ATF/includes/validationStore.php" + '?q=' + e, true);
			searchReq.onreadystatechange = handleResults; 
			searchReq.send(null);
		}	
}

//Called when the AJAX response is returned.
function handleResults() {
//	$form 	= document.getElementById("formFindStore");
	$form = $("form[autocomplete=off]");
	$region = document.getElementById("txtSearch").value;
	var $invReasons = new Array;
		if (searchReq.readyState == 4) {
			if ($region == '') {
				$invReasons[$invReasons.length] = 'Please, enter a placename.';
			} else {
			if (searchReq.responseText == 3) {
				$invReasons[$invReasons.length] = 'Sorry, we could not find "'+$region+'" in our database.\n Please ensure that you\'ve spelt this correctly, or try another placename.';
			}
			if (searchReq.responseText == 2) {
				$invReasons[$invReasons.length] = 'Sorry, but there is more than one "'+$region+'" in our database.\n Please select an option from the list under the suburb field.';
			}
		}
	$success = validation_process($invReasons, $form);
	}
}

// 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();
	}
}
