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 verify(f)
{
  var empty_fields = "";
  var fCount = 0;
  var empty_options = "";
  var oCount = 0;
  var errors = "";
  var eCount = 0;
  var msg = "";

  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))
      {
        empty_fields += "\n" + replace(e.name,"_"," ") + " is required";
        fCount += 1
        continue;
      }
      if(e.numeric)
      {
	for(zz=0;zz<e.value.length;zz++)
	{
	  var v = parseFloat(e.value.substring(zz));
          if (isNaN(v))
          {	  
            errors += "\n" + replace(e.name,"_"," ") + " must be numeric";
            eCount = eCount + 1;
	    break;
          }
	}
      }
      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(test(e.value) == 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 == ""))
      {
        empty_options += "\nA " + replace(e.name,"_"," ") + " selection is required";
        oCount = oCount + 1;
        continue;
      }
    }
  }

  if ((!empty_fields) && (!empty_options) && !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 = "choice"
      if (oCount > 1)
        {choicesVar += "s"}
      msg += "\n" + oCount + " required " + choicesVar + " not selected.";
      msg += empty_options += "\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)
  {
  alert(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)
  {
  alert(fieldErrors);
  return false;
  }
  else
  {
  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 test(src) {
     var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
     return regex.test(src);
  }
