function ValidateInteger(aElement, aiMin, aiMax, aiDefault)
{
  var lsElementValue = aElement.value
//  if (lsElementValue == "")
//  {
//    alert("Enter a number in the field, please.")
//    aElement.value = aiDefault
//    return false
//  }
  for (var i = 0; i < lsElementValue.length; i++)
  {
    var lsCh = lsElementValue.substring(i, i + 1)
    if ((lsCh < "0") || (lsCh > "9"))
    {
      
      alert("Enter a number, please.")
      aElement.value = aiDefault
      return false
    }
  }
  var liVal = parseInt(lsElementValue, 10)
  if ((liVal < aiMin) || (liVal > aiMax))
  {
    alert("Enter a number from " + aiMin + " to " + aiMax + ".")
    aElement.value = aiDefault
    return false
  }
  aElement.value = liVal
  return true
}

function ValidatePhone(aElement)
{
  var lsElementValue = aElement.value
  if (lsElementValue.length < 10)
  {
    alert("Enter a valid 10 digit phone number please, without dashes or spaces, i.e., numbers only.")
    return false
  }
  for (var i = 0; i < lsElementValue.length; i++)
  {
    var lsCh = lsElementValue.substring(i, i + 1)
    if ((lsCh < "0") || (lsCh > "9"))
    {
      alert("Enter a valid 10 digit phone number please, without dashes or spaces, i.e., numbers only.")
      return false
    }
  }
  return true
}

function RemoveFormatting(aElement)
{
  var lsElementValue = aElement.value
  var lsBuffer1 = ""
  for (var i = 0; i < lsElementValue.length; i++)
  {
    var lsCh = lsElementValue.substring(i, i + 1)
    if (((lsCh >= "0") && (lsCh <= "9")) || (lsCh == "."))
      lsBuffer1 += lsCh;
  }
  aElement.value = lsBuffer1
}

function ValidateDollar(aElement, arMin, arMax, asDefault)
{
  var lsElementValue = aElement.value
  var isNegative = false
  if (lsElementValue.substring(0, 1) == "-")
  {
    isNegative = true
    lsElementValue = lsElementValue.substring(1, lsElementValue.length)
  }
//  if (lsElementValue == "")
//  {
//    alert("Enter a number in the field, please.")
//    aElement.value = asDefault
//    return false
//  }
  var lsBuffer1 = ""
  for (var i = 0; i < lsElementValue.length; i++)
  {
    var lsCh = lsElementValue.substring(i, i + 1)
    if (((lsCh >= "0") && (lsCh <= "9")) || (lsCh == "."))
      lsBuffer1 += lsCh;
    else if ((lsCh != "$") && (lsCh != ","))
    {
      alert("Enter a number, please.")
      aElement.value = asDefault
      return false
    }
  }
  if (isNegative)
  {
    lsBuffer1 = "-" + lsBuffer1
  }
  var lrVal = parseFloat(lsBuffer1)
  if ((lrVal < arMin) || (lrVal > arMax))
  {
    alert("Try a number from $" + arMin + " to $" + arMax + ".")
    aElement.value = asDefault
    return false
  }
  else
  {
    if (isNegative)
    {
      lsBuffer1 = lsBuffer1.substring(1, lsBuffer1.length)
    }
    var liComma = 0
    var lsBuffer2 = ""
    var liDecimalPos = lsBuffer1.indexOf(".")
    for (var i = lsBuffer1.length; i > 0; i--)
    {
      if (liComma > 2)
      {
        lsBuffer2 += ","
        liComma = 0
      }
      lsBuffer2 += lsBuffer1.substring(i, i - 1);
      if ((liDecimalPos == -1) || (i <= liDecimalPos))
      {
        liComma += 1
      }
    }
    if (isNegative)
    {
      lsBuffer1 = "-$"
    }
    else
    {
      lsBuffer1 = "$"
    }
    for (var i = lsBuffer2.length; i > 0; i--)
      lsBuffer1 += lsBuffer2.substring(i, i - 1)
    aElement.value = lsBuffer1
  }
  return true
}

function ValidatePercent(aElement, arMin, arMax, asDefault)
{
  var lsElementValue = aElement.value
//  if (lsElementValue == "")
//  {
//    alert("Enter a number in the field, please.")
//    aElement.value = asDefault
//    return false
//  }
  var lsBuffer1 = ""
  for (var i = 0; i < lsElementValue.length; i++)
  {
    var lsCh = lsElementValue.substring(i, i + 1)
    if (((lsCh >= "0") && (lsCh <= "9")) || (lsCh =="."))
      lsBuffer1 += lsCh;
    else if (lsCh !="%")
    {
      alert("Enter a number, please.")
      aElement.value = asDefault
      return false
    }
  }
  var lrVal = parseFloat(lsBuffer1)
  if (lrVal >= 1)
    lrVal /= 100
  if ((lrVal < (arMin)) || (lrVal > (arMax)))
  {
    alert("Enter a number from " + (arMin * 100) + "% to " + (arMax * 100) + "%.")
    aElement.value = asDefault
    return false
  }
  else
  {
    lsBuffer = round(lrVal * 100, 2)
    aElement.value = lsBuffer + "%";
  }
  return true
}

function round(number,X) 
{
  // rounds number to X = (!X ? 3 : X);
  return Math.round(number*Math.pow(100,X))/Math.pow(100,X);
}

