function verify(f)
{
  var empty_fields = "";
  var fCount = 0;
  var empty_options = "";
  var oCount = 0;
	var empty_checks = '';
	var cCount = 0;
  var errors = "";
  var eCount = 0;
  var msg = "";
	var checkedSets = new Array();

  for(var i = 0; i < f.length; i++)
  {
    var e = f.elements[i];
    if(((e.type == "text") || (e.type == "textarea") || (e.type == "password")) && !e.optional)
    {
      if (((e.value == null) || (e.value == "") || isblank(e.value)) && !e.conditional)
      {
        empty_fields += "\n" + replace(e.name,"_"," ") + " is required";
        fCount += 1
        continue;
      }
		}

		//CHECKBOXES
		if(e.type == 'checkbox' && !e.optional && !e.conditional)
		{
			var setName = e.name;
			var setChecked = false;
			if(!Array.indexOf)
			{
					for(var cs=0; cs<checkedSets.length; cs++)
					{
						if(checkedSets[cs]==setName)
						{
							setChecked = true;
							break;
						}
					}
			}
			if(Array.indexOf)
			{
				if(checkedSets.indexOf(setName) > -1)
				{
					setChecked = true;
				}
			}
			if(!setChecked)
			{
				var checkVal = false;
				var currentSet = f[setName];
				for(zz=0;zz<currentSet.length;zz++)
				{
					if(currentSet[zz].checked)
					{
						checkVal = true;
						break;
					}
				}
				if(!checkVal)
				{
	        empty_checks += "\n" + replace(e.id,"_"," ") + " selection is required";
	        cCount = cCount + 1;
				}
				checkedSets.push(setName);
			}
		}	//END OF CHECKBOXES

		//RADIOS
		if(e.type == 'radio' && (!e.optional || !e.conditional))
		{
			var setName = e.name;
			var currentSet = f[setName];
			var setChecked = false;
			if(!Array.indexOf)
			{
					for(var cs=0; cs<checkedSets.length; cs++)
					{
						if(checkedSets[cs]==setName)
						{
							setChecked = true;
							break;
						}
					}
			}
			if(Array.indexOf)
			{
				if(checkedSets.indexOf(setName) > -1)
				{
					setChecked = true;
				}
			}
			if(!setChecked)
			{
				var checkVal = false;
				var currentSet = f[setName];
				for(zz=0;zz<currentSet.length;zz++)
				{
					if(currentSet[zz].checked)
					{
						checkVal = true;
						break;
					}
				}
				if(!checkVal)
				{
		       empty_checks += "\n" + replace(e.id,"_"," ") + " selection is required";
		       cCount = cCount + 1;
				}
				checkedSets.push(setName);
			}
		}	//END OF RADIOS
		
		//CONDITONAL FIELDS
    if(e.conditional)
    {
			var triggered = false;
			var failed = false;
			//FIRST CHECK IF THE CONDITONAL FIELD IS REQUIRING THIS TO HAVE A VALUE
			var conditionField = e.condition;
			if(regExTest(conditionField,/[\d]/))
			{
				var whatField = f.elements[conditionField];
			}
			else
			{
				var whatField = f[conditionField];
			}
			var fieldType = whatField.type;
			var fieldName = whatField.id;
			//WHAT TYPE OF FIELD WAS SELECTED TO TRIGGER THIS
			if(fieldType == 'checkbox' && whatField.checked)
			{
				triggered = true;
			}
			if(fieldType == 'radio' && whatField.checked)
			{
				triggered = true;
			}
			if(fieldType == 'text' && ((whatField.value != null) || (whatField.value != "") || !isblank(whatField.value)))
			{
				triggered = true;
			}
			if(whatField.options)
	    {
				//LAST OPTION FOR "OTHER" FIELD
      	if (whatField.selectedIndex == (whatField.length-1))
      	{
					triggered = true;
				}
			}
			if(triggered == true)
			{
				//CHECK THAT THE CONDITION IS MET
				if ((e.options) && !e.optional)
    		{
      		var which = e.options.selectedIndex;
      		var choice = e.options[which].value;
      		if ((choice == null) || (choice == ""))
					{
						failed = true;
					}
				}
				if((e.type == "text") || (e.type == "textarea"))
				{
					if((e.value == null) || (e.value == "") || isblank(e.value))
					{
						failed = true;
					}
				}
				if(failed == true)
				{
        	empty_fields += "\n" + replace(e.id,"_"," ") + " is required: " + replace(fieldName,"_"," ") + " = " + whatField.value + " selected";
          fCount += 1
				}
			}
    }
		
		if(e.time)
		{
			if(regExTest(e.value,"[^0-9:]") == true)
			{
			  errors += "\nA valid " + replace(e.name,"_"," ") + " is required. Format as: xx:xx";
			  eCount += 1;
			}
    }
		
    if(e.phone)
    {
			var rangePattern = /[^0-9\s\(\)\.-]/;
			if(regExTest(e.value,rangePattern) == true)
			{
         errors += "\n" + replace(e.name,"_"," ") + " must be numeric. Format as: (xxx)xxx-xxxx";
         eCount = eCount + 1;
			}
    }
		
    if(e.money)
    {
			var moneyPattern = /[^0-9\$\.\,]/;
			if(regExTest(e.value,"[^0-9\.\$,]") == true)
			{
         errors += "\n" + replace(e.name,"_"," ") + " must be dollars and cents";
         eCount = eCount + 1;
			}
    }
		
    if(e.numeric)
    {
			var numPattern = /[^0-9\.\,]/;
			if(regExTest(e.value,numPattern) == true)
			{
         errors += "\n" + replace(e.name,"_"," ") + " must be numeric";
         eCount = eCount + 1;
			}
    }
		
    if(e.numericRange)
    {
			var rangePattern = /[^0-9\s\$\.\,-]/;
			if(regExTest(e.value,rangePattern) == true)
			{
         errors += "\n" + replace(e.name,"_"," ") + " must be numeric";
         eCount = eCount + 1;
			}
    }
		
		if(e.date)
		{
			var datePattern = /\d{1,2}\/\d{1,2}\/\d{4}/;
			if(regExTest(e.value,datePattern) == false)
			{
			  errors += "\nA valid " + replace(e.name,"_"," ") + " is required. Format as: xx/xx/xxxx";
			  eCount += 1;
			}
    }
			
    if(e.count != null)
    {
	    if (e.value.length < e.count)
      {	  
  	    errors += "\n" + replace(e.name,"_"," ") + " must be at least " + e.count + " characters in length";
        eCount = eCount + 1;
      }
    }
    if(e.minValue != null)
    {
		  var v = parseFloat(e.value);
      if (isNaN(v) || ((e.minValue != null) && (v < e.minValue)))
      {	  
        errors += "\n" + replace(e.name,"_"," ") + " must be greater than " + e.minValue;
        eCount = eCount + 1;
      }
    }
    if(e.maxValue != null)
    {
		  var v = parseFloat(e.value);
      if (isNaN(v) || ((e.maxValue != null) && ( v > e.maxValue)))
      {	  
        errors += "\n" + replace(e.name,"_"," ") + " must be less than " + e.maxValue;
        eCount = eCount + 1;
      }
    }
    if(e.email)
    {
			if(regExTest(e.value,"^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$") == false)
			{
			  errors += "\nA valid email address is required";
			  eCount += 1;
			}
    }

    if (e.options && !e.optional)
    {
      var which = e.options.selectedIndex;
      var choice = e.options[which].value;
      if ((choice == null) || (choice == "") && !e.conditional)
      {
        empty_options += "\n" + replace(e.name,"_"," ") + " selection is required";
        oCount = oCount + 1;
        //continue;
      }
    }
  }

  if ((!empty_fields) && (!empty_options) && (!empty_checks) && !errors)
  {
    return true;
  }
  else
  {
    msg = "Please be sure to complete all selections or fill in all required fields.\n";
    if (empty_fields)
    {
      fieldsVar = "field"
      if(fCount > 1){fieldsVar += "s"}
      msg += "\n" + fCount + " required " + fieldsVar + " left blank.";
      msg += empty_fields += "\n";
    }
    if (errors)
    {
      fieldsVar = "field"
      if(eCount > 1){fieldsVar += "s"}
      msg += "\n" + eCount + " " + fieldsVar + " with invalid entries.";
      msg += errors += "\n";
    }
    if (empty_options)
    {
      choicesVar = "drop-down selection"
      if (oCount > 1)
        {choicesVar += "s"}
      msg += "\n" + oCount + " required " + choicesVar + " not completed.";
      msg += empty_options += "\n";
    }
    if (empty_checks)
    {
      choicesVar = "checkbox\/radio option"
      if (cCount > 1)
        {choicesVar += "s"}
      msg += "\n" + cCount + " required " + choicesVar + " not chosen.";
      msg += empty_checks += "\n";
    }
    alert(msg);
    return false;
  }
}

function isLetter(str)
{
  var fieldErrors = ""
  var rex = /[^a-zA-Z\s]/

  if ((str == "") || (str == null))
  {
  fieldErrors = "You must enter text.\n";
  }

  if(rex.test(str))
  {
  fieldErrors += "Invalid data was entered.";
  }

  if (fieldErrors)
  {
  return false;
  }
  else
  {
  return true;
  }
}

function isAlphaNum(str)
{
  var fieldErrors = ""
  var rex = /[^\w]/

  if ((str == "") || (str == null) || isblank(str))
  {
  fieldErrors = "You must enter something.\n";
  }

  if(rex.test(str))
  {
  fieldErrors += "Check that you have only entered letters and-or numbers.";
  }

  if (fieldErrors)
  {
  return false;
  }
  else
  {
  return true;
  }
}

function isblank(s)
{
  for(var i = 0; i < s.length; i++)
  {
    var c = s.charAt(i);
    if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
  }
  return true;
}

function replace(string,text,by) 
{
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function regExTest(whatString,whatPattern)
{
	var regX = new RegExp(whatPattern);
	return regX.test(whatString);
}
	
	
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e)
{
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];

	if(input.value.length >= len && !containsElement(filter,keyCode)) 
	{
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}
}

function containsElement(arr, ele)
{
	var found = false
	var index = 0;
	while(!found && index < arr.length)
	{
		if(arr[index] == ele)
		{
			found = true;
		}
		else
		{
			index++;
		}
	}
	return found;
}

function getIndex(input) 
{
	var index = -1
	var i = 0
	var found = false;
	while (i < input.form.length && index == -1)
	{
		if (input.form[i] == input)
		{
			index = i;
		}
		else 
		{
			i++;
		}
	}
	return index;
}

