$(document).ready(function() {
	
	$("#hideCartDialog").click(function(e) {
		e.preventDefault();
		$("#cartDialog").fadeOut(200);
	});

	$(".addToCart").click(function(e) {
		e.preventDefault();

		$.get('ajax'+$(this).attr("href"),function(d) {
			if (d != "") alert(d);
			$("#cartDialog").fadeIn(200);
		});
	});



	// Enforce required fields
	$("form").submit(function(event) {
		var alertMessage = "";

		//Check the value of each input with class="required"
		$(this).find(".required").each(function()
		{
			var fieldName = $(this).attr("rel");
			if ($(this).val()=="") {
				alertMessage += fieldName+" is required\n";
			}
			if ($(this).attr("name")=="email" && !$(this).val().match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) {
				alertMessage += $(this).attr("rel")+" is not a valid email address\n";
			}
		});

		//If there is an error message, alert it and stop form submission
		if (alertMessage.length > 0) {
			alert(alertMessage);
			return false;
		}
		else return true;
	});


	$("input[type=radio]").click(function() {
		if ($(this).attr("name")=="paymethod") {
			if ($(this).val()=="Credit Card") $("#carddetails").show();
			else $("#carddetails").hide();
		}
	});

	$("table.cart").find("td").each(function(i) {
		if ($(this).text().substring(0,1)=="$" || $(this).text().substring(0,2)=="-$") 
		{
			$(this).css("text-align", "right");
			$(this).css("font-size", "14px");
		}
	});


	// Enforce only numbers in a number field
	// IMPORTANT: What about decimal places?
	$("input.number").blur(function() {
		$(this).val($(this).val().replace(/[^0-9]/gi, ""));
	});

	$("a.zoom").zoomimage({
		border: 5,
		caption: true,
		centered: true,
		hideSource: true
});

});
