
var ProductUpdater = Class.create();

ProductUpdater.attributes = [ "n", "product_id", "retail_price", "list_price", "discount", "final_price", "image_filename", "big_thumbnail", "small_thumbnail", "caption", "inventory", "sku" ];

ProductUpdater.prototype = {

	initialize: function() 
	{
	},
		
	ajaxUpdate: function(ajaxResponse) 
	{
		this.handleReturn(ajaxResponse.childNodes[0]);
	},
		
	handleReturn: function(returnValue) 
	{
		var n;
		var product_id;
		var retail_price;
		var list_price;
		var discount;
		var final_price;
		var image_filename;
		var big_thumbnail;
		var small_thumbnail;
		var caption;
		var inventory;
		var sku;

		for ( var i = 0 ; i < ProductUpdater.attributes.length ; i++ )
		{
			var attr = ProductUpdater.attributes[i];
			var value = returnValue.getAttribute(attr);

			if (attr == "n")
			{
				n = value;
			}
			else if (attr == "product_id")
			{
				product_id = value;
			}
			else if (attr == "retail_price")
			{
				retail_price = value;
			}
			else if (attr == "list_price")
			{
				list_price = value;
			}
			else if (attr == "discount")
			{
				discount = value;
			}
			else if (attr == "final_price")
			{
				final_price = value;
			}
			else if (attr == "image_filename")
			{
				image_filename = value;
			}
			else if (attr == "big_thumbnail")
			{
				big_thumbnail = value;
			}
			else if (attr == "small_thumbnail")
			{
				small_thumbnail = value;
			}
			else if (attr == "caption")
			{
				caption = value;
			}
			else if (attr == "inventory")
			{
				inventory = value;
			}
			else if (attr == "sku")
			{
				sku = value;
			}
		}

		this.updateElements(n, product_id, retail_price, list_price, discount, final_price, image_filename, big_thumbnail, small_thumbnail, caption, inventory, sku);
	},

	updateElements: function (n, product_id, retail_price, list_price, discount, final_price, image_filename, big_thumbnail, small_thumbnail, caption, inventory, sku)
	{
		hiddenProductId = document.getElementById('product_id' + n);
		if (hiddenProductId != null)
		{
			hiddenProductId.value = product_id;
		}

		suggestedPrice = document.getElementById('suggested_price' + n);
		if (suggestedPrice != null)
		{
			suggestedPrice.innerHTML = formatCurrency(retail_price);
		}

		ourPrice = document.getElementById('our_price' + n);
		if (ourPrice != null)
		{
			ourPrice.innerHTML = formatCurrency(list_price);
		}

		discountDiv = document.getElementById('discount' + n);
		if (discountDiv != null)
		{
			discountDiv.innerHTML = formatCurrency(discount);
		}

		savingsDiv = document.getElementById('savings' + n);
		if (savingsDiv != null)
		{
			savingsDiv.innerHTML = formatCurrency(parseFloat(list_price) - parseFloat(discount));
		}

		finalPrice = document.getElementById('final_price' + n);
		if (finalPrice != null)
		{
			finalPrice.innerHTML = formatCurrency(final_price);
		}

		imageLink = document.getElementById('image_link' + n);
		if (imageLink != null)
		{
			if (imageLink.name == "product")
			{
				imageLink.href = image_filename;
			}
		}

		imageImg = document.getElementById('image_thumbnail' + n);
		if (imageImg != null)
		{
			if (imageImg.name == "big_thumbnail")
			{
				imageImg.src = big_thumbnail;
			}
			else if (imageImg.name == "small_thumbnail")
			{
				imageImg.src = small_thumbnail;
			}
		}

		if (parseInt(inventory) < 1)
		{

			noticeDiv = document.getElementById('notice_div' + n);
			if (noticeDiv != null)
			{
				noticeDiv.style.display = "block";
				noticeDiv.innerHTML = "<p>Sorry, this product feature selection is temporarily out of stock.  Please check again later.  Thanks.<br/><br/><a href=\"javascript:stockNotification("+n+");\">Email me when this is in stock.</a></p>";

				orderBlock = document.getElementById('order_block' + n);
				orderBlock.style.display = "none";

				// get last selected features
			}
		}
		else
		{
			noticeDiv = document.getElementById('notice_div' + n);
			if (noticeDiv != null)
			{
				noticeDiv.style.display = "block";
				noticeDiv.innerHTML = "<p><strong>Inventory Available:</strong> " + inventory + "&nbsp;&nbsp;<a href=\"contact_us.html?subject=need_more&amp;product_id="+product_id+"\">Need more?</a></p>";

				orderBlock = document.getElementById('order_block' + n);
				if (orderBlock != null)
				{
					orderBlock.style.display = "block";
				}
			}
		}

		skuBlock = document.getElementById('sku' + n);
		if (skuBlock != null)
		{
			skuBlock.innerHTML = sku;
		}
	}
};