// JavaScript Document
	//*************************
	//testJavascriptEnabled()
	//This function hides text about javascript if enabled
	function testJavascriptEnabled(){
		
		if($('Stage') != null && $('Stage').value == '2'){
			$('JavascriptMessage').innerHTML = '';
			$('JavascriptMessage').hide();
		}
	}
	//*************************


	//*************************
	//swzDateSelect()
	//class to create a date select...
	function swzDateSelect(){
		
		//set the variables
		this.startDate = new Date()
		var arrMonthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	
	
		//get month name
		this.getMonthName = function(){
			return arrMonthNames[this.startDate.getMonth()];			
		}
		
		//getDaysInMonth
		//method to return the days in the month...
		this.getDaysInMonth = function(){
			this.daysInMonth = new Date(this.startDate.getYear(),(this.startDate.getMonth() + 1),0);
			return this.daysInMonth.getDate();		
		}
		
		//create date select
		this.createDateSelect = function(){
			//reset the variables
			this.output = "";
			
			//day select
			this.output += "<div class=\"day\">";
			this.output += this.createDaySelect();
			this.output += "</div>";
			
			//month select
			this.output += "<div class=\"month\">";
			this.output += this.createMonthSelect();
			this.output += "</div>";
			
			//year select
			this.output += "<div class=\"year\">";
			this.output += this.createYearSelect();
			this.output += "</div>";
			
			this.output += "<input type=\"hidden\" id=\"" + this.inputField + "\" name=\"" + this.inputField + "\" value=\"\" />";
			
			//return the output
			return this.output;		
		}
		
		//create day select
		this.createDaySelect = function(){
			//get the number of days in the month.
			this.output = "<select id=\"" + this.inputField + "Day\" onChange=\"swzDateSelectUpdate('" + this.inputField + "')\">";
			//create the select box
			for(this.i=1;this.i<=this.getDaysInMonth();this.i++){
				if(this.i==this.startDate.getDate()){
					this.output += "<option value=\"" + this.i + "\" SELECTED>" + this.i + "</option>";
				} else {
					this.output += "<option value=\"" + this.i + "\">" + this.i + "</option>";
				}
			}
			this.output += "</select>";
			return this.output;
		}
		
		//create month select
		this.createMonthSelect = function(){
			this.output = "<select id=\"" + this.inputField + "Month\" onChange=\"swzDateSelectUpdate('" + this.inputField + "')\">";
			//create the select box
			for(this.i=0;this.i<12;this.i++){
				if(this.i==this.startDate.getMonth()){
					this.output += "<option value=\"" + this.i + "\" SELECTED>" + arrMonthNames[this.i] + "</option>";
				} else {
					this.output += "<option value=\"" + this.i + "\">" + arrMonthNames[this.i] + "</option>";
				}
			}
			this.output += "</select>";
			return this.output;
		}
		
		//create year select
		this.createYearSelect = function(){
			this.startYear = this.startDate.getYear();
			this.endYear = this.startYear + 2;
			this.output = "<select id=\"" + this.inputField + "Year\" onChange=\"swzDateSelectUpdate('" + this.inputField + "')\">";
			for(this.i=this.startYear;this.i<this.endYear;this.i++){
				if(this.i==this.startDate.getYear()){
					this.output += "<option value=\"" + this.i + "\" SELECTED>" + this.i + "</option>";
				} else {
					this.output += "<option value=\"" + this.i + "\">" + this.i + "</option>";
				}
			}
			this.output += "</select>";
			return this.output;
		}		
	}
	//*************************


	//*************************
	//swzDateSelectUpdate(field)
	//function to update the date select...
	function swzDateSelectUpdate(field){
	
		year  = Number(selectValues(field + "Year","value"));
		month = Number(selectValues(field + "Month","value")) + 1;
		day   = Number(selectValues(field + "Day","value"));
		
		//get the number of days in the month
		myDate = new Date(year,month,0);
		daysInMonth = myDate.getDate();

		//reset the days...
		document.getElementById(field + "day").options.length=0;
		for(i=0;i<daysInMonth;i++){
			document.getElementById(field + "day").options[i] = new Option(i+1,i+1);
		}	
		
		//check the selected index
		if(day <= daysInMonth){
			document.getElementById(field + "day").selectedIndex = (day - 1);
		} 
		
		//update the display field
		displayYear = year;
		displayMonth = month;		
		if(displayMonth < 10){
			displayMonth = "0" + displayMonth;
		} 
		displayDay = Number(selectValues(field + "Day","value"));
		if(displayDay < 10){
			displayDay = "0" + displayDay;
		}
		document.getElementById(field).value = displayYear + "-" + displayMonth + "-" + displayDay;
	}
	//*************************


	//*************************
	//swzDateSubtraction(fielda,fieldb)
	//check two dates to see which is earlier...
	function swzDateSubtraction(fielda,fieldb){
	
		dateA = String(document.getElementById(fielda).value);
		dateB = String(document.getElementById(fieldb).value);
		
		arrDateA = dateA.split("-");
		arrDateB = dateB.split("-");
		
		myDateA = new Date(Number(arrDateA[0]),(Number(arrDateA[1]) - 1),Number(arrDateA[2]));
		myDateB = new Date(Number(arrDateB[0]),(Number(arrDateB[1]) - 1),Number(arrDateB[2]));
		myDateDiff = myDateB.getTime() - myDateA.getTime();

		return myDateDiff;
	}
	//*************************


	//*************************
	//calculateBTOLineValue();
	//function to calculate the line value when you Buy Tickets Online
	function calculateBTOLineValue(){

		//set the total value to 0
		this.totalValue = 0;

		//check all the tickets
		for(i=1;i<11;i++){
			if(i<10){
				stri = "0" + i;
			} else {
				stri = i;	
			}
			if(document.getElementById("Ticket" + stri + "Price")){
				this.quantity = document.getElementById("Ticket" + stri + "Qty").value;
				this.price = document.getElementById("Ticket" + stri + "Price").value;
				this.value = this.quantity * this.price;
				this.totalValue += this.value;
				document.getElementById("Ticket" + stri + "Value").innerHTML = "&pound;" + this.value.toFixed(2);
			}
		}
		
		//set the total value
		document.getElementById("TotalValue").innerHTML = "&pound;" + this.totalValue.toFixed(2);
	}
	//*************************


	//*************************
	//validateBTOTicketSelection();
	//function to check at least one ticket has been selected
	function validateBTOTicketSelection(){

		//set the total value to 0
		this.totalTickets = 0;
		errorList = "";
		errorCount = 0;
		errorMessage = "";

		//check all the tickets
		for(i=1;i<31;i++){
			//format i
			if(i<10){
				stri = "0" + i;
			} 
			else {
				stri = i;	
			}
			
			//check for quantity
			if(document.getElementById("Ticket" + stri + "Price")){
				this.totalTickets += Number(document.getElementById("Ticket" + stri + "Qty").value);
			}
		}	
		
		//check tickets selected
		if(totalTickets == 0){
			errorList += ("\n - You must select at least 1 ticket.     ");	
			errorCount += 1;
		}
		
		
		//check the date
		if(swzDateSubtraction("CurrentDate","DateSelect") < 0){
			errorList += ("\n - You must select a date that has not already passed.     ");
			errorCount += 1;
		} 
		else {
			if($('CurrentDate').value == $('DateSelect').value && $('CurrentTime').value >= '16:00:00'){
				errorList += ("\n - Tickets for today are no longer available. Please select another date.     ");
				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;
		}
	}
	//*************************
	

	//*************************
	//validateBTOYourDetails()
	//function to validate step 2 of the buy tickets online form!
	function validateBTOYourDetails(){
		
		//initialise the error variables
		errorList 		= "";
		errorCount 		= 0;
		errorMessage 	= "";
		
		//get the required values	
		title 			= document.getElementById("Title").value;
		forename 		= document.getElementById("Forename").value;
		surname 		= document.getElementById("Surname").value;
		address1 		= document.getElementById("Address1").value;
		postcode 		= document.getElementById("Postcode").value;
		email 			= document.getElementById("Email").value;
		cardType 		= selectValues("CardType","value");
		cardName 		= document.getElementById("CardName").value;
		cardNumber 		= document.getElementById("CardNumber").value;
		cardExpiryMonth = selectValues("CardExpiryMonth","value");
		cardExpiryYear 	= selectValues("CardExpiryYear","value");
		cardCVC			= document.getElementById("CardCVC").value;
		confirmed		= checkboxValue("Confirmed","checked")	
		
		//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;
		}
		//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;
		}
		if(!confirmed){
			errorList 	+= "\n- You must agree to the terms and conditions by checking the box above the continue button.     ";
			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;
		}
		
	}
	//*************************

