// JavaScript Document

	var paymentSubmitted = false;


	//*************************
	// openFixturePage()
	// this functions opens the fixture page
	function openFixturePage(thispage){
		window.location = thispage;
	}
	

	//*************************
	// calculateLineValue(thisline)
	// this function will calculate the line value based upon the line entered in the ticket
	function calculateLineValue(){
		totalValue = 0;
		for(i=1;i<11;i++){
			if(i<10){
				stri = new String('0' + i);	
			} else {
				stri = new String(i);
			}
			
			if($('Ticket' + stri + 'Price')){			
				thisTicketPrice 	= Number($('Ticket' + stri + 'Price').value);
				thisTicketQuantity 	= Number($('Ticket' + stri + 'Quantity').value);
				thisTicketLineValue = Number(thisTicketPrice * thisTicketQuantity).toFixed(2);
				totalValue += Number(thisTicketLineValue);
				
				$('Ticket' + stri + 'LineValue').innerHTML = '&pound;' + thisTicketLineValue;
			}
		}
		$('TotalValue').innerHTML = "&pound;" + Number(totalValue).toFixed(2);
	}
	//*************************


	//*************************
	//checkboxValue(thisid)
	//function to return the checked value of a checkbox
	function checkboxValue(thisid,type){
				
		checkbox = new Object(document.getElementById(thisid));
		
		if(type=="checked"){
			return checkbox.checked;	
		}
		
		if(type=="value" && checkbox.checked){
			return checkbox.value;	
		} else {
			return "";	
		}
				
	}
	//*************************

	//*************************
	// 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;
	}



	//*************************
	// validateTicketSelection();
	// function to check the number of tickets selected...
	function validateTicketSelection(){
		
		//initialise the error vars
		errorList 	  = "";
		errorCount 	  = 0;
		errorMessage  = "";

		//get the values
		totalTickets 	= 0;
		
		//is there at least 1 adult ticket?
		for(i=1;i<11;i++){
			if(i<10){
				stri = new String('0' + i);	
			} else {
				stri = new String(i);
			}
			
			if($('Ticket' + stri + 'Quantity')){			
				thisTicketQuantity 	= Number($('Ticket' + stri + 'Quantity').value);
				totalTickets += thisTicketQuantity;
			}
		}
		
		if(totalTickets==0){
			errorList 	+= "Sorry, you must select at least 1 ticket to continue."; 
			errorCount 	+= 1;
			errorMessage = errorList;
			alert(errorMessage);
		} else {
			
			//check the ticket availability
			url = 'ajax/check-ticket-availability.asp'
			
			new Ajax.Request(url,{
				method : 'post',
				parameters : $('frmTickets').serialize(true),
				onSuccess:function(transport){
					
					json = String(transport.responseText).evalJSON(true);
					
					if(json.available=='true'){
						$('frmTickets').submit();
					} else {
						alert(json.message);		
					}
				},
				onFailure:function(transport){
				//	alert('b');
				}							 
			});
		}
	
	}	


	//*************************
	//validateBuyTicketsOnline()
	//function to validate step 3 of the buy tickets online form!
	function validateBuyTicketsOnline(){
		
		if(paymentSubmitted == false){
		
			//initialise the error variables
			errorList 		= "";
			errorCount 		= 0;
			errorMessage 	= "";
					
			//get the required values	
			title 			= $('Title').value;
			forename 		= $('Forename').value;
			surname 		= $('Surname').value;
			address1 		= $('Address1').value;
			postcode 		= $('Postcode').value;
			email 			= $('Email').value;
			cardType 		= $('CardType').value;
			cardName 		= $('CardName').value;
			cardNumber 		= $('CardNumber').value;
			cardExpiryMonth = $('CardExpiryMonth').value;
			cardExpiryYear 	= $('CardExpiryYear').value;
			cardCVC			= $('CardCVC').value;
			if($('Confirmed').checked){
				confirmed	= $('Confirmed').value;
			} else {
				confirmed	= '';	
			}
			
			//title
			if(title==''){
				errorList 	+= "\n- You must enter your title.     ";
				errorCount 	+= 1;
			}
			//forename
			if(forename==''){
				errorList 	+= "\n- You must enter your forename.     ";
				errorCount 	+= 1;
			}
			//surname
			if(surname==''){
				errorList 	+= "\n- You must enter your surname.     ";
				errorCount 	+= 1;
			}
			//address1
			if(address1==''){
				errorList 	+= "\n- You must enter the first line of your address.     ";
				errorCount 	+= 1;
			}
			//postcode
			if(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(cardType==''){
				errorList 	+= "\n- You must select a card type.     ";
				errorCount 	+= 1;
			}
			//cardName
			if(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(cardExpiryMonth==''){
				errorList 	+= "\n- You must select the month your card expires.     ";
				errorCount 	+= 1;
			} 
			//cardExpiryYear!
			if(cardExpiryYear==''){
				errorList 	+= "\n- You must select the year your card expires.     ";
				errorCount 	+= 1;
			}
			//has the card expired?
			if(cardExpiryMonth!='' && cardExpiryYear!=''){
	
				//create the dates
				cardExpiry = Number(String(cardExpiryYear) + String(cardExpiryMonth));
				currentYearMonth = Number(String($('CurrentYear').value) + String($('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 || cardCVC==''){
				errorList 	+= "\n- You must enter your CVC Number.     ";
				errorCount 	+= 1;
			}
			if(confirmed!='Y'){
				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;
				paymentSubmitted = true;
			}
		} else {
			return false;	
		}		
	}

/* ------------------------- JS VALIDATION TOOLS ------------------------- */	

	//*************************
	//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;     
	}


	//*************************
	//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;	
		}
	}
	

	//*************************
	//isInteger
	//function to check if a value is numeric
	function isInteger(txt){
	
		var validChars = "0123456789";
		var isNumber=true;
		var char;
	
		for(i=0;i<txt.length && isNumber == true; i++){
			char = txt.charAt(i);
			if(validChars.indexOf(char) == -1){
				isNumber = false;
			}
		}
		
		//return the value;
		return isNumber;
	}
	

	//*************************
	//isNull
	//function to check if a value isNull!
	function isNull(val){return(val==null);}


	//*************************
	//isNumeric
	//function to check if a value is numeric
	function isNumeric(txt){
	
		var validChars = "0123456789.";
		var isNumber=true;
		var char;
	
		for(i=0;i<txt.length && isNumber == true; i++){
			char = txt.charAt(i);
			if(validChars.indexOf(char) == -1){
				isNumber = false;
			}
		}
		
		//return the value;
		return isNumber;
	}
	//*************************
	
	
	
	function updateMyOrder(){
		
		$('OfferCode').value = $('txtOfferCode').value;
		
		url = 'ajax/update-my-order.asp';
		
		new Ajax.Request(url,{
			method : 'post',
			parameters : $('frmTickets').serialize(true),
			onCreate:function(){
				
			},
			onSuccess:function(transport){
				
				html = String(transport.responseText);
								
				$('TicketDetails').innerHTML = html;	
				
			},
			onFailure:function(transport){
				alert(transport.responseText);
			}		 
						 
		});
		
		
	}
	