$(document).ready(function() {

	clearError();
	
	$("#firstname").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError("#firstname-container");
		else
			clearError("#firstname-container");
	});	
	
	$("#lastname").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError("#lastname-container");
		else
			clearError("#lastname-container");
	});
	
	$("#title").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError("#title-container");
		else
			clearError("#title-container");
	});
	
	$("#emailaddress").blur(function() {
		$(this).val($.trim($(this).val()));
		
		var re1 = /^([a-zA-Z0-9_\.\-\+])+@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/;
		var re2 = /@[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/;
		
		var error = false;
		
		if (!error) error = !($(this).val().match(re1));
		if (!error) error = $(this).val().match(re2);		
		
		if (error)
			setError("#emailaddress-container");
		else
			clearError("#emailaddress-container");
	});
	
	$("#phone").blur(function() {
		$(this).val($.trim($(this).val()));
		
		var error = false;
		
		if (!error) error = ($(this).val() == "");
		if (!error) error = !($(this).val().match(/^[0-9+\-\(]/));
		
		if (error)
			setError("#phone-container");
		else
			clearError("#phone-container");
	});
	
	$("#postal").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "" || !($(this).val().match(/^[0-9A-Za-z\ ]{2,}$/)))
			setError("#postal-container");
		else
			clearError("#postal-container");
	});
	
	$("#country").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError("#country-container");
		else
			clearError("#country-container");
	});
	
	$("#company").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError("#company-container");
		else
			clearError("#company-container");	
	});
	
	$("#numemployees").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError("#numemployees-container");
		else
			clearError("#numemployees-container");
	});
	
	$("#server").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError("#server-container");
		else
			clearError("#server-container");
	});

	$("#timeframe").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError("#timeframe-container");
		else
			clearError("#timeframe-container");
	});
	
	$("#contact_me").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError("#contact_me-container");
		else
			clearError("#contact_me-container");
	});

	$(".submitbutton").click(function() {
		$("input, select, textarea").blur();

		if ($(".validation-error").length > 0)
			return false;
		else
			return true;
	});
});


function setError(expr, msg)
{
	$(expr).addClass("validation-error");
	$(".validation-status").html("Please complete all required fields.");
}

function clearError(expr)
{
	$(expr).removeClass("validation-error");

	if ($(".validation-error").length == 0) $(".validation-status").html("&nbsp;");
}