$(document).ready(function() {
	$('#contact-form').ajaxForm({
		beforeSubmit: validateForm,
		success: formSuccess
	});
});
function validateForm() {
	var errorCount = 0;
	var errorFieldsFriendly = Array();
	var errorFields = Array();
	//validate first name
	if (jQuery.trim($("input[name='firstName']").val())=="") {
		errorFieldsFriendly[errorCount]="First Name";
		errorFields[errorCount]="firstName";
		errorCount += 1;
	}
	//validate last name
	if (jQuery.trim($("input[name='lastName']").val())=="") {
		errorFieldsFriendly[errorCount]="Last Name";
		errorFields[errorCount]="lastName";
		errorCount += 1;
	}
	//validate email
	var filter=/^[a-z0-9_\-\.\+]+@[a-z0-9_\-\.]+\.[a-z]{2,4}$/i;
	if (! filter.test(jQuery.trim($("input[name='email']").val()))) {
		errorFieldsFriendly[errorCount]="Email";
		errorFields[errorCount]="email";
		errorCount += 1;
	}
	//validate company
	if (jQuery.trim($("input[name='company']").val())=="") {
		errorFieldsFriendly[errorCount]="Company";
		errorFields[errorCount]="company";
		errorCount += 1;
	}
	//handle validation errors and/or submit form
	if (errorCount > 0) {
		if (errorCount == 1) errMsg = "<strong>The following required field is missing or invalid:</strong><br/> ";
		else errMsg = "<strong>The following required fields are missing or invalid:</strong><br/> ";
		for (n=0; n < errorFields.length; n++) {
			if (n > 0) {
				errMsg += ", ";
			}
			errMsg += errorFieldsFriendly[n];
		}
		$("#form-errors").html("<p>" + errMsg + "</p>").show();
		window.scroll(0,0);
		$("input[name='" + errorFields[0] + "']").focus();
		return false;
	} else {
		$("#form-errors").html("").hide();
		$("#form-shell").css("visibility", "hidden");
		$("#sending-progress").show();
		return true;
	}
}
function formSuccess(responseText) {
	if (jQuery.trim(responseText)=="OK") {
		$("#form-shell").html("<p><strong>Thank You.</strong><br/><br/>Your message has been sent to Decision Counsel.</p>").css("visibility", "visible");
		$("#sending-progress").hide();
	} else {
		$("#form-errors").html("<p>" + responseText + "</p>").show();
		$("#form-shell").css("visibility", "visible");
		$("#sending-progress").hide();
	}
}