function ShowImage() {
	/* 
	Shows an animated spinny wheel thingy ala osx
	Version 1.0 | by peter graves
	*/
	pwimage = document.getElementById("imgRing");
	frmbutton = document.getElementById("Submit");
	frmbutton.style.visibility = 'hidden';
	pwimage.style.visibility = "visible"; 
	pwimage.style.display= "inline";
	setTimeout('pwimage.src = "/images/loading.gif"', 200); 
	}
	
	/* Simple AJAX Code-Kit (SACK) v1.6.1 */
	/* ©2005 Gregory Wild-Smith */
	/* www.twilightuniverse.com */
	/* Software licenced under a modified X11 licence,
	   see documentation or authors website for more details */
	
	function sack(file) {
		this.xmlhttp = null;
	
		this.resetData = function() {
			this.method = "POST";
			this.queryStringSeparator = "?";
			this.argumentSeparator = "&";
			this.URLString = "";
			this.encodeURIString = true;
			this.execute = false;
			this.element = null;
			this.elementObj = null;
			this.requestFile = file;
			this.vars = new Object();
			this.responseStatus = new Array(2);
		};
	
		this.resetFunctions = function() {
			this.onLoading = function() { };
			this.onLoaded = function() { };
			this.onInteractive = function() { };
			this.onCompletion = function() { };
			this.onError = function() { };
			this.onFail = function() { };
		};
	
		this.reset = function() {
			this.resetFunctions();
			this.resetData();
		};
	
		this.createAJAX = function() {
			try {
				this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e1) {
				try {
					this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e2) {
					this.xmlhttp = null;
				}
			}
	
			if (! this.xmlhttp) {
				if (typeof XMLHttpRequest != "undefined") {
					this.xmlhttp = new XMLHttpRequest();
				} else {
					this.failed = true;
				}
			}
		};
	
		this.setVar = function(name, value){
			this.vars[name] = Array(value, false);
		};
	
		this.encVar = function(name, value, returnvars) {
			if (true == returnvars) {
				return Array(encodeURIComponent(name), encodeURIComponent(value));
			} else {
				this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
			}
		}
	
		this.processURLString = function(string, encode) {
			encoded = encodeURIComponent(this.argumentSeparator);
			regexp = new RegExp(this.argumentSeparator + "|" + encoded);
			varArray = string.split(regexp);
			for (i = 0; i < varArray.length; i++){
				urlVars = varArray[i].split("=");
				if (true == encode){
					this.encVar(urlVars[0], urlVars[1]);
				} else {
					this.setVar(urlVars[0], urlVars[1]);
				}
			}
		}
	
		this.createURLString = function(urlstring) {
			if (this.encodeURIString && this.URLString.length) {
				this.processURLString(this.URLString, true);
			}
	
			if (urlstring) {
				if (this.URLString.length) {
					this.URLString += this.argumentSeparator + urlstring;
				} else {
					this.URLString = urlstring;
				}
			}
	
			// prevents caching of URLString
			this.setVar("rndval", new Date().getTime());
	
			urlstringtemp = new Array();
			for (key in this.vars) {
				if (false == this.vars[key][1] && true == this.encodeURIString) {
					encoded = this.encVar(key, this.vars[key][0], true);
					delete this.vars[key];
					this.vars[encoded[0]] = Array(encoded[1], true);
					key = encoded[0];
				}
	
				urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
			}
			if (urlstring){
				this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
			} else {
				this.URLString += urlstringtemp.join(this.argumentSeparator);
			}
		}
	
		this.runResponse = function() {
			eval(this.response);
		}
	
		this.runAJAX = function(urlstring) {
			if (this.failed) {
				this.onFail();
			} else {
				this.createURLString(urlstring);
				if (this.element) {
					this.elementObj = document.getElementById(this.element);
				}
				if (this.xmlhttp) {
					var self = this;
					if (this.method == "GET") {
						totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
						this.xmlhttp.open(this.method, totalurlstring, true);
					} else {
						this.xmlhttp.open(this.method, this.requestFile, true);
						try {
							this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
						} catch (e) { }
					}
	
					this.xmlhttp.onreadystatechange = function() {
						switch (self.xmlhttp.readyState) {
							case 1:
								self.onLoading();
								break;
							case 2:
								self.onLoaded();
								break;
							case 3:
								self.onInteractive();
								break;
							case 4:
								self.response = self.xmlhttp.responseText;
								self.responseXML = self.xmlhttp.responseXML;
								self.responseStatus[0] = self.xmlhttp.status;
								self.responseStatus[1] = self.xmlhttp.statusText;
	
								if (self.execute) {
									self.runResponse();
								}
	
								if (self.elementObj) {
									elemNodeName = self.elementObj.nodeName;
									elemNodeName.toLowerCase();
									if (elemNodeName == "input"
									|| elemNodeName == "select"
									|| elemNodeName == "option"
									|| elemNodeName == "textarea") {
										self.elementObj.value = self.response;
									} else {
										self.elementObj.innerHTML = self.response;
									}
								}
								if (self.responseStatus[0] == "200") {
									self.onCompletion();
								} else {
									self.onError();
								}
	
								self.URLString = "";
								break;
						}
					};
	
					this.xmlhttp.send(this.URLString);
				}
			}
		};
	
		this.reset();
		this.createAJAX();
	}	
	
var ajaxObjects = new Array();

function GetQuantity(productID,ID) {
	var ajaxIndex = ajaxObjects.length;
	document.getElementById('QuantitySelect').innerHTML='<br/><img src="/includes/2.gif" />&nbsp;Getting quantities...';
	ajaxObjects[ajaxIndex] = new sack();
	var url = '/includes/ajax_quantity.asp?ProductID=' + productID + '&ID=' + ID;
	ajaxObjects[ajaxIndex].requestFile = url;
	ajaxObjects[ajaxIndex].onCompletion = function() { GetQuantityComplete(ajaxIndex); } ;
	ajaxObjects[ajaxIndex].runAJAX();			
}

function GetQuantityComplete(index) {
	var xmlString = ajaxObjects[index].response;
	if (window.ActiveXObject)
	  {
	  var doc=new ActiveXObject("Microsoft.XMLDOM");
	  doc.async="false";
	  doc.loadXML(xmlString);
	  }
	else
	  {
	  var parser=new DOMParser();
	  var doc=parser.parseFromString(xmlString,"text/xml");
	  }
	var x=doc.documentElement;
	sStatus = x.childNodes[0].childNodes[0].nodeValue;
	sHtml = x.childNodes[1].childNodes[0].nodeValue;
	if (sStatus=='ok') {
		document.getElementById('QuantitySelect').innerHTML = sHtml;
	} else {
		document.getElementById('QuantitySelect').innerHTML = '*';	
	}
}

var basketshow = "on";
var checkoutshow = "off";
 
function change() {
    if (basketshow == "on") {
        basketshow = "off";
		checkoutshow = "on";		
		Effect.toggle('MyBasket','slide');
		//document.getElementById("MyCheckout").displauy = 'visible';
		Effect.toggle('MyCheckout','slide');
        document.getElementById("ToggleBasketText").innerHTML = 'Show My Basket';
    }
    else {
        basketshow = "on";
		checkoutshow = "off";		
		Effect.toggle('MyBasket','slide');
		//document.getElementById("MyCheckout").displauy = 'none';
		Effect.toggle('MyCheckout','slide');		
        document.getElementById("ToggleBasketText").innerHTML = 'Checkout Now';
    }
}

function CheckInvoiceDetails(obj) {
    if (obj.checked) {
		Effect.toggle('MyInvoiceDetails','slide');
		//document.getElementById("MyInvoiceDetails").style.visibility = "hidden";
    }
    else {	
		//document.getElementById("MyInvoiceDetails").style.visibility = "visible";
		Effect.toggle('MyInvoiceDetails','slide');
    }	
}

function validateFormOnSubmit(theForm) {
	var reason = "";

	reason += validateEmpty(theForm.fFirstName,"Please enter your first name");
    reason += validateEmpty(theForm.fLastName,"Please enter your last name");
    reason += validateEmpty(theForm.fAddress1,"Please enter your address");
    reason += validateEmpty(theForm.fAddress4,"Please enter your town / city");	
    reason += validatePostcode(theForm.fPostcode,"Please enter your postcode");
	reason += validateEmpty(theForm.fDayTel,"Please enter your day time telephone number");
    reason += validateEmail(theForm.fEmailAddress);
	
	if (!theForm.fInvoiceDetails.checked) {
	    reason += validateEmpty(theForm.fDeliveryAddress1,"Please enter your delivery address");
	    reason += validatePostcode(theForm.fDeliveryPostCode,"Please enter your delivery postcode");		
	}
      
	//alert(reason);  
	
    if (reason != "") {
      alert(reason);
      return false;
    }
    return true; 
  }
  
  
  
  function validateRegistrationOnSubmit(theForm) {
	var reason = "";

	reason += validateEmpty(theForm.fFirstName,"Please enter your first name");
    reason += validateEmpty(theForm.fLastName,"Please enter your last name");
    reason += validateEmpty(theForm.fAddress1,"Please enter your address");
    reason += validateEmpty(theForm.fAddress4,"Please enter your town / city");	
    reason += validatePostcode(theForm.fPostcode,"Please enter your postcode");
	reason += validateEmpty(theForm.fDayTel1,"Please enter your area code");
	reason += validateEmpty(theForm.fDayTel2,"Please enter your telephone number");
	reason += validateEmpty(theForm.fPassword,"Please enter a password for your account");
    reason += validateEmail(theForm.fEmailAddress);
	
	if (!theForm.fInvoiceDetails.checked) {
	    reason += validateEmpty(theForm.fDeliveryAddress1,"Please enter your delivery address");
	    reason += validatePostcode(theForm.fDeliveryPostCode,"Please enter your delivery postcode");		
	}
      
	//alert(reason);  
	
    if (reason != "") {
      alert(reason);
      return false;
    }
    return true; 
  }
  
  

function validateEmpty(fld,str) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = '#caf190'; 
        error = str+"\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function trim(s){ return s.replace(/^\s+|\s+$/, '');} 

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = '#caf190';
        error = "You didn't enter an email address\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#caf190';
        error = "Please enter a valid email address\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#caf190';
        error = "The email address contains illegal characters\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePostcode(fld,str) {
    var error = "";
  
    if (!isValidPostcode(fld.value)) {
        fld.style.background = '#caf190'; 
        error = str+"\n"
    } else {
        fld.style.background = 'White';
		fld.value = formatPostcode(fld.value);
    }
    return error;   
}

/* tests to see if string is in correct UK style postcode: AL1 1AB, BM1 5YZ etc. */
function isValidPostcode(p) {
	//var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
	var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2}[A-Z]{0,1} ?[0-9][A-Z]{2}/i;
	return postcodeRegEx.test(p);
}
/*	formats a VALID postcode nicely: AB120XY -> AB1 0XY */
function formatPostcode(p) {
	if (isValidPostcode(p)) {
		var postcodeRegEx = /(^[A-Z]{1,2}[0-9]{1,2})([0-9][A-Z]{2}$)/i;
		//var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2}[A-Z]{0,1} ?[0-9][A-Z]{2}/i;
		return p.replace(postcodeRegEx,"$1 $2").toUpperCase();
	} else {
		return p;
	}
}

function cbsel(item)
{
    fe = $(item);
    if (fe.checked) { fe.checked=false; }
    else { fe.checked = true; }
}
