// ONLINE-SHOP.js

	//*************************
	//updateQuantity(thisrow)
	//function to submit the line update form
	function updateQuantity(thisrow){
		//submit the form for the appropriate row!
		document.forms["frmUpdate" + thisrow].submit();
	}
	//*************************
	
	
	//*************************
	//validateYourDetails()
	//function to validate the customer's details on the last stage of the form!
	function validateYourDetails(){
		
		errorList = "";
		errorCount = 0;
		errorMessage = "";
		
		//get the details from the form
		title 			= document.getElementById("Title").value;
		forename 		= document.getElementById("Forename").value;
		surname 		= document.getElementById("Surname").value;
		organisation 	= document.getElementById("Organisation").value;
		address1 		= document.getElementById("Address1").value;
		postcode 		= document.getElementById("Postcode").value;
		telephone 		= document.getElementById("Telephone").value;
		mobile 			= document.getElementById("Mobile").value;
		email 			= document.getElementById("Email").value;
		delTitle 		= document.getElementById("DelTitle").value;
		delForename 	= document.getElementById("DelForename").value;
		delSurname 		= document.getElementById("DelSurname").value;
		delAddress1 	= document.getElementById("DelAddress1").value;
		delPostcode 	= document.getElementById("DelPostcode").value;
		delTelephone 	= document.getElementById("DelTelephone").value;
		cardType 		= selectValues("CardType","value");
		cardName 		= document.getElementById("CardName").value;
		cardNumber 		= document.getElementById("CardNumber").value;
		cardStartMonth 	= selectValues("CardStartMonth","value");
		cardStartYear 	= selectValues("CardStartYear","value");
		cardExpiryMonth = selectValues("CardExpiryMonth","value");
		cardExpiryYear 	= selectValues("CardExpiryYear","value");
		cardCVC 		= document.getElementById("CardCVC").value;
		
		allDelivery = delTitle + delForename + delSurname;
		allDelivery + delAddress1 + delPostcode + delTelephone;

		cardStart  = String(cardStartYear) + String(cardStartMonth);
		cardExpiry = String(cardExpiryYear) + String(cardExpiryMonth);
		
		var testing = "title:" + title + "\n";
		testing += "forename:" + forename + "\n";
		testing += "surname:" + surname + "\n";
		testing += "organisation:" + organisation + "\n";
		testing += "address1:" + address1 + "\n";
		testing += "postcode:" + postcode + "\n";
		testing += "telephone:" + telephone + "\n";
		testing += "mobile:" + mobile + "\n";
		testing += "email:" + email + "\n";
		testing += "delTitle:" + delTitle + "\n";
		testing += "delForename:" + delForename + "\n";
		testing += "delSurname:" + delSurname + "\n";
		testing += "delAddress1:" + delAddress1 + "\n";
		testing += "delPostcode:" + delPostcode + "\n";
		testing += "delTelephone:" + delTelephone + "\n";
		testing += "cardType:" + cardType + "\n";
		testing += "cardName:" + cardName + "\n";
		testing += "cardNumber:" + cardNumber + "\n";
		testing += "cardStartMonth:" + cardStartMonth + "\n";
		testing += "cardStartYear:" + cardStartYear + "\n";
		testing += "cardExpiryMonth:" + cardExpiryMonth + "\n";
		testing += "cardExpiryYear:" + cardExpiryYear + "\n";
		testing += "cardCVC:" + cardCVC + "\n";
		
		
		//validate!
		if(isBlank(title)){
			errorCount += 1;
			errorList += "\n- Title is a required field    ";
		}
		if(isBlank(forename)){
			errorCount += 1;
			errorList += "\n- Forename is a required field    ";
		}
		if(isBlank(surname)){
			errorCount += 1;
			errorList += "\n- Surname is a required field    ";
		}
		//address1
		if(isBlank(address1)){
			errorList 	+= "\n- The first line of your address is a required field.    ";
			errorCount 	+= 1;
		}
		//postcode
		if(isBlank(postcode)){
			errorList 	+= "\n- 'Postcode' is a required field.    ";
			errorCount 	+= 1;
		}
		//telephone
		if(isBlank(telephone) && isBlank(mobile)){
			errorList 	+= "\n- You must enter at least one telephone number.    ";
			errorCount 	+= 1;
		} 
		//email
		if(!isEmail(email)){
			errorList 	+= "\n- Please enter a valid email address.    ";
			errorCount 	+= 1;
		}
		//allDelivery
		if(!isBlank(allDelivery)){

			if(isBlank(delTitle)){
				errorCount += 1;
				errorList += "\n- Delivery Title is a required field    ";
			}
			if(isBlank(delForename)){
				errorCount += 1;
				errorList += "\n- Delivery Forename is a required field    ";
			}
			if(isBlank(delSurname)){
				errorCount += 1;
				errorList += "\n- Delivery Surname is a required field    ";
			}
			//address1
			if(isBlank(delAddress1)){
				errorList 	+= "\n- The first line of the delivery address is a required field.    ";
				errorCount 	+= 1;
			}
			//postcode
			if(isBlank(delPostcode)){
				errorList 	+= "\n- Delivery 'Postcode' is a required field.    ";
				errorCount 	+= 1;
			}
			//telephone
			if(isBlank(delTelephone)){
				errorList 	+= "\n- You must enter a telephone number for the delivery address.    ";
				errorCount 	+= 1;
			} 

		} 
		if(isBlank(cardType)){
			errorList 	+= "\n- You must select a Card Type.    ";
			errorCount 	+= 1;
		} 
		if(isBlank(cardName)){
			errorList 	+= "\n- You must enter the name on the card.    ";
			errorCount 	+= 1;
		} 
		if(isBlank(cardNumber)){
			errorList 	+= "\n- You must enter the card number.    ";
			errorCount 	+= 1;
		} 
		if(isBlank(cardExpiryMonth) || isBlank(cardExpiryYear)){
			errorList 	+= "\n- You must select the card expiry date.    ";
			errorCount 	+= 1;
		} 
		if(!isBlank(cardStart) && Number(cardExpiry) <= Number(cardStart)){
			errorList 	+= "\n- Your card's start date must be before your card's expiry date .    ";
			errorCount 	+= 1;
		}
		
		
		
		//check the error count
		if(errorCount != 0){
			if(errorCount == 1){
				errorMessage = "The following error occurred whilst processing your details.    ";
				errorMessage += "\n" + errorList + "\n";
				errorMessage += "\nPlease fix this error before clicking 'Complete My Order'.    ";
			} else {
				errorMessage = "The following errors occurred whilst processing your details.    ";
				errorMessage += "\n" + errorList + "\n";
				errorMessage += "\nPlease fix these errors before clicking 'Complete My Order'.    ";
			}			
			
			//show the error message
			alert(errorMessage);

			//alert("I DON'T FEEL VERY WELL");
			return false;

		} else {
			//alert("I FEEL FINE");
			return true;	
		}

	}
	//*************************
	
	
	//*************************
	//selectValues(thisid,thiselement)
	//function to access parts of a select box value...the label, value or selectedIndex
	function selectValues(thisid,thiselement){
	
		var si 		= document.getElementById(thisid).selectedIndex;
		var value   = document.getElementById(thisid).options[si].value
		var label	= document.getElementById(thisid).options[si].text;
		var output 	= new String("");
		
		switch(thiselement){
		
			case "selectedIndex":
				output = si;
				break;
			case "value":
				output = value;
				break;
			case "label":
				output = label;
				break;
			default:
				output = value;
		}
		
		//return the output
		return output;
	}
	//*************************
	

	//*************************
	//isNull
	//function to check if a value isNull!
	function isNull(val){return(val==null);}
	//*************************

	//*************************
	//isEmail
	//function to check the validity of an email
	function isEmail(email) {
		if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
			return true
		}
		else {
			return false;	
		}
	}
	//*************************

	//*************************
	//isBlank
	//function to check if there is a value present.
	function isBlank(val){
		if(val==null){return true;}
		if(val.length==0) {return true;}
		return false;     
	}
	//*************************
