// JavaScript Document

document.write('<script type="text/javascript" src="/js/ajax.js"></script>');

var ajaxProductSearch =
{
	// Ajax Config
	config:
	{
		reqFile:"/includes/ajax_product_search.php"
	},
	
	// Ajax Requests
	requests:
	{
		getSubCategories:function(object)
		{
			if (object.selectedIndex > 0)
			{
				new ajax
				({
					script : ajaxProductSearch.config.reqFile,
					method : 'post',
					get_vars : ['action=getSubCategories'],
					post_vars : ['categoryID=' + object.value],
					callback : ajaxProductSearch.callbacks.setSubCategories
				});
			}
		},
		
		getSuppliers:function(object)
		{
			if (object.selectedIndex > 0)
			{
				if (object.value == '22' || object.value == '23')
				{
					location.href = 'products.php?categoryID='+$_element("id", "psCategory").value+'&subCategoryID='+object.value;
				}
				else
				{
					new ajax
					({
						script : ajaxProductSearch.config.reqFile,
						method : 'post',
						get_vars : ['action=getSuppliers'],
						post_vars : ['subCategoryID=' + object.value],
						callback : ajaxProductSearch.callbacks.setSuppliers
					});
				}
			}
		}
	},
	
	// Ajax Callbacks
	callbacks:
	{
		setSubCategories:function(response)
		{
			if (response != "no-response")
			{
				$_element("id", "psSubCategory").options.length=1;
				
				var data = response.split("^");
				
				for(var i=0; i<data.length; i++)
				{
					$_element("id", "psSubCategory").options[i+1]=new Option(data[i].split(",")[0], data[i].split(",")[1]);
				};
				
				$_element("id", "psSubCategory").disabled = false;
				$_element("id", "psSubCategory").options[0].selected = "selected";
				$_element("id", "psSubCategory").focus();
			}
			else
			{
				alert('No sub categories could be found.');
			};
		},
		
		setSuppliers:function(response)
		{
			if (response != "no-response")
			{
				$_element("id", "psSupplier").options.length=1;
				
				var data = response.split("^");
				
				for(var i=0; i<data.length; i++)
				{
					$_element("id", "psSupplier").options[i+1]=new Option(data[i].split(",")[0], data[i].split(",")[1]);
				};
				
				$_element("id", "psSupplier").disabled = false;
				$_element("id", "psSupplier").options[0].selected = "selected";
				$_element("id", "psSupplier").focus();
			}
			else
			{
				//alert('No suppliers could be found.');
			};
		}
	}
}