// create form validation object
function formValidator()  {
	this.errorList = new Array; // array to hold error messages
	this.temp = '';
	
	// set up object methods
	this.isEmpty = isEmpty;
	this.isNumber = isNumber;
	this.isWithinRange = isWithinRange;
	this.isWithinLength = isWithinLength;
	this.isChecked = isChecked;
	this.isAlpha = isAlpha;
	this.isAlphaNumeric = isAlphaNumeric;
	this.isEmailValid = isEmailValid;
	this.doesStartWith = doesStartWith;
	this.isSelected = isSelected;
	this.isRadioSelected = isRadioSelected;
	
	this.highlightError = highlightError;
	this.raiseError = raiseError;
	this.numErrors = numErrors;
	this.displayErrors = displayErrors;
	this.flushErrors = flushErrors;
}

// check to see if input is whitespace only or empty
function isEmpty(val)  {
	if(val.match(/^\s+$/) || val == "") return true;
	else return false;
}

// check to see if input is numeric
function isNumber(val)  {
	if(isNaN(val)) return false;
	else return true;
}

// check to see if value is within a min & max range
function isWithinRange(val, vMin, vMax)  {
	if(val >= vMin && val <= vMax) return true;
	else return false;
}

function isWithinLength(val, maxLength)  {
	if(val.length <= maxLength) return true;
	else return false;
}

// check to see if checkbox has been checked
function isChecked(obj)  {
	if(obj.checked) return true;
	else return false;
}

// check to see if a radio button has been selected
function isRadioSelected(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return true;
         }
      }
   } else {
      if (buttonGroup.checked) { return true; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return false;
}

// check to see if input is alpha only
function isAlpha(val)  {
	if(val.match(/^[a-zA-Z]+$/)) return true;
	else return false;
}

// check to see if input is alpha-numeric
function isAlphaNumeric(val)  {
	if(val.match(/^[a-zA-Z0-9]+$/)) return true;
	else return false;
}

// check to see if input is a valid email address
function isEmailValid(val)  {
	if(!val.match(/^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/)) return true;
	else return false;
}

// check to see if string starts with substring
function doesStartWith(val,checkString) {
	if(val.substring(0,checkString.length).toLowerCase() == checkString.toLowerCase()) return true;
	else return false;
}

// check to see that an item in select is selected
function isSelected(el,num) {
	for(i=0;i<=num;i++) {
		if(el.options[i].selected) {
			return true;
		}
	}
	return false;
}

// add an error to error list
function raiseError(msg)  {
	this.errorList[this.errorList.length] = msg;
}

// highlight the form field with the error on 
function highlightError(el) {
	el.style.border = String ('1px solid #ff0000');
}

// return number of errors in error array
function numErrors()  {
	return this.errorList.length;
}

// display all errors -- iterate through error array and print each item
function displayErrors()  {
	var errText = "";
	for (i=0; i <= this.errorList.length -1; i++)  {
		errText += 'Error : ' + this.errorList[i] + '\n';
		//alert('Error : '+this.errorList[i]);
	}
	alert(errText);
	this.flushErrors();
}

// flushes the errors once they have been displayed
function flushErrors() {
	this.errorList = new Array;
}
