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();
  if (Trim(item.value).length == 0) {
    item.focus();
    alert(strErrorMsg+".");
    return false;
  }
  return true;
}

function nonBlankAdmin(item, itemname) {
  var strErrorMsg = Trim(itemname).toLowerCase();
  if (Trim(item.value).length == 0) {
    item.focus();
    alert(strErrorMsg+".");
    return false;
  }
  return true;
}

function validDate(item, itemname) {
  var strErrorMsg = Trim(itemname) + " must be a valid date.\n In mm/dd/yyyy format";
  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;
    }
  }
  if (isNaN(Date.parse(item.value))) {
    item.focus();
    alert(strErrorMsg+".");
    return false;
  }
  else {
    var correctDate = item.value.split("/");
    var validMonth = parseFloat(correctDate[0]);
    var validDay = parseFloat(correctDate[1]);
    var validYear = parseFloat(correctDate[2]);
	
    if ((validMonth <= 0) || (validMonth > 12)) {
      item.focus();
      alert(strErrorMsg+".");
      return false;
    }
    if ((validDay <= 0) || (validDay > 31)) {
      item.focus();
      alert(strErrorMsg+".");
      return false;
    }
    if ((validMonth == 4) || (validMonth == 6) || (validMonth == 9) || (validMonth == 11)) {
      if (validDay > 30) {
        item.focus();
        alert(strErrorMsg+".");
        return false;
      }
    }
    if (validYear < 1901) {
      item.focus();
      alert(strErrorMsg+".");
      return false;
    }
    if (validYear > 2079) {
      item.focus();
      alert(strErrorMsg+".");
      return false;
    }    
    var isLeapYear = true;
    isLeapYear = (((validYear % 4) == 0) && ((validYear % 100) == 0) && ((validYear % 400) == 0));
    if (!(isLeapYear)) {
      if (validMonth == 2) {
        if (validDay > 28) {
          item.focus();
          alert(strErrorMsg+".");
          return false;
        }
      }
    }
    else {
      if (validMonth == 2) {
        if (validDay > 29) {
          item.focus();
          alert(strErrorMsg+".");
          return false;
        }
      }
    }
  }
  return true;
}

function validPhone(item, itemname) {
  var strErrorMsg = Trim(itemname) + " must consists atleast 6 char and\nnumbers, spaces, hyphens and parentheses only";
  if (Trim(item.value).length > 0) {
    if (Trim(item.value).length < 6) {
      item.focus();
      alert(strErrorMsg+".");
      return false;
    }
    if (!(/^[-0-9() ]*$/.test(item.value))) {
      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 < 4 ) {
    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 validRegion(item,itemname) {
  var strErrorMsg = Trim(itemname) + " is not a valid region code";
  item.value = Trim(item.value);
  if (item.value.length < 3 ) {
    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 = Trim(itemname) + " is not 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 consists 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, itemtype) {
  var strErrorMsg = Trim(itemname) + " is not a valid number";
  if (Trim(itemtype) == "N") {
    var strErrorMsg = Trim(itemname) + " should be in whole dollar";
  }
  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 validTime(item,itemname) {
  var strErrorMsg = Trim(itemname) + " must be a valid time.\n In hh:mm format";
  item.value = Trim(item.value);

  if (!((/^\d{5}$/.test(item.value)) || (/^\d{2}:\d{2}$/.test(item.value)))) {
    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;
}

function validDateRange(item1, item2, itemname1, itemname2) {
  var strErrorMsg = Trim(itemname2) + " should be greater than " + Trim(itemname1);
  var sDateValue = Date.parse(item1.value);
  var eDateValue = Date.parse(item2.value);
  if (eDateValue < sDateValue) {
    item1.focus();
    alert(strErrorMsg+".");
    return false;
  }
  return true;
}

function validDecimal(item, itemname, itemlength) {
  var strErrorMsg = Trim(itemname) + " is not a valid decimal";
  item.value = Trim(item.value);
  var len = parseInt(itemlength);
  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;
    }
  }

  if (item.value.indexOf(".") != item.value.lastIndexOf(".")) {
    item.focus();
    alert(strErrorMsg+".");
    return false;
  }

  if (len == 4) {
    if (parseFloat(item.value) <= 0.0) {
      item.focus();
      alert(strErrorMsg+".");
      return false;
    }

    if (parseFloat(item.value) > 99.99) {
      item.focus();
      alert(Trim(itemname) + " should not be more than 99.99");
      return false;
    }
  }

  if (len == 44) {
    if (parseFloat(item.value) > 99.99) {
      item.focus();
      alert(Trim(itemname) + " should not be more than 99.99");
      return false;
    }
  }

  if (len == 5) {
    if (parseFloat(item.value) > 999.99) {
      item.focus();
      alert(Trim(itemname) + " should not be more than 999.99");
      return false;
    }
  }
 if (len == 6) {
    if (parseFloat(item.value) > 9999.99) {
      item.focus();
      alert(Trim(itemname) + " should not be more than 9999.99");
      return false;
    }
  }

  if (len == 7) {
    if (parseFloat(item.value) > 99999.99) {
      item.focus();
      alert(Trim(itemname) + " should not be more than 99999.99");
      return false;
  }
 }
  if (len == 10) {
    if (parseFloat(item.value) > 9999999999.99) {
      item.focus();
      alert(Trim(itemname) + " should not be more than 9999999999.99");
      return false;
    }
  }

 if (len == 13) {
    if (parseFloat(item.value) > 9999999999.99) {
      item.focus();
      alert(Trim(itemname) + " should not be more than 9999999999.99");
      return false;
    }
  }
  if (len == 15) {
    if (parseFloat(item.value) > 999999999999.99) {
      item.focus();
      alert(Trim(itemname) + " should not be more than 999999999999.99");
      return false;
    }
  }
  
  if (len == 43) {
    if (parseFloat(item.value) < 0.0) {
      item.focus();
      alert(strErrorMsg+".");
      return false;
    }

    if (parseFloat(item.value) > 99.999) {
      item.focus();
      alert(Trim(itemname) + " should not be more than 99.999");
      return false;
    }
  }
  return true;
}

function validUserName(item, itemname) {
  var strErrorMsg = "Invalid " + Trim(itemname);
  item.value = Trim(item.value);
  if (item.value.length < 3) {
    var strErrorMsg = Trim(itemname) + " must have atleast 3 char.";
    item.focus();
    alert(strErrorMsg+".");
    return false;
  }
  var num = "_-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  for (var intLoop = 0; intLoop < item.value.length; intLoop++) {
    if (num.indexOf(item.value.toUpperCase().charAt(intLoop)) == -1) {
      item.focus();
      alert(strErrorMsg+".");
      return false;
    }
  }
  return true;
}

function confirmPassword(item1, item2) {
  var strErrorMsg = "Password and confirm password should be same";
  var sPassValue = Trim(item1.value);
  var ePassValue = Trim(item2.value);
  if (ePassValue != sPassValue) {
    item1.focus();
    alert(strErrorMsg+".");
    return false;
  }
  return true;
}
