	//This function updates the total price field for each ticket
	//when the user enters a qunatity. onBlur action is used.

	//DANGER! Hard coded composite ticket numbers!
	function calculateSubTotal(quantity, subTotal, price, dspHolder){

			if(!isNaN(quantity.options[quantity.selectedIndex].value)){
				subTotal.value = eval(quantity.options[quantity.selectedIndex].value * price);
				dspHolder.innerHTML = "&pound;" + subTotal.value;
			}		
			else
				subTotal.value = "0";		
				
		 	 calculateTotal();	  	
	 } 
	 
  function calculateTotal(){
	 	var totalCost = 0;
	  var myForm = document.theForm;
	 	for (var i = 0; i < myForm.elements.length; i++) {
			if (myForm.elements[i].type == "hidden") {
				if(myForm.elements[i].name.indexOf("total_") != -1){
					totalCost = eval(totalCost + parseInt(myForm.elements[i].value));
					if(navigator.appName == "Netscape"){
						document.getElementById("totalHolder").innerHTML = "&pound;" + totalCost;
					}
					else{
						document.all.totalHolder.innerHTML = "&pound;" + totalCost;
					}
				 }
			 }
		 }
		 document.theForm.totalCost.value = totalCost;		 
	 }

	 
	function checkPO (source) {
  	var address = /(p+[^\n]*o+[^\n]*box+[^\n]*)+/i;
		if (source.value.match(address) != null) {
			alert('We cannot deliver to PO Box addresses.');
			source.focus();
		}
  }
	
	function checkTerms (theForm) {		
 		if(!theForm.terms.checked){
			alert("Please confirm you have read the terms & conditions by ticking the checkbox.")
			return false;
		}
		return true;
  }
	
	function testCheckBoxes() {
		var atLeastOne = false;
 		for(var x=0; x<document.theForm.length; x++){		
			if(document.theForm.elements[x].type == "checkbox"){
				if(document.theForm.elements[x].checked){
					atLeastOne = true;
				}
			}
		}
		if(!atLeastOne){
			alert("Please select an order.")
			return false;
		}
		else
			return true;
  }
	
	function checkForm(){
	
		sname = trim(document.theForm.sname.value);
		fname = trim(document.theForm.fname.value);
		daddress1 = trim(document.theForm.daddress1.value);
		dtown = trim(document.theForm.dtown.value);
		dcounty = trim(document.theForm.dcounty.value);
		dcountry = trim(document.theForm.dcountry.value);
		dpostcode = trim(document.theForm.dpostcode.value);
		tel = trim(document.theForm.tel.value);
		hometel = trim(document.theForm.hometel.value);
		email = trim(document.theForm.email.value);
		confirmemail = trim(document.theForm.confirmemail.value);
		referralsourceItem = trim(document.theForm.referralsource.selectedIndex);
				
		if(sname.length == 0 || sname.length == 0){
			alert("Please enter a forename and surname");
			return false;
		}
		else if(daddress1.length == 0){
			alert("Please enter delivery address details. You may not use a PO Box address");
			return false;
		}
		else if(dtown.length == 0){
			alert("Please enter a town");
			return false;
		}
		else if(dcounty.length == 0){
			alert("Please enter a state/county");
			return false;
		}		
		else if(dcountry.length == 0){
			alert("Please enter a country");
			return false;
		}	
		else if(dpostcode.length == 0){
			alert("Please enter a postcode");
			return false;
		}	
		else if(tel.length == 0){
			alert("Please enter a daytime telephone number");
			return false;
		}	
		else if(hometel.length == 0){
			alert("Please enter a home telephone number");
			return false;
		}			
		else if(email.length == 0){
			alert("Please enter an email address");
			return false;
		}			
		else if(confirmemail.length == 0){
			alert("Please confirm your email address");
			return false;
		}			
		else if(email != confirmemail){
			alert("Your confirm email is different to your email address!");
			return false;
		}			
		else if(referralsourceItem == 0){
			alert("Please select an entry in the 'Where did you hear about Open Tickets' dropdown");
			return false;
		}			
		alert(referralsource.selectedIndex);
		if(!checkDropDowns()){
			return false;
		}
		else if(!validEmail()){
			return false;
		} 
		else if(!checkForComposites()){
			return false;
		} 
		else
			return true;
	

	}
	
	function checkForComposites(){

		var compTotal = 0;
	 	for(var x=0; x<document.theForm.length; x++){		
			
			if(document.theForm.elements[x].type == "select-one"){
		
				if(document.theForm.elements[x].name == "quantity_33" || document.theForm.elements[x].name == "quantity_34"){				
					compTotal = compTotal + eval(document.theForm.elements[x].value);					
				}
			}
		}	
		if(compTotal>4){
			alert("Only 4 composite tickets are permitted per order");
			return false;
		}
		else
			return true;
	}	
	
	function checkRegForm(){
	
		sname = trim(document.theForm.sname.value);
		fname = trim(document.theForm.fname.value);
		
				
		if(sname.length == 0 || fname.length == 0){
			alert("Please enter a forename and surname");
			return false;
		}
				
		
		if(!validEmail()){
			return false;
		} 
		else
			return true;
	

	}

	
function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function
	
	function checkDropDowns() {
		var atLeastOne = false;
 		for(var x=0; x<document.theForm.length; x++){		
			if(document.theForm.elements[x].type == "select-one"){
				if(document.theForm.elements[x].options[document.theForm.elements[x].selectedIndex].value != "0"){
					atLeastOne = true;
				}
			}
		}
		if(!atLeastOne){
			alert("You have not selected any tickets for purchase.")
			return false;
		}
		else
			return true;
  }
	
	
	function validEmail(){
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.theForm.email.value)) {
			return true;
		} 
		else {
			alert("Invalid E-mail Address! Please re-enter.");
			return false; 
		}
	}