var common_inits = {are_done: 0};


function common_init() {
  // Register functions in common_inits
  // Call from body.onload or near the end of the document
  // Multiple calls are OK
  if (common_inits.are_done) return;
  delete common_inits.are_done;
  for (var f in common_inits) { common_inits[f](); }
  common_inits.are_done = 1;
}

function get(o) {
  if ((typeof o) == 'undefined') { return null; }
  for (var i=1; i < arguments.length; i++) {
    var a = arguments[i];
    if ((typeof o[a]) == 'undefined') { return null; }
    o = o[a];
  }
  return o;
}

if (!get(window, 'onload'))
   window.onload = common_init;

if (!get(window, 'VB_support')) 
   window.VB_support = false;

function attr_map(ob, keys) {
  var m = {};
  for (var i = 0; i < keys.length; i++) {
    var k = keys[i];
    var v = ob.getAttribute(k);
    if (k.charAt(0) == '_') k = k.slice(1);
    m[k] = v;
  }
  return m;
}

function map_query(m) {
  var qs = '';
  for (var k in m) {
    qs = qs + '&' + escape(k) + '=' + escape(m[k]);
  }
  return qs;
}

function set_field(name, v) {
  var obs = document.getElementsByName(name);
  for (var i = 0; i < obs.length; i++) {
    var ob = obs[i];
    if (ob.tagName == 'INPUT' || ob.tagName == 'TEXTAREA') {
      switch (ob.type) {
      case "checkbox":
        ob.checked = !!v;
        break;
      case "radio":
        ob.checked = (v == ob.value);
        break;
      case "select-one":
      case "select-many":
        if (typeof v == "number") { ob.selectedIndex = v; }
        else { ob.value = v; }
        break;
      default:
        ob.value = v;
      }
    }
  }  
}

function set_prop(propname, value) {
  var pp = propname.split('.');
  var elem = document.getElementById(pp[0]);
  for (var i = 1; i < pp.length - 1; i++) {
    elem = elem[pp[i]];
  }
  elem[pp[i]] = value;
  return true;
}

// Cross-browser event handling

function get_event_target(e) {
  return get(e, 'target') || get(window, 'event', 'srcElement');
}

function get_event_other(e) {
  var e2 = get(window, 'event');
  return get(e, 'relatedTarget') || 
         get(e2, 'fromElement') ||
         get(e2, 'toElement');
}

function get_event_key(e) {
  return get(e, 'which') || get(window, 'event', 'keyCode') || 0;
}

function handled_event(e) {
  if (get(e, 'preventDefault')) { e.preventDefault(); }
  if (get(window, 'event')) { window.event.returnValue=false; }
}

function null_event() {
  return false;
}

function write_today() {
  var weekdays = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"
  , "Friday", "Saturday");
  var months = new Array("January", "February", "March", "April", "May", "June"
  , "July", "August", "September", "October", "November", "December");
  var suffixes = new Array("th", "st", "nd", "rd");

  var mydate = new Date();
  var myday = mydate.getDate();
  var mymonth = months[mydate.getMonth()];
  var myyear = mydate.getYear();
  if (myyear<1900) myyear = myyear + 1900;

  if ((myday % 10) < 4 && (myday < 4 || myday >20)) myday = myday+suffixes[myday % 10]
  else myday = myday + "th";
                  
  document.write(mymonth + " " + myday + ", " + myyear);
}

function jumpMenu(targ, selObj) {
  targ.location=selObj.options[selObj.selectedIndex].value;
}

function popWindow(wurl, wname, wwidth, wheight) {
  var opts = 'status=no,scrollbars=yes,resizable=yes,left=100,top=100';
  if (wwidth) opts = opts + ',width=' + wwidth;
  if (wheight) opts = opts + ',height=' + wheight;
  var w = window.open('/mlib/null', wname, opts);
  w.location.replace(wurl);
  if (window.focus) w.focus();
}

function image_auto_over(e) {
  var img = get_event_target(e);
  img.src = img.overimg.src;
}

function image_auto_out(e) {
  var img = get_event_target(e);
  img.src = img.outsrc;
}

var auto_image_dn = false;

function prepare_auto_image(img) {
  var src = img.src;
  if (src.indexOf('/rollover/')>=0) {
    var lastdot = src.lastIndexOf('.');
    if (src.substring(lastdot - 5, lastdot) != "-over") {
      var p = img.parentNode;
      var srcov = src.substring(0, lastdot) + "-over" +
                  src.substring(lastdot, src.length);
      var curbase = document.location.pathname;
      var ppath = p.pathname;
      if (p.tagName == 'A' && ppath != '/' && (
           (ppath.substring(0, 1) == '/' && curbase.indexOf(ppath) == 0) ||
           (curbase.indexOf(ppath) == 1))) {
        if (auto_image_dn) srcov = src.substring(0, lastdot) + "-dn" +
                                   src.substring(lastdot, src.length);
        img.src = srcov;
      } else {
        img.onmouseover = image_auto_over;
        img.onmouseout = image_auto_out;
        img.outsrc = img.src;
        img.overimg = new Image();
        img.overimg.src = srcov;
      }
    }
  }
}

function prepare_auto_images() {
  for (var ii=document.images.length;ii; ii--) {
    var img = document.images[ii - 1];
    prepare_auto_image(img);
  }
  for (ii=0; ii<(document.forms.length); ii++) {
    var form = document.forms[ii];
    for (var jj=0; jj<(form.elements.length); jj++) {
      var elem = form.elements[jj];
      if (elem.type == 'image') prepare_auto_image(elem);
    }
  }
}
common_inits.prepare_auto_images = prepare_auto_images;

function input_auto_advance(e) {
  var inp = get_event_target(e);
  var key = get_event_key(e);
  if (key == 13) {
    handled_event(e);
    inp.next_input.focus();
    if (get(inp, 'next_input', 'select')) { inp.next_input.select(); }
  }
}

function input_auto_submit(e) {
  var inp = get_event_target(e);
  var key = get_event_key(e);
  if (key == 13) {
    inp.form.submit();
    handled_event(e);
    return false;
  }
  return true;
}

function prepare_input_advance() {
  var tofocus = 0;
  for (var fi=document.forms.length - 1; fi>=0; fi--) {
    var form = document.forms[fi];
    var inputs = new Array();
    var ict = 0;
    var inp, neednxt = false;
    for (var ii=0; ii<(form.elements.length); ii++) {
	inp = form.elements[ii];
	if (inp.type == 'hidden') continue;
	if (neednxt) {
	  inputs[ict-1].next_input = inp;
	  neednxt = false;
	}
        switch (inp.type) {
          case 'text':
          case 'password':
          case 'checkbox':
          case 'radio':
          case 'select-one':
          case 'select-multiple':
            inputs[ict++] = inp;
            neednxt = true;
	}
        if (inp.className.search(/\bfirst_focus\b/) >= 0)
          tofocus = inp;
    }
    if (ict > 0) {
	inputs[--ict].onkeypress = input_auto_submit;
    }
    while (ict-- > 0) {
	inp = inputs[ict];
	inp.onkeypress = input_auto_advance;
    }
  }
  if (tofocus) {
    tofocus.focus();
    tofocus.select();
  }
}
common_inits.prepare_input_advance = prepare_input_advance;

function focus_within(id) {
  var inputs = document.getElementById(id).getElementsByTagName('input');
  var tofocus = 0;
  for (var ii=0; ii<(inputs.length); ii++) {
    var inp = inputs[ii];
    if (inp.type == 'hidden') continue;
    if (inp.className.search(/\bshow_focus\b/) >= 0) tofocus = inp;
  }
  if (tofocus) {
    tofocus.focus();
    tofocus.select();
  }
}

var ac_last_over = {};

function auto_class_over(e) {
  var ob = get_event_target(e);
  var leaving = get_event_other(e);
  var last = ac_last_over[ob.ac_group];
  if (leaving == last && ob.parentNode == last) return;
  ac_last_over[ob.ac_group] = ob;
  if (last && last!=ob) last.className = last.ac_base;
  ob.className = ob.ac_over;
}

function auto_class_out(e) {
  var ob = get_event_target(e);
  var entering = get_event_other(e);
  var last = ac_last_over[ob.ac_group];
  if (ob != last || entering.parentNode == last) return;
  ob.className = ob.ac_base;
  ac_last_over[ob.ac_group] = null;
}

function auto_class_click(e) {
  var ob = get_event_target(e);
  if (ob.ac_link) document.location.href = ob.ac_link.href;
}

function prepare_auto_class(ob, c_over, acgroup) {
  var children = ob.children;
  if (!children) children = ob.childNodes;
  if (children)
  for (var ii=0; ii<(children.length); ii++) {
    var child = children[ii];
    if (child.href && child.getAttribute('name') == 'ac_link') {
      ob.ac_link = child;
      ob.onclick = auto_class_click;
      if (document.location.pathname.indexOf(child.pathname) == 0) {
        ob.className = c_over;
      }
      break;
    }
  }
  var c = ob.className;
  if (c != c_over) {
    ob.onmouseover = auto_class_over;
    ob.onmouseout = auto_class_out;
    ob.ac_base = c;
    ob.ac_over = c_over;
    ob.ac_group = acgroup;
  } else ob.class_over = c;
  ob.onmouseover({target: ob})
}

function prepare_auto_class_by_group() {
  var defs = document.getElementsByName('ac_group_def');
  for (var ii=0; ii<defs.length; ii++) {
    var def = defs[ii];
    var group = document.getElementsByName('ac_' + def.value);
    for (var jj=0; jj<group.length; jj++) {
      prepare_auto_class(group[jj], def.className, def.value);
    }
  }
}
common_inits.prepare_auto_class_by_group = prepare_auto_class_by_group;

function SameWinPopup(name) {
  this.name = name
  this.iframe = null;
  this.popwin = null;
  this.target = null;

  this.init = function() {
    var iframe = document.createElement("iframe");
    this.iframe = iframe;
    iframe.name = this.name + '_iframe'
    iframe.style.display = "none";

    var pw = document.getElementById(this.name);
    if (pw == null) {
      pw = document.createElement("div");
      pw.id = this.name;
      document.body.appendChild(pw);
    }
    this.popwin = pw;
    pw.style.display = "none";
    pw.style.position = "absolute";
    pw.onclick = function(e) {e.cancelBubble = true;}
    
    var popup = this;
    iframe.onload = function(e) { popup.loaded(e); }
    this.closer = function(e) { popup.close() }
    document.body.appendChild(iframe);
  }

  this.loaded = function(e) {
    if (this.target == null) return;
    var sx = scrollX;
    var sy = scrollY;
    var pw = this.popwin;
    var src = this.iframe.contentDocument.body.innerHTML;
    if (src.slice(0, 14) == '!ALERT-RELOAD:') {
      alert(src.slice(14));
      src = '!RELOAD';
    }
    if (src == '!RELOAD') {
      location.reload(true);
      return;
    }
    if (src.slice(0, 7) == '!ALERT:') {
      alert(src.slice(7));
      return;
    }
    pw.innerHTML = src;
 
    var cb = document.createElement("div");
    cb.className = "swpcloser";
    cb.style.position = "absolute";
    cb.style.top = "-4px";
    cb.style.height = "7px";
    cb.style.width = "7px";
    cb.onclick = this.closer;
    pw.appendChild(cb);

    var t = this.target;
    var off = t;
    var offtop = t.offsetTop;
    var offleft = t.offsetLeft;
    while (off.offsetParent != pw.offsetParent) {
      off = off.offsetParent;
      offtop += off.offsetTop;
      offleft += off.offsetLeft;
    }
    pw.style.top = offtop + t.offsetHeight + "px";
    var fullw = off.offsetWidth;
    if (offleft * 2 > fullw) {
      pw.style.left = "auto"
      var r = fullw - (offleft + t.offsetWidth);
      if (r < 8) r = 8;
      pw.style.right = r + "px";
      cb.style.left = "-4px";
    } else {
      pw.style.right = "auto"
      pw.style.left = offleft + "px";
      cb.style.right = "-4px";
    }
    pw.style.display = "block";
    scrollTo(sx, sy);
    var subelems = pw.getElementsByTagName("*");
    for (var i=0; i < subelems.length; i++) {
      var ob = subelems[i];
      if ('focus' in ob) {
        ob.focus();
        if ('select' in ob) ob.select();
        break;
      }
    }
    document.addEventListener("click", this.closer, false);
  }
  this.pop = function(target, url) {
    if (!this.iframe) this.init()
    this.target = target;
    this.load(url);
    return false;
  }
  this.load = function(url) {
    this.iframe.contentWindow.location.replace(url);
  }
  this.close = function() {
    this.target = null;
    this.clear();
    this.popwin.innerHTML = "";
  }
  this.clear = function() {
    this.popwin.style.display = "none";
    document.removeEventListener("click", this.closer, false);
  }
}

function AsyncLoad(name, ids) {
  this.name = name
  this.ids = ids
  this.iframe = null;
  this.targets = null;

  this.init = function() {
    var iframe = document.createElement("iframe");
    this.iframe = iframe;
    iframe.name = this.name + '_iframe'
    iframe.style.display = "none";

    var me = this;
    iframe.onload = function(e) { me.loaded(e); }
    document.body.appendChild(iframe);

    var targets = {};
    var ids = this.ids.replace(/ /g, '').split(',');
    for (var i = 0;i < ids.length; i++) {
      var id = ids[i];
      if (id == '') id = this.name;
      targets[id] = document.getElementById(id);
    }
    this.targets = targets;
  }

  this.loaded = function(e) {
    //var sx = scrollX;
    //var sy = scrollY;
    for (id in this.targets) {
      var srcnode = this.iframe.contentDocument.getElementById(id);
      if (srcnode != null) {
        var src = srcnode.innerHTML;
        var target = this.targets[id];
        var s_alert = src.match(/^\s*!ALERT:/);
        if (s_alert) {
          alert(src.slice(s_alert[0].length));
        } else if (target != null) {
          target.innerHTML = src;
          target.style.display = "block";
        }
      }
    }
 
    //scrollTo(sx, sy);
  }
  this.load = function(url) {
    if (!this.iframe) this.init()
    this.iframe.contentWindow.location.replace(url);
  }
  this.clear = function() {
    for (id in this.targets) {
      var t = this.targets[id];
      if (t != null) t.style.display = "none";
    }
  }
}

function select_anchor() {
  // The variable 'selected_anchor' is defined by a script in the document
  document.location.hash = '#' + selected_anchor;
  var anchor = document.getElementsByName(selected_anchor)[0];
  anchor.className = anchor.className + ' highlight';
}

function xmenu_auto_over(e) {
  for (xm in xmenus) {
    if (xmenus[xm].shown) {
      xmenus[xm].hide();
    }
  }
  var m = get_event_target(e);
  while (m.nodeName != 'A') m = m.parentNode;
  if (m.id) webFXMenuHandler.showMenu(xmenus[m.id], m);
}

function xmenu_auto_out(e) {
  var m = get_event_target(e);
  while (m.nodeName != 'A') m = m.parentNode;
  if (m.id) webFXMenuHandler.hideMenu(xmenus[m.id]);
}

function prepare_auto_xmenu(m) {
  m.onmouseover = xmenu_auto_over;
  m.onmouseout = xmenu_auto_out;
}

function prepare_auto_xmenus() {
  var xmenu_els = document.getElementsByName('xmenu');
  for (var ii=0; ii<(xmenu_els.length); ii++) {
    prepare_auto_xmenu(xmenu_els[ii]);
  }
}

function prepare_auto_xmenu_defs() {
  webfxMenuUseHover = true;
  webfxMenuImagePath = "/shared/images/";
  for (var xmi = 0; xmi < xmenu_defs.length; xmi++) {
    var mdef = xmenu_defs[xmi];
    var mname = mdef.menu;
    var menu;
    if (mname in xmenus) {
      menu = xmenus[mname];
    } else {
      menu = new WebFXMenu; 
      menu.width = 150; 
      xmenus[mname] = menu;
    }
    if ('type' in mdef) {
      if (mdef.type == 'separator') {
        menu.add(new WebFXMenuSeparator());
      } else if (mdef.type == 'menu') {
        for (var attr in mdef) if (attr in menu) {
          menu[attr] = mdef[attr]
        }
      }
    } else {
      menu.add(new WebFXMenuItem(mdef.text, mdef.link, mdef.tip));
    }
  }
  for (var xmenu in xmenus) {
    document.write(xmenus[xmenu]);
  }
}

function mime_support(name) {
  var mt = get(navigator, 'mimeTypes');
  return (mt && mt[name]) ? mt[name].enabledPlugin : false;
}

function Flash_support() {
  if (typeof _Flash_support == "undefined") {
    window._Flash_support = false;
    if (VB_support) {
       window._Flash_support = VB_Flash_support();
    } else {
      if (mime_support('application/x-shockwave-flash')) {
        var fv = navigator.plugins["Shockwave Flash"].description;
        window._Flash_support = parseInt(fv.charAt(fv.indexOf('.')-1));
      }
    }
  }
  return _Flash_support;
}

var Flash_n = 0;
function Flash_insert(w, h, path) {
  if (Flash_support() >= 5) {
    var fid = 'Flash_' + Flash_n;
    document.write('<div id="' + fid + '">');
    document.write(Flash_object_s(fid + 'o', w, h, path));
    document.write('<' + '/div>');
    Flash_n++;
  }
}

function setFlash() {
  while (Flash_n) {
    Flash_n--;
    var fid = 'Flash_' + Flash_n;
    var fdiv = document.getElementById(fid);
    fdiv.parentNode.removeChild(fdiv);
  }
}

function Flash_object_s(id, w, h, path) {
  return (
'<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"\n'+
'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"\n'+
' ID="' + id + '" WIDTH="' + w + '" HEIGHT="' + h + '" ALIGN="">\n'+
' <PARAM NAME=movie VALUE="' + path + '">\n'+
' <PARAM NAME=quality VALUE=high>\n'+
' <PARAM NAME=wmode VALUE=transparent>\n'+
' <PARAM NAME=bgcolor VALUE=#FFFFFF>\n'+
' <EMBED src="' + path + '"\n'+
'  quality=high wmode=transparent bgcolor=#FFFFFF swLiveConnect=FALSE\n'+
'  WIDTH="' + w + '" HEIGHT="' + h + '" NAME="' + id + '" ALIGN=""\n'+
'  TYPE="application/x-shockwave-flash"\n'+
'  PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">\n'+
' </EMBED>\n'+
'</OBJECT>\n'
);
}

function fit_screen() {
  if (typeof window.sizeToContent == 'undefined') return false;
  resizeTo(400, 200);
  sizeToContent();
  resizeBy(32, 32);
  var overY = screenY + window.outerHeight - screen.availHeight + 100;
  if (overY > 0)
    resizeBy(0, -overY);
  return true;
}

function docwrite() {
  var lines = docwrite.arguments;
  for (var ii=0; ii < lines.length; ii++) {
    document.write(lines[ii]);
    document.write('\n');
  }
}

if (typeof xmenu_defs == 'object') {
  // For XMenus to work, xmenu_defs must be defined before common.js,
  // and there must be a script in the body that calls
  // prepare_auto_xmenu_defs().
  common_inits.prepare_auto_xmenus = prepare_auto_xmenus;
  var xmenus = {};
  docwrite(
    '<script type="text/javascript"',
    ' src="/shared/xmenu/cssexpr.js"></script>',
    '<link href="/shared/xmenu/xmenu.css"',
    ' type="text/css" rel="StyleSheet">',
    '<script type="text/javascript" ',
    ' src="/shared/xmenu/xmenu.js"></script>');
}


function formatCurrency(num)
{
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
    num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
    cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
  num.substring(num.length-(4*i+3));
  return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function trimNumber(s)
{// Removes leading zero from passed string
  while (s.substr(0,1) == '0' && s.length>1)
  { s = s.substr(1,9999); }
  return s;
}

function pluralize(nAmount)
{// returns a lower case "s" if passed amount > 0
  if (nAmount>1)
    {return "s";}
  return "";
}

