
var ProductUpdater = Class.create();

ProductUpdater.attributes = [ "n", "product_id", "retail_price", "list_price", "points_price", "discount", "final_price", "image_filename", "big_thumbnail", "small_thumbnail", "caption", "inventory", "batch_processing", "sku", "points_program", "minimal_inventory_available", "store_name" ];

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 points_price;
		var discount;
		var final_price;
		var image_filename;
		var big_thumbnail;
		var small_thumbnail;
		var caption;
		var inventory;
		var batch_processing;
		var sku;
		var points_program;
		var minimal_inventory_available;
		var store_name;

		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 == 'points_price')
			{
				points_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 == "batch_processing")
			{
				batch_processing = value;
			}
			else if (attr == "sku")
			{
				sku = value;
			}
			else if (attr == 'points_program')
			{
				points_program = value;
			}
			else if (attr == "minimal_inventory_available")
			{
				minimal_inventory_available = value;
			}
			else if (attr == "store_name")
			{
				store_name = value;
			}
		}

		this.updateElements(n, product_id, retail_price, list_price, points_price, discount, final_price, image_filename, big_thumbnail, small_thumbnail, caption, inventory, batch_processing, sku, points_program, minimal_inventory_available, store_name);
	},

	updateElements: function (n, product_id, retail_price, list_price, points_price, discount, final_price, image_filename, big_thumbnail, small_thumbnail, caption, inventory, batch_processing, sku, points_program, minimal_inventory_available, store_name)
	{
		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)
		{
			if (points_program == '1')
			{
				ourPrice.innerHTML = points_price;
			}
			else
			{
				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)
		{
			if (points_program == '1')
			{
				finalPrice.innerHTML = points_price;
			}
			else
			{
				finalPrice.innerHTML = formatCurrency(final_price);
			}
		}

		if (store_name != 'Digg Shop')
		{
			imageLink = document.getElementById('image_link' + n);
			if (imageLink != null)
			{
				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;
				}
			}

			imageCaption = document.getElementById('image_caption' + n);
			if (imageCaption != null)
			{
				imageCaption.innerHTML = atob(caption);
			}

			profileLinks = document.getElementById('profile_links' + n);
			if (profileLinks != null)
			{
				profileLinks.innerHTML = atob(caption);
			}
		}

		if (batch_processing == '0')
		{
			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)
				{				
					if (minimal_inventory_available !== false)
					{
						if (parseInt(inventory) >= parseInt(minimal_inventory_available))
						{
							noticeDiv.innerHTML = "<p>In Stock</p>";
						}
						else
						{
							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>";
						}
					}
					else
					{
						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>";
					}
					
					noticeDiv.style.display = "block";

					orderBlock = document.getElementById('order_block' + n);
					if (orderBlock != null)
					{
						orderBlock.style.display = "block";
					}
				}
			}
		}
		else
		{
			noticeDiv = document.getElementById('notice_div' + n);
			if (noticeDiv != null)
			{
				noticeDiv.style.display = "none";
			}
		}

		skuBlock = document.getElementById('sku' + n);
		if (skuBlock != null)
		{
			skuBlock.innerHTML = sku;
		}

		
		buyNowBlock = document.getElementById('buy_now_block' + n);
		if (buyNowBlock != null)
		{
			buyNowBlock.innerHTML = '<a href="/contact_us.html?subject=custom_order&amp;product_id='+product_id+'"><img src="/images/blackanddecker/buy_now.gif" alt="Buy Now"/></a>';
		}
	}
};