$(document).ready(function(){
	//monitor keyup on amount
	var pricing_amount_delayed;
 	$('#pricing_amount').live("keyup", function(){
 		clearTimeout(pricing_amount_delayed); 
 		var entered_amount = $(this).val(); 
 		//make sure quantity is numeric
 		if (entered_amount != "")
 		{
			entered_amount = entered_amount.replace("$","");
 			if (parseFloat(entered_amount) == entered_amount)
 			{
 					pricing_amount_delayed = setTimeout(function() {
						//calculate shipping
						var shipping_cost = mnbusinesscalc.get_shipping_cost(entered_amount);
						$("#shipping_cost").html("$"+shipping_cost.price.toFixed(2));
						$("#pricing_zipcode").trigger("keyup");
 				    }, 500);	
 			}
 			else
 			{
 				$("#shipping_cost").html("Please enter a valid amount");
 			}
 		}
		else
		{
			$("#shipping_cost").html("$0.00");	
		}
 	});
	
	//monitor keyup on zipcode
	var pricing_zipcode_delayed;
 	$('#pricing_zipcode').live("keyup", function(){
 		clearTimeout(pricing_zipcode_delayed); 
 		var entered_zip = $(this).val(); 
 		//make sure quantity is numeric
 		if (entered_zip != "")
 		{
 			if (parseInt(entered_zip) == entered_zip)
 			{
 				if (entered_zip.length == 5)
 				{
 					pricing_zipcode_delayed = setTimeout(function() {
						if (mnbusinesscalc.is_valid_zipcode(entered_zip) == true)
						{	
							//check to see if an amount has been entered
							if ($("#shipping_cost").html() != "$0.00" && $("#shipping_cost").html() != "Please enter a valid amount")
							{
								var subtotal = $("#pricing_amount").val().replace("$","")-0;
								var shipping_cost = $("#shipping_cost").html().replace("$","")-0;
								var amount = subtotal+shipping_cost
								var home_delivery_cost = mnbusinesscalc.get_delivery_cost(amount, entered_zip);
								$("#home_delivery_cost").html("$"+home_delivery_cost.price.toFixed(2));
								
							}
							else
							{
								$("#home_delivery_cost").html("Please enter a valid Amount of Purchase first");
								//highlight amount of purchase field
							}
						}
						else
						{
							$("#home_delivery_cost").html("ModerNash does not currently ship to that zipcode");
						}
 				    }, 500);	
 				}
				else //less than 5 digits
				{
					$("#home_delivery_cost").html("");
				}
 			}
 			else
 			{
 				$("#home_delivery_cost").html("Please enter a valid zipcode");
 			}
 		}
		else
		{
			$("#home_delivery_cost");
		}
	});
});
