$(document).ready(function(){
	
	// Add hidden field to stop bots
	$("#enquiry_form").append('<input type="hidden" name="nobots" value="niyfsdcrucnkdncihhvb" />');
						   
	// Form Submission Checking
	$("#enquiry_form input").keyup(function(){ checkForm(); });
	$("#enquiry_form select").change(function(){ checkForm(); });
	$("#reset_form").click(function(){ setInterval(checkForm, 100); });
	checkForm();
	
	// Outbound Link Tracking - Requires GA to be set up
	// Check every click on an <a> tag
	$("a").click(function(){
		// Get the <a> tag link destination
		var href = $(this).attr("href");
		// Get the current page location
		var loc = location.href;
		// Check to see if the http protocol is being used
		if (href.indexOf("http://") == 0) {
			// If so, strip out the http://www. and everything after the end of the domain name
			var str = loc.substring(11, loc.indexOf("/", 7));
			// Check to see if the link destination does not contain the current domain i.e. it's an outbound link
			if (href.indexOf(str) == -1) {
				// Call the GA pageTracker
				var pageTracker=_gat._getTracker("UA-1326139-1");
				// If it's an outbound link then grab the title attribute value
				var title = $(this).attr("title");
				// Now try calling the GA event tracking script and send the title and destination details with a type of "Outbound Links"
				try { pageTracker._trackEvent("Outbound Links", title, href); } catch(err) {}
			}
		}
	});
	
	$("#present").colorbox({iframe: true, innerWidth: 448, innerHeight: 516});
	
});

	
function checkForm() {
	var i = 0;
	if (!checkVal("name")) { highlight("name", "red"); i++; } else { highlight("name", "green"); }
	if (!checkVal("email") || !isValidEmailAddress($("#email").val())) { highlight("email", "red"); i++; } else { highlight("email", "green"); }
	if (!checkVal("item")) {
		$(".shape_size .left").css("color","#999");
		$(".shape_size .right input").each(function(){
			$(this).css("background-color","#eee");
			$(this).css("border-color","#ccc");
			$(this).attr("disabled", "disabled");
			$(this).val("");
		});
		highlight("item", "red"); 
		i++;
	} else { 
		if ($("#item").val() == "Oyster Card Holders" || $("#item").val() == "Tax Disc Holders") {
			$(".shape_size .left").css("color","#999");
			$(".shape_size .right input").each(function(){
				$(this).css("background-color","#eee");
				$(this).css("border-color","#ccc");
				$(this).attr("disabled", "disabled");
				$(this).val("");
			});
		} else {
			$(".shape_size .left").css("color","#000");
			$(".shape_size .right input").each(function(){
				$(this).css("background-color","#fff");
				$(this).css("border-color","red");
				$(this).attr("disabled", "");
			});
			if (!checkVal("width")) { highlight("width", "red"); i++; } else { highlight("width", "green"); }
			if (!checkVal("height")) { highlight("height", "red"); i++; } else { highlight("height", "green"); }
		} 
		highlight("item", "green"); 
	}
	if (!checkVal("colours")) { highlight("colours", "red"); i++; } else { highlight("colours", "green"); }
	if ($("#quantity").val() == "") { highlight("quantity", "red"); i++; } else { highlight("quantity", "green"); }
	if (i == 0) { $("#submit_form").attr("disabled", ""); } else { $("#submit_form").attr("disabled", "disabled"); }
}

function checkVal(field) {
	if ($("#"+ field).val() == "") { return false; } else { return true; }
}

function highlight(field, colour) {
	$("#"+ field).css("border","1px solid "+ colour);
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
} 

