/*
   This will become a standard routines library for general JavaScripting
   in any html file produced.  It can be included with:

     <SCRIPT LANGUAGE="Javascript" SRC="<path>standard.js"></SCRIPT>

   where <path> is the location of this resource.
*/

var month_short=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var day_short=new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
var month_long=new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var day_long=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var ordinal_ext=new Array('th','st','nd','rd');

function printtoday(type) {
  t = new Date();
  d = t.getDate();
  y = t.getYear();

  if (y < 1900) y += 1900;

  if (d % 10 > 3 || (d > 10 && d < 14))
    dext = 0;
  else
    dext = d % 10;

  switch (type) {
  case "short":
    document.write(day_short[t.getDay()] + ", " + month_short[t.getMonth()] + "&nbsp;" + d + ordinal_ext[dext] + " " + y);
  break;

  default: 
    document.write(day_long[t.getDay()] + ", " + month_long[t.getMonth()] + "&nbsp;" + d + ordinal_ext[dext] + " " + y);
  }
}

/* #### START OF FORM STUFF #### */

/*
   You can validate URLs, email addresses, text, integers, decimal numbers,
   dates, times, and elapsed times.

   To use these functions you will need to NAME= all forms and input items.

   The form needs to have included onSubmit="return valform(this);".  This
   will return true, and hence submit, if all candidate fields are valid.
   If any field fails validation then false is returned, a message is
   displayed to the user, the invalid field is highlighted, and focus is
   sent to it.

   After defining an input text field you wish to validate, you will need
   to include some javascript to attach a validation rule to the field.

   e.g. <SCRIPT LANGUAGE="Javascript">addval(document.f1.t3, "int");</SCRIPT>

   If you have more than one field with the same name in the same form you
   will have to reference it by its array offset.  This offset is zero (0)
   based.  Therefore, the second instance of a given name within a given
   form would be:

   e.g. <SCRIPT LANGUAGE="Javascript">addval(document.f1.t6[1], "int");</SCRIPT>

   The addval function requires at least two (2) arguments with a maximum
   of four (4) arguments.  The first argument is the object which requires
   validation.  The second argument is a string which represents the type
   of validation required.  The validation routines understand the
   following rules:

      text      Any characters at all.
      int       A series of digits (0-9) with or without a leading plus (+)
                or minus (-).
      float     The same as int except that it may be followed by a period
                (.) and more digits (0-9).
      date      A date in the following form dd/mm/[cc]yy.  You can use
                periods (.) or dashes (-) instead of the slash (/) if you
                prefer.  The separator must be consistent.
      email     A proper email address. ie. Joe.Blow@mail-host2.com
      url       A proper http based URL. ie. http://that.server.com/index.cfm
      time      A time of day like hh:mm[:ss][ (a|p)m].
      etime     An elapsed time like hh:mm[:ss].

   The remaining arguments are optional.  The first of these, if present,
   is a boolean (true or false) argument stating whether or not the field
   is mandatory.  The default is false.  If the field is mandatory, then
   it cannot be blank and must contain a valid value.

   The next optional field is a string which can be used to set a message
   you wish displayed if the value in the field in invalid.  The default
   message for any invalid field is "Illegal values present.  Please correct."
*/

urlre = /^\s*([hH][tT][tT][pP][sS]?:\/\/(([A-Za-z0-9][A-Za-z0-9\.\-]*\.[A-Za-z]{2,6})|([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+))(:[0-9]+)?\/.*)/;

emailre = /^\s*([a-zA-Z0-9_\.\-]+\@(([A-Za-z0-9][A-Za-z0-9\.\-]*\.[A-Za-z]{2,6})|([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)))\s*$/;

textre = /^\s*(.*)\s*$/;

intre = /^\s*([-+]?[0-9]+)\s*$/;

floatre = /^\s*([-+]?(([0-9]+(\.[0-9]+)?)|(\.[0-9]+)))\s*$/;

datere = /^\s*([0-9][0-9]?)([\/\-\.])([0-9][0-9]?)\2(([0-9][0-9])?[0-9][0-9])\s*$/;

timere = /^\s*([0-9][0-9]?):([0-9][0-9])(:([0-9][0-9]))?( ?([aApP])(\.?[mM]\.?)?)?\s*$/;

etimere = /^\s*([0-9]+):([0-9][0-9])(:([0-9][0-9]))?\s*$/;


function gettime(teststr) {
  if (typeof teststr != "string")
    return null;

  if (teststr == "")
    return "";

  timeparts = timere.exec(teststr);
  if (timeparts == null)
    return null;

  hr = timeparts[1] * 1;
  min = timeparts[2] * 1;
  offset = 0;
  if (timeparts.length >= 5) {
    sec = timeparts[4] * 1;
    if (timeparts.length >= 7) {
      if (timeparts[6] != "") {
        if (timeparts[6] == "p" || timeparts[6] == "P")
          offset = 43200;
        else
          offset = 86400;
        if (hr == 0) hr = 13;
        if (hr == 12) hr = 0;
      }
    }
  }
  else
    sec = 0;
  if ((offset > 0 && hr > 11) || (offset == 0 && hr > 23) || min > 59 || sec > 59)
    return null;

  return (hr * 60 + min) * 60 + sec + (offset % 86400);
}

function getetime(teststr) {
  if (typeof teststr != "string")
    return null;

  if (teststr == "")
    return "";

  timeparts = etimere.exec(teststr);
  if (timeparts == null)
    return null;

  hr = timeparts[1] * 1;
  min = timeparts[2] * 1;
  if (timeparts.length >= 5)
    sec = timeparts[4] * 1;
  else
    sec = 0;
  if (min > 59 || sec > 59)
    return null;

  return (hr * 60 + min) * 60 + sec;
}

function geturl(teststr) {
  if (typeof teststr != "string")
    return null;

  if (teststr == "")
    return "";

  urlparts = urlre.exec(teststr);
  if (urlparts == null)
    return null;

  if (urlparts[1] == "")
    return null;
  else
    return urlparts[1];
}

function getemail(teststr) {
  if (typeof teststr != "string")
    return null;

  if (teststr == "")
    return "";

  emailparts = emailre.exec(teststr);
  if (emailparts == null)
    return null;

  if (emailparts[1] == "" || emailparts[2].indexOf("..") != -1)
    return null;
  else
    return emailparts[1];
}

function getint(teststr) {
  if (typeof teststr != "string")
    return null;

  if (teststr == "")
    return "";

  intparts = intre.exec(teststr);
  if (intparts == null)
    return null;

  if (intparts[1] == "")
    return null;
  else
    return intparts[1] * 1;
}

function getfloat(teststr) {
  if (typeof teststr != "string")
    return null;

  if (teststr == "")
    return "";

  floatparts = floatre.exec(teststr);
  if (floatparts == null)
    return null;

  if (floatparts[1] == "")
    return null;
  else
    return floatparts[1] * 1.0;
}

function gettext(teststr) {
  if (typeof teststr != "string")
    return null;

  if (teststr == "")
    return "";

  textpart = textre.exec(teststr);
  if (textpart == null)
    return null;
  else
    return textpart[1];
}

function getdate(teststr) {
  if (typeof teststr != "string")
    return null;

  if (teststr == "")
    return "";

  dateparts = datere.exec(teststr);
  if (dateparts == null)
    return null;
  if (dateparts.length == 5 || dateparts[5] == "")
    dateparts[4] = (dateparts[4] < 90 ? "20" : "") + dateparts[4];

  testdate = new Date(dateparts[4], dateparts[3] - 1, dateparts[1]);
  y = testdate.getYear();
  if (y < 1900) y += 1900;

  if (dateparts[1] != testdate.getDate() || dateparts[3] != (testdate.getMonth() + 1) || dateparts[4] != y)
    return null
  else
    return testdate;
}

valformflds = new Array();

function addval(obj, objtype, arg1, arg2) {
  if (typeof obj.form == "undefined" || typeof objtype != "string")
    return;

  switch (objtype) {
    case "date" :
      fn = getdate;
    break;

    case "text" :
      fn = gettext;
    break;

    case "int" :
      fn = getint;
    break;

    case "float" :
      fn = getfloat;
    break;

    case "time" :
      fn = gettime;
    break;

    case "etime" :
      fn = getetime;
    break;

    case "email" :
      fn = getemail;
    break;

    case "url" :
      fn = geturl;
    break;

    default :
      fn = null;
    break;
  }

  if (fn == null)
    return;

  i = 0;
  while (i < valformflds.length) {
    if (valformflds[i][0] == obj.form)
      break;
    i ++;
  }
  if (i >= valformflds.length) {
    valformflds[i] = new Array();
    valformflds[i][0] = obj.form;
  }
  j = valformflds[i].length;
  valformflds[i][j] = new Array(4);
  valformflds[i][j][0] = obj;
  valformflds[i][j][1] = fn;
  valformflds[i][j][2] = false;
  if (typeof arg2 == "boolean")
    valformflds[i][j][2] = arg2;
  if (typeof arg1 == "boolean")
    valformflds[i][j][2] = arg1;
  if (typeof arg1 == "string")
    valformflds[i][j][3] = arg1;
  if (typeof arg2 == "string")
    valformflds[i][j][3] = arg2;
}

function valform(frm) {
  i = 0;
  while (i < valformflds.length) {
    if (valformflds[i][0] == frm)
      break;
    i ++;
  }
  if (i >= valformflds.length)
    return true;

  for (j = 1; j < valformflds[i].length; j ++) {
    result = valformflds[i][j][1](valformflds[i][j][0].value);
    if (result == null || (valformflds[i][j][2] && result + "" == "")) {
      if (typeof valformflds[i][j][3] == "undefined")
        alert("Illegal values present.  Please correct.");
      else
        alert(valformflds[i][j][3]);
      valformflds[i][j][0].select();
      valformflds[i][j][0].focus();
      return false;
    }
  }

  return true;
}

/* #### END OF FORM STUFF #### */

/* #### Rollover stuff #### */

rollovers = new Array();

function addroll(img_name, status_text, over_img) {

/* there are no imagesso passing an alt image is silly */

  if (typeof document.images.length == "undefined" && typeof over_img != "undefined")
    return;

  rollovers[img_name] = new Array(4);
  
  rollovers[img_name][0] = null;
  rollovers[img_name][1] = status_text;
  if (typeof over_img != "undefined") {
    for (i = 0; i < document.images.length; i ++) {
      if (document.images[i].name == img_name) {
        rollovers[img_name][0] = document.images[i];
        break;
      }
    }
    if (rollovers[img_name][0] != null) {
      rollovers[img_name][2] = new Image();
      rollovers[img_name][2].src = over_img;

      rollovers[img_name][3] = new Image();
      rollovers[img_name][3].src = document.images[i].src;
    }
  }
}

function rollover(img_name) {
  if (typeof img_name.name == "string")
    img_name = img_name.name;

  if (typeof rollovers.length == "undefined" || typeof rollovers[img_name].length == "undefined")
    return true;

  if (rollovers[img_name][0] != null)
    rollovers[img_name][0].src = rollovers[img_name][2].src;

  window.status = rollovers[img_name][1];
  return true;
}

function rollout(img_name) {
  if (typeof img_name.name == "string")
    img_name = img_name.name;

  if (typeof rollovers.length == "undefined" || typeof rollovers[img_name].length == "undefined")
    return true;

  if (rollovers[img_name][0] != null)
    rollovers[img_name][0].src = rollovers[img_name][3].src;

  window.status = '';
  return true;
}

/* #### Window creation stuff #### */

function newwin(url, name, width, height, resize, scroll, menubar, locationbar, statusbar) {
  win = window.open(url, name, "width=" + width + ",height=" + height + ",resizable=" + resize + ",scrollbars=" + scroll + ",menubar=" + menubar + ",location=" + locationbar + ",status=" + statusbar);
  win.opener = self;

  return false;
}

/* #### Calendar popups */

var calwin;

function fillmth(frmfld, yr, mth, current) {

  calwin.document.open();

  if (current == null) current = "";

  sdate = new Date(yr, mth, 1);
  i = sdate.getDay();
  sdate = sdate.getTime();
  if (mth == 11)
    edate = new Date(yr + 1, 0, 1);
  else
    edate = new Date(yr, mth + 1, 1);
  edate = edate.getTime();
  days = (edate - sdate) / 86400000;
  t = new Date();
  tyr = t.getYear();
  if (tyr < 1900) tyr += 1900;
  tmth = t.getMonth();
  today = -7;
  if (t.getTime() >= sdate && t.getTime() < edate)
    today = t.getDate();
  picked = -7;
  if (current > "") {
    dateparts = datere.exec(current);
    if (dateparts != null) {
      if (dateparts.length == 5 || dateparts[5] == "")
        dateparts[4] = (dateparts[4] < 90 ? "20" : "") + dateparts[4];
      cdate = new Date(dateparts[4], dateparts[3] - 1, dateparts[1]);
      cyr = cdate.getYear();
      if (cyr < 1900) cyr += 1900;
      cmth = cdate.getMonth();
      if (cdate.getTime() >= sdate && cdate.getTime() < edate)
        picked = cdate.getDate();
    }
    else
      current = "";
  }

  with (calwin.document) {
    write('<HTML><HEAD><TITLE>calendar</TITLE><STYLE>\
.basefont { font-family: Verdana; font-size: 12pt; font-weight: bold; }\
.calfont { font-family: Verdana; font-size: 10pt; }\
.dayfont { color: darkgreen; font-weight: bold; }\
</STYLE><SCRIPT LANGUAGE="Javascript"><!--\n\
function retdate(dtval) {\n\
 self.opener.document.' + frmfld + '.value = dtval;\n\
 window.close();\n\
 return false;\n\
}\n\
// -->\
</SCRIPT></HEAD>\
<BODY LINK=black VLINK=black ALINK=black TEXT=black CLASS=basefont><CENTER>\
' + month_long[mth] + ' ' + yr + '<BR><TABLE WIDTH=100% BORDER=1 CELLSPACING=0 CELLPADDING=0 BGCOLOR=#e0e0e0 CLASS=basefont><TR ALIGN=center><TD>\
[<A HREF="javascript:self.opener.fillmth(\'' + frmfld + '\', ' + (yr - 1) + ', ' + mth + ', \'' + current + '\');">' + (yr - 1) + '</A>]<TD>\
[<A HREF="javascript:self.opener.fillmth(\'' + frmfld + '\', ' + (yr - (mth == 0 ? 1 : 0)) + ', ' + ((mth + 11) % 12) + ', \'' + current + '\');">' + month_short[((mth + 11) % 12)] + '</A>]<TD>\
[<A HREF="javascript:self.opener.fillmth(\'' + frmfld + '\', ' + (yr + (mth == 11 ? 1 : 0)) + ', ' + ((mth + 1) % 12) + ', \'' + current + '\');">' + month_short[((mth + 1) % 12)] + '</A>]<TD>\
[<A HREF="javascript:self.opener.fillmth(\'' + frmfld + '\', ' + (yr + 1) + ', ' + mth + ', \'' + current + '\');">' + (yr + 1) + '</A>]\
<TR ALIGN=center><TD COLSPAN=');
    if (current > "")
      write('2><A HREF="javascript:self.opener.fillmth(\'' + frmfld + '\', ' + cyr + ', ' + cmth + ', \'' + current + '\');">Selected</A><TD COLSPAN=2');
    else
      write('4');
    write('><A HREF="javascript:self.opener.fillmth(\'' + frmfld + '\', ' + tyr + ', ' + tmth + ', \'' + current + '\');">Today</A></TABLE>\
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=2 WIDTH=100% CLASS=calfont>\
<TR CLASS=dayFont><TD WIDTH=14%>Sun<TD WIDTH=14%>Mon<TD WIDTH=14%>Tue<TD WIDTH=*>Wed<TD WIDTH=14%>Thu<TD WIDTH=14%>Fri<TD WIDTH=14%>Sat');

    l = 1 - i;
    k = i + days - 1;
    mystr = '/' + (mth + 1) + '/' + yr;
    for (j = 0; i <= k || j % 7 > 0; j++, l ++) {
      if (j % 7 == 0)
        write('<TR>');
      write('<TD');
      if (l == picked)
        write(' BACKGROUND=/images/cal_circle.gif');
      if (l == today)
        write(' BGCOLOR=pink');
      else if (j % 7 == 0 || j % 7 == 6)
        write(' BGCOLOR=#e0e0e0');
      if (j < i || j > k)
        write('>&nbsp;');
      else
        write(' onClick="retdate(\'' + l + mystr + '\')"><A HREF="#" onClick="retdate(\'' + l + mystr + '\')">' + l + '</A>');
      if (j == i) i ++;
    }

    write('</TABLE></CENTER></BODY></HTML>');
  }

  calwin.document.close();
}

/* pop_mth_date creates a window which will display a calendar for the "current"
   date and fill in frmfld when a date is selected.
 */

function pop_mth_date(frmfld, current) {
  if (typeof frmfld == 'object') frmfld = frmfld.form.name + "." + frmfld.name;
  if (frmfld == null || frmfld == "") return false;

  if (current == null) current = "";
  t = new Date();
  if (current > "") {
    dateparts = datere.exec(current);
    if (dateparts != null) {
      if (dateparts.length == 5 || dateparts[5] == "")
        dateparts[4] = (dateparts[4] < 90 ? "20" : "") + dateparts[4];
      t = new Date(dateparts[4] * 1, dateparts[3] * 1 - 1, dateparts[1]);
    }
  }
  yr = t.getYear();
  if (yr < 1900) yr += 1900;
  mth = t.getMonth();
  if (current > "")
    current = t.getDate() + "/" + (mth + 1) + "/" + yr;

  calwin = window.open("", "Calendar", "width=260,height=235,status=no,resizable=no,top=200,left=200");
  calwin.opener = self;

  fillmth(frmfld, yr, mth, current);
  return false;
}

