/**
* validate: validates current form before submission and returns true/false
*/
var validate = function(formName, combinations) {
	/**
	* isNumeric: check for numeric input
	*/		
	var isNumeric = function(value) {
		if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
		return true;
	}
	
	/**
	* isAlpha: check for alpha input
	*/	
	var isAlpha = function(value) {
		if (value == null || !value.toString().match(/[A-Za-z]/)) return false;
		return true;
	}
	
	/**
	* isEmail: check for valid email
	*/
	var isEmail = function(email) {
		filter = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
		return filter.test(email);
	}
	
	var isPhone = function(phone) {
		filter = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/;
		return filter.test(phone);
	}	
	
	var isEmpty = function(field) {
		emptyField = true;
			
		if (field.type == "checkbox") {
			if (field.checked == true) emptyField = false;
		} else {
			if (field.value != '') emptyField = false;
		}
		return emptyField;
	}	
	
	/**
	* getLabel: returns corresponding label for validation
	*/		
	var getLabel = function(labelName) {
		labelTxt = "";
		formLabels = document.getElementsByTagName("label");
		
		for (l = 0; l < formLabels.length; l++)
			if (formLabels[l].htmlFor == labelName) labelTxt = formLabels[l].childNodes[0].nodeValue;
		
		return labelTxt;
	}
	
	/**
	* errorMsg: returns error message array
	*/		
	var errorMsg = function() {
		msg = "Your form could not be submitted:\n";
		msg += "-------------------------------\n";
		for (err = 0; err < formErrors.length; err++) {
			msg += formErrors[err] + "\n";
		}
		
		return msg;
	}

	var combineValues = function() {
		for (combo = 0; combo < combinations.length; combo++) {
			fieldName = combinations[combo];
			combined = "";
			
			if (/phone/i.test(fieldName.toString())) {
				combined = formElements[fieldName + "AreaCode"].value + formElements[fieldName + "Prefix"].value + formElements[fieldName + "Suffix"].value;
			}
			formElements[fieldName].value = combined;
		}
	}
	
	valid = false;
	formErrors = new Array();
	formElements = document.getElementById(formName).elements;
	combineValues();
	
	for (el = 0; el < formElements.length; el++) {
		// check for required 
		if (formElements[el].className.indexOf("required") != -1 && isEmpty(formElements[el])) {
			if (getLabel(formElements[el].name) != '') formErrors.push(getLabel(formElements[el].name) + " is required.");
		} else {
			// check for numeric
			if (formElements[el].className.indexOf("numeric") != -1 && !isEmpty(formElements[el]) && !isNumeric(formElements[el].value))
				if (getLabel(formElements[el].name) != '') formErrors.push(getLabel(formElements[el].name) + " allows only numeric characters.");
			// check for alpha
			if (formElements[el].className.indexOf("alpha") != -1 && !isEmpty(formElements[el]) && !isAlpha(formElements[el].value))
				if (getLabel(formElements[el].name) != '') formErrors.push(getLabel(formElements[el].name) + " allows only characters A-Z.");
			// check for valid email
			if (formElements[el].className.indexOf("email") != -1 && !isEmpty(formElements[el]) && !isEmail(formElements[el].value))
				if (getLabel(formElements[el].name) != '') formErrors.push(getLabel(formElements[el].name) + " is not a valid email.");
			// check for phone number
			if (formElements[el].className.indexOf("phone") != -1 && !isEmpty(formElements[el]) && !isPhone(formElements[el].value))
				if (getLabel(formElements[el].name) != '') formErrors.push(getLabel(formElements[el].name) + " is not a valid phone number.");
		}
	}
	
	if (formErrors.length > 0) alert(errorMsg());
	else valid = true;
	
	return valid;
}