/* validateForm is the function that validates the form and either submits it, or returns a list of 
items to be fixed before re-trying the submit*/
function validateTForm()
	{
		var args = validateTForm.arguments;			//grab arguments sent into function
		if(args.length == 1)									//if there is only one argument, default submission to true, else use the parm value
			var blnSubmit = true;					
		else
			var blnSubmit = args[1];
		var form_IN = args[0];								//get the form name from the arguments
		var errMessage = "The form was not properly completed:" //error message to be built with any new errors
		var errCount = 0;										//count of errors found
		var strResult = '';										//result string from the validation check on each field
		var editKey = '';
		var strFieldName,myField;							//the field name that is composed of the record ID/field Name		
				
		// list of fields that are contained in the form 
		var strFields = form_IN.fieldList.value;
		var arrFields = strFields.split(",");

		// list of the records that are being displayed on the current form 
		// Note: if this is a single record input form, then editKeys should exist but just be blank 
		var strRecords = form_IN.editKeys.value;
		var arrRecords = strRecords.split(",");

		//cycle through all of the fields in the form and validate them 
		for (var x=0; x<arrRecords.length; x++)
		{
			editKey = arrRecords[x];
			if(editKey == '*')
				editKey = '';
			//flags are used to see if the record has been edited at all 
			//if the flag is on (1), then check its fields, otherwise it hasn't changed so there's no reason to check it
			if(eval('form_IN.Flag' + editKey+ '.value == "1"'))
			{
					for(var j=0; j<arrFields.length;j++)
					{
						for(var i=0;i<form_IN.elements.length;i++)
						{
							if(form_IN.elements[i].name == (arrFields[j] + editKey))
							{
								strResult = validateTField(form_IN.elements[i]);
								if(strResult != "")
								{
								//	swapStyles(strFieldName,true);
									errCount += 1;
									errMessage += " \n " + errCount + ". " + strResult;
								}else{
								//	swapStyles(strFieldName,false);
								}//end else
							}//end if
						}//end for	
					}//end for
			}//end if
		}
		//if there were any errors, throw the user an error messege, else, submit the form
		if(errCount > 0)
		{
			checkSwitch = '2';
			if(blnSubmit)
				alert(errMessage)
			else
				return errMessage;
		}else
			if(blnSubmit)
			{
				checkSwitch = '0';
				form_IN.submit();
			}else
				return "";
	}
	
	//checkField is a "mini-validation" that runs just after the user enters a value into the field
	function checkTField(field_IN)
	{
		//calls the validateField function on the field - this is the same function at the base of the form validation function
		var strResult = validateTField(field_IN);

		//validateField will return an error string if there was one
		//if there was an error, change the classes to highlight the proper fields, and give the user the error message 
		//else, set all the classes back to the original classes
		if(strResult != "")
		{
		//	swapStyles(fieldName_IN,true);
			alert(strResult);					
		}else{
		//	swapStyles(fieldName_IN,false);
		}
		//after the field has been changed, the flag for that record needs to be set
		eval('field_IN.form.Flag' + field_IN.getAttribute("rowid") + '.value = 1;');
//		eval('document.' + field_IN.form.name + '.Flag.value = 1');
	}
	
	//the true guts of the validation - this function verifies each field's value with the proper criteria that is found 
	//   in the field's 'validity' attribute 
	function validateTField(field_IN)
	{
		// grab the necessary information needed to validate the field - validity text, title and value 
		var arrField = field_IN.getAttribute("validity");
		var fldDesc = field_IN.title;
		var fldValue = field_IN.value;
		//verify that the field has the necessary validity text to be verified against 
		if(arrField != "")
		{
			arrField = arrField.split("|")	
			//arrField[0] is true or false for required or optional, everything after that is a validation sequence
			for(i=1;i<arrField.length;i++)
				switch(arrField[i])
				{
				//arrField[i] is going to contain the field type for the validation - numbers, date, string
				case "numbers":
					if(isNaN(fldValue))
						return fldDesc + " is not numeric.  Please supply a number value";
					else
					{
						fldValue = parseInt(fldValue);
						//arrField[i+1] is going to contain either a sub type for the validation or nothing 
						//nothing means that the field just has to be of the field type and nothing else in particular
						switch(arrField[i+1])
						{
							//number interval - has two values X|Y;  used to make sure the field value is in between
							//X and Y; if Y is 0 then X acts as a lower bound
							case "interval":
								
								var strEnd = arrField[i+3] += '';
								var strStart = arrField[i+2] += '';

								if(arrField[i+3] == 0)
								{
									if(fldValue < arrField[i+2])
										return fldDesc + " must be greater than " + arrField[i+2];
								}
								else
								{
									if(fldValue < arrField[i+2] || fldValue > arrField[i+3])
										return fldDesc + " must be between " + strStart + " and " + strEnd;
								}
								break;
							//automatically rounds a number down/up to become divisible by X
							case "divisible":
								if(fldValue % arrField[i+2] > 0)
								{
									if((fldValue % arrField[i+2]) > arrField[i+2]/2)
									{
										field_IN.value = parseInt(fldValue) + parseInt(arrField[i+2] - (fldValue % arrField[i+2]));
									}else{
										field_IN.value = parseInt(fldValue) - parseInt(fldValue % arrField[i+2]);
									}
								}
								break;
							case "length":
								if(field_IN.value.length < arrField[i+2])
									if(arrField[i+2] == field_IN.maxLength)
										return fldDesc + " must be " + arrField[i+2] + " digits long"
									else
										return fldDesc + " must be at least " + arrField[i+2] + " digits long"
								break;
							default:
								break;
								
						}
					}
					break;
				case "dates":
					var dteResult = isDate(fldValue);
					if(dteResult != "")
						return dteResult;
					else
						switch(arrField[i+1])
						{
							//same as numbers|interval, except with dates
							//if X is 0 then dates must be prior to Y, if Y is 0 then dates must be after X
							case "interval":
								var dteValue = createDate(fldValue);
								if(arrField[i+2] != "0"){
									if(arrField[i+3] != "0")
										var intervalA = createDate(arrField[i+2]);
										var intervalB = createDate(arrField[i+3]);
								
										if(dteValue < intervalA || dteValue > intervalB)
											return fldDesc + " must be between " + arrField[i+2] + " and " + arrField[i+3];
									else
										var intervalA = createDate(arrField[i+2]);
										
										if(dteValue < intervalA)
											return fldDesc + " must be on or past " + arrField[i+2]
								}else{
									var intervalB = createDate(arrField[i+3]);
										
									if(dteValue > intervalB)
										return fldDesc + " must be on or before " + arrField[i+3]
								}	
								break;
							default:
								break;
						}
					break;
				case "string":
					switch(arrField[i+1])
					{
						//maxlength attributes can control the maximum length of a string, this validation can control the minimum
						case "length":
							if(fldValue.length < parseInt(arrField[i+2]))
								return fldDesc + " must be at least " + arrField[i+2] + " characters long";
							break;
						case "limit":
							if(fldValue.length > parseInt(arrField[i+2]))
								return fldDesc + " must be at most " + arrField[i+2] + " characters long";
							else
								if(parseInt(arrField[i+3]) > 0)
									if(fldValue.length < parseInt(arrField[i+2]))
										return fldDesc + " must be at least " + arrField[i+3] + " characters long";
							break;
						//justtext requires that there be no numbers or punctuation in the value of the text box, just letters
						case "justtext":
							if(getNonText(fldValue).length > 0)
								return fldDesc + " cannot contain anything but letters";
							break;
						//justgrammar will not allow any numbers to be entered into a field, just letters and punctuation
						case "justgrammar":
							if(getNumbers(fldValue).length > 0)
								return fldDesc + " cannot contain any numbers";
							break;
						//alphanumeric will not allow any punctuation to be entered into a field, just letters and numbers
						case "alphanumeric":
							if(getPunctuation(fldValue).length >0)
								return fldDesc + " cannot contain any punctuation";
							break;
						case "zipcode":
							return formatZip(field_IN.name);
							break;
						case "phone":
							if(formatPhone(field_IN.value).length > 0)
								field_IN.value = formatPhone(fldValue);
							else
								return fldDesc + " must be a valid phone number";
							break;
						case "email":
							if(!(verifyEmail(fldValue)))
								return fldDesc + " must be a valid email address";
							break;
					}
			}
		}
		//if the validation has cleared up to here, then it needs to check if the value is required or not
		//if it is, then it will not be able to be blank
		if(arrField[0] == "true")
		{
			if(textTrim(field_IN.value) == '') 
				return "A value must be entered for " + fldDesc ;
			else
				return "";
		}else
			return "";
	}