// JavaScript Document

// This function validates the tyre search form (and submits it if it is valid).
function validTyreFinder(f) {
	// Initialise vars
	var $width = "";
	var $ratio = "";
	var $rim = "";
	var $invReasons = new Array;
	var $message = "";
	var $curReason = "";
	
	// Get handles to fields
	$form 	= document.getElementById(f.id);
	$width = document.getElementById("width");
	$ratio = document.getElementById("ratio");
	$rim = document.getElementById("rim");

	// 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 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 = "";
		for ($i=0; $i<$invReasons.length; $i++) {
			$curReason = $invReasons[$i];
			$message +=  $curReason + "\n";
		}
		alert($message);
		$("#txtSearch").keydown();
	// If there are NOT any invReasons
	} else {
		// Submit the form
		$form.submit();
	}
}

// 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", "/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");
	$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 == 1) {
				$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);
	}
}
