$(document).ready(function()
{
	$("#nav a").click(function(event)
	{
		$(this).blur();
	});
	
	$("#preferred").click(function(event)
	{
		if($(this).attr("checked"))
		{
			$("#supplier-name").show("normal");
			$("#preferred_supplier").show("normal");
		}
		else
		{
			$("#supplier-name").hide("normal");
			$("#preferred_supplier").hide("normal");
		}
	});
	
	$("input.print").click(function(event)
	{
		event.preventDefault();
		print();
	});
	
	defaultCustomerDetails = Array();
	customerInputs = $("#customer-details input");		
	for(var x = 0; x < customerInputs.length; x++)
	{
		defaultCustomerDetails[customerInputs[x].name] = customerInputs[x].value;
	}
	customerTextareas = $("#customer-details textarea");		
	for(var x = 0; x < customerTextareas.length; x++)
	{
		defaultCustomerDetails[customerTextareas[x].name] = customerTextareas[x].value;
	}	
	
	
	$("#customer-details input").focus(function(event)
	{
		if(defaultCustomerDetails[$(this).attr("name")] == $(this).attr("value"))
		{
			$(this).attr("value","");
		}
	});
	$("#customer-details input").blur(function(event)
	{
		if($(this).attr("value") == "")
		{
			$(this).attr("value",defaultCustomerDetails[$(this).attr("name")]);
		}
	});	
	
	$("#customer-details textarea").focus(function(event)
	{
		if(defaultCustomerDetails[$(this).attr("name")] == $(this).attr("value"))
		{
			$(this).attr("value","");
		}
	});	
	$("#customer-details textarea").blur(function(event)
	{
		if($(this).attr("value") == "")
		{
			$(this).attr("value",defaultCustomerDetails[$(this).attr("name")]);
		}
	});	
	
	/* FREIGHT CALC */
	
	$("#freight-multi .add").livequery(function()
	{
		$(this).click(function(event)
		{
			event.preventDefault();
			row = $(this).parent().parent();
			newRow = row.clone();
			newRow.insertAfter(row);

			rows = $("#freight-multi tr").length;

			newRow.attr("id","calc-"+(rows+1));

		});	

	});	
	
	$("#freight-multi input").livequery(function()
	{
		$(this).change(function(event)
		{			
			row = $(this).parent().parent();		
			rowid = row.attr("id");

			if($("#"+rowid+" .pcode").attr("value") != '' && $("#"+rowid+" .qty").attr("value") != '')
			{
				freightdata = {
					'ProductCode':$("#"+rowid+" .productcode").text(),
					'Quantity':$("#"+rowid+" .qty").attr("value"),
					'PostCode':$("#"+rowid+" .pcode").attr("value")
				};

				$.ajax({
					type: "POST",
					url: "/freight/multi/",
					dataType: "json",
					data: freightdata,
					success: function(d)
					{
						if(d.success)
						{
							$("#"+rowid+" .delivery").html(d.delivery);								
							$("#"+rowid+" .weight").html(d.weight);
							$("#"+rowid+" .cartons").html(d.cartons);
							$("#"+rowid+" .cost").html("$"+d.cost);										

							$("#"+rowid+" .error").html("");										

						}
						else
						{
							$("#"+rowid+" .delivery").html('');								
							$("#"+rowid+" .weight").html('');
							$("#"+rowid+" .cartons").html('');
							$("#"+rowid+" .cost").html('');

							$("#"+rowid+" .error").html(d.error);


						}
					}
				});				
			}

		});		
	});

});
