var i = 0, n = 0, Valid = true;
//
// ...Format number into currency string
function FormatCurrency(s) {
  var d = String(s + .005);
  if (d == 'null') d = '$.00'; else {
    if (d.charAt(0) == '-') d = d.substring(1);
    while (d.charAt(0) == '0') d = d.substring(1);
    i = d.lastIndexOf('.');
    if (i == -1) d += '.';
    d += '00';
    i = d.lastIndexOf('.');
    n = i + 3;
    if (i < (d.length - 3))
      d = d.substring(0, n);
    i -= 3;
    while (i > 0) {
      d = d.substring(0, i) + ',' + d.substring(i);
      i -= 3;}
    if (s < 0) d = '(' + d + ')';
    d = '$' + d;}
  return d;}
//
// ...Format number into dollar string
function FormatDollar(s) {
  var d = String(s);
  if (d == 'null') d = ''; else {
    if (d.charAt(0) == '-') d = d.substring(1);
    while (d.charAt(0) == '0') d = d.substring(1);
    i = d.length;
    i -= 3;
    while (i > 0) {
      d = d.substring(0, i) + ',' + d.substring(i);
      i -= 3;}
    if (s < 0) d = '(' + d + ')';
    if (d.length) d = '$' + d;}
  return d;}
//
// ...Format number into display string
function FormatNumber(s) {
  var d = String(s);
  if (d == 'null') d = ''; else {
    if (d.charAt(0) == '-') d = d.substring(1);
    while (d.length && d.charAt(0) == '0') d = d.substring(1);
    i = d.length - 3;
    while (i > 0) {
      d = d.substring(0, i) + ',' + d.substring(i);
      i -= 3;}
    if (s < 0) d = '(' + d + ')';}
  return d;}
//
// ...Format number into percent string
function FormatPercent(s) {
  var d = String(s);
  if (d == 'null') d = ''; else {
    if (d.charAt(0) == '-') d = d.substring(1);
    while (d.charAt(0) == '0') d = d.substring(1);
    i = d.length;
    i -= 3;
    while (i > 0) {
      d = d.substring(0, i) + ',' + d.substring(i);
      i -= 3;}
    if (s < 0) d = '(' + d + ')';
    if (d.length) d += '%';}
  return d;}
//
// ...Validate a dollar amount
function ParseCurrency(o) {
  n = Number(o.value.replace(/[$,]/g, ''));
  Valid = (!isNaN(n));
  if (Valid) o.value = FormatCurrency(n); else {
    var s = o.value + ' is not a valid dollar amount.\r\n';
    s += 'Please enter a dollar amount in a 2-decimal format.';
    alert(s);
    o.focus();}
  return Valid;}
//
// ...Validate a dollar amount
function ParseDollar(o) {
  n = Number(o.value.replace(/[$,.]/g, ''));
  Valid = (!isNaN(n));
  if (Valid) o.value = FormatDollar(n); else {
    var s = o.value + ' is not a valid dollar amount.\r\n';
    s += 'Please enter a whole dollar amount.';
    alert(s);
    o.focus();}
  return Valid;}
//
// ...Validate a number
function ParseNumber(o) {
  n = Number(o.value.replace(/[,]/g, ''));
  Valid = (!isNaN(n));
  if (Valid) o.value = FormatNumber(n); else {
    var s = o.value + ' is not a valid number.\r\n';
    s += 'Please enter a number with only digits and a decimal point.';
    alert(s);
    o.focus();}
  return Valid;}
function ParseInteger(o) {
  var s = o.value.replace(/[,]/g, '');
  if (s.charAt(0) == '(' && s.charAt(s.length - 1) == ')')
    s = '-' + s.substr(1, s.length - 2);
  Valid = (s.indexOf('.') == -1);
  var n = Number(s);
  if (Valid) Valid = (!isNaN(n));
  if (Valid) o.value = FormatNumber(n); else {
    s = o.value + ' is not a valid number.\r\n';
    s += 'Please enter a number with only digits.';
    alert(s);
    o.focus();}
  return Valid;}
//
// ...Validate an age
function ParseAge(o) {
  n = Number(o.value.replace(/[,]/g, ''));
  Valid = (!isNaN(n));
  if (Valid) Valid = (n >= 0 && n <= 120);
  if (Valid) o.value = FormatNumber(n); else {
    var s = o.value + ' is not a valid number.\r\n';
    s += 'Please enter an age between 0 and 120 years.';
    alert(s);
    o.focus();}
  return Valid;}
//
// ...Validate a percentage
function ParsePercent(o) {
  n = Number(o.value.replace(/[%,.]/g, ''));
  Valid = (!isNaN(n));
  if (Valid) Valid = (n >= 0 && n <= 100);
  if (Valid) o.value = FormatPercent(n); else {
    var s = o.value + ' is not a valid percentage.\r\n';
    s += 'Please enter a percentage that is between 0 and 100.';
    alert(s);
    o.focus();}
  return Valid;}
//
// ...Validate a date
function ParseDate(o) {
  var DateString = o.value.replace(/[\.,\s\\-]/g, '/');
  if (!DateString.length) return;
  var Today = new Date();
  var CurrentYear = Today.getFullYear().toString();
  if (/^\d{6}$/.test(DateString) || /^\d{8}$/.test(DateString))
    DateString = DateString.substring(0, 2) + '/' +
      DateString.substring(2, 4) + '/' + DateString.substring(4);
  if (/^\d{4}\/\d\d?\/\d\d?$/.test(DateString))
    DateString = DateString.substring(5) + '/' + DateString.substring(0, 4);
  if (/^\d\d?\/\d\d?$/.test(DateString)) DateString += '/' + CurrentYear;
  var ys = DateString.lastIndexOf('/');
  if (ys != -1) {
    var dl = DateString.length - 1;
    var yl = dl - ys;
    if (yl == 0) DateString += CurrentYear; else {
      if (yl > 1) dl--; else
        DateString = DateString.substr(0, dl) + '0' + DateString.substr(dl);
      if (yl <= 2) {
        var cc = parseInt(CurrentYear.substr(0, 2));
        var dy = parseInt(DateString.substr(dl));
        var cy = parseInt(CurrentYear.substr(2));
        if (dy >  50 && cy <= 50) cc -= 1;
        if (dy <= 50 && cy >  50) cc += 1;
        cc += 100;
        DateString = DateString.substr(0, dl) +
          cc.toString().substr(1) + DateString.substr(dl);}}}
  var DateObject = new Date(DateString);
  if (DateObject.toString() == 'NaN') {
    var s = o.value + ' is not a valid date.\r\n';
    s += 'Please enter a date in the mm/dd/yyyy format.';
    alert(s);
    o.focus();
    return Valid = false;}
  var Month = DateObject.getMonth() + 101;
  DateString = Month.toString().substr(1) + '/';
  Month = DateObject.getDate() + 100;
  DateString += Month.toString().substr(1) + '/';
  DateString += DateObject.getFullYear().toString();
  o.value = DateString;
  return Valid = true;}
//
// ...Validate a dollar amount with limits
function ValidDollar(o, Low, High) {
  n = Number(o.value.replace(/[$,.]/g, ''));
  Valid = (!isNaN(n));
  if (Valid) o.value = FormatDollar(n); else {
    var s = o.value + ' is not a valid dollar amount.\r\n';
    s += 'Please enter a whole dollar amount.';
    alert(s);}
  if (Valid && n < Low) {
    Valid = false;
    var s = FormatDollar(n) + ' is less than the wholesale price of ';
    s += FormatDollar(Low) + '\r\nPlease enter an amount greater than that.';
    alert(s);}
  if (Valid && n > High) {
    Valid = false;
    var s = FormatDollar(n) + ' is greater than the maximum retail price of ';
    s += FormatDollar(High) + '\r\nPlease enter an amount less than that.';
    alert(s);}
  if (!Valid) o.focus();
  return Valid;}
function CheckDollar(f, Low, High) {
  var o = document.getElementsByName('A' + f)[0];
  if (document.getElementsByName('F' + f)[0].checked) ValidDollar(o, Low, High);
  else if (o.value.length) {
    Valid = false;
    var s = 'This amount has not been selected and must be blank.\r\n';
    s += 'Please clear the amount from this entry field.';
    alert(s);
    o.focus();}
  return Valid;}
//
// ...Validate required field
function ValidRequired(f) {
  var o = document.getElementsByName(f)[0];
  Valid = (o.value.length > 0);
  if (!Valid) {
    alert('This field is required.\r\nPlease make an entry.');
    o.focus();}
  return Valid;}
//
// ...Validate required field
function ValidDelete(o) {
  var s = 'WARNING: You are about to delete ' + o;
  s += '\r\n\r\nDo you want to proceed?';
  return confirm(s);}
//
// ...Validate passwords identical
function ValidCheck(o1, o2) {
  o = document.getElementsByName(o1)[0];
  Valid = (o.value == document.getElementsByName(o2)[0].value);
  if (!Valid) {
    var s = 'The two password fields do not match as required.\r\n';
    s += 'Please reenter them both.';
    alert(s);
    o.focus();}
  return Valid;}

