
	var ticketTypeID 			= [];
	var ticketTypeName 			= [];
	var ticketTypePrice			= [];

	var ticketTypeAmount		= [];
	
	var orderRowTicketTypeID	= [];
	var orderRowTicketName 		= [];
	var orderRowTicketUserID 	= [];
	var orderRowTicketUserName 	= [];
	var orderRowTicketPrice 	= [];	

	// The method that is run after the entire DOM (page) has been loaded:
	document.observe("dom:loaded", function() {
	
		// gör en koll för att se om vi faktiskt är på rätt sida!
		if ($('ticketOrderTotalPrice'))
		{	
			getDataFromDatabase();
		
			renderTotalPrice();
		
			Event.observe( $('newOrderTicket'), 'change', changePriceForTicket);
		}
	});
	
	function addTicket()
	{
		addTicketToOrder( $('orderItemsForm') );
		return false;
	}

	function getDataFromDatabase()
	{
		
		//	Hämtar data från databasen via Ajax:
		new Ajax.Request(
			'includes/ajax/ticketTypeData.php', 
			{
				method: 'get',
		  		onSuccess: function(transport) {
		  			
					var ticketTypes = eval(transport.responseText);
					
					ticketTypes.each(function(ticketType, index){
					
						ticketTypeID.push(ticketType.id);
						ticketTypeName.push(ticketType.name);
						ticketTypePrice.push(ticketType.price);
						
					});
		  		}
			}
		);
	}

	function changePriceForTicket()
	{
		// Denna raden gör samma sak som loopen nedanför bara det att den exikveras
		// exstremt mycket snabbare, specielt om vi har många tickettypes i databasen!
		
		// Not needed anymore, the "newOrderPrice" field removed. 
		//$('newOrderPrice').value = ticketTypePrice[ticketTypeID.indexOf($('newOrderTicket').value)]
	
		/*for (var i = 0; i < ticketTypeID.length; i++)
		{
			if (ticketTypeID[i] == $('newOrderTicket').value)
			{
				$('newOrderPrice').value = ticketTypePrice[i];
			}
		}
		*/
	}

	function addTicketToOrder( form )
	{
	
		var id 	= form.ticket.value;
		var name = ticketTypeName[ticketTypeID.indexOf(id)];
		var userID 	= form.userID.value;
		var userName = form.userID.options[form.userID.selectedIndex].innerHTML;
		//var price	= form.price.value;	// Removed, better to use data from the price array:
		var price = ticketTypePrice[ticketTypeID.indexOf(id)];

		//	L?gg till ny rad i Biljett-arrayerna:
		//	orderRowTicketID.push( productID[addingProductArrayID] );
		orderRowTicketTypeID.push(id);
		orderRowTicketName.push( name );
		orderRowTicketUserID.push( userID );
		orderRowTicketUserName.push(userName);
		orderRowTicketPrice.push( price );
		
		renderOrderTicketTable();
		
		//	Vilken ?r den sista positionen i biljett-arrayerna? (dvs, pë vilken position hamnade den nya orderraden?)
		var lastPosInOrderArray = orderRowTicketName.length - 1;
		
		new Effect.Highlight('orderTicketRow' + lastPosInOrderArray, {
			startcolor: '#117711',
			endcolor: '#0b0b0b', 
			duration: 1.0
		});
		
		
//		updateOrderTotalSum();
		
	}
	
	
	function renderOrderTicketTable()
	{
		// Reset the ticketTypeAmount array.
		ticketTypeName.each(function(value, index){
			ticketTypeAmount[index] = 0;	
		});
		
		//	Radera alla rader i ordertabellen i HTML:
		$$('.orderTicketRow').each(function(item) {
			item.remove();
        });
		
		
		//	Radera alla rader i den gömda beställningslistan i HTML:
		$$('.orderItem').each(function(item) {
			item.remove();
        });
        
        //	Skapa nya rader f?r varje ticketTyp i ordern:
        orderRowTicketName.each(function(value, index){
        	var tr = Builder.node('tr', { 'class': 'orderTicketRow', id: 'orderTicketRow' + index });	// ska det vara index som aktuell parameter i delete-funktionen???
        	/*
        	var tdName = Builder.node('td', {}, orderSpecialOfferQuantity[index] + ' x ' + orderSpecialOfferName[index] );
        	var tdPrice = Builder.node('td', {}, orderSpecialOfferPrice[index] );
        	var tdRowfum = Builder.node('td', {}, orderSpecialOfferRowSum[index] );
        	*/
        	//	L?gg till varje kolumn:
			var tdTicket 		= Builder.node('td', { 'class': 'tdTicket' }, orderRowTicketName[index]);
			var tdUser 			= Builder.node('td', { 'class': 'tdUser' }, orderRowTicketUserName[index]);
			//var tdDiscount	= Builder.node('td', { id: 'orderRow' + productPosInOrderArray + '_Discount' }, orderRowDiscount);	//	ej implementerat ?nnu...
			var tdPrice			= Builder.node('td', { 'class': 'tdPrice' }, orderRowTicketPrice[index]);
			var tdDelete		= Builder.node('td', { 'class': 'tdDelete', onclick: 'removeTicketFromOrder(' + index + ');'  });
			
			
			 var spanDelete		= Builder.node('span', { 'class': 'value', 'class': 'orderTicketRowDelete' }, "X");

			//	L?gg till dataf?lten:
/*			var nameValue		= Builder.node('span', { class: 'value nameValue', id: 'orderSpecialOfferRow' + index + '_ProductName' }, orderTicketName[index]);
			var priceValue		= Builder.node('span', { class: 'value', id: 'orderTicketRow' + index + '_ProductPrice' }, orderTicketPrice[index]);
			var quantityValue	= Builder.node('span', { class: 'value', id: 'orderTicketOfferRow' + index + '_Quantity' }, orderTicketQuantity[index]);
			var rowSumValue		= Builder.node('span', { class: 'value', id: 'orderTicketRow' + index + '_Sum' }, orderTicketRowSum[index]);
        	
			tdName.insert({top: quantityValue});
			tdName.insert({bottom: nameValue});
			tdPrice.insert({top: priceValue});
			tdRowSum.insert({top: rowSumValue});
*/			
			tdDelete.insert({top: spanDelete});

        	tr.appendChild(tdTicket);
        	tr.appendChild(tdUser);
        	tr.appendChild(tdPrice);
        	tr.appendChild(tdDelete);
        	
        	$('orderTicketTable').appendChild(tr);
			
			// Adding the hidden input fields, so that we can access the data after the submit:
			var item 			= Builder.node('span', { 'class': 'orderItem' });
			var itemTicketID 	= Builder.node('input', { 'type': 'hidden', 'name': 'orderItem_' + index + '_ticketTypeID', 'value': orderRowTicketTypeID[index] });
			var itemPrice 		= Builder.node('input', { 'type': 'hidden', 'name': 'orderItem_' + index + '_price', 'value': orderRowTicketPrice[index] });
			var itemUserID 		= Builder.node('input', { 'type': 'hidden', 'name': 'orderItem_' + index + '_userID', 'value': orderRowTicketUserID[index] });
			
			item.appendChild(itemTicketID);
			item.appendChild(itemPrice);
			item.appendChild(itemUserID);
			$('orderItems').appendChild(item);

			ticketTypeAmount[ticketTypeName.indexOf(value)]++;
			
        });

		$('total_amount_of_tickets').value = orderRowTicketPrice.length;

		renderTotalAmountOfTickets();
		renderTotalPrice();
	}

	function removeTicketFromOrder( removingOrderRow ){
	
		orderRowTicketTypeID.splice(removingOrderRow, 1);
		orderRowTicketName.splice(removingOrderRow, 1);
		orderRowTicketUserID.splice(removingOrderRow, 1);
		orderRowTicketPrice.splice(removingOrderRow, 1);
		orderRowTicketUserName.splice(removingOrderRow, 1);
	
		renderOrderTicketTable();
		
	}

	function renderTotalAmountOfTickets()
	{

		$$('.amountOfTickets').each(function(item) {
			item.remove();
        });

		ticketTypeID.each(function(ID, index){
			
			if ( ticketTypeAmount[index]  == 0)
				var spanAmount = Builder.node('span', { 'class': 'value', 'class': 'amountOfTickets' }, "0");
			else
				var spanAmount = Builder.node('span', { 'class': 'value', 'class': 'amountOfTickets' }, ticketTypeAmount[index]);

			$(ID + "-biljett-antal").insert({bottom: spanAmount});
		});
		
		
		
		
		// Goes through each of the spearet tickets and checks for what index that type of ticket has and adds one to the array 
		// that holds the count of how meany tickets of that type that has bean bought.
	/*	for (var i = 0; i < orderRowTicketName.length; i++)
		{
			
			ticketTypeNameAmount[ticketTypeName.indexOf(orderRowTicketName[i])];
			ticketTypeNameAmount[]
			if (ticketTypeNameAmount[ticketTypeName.indexOf(orderRowTicketName[i])] == null)
				ticketTypeNameAmount[ticketTypeName.indexOf(orderRowTicketName[i])] = 1;
			else
			{
				ticketTypeNameAmount[ticketTypeName.indexOf(orderRowTicketName[i])]++;
			}
		}
		
		
		ticketTypeNameAmount.each(function(item){
			
			alert("-->" + item);
			
		});
	/*	
		for (var i = 0; i < ticketTypeNameAmount.length; i++)
		{
			alert(ticketTypeNameAmount[i]);
			var spanAmount = Builder.node('span', { class: 'value' }, ticketTypeNameAmount[i]);
			//$(ticketTypeName[i] + "-biljett-antal").insert({bottom: spanAmount});
			var lol = ticketTypeName[i] + "-biljett-antal";
			alert(lol);
			$(lol).insert({bottom: spanAmount});
		}
		//$('ticketOrderTotalPrice').value = totalPrice;
	*/	
	}

	function renderTotalPrice()
	{
		var totalPrice = 0;
		for (var i = 0; i < orderRowTicketPrice.length; i++)
		{
			totalPrice += parseInt(orderRowTicketPrice[i]);
		}
		$('orderTotal').innerHTML = totalPrice;
		$('ticketOrderTotalPrice').value = totalPrice;
		
	}	

