/* Product */

function Product_getId() { return this.productId; }
function Product_getName() { return this.productName; }
function Product_getPrice() { return this.productPrice; }
function Product_getData() { return this.productData; }
function Product_getDateEnabled() { return this.productDateEnabled; }
function Product_getCategory() { return this.category; }

function Product(productId, productName, productPrice, productData, dateEnabled) {

	this.productId = productId;
	this.productName = productName;
	this.productPrice = productPrice;
	this.productData = productData;
	this.category = null;
	this.productDateEnabled = dateEnabled;

	this.getId = Product_getId;
	this.getName = Product_getName;
	this.getPrice = Product_getPrice;
	this.getData = Product_getData;
	this.getCategory = Product_getCategory;
	this.getDateEnabled = Product_getDateEnabled;
}

/* Category */

function Category_numProducts() { return this.products.length; }
function Category_getId() { return this.categoryId; }
function Category_getName() { return this.categoryName; }
function Category_getProduct(index) { return this.products[index]; }

function Category_addProduct(product) {
	this.products[this.products.length] = product;
	product.category = this;
}

function Category(categoryId, categoryName) {
	this.categoryId = categoryId;
	this.categoryName = categoryName;
	this.products = new Array();
	
	this.addProduct = Category_addProduct;
	this.numProducts = Category_numProducts;
	this.getId = Category_getId;
	this.getProduct = Category_getProduct;
}

/* ProductData */

function ProductData_getProductById(productId) {
	for (i = 0; i < this.products.length; ++i) {
		product = this.products[i];
		if(product.getId() == productId) {
			return product;
		}
	}
	return null;
}

function ProductData_getCategoryById(categoryId) {
	for (i = 0; i < this.categories.length; ++i) {
		category = this.categories[i];
		if (category.getId() == categoryId) {
			return category;
		}
	}
	return null;
}

function ProductData_addProduct(productId, categoryId, productName, productPrice, productData, dateEnabled) {
	category = this.getCategoryById(categoryId);
	if (category) {
		product = new Product(productId, productName, productPrice, productData,dateEnabled);
		category.addProduct(product);
		this.products[this.products.length] = product;
	}
}

function ProductData_addCategory(categoryId, categoryName) {
	this.categories[this.categories.length] = new Category(categoryId, categoryName);
}

function ProductData_numCategories() { return this.categories.length; }

function ProductData() {
	this.categories = new Array();
	this.products = new Array();

	this.addCategory = ProductData_addCategory;
	this.addProduct = ProductData_addProduct;
	this.getCategoryById = ProductData_getCategoryById;
	this.getProductById = ProductData_getProductById;
	this.numCategories = ProductData_numCategories;
}

/* ProductWidget */

function ProductWidget_updateTotal() {
	quantity = this.getQuantity();
	price = this.priceInput.value;
	var total = quantity * price;
	this.totalInput.value = total;
}

function ProductWidget_setProductSelectOption(index, value, text) {
	this.productSelect.options[index].text = text;
	this.productSelect.options[index].value = value;
}

function ProductWidget_updateProductSelect(categoryId) {
	category = this.productData.getCategoryById(categoryId);
	if (category) {
		numProducts = category.numProducts();
		this.productSelect.length = numProducts + 1;
		this.setProductSelectOption(0, "", this.selectProductText);
		for (i = 0; i < numProducts; ++i) {
			product = category.getProduct(i);
			this.setProductSelectOption(i + 1, product.getId(), product.getName());
		}
		this.productSelect.selectedIndex = 0;
	}
	this.onChangeProduct();
}

function ProductWidget_getSelectValue(selectWidget) {
	return selectWidget.options[selectWidget.selectedIndex].value;
}

function ProductWidget_getValue(widget) {
	if (widget.options) { // assume select
		return this.getSelectValue(widget);
	}
	// assume input
	return widget.value;
}

function ProductWidget_getCategoryId() { return this.getSelectValue(this.categorySelect); }
function ProductWidget_getProductId() { return this.getSelectValue(this.productSelect); }
function ProductWidget_getQuantity() { return this.getValue(this.quantityInput); }

function ProductWidget_onChangeCategory() {
	categoryId = this.getCategoryId();
	this.updateProductSelect(categoryId);
	this.updateTotal();
}

function ProductWidget_onChangeProduct() {
	productId = this.getProductId();
	product = this.productData.getProductById(productId);

	if (product) {

		if(product.getDateEnabled()=="1"){	
			this.ticketdate_enabled.value=1;
			toggleBox('datediv',1);
		}
		else{
			this.ticketdate_enabled.value=0;
			toggleBox('datediv',0);
		}

		this.priceInput.value = product.getPrice();
	} else {
		this.priceInput.value = 0;
	}
	this.updateTotal();
}

function ProductWidget_onChangeQuantity() {
	this.updateTotal();
}

function ProductWidget_getSelectedProduct() {
	productId = this.getSelectValue(this.productSelect);
	product = this.productData.getProductById(productId);
	return product;
}

function ProductWidget_setCategoryFromProduct() {
	product = this.getSelectedProduct();
	if (product) {
		pId = "" + product.getId();
		category = product.getCategory();
		categoryId = category.getId();
		numCategories = this.productData.numCategories();
		for (i = 0; i < numCategories; ++i) {
			if (this.categorySelect.options[i + 1].value == categoryId)
				this.categorySelect.selectedIndex = i + 1;
		}
		this.updateProductSelect(categoryId);
		numProducts = category.numProducts();
		for (i = 0; i < numProducts; ++i) {
			if (this.productSelect.options[i + 1].value == pId)
				this.productSelect.selectedIndex = i + 1;
		}
		this.onChangeProduct();
	} else {
		this.categorySelect.selectedIndex = 0;
		this.productSelect.length = 1;
	}
}

function ProductWidget(productData, categorySelect, productSelect, priceInput, quantityInput, totalInput,ticketdate,ticketdate_enabled) {
	this.productData = productData;
	this.categorySelect = categorySelect;
	this.productSelect = productSelect;
	this.priceInput = priceInput;
	this.quantityInput = quantityInput;
	this.totalInput = totalInput;
	this.ticketdate = ticketdate;
	this.ticketdate_enabled = ticketdate_enabled;
	
	this.selectProductText = "please select a product";
	
	this.getSelectedProduct = ProductWidget_getSelectedProduct;
	this.setCategoryFromProduct = ProductWidget_setCategoryFromProduct;
	this.getCategoryId = ProductWidget_getCategoryId;
	this.getProductId = ProductWidget_getProductId;
	this.getQuantity = ProductWidget_getQuantity;
	this.getValue = ProductWidget_getValue;
	this.getSelectValue = ProductWidget_getSelectValue;
	this.updateProductSelect = ProductWidget_updateProductSelect;
	this.updateTotal = ProductWidget_updateTotal;
	this.setProductSelectOption = ProductWidget_setProductSelectOption;
	
	this.onChangeCategory = ProductWidget_onChangeCategory;
	this.onChangeProduct = ProductWidget_onChangeProduct;
	this.onChangeQuantity = ProductWidget_onChangeQuantity;
	
	widget = this;
	this.categorySelect.onchange = function() { return widget.onChangeCategory(); };
	this.productSelect.onchange = function() { return widget.onChangeProduct(); };
	this.quantityInput.onchange = function() { return widget.onChangeQuantity(); };
	
	this.setCategoryFromProduct();
}
