/**
 * @author saiperchard
 */

// Define variables

var ticketPrice = Number("51.50");

// Add ticket

function addFormField(){
	
	var currentTicketCount = Number($('OrderTicketCount').readAttribute('value')); // Get current ticket count from hidden input field
	
	if (currentTicketCount < 10) { // Maximum of 10 tickets per order
	
		// Create the element
		var newTicket = new Element('div', {
				'id': 'Ticket_' + currentTicketCount, 'style' : 'display: none;' // Hidden initially, so we can fade it in with an effect
			});
			
		// Set element contents
		var content = '\
			<table>\
			  <tr>\
			    <th colspan="2">Ticket #' + (currentTicketCount + 1) + '</th>\
			  </tr>\
			  <tr>\
			    <td class="light">First Name</td>\
				<td class="inputcell"><input name="data[Ticket][' + currentTicketCount + '][firstname]" type="text" class="required" maxlength="255" value="" id="TicketFirstname' + (currentTicketCount + 1) + '" /></td>\
			  </tr>\
			  <tr>\
			    <td class="light">Last Name</td>\
				<td class="inputcell"><input name="data[Ticket][' + currentTicketCount + '][lastname]" type="text" class="required" maxlength="255" value="" id="TicketLastname' + (currentTicketCount + 1) + '" /></td>\
			  </tr>\
			  <tr>\
			    <td class="light">Email Address</td>\
				<td class="inputcell"><input name="data[Ticket][' + currentTicketCount + '][email]" type="text" maxlength="255" value="" id="TicketEmail' + (currentTicketCount + 1) + '" /></td>\
			  </tr>\
			</table>';
		
		newTicket.update(content);
		
		// Insert element
		$('ticketContainer').insert({
				'bottom': newTicket
			});
			
		// Display the element
		new Effect.BlindDown('Ticket_' + currentTicketCount, { duration: 0.7 });
		
		// Update the ticket counter and total
		$('OrderTicketCount').writeAttribute('value', currentTicketCount + 1);
		$('OrderTheTotal').writeAttribute('value', (currentTicketCount + 1) * ticketPrice);
		
		// Update orderTotal
		$('quantity').update(currentTicketCount + 1);
		$('total').update((currentTicketCount + 1) * ticketPrice);
		
		// Re-create the validation object so the new field will be included
		new Validation('OrderExpressCheckout/1Form', {immediate: true});
	
	}
 
}

// Remove ticket

function removeFormField(){
	
	var currentTicketCount = Number($('OrderTicketCount').readAttribute('value'));
	
	if (currentTicketCount > 0) // Can't remove a ticket if there are none there!
	{
		var newTicketCount = currentTicketCount - 1; // The new ticket count after removing one
		
		// Remove the element
		new Effect.BlindUp('Ticket_' + newTicketCount, { duration: 0.7, afterFinish: function() {
			$('Ticket_' + newTicketCount).remove();
		}});	
		
		// Update the ticket counter and total
		$('OrderTicketCount').writeAttribute('value', newTicketCount);
		$('OrderTheTotal').writeAttribute('value', (newTicketCount * ticketPrice));
		
		// Update orderTotal
		$('quantity').update(newTicketCount);
		$('total').update(newTicketCount * ticketPrice);
	
	}

}
 
 