// JavaScript Document

var demo_mode = false; // set to false to allow form submit

function checkFieldTask (aFieldName) {
	var bool = true;
	switch (aFieldName) {
		/*
			checkFieldTask() customization instructions:
			
			1. Make a new "case" entry for each of the fields you want to validate.
			2. Put your validation test for the field inside of the "if" statement.
			3. Call "failField" when the test fails, passing in the error message that should appear.
			4. Set "bool=false" when the test fails.
			5. Call "passField" when the test succeeds.
		*/
		case "FirstName":
			if ($("#"+aFieldName).val()=="") {
				failField(aFieldName,"Please enter your first name.");
				bool = false;
			} else {
				passField(aFieldName);
			}
			break;
			
			case "LastName":
			if ($("#"+aFieldName).val()=="") {
				failField(aFieldName,"Please enter your last name.");
				bool = false;
			} else {
				passField(aFieldName);
			}
			break;
			
			case "Company":
			if ($("#"+aFieldName).val()=="") {
				failField(aFieldName,"Please enter your company.");
				bool = false;
			} else {
				passField(aFieldName);
			}
			break;
			
			case "Email":
			

			var emailFilter=/^.+@.+\..{2,3}$/;

			
			if ($("#"+aFieldName).val()=="") {
				failField(aFieldName,"Please enter your e-mail address.");
				bool = false;
			}
			else if (!(emailFilter.test(($("#"+aFieldName).val())))) {
				failField(aFieldName,"Please enter a valid email address.");
				bool = false;
			}
			else {
				passField(aFieldName);
			}
			break;

		
	}
	return bool;
}

function checkField (aField) {
	return checkFieldTask(aField.name);
}

function checkForm (aForm) {
	var bool = true;
	for (var i=0; i < aForm.elements.length; i++) {
		if (!checkFieldTask(aForm.elements[i].name)) {
			bool = false;
		}
	}
	if (bool) {
		passField("subbtn");
		if (demo_mode) {
			$("#myform").hide(250);
			$("#instructions").html('Good job. <a href="#" onclick="demoShowForm();return false;">Show Form Again</a>');
			return false;
		}
	} else {
		failField("subbtn","Please resolve issues first.");
	}
	return bool;
}

function passField (aFieldName) {
	$("#form_alert_"+aFieldName+"_msg").remove();
}

function failField (aFieldName,msg) {
	$("#form_alert_"+aFieldName+"_msg").remove(); // in case there are any from last time
	$("#"+aFieldName).after(alertMsgHTML(aFieldName,msg));
}
function alertMsgHTML (aFieldName, msg) {
	return '<div id="form_alert_'+aFieldName+'_msg" class="form_alert_msg">'+msg+'</div>';
}
function demoShowForm () {
	$("#instructions").text("Leave fields blank, then tab or submit, for error.");
	$("#myform").show(250);
}


