// return 1st error so calling prog can set focus
// build up all errors into one string by returning them
// could turn this into error object which will provide the above as properties
// need clear function

function Trim(oldString) {
   // is oldString made up completely of whitespace?
   var emptyPattern = /^(\s*)$/;
   if(emptyPattern.test(oldString))
      // if so, return null string
      return '';
   else
      // else, return non-whitespace characters from middle of oldString
      return oldString.replace(/^\s*(.*\S+)\s*$/,"$1");
} 

function ValidateField(obj, strErrText, regExpObj, dType)
	{
//	if (!(obj.value==null || obj.value==""))
		{
		var blnValid = true
		if (dType!=null)
			{
			switch (dType)
				{
				case "Alpha":
					blnValid = isNaN(obj.value)
					break
				case "Num":
					blnValid = !isNaN(obj.value)
					break
				// case "Date": Date type validation
				//	goes here
				}
			}
		if (regExpObj)
			{
			var valReg = new RegExp(regExpObj, "")
				blnValid = valReg.test
	(obj.value)
			}
		if (!blnValid)	
			{
			if (this.ErrText == "")
				{
					this.ErrText = "The following error(s) have occured: \n" 
				}
			this.ErrNumber = this.ErrNumber + 1
			this.ErrText = this.ErrText + "\n" + this.ErrNumber + ".  " + strErrText
			if (this.FirstError==null)
				{
					this.FirstError = obj
				}
			//alert(strErrText)
			//this.elem.value=""
//			this.elem.focus()
			}
		}	
	}

function ShowErrors2()
	{
		if (this.ErrNumber != 0 ) // error occured
			{
				alert(this.ErrText)
				this.FirstError.focus()
				//event.returnValue = false
			}
	}

function IgnoreError()
	{
		return true
	}
	
function AddError(obj, strErrText)
	{
		if (this.ErrText == "")
			{
				this.ErrText = "The following error(s) have occured: \n" 
			}
		this.ErrNumber = this.ErrNumber + 1
		this.ErrText = this.ErrText + "\n" + this.ErrNumber + ".  " + strErrText
		if (this.FirstError==null)
			{
				this.FirstError = obj
			}
	}
	
/* ******************************************
	Primary object prototype
*********************************************/
function Validator()
	{
		this.ErrText = ""  // text to display to user with all errors in
		this.ErrNumber = 0 // number of fields with errors
		this.FirstError = null // first field with an error in

		this.Validate = ValidateField 
		this.ShowErrors = ShowErrors2
		this.AddError = AddError
	}

