// IS functions

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 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;*/
}


