$(function() {

	$("#pdDate").datepicker({minDate: +1,numberOfMonths: 1});
	
	/* Reset Form */
	
	/* Reset Cake Information */
	$("#bfmCakeType option:eq(0)").attr("selected","selected");
	$("#bfmCakeFilling option:eq(0)").attr("selected","selected");
	$("#bfmCakeIcing option:eq(0)").attr("selected","selected");
	$("#bfmCakeDecoration option:eq(0)").attr("selected","selected");
	$("#bfmCakeMessage").attr("value","");
	$("#bfmCakeSize option:eq(0)").attr("selected","selected");
					
	/* Reset Billing Information */
	$("#billingFirstName").attr("value","");
	$("#billingLastName").attr("value","");
	$("#billingAddress1").attr("value","");
	$("#billingAddress2").attr("value","");
	$("#billingCity").attr("value","");
	$("#billingState option:eq(0)").attr("selected","selected");
	$("#billingZipCode").attr("value","");
	$("#billingPhone").attr("value","");
	$("#billingAltPhone").attr("value","");
	$("#billingEmail").attr("value","");
	
	/* Reset Payment Information */
	$("input[name='paymentType']:checked").attr('checked', false);
	
	/* Reset Pickup/Delivery Information */
	$("input[name='pdType']:checked").attr('checked', false);
	$("input[name='pickupLocation']:checked").attr('checked', false);
	$("#pdDate").attr("value","");
	$("#pdTime").attr("value","");
	
	/* Set Cake Types */
	for (var i in bfmCakeTool.items) {
		$("#bfmCakeType").append("<option value='" + i + "'>" + i + "</option>");
	}
	
	/* Set Cake Fillings */
	for (var i in bfmCakeFillings.items) {
		$("#bfmCakeFilling").append("<option value='" + i + "'>" + i + "</option>");
	}
	
	/* Set Cake Icings */
	for (var i in bfmCakeIcings.items) {
		$("#bfmCakeIcing").append("<option value='" + i + "'>" + i + "</option>");
	}
	
	/* Set Cake Decoration */
	for (var i in bfmCakeDecorations.items) {
		$("#bfmCakeDecoration").append("<option value='" + i + "'>" + i + "</option>");
	}
	
	/* Set Cake Sizes */
	//for (var i in bfmCakeSizes.items) {
	//	$("#bfmCakeSize").append("<option value='" + i + "'>" + bfmCakeSizes.getItem(i) + "</option>");
	//}
	
	/* Check URL for cakeType */
	if($.QueryStringExist({ ID:"cakeType" })) {
		var ct = $.getQueryString({ ID:"cakeType" });
		ct = $.URLDecode(ct);
		
		$("#bfmCakeType").val(ct);
		updateCakeFilling();
		updateCakeIcing();
		updateCakeSize();
		updateDisplay();
	}
	
	/* Event handler for Cake Type */
	$("#bfmCakeType").change(function () {
		updateCakeFilling();
		updateCakeIcing();
		updateCakeSize();
		updateDisplay();
	});
	
	/* Event handler for Cake Size */
	$("#bfmCakeSize").change(function () {
		updateDisplay();
	});
	
	/* Event handler for Cake Decoration */
	$("#bfmCakeDecoration").change(function () {
		if($("#bfmCakeDecoration").val() == "Special Request") {
			$("#specialRequest").show();
			$("#bfmCakeSpecialRequest").focus();
		}
		else {
			$("#specialRequest").hide();
			$("#bfmCakeSpecialRequest").attr("value","");
		}
	});
	
	function updateDisplay() {
		var cakeType = $("#bfmCakeType").val();
		var cakeSize = $("#bfmCakeSize").val();
		var cakeLayers = "";
		var cakeServings = "";
		var cakePrice = "";
		
		/* if cake type and cake size are selected, display layers, servings, and base price */
		if (cakeType && cakeSize) {
			cakeLayers = bfmCakeTool.getItem(cakeType).getLayers(cakeSize);
			cakeServings = bfmCakeTool.getItem(cakeType).getServings(cakeSize) + " people";
			cakePrice = "$" + bfmCakeTool.getItem(cakeType).getPrice(cakeSize);
			
			$("#cakeBuild").show("slow");
			$("#cakeLayers").text(cakeLayers);
			$("#cakeServings").text(cakeServings);
			$("#cakePrice").text(cakePrice);
			
			/* set hidden input fields */
			$("#bfmCakeLayers").attr("value",cakeLayers);
			$("#bfmCakeServings").attr("value",cakeServings);
			$("#bfmCakePrice").attr("value",cakePrice);
		}
		else {
			$("#cakeBuild").hide("slow");
			$("#cakeLayers").text("0");
			$("#cakeServings").text("0 people");
			$("#cakePrice").text("$0.00");
			
			/* clear hidden input fields */
			$("#bfmCakeLayers").attr("value","");
			$("#bfmCakeServings").attr("value","");
			$("#bfmCakePrice").attr("value","");

		}
	}
	
	function updateCakeFilling() {
		var cakeType = $("#bfmCakeType").val();

		if (cakeType) {
			var cakeFilling = bfmCakeTool.getItem(cakeType).getFilling();
			/* Set default cake filling for this cake type */
			if (cakeFilling)
				$("#bfmCakeFilling").val(cakeFilling);
			else
				$("#bfmCakeFilling option:eq(0)").attr("selected","selected");
		}
		else {
			$("#bfmCakeFilling option:eq(0)").attr("selected","selected");
		}
	}
	
	function updateCakeIcing() {
		var cakeType = $("#bfmCakeType").val();
		
		if (cakeType) {
			var cakeIcing = bfmCakeTool.getItem(cakeType).getIcing();
			/* Set default cake icing for this cake type */
			if (cakeIcing)
				$("#bfmCakeIcing").val(cakeIcing);
			else
				$("#bfmCakeIcing option:eq(0)").attr("selected","selected");
		}
		else {
				$("#bfmCakeIcing option:eq(0)").attr("selected","selected");
		}
	}
	
	function updateCakeSize() {
		var cakeType = $("#bfmCakeType").val();
		var bfmCake;

		$('#bfmCakeSize').children().remove().end().append('<option value="">Select a Cake Size</option><option value="">---------------</option>');
		
		if(cakeType) {
			//get the cake type object currently selected
			bfmCake = bfmCakeTool.getItem(cakeType);

			//loop through the prices and get the cake sizes
			/*
			for (var i in bfmCake.Prices.items) {

				var cakeShape = bfmCake.Shapes.getItem(i);
				var text = "";
				
				//build text string based on Round or Rectangle cake size
				cakeShape == "Round" ? text = cakeShape + ": " + i + " inch" : text = cakeShape + ": " + i + " sheet" ;
				
				$("#bfmCakeSize").append("<option value='" + i + "'>" + text + "</option>") ;
			}
			*/
			
			//loop through the prices and get the cake sizes
			for (var i in bfmCake.Prices.items) {

				var text = bfmCakeSizes.getItem(i);
				
				$("#bfmCakeSize").append("<option value='" + i + "'>" + text + "</option>") ;
			}
			
			
		}
	}
	
	/* Event handler for Payment Type */
	$("input[name='paymentType']").click(function () {
		if($("input[name='paymentType']:checked").val() == "Credit Card")
			$("#paymentTypeNote").text("We will contact you for your credit card information.  Credit cards used for purchase must be presented at pick-up.");
		else
			$("#paymentTypeNote").text("");
	});
	
	/* Event handler for Pickup or Deliver */
	$("input[name='pdType']").click(function () {
		if($("input[name='pdType']:checked").val() == "Pickup") {
		
			/* show pickup form */
			$("#required_for_delivery").hide();
			$("#required_for_pickup").show("slow");
			
			/* Clear all delivery information */
			$("#deliveryCompanyName").attr("value","");
			$("#deliveryAttention").attr("value","");
			$("#deliveryAddress1").attr("value","");
			$("#deliveryAddress2").attr("value","");
			$("#deliveryCity").attr("value","");
			$("#deliveryState").attr("value","");
			$("#deliveryZipCode").attr("value","");
			$("#deliveryInstructions").attr("value","");
		}
		
		if($("input[name='pdType']:checked").val() == "Delivery") {
		
			/* show delivery form */
			$("#required_for_pickup").hide();
			$("#required_for_delivery").show("slow");

			/* Clear all pickup location radio buttons */
			$("input[name='pickupLocation']:checked").attr('checked', false);
		}
		
	});
	
	$("#bfmCakeOrderForm").validate({
		rules: {
				bfmCakeType: "required",
				bfmCakeFilling: "required",
				bfmCakeIcing: "required",
				bfmCakeSize: "required",
				billingFirstName: "required",
				billingLastName: "required",
				billingPhone: "required",
				billingEmail: {
					required: true,
					email: true
				},
				paymentType: "required",
				pdType: "required",
				pdDate: "required",
				pdTime: "required",
				deliveryAttention: {
					required: function(element){
						return ($("input[name='pdType']:checked").val() == "Delivery");
					}
				},
				deliveryAddress1: {
					required: function(element){
						return ($("input[name='pdType']:checked").val() == "Delivery");
					}
				},
				deliveryCity: {
					required: function(element){
						return ($("input[name='pdType']:checked").val() == "Delivery");
					}
				},
				deliveryState: {
					required: function(element){
						return ($("input[name='pdType']:checked").val() == "Delivery");
					}
				},
				deliveryZipCode: {
					required: function(element){
						return ($("input[name='pdType']:checked").val() == "Delivery");
					}
				},
				pickupLocation: {
					required: function(element){
						return ($("input[name='pdType']:checked").val() == "Pickup");
					}
				}
			}
			/*,
		messages: {
				bfmCakeType: "This field is required.",
				bfmCakeFilling: "This field is required.",
				bfmCakeIcing: "This field is required.",
				bfmCakeSize: "This field is required.",
				billingFirstName: "This field is required.",
				billingLastName: "This field is required.",
				billingPhone: "This field is required.",
				billingEmail: "This field is required.",
				pdDate: "This field is required."
			}*/
	});

	$("#billingPhone").mask("(999) 999-9999");
	$("#billingAltPhone").mask("(999) 999-9999");
	$("#billingZipCode").mask("99999");
	$("#deliveryZipCode").mask("99999");
	$("#pdDate").mask("99/99/9999");

	$("#bfmCakeOrderForm").append("<input  id='BFM' type='hidden' name='BFM' value='j5Ww9HaN2ysfr828edEyrkYukYN4s1ZPw4X40wAmCM4'/>");
	
});

//URL Encode & Decode
$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
  while(x<c.length){var m=r.exec(c.substr(x));
    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});


function getParameterByName(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}