// JavaScript Document 
//CHECKOUT.js 

	//*************************
	//backStage(thisStage,thisForm)
	//This function returns the user back a stage
	function backStage(thisStage,thisForm){
		
		$('Stage').value = thisStage;
		$(thisForm).submit();
			
	}
	//*************************


	//*************************
	//checkAcceptTerms()
	//This function checks the user has read and accepts the terms and conditions
	function checkAcceptTerms(){
	
		//initialise the error variables
		var errorMessage = "";
		
		if($('Stage').value == '2'){
			//going back a stage
			return true;			
		}
		else {
			//validate form
			var accept	= checkboxValue("AcceptTerms","checked");
			
			if(!accept){
				errorMessage = "The following error occurred whilst processing your details.     ";
				errorMessage += "\n\n- You must agree to the terms and conditions by checking the box above the continue button.     ";
				errorMessage += "\n\nPlease fix this error before clicking 'Continue'.     ";

				alert(errorMessage);
				return false;
			}
			else {
				return true;
			}
		}	
	}
	//*************************


	//*************************
	//checkDelivery()
	//This function checks that a delivery area has been selected
	function checkDelivery(){
		
		//Shipping
		if($('Stage').value == '1'){
			//going back a stage
			return true;			
		}
		else {
			//validate form
			var index = $('Shipping').selectedIndex;
			var value = $('Shipping').options[index].value;		
			
			if(value == ""){
				alert('You must select the Delivery Area before clicking \'Continue\'.');
				return false;
			}
			else {
				return true;
			}
		}
	}
	//*************************


	//*************************
	//checkPaymentDetails()
	//This function validates the payment details
	function checkPaymentDetails(){

		//initialise the error variables
		var errorList 	 = "";
		var errorCount 	 = 0;
		var errorMessage = "";
		

		var cardType 		= selectValues("CardType","value")	
		var cardName 		= $("CardName").value;
		var cardNumber 		= $("CardNumber").value;
		var cardExpiryMonth = selectValues("CardExpiryMonth","value")	
		var cardExpiryYear 	= selectValues("CardExpiryYear","value")	
		var cardCVC			= $("CardCVC").value;

		//cardType
		if(isBlank(cardType)){
			errorList 	+= "\n- You must select a card type.     ";
			errorCount 	+= 1;
		}
		//cardName
		if(isBlank(cardName)){
			errorList 	+= "\n- You must enter the name of the cardholder.     ";
			errorCount 	+= 1;
		}
		//cardNumber
		if(cardNumber.length < 16 || !isNumeric(cardNumber)){
			errorList 	+= "\n- Card Number must be at least 16 characters long and contain numbers only.     ";
			errorCount 	+= 1;
		}
		//cardExpiryMonth!
		if(isBlank(cardExpiryMonth)){
			errorList 	+= "\n- You must select the month your card expires.     ";
			errorCount 	+= 1;
		} 
		//cardExpiryYear!
		if(isBlank(cardExpiryYear)){
			errorList 	+= "\n- You must select the year your card expires.     ";
			errorCount 	+= 1;
		}
		//has the card expired?
		if(!isBlank(cardExpiryMonth) && !isBlank(cardExpiryYear)){

			//create the dates
			cardExpiry = Number(String(cardExpiryYear) + String(cardExpiryMonth));
			currentYearMonth = Number(String(document.getElementById("CurrentYear").value) + String(document.getElementById("CurrentMonth").value));
			
			//check the dates against one another
			if(cardExpiry < currentYearMonth){
				errorList 	+= "\n- The expiry date you have entered has passed. Please check if your card has expired.     ";
				errorCount 	+= 1;
			}
		}
		//cardCVC
		if(cardCVC.length < 3 || !isNumeric(cardCVC)){
			errorList 	+= "\n- You must enter your CVC Number.     ";
			errorCount 	+= 1;
		}

		//check for errors!
		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 'Continue'.     ";
			} 
			else {
				errorMessage = "The following errors occurred whilst processing your details.     ";
				errorMessage += "\n" + errorList + "\n";
				errorMessage += "\nPlease fix these errors before clicking 'Continue'.     ";	
			}
			
			alert(errorMessage);
			return false;
		} 
		else {
			return true;
		}

	}
	//*************************
	

	//*************************
	//checkPersonalDetails()
	//This function validates the personal details
	function checkPersonalDetails(){
		
		//initialise the error variables
		var errorList 	 = "";
		var errorCount 	 = 0;
		var errorMessage = "";
		
		if($('Stage').value == '3'){
			//going back a stage
			return true;			
		}
		else {
			//get the required values	
			var title 	 = $("Title").value;
			var forename = $("Forename").value;
			var surname  = $("Surname").value;
			var address1 = $("Address1").value;
			var postcode = $("Postcode").value;
			var email 	 = $("Email").value;
		
			//title
			if(isBlank(title)){
				errorList 	+= "\n- You must enter your title.     ";
				errorCount 	+= 1;
			}
			//forename
			if(isBlank(forename)){
				errorList 	+= "\n- You must enter your forename.     ";
				errorCount 	+= 1;
			}
			//surname
			if(isBlank(surname)){
				errorList 	+= "\n- You must enter your surname.     ";
				errorCount 	+= 1;
			}
			//address1
			if(isBlank(address1)){
				errorList 	+= "\n- You must enter the first line of your address.     ";
				errorCount 	+= 1;
			}
			//postcode
			if(isBlank(postcode)){
				errorList 	+= "\n- You must enter your postcode.     ";
				errorCount 	+= 1;
			}
			//email
			if(!isEmail(email)){
				errorList 	+= "\n- You must enter a valid email address.     ";
				errorCount 	+= 1;
			}
		
			//check for errors!
			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 'Continue'.     ";
				} 
				else {
					errorMessage = "The following errors occurred whilst processing your details.     ";
					errorMessage += "\n" + errorList + "\n";
					errorMessage += "\nPlease fix these errors before clicking 'Continue'.     ";	
				}
			
				alert(errorMessage);
				return false;
			} 
			else {
				return true;
			}
		}
	}
	//*************************
	

