$(document).ready(function() {
	
	// Stop content being copied via right mouse click
	document.oncontextmenu = function () {
		return false;
	}

	var omitformtags=["input", "textarea", "select"];

	omitformtags=omitformtags.join("|");

	function disableselect(e){
	if (omitformtags.indexOf(e.target.tagName.toLowerCase())==-1)
		return false;
	}

	function reEnable(){
		return true;
	}

	if (typeof document.onselectstart!="undefined")
		document.onselectstart=new Function ("return false");
	else{
		document.onmousedown=disableselect;
		document.onmouseup=reEnable;
	}

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

		//Check the value of each input with class="required"
		$("input.required").each(function()
		{
			var fieldName = ucfirst($(this).attr("name"));
			if ($(this).val()=="") {
				alertMessage += $(this).attr("rel")+" 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, ""));
	});

});
