
var TicketForm = Class.create({
	initialize: function(eventInfo) {
		this.textunits = new Array();
		this.eventInfo = eventInfo;
		this.BEST_AVAILABLE='BEST_AVAILABLE';
		this.sectionSelect = $('section_'+this.eventInfo.eventID);
		
		this.eventInfo.prices.priceType.each(function(priceType) {
			var quantitySelect = this.getQuantitySelectBox(priceType);
			if (quantitySelect) {
				value = quantitySelect.options[quantitySelect.selectedIndex].value;
				if (value > 0) {
					this.preselectedPriceType = priceType.code;
					this.preselectedQuantity = value;
				}
			}
		}, this);
		
	},
	
	getEventCode:function() {
		return this.eventInfo.eventID;
	},
	
	setTextunit:function(name,value) {
		this.textunits[name] = value;
	},
	
	getCategorySelectBox: function(priceType) {
		return $('category_'+this.eventInfo.eventID+'_'+priceType.code);;
	}, 
	
	getQuantitySelectBox: function(priceType) {
		return $('quantity_'+this.eventInfo.eventID+'_'+priceType.code);
	}, 
	
	onCategoryChange: 	function () {
		var selectedCategories = new Array();
		
		this.eventInfo.prices.priceType.each(function(priceType) {
			var categorySelect = this.getCategorySelectBox(priceType);
			value = categorySelect.options[categorySelect.selectedIndex].value;
			if (value != this.BEST_AVAILABLE && selectedCategories.indexOf(value) < 0) {
				selectedCategories.push(value);
			}
		}, this);
		
		this.filterSections(selectedCategories);
	},
	
	
	
	filterSections : function(selectedCategories) {
		var availableSections = this.eventInfo.prices.section.clone();
		selectedCategories.each(function(categoryNumber){
			filteredSections = new Array();
			availableSections.each(function(section) {
				if (section.priceCategory.indexOf(categoryNumber) >= 0) {
					filteredSections.push(section);
				}
			}, this);
			availableSections = filteredSections;
		});
		this.setSections(availableSections);		
	},
	
	
	
	setSections: function(availableSections) {
		selectedSection = this.sectionSelect.options[this.sectionSelect.selectedIndex].value;

		this.emptySelect(this.sectionSelect);
		
		// To keep the current selection
		var selectedIndex = 0;
		
		availableSections.each(function(section){
			if (section.code == selectedSection) {
				selectedIndex = this.sectionSelect.options.length;
			}
			this.sectionSelect.options[this.sectionSelect.options.length] = new Option(section.name, section.code);
		}, this);
		this.sectionSelect.selectedIndex = selectedIndex;
	},
	
	
	onSectionChange: function() {
		this.setCategories(this.getAvailableCategories());
	},
	
	setCategories: function(availableCategories) {
		this.eventInfo.prices.priceType.each(function(priceType) {
			var categorySelect = this.getCategorySelectBox(priceType);
			var quantitySelect = this.getQuantitySelectBox(priceType);
			
			// To keep the current selection
			selectedCategory = categorySelect.options[categorySelect.selectedIndex].value;

			this.emptySelect(categorySelect);
			var selectedIndex = 0;
			availableCategories.each(function(category){
				var categorySelect = this.getCategorySelectBox(priceType);
				if (category.number == selectedCategory) {
					selectedIndex = categorySelect.options.length;
				}
				var price = this.getPrice(category.number,priceType.number);
				if (price) { 
					categorySelect.options[categorySelect.options.length] = new Option(category.code.substr(0,40) + ' - ' + price.priceFormatted, category.number);
				}
			}, this);
			if (categorySelect.options.length < 2) {
				// This price type is not available for this category
				// (There is always at least one item in the select box)
				categorySelect.disabled = true;	
				quantitySelect.selectedIndex = 0;
				quantitySelect.disabled = true;
			} else {
				categorySelect.disabled = false;	
				quantitySelect.disabled = false;
			}
			categorySelect.selectedIndex = selectedIndex;
		}, this);
	},
	
	resetCategories: function() {
		this.setCategories(this.eventInfo.prices.priceCategory);
	},
	
	resetSections: function() {
		this.setSections(this.eventInfo.prices.section);
	},
	
	resetAll: function() {

		this.resetCategories();
		this.resetSections();
		this.sectionSelect.selectedIndex = 0;
		this.eventInfo.prices.priceType.each(function(priceType) {
			var categorySelect = this.getCategorySelectBox(priceType);
			var quantitySelect = this.getQuantitySelectBox(priceType);
			if (priceType.code == this.preselectedPriceType) {
				quantitySelect.selectedIndex = this.preselectedQuantity;
			} else {
				quantitySelect.selectedIndex = 0;
			}
			categorySelect.selectedIndex = 0;
		}, this);
	},
		
	getAvailableCategories: function() {
		var selectedSection = this.sectionSelect.options[this.sectionSelect.selectedIndex].value;

		if (selectedSection == this.BEST_AVAILABLE) {
			// No section selected, all categories are available 
			return this.eventInfo.prices.priceCategory;
		}

		// Find categories available on the selected section
		var availableCategories = new Array();
		for (k = 0; k < this.eventInfo.prices.section.length; k++) {
			var section = this.eventInfo.prices.section[k];
			if (section.code == selectedSection) {
				for (i = 0; i < section.priceCategory.length; i++) {
					var availableCategoryNumber = section.priceCategory[i];
					for (j = 0; j < this.eventInfo.prices.priceCategory.length; j++) {
						category = this.eventInfo.prices.priceCategory[j];
						if (category.number == availableCategoryNumber) {
							availableCategories.push(category);
						}						
					}
				}
			}
		}

		return availableCategories;
	},
	
	emptySelect: function(select) {
		while (select.options[0]) {
			select.options[0] = null;
		}
		select.options[0] = new Option(this.textunits['BEST_AVAILABLE'], this.BEST_AVAILABLE);
	},
	
	getPrice: function(priceCategory, priceType) {
		for (i = 0; i < this.eventInfo.prices.price.length; i++) {
			var price = this.eventInfo.prices.price[i];
			if (price.priceCategory == priceCategory && price.priceType == priceType) {
				 return price;
			}
		}
		return null;
	},
	
	adjacentSeatsPopup: function(formName) {
		var popup = new PopupError();
		popup.setTitle(this.textunits['OOPS']);
		popup.setContent(this.textunits['MAY_RETURN_NON_ADJACENT_SEATS']);
		// NOTE: This depends on the form name in HTML
		
		if (TicketForm.useRecaptcha) {
			popup.addButton(this.textunits['OK'], 'button', "Captcha.prepareCaptcha(); $('popup-overlay').hide(); $('popup-window').hide();");
		} else {
			popup.addButton(this.textunits['OK'], 'button', 'document.forms.'+formName+'.submit()');
		}
		popup.addButton(this.textunits['CANCEL'], 'button-secondary');
		popup.generate();
		popup.show();
	},
	
	// Is it possible to find adjacent seats with the current choises
	adjacentSeatsPossible: function() {
		var selectedPriceTypes = new Array();
		
		for (i = 0; i < this.eventInfo.prices.priceType.length; i++) {
			var priceType = this.eventInfo.prices.priceType[i];
			var quantitySelect = this.getQuantitySelectBox(priceType);
			if (quantitySelect.value > 0) {
				selectedPriceTypes.push(priceType);
			}
		}
		
		if (selectedPriceTypes.length > 1) {
			// Find out common combinations of categories and blocking codes
			// between the selected price types
			
			var availableCategories = this.getAvailableCategories();
			
			var commonCategoriesAndBlockings = new Array();
			var i, j, k;
			for (i = 0; i < selectedPriceTypes.length; i++) {
				var priceType = selectedPriceTypes[i];
				var blockings = priceType.blockingCodes;
				var categoryAndBlockings = new Array();
				
				var availableCategoryNumbers = new Array();
				var categorySelect = this.getCategorySelectBox(priceType);
				if (categorySelect.selectedIndex > 0) {
					availableCategoryNumbers.push(categorySelect.value);
				} else {
					for (j = 0; j < availableCategories.length; j++) {
						availableCategoryNumbers.push(String(availableCategories[j].number));
					}
				}
				
				
				for (j = 0; j < availableCategoryNumbers.length; j++) {
					var price = this.getPrice(availableCategoryNumbers[j],priceType.number);
					if (price) { 
						if (blockings.length == 0) {
							categoryAndBlockings.push(availableCategoryNumbers[j]);
						} else {
							for (k = 0; k < blockings.length; k++) {
								var blockingCode = blockings[k];
								categoryAndBlockings.push(availableCategoryNumbers[j] + '$$' + blockingCode);
							}
						}
					}
				}
				if (i == 0) {
					commonCategoriesAndBlockings = categoryAndBlockings;
				} else {
					commonCategoriesAndBlockings = commonCategoriesAndBlockings.intersect(categoryAndBlockings);
				}
			}
			if (commonCategoriesAndBlockings.length == 0) {
				// Adjacent seats are not possible
				return false;
			}
		}
		
		
		return true;
	},
	
	onSubmit: function() {
		var adjacentBox = $('accept_adjacent');
		if (adjacentBox && !adjacentBox.checked) {
			if (!this.adjacentSeatsPossible()) {
				this.adjacentSeatsPopup('searchForTickets');
				this.markNotBestAvailable();

				return false;
			}
		}
		
		if (TicketForm.useRecaptcha) {
			Captcha.prepareCaptcha();
			return false;
		}
		
		return true;
	},
	
	markQuantity: function(priceTypeCode) {
		this.eventInfo.prices.priceType.each(function(priceType) {
			if (priceType.code == priceTypeCode) {
				var quantitySelect = this.getQuantitySelectBox(priceType);
				quantitySelect.addClassName('error');
			}
		},this);

	},

	markSelections: function() {
		this.eventInfo.prices.priceType.each(function(priceType) {
			var categorySelect = this.getCategorySelectBox(priceType);
			var quantitySelect = this.getQuantitySelectBox(priceType);
			if (quantitySelect.value > 0) {
				quantitySelect.addClassName('error');
				categorySelect.addClassName('error');
			}
		},this);
	},

	markNotBestAvailable: function() {
		this.eventInfo.prices.priceType.each(function(priceType) {
			var categorySelect = this.getCategorySelectBox(priceType);
			if (categorySelect.value != this.BEST_AVAILABLE) {
				categorySelect.addClassName('error');
			}
		},this);
	}
	 
});

TicketForm.showTooltip =  function(parent, divName) {
	var ext = $(divName);
	var anchor = parent.positionedOffset();
	ext.style.left = 20 + anchor[0] +'px';
	ext.style.top = -20 + anchor[1] +'px';						 
	ext.show();
};

TicketForm.useRecaptcha = false;



