// Universal form validation

function uslValidateForm(theForm) {
	
	var reason = "";
	
	var validateString = "";
	var validateInput = "";
	var validateField = "";
	var validateFieldName = "";
	var validateFieldDescription = "";
	var validateFieldValue = "";
	
	for(i=0; i<theForm.length; i++) {
		//find the hidden fields : these contain the validation strings
		if (theForm.elements[i].type == "hidden" && theForm.elements[i].name.search("validate_") > -1) {				
			validateInput = theForm.elements[i].name.replace("validate_", "");
			validateString = theForm.elements[i].value;
			//get the individual fields from the validate string
			validateArray = validateString.split(";");
			
			//loop through the validateArray
			for(iV=0; iV<validateArray.length; iV++) {
				validateField = validateArray[iV];
				if(validateField) {
					//lose the spaces
					validateField = validateField.replace(/\s[]* = []*\s/, "=");
					validateField = uslTrimString(validateField);
					//get the field name and the value;
					validateFieldName = validateField.split("=")[0];
					validateFieldValue = validateField.split("=")[1];
					
					if(validateFieldName == "fieldname") {
						validateFieldDescription = validateFieldValue;
					}
					else {
						//if the field name and value exist - parse the validation for that field
						reason = reason + uslValidateField(theForm, validateInput, validateFieldDescription, validateFieldName, validateFieldValue);
					}
				}
			}
		}
	}
	
	if(reason) {
		alert("The following errors occured: \n" + reason);
		return false;
	}
	else {
		return true;
	}
}

function uslValidateField(theForm, theInput, theName, validateName, validateValue) {
	var reason = "";
	
	//get the input type
	var theType = theForm[theInput].type;
	if(theForm[theInput][0]) {
		if(theForm[theInput][0].type) theType = theForm[theInput][0].type;
	}
	
	switch(theType) {
		case "checkbox" :
			if(!theForm[theInput].checked) reason = theName + " must be checked";
			break;
		case "select-one" :
			if(!theForm[theInput].options[theForm[theInput].selectedIndex].value) reason = theName + " must be selected from the drop down list";
			break;
		case "radio" :
			radioChecked = false;
			for(iR=0; iR<theForm[theInput].length; iR++) {
				if(theForm[theInput][iR].checked) radioChecked = true;
			}
			if(!radioChecked) reason = theName + " must be selected from the radio buttons list";
			break;
		default :
			reason = "";
			break;
	}
	
	if(theType == "text" || theType == "textarea" || theType == "file") {
		
		//get the value of the field in question
		var theField = theForm[theInput];
		var theValue = theField.value;
		
		//validate the value with the parameters parsed
		switch(validateName) {
			case "required" :
				switch(validateValue) {
					case "yes" :
						if(!theValue) reason = theName + " cannot be blank";
						break;
					case "no" :
						//do nothing - not required
						break;
				}
				break;
			case "datatype" :
				switch(validateValue) {
					case "int" :
						if(!uslIsInt(theValue)) reason = theName + " must be an integer value";
						break;
					case "float" :
						if(!uslIsFloat(theValue)) reason = theName + " must be an floating point (or decimal) value";
						break;
					case "email" :
						if(!uslIsEmail(theValue)) reason = theName + " must be a valid email address";
						break;
					case "telephone" :
						if(!uslIsTel(theValue)) reason = theName + " must be a valid telephone number";
						break;
					case "date" :
						if(!uslIsDate(theValue)) reason = theName + " must be a valid date of the format dd/mm/yy";
						break;
				}
				break;
		}
	}
		
	if(reason) reason = "\n" + reason + "   ";
	
	return reason;
}

function uslIsInt(value) {
	var pattern = /\d|[-]\d/;
	if(value) {
		return(pattern.test(value));
	}
	else return true;
}

function uslIsTel(value) {
	var pattern = /\d/;
	if(value) {
		return(pattern.test(value));
	}
	else return true;
}

function uslIsEmail(str) {
  if(str) {
	  // are regular expressions supported?
	  var supported = 0;
	  if (window.RegExp) {
	    var tempStr = "a";
	    var tempReg = new RegExp(tempStr);
	    if (tempReg.test(tempStr)) supported = 1;
	  }
	  if (!supported) 
	    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	  return (!r1.test(str) && r2.test(str));
  }
  else return true;
}

function uslIsDate(str) {
	if(str) {
		dateArray = str.split("/");
		if(!(dateArray[0] <= 31 && dateArray[0] >= 1)) return false;
		else if(!(dateArray[1] <= 12 && dateArray[1] >= 1)) return false;
		else if((!(dateArray[2] <= 40000 && dateArray[2] >= 1))) return false
		else return true;
	}
	else return true;
}

function uslTrimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}