var Valid = true;
//
// ...Prevent the enter key from going to the introduction
function KeyStruck(e) {
  if (e.keyCode == 13) {
    if (e.srcElement.type == 'textarea') return true;
    for (var i = 0; i < document.body.all.length; i++) {
      var F = document.body.all[i];
      if (F.name == 'process' && F.value == 'Save / Continue') {
        F.click();
        break;}}
    return false;}
  return true;}
//
// ...Attempt to block browser navigation
function BlockNavigation() {
  if (event.clientY < 0) {
    var S = 'It appears that you are attempting to leave this page using\r\n';
    S += 'the browser control buttons instead of the control buttons\r\n';
    S += 'on the page itself.  This may cause loss of entered data or\r\n';
    S += 'log you out of the web site entirely.\r\n';
    S += '\r\nYou may return to your Console Page by clicking "Cancel"\r\n';
    S += 'here and "Save/Return to Console" at the bottom of this page.';
    event.returnValue = S;}
  return;}
//
// ...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;}
//
// ...Format number into currency string
function FormatCurrency(s) {
  var d = String(s).replace(/[$,]/g, '');
  if (d == 'null') d = '$.00'; else {
    d = String(Number(s) + .005);
    if (d.charAt(0) == '-') d = d.substring(1);
    while (d.charAt(0) == '0') d = d.substring(1);
    var i = d.lastIndexOf('.');
    if (i == -1) d += '.';
    d += '00';
    i = d.lastIndexOf('.');
    var 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).replace(/[$,]/g, '');
  if (d == 'null') d = ''; else {
    if (d.charAt(0) == '-') d = d.substring(1);
    while (d.charAt(0) == '0') d = d.substring(1);
    var 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).replace(/[,]/g, '');
  if (d == 'null') d = ''; else {
    if (d.charAt(0) == '-') d = d.substring(1);
    while (d.length && d.charAt(0) == '0') d = d.substring(1);
    var i = d.length - 3;
    while (i > 0) {
      d = d.substring(0, i) + ',' + d.substring(i);
      i -= 3;}
    if (s < 0) d = '(' + d + ')';}
  return d;}
function StringTwoDigit(o) {
  var s = String(o);
  if (s.length < 2) s = '0' + s;
  return s;}
//
// ...Format number into percent string
function FormatPercent(s) {
  var d = String(s).replace(/[%,.]/g, '');
  if (d == 'null') d = ''; else {
    if (d.charAt(0) == '-') d = d.substring(1);
    while (d.charAt(0) == '0') d = d.substring(1);
    var 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 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 required selection
function ValidRadio(f, n) {
  Valid = false;
  var o = -1;
  for (var i = 0; i < f.length; i++) if (f[i].name == n) {
    if (o < 0) o = i;
    if (f[i].checked) Valid = true;}
  if (o < 0) {
    alert('The field ' + n + ' does not exist in the form.');
    Valid = false;}
  else if (!Valid) {
    alert('One of these selections is required.\r\nPlease select an item.');
    f[o].focus();}
  return Valid;}
function ValidSelect(o) {
  var O = o.options;
  Valid = false;
  for (var i = 0; i < O.length; i++)
    if (O[i].selected) Valid = (i > 0);
  if (!Valid) {
    alert('One of these selections is required.\r\nPlease select an item.');
    o.focus();}
  return Valid;}
//
// ...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;}
//
// ...Validate a dollar amount
function ParseCurrency(o) {
  var 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) {
  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 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;}
//
// ...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 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) {
  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 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 an expiration date
function ParseExpDate(o) {
  var d = o.value.replace(/[\.,\s\\-]/g, '/');
  if (!d.length) return;
  var s = d.lastIndexOf('/');
  Valid = (s != -1);
  if (Valid) {
    var m = Number(d.substring(0, s++));
    Valid = (!isNaN(m));}
  if (Valid) Valid = (m >= 1 && m <= 12);
  if (Valid) {
    var y = Number(d.substring(s));
    Valid = (!isNaN(y));}
  if (Valid) Valid = (y <= 2300);
  if (Valid && y < 100) {
    var t = new Date();
    var c = Number(String(t.getFullYear()).substr(0, 2)) * 100;
    y += c;}
  if (Valid) o.value = StringTwoDigit(m) + '/' + String(y); else {
    var s = o.value + ' is not a valid expiration date.\r\n';
    s += 'Please enter a date in the "mm/yyyy" format.';
    alert(s);
    o.focus();}
  return Valid;}
//
// ...Validate a time of day
function ParseTime(o) {
  Valid = true;
  var H = 0, M = 0, S = 0, AP, i = 0, j = 0;
  var T = o.value.replace(/[\.,\\-]/g, ':');
  if (!T.length) return Valid;
  if (/^\d{6}.*$/.test(T)) {
    H = Number(T.substr(0, 2));
    M = Number(T.substr(2, 2));
    S = Number(T.substr(4, 2));
    AP = T.substring(6);}
  else if (/^\d{4}.*$/.test(T)) {
    H = Number(T.substr(0, 2));
    M = Number(T.substr(2, 2));
    S = 0;
    AP = T.substring(4);}
  else if (/^\d{1,2}:\d{1,2}:\d{1,2}.*$/.test(T)) {
    i = T.indexOf(':');
    H = Number(T.substring(0, i));
    j = i + 1;
    i = T.indexOf(':', j);
    M = Number(T.substring(j, i));
    T = T.substring(++i);
    i = T.search(/\D/);
    if (i == -1) i = T.length
    S = Number(T.substring(0, i));
    AP = T.substring(i);}
  else if (/^\d{1,2}:\d{1,2}.*$/.test(T)) {
    i = T.indexOf(':');
    H = Number(T.substring(0, i));
    T = T.substring(++i);
    i = T.search(/\D/);
    if (i == -1) i = T.length
    M = Number(T.substring(0, i));
    S = 0;
    AP = T.substring(i);}
  else Valid = false;
  if (!(/^\s*$/.test(AP)))
    if (/^\s*[Pp][Mm]?\s*$/.test(AP)) H += 12;
    else if (!(/^\s*[Aa][Mm]?\s*$/.test(AP))) Valid = false;
  if (H < 0 || H > 23) Valid = false;
  if (M < 0 || M > 59) Valid = false;
  if (S < 0 || S > 59) Valid = false;
  if (Valid) {
    if (H > 12) T = StringTwoDigit(H - 12);
    else T = StringTwoDigit(H);
    T += ':' + StringTwoDigit(M) + ':' + StringTwoDigit(S);
    if (H < 12) T += ' AM';
    else T += ' PM';
    o.value = T;}
  else {
    T = o.value + ' is not a valid time.\r\n';
    T += 'Please enter a date in the "hh:mm:ss AP" format.';
    alert(T);
    o.focus();}
  return Valid;}
//
// ...Validate a telephone number
function ParsePhone(o) {
  Valid = false;
  var s = o.value;
  if (!s.length) return Valid = true;
  if (/^\(\d{3}\)\s\d{3}-\d{4}$/.test(s)) return Valid = true;
  if (/^\(\d{3}\)\d{3}-\d{4}$/.test(s)) {
    s = s.substring(0, 5) + ' ' + s.substring(5);
    Valid = true;}
  if (/^\d{3}\-\d{3}-\d{4}$/.test(s)) {
    s = '(' + s.substring(0, 3) + ') ' + s.substring(4);
    Valid = true;}
  if (/^\d{10}$/.test(s)) {
    s = '(' + s.substring(0, 3) + ') ' + s.substring(3, 6) + '-' + s.substring(6);
    Valid = true;}
  if (Valid) o.value = s; else {
    T = s + ' is not a valid phone number.\r\n';
    T += 'Please enter a phone number in the "(nnn) nnn-nnnn" format.';
    alert(T);
    o.focus();}
  return Valid;}
//
// ...Validate a dollar amount with limits
function ValidDollar(o, Low, High) {
  var 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 an email address
function ValidEmail(o) {
  var s = o.value;
  Valid = (/^.+@.+\..{1,4}$/.test(s));
  if (!Valid) {
    T = s + ' is not a valid email address.\r\n';
    T += 'Please enter an email address in the "name@domain" format.';
    alert(T);
    o.focus();}
  return Valid;}

