var formElements;
$(function() {

	formElements = new FormElements();

	$('.order_form .availability').each(function() {
		detail  = $(this).find('.detail');
		warning = $(this).find('.warning');

		if (detail.html().length == 0) {
			detail.hide();
		}
		if (warning.html().length == 0) {
			warning.hide();
		}
	});

	$('.special_offer_quantity').change(function(){
		maxQty = $(this).parent().find('input[type="hidden"]').val();

		if ($(this).val() > maxQty) {
			$(this).val(maxQty);
		}
	});
});


function FormElements()
{
	this.key = 0;
	this.currencyTypeId = $("#currency_type_id").val();
	this.currencySymbol = $("#currency_symbol").val();
	this.customerDiscount = $("#customer_discount").val();
	this.rowElements = new Array();
	idRoot = 'catalog_number_';

	parseElements = $(".cat_number");

	var j = 0;
	for (i = 0; i < parseElements.length; i++) {
		currentId = parseElements[i].id
		if (currentId.substr(0,idRoot.length) == idRoot) {
			n = currentId.substr(idRoot.length);
			this.rowElements[j] = new RowElement(n);
			j++;
		}
	}
}

FormElements.prototype.changeItem = function(n)
{
	this.key = n;
	row = this.getRowElement(n);

	if (row != 'undefined') {
		catalogNumber = row.catalogNumber.val();

		row.catalogNumber.css("background", "#fff");

		if (catalogNumber.length == 7) {
			data = {catalog_number:catalogNumber, currency_type_id:this.currencyTypeId, currency_symbol:this.currencySymbol, customer_discount:this.customerDiscount};

			$.getJSON('/lib/ajax/order_form.php', data, function(item) {
				row.parseItem(item);
				return;
			});
		}

		row.catalogNumber.css("background", "#fffabf");
		row.description.text('');
		row.priceIdValues = new Array();
		row.priceValues = new Array('');
		row.unitSizeValues = new Array('N/A');
		row.quantity.val('');
		row.setPrice(0);
		row.setAvailability();
	}
}


FormElements.prototype.getRowElement = function(n)
{
	for (i = 0; i < this.rowElements.length; i++) {
		if (this.rowElements[i].rowNumber == n) {
			return this.rowElements[i];
		}
	}
}

function RowElement(n)
{
	this.rowNumber = n;
	this.currentPrice = 0;
	this.priceValues = new Array();
	this.priceIdValues = new Array();
	this.unitSizeValues = new Array();

	this.catalogNumber = $("#catalog_number_" + n);
	this.itemId = $("#item_id_" + n);
	this.description = $("#description_" + n);
	this.availability = $('#availability_' + n);
	this.unitSize = $("#unit_size_" + n);
	this.priceId = $("#price_id_" + n);
	this.priceText = $("#price_text_" + n);
	this.quantity = $("#quantity_" + n);

	this.quantity.keypress(function(event){return (event.keyCode == 13) ? false : true;});

	this.catalogNumber.keyup(function(){formElements.changeItem(eval(n))});
	this.unitSize.change(function(){formElements.rowElements[eval(n)].changeSize()});
	this.quantity.keyup(function() {formElements.rowElements[eval(n)].setAvailability()});

	return this;
}

RowElement.prototype.changeSize = function()
{
	value = this.unitSize.val();
	key = 0;
	for (i = 0; i < this.unitSizeValues.length; i++) {
		if (value == this.unitSizeValues[i]) {
			key = i;
			break;
		}
	}

	this.setPrice(key);
	this.setAvailability();
}

RowElement.prototype.setPrice = function(j)
{
	if (this.priceValues[j] != 'undefined') {
		this.priceId.val(this.priceIdValues[j]);
		this.priceText.text(this.priceValues[j]);

		html = '';
		for (i = 0; i < this.unitSizeValues.length; i++) {
			sel = (i == j) ? ' selected="selected"' : '';
			html = html + '<option value="' + this.unitSizeValues[i] + '"' + sel + '>' + this.unitSizeValues[i] + '</option>';
		}

		this.unitSize.empty();
		this.unitSize.append(html);
	}
}

RowElement.prototype.setAvailability = function()
{
	qty     = parseInt(this.quantity.val());
	summary = this.availability.find('.summary');
	detail  = this.availability.find('.detail');
	warning = this.availability.find('.warning');

	if (!qty > 0) {
		summary.html('')
		detail.html('').hide();
		warning.html('').hide();
		return;
	}

	params  = {price_id:this.priceId.val(),quantity:qty};

	$.getJSON('/lib/ajax/quantity_availability.php', params, function(json) {
		if (json.summary) {
			summary.html(json.summary);
		}
		if (json.detail) {
			detail
				.html(json.detail.replace(/\n/g, '<br />'))
				.show();
		}
		if (json.warning) {
			warning
				.html(json.warning.replace(/\n/g, '<br />'))
				.show();
		} else {
			warning
				.html('')
				.hide();
		}
	});
}

RowElement.prototype.parseItem = function(item)
{
	if (item.item_id) {
		this.catalogNumber.css("background", "#fff");
		this.itemId.val(item.item_id);
		this.description.text(item.description.replace('<br />', '\n'));
		this.priceIdValues = new Array();
		this.priceValues = new Array();
		this.unitSizeValues = new Array();

		for (i = 0; i < item.prices.length; i++) {
			this.priceIdValues[i]        = item.prices[i]['price_id'];
			this.priceValues[i]          = item.prices[i]['price'];
			this.unitSizeValues[i]       = item.prices[i]['unit_size'];
		}

		this.quantity.val(1);
		this.setPrice(0);
		this.setAvailability();
	} else {
		this.description.text('').append('<em>Invalid Catalog Number</em>');
	}
}
