//****************** Validation functions ********************************
//isEmpty(s) - Check whether string s is empty. Alert with str
//isWhitespace (s) - Check whether string s has a blank character
//isEmail (s) - Check whether string s is a valid e-mail. Alert with str
//isZipcode(strZip) - Check whether string s is a valid US zip code
//IsTime(strTime) - Check whether string s is a valid time
//ForceNumber(val, str) - Check whether a field val is numeric. Alert with str
//ForceMoney(val, str) - Check whether a field val is money. Alert with str


//------------------------------------------------------------------------
function isEmpty(entered, alertbox)
{
  with (entered)
  {
    if (value==null || value=="" || value.length == 0)
    {
      if (alertbox!="")
      {
        alert(alertbox);
      }
      return false;
    }
    else
    {
      return true;
    }
  }
} 

//------------------------------------------------------------------------
function isWhitespace (s)
{
  var i;

  if (isEmpty(s)) return true;

  for (i = 0; i < s.length; i++)
  {
    var c = s.charAt(i);
    if (whitespace.indexOf(c) == -1) return false;
  }

  // All characters are whitespace.
  return true;
}


//------------------------------------------------------------------------
function isNumber(entered, alertbox)
{
  with (entered)
  {
    for (var i=0; i < value.length; i++)
    {
      if (value.charAt(i) < '0' || value.charAt(i) > '9')
      {
        if (alertbox!="")
        {
            alert(alertbox + " You must enter only digits.");
        }
        return false;
      }
    }
  }
return true;
}


//------------------------------------------------------------------------
function isEmail (entered, alertbox)
{

  with (entered)
  {
    if (value.length < 1 ) return true;

    apos=value.indexOf("@"); 
    dotpos=value.lastIndexOf(".");
    lastpos=value.length-1;
    if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) 
    {
      if (alertbox!="")
        {
          alert(alertbox);
        }
      return false;
    }
    else
    {
      return true;
    }
  }
} 


//------------------------------------------------------------------------
function isZipCode(entered, alertbox)
{
  with (entered)
  {
    if (value.length != 5 && value.length != 10) 
    {
      if (alertbox!="")
      {
        alert(alertbox + " The length must be 5 digits or 9 digits and a - after the fifth digit.");
      }
      return false;
    }
    else
    {
      for (var i=0; i < value.length; i++)
      {
        if ((value.charAt(i) < '0' || value.charAt(i) > '9') && value.charAt(i) != '-')
        {
          if (alertbox!="")
          {
              alert(alertbox + " You must enter only digits or a - after the fifth digit.");
          }
          return false;
        }
        if ((value.charAt(i) == '-') && (i != 5))
        {
          if (alertbox!="")
          {
              alert(alertbox + " You only use a - after the fifth digit of the zip code.");
              }
          return false;
        }
      }
    }
return true;
  }
}


//------------------------------------------------------------------------
function IsTime(strTime)
{
  var strTestTime = new String(strTime);
  strTestTime.toUpperCase();
  var bolTime = false;

  if (strTestTime.indexOf("PM",1) != -1 || strTestTime.indexOf("AM",1))
    bolTime = true;

  if (bolTime && strTestTime.indexOf(":",0) == 0)
    bolTime = false;

  var nColonPlace = strTestTime.indexOf(":",1);
  if (bolTime && ((parseInt(nColonPlace) + 5) < (strTestTime.length - 1) || (parseInt(nColonPlace) + 4) > (strTestTime.length - 1)))
    bolTime = false;

  return bolTime;
}


//------------------------------------------------------------------------
function isPhoneFax(entered, alertbox)
{
  with (entered)
  {
    if ( value.length < 7 && value.length >= 1 )
    {
      if (alertbox!="")
      {
          alert(alertbox + " The phone/fax number entered is not long enough to be valid.");
      }
      return false;
    }
    for (var i=0; i < value.length; i++)
    {
      if ((value.charAt(i) < '0' || value.charAt(i) > '9') && value.charAt(i) != '-' && value.charAt(i) != '(' && value.charAt(i) != ')' && value.charAt(i) != ' ')
      {
        if (alertbox!="")
        {
            alert(alertbox + " You must enter only numeric digits, (, ), or - to create a valid phone/fax number.");
        }
        return false;
      }
    }
  }
  return true;
}


//------------------------------------------------------------------------
function ForceNumber(val, str)
{
  var strField = new String(val.value);
  var i = 0;
	
  if (isWhitespace(strField)) return true;

  for (i = 0; i < strField.length; i++)
    if (strField.charAt(i) < '0' || strField.charAt(i) > '9')
    {
      alert(str);
      val.focus();
      val.select();
      return false;
    }
  return true;
}

//------------------------------------------------------------------------
function ForceMoney(val, alertbox)
{
  var strField = new String(val.value);
  var i = 0;
	
  for (i = 0; i < strField.length; i++)
    if ((strField.charAt(i) < '0' || strField.charAt(i) > '9') && (strField.charAt(i) != '.') && (strField.charAt(i) != '$'))
    {
      if (alertbox!="")
      {
        alert(alertbox);
      }
      return false;
    }

  return true;
}



//------------------------------------------------------------------------
function ForceDate(strDate,strErrMsg)
{
  var str = new String(strDate.value);

  if (isWhitespace(str))
  {
    return true;
  }

  var i = 0, count = str.length, j = 0;
  while ((str.charAt(i) != "/" && str.charAt(i) != "-") && i < count)
    i++;

  if (i == count || i > 2)
  {
    alert(strErrMsg);
    strDate.focus();
    strDate.select();
    return false;
  }

  var addOne = false;
  if (i == 2) addOne = true;

  if (!isDateNumber(str.substring(0,i),1))
  {
    alert(strErrMsg);
    strDate.focus();
    strDate.select();
    return false;
  }

  j = i+1;
  i = 0;

  while ((str.charAt(i+j) != "/" && str.charAt(j+i) != "-") && i+j < count)
    i++;

  if (i+j == count || i > 2)
  {
    alert(strErrMsg);
    strDate.focus();
    strDate.select();
    return false;
  }

  if (!isDateNumber(str.substring(j,i+j),2))
  {
    alert(strErrMsg);
    strDate.focus();
    strDate.select();
    return false;
  }

  j = i+3;
  i = 0;

  if (addOne) j++;

  while (i+j < count)
    i++;

  if (i != 2 && i != 4)
  {
    alert(strErrMsg);
    strDate.focus();
    strDate.select();
    return false;
  }

  if (!isDateNumber(str.substring(j,i+j),3))
  {
    alert(strErrMsg);
    strDate.focus();
    strDate.select();
    return false;
  }

  return true;
}


//------------------------------------------------------------------------
function MakeDecimalNumber(strInput)
{
  var re = new RegExp('[$,]', 'gi')
  return(strInput.replace(re, ''));
}

//------------------------------------------------------------------------
  function formatCurrency(num)
  {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
      num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
      cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
      num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '$' + num + '.' + cents);
  }


