// 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));
	});

	$("#street1").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError($(this));
		else
			clearError($(this));
	});

	$("#city").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError($(this));
		else
			clearError($(this));
	});

	$("#zip").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError($(this));
		else
			clearError($(this));
	});

	$("#dellcustomer").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError($(this));
		else
			clearError($(this));
	});
	
	$("#emailoptin").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));
	});
	
	$("#state").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError($(this));
		else
			clearError($(this));
	});

	$("#country").blur(function() {
		$(this).val($.trim($(this).val()));

		if ($(this).val() == "United States" || $(this).val() == "Canada" || $(this).val() == "") {
			$("#state").removeAttr("disabled");
		}
		else {
			$("#state").attr("disabled", "true");
			$("#state option:first").attr("selected", "true");
			clearError($("#state"));
		}
		
		if ($(this).val() == "" || $(this).val() == "--")
			setError($(this));
		else
			clearError($(this));
	});

	$("#state").blur(function() {
		if ($("#country").val() == "United States" || $("#country").val() == "Canada" || $("#country").val() == "") {
			$(this).val($.trim($(this).val()));
			
			if ($(this).val() == "" || $(this).val() == "--")
				setError($(this));
			else
				clearError($(this));	
		}
		else {
			$("#state option:first").attr("selected", "true");
			clearError($(this));
		}
	});
	
	$("#company").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError($(this));
		else
			clearError($(this));	
	});
	
	$("#numemployees").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError($(this));
		else
			clearError($(this));
	});
	
	$("#timeframe").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($(this).val() == "")
			setError($(this));
		else
			clearError($(this));
	});

	$("#productinterest").blur(function() {
		$(this).val($.trim($(this).val()));
		
		if ($("#requestdemo").attr("checked")) {
			if ($(this).val() == "")
				setError($(this));
			else
				clearError($(this));
		}
	});
	$(".phase-2").hide();
	
	$("#requestdemo").click(function() {
		if ($(this).attr("checked")) {
			$(".phase-2").fadeIn(250);
		}
		else {
			$(".phase-2").fadeOut(250);
			clearError($("#numemployees"));
			clearError($("#productinterest"));
		}			
	});
	
	$("#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).closest(".field-group").addClass("validation-error");
		$("#validation-status").html("Please complete all required fields.");
	}
}

function clearError(expr)
{
	if ($(expr) != undefined) {
		$(expr).closest(".field-group").removeClass("validation-error");
		if ($(".validation-error").length == 0) $("#validation-status").html("&nbsp;");
	}
}

