$(document).ready(function()
	{
		if($("form#RightSideCalculator"))
		{
			var $RightCalculator = $("form#RightSideCalculator");
			$.getJSON('/JSON/calculator.js',function(data)
				{
					if(data.length==0)
					{
						alert(data.error);
					}
					else
					{
						var $ManSelect = $("select[name='SelectManufacturer']",$RightCalculator);
						for($i=0;$i<data.length;$i++)
						{
							$("<option></option>").attr("value",data[$i].ID).html(data[$i].text).appendTo($ManSelect);
						}
					}
				}
			);
			$("select[name='SelectManufacturer']",$RightCalculator).linkedSelect('/JSON/calculator.js?action=categories',"select[name='SelectCategory']",{firstOption:'Categories'});
			$("select[name='SelectCategory']",$RightCalculator).linkedSelect('/JSON/calculator.js?action=products',"select[name='SelectProducts']",{firstOption:'Products'});
			$("input[name='CalculatePacks']",$RightCalculator).click(function ()
				{
					var $ProductSelect = $("select[name='SelectProducts']",$RightCalculator);
					var $ProductName = $("select[name='SelectProducts'] option:selected",$RightCalculator).text()
					var $width = $("input[name='RoomWidth']",$RightCalculator).val();
					var $length = $("input[name='RoomLength']",$RightCalculator).val();
					var $ProductPrice = 0.00;
					var $ProductID = 0;
					var $RoomArea = 0;
					var $PackSize = $ProductSelect.val();
					var $numPacks = 0;
					if( ($width==''||$length=='') || ($width==0||$length==0) || (isNaN($width) || isNaN($length))){alert("Please enter your room dimentions!");return false;}
					if($PackSize==0||$PackSize==''){return false;}
					$RoomArea = $length * $width;
					$numPacks = Math.ceil( ($RoomArea / $PackSize) );
					$.getJSON('/JSON/calculator.js?action=price',{ProductName:$ProductName,CatID:$("select[name='SelectCategory'] option:selected",$RightCalculator).val()}, function(data)
						{
							$ProductPrice = data[0].Price * $numPacks;
							$ProductID = data[0].productID;
							$ProductThumb = (data[0].Thumb == '')? false : data[0].Thumb.substr(1);
							
							$("input[name='amount']",$RightCalculator).val($numPacks);
							$("input[name='productid']",$RightCalculator).val($ProductID);
							$("input[name='TotalPrice']",$RightCalculator).val($ProductPrice);
							if($ProductThumb!=false)
							{
								if($("a[href^='/product/']",$RightCalculator).length>0)
								{
									$("a[href^='/product/']",$RightCalculator).remove()
								}
								var $ThumbDetails = $("<a></a>")
															.attr("href","/"+data[0].URL+"?RoomWidth="+$width+"&RoomLength="+$length+"&CalcPrice="+$ProductPrice+"&Quant="+$numPacks)
															.text("Click buy to add to your cart now! or click the product image bellow to view more details");
													$("<img/>")
															.attr("src",$ProductThumb)
															.appendTo($ThumbDetails);
								$ThumbDetails.appendTo($RightCalculator);
							}
							return false;
						}
					);
				}
			);
		}
	}
);