var i = 0, n = 0, Valid = true, LastClass, LastSrc, LastClick;
var Hold = new Array();
var Clear = new Array();
//
// ...Count text field characters
function TextCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit)
    field.value = field.value.substring(0, maxlimit);
  else 
    document.getElementById(countfield).innerText = '(' +
      String(maxlimit - field.value.length) + ' Characters Available)';
  return;}
//
// ...Clear and restore text and data fields
function HoldText(n, field) {
  Hold[n] = field.value;
  return;}
function ClearText(n, field, countfield) {
  Hold[n] = field.value;
  field.value = '';
  document.getElementById(countfield).innerText = '';
  return;}
function RestoreText(n, field, countfield, maxlimit) {
  field.value = Hold[n];
  TextCounter(field, countfield, maxlimit);
  return;}
function HoldData(n, field) {
  Hold[n] = document.getElementsByName(field)[0].value;
  Clear[n] = false;
  return;}
function ClearData(n, field) {
  if (!Clear[n]) {
    Clear[n] = true;
    Hold[n] = document.getElementsByName(field)[0].value;
    document.getElementsByName(field)[0].value = '';}
  return;}
function CompareData(n, field) {
  return (Hold[n] == document.getElementsByName(field)[0].value);}
function RestoreData(n, field) {
  document.getElementsByName(field)[0].value = Hold[n];
  Clear[n] = false;
  return;}
function HoldFields(Start, End) {
  for (i = Start; i <= End; i++) {
    SetName(i);
    HoldData(i, fn);}
  return;}
function CopyFields(Start, End, Offset) {
  for (i = Start; i <= End; i++) {
    SetName(i + Offset);
    HoldData(i, fn);}
  return;}
function ClearFields(Start, End) {
  for (i = Start; i <= End; i++) {
    SetName(i);
    ClearData(i, fn);}
  return;}
function RestoreFields(Start, End) {
  for (i = Start; i <= End; i++) {
    SetName(i);
    RestoreData(i, fn);}
  return;}
function ResetFields(Start, End, Offset) {
  for (i = Start; i <= End; i++) {
    SetName(i);
    if (!CompareData(i + Offset, fn)) {
      ClearData(i, fn);
      RestoreData(i + Offset, fn);}}
  return;}
function SetClear(Start, End) {
  for (i = Start; i <= End; i++) {
    Hold[i] = '';
    Clear[i] = true;}
  return;}
//
// ...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 n = Number(String(s).replace(/[$,]/g, '')), d;
  if (isNaN(n)) d = 'null'; else {
    d = String(n + .5);
    n = d.indexOf('.');
    if (n != -1) d = d.substr(0, n);}
  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) {
  var s = String(o.value.replace(/[$,]/g, ''));
  if (s.charAt(0) == '(' && s.charAt(s.length - 1) == ')')
    s = '-' + s.substr(1, s.length - 2);
  var n = Number(s);
  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;}
function ParsePositiveCurrency(o) {
  var n = Number(o.value.replace(/[$,]/g, ''));
  Valid = (!isNaN(n));
  if (Valid) Valid = (Number(n) >= 0);
  if (Valid) o.value = FormatCurrency(n); else {
    var s = o.value + ' is not a valid positive dollar amount.\r\n';
    s += 'Please enter a positive dollar amount in a 2-decimal format.';
    alert(s);
    o.focus();}
  return Valid;}
function NumberFromCurrency(s) {
  var d = String(s);
  var p = (d.indexOf('(') == -1);
  var d = d.replace(/[$,\(\)]/g, '');
  if (!d.length) d = '0';
  if (!p) d = '-' + d;
  return Number(d);}
//
// ...Validate a dollar amount
function ParseDollar(o) {
  var n = Number(o.value.split('.')[0].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) {
  var s = String(o.value.replace(/[,]/g, ''));
  if (s.charAt(0) == '(' && s.charAt(s.length - 1) == ')')
    s = '-' + s.substr(1, s.length - 2);
  var n = Number(s);
  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 and a decimal point.';
    alert(s);
    o.focus();}
  return Valid;}
function ParseInteger(o) {
  var s = String(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) {
  var 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 age.\r\n';
    s += 'Please enter an age between 0 and 120 years.';
    alert(s);
    o.focus();}
  return Valid;}
//
// ...Validate a year
function ParseYear(o) {
  var n = Number(o.value.replace(/[,]/g, ''));
  Valid = (!isNaN(n));
  if (Valid) {
    if (!o.value.length) return Valid;
    Valid = (n >= 1800 && n <= 2200);}
  if (!Valid) {
    var s = o.value + ' is not a valid year.\r\n';
    s += 'Please enter a year between 1800 and 2200.';
    alert(s);
    o.focus();}
  return Valid;}
//
// ...Validate a percentage
function ParsePercent(o) {
  var 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 ratio
function ParseRatio(o) {
  var n = Number(o.value.replace(/[%,.]/g, ''));
  Valid = (!isNaN(n));
  if (Valid) Valid = (n >= 0 && n < 4096);
  if (Valid) o.value = FormatNumber(n); else {
    var s = o.value + ' is not a valid ratio.\r\n';
    s += 'Please enter a number that is between 0 and 4095.';
    alert(s);
    o.focus();}
  return Valid;}
//
// ...Validate a standard date
function ParseDate(o) {
  var DateString = o.value.replace(/[\.,\s\\-]/g, '/');
  if (!DateString.length) return Valid = true;
  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(o) {
  Valid = (o.value.length > 0);
  if (!Valid) {
    alert('This field is required.\r\nPlease make an entry.');
    o.focus();}
  return Valid;}
//
// ...Validate password field
function ValidPassword(o) {
  Valid = (o.value.length >= 7);
  if (Valid) Valid = (o.value.search(/[A-Z]/) != -1);
  if (Valid) Valid = (o.value.search(/[a-z]/) != -1);
  if (Valid) Valid = (o.value.search(/\d/) != -1);
  if (!Valid) {
    var s = 'The password must be at least seven (7) characters long.\r\n';
    s += 'It must contain at least one upper-case letter, one lower-\r\n';
    s += 'case letter, and one number.  Please adjust accordingly.';
    alert(s);
    o.focus();}
  return Valid;}
//
// ...Validate passwords identical
function ValidCheck(o1, o2) {
  if (!ValidPassword(document.forms.Entry[o1])) return false;
  Valid = (document.forms.Entry[o1].value == document.forms.Entry[o2].value);
  if (!Valid) {
    var s = 'The two password fields do not match as required.\r\n';
    s += 'Please reenter them both.';
    alert(s);
    document.forms.Entry[o1].focus();}
  return Valid;}
//
// ...Change menu background color
function MenuOver(o) {
  LastClass = o.className;
  o.className = 'menubarpink';
  LastSrc = o.childNodes[1].src;
  o.childNodes[1].src = LastSrc.replace(/PointOff/, 'Pointer');
  return;}
function MenuOut(o) {
  o.className = LastClass;
  o.childNodes[1].src = LastSrc;
  return;}
function MenuClick(o) {
  var e = document.getElementsByName('MenuButton');
  n = e.length;
  for (i = 0; i < n; i++) {
    e[i].className = 'menubar';
    e[i].childNodes[1].src =
      e[i].childNodes[1].src.replace(/Pointer/, 'PointOff');}
  LastClass = 'menubarblue';
  o.className = LastClass;
  LastSrc = o.childNodes[1].src.replace(/PointOff/, 'Pointer');
  o.childNodes[1].src = LastSrc;
  LastClick = o.childNodes[3].innerHTML.replace('&amp;', '&');
  return;}
//
// ...Open the explanatory in a new window
function Explanatory(P, H) {
  var S = 'width=700,height=';
  var sh = screen.height - 60;
  if (H > sh) S += String(sh) + ',top=0'; else {
    S += String(H) + ',top=';
    S += String((screen.height) ? (screen.height - H) / 2 : 100);}
  S += ',left=' + String((screen.width) ? (screen.width - 700) / 2 : 100);
  S += ',scrollbars=yes,location=no,directories=no';
  S += ',status=no,menubar=no,toolbar=no,resizable=no';
  window.open(P, 'Explanatory', S);
  return false;}
//
// ...Show file upload progress bar
function ShowPseudoProgress(F, P) {
  if (F.length) {
    var i = F.lastIndexOf('\\');
    var N = F;
    if (i != -1) N = F.substring(i + 1);
    var W = '/ADMIN/FrameBar.asp?to=10&PID=' + P + '&FileName=' + N + '&b=';
    var strAppVersion = navigator.appVersion;
    if (strAppVersion.indexOf('MSIE') != -1
    && strAppVersion.substr(strAppVersion.indexOf('MSIE') + 5, 1) > 4) {
      var winstyle = "dialogWidth=385px; dialogHeight:140px; center:yes";
      window.showModelessDialog(W + 'IE', null, winstyle);}
    else {
      window.open(W + 'NN', '', 'width=370,height=115', true);}}
  return true;}
