function checkDiscountCode() {

	var getURL;
	
	function handleResponse() {
		if (http.readyState == 4) {
			var response = http.responseText;
			document.getElementById('discount_code_response_row').style.display = '';
			document.getElementById('discount_code_response').innerHTML = response;
			http = null;
		}
	}
	
	function doAjaxRequest() {	
		// setup the ajax object, define the response handler and send the data
		http = createRequestObject();
		http.open('GET', getURL);
		http.onreadystatechange = handleResponse;
		http.send(null);
	}
	
	if (document.getElementById('discount_code')) {
	
		var discount_code = document.getElementById('discount_code').value;
		
		if (discount_code.length == 0) {
			alert('Please enter a discount code.');
			return false;
		}
		
		var getURL = getBaseURL() + 'inc/ajax-check-discount-code.php?code=' + escape(discount_code);
		doAjaxRequest();
	}
	
}

function querystring(name) { // returns a named value from the querystring
   var tmp = (location.search.substring(1));
   var i = tmp.toUpperCase().indexOf(name.toUpperCase()+"=");

   if (i >= 0) {
      tmp = tmp.substring( name.length+i+1 );
      i = tmp.indexOf("&");
      return unescape( tmp = tmp.substring( 0, (i>=0) ? i : tmp.length ));
   }

   return("");
}

function createRequestObject() {
	var ro;
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer") {
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		ro = new XMLHttpRequest();
	}
	return ro;
}

function getSubCategories() {

	// some default values, these need global scope
	var cat = 0;
	var subcat = 0;
	var getURL = '';
	var updateID = '';
	var http;
	
	// specific to this site, grab the cat ID
	if (document.getElementById('cat_id')) {
		cat = document.getElementById('cat_id').value;
	}
	
	// grab the subcat from the URL
	var subcatValue = querystring("subcat");
	if (subcatValue.length > 0) {
		subcat = subcatValue;
	}
	
	// ooh, nested functions. Isn't javascript clever ;-)
	
	function handleResponse() {
		// updates element on page with HTML from server-side script (should be XML?...)
		if (http.readyState == 4) {
			var response = http.responseText;
			if (document.getElementById(updateID)) {
				document.getElementById(updateID).innerHTML = response;
			}
			http = null; // don't need this anymore, null so we can create another
		} else {
			//alert('status '+ http.status + '. readyState ' + http.readyState);
		}
	}
	
	function doAjaxRequest() {	
		// setup the ajax object, define the response handler and send the data
		http = createRequestObject();
		http.open('GET', getURL);
		//http.setRequestHeader('Content-Length', '52282');
		//alert('content-length ' + http.getResponseHeader('Content-Length'));
		http.onreadystatechange = handleResponse;
		http.send(null);
	}
	
	// the ID of the element to update
	updateID = 'subcategories';
	// the serverside script to handle processing
	getURL = getBaseURL() + 'inc/ajax-subcategories.php?cat=' + cat + '&subcat=' + subcat;
	//alert(getURL);
	// do the ajax thing
	doAjaxRequest();
	
}

function doAjax(module, action) {

	// some default values, these need global scope
	var cat = 0;
	var getURL = '';
	var updateID = '';
	var http;
	
	// specific to this site, grab the product type ID
	if (document.getElementById('catID')) {
		cat = document.getElementById('catID').value;
	}
	
	// ooh, nested functions. Isn't javascript clever ;-)
	
	function handleResponse() {
		// updates element on page with HTML from server-side script (should be XML?...)
		if (http.readyState == 4) {
			var response = http.responseText;
			if (document.getElementById(updateID)) {
				document.getElementById(updateID).innerHTML = response;
			}
			http = null; // don't need this anymore, null so we can create another
		}
	}
	
	function doAjaxRequest() {	
		// setup the ajax object, define the response handler and send the data
		http = createRequestObject();
		http.open('post', getURL);
		http.onreadystatechange = handleResponse;
		http.send(null);
	}
	
	// switch statement sets up params for each module
	switch (module) {
			
		// colors module
		case 'colours' :
			var newColour = '';
			// get the new colour
			if (document.getElementById('newColour')) {
				newColour = document.getElementById('newColour').value;
			}
			// the ID of the element to update
			updateID = 'colours';
			// the serverside script to handle processing
			getURL = 'ajax-colours.php?action=' + action + '&cat=' + cat + '&colour=' + newColour;
			// clear the input
			if (document.getElementById('newColour')) {
			document.getElementById('newColour').value = '';
			}
			// do the ajax thing
			doAjaxRequest();
			break;
			
		// sizes modules
		case 'sizes' :
			var newSize = '';
			// get the new size
			if (document.getElementById('newSize')) {
				newSize = document.getElementById('newSize').value;
			}
			// the ID of the element to update
			updateID = 'sizes';
			// the serverside script to handle processing
			getURL = 'ajax-sizes.php?action=' + action + '&cat=' + cat + '&size=' + newSize;
			// reset the field
			if (document.getElementById('newSize')) {
				document.getElementById('newSize').value = '';
			}
			// do the ajax thing
			doAjaxRequest();
			break;
	}
	
}


