// JavaScript Document

var shows = 
		[{
			name:'Jesus Christ Superstar',
			performances :[
						{
							date:'Tue 17 Aug',
							time:'20:30',
							full:'&pound;12.00',
							con:'&pound;8.50'
							},
						{
							date:'Wed 18 Aug',
							time:'16:30',
							full:'&pound;12.00',
							con:'&pound;8.50'
							},
						{
							date:'Thu 19 Aug',
							time:'20:30',
							full:'&pound;12.00',
							con:'&pound;8.50'
							},
						{
							date:'Fri 20 Aug',
							time:'20:30',
							full:'&pound;12.00',
							con:'&pound;8.50'
							},
						{
							date:'Sat 21 Aug',
							time:'15:00',
							full:'&pound;12.00',
							con:'&pound;8.50'
							}
						]
		},
		{
			name:'Seussical',
			performances : [
						{
							date:'Fri 20 Aug',
							time:'11:30',
							full:'&pound;12.00',
							con:'&pound;8.50'
							},
						{
							date:'Sat 21 Aug',
							time:'11:30',
							full:'&pound;12.00',
							con:'&pound;8.50'
							}
					]
		},
		{
			name:'Thumberlina',
			performances:[
			              {
							date:'Tue 10 Aug',
							time:'13:30',
							full:'&pound;7.50',
							con:'&pound;5.00'
			              },
			              {
								date:'Wed 11 Aug',
								time:'13:30',
								full:'&pound;7.50',
								con:'&pound;5.00'
				              },
			              {
								date:'Thu 12 Aug',
								time:'11:30',
								full:'&pound;7.50',
								con:'&pound;5.00'
				              },	
			              {
								date:'Fri 13 Aug',
								time:'11:30',
								full:'&pound;7.50',
								con:'&pound;5.00'
				              },
			              {
								date:'Sat 14 Aug',
								time:'11:30',
								full:'&pound;7.50',
								con:'&pound;5.00'
				              },
			              
			              ]
		},
		{
			name:'Mother Courage',
			performances:[
			              {
							date:'Tue 10 Aug',
							time:'15:30',
							full:'&pound;8.00',
							con:'&pound;5.00'
			              },
			              {
								date:'Wed 11 Aug',
								time:'17:00',
								full:'&pound;8.00',
								con:'&pound;5.00'
				              },
			              {
								date:'Thu 12 Aug',
								time:'15:30',
								full:'&pound;8.00',
								con:'&pound;5.00'
				              },	
			              {
								date:'Fri 13 Aug',
								time:'15:30',
								full:'&pound;8.00',
								con:'&pound;5.00'
				              },
			              {
								date:'Sat 14 Aug',
								time:'15:30',
								full:'&pound;8.00',
								con:'&pound;5.00'
				              },
			              
			              ]
		},
		{
			name:'The Ark',
			performances:[
			              {
							date:'Tue 10 Aug',
							time:'11:00',
							full:'&pound;8.00',
							con:'&pound;5.00'
			              },
			              {
								date:'Wed 11 Aug',
								time:'11:00',
								full:'&pound;8.00',
								con:'&pound;5.00'
				              }
			              ]
		},
		{
			name:'Azincourt',
			performances:[
			              {
							date:'Tue 17 Aug',
							time:'14:00',
							full:'&pound;7.50',
							con:'0'
			              },
			              {
								date:'Wed 18 Aug',
								time:'14:00',
								full:'&pound;10.00',
								con:'&pound;7.50'
				              },
			              {
								date:'Thu 19 Aug',
								time:'14:00',
								full:'&pound;10.00',
								con:'&pound;7.50'
				              },	
			              {
								date:'Fri 20 Aug',
								time:'14:00',
								full:'&pound;10.00',
								con:'&pound;7.50'
				              }
			              
			              ]
		},
		{
			name:'The Swan',
			performances:[
			              {
							date:'Tue 10 Aug',
							time:'20:30',
							full:'&pound;7.50',
							con:'0'
			              },
			              {
								date:'Wed 11 Aug',
								time:'19:30',
								full:'&pound;10.50',
								con:'&pound;7.50'
				              },
			              {
								date:'Fri 13 Aug',
								time:'20:30',
								full:'&pound;10.50',
								con:'&pound;7.50'
				              },
			              {
								date:'Sat 14 Aug',
								time:'16:00',
								full:'&pound;10.50',
								con:'&pound;7.50'
				              },
			              {
								date:'Sun 15 Aug',
								time:'19:30',
								full:'',
								con:'&pound;7.50'
				              },
			              {
								date:'Mon 16 Aug',
								time:'20:30',
								full:'&pound;10.00',
								con:'&pound;7.50'
				              },
			              {
								date:'Tue 17 Aug',
								time:'20:30',
								full:'&pound;12.00',
								con:'&pound;9.00'
				              },
			              {
								date:'Wed 18 Aug',
								time:'16:30',
								full:'&pound;12.00',
								con:'&pound;9.00'
				              },
			              {
								date:'Fri 20 Aug',
								time:'20:30',
								full:'&pound;12.00',
								con:'&pound;9.00'
				              },
			              {
								date:'Sat 21 Aug',
								time:'15:00',
								full:'&pound;12.00',
								con:'&pound;9.00'
				              }    
			              
			              ]
		},
		{
			name:'Kit and The Widow',
			performances:[
			              {
								date:'Sat 7 Aug',
								time:'19:00',
								full:'&pound;10.00',
								con:'0'
				              },
			              {
								date:'Sun 8 Aug',
								time:'16:00',
								full:'&pound;13.00',
								con:'&pound;11.00'
				              },
			              {
								date:'Mon 9 Aug',
								time:'19:00',
								full:'&pound;13.00',
								con:'&pound;11.00'
				              },
			              {
							date:'Tue 10 Aug',
							time:'19:00',
							full:'&pound;13.00',
							con:'&pound;11.00'
			              },
			              {
								date:'Wed 11 Aug',
								time:'15:00',
								full:'&pound;13.00',
								con:'&pound;11.00'
				              },
			              {
								date:'Thu 12 Aug',
								time:'19:00',
								full:'&pound;13.00',
								con:'&pound;11.00'
				              },	
			              {
								date:'Fri 13 Aug',
								time:'19:00',
								full:'&pound;14.00',
								con:'&pound;12.00'
				              },
			              {
								date:'Sat 14 Aug',
								time:'19:00',
								full:'&pound;14.00',
								con:'&pound;12.00'
				              },
			              {
								date:'Sun 15 Aug',
								time:'16:00',
								full:'&pound;13.00',
								con:'&pound;11.00'
				              },
			              {
								date:'Mon 16 Aug',
								time:'19:00',
								full:'&pound;13.00',
								con:'&pound;11.00'
				              },
			              {
								date:'Tue 17 Aug',
								time:'19:00',
								full:'&pound;13.00',
								con:'&pound;11.00'
				              },
			              {
								date:'Wed 18 Aug',
								time:'19:00',
								full:'&pound;13.00',
								con:'&pound;11.00'
				              },
			              {
								date:'Thu 19 Aug',
								time:'19:00',
								full:'&pound;13.00',
								con:'&pound;11.00'
				              }, 
			              {
								date:'Fri 20 Aug',
								time:'19:00',
								full:'&pound;14.00',
								con:'&pound;12.00'
				              },
			              {
								date:'Sat 21 Aug',
								time:'19:00',
								full:'&pound;14.00',
								con:'&pound;12.00'
				              },
			              {
								date:'Sun 22 Aug',
								time:'19:00',
								full:'&pound;13.00',
								con:'&pound;11.00'
				              }  
			              
			              ]
		}
]
//removes options
function removeOptions(elem) {
	$each(elem.options, function(i) {
		i.destroy();
	});
}

//enables ticket options
function enableTicketOptions(){
	var selectedShow = $('show').value;
	var performace = $('date').value;
	var theShow = shows[selectedShow];
	var thePerformance = theShow.performances[performace]
	
	if (thePerformance.con  !=0 && thePerformance.con != null) {
		$('concessions').disabled = false;
	}
	else {
		$('concessions').disabled = true;
	}
	$('tickets').disabled = false;
	$('basket').disabled = false;
	
	return;
}
//sets which show to appear in the dropdown
function setShows() {
	var total = shows.length;
	
	for (i=0;i<total;i++) {
		$('show').options[i+1] = new Option(shows[i].name,i);
	}
}
//sets the date options
function setDateOptions(show) {

	var elem = $('date');
	//remove existing
	removeOptions(elem);
	//handle no show
	if (show == undefined||show=='') {
		elem.disabled = true;
		return
	}
	var theShow = shows[show];
	var totalPerformances = theShow.performances.length
	elem.options[0] = new Option('Please select a date','');
	for (i=0;i<totalPerformances;i++) {
		elem.options[i+1] = new Option(theShow.performances[i].date,i);
	}
	
	elem.disabled = false;
	
}
//set time options
function setTimeOptions(performance){

	
	var selectedShow = $('show').value;
	var thePerformance = shows[selectedShow].performances[performance]
	var showTimes = thePerformance.time
	var elem = $('time');
	if (performance=='') {
		elem.disabled = true;
		return;
	}
	elem.options[0] = new Option(showTimes,showTimes);
	elem.disabled = false;
	//set the hidden form elements
	$('conPrice').value = thePerformance.con.replace('&pound;','');
	$('fullPrice').value = thePerformance.full.replace('&pound;','');
		
	
	enableTicketOptions();
	
}

function showinfo (show) {
	
	var amadeus = "<span>Show info: A sumptuous revival of Shaffer's classic tale of Mozart, malice and murder by the award-winning vocational training school.</span>";
	var kit		= "<span>Show info: Twitter? These two old songbirds perched at the piano have been peddling trivial nonsense since Alan Turing was in nappies. Here's more. Stephen Fry not included. 'Achingly funny' (<i>Times</i>). 'Free parking' (<i>Scotsman</i>).</span>";
	var sweeney = "<span>Show info: Sondheim's macabre masterpiece. 'Easily the best amateur production I have seen in a very long time' (<i>Herald Express</i>, Torquay). 'Flagship youth theatre' (<i>Western Morning News</i>).</span>";
	var dance   = "<span>Show info: Modern/contemporary dance program featuring 'Table for Two', award winning romantic duet danced atop table and chairs. World premier of 'Musical Chairs', five dancers use four chairs. One solo, one duet and four ensemble dances.</span>";
	var seussical = "<span>Show info: On the 15th May in the Jungle of Nool in the heat of the day in the cool of the pool he was splashing, enjoying the jungle's great joys when Horton the elephant heard a small noise ...</span>";
	var url = '/js/default.html';
	 $('detailTotal').empty();
	  $('subTotal').value = '';
	
	setDateOptions(show);  
	  
	
}


function total() {
	
 var concession = $('concessions').value;
 var tickets = $('tickets').value;
 var conPrice = $('conPrice').value;
 var fullPrice = $('fullPrice').value;
 

 
 var total  = (concession * conPrice) + (tickets * fullPrice);
 
 $('detailTotal').set('html','<span>Total: &pound;'+total+'</span>');
 $('subTotal').value = total;
	
	
}

function addToBasket(form) {
	
	//get variables
		var show = shows[$('show').value];
		var date = show.performances[$('date').value].date;
		var time = show.performances[$('date').value].time;
		var tickets = $('tickets').value;
		var concessions = $('concessions').value;
		var conPrice = $('conPrice').value;
		var fullPrice = $('fullPrice').value;
		var subtotal = $('subTotal').value
		var ticket1 ='';
		var ticket2 ='';
		var grandtotal  = parseInt($('grandTotal').value) + parseInt(subtotal);
		
		
		if (tickets) {
			
			if (tickets ==1) {
				ticket1 = tickets+' Standard Ticket @ &pound;'+fullPrice+' each <br />';
			} else {
			
		ticket1 = tickets+' Standard Tickets @ &pound;'+fullPrice+' each <br />';
		
			}
		}
		if (concessions) {
			
			if (concessions ==1) {
			ticket2 = concessions+' Concession Ticket @ &pound;'+conPrice+' each <br />';	
			} else {
				
		ticket2 = concessions+' Concession Tickets @ &pound;'+conPrice+' each <br />';	
		
			}
		}
		var entry = show.name+' on '+date+' at '+time+'<br />'+ticket1+ticket2+'<span class="subtotal">Sub Total: &pound;'+subtotal+'</span><br />';
		
		var ticketorder = $('ticketorder').value + entry;
		
		
	
		if (Spry.Widget.Form.validate(form) == true){
			
			if ( $('subTotal').value == '') {
			$('tickets').addClass('selectInvalidState');
		$('concessions').addClass('selectInvalidState');
		$('errorChk').set('class','selectRequiredState');
			$('error').set('html','You need to have at least 1 ticket!');
			
			}
			else {
				// add into basket
			
			var elem = new Element ('div',{
										'class':'basketitem',
										'html': entry
										});
				var basketContain = $('basketItems');
				elem.inject(basketContain);
				
				//update totals
				
				$('grandTotal').value = grandtotal;
				$('basketTotal').set('html','Total: &pound;'+grandtotal);
				$('ticketorder').value = ticketorder;
				
				//check validation
				validCheckout();
				
			//alert (entry);
				
				//now reset form
				form.reset();
				$('tickets').value = '';
				$('concessions').value = '';
				 $('detailTotal').empty();
				 if ($('fullText')) {
				 $('fullText').empty();
				 }
				 if ($('conText')) {
				$('conText').empty();
				 }
				 $('date').disabled = true;
				$('tickets').disabled = true;
				$('concessions').disabled = true;
				$('time').disabled = true;
				$('basket').disabled = true;
				 $('subTotal').value = '';
					$('errorChk').set('class','');	
					
					
			
			}
		}
		return false;
	

	
}

function validCheckout() {
//set vars

	var name = $('name').value;
	var daytel = $('daytel').value;
	var evetel = $('evetel').value;
	var total = $('grandTotal').value;
	
	if ((name != '') && (daytel != '') && (evetel != '') && (total > 0)) {
						$('checkout').disabled = false;									 
															 }
															 
									else {
									$('checkout').disabled = true;		
									}
	$('sendname').value = name;
	$('sendday').value = daytel;
	$('sendeve').value = evetel;
	
															 
	
}