
var isNav = (navigator.appName.indexOf("Netscape") != -1);
var isIE = (navigator.appName.indexOf("Microsoft") != -1);

function isIE3() {   
  return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='3')); 
}

var re1 = new RegExp("^\\W+");     // get rid of leading nonword characters.
var re2 = new RegExp("\\W+$");     // get rid of trailing nonword characters.
var re3 = new RegExp("\\W+", "g"); // match one or more consequetive nonword chars (i.e. spaces/hyphens/slashes etc)

var re4 = new RegExp("^ +");       // search values: leading spaces
var re5 = new RegExp(" +$");       // search values: trailing spaces

function rdr_hist_dates(frm) {

  var fromstr = frm.datefrom.value;
  var tostr = frm.dateto.value;

  fromemp = fromstr.replace(re3, '');
  toemp = tostr.replace(re3, '');

// reject if effectively empty fields ?
  if ((fromemp.length == 0) || (toemp.length == 0)) {
    alert('date values required in both fields');
    return false;    
  }

  err_str = '';
  delim = '_';                            // using a space caused problems for NN4, so using an underscore

  fromstr = fromstr.replace(re1, '');
  fromstr = fromstr.replace(re2, '');
  fromstr = fromstr.replace(re3, delim);
  tostr = tostr.replace(re1, '');
  tostr = tostr.replace(re2, '');
  tostr = tostr.replace(re3, delim);

  var fromarr = fromstr.split(delim);     // split is javascript 1.2
  var toarr = tostr.split(delim);

// three elements or less ?
  if (fromarr.length > 3) {
    err_str = err_str.concat((err_str ? "\n" : ''), (' From date indeterminable'));
  }
  if (toarr.length > 3) {
    err_str = err_str.concat((err_str ? "\n" : ''), (' To date indeterminable'));
  }

  if (err_str != '') {                    // there were more than three elements
    alert(err_str);
    return false;
  }

  var months = ';jan;feb;mar;apr;may;jun;jul;aug;sep;oct;nov;dec';

  in_fmon = fromarr[1];
  in_tmon = toarr[1];

  out_fmon = ''; out_tmon = '';

  for (var i=0; i<2; i++) {
    outm = '';
    check = ((i == 0) ? in_fmon : in_tmon);
    if (isNaN(check)) {                                       // getting "wordy"
      srch4 = ';' + check.substr(0, 3).toLowerCase();
      where = months.indexOf(srch4);
      if (where != -1) outm = (where/4 + 1) * 1;              // it's in the abbreviated string
    } else {
      if ((check >= 1) && (check <= 12)) outm = (check * 1);  // within 1 to 12 - ok
    }
    if (outm == '') {
      alert('invalid ' + ((i == 0) ? 'from' : 'to') + ' month');
      return false;
    }
    if (i == 0) {
      out_fmon = outm;
    } else {
      out_tmon = outm;
    }
  }

  var day_arr = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  in_fday = fromarr[0];
  in_tday = toarr[0];

  out_fday = ''; out_tday = '';

  for (var i=0; i<2; i++) {
    outd = '';
    check = ((i == 0) ? in_fday : in_tday);
    if (!(isNaN(check))) {                                     // it's a number
      month = ((i == 0) ? out_fmon : out_tmon);
      maxd = day_arr[(month - 1)];
      if ((check >= 1) && (check <= maxd)) outd = (check * 1)
    }
    if (outd == '') {
      alert('invalid ' + ((i == 0) ? 'from' : 'to') + ' day');
      return false;
    }
    if (i == 0) {
      out_fday = outd;
    } else {
      out_tday = outd;
    }
  }

  today  = new Date();
  thisyr = today.getFullYear();

  in_fyr = ((fromarr.length == 3) ? fromarr[2] : thisyr);   // default to current year if not defined
  in_tyr = ((toarr.length == 3) ? toarr[2] : thisyr);

  out_fyr = ''; out_tyr = '';

  for (var i=0; i<2; i++) {
    outyr = '';
    check = ((i == 0) ? in_fyr : in_tyr);
    if (check == '') {
      outyr = thisyr;
    } else {
      if (!(isNaN(check))) {        // only consider years as numbers.
        check = check * 1;          // don't need to test for -ve (non-alphanums deleted earlier)
        if (check <= 30) {          // 0 to 30 - assume 2000->2030
          outyr = check + 2000;
          if (outyr > thisyr) outyr = thisyr;
        } else {
          if (check <= 99) {        // 31 to 99 - assume 1931->1999
            outyr = check + 1900;
          } else {
            if (check < 1900) {     // 100 to 1899 - error
            } else {
              if (check > thisyr) { // beyond current year - assume current year
                outyr = thisyr;
              } else {              // 1900 to current year - no modification required.
                outyr = check;
              }
            }
          }
        }
      }
    }
    if (outyr == '') {
      alert('invalid ' + ((i == 0) ? 'from' : 'to') + ' year');
      return false;
    }
    if (i == 0) {
      out_fyr = outyr * 1;
    } else {
      out_tyr = outyr * 1;
    }
  }

  for (var i=0; i<2; i++) {
    if (i == 0) {
      dd = out_fday; mm = out_fmon; yy = out_fyr;
    } else { 
      dd = out_tday; mm = out_tmon; yy = out_tyr;
    }
    if ((mm == 2) && (dd == 29)) {
      if (Math.ceil(yy/4) != Math.floor(yy/4))  {       // not exactly divisible by 4 so a leap year
        alert('invalid ' + ((i == 0) ? 'from' : 'to') + ' date');
        return false;
      }
    }
  }

// now compare the dates
  frdate = new Date(out_fyr, (out_fmon - 1), out_fday);
  todate = new Date(out_tyr, (out_tmon - 1), out_tday);

  if (frdate > todate) {                                    // incorrectly sequenced dates
    alert('The "From" date (' + frdate.toLocaleString() + ') must be prior to the "To" date (' + todate.toLocaleString() + ')');
    return false;
  }

  var month_arr = new Array("JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER");

  frstr = out_fday + ' ' + month_arr[(out_fmon - 1)] + ' ' + out_fyr;
  tostr = out_fday + ' ' + month_arr[(out_tmon - 1)] + ' ' + out_tyr;

  frm.datefrom.value = frstr;
  frm.dateto.value = tostr;

  return false;
}

function login_go2(rdr, pwd) {
  if (rdr != 'geoff') {
    window.location='error.htm';
  } else {
    if (pwd == 'password') {
      window.location='srch_adv.htm';
    } else {
      window.location='error.htm';
    }
  }
  return true;    
}

function validate(frm) {
  if (frm.name == "advsrch") {
     return srch_adv_validate();
  } else {
    if (frm.name == "gensrch") {
      return srch_gen_validate();
    }
  }  
  return false;
}

function srch_gen_validate() {
  var frm = document.gensrch;
  err_str = '';

  st0val = frm.searchterm0.value.replace(re4, ''); st0val = st0val.replace(re5, '');

  frm.searchterm0.value = st0val;                               // update the html prior to form submission

  if (st0val == '') {
    err_str = ' Please enter a search term'
  }
  if (err_str != '') {
    alert(err_str);
    frm.searchterm0.focus();
    return false;
  }
  return true;
}

function srch_adv_validate() {
  var frm = document.advsrch;
  err_str = '';

//---- check the terms first, make sure there's a term and then in order

  st1val = frm.searchterm1.value.replace(re4, ''); st1val = st1val.replace(re5, '');
  st2val = frm.searchterm2.value.replace(re4, ''); st2val = st2val.replace(re5, '');
  st3val = frm.searchterm3.value.replace(re4, ''); st3val = st3val.replace(re5, '');

  frm.searchterm1.value = st1val;                              // update the html prior to form submission
  frm.searchterm2.value = st2val;                              // update the html prior to form submission
  frm.searchterm3.value = st3val;                              // update the html prior to form submission

  if ((st1val == '') && (st2val == '') && (st3val == '')) {
    err_str = ' Please enter a search term';
    frm.searchterm1.focus();
  } else {
    if ( ((st1val == '') && (st2val != '')) || ((st2val == '') && (st3val != '')) ) {
      err_str = ' Please enter search terms in order';
    }
  }

  if (err_str != '') {
    alert(err_str);
    return false;
  }

//---- now check the media types and booleans
//     don't bother checking for someone "AND NOT"'ing on identical terms!

  m1val = frm.Media1.selectedIndex;
  m2val = frm.Media2.selectedIndex;
  m3val = frm.Media3.selectedIndex;

  b1val = frm.Bool1.options[frm.Bool1.selectedIndex].text;     // can't use 'value' values until they're in the html
  b2val = frm.Bool2.options[frm.Bool2.selectedIndex].text;     //   so using 'text' values instead

  isSt1 = (st1val != ''); isSt2 = (st2val != ''); isSt3 = (st3val != '');

//---- situation 1
//     only 1st and 2nd search terms, being AND'ed, neither media of "Any Media", and different media

  if (isSt2 && !isSt3) {
    if ((b1val == "AND") && ((m1val != 0) && (m2val != 0)) && (m1val != m2val)) {
      err_str = 'cannot "AND" on different media types';
    }
  }

  if (err_str != '') {
    alert(err_str);
    return false;
  }

//---- situation 2
//     all three search terms

  if (isSt3) {                                                 // all three search terms
    if ((b1val != "AND") && (b2val != "AND")) {                // "OR"'ing or "AND NOT"'ing

    } else {
      if ((b1val == "AND") && (b2val == "AND")) {              // medias must be of Any Media and/or one other type
        flag = true;
        for (i=1; i<4; i++) {
          switch(i) {
            case 1: media = m1val; break;
            case 2: media = m2val; break;
            case 3: media = m3val; break;
          }
          if (media != 0) {                                    // if media type not "Any media" it's significant
            if (flag) {                                        
              match = media;                                   // record the media type
              flag = false;
            } else {
              if (media != match) {
                err_str = 'cannot "AND" on different media types';
              }
            }
          }      
        }
      } else {
        if ((b1val == "AND") && (b2val == "AND NOT")) {        // make sure not "AND"'ing opposing media
          if (((m1val != 0) && (m2val != 0)) && (m1val != m2val)) {
            err_str = 'cannot "AND" on different media types';
          }
        } else {
          if ((b1val == "AND NOT") && (b2val == "AND")) {      // make sure not "AND"'ing opposing media
            if (((m1val != 0) && (m3val != 0)) && (m1val != m3val)) {
              err_str = 'cannot "AND" on different media types';
            }
          }
        }
      }
    } 
  }

  if (err_str != '') {
    alert(err_str);
    return false;
  }

  return true;
}

function sys_busy() {
  alert('system busy');
  window.location.replace('sys_busy.htm');
  history.go();
  return false;
}

function rdrlogin_go2() {
//  alert('you\'ve come from ' + document.referrer);
  if (document.referrer.indexOf('_res') != -1) {     // coming from results screen ..
    if (document.rdr.code.value == 'R0001' && document.rdr.pin.value == 'PIN') {  // .. and valid reader entered
      window.location = 'rsv_stat.htm';
    } else {
      window.location = 'error.htm';
    }
  } else {                                           // coming from link ..
    if (document.rdr.code.value == 'R0001' && document.rdr.pin.value == 'PIN') {  // .. and valid reader entered
      window.location='rdr_loan.htm';
    } else {
      window.location = 'error.htm';
    }
  }
  return true;
}

function rdrlogin_godirect() {
/* reader has to log in every time!
  alert(document.referrer);
  if (document.referrer.indexOf('_res') == -1) {     // not coming from results screen ..
    if (confirm('is reader known?')) {
      window.location.replace('rdr_loan.htm');
      history.go();
    }
  }
  return true;*/
}

