var numErrors = 0;

function checkform(formObj) {	
	
	numErrors = 0;
	
	//Remove the error message if it exists.
	if($("#alertMessage").length > 0) {
		$("#alertMessage").remove();
	}
	
	//Loop over each required field.
	if ($("#required").length) {
		var reqFields = $("#required").attr("value").split(",");
		for(var i=0;i<reqFields.length;i++) {
			var fieldID = jQuery.trim(reqFields[i]);
			var obj = $("#"+(fieldID));	
			if (obj.length) {
				var objID = obj.attr("id");
				if (document.getElementById(objID)) {	
					var objType = document.getElementById(objID).type;
				}	
				if (!objType) {
					var objType = "select-group";				
				}
		
				switch(objType) {
					
					//The user has no control over hidden inputs, so there is no use in validating them.
					case "hidden":
						break;
					default:					
						var objValue = obj.attr("value");
						if(!objValue){ 
							addError(obj,objType,formObj,'We were unable to process your form.','Please review the highlighted fields and resubmit.');
						} else if (objID=='email' && !isEmailAddr(objValue)) {
							addError(obj,objType,formObj,'We were unable to process your form.','Please review the highlighted fields and resubmit.');
						} else if(objID=='phone'){
							if(!isPhone(objValue)){
								addError(obj,objType,formObj,'We were unable to process your form.','Please review the highlighted fields and resubmit.');
							} else { 
								var objValue = isPhone(objValue);
							}
						} else {
							removeError(obj,objType,formObj);
						}
						break;
					case 'password':
						var objValue = obj.attr("value");
						if(!objValue){
							addError(obj,objType,formObj,'We were unable to process your form.','Please review the highlighted fields and resubmit.');
						} else if(objValue.length < 6){
							addError(obj,objType,formObj,'We were unable to process your form.','Your password must be at least 6 characters in length.');
						} else {
							removeError(obj,objType,formObj);
						}	
						break;
					case 'select-group':
						var inputName = $("input:first", obj).attr("name");				
						var objName = obj.attr("name");	
						var objValue = $('input[name='+inputName+']:checked').val();
						if(!objValue){
							addError(obj,objType,formObj,'We were unable to process your form.','Please review the highlighted fields and resubmit.');
						} else {
							removeError(obj,objType,formObj);
						}		
						break;
				}
			}
		}
	}
	
	//Were there any errors?
	if(numErrors > 0) {
		return false;
	} else {
		return true;
	}	
}

//Validate a single form field.
function validate(formField, method) {
	
	//Remove the error message if it exists.
	if($("#alertMessage").length) {
		$("#alertMessage").remove();
	}	
		
	formField = $("#"+formField.id);
	value = formField.attr("value");
	formObj = $(formField.parents("form"));
		
	switch(method) {
		case "numeric":
			if (isNumeric(value) == false) {
				addError(formField,true,getFormFieldType(formField),formObj,'We were unable to process your form.','Please enter only numeric values.');
			} else {
				removeError(formField,getFormFieldType(formField),formObj);
			}	
			break;
		case "email":
			if (isEmailAddr(value) == false) {
				addError(formField,true,getFormFieldType(formField),formObj,'We were unable to process your form.','Please enter a valid email address.');
			} else {
				removeError(formField,getFormFieldType(formField),formObj);
			}
			break;
	}	
	
}

function removeError(obj,objType,formObj) {		
	var containerDiv = getContainerDiv(obj, objType);
	if (containerDiv.hasClass("alerted") == true) {
		containerDiv.removeClass("alerted")
	}		
}	

function addError(obj,objType,formObj,msg,notes) {
	
	obj.attr("id");
	
	var containerDiv = getContainerDiv(obj, objType);
	//Add some sort of display to let the user know there is something wrong with this field.
	containerDiv.addClass("alerted");	
	
	//Add an error message.
	if ($("#alertMessage").length == 0) {
		var alertDiv = $(
		"<div id='alertMessage' class='container'>" +
			"<div id='alertIcon'>" +
				"<img alt='We were unable to process your form. Please review the highlighted fields and resubmit.' src='"+SETTINGS.webRoot+"img/alertIcon.gif' />" +
			"</div>" +
			"<p>"+msg+"<span class='notes'>"+notes+"</span></p>" +									
		"</div>");
		formObj.parent().parent().prepend(alertDiv);
	}
	numErrors += 1;
}

function getContainerDiv(obj, objType) {
	switch(objType) {
		default:
			containerDiv = $(obj.parents(".field,.container"));
			break;
			
		case "select-group":
			containerDiv = obj;
			break;
	}
	
	return containerDiv;
}


function isNumeric(strString) {
	var strValidChars = "0123456789.";
	var strChar;
	var blnResult = true;
	if (strString.length == 0) {
		return false;
	}
	for (i = 0; i < strString.length && blnResult == true; i++)	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1){
			blnResult = false;
		}
	}
	return blnResult;
}

function isEmailAddr(str) {
    return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
}

function isPhone(str) {
	str = str.replace( /[^0-9]/g, "" ); // Strip out non-numerics 
	var reg = /^([0-9]{3})([0-9]{3})([0-9]{4})$/;
	if (vals = reg.exec(str)) {
		return "(" + vals[1] + ") " + vals[2] + "-" + vals[3];
	} else {
		return false;
	}
}

function leaveDigits(master) {
	master.value=strip(master.value);
}

function strip(_str) {
	var STR="";
	var _exp =/\d+|\./g;
	var _array;
	while(_array=_exp.exec(_str)) {
		STR+=_array.join();
	}
	return(STR);
}