// ProductSearch class

// Creates the new object
var ProductSearch;
if (!ProductSearch) ProductSearch = {}
else if (typeof ProductSearch != "object") throw new Error("'ProductSearch' already exists and is not an object");

// Declare properties
ProductSearch.ajaxURL = "/ajax/search.aspx";
ProductSearch.progressWheel = "/images/icons/progress_blue.gif";
ProductSearch.timer = null;
ProductSearch.typeResult = null;
ProductSearch.sizeResult = null;
ProductSearch.qtyResult = null;

// Sends an AJAX call to the server to get product data
ProductSearch.getInfo = function() {
	var form = document.ProductSearch;
	if (form) {
		if (ProductSearch.timer != null) {
			clearTimeout(ProductSearch.timer);
			ProductSearch.timer = null;
		}
		if (request.readyState == 0 || request.readyState == 4) {
			var params = "";
			if (form.k) params += "k=" + escape(form.k.value) + "&";
			if (form.t) params += "t=" + escape(form.t.value) + "&";
			if (form.t) params += "s=" + escape(form.s.value) + "&";
			request.open("POST", ProductSearch.ajaxURL, true);
			request.onreadystatechange = function() { ProductSearch.updateInfo(); }
			request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			request.send(params + "a=q");
		} else ProductSearch.timer = setTimeout(ProductSearch.getInfo, 0);
	} else alert("Cannot get product information due to missing variables in AJAX post.");
}

// Event that fires once the product data is received
ProductSearch.updateInfo = function() {
	var status = document.getElementById("FindStatus");
	ProductSearch.showProgressWheel();
	if (request.readyState == 4) {
		var requery = false;
		if (request.status == 200) {
			// Success - Process response
			ProductSearch.typeResult = null;
			ProductSearch.sizeResult = null;
			ProductSearch.qtyResult = null;
			eval(request.responseText);
			requery = ProductSearch.processResults();
		} else if (request.status == 501) {
			var msg = "Data Query Error";
			if (status) {
				replaceText(status, msg);
				status.style.color = '#CC0000';
			} else alert(msg);
		} else {
			var msg = "Communications Error";
			if (status) {
				replaceText(status, msg);
				status.style.color = '#CC0000';
			} else alert(msg);
		}
		request = setupNewAjaxRequest();
		// Initiate another query if the list of options have changed
		if (requery) ProductSearch.getInfo();
	}
}

// Processes the results from the server
ProductSearch.processResults = function() {
	var form = document.ProductSearch;
	var status = document.getElementById("FindStatus");
	var requery = false;
	// Process product types
	if (form) {
		if (form.t) {
			var tmpval = form.t.value;
			if (ProductSearch.typeResult != null) {
				var rs = ProductSearch.typeResult.ProductType;
				if (rs.length > 0) {
					var tmptext = form.t.options[form.t.selectedIndex].text;
					form.t.options.length = 0;
					for (var x = 0; x < rs.length; x++) form.t.options.add(new Option(rs[x].Description, rs[x].ID));
					form.t.options.add(new Option("Product Type", ""), 0);
					form.t.style.fontStyle = 'normal';
					selectDropDownItem(tmptext, form.t, "text");
				} else {
					form.t.options.length = 0;
					form.t.options.add(new Option("Nothing Available", ""), 0);
					form.t.selectedIndex = 0;
					form.t.style.fontStyle = 'italic';
				}
			} else {
				form.t.options.length = 0;
				form.t.options.add(new Option("Nothing Available", ""), 0);
				form.t.selectedIndex = 0;
				form.t.style.fontStyle = 'italic';
			}
			// If this value changed inadvertently, flag a requery
			if (form.t.options[form.t.selectedIndex].value != tmpval) requery = true;
		}
	
		// Process product sizes
		if (form.s) {
			var tmpval = form.s.value;
			if (ProductSearch.sizeResult != null) {
				var rs = ProductSearch.sizeResult.ProductSize;
				var tmptext = form.s.options[form.s.selectedIndex].text;
				form.s.options.length = 0;
				for (var x = 0; x < rs.length; x++) form.s.options.add(new Option(rs[x].Description, rs[x].ID));
				form.s.options.add(new Option("All Sizes", ""), 0);
				selectDropDownItem(tmptext, form.s, "text");
			} else {
				form.s.options.length = 0;
				form.s.options.add(new Option("All Sizes", ""));
				form.s.selectedIndex = 0;
			}
			// If this value changed inadvertently, flag a requery
			if (form.s.options[form.s.selectedIndex].value != tmpval) requery = true;
		}
	
		// Process matched products
		if (status && ProductSearch.qtyResult != null) {
			var matches = parseInt(ProductSearch.qtyResult.ProductQty.NumMatches);
			if (matches == 0) {
				status.style.color = '#CC0000';
				replaceText(status, "No matching products found");
			} else if (matches == 1) {
				status.style.color = '';
				replaceText(status, "1 matching product found");
			} else {
				status.style.color = '';
				replaceText(status, matches + " matching products found");
			}
		} else if (status) {
			replaceText(status, "");
		}
	}
	return requery;
}

// Validates the product search form
ProductSearch.validateForm = function() {
	var form = document.ProductSearch;
	var status = document.getElementById("FindStatus");
	if (form && status) {
		if (form.t.selectedIndex == 0) {
			status.style.color = '#CC0000';
			replaceText(status, "Please select a product type");
			return false;
		} else {
			return true;
		}
	} else return true;
}

// Shows the progress wheel image in place of the status text
ProductSearch.showProgressWheel = function() {
	var status = document.getElementById("FindStatus");
	if (status) {
		while (status.firstChild) status.removeChild(status.firstChild);
		var img = document.createElement("img");
		img.src = ProductSearch.progressWheel;
		img.width = 16;
		img.height = 16;
		status.appendChild(img);
	}
}

ProductSearch.onLoad = function() {
	var form = document.ProductSearch;
	if (form) {
		form.onsubmit = function() { return ProductSearch.validateForm(); };
		if (form.k) form.k.onchange = ProductSearch.getInfo;
		if (form.t) form.t.onchange = ProductSearch.getInfo;
		if (form.s) form.s.onchange = ProductSearch.getInfo;
	}
	preloadImages(ProductSearch.progressWheel);
}


if (window.addEventListener) window.addEventListener("load", ProductSearch.onLoad, false);
else if (window.attachEvent) window.attachEvent("onload", ProductSearch.onLoad);