$(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 = "$"+calculateShipping(entered_amount).toFixed(2);
						$("#shipping_cost").html(shipping_cost);
 				    }, 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 (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 home_delivery_cost = CalculateDirectDelivery(entered_zip).toFixed(2);
								$("#home_delivery_cost").html("$"+home_delivery_cost);
								
							}
							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");
		}
	});
});

function CalculateDirectDelivery(zipcode) {
	var distance = "0";
	var price = -1;
	var subT = $("#pricing_amount").val().replace("$","")-0;//This does not include taxes or shipping
	var shipping = $("#shipping_cost").html().replace("$","")-0;
	var amount = subT+shipping-0;//DD cost is calculated based on subtotal+shipping cost
	if (zipcodeToDistance[zipcode]) {
		distance = zipcodeToDistance[zipcode];
		if (distanceToPrice[distance]) {
			priceArray = distanceToPrice[distance];
			if (amount < 500) {
				price = priceArray[0];
			} else if(amount < 750) {
				price = priceArray[1];
			} else if(amount < 1000) {
				price = priceArray[2];
			} else if(amount < 1500) {
				price = priceArray[3];
			} else if(amount < 2000) {
				price = priceArray[4];
			} else {
				price = priceArray[5];
			}
		}
	}
	set_price = price;
	if (price == -1)
	{
		set_price = 0;
	}
	set_direct_delivery(set_price);
	return price;
}