function Trim(str) {
  var iChar, iCount;
  var strValue = str;
  iChar = strValue.length - 1;
  iCount = -1;
  while (strValue.charAt(iChar) == ' ' && iChar > iCount)
    --iChar;
  if (iChar != (strValue.length - 1))
    strValue = strValue.slice(0, iChar+1);
  iChar = 0;
  iCount = strValue.length - 1;
  while (strValue.charAt(iChar) == ' ' && iChar < iCount)
    ++iChar;
  if (iChar != 0)
    strValue = strValue.slice(iChar, strValue.length);
  return strValue;
}

function nonBlank(item, itemname) {
//  var strErrorMsg = "Please enter " + Trim(itemname).toLowerCase();
  var strErrorMsg = "Please enter " + Trim(itemname);
  if (Trim(item.value).length == 0) {
    item.focus();
    alert(strErrorMsg);
    return false;
  }
  return true;
}

function validZip(item,itemname) {
  var strErrorMsg = Trim(itemname) + " is not a valid zip code";
  item.value = Trim(item.value);
  if (item.value.length < 5 ) {
    item.focus();
    alert(strErrorMsg);
    return false;
  }
  var num = "0123456789";
  for (var intLoop = 0; intLoop < item.value.length; intLoop++) {
    if (num.indexOf(item.value.charAt(intLoop)) == -1) {
      item.focus();
      alert(strErrorMsg);
      return false;
    }
  }
  return true;
}

function validEmail(item, itemname) {
  var strErrorMsg = "Please enter a valid email address";
  item.value = Trim(item.value);
  var invalidChars = " /:,;";
  if (item.value.length > 0) {     
    for (i=0; i < invalidChars.length; i++)  {    //check for invalid characters
      badChar = invalidChars.charAt(i);
      if (item.value.indexOf(badChar,0) != -1)   {
        item.focus();
        alert(strErrorMsg);
        return false;
      }
    }
    atPos = item.value.indexOf("@",1)         //there must be one "@" symbol
    if (atPos == -1)  {
      item.focus();
      alert(strErrorMsg);
      return false;
    }
    if (item.value.indexOf("@", atPos+1) != -1)  {   //check to make sure only one "@" symbol
      item.focus();
      alert(strErrorMsg);
      return false;
    }
    periodPos = item.value.indexOf(".",atPos);
    if ((periodPos - atPos) == 1) {
      item.focus();
      alert(strErrorMsg);
      return false;
    }
    if (periodPos == -1)  {            //make sure there is one "." after the "@"
      item.focus();
      alert(strErrorMsg);
      return false;
    }
    if (periodPos+3 > item.value.length)  {  //must be at least 2 chars after the "."
      item.focus();
      alert(strErrorMsg);
      return false;
    }
    if (item.value.lastIndexOf(".") >= item.value.length - 1) {
      item.focus();
      alert(strErrorMsg);
      return false;
    }
  }
  return true;
}

function validText(item, itemname) {
  var strErrorMsg = Trim(itemname) + " must consist of letters and spaces only";
  item.value = Trim(item.value);
  var alpha = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  for (var intLoop = 0; intLoop < item.value.length; intLoop++) {
    if (alpha.indexOf(item.value.toUpperCase().charAt(intLoop)) == -1) {
      item.focus();
      alert(strErrorMsg);
      return false;
    }
  }
  return true;
}

function validNumber(item, itemname) {
  var strErrorMsg = Trim(itemname) + " is not a valid number";
  item.value = Trim(item.value);
  var num = "0123456789";
  for (var intLoop = 0; intLoop < item.value.length; intLoop++) {
    if (num.indexOf(item.value.charAt(intLoop)) == -1) {
      item.focus();
      alert(strErrorMsg);
      return false;
    }
  }
  return true;
}

function validChar(item,itemname) {
  var strErrorMsg = Trim(itemname) + " contains invalid characters";
  item.value = Trim(item.value);
  var invalidChars = " /:,;";
  if (item.value.length > 0) {     
    for (i=0; i < invalidChars.length; i++)  {    //check for invalid characters
      badChar = invalidChars.charAt(i);
      if (item.value.indexOf(badChar,0) != -1)   {
        item.focus();
        alert(strErrorMsg);
        return false;
      }
    }
  }
  return true;
}
