// JavaScript Document

$(document).ready(function() {

	clearError("input, select, textarea");
	
	$("#firstname").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError($(this));
		else
			clearError($(this));
	});	
	
	$("#lastname").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError($(this));
		else
			clearError($(this));
	});
	
	$("#title").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError($(this));
		else
			clearError($(this));
	});
	
	$("#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($(this));
		else
			clearError($(this));
	});
	
	$("#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($(this));
		else
			clearError($(this));
	});
	
	$("#zip").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "" || !($(this).val().match(/^[0-9A-Za-z\ ]{2,}$/)))
			setError($(this));
		else
			clearError($(this));
	});
	
	$("#country").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError($(this));
		else
			clearError($(this));
	});
	
	$("#company").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError($(this));
		else
			clearError($(this));	
	});
	
	if ($("#numemployees").length > 0) {
		$("#numemployees").blur(function() {
			$(this).val($.trim($(this).val()));
			
			if ($(this).val() == "")
				setError($(this));
			else
				clearError($(this));
		});
	}
		
	if ($("#timeframe").length > 0) {
		$("#timeframe").blur(function() {
			$(this).val($.trim($(this).val()));
			
			if ($(this).val() == "")
				setError($(this));
			else
				clearError($(this));
		});
	}

	if ($("#contact_me").length > 0) {	
		$("#contact_me").blur(function() {
			$(this).val($.trim($(this).val()));
			
			if ($(this).val() == "")
				setError($(this));
			else
				clearError($(this));
		});
	}
	
	if ($("input[name='requestdemo']").length > 0 && $("input[name='requestdemo']").attr("type") == "radio") {
		$("input[name='requestdemo']").blur(function() {
			if ($("input[name='requestdemo']:checked").length == 0)
				setError($(this));
			else
				clearError($(this));
		});
	}

	$("#stdform").submit(function() {
		$("input, select, textarea").blur();

		if ($(".validation-error").length > 0)
			return false;
		else
			return true;
	});
});


function setError(expr, msg)
{
	if ($(expr) != undefined) {
		$(expr).parent(".field-group").addClass("validation-error");
		$("#validation-status").html("Please complete all required fields.");
	}
}

function clearError(expr)
{
	if ($(expr) != undefined) {
		$(expr).parent(".field-group").removeClass("validation-error");
		if ($(".validation-error").length == 0) $("#validation-status").html("&nbsp;");
	}
}

