//----------------------------------------------------------------------------
// Code to determine the browser and version.
//----------------------------------------------------------------------------

function Browser() {

  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isOP    = false;  // Opera
  this.isNS    = false;  // Netscape
  this.isSAFARI = false;  // Safari
  this.version = null;

  ua = navigator.userAgent;

  s = "Safari";
  if ((i = ua.indexOf(s)) >= 0 ) {
    this.isSAFARI = true;
  }

  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isOP = true;
    this.version = parseFloat(ua.substr(i + s.length));
    if (isNaN(this.version)) {
      // opera 9, at least, uses Opera/9 as the string
      this.version = parseFloat(ua.substr(i + s.length + 1));
    }
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as Netscape 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }

  s = "MSIE";
  if ((i = ua.indexOf(s))) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
}

var browser = new Browser();

function exists(obj)
{
  if (!obj) {
    return false;
  }
  if (typeof(obj)=="unknown") {
    return false;
  }
  if (typeof(obj)=="undefined") {
    return false;
  }
  return true;
}

function kill_event(oEvent)
{
  if (exists(oEvent)) {
    if (exists(oEvent.return_value)) {
      oEvent.return_value=false;
    }
    if (exists(oEvent.cancelBubble)) {
      oEvent.cancelBubble=true;
    }
    if (exists(oEvent.stopPropogation)) {
      oEvent.stopPropagation();
    }
    if (exists(oEvent.preventDefault)) {
      oEvent.preventDefault();
    }
  }
}

// front ends for measuring various attributes under different browsers

function browserWindowLeft()
{
  return window.screenLeft ||
         window.screenX ||
         0;
}

function browserWindowTop()
{
  return window.screenTop ||
         window.screenY ||
         0;
}

function browserWindowWidth()
{
  return window.innerWidth ||
         document.documentElement.clientWidth ||
         document.body.clientWidth ||
         0;
}

function browserWindowHeight()
{
  return window.innerHeight ||
         document.documentElement.clientHeight ||
         document.body.clientHeight ||
         0;
}

function browserBrowserWidth()
{
  return window.outerWidth ||
         0;
}

function browserBrowserHeight()
{
  return window.outerHeight ||
         0;
}

function browserDocumentWidth()
{
  var pc = document.getElementById('page_container');
  return pc.offsetWidth ||
         window.innerWidth ||
         document.documentElement.scrollWidth ||
         document.body.scrollWidth ||
         0;
}

function browserDocumentHeight()
{
  var pc = document.getElementById('page_container');
  return pc.offsetHeight ||
         window.innerHeight ||
         document.documentElement.scrollHeight ||
         document.body.scrollHeight ||
         0;
}

function browserScrollX()
{
  return document.documentElement.scrollLeft ||
         document.body.scrollLeft ||
         window.pageXOffset ||
         0;
}

function browserScrollY()
{
  return document.documentElement.scrollTop ||
         document.body.scrollTop ||
         window.pageYOffset ||
         0;
}

function browserScreenWidth()
{
  return screen.availWidth ||
         screen.width ||
         0;
}

function browserScreenHeight()
{
  return screen.availHeight ||
         screen.height ||
         0;
}

function browserObjectX(obj)
{
  var x = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      x += obj.offsetLeft
      obj = obj.offsetParent;
    }
  }
  else if (obj.x)
    x += obj.x;
  return x;
}

function browserObjectY(obj)
{
  var y = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      y += obj.offsetTop
      obj = obj.offsetParent;
    }
  }
  else if (obj.y)
    y += obj.y;
  return y;
}

function browserStyleSheetMedia(ss)
{
  if (ss.media.mediaText)
    return(ss.media.mediaText)
  if (ss.media)
    return(ss.media)
  return '';
}

function browserCopyToClipboard(value)
{
  if (window.clipboardData) {
    window.clipboardData.setData("Text",value);
  }
}

function browserRowIndex(obj)
{
  if (obj.rindex) {
    return obj.rindex;
  }
  if (obj.rowIndex) {
    return obj.rowIndex;
  }
  return 0;
}

// disable backspace key
document.onkeydown = mykeyhandler;
function mykeyhandler()
{
  if (window.event && window.event.keyCode == 8 && !window.event.srcElement.type) {
    // they clicked backspace off a form element, disable the history back
    window.event.cancelBubble = true;
    window.event.returnValue = false;
    return false;
  }
}

// SECTION: PRINT

function before_printing()
{
//  if (typeof(report_buttons)!="undefined") {
//    report_buttons.style.visibility='hidden';
//  }

  // parse the DOM, switching ON printer-friendly versions of tabs, switching OFF old tabs and images
  if (document.allelements) {
    for (var i=0; i<document.allelements.length; i++) {
      var e = document.allelements[i];
      if (e.getAttribute('sciprint')) {
        if (e.getAttribute('sciprint')=='yes') {
          e.setAttribute('sciprint_saved',e.style.display);
          e.style.display = '';
        } else if (e.getAttribute('sciprint')=='no') {
          e.setAttribute('sciprint_saved',e.style.display);
          e.style.display = 'none';
        }
      }
      if (e.style.getAttribute && e.style.getAttribute('print-font-size')) {
        e.style.setAttribute('screen-font-size',e.style.fontSize);
        e.style.fontSize=e.style.getAttribute('print-font-size');
      }
      // - firefox/safari refuse to add properties on the fly, so I've disabled this for now
      //   but I'm leaving it in case we come up with a way to do this in the future
      // else if (e.style.getPropertyValue && e.style.getPropertyValue('print-font-size')) {
      //   e.style.setPropertyValue('screen-font-size',e.style.fontSize);
      //   e.style.fontSize=e.style.getPropertyValue('print-font-size');
      // }
    }
  }

  // fix textarea bug in IE
  if (document.all) {
    var collTextAreas = document.all.tags("TEXTAREA");
    var oNewDiv;
    var sTemp;
    for (var i = 0; i < collTextAreas.length; i++) {
      oNewDiv = document.createElement("DIV");
      oNewDiv.style.width = collTextAreas(i).clientWidth;
      oNewDiv.style.height = collTextAreas(i).clientHeight;
      oNewDiv.id = collTextAreas(i).uniqueID + "_div";
      // replace all line breaks With HTML line breaks
      sTemp = collTextAreas(i).value.replace(/\n/gi,"<BR>");
      // match 2 spaces With To non-breaking spaces
      sTemp = sTemp.replace(/\s\s/gi,"  ");
      oNewDiv.innerHTML = sTemp;
      collTextAreas(i).parentNode.insertBefore(oNewDiv, collTextAreas(i));
      collTextAreas(i).style.display = "none";
      oNewDiv = null;
    }
  }
}

function after_printing()
{
//  if (typeof(report_buttons)!="undefined") {
//    if (report_generated==true)
//      report_buttons.style.visibility='visible';
//  }

  // parse the DOM, switching OFF printer-friendly versions of tabs, switching ON old tabs and images
  if (document.allelements) {
    for (var i=0; i<document.allelements.length; i++) {
      var e = document.allelements[i];
      if (e.getAttribute('sciprint')) {
        if (e.getAttribute('sciprint')=='yes' || e.getAttribute('sciprint')=='no') {
          e.style.display = e.getAttribute('sciprint_saved');
        }
      }
      if (e.style.getAttribute && e.style.getAttribute('screen-font-size')) {
        e.style.fontSize=e.style.getAttribute('screen-font-size');
      }
      // - firefox/safari refuse to add properties on the fly, so I've disabled this for now
      //   but I'm leaving it in case we come up with a way to do this in the future
      // else if (e.style.getPropertyValue && e.style.getPropertyValue('screen-font-size')) {
      //   e.style.fontSize=e.style.getPropertyValue('screen-font-size');
      // }
    }
  }

  // fix textarea bug in IE
  if (document.all) {
    var collTextAreas = document.all.tags("TEXTAREA");
    var oDivToRemove;
    for (var i = 0; i < collTextAreas.length; i++) {
      oDivToRemove = document.all(collTextAreas(i).uniqueID + "_div");
      if (oDivToRemove != null) {
        oDivToRemove.removeNode(true);
      }
      collTextAreas(i).style.display = "";
    }
  }
}

function browserDisableEnterKey(evt)
{
  evt = (evt) ? evt : ((window.event) ? window.event : "")
  if (evt) {
    // process event here
    // alert(evt.keyCode); // IE and Safari
    // alert(evt.which); // FF
    return !(evt.keyCode==13 || evt.which==13);
  }
}

// initialize Safari/KDE
if (navigator.vendor == "Apple Computer, Inc." || navigator.vendor == "KDE") { // WebCore/KHTML
  function Document() {}
  function Event() {}
  function HTMLCollection() {}
  function HTMLElement() {}
  function Node() {}
  Document.prototype       = window["[[DOMDocument]]"];
  Event.prototype          = window["[[DOMEvent]]"];
  HTMLCollection.prototype = window["[[HTMLCollection.prototype]]"];
  HTMLElement.prototype    = window["[[DOMElement.prototype]]"];
  Node.prototype           = window["[[DOMNode.prototype]]"];
  // not sure if this works or not...
  var HTMLDocument = HTMLElement;
}

if (document.all) {
  // document.all surrogate
  document.allelements = document.all;

  // window.print surrogate
  window.onbeforeprint=before_printing;
  window.onafterprint=after_printing;
} else {
  if (HTMLDocument && HTMLDocument.prototype) {
    // document.all surrogate
    var allGetter = function () {
      var a = this.getElementsByTagName("*");
      var node = this;
      a.tags = function (sTagName) {
        return node.getElementsByTagName(sTagName);
      };
      return a;
    };
    HTMLDocument.prototype.__defineGetter__("allelements", allGetter);
  } else {
    document.allelements=document.getElementsByTagName("*");
  }

  // window.print surrogate
  window.old_print=window.print;
  var mozilla_print = function () {
    before_printing();
    window.old_print();
    // need to wait a second
    setTimeout("after_printing()",1000);
  };
  window.print=mozilla_print;
}
