/*
FILE NAME: fieldscan.js
APPLICATION: modular

DATE CREATED: 9/11/00
LAST MODIFIED: 9/11/00
AUTHOR: Tom Besser

DESCRIPTION: See function descriptions
*/
	
	function fieldScan(fieldname, inputfield, allowedChars, req, mode)
	{/*	fieldScan function
			Author: Tom Besser
			Date: 8/10/00
	
			Description: Character-by-character validation routine.
				Weeds out bad characters from input fields.
				
			REQUIRED: trim() function (included)
	
			fieldname: Required string, the name of the field for display purposes.
			inputfield: Required object, the imput object.
			allowedChars: Required string, all valid characters.
			req: Required boolean, true = required field, false = optional field
	  
			Returns true when successful.
			Displays error prompt, hilights error field and returns false when not successful. 	
	*/

		// Utility function to trim spaces from both ends of a string
		
		function trim(inString) {
		  var retVal = "";
		  var start = 0;
		  while ((start < inString.length) && (inString.charAt(start) == ' ')) {
		    ++start;
		  }
		  var end = inString.length;
		  while ((end > 0) && (inString.charAt(end - 1) == ' ')) {
		    --end;
		  }
		  retVal = inString.substring(start, end);
		  return retVal;
		}	

		inputfield.value = trim(inputfield.value);
		tempValue = "";
		
		if (inputfield.value.length > 0)
		{
			for (var i = 0; i < inputfield.value.length; i++)
			{
				if(mode == "remove")
				{
					if (allowedChars.indexOf(inputfield.value.charAt(i)) != -1)
					{
						tempValue += inputfield.value.charAt(i);
					}
				}
				else
				{
					if (allowedChars.indexOf(inputfield.value.charAt(i)) == -1)
					{
						switch (inputfield.value.charAt(i))
						{
							case ' ':
								alert('Spaces are not allowed in ' + fieldname + '.');
								break;
							
							case '.':
								alert('Periods are not allowed in ' + fieldname + '.');
								break;
							
							case ',':
								alert('Commas are not allowed in ' + fieldname + '.');
								break;
								
							case ':':
								alert('Colons are not allowed in ' + fieldname + '.');
								break;
							
							case ('\"'):
								alert('Quotation marks are not allowed in ' + fieldname + '.');
								break;
								
							case ('\''):
								alert('Single quotation marks are not allowed in ' + fieldname + '.');
								break;
								
							default:
								alert('\"' + inputfield.value.charAt(i) +  '" is not allowed in ' + fieldname + '.');
								break;
						}
						inputfield.focus();
						inputfield.select();
						return false;
					}
				}
			}
		}
		else
		{
			if (req == true)
			{
				alert(fieldname + ' is required.');
				inputfield.focus();
				inputfield.select();
				return false;
			}
		}
		
		if(mode == "remove")
			inputfield.value = tempValue;
		
		return true;
	}

