/* ***** BEGIN LICENSE BLOCK *****
 * Licensed under Version: MPL 1.1/GPL 2.0/LGPL 2.1
 * Full Terms at /lib/js/license/mpl-tri-license.txt
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Netscape code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2001
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s): Bob Clary <bclary@netscape.com>
 *
 * ***** END LICENSE BLOCK ***** */

// default xbLibrary
//
// it is an error to reference anything from the dynamically loaded file inside the
// same script block.  This means that a file can not check its dependencies and
// load the files for it's own use.  someone else must do this.  

xblibrary = new xbLibrary('../js/');

function xg(idstr) {
	return xbGetElementById(idstr);
}

xbLibrary.prototype.loadScript = 
function (scriptName)
{
  document.write('<script language="javascript" src="' + this.path + '/' + scriptName + '"><\/script>');
};

function xbLibrary(path)
{
  if (path.charAt(path.length-1) == '/')
  {
    path = path.substr(0, path.length-1)
  }
  this.path = path;
}
 

function xbToInt(s)
{
  var i = parseInt(s, 10);
  if (isNaN(i))
    i = 0;

  return i;
}


/////////////////////////////////////////////////////////////
// xbStyle.getEffectiveValue()
// note that xbStyle's constructor uses the currentStyle object 
// for IE5+ and that Opera's style object contains computed values
// already. Netscape Navigator's layer object also contains the 
// computed values as well. Note that IE4 will not return the 
// computed values.

function xbStyleGetEffectiveValue(propname)
{
  var value = null;

  if (this.window.document.defaultView && this.window.document.defaultView.getComputedStyle)
  {
    // W3
    // Note that propname is the name of the property in the CSS Style
    // Object. However the W3 method getPropertyValue takes the actual
    // property name from the CSS Style rule, i.e., propname is 
    // 'backgroundColor' but getPropertyValue expects 'background-color'.

     var capIndex;
     var cappropname = propname;

     while ( (capIndex = cappropname.search(/[A-Z]/)) != -1)
     {
       if (capIndex != -1)
       {
         cappropname = cappropname.substring(0, capIndex) + '-' + cappropname.substring(capIndex, capIndex+1).toLowerCase() + cappropname.substr(capIndex+1);
       }
     }

     value = this.window.document.defaultView.getComputedStyle(this.object, '').getPropertyValue(cappropname);

     // xxxHack for Gecko:
     if (!value && this.styleObj[propname])
     {
       value = this.styleObj[propname];
     }
  }
  else if (typeof(this.styleObj[propname]) == 'undefined') 
  {
    value = xbStyleNotSupportStringValue(propname);
  }
  else if (typeof(this.object.currentStyle) != 'undefined')
  {
    // IE5+
    value = this.object.currentStyle[propname];
    if (!value)
    {
      value = this.styleObj[propname];
    }

    if (propname == 'clip' && !value)
    {
      // clip is not stored in IE5/6 handle separately
      value = 'rect(' + this.object.currentStyle.clipTop + ', ' + this.object.currentStyle.clipRight + ', ' + this.object.currentStyle.clipBottom + ', ' + this.object.currentStyle.clipLeft + ')';
    }
  }
  else
  {
    // IE4+, Opera, NN4
    value = this.styleObj[propname];
  }

  return value;
}


function xbStyleNotSupportStringValue(propname) { xbDEBUG.dump(propname + ' is not supported in this browser'); return '';};




/////////////////////////////////////////////////////////////
// xbStyle
//
// Note Opera violates the standard by cascading the effective values
// into the HTMLElement.style object. We can use IE's HTMLElement.currentStyle
// to get the effective values. In Gecko we will use the W3 DOM Style Standard getComputedStyle

function xbStyle(obj, win, position)
{
  if (typeof(obj) == 'object' && typeof(obj.style) != 'undefined') 
    this.styleObj = obj.style;
  else if (document.layers) // NN4
  {
    if (typeof(position) == 'undefined')
      position = '';
        
    this.styleObj = obj;
    this.styleObj.position = position;
  }
  this.object = obj;
  this.window = win ? win : window;
}

xbStyle.prototype.styleObj = null;
xbStyle.prototype.object = null;
xbStyle.prototype.getEffectiveValue     = xbStyleGetEffectiveValue; 


if (document.all || document.getElementsByName)
{
  xblibrary.loadScript('xbStyle-css2.js');
}
else if (document.layers)
{
  xblibrary.loadScript('xbStyle-nn42.js');
}
else 
{
  xblibrary.loadScript('xbStyle-not-supported2.js');
}


if (!document.getElementById || navigator.userAgent.indexOf('Opera') != -1)
{
  // assign error handler for downlevel browsers
  // Note until Opera improves it's overall support
  // for JavaScript and the DOM, it must be considered downlevel

  window.onerror = defaultOnError;
  
  function defaultOnError(msg, url, line)
  {
    // handle bug in NS6.1, N6.2
    // where an Event is passed to error handlers
    if (typeof(msg) != 'string')
    {
        msg = 'unknown error';
    }
    if (typeof(url) != 'string')
    {
        url = document.location;
    }

    // customize this for your site
    if (top.location.href.indexOf(xblibrary.path + '/errors/') == -1)
      top.location = xblibrary.path + '/errors/index.html?msg=' + escape(msg) + '&url=' + escape(url) + '&line=' + escape(line);
  }
}





// xbDom


function xbToInt(s)
{
  var i = parseInt(s, 10);
  if (isNaN(i))
    i = 0;

  return i;
}

function xbGetWindowWidth(windowRef)
{
  var width = 0;

  if (!windowRef)
  {
    windowRef = window;
  }
  
  if (typeof(windowRef.innerWidth) == 'number')
  {
    width = windowRef.innerWidth;
  }
  else if (windowRef.document.body && typeof(windowRef.document.body.clientWidth) == 'number')
  {
    width = windowRef.document.body.clientWidth;  
  }
    
  return width;
}

function xbGetWindowHeight(windowRef)
{
  var height = 0;
  
  if (!windowRef)
  {
    windowRef = window;
  }

  if (typeof(windowRef.innerWidth) == 'number')
  {
    height = windowRef.innerHeight;
  }
  else if (windowRef.document.body && typeof(windowRef.document.body.clientWidth) == 'number')
  {
    height = windowRef.document.body.clientHeight;    
  }
  return height;
}

function xbGetElementsByNameAndType(name, type, windowRef)
{
  if (!windowRef)
    windowRef = window;

  var elmlist = new Array();

  xbFindElementsByNameAndType(windowRef.document, name, type, elmlist);

  return elmlist;
}

function xbFindElementsByNameAndType(doc, name, type, elmlist)
{
  var i;
  var subdoc;
  
  for (i = 0; i < doc[type].length; ++i)
  {
    if (doc[type][i].name && name == doc[type][i].name)
    {
      elmlist[elmlist.length] = doc[type][i];
    }
  }

  if (doc.layers)
  {
    for (i = 0; i < doc.layers.length; ++i)
    {
      subdoc = doc.layers[i].document;
      xbFindElementsByNameAndType(subdoc, name, type, elmlist);
    }
  }
}

if (document.getElementById)
{
  xbGetElementById = 
  function (id, windowRef) 
  { 
    if (!windowRef) 
    {
      windowRef = window; 
    }
    return windowRef.document.getElementById(id); 
  };

  xbGetElementsByName = 
  function (name, windowRef) 
  { 
    if (!windowRef) 
    {
      windowRef = window; 
    }
    return windowRef.document.getElementsByName(name); 
  };
}
else if (document.all)
{
  xbGetElementById = 
  function (id, windowRef) 
  { 
    if (!windowRef) 
    {
      windowRef = window; 
    }
    var elm = windowRef.document.all[id]; 
    if (!elm) 
    {
      elm = null; 
    }
    return elm; 
  };

  xbGetElementsByName = function (name, windowRef)
  {
    if (!windowRef)
      windowRef = window;

    var i;
    var idnamelist = windowRef.document.all[name];
    var elmlist = new Array();

    if (!idnamelist.length || idnamelist.name == name)
    {
      if (idnamelist)
        elmlist[elmlist.length] = idnamelist;
    }
    else
    {
      for (i = 0; i < idnamelist.length; i++)
      {
        if (idnamelist[i].name == name)
          elmlist[elmlist.length] = idnamelist[i];
      }
    }

    return elmlist;
  }

} else if (document.layers)
{
  nav4FindLayer =
  function (doc, id)   {
    var i;
    var subdoc;
    var obj;
    
    for (i = 0; i < doc.layers.length; ++i)     {
      if (doc.layers[i].id && id == doc.layers[i].id)
        return doc.layers[i];
        
      subdoc = doc.layers[i].document;
      obj    = nav4FindLayer(subdoc, id);
      if (obj != null)
        return obj;
    }
    return null;
  }

  nav4FindElementsByName = 
  function (doc, name, elmlist)   {
    var i;
    var j;
    var subdoc;
    
    for (i = 0; i < doc.images.length; ++i)     {
      if (doc.images[i].name && name == doc.images[i].name)       {
        elmlist[elmlist.length] = doc.images[i];
      }
    }

    for (i = 0; i < doc.forms.length; ++i)     {
      for (j = 0; j < doc.forms[i].elements.length; j++)       {
        if (doc.forms[i].elements[j].name && name == doc.forms[i].elements[j].name)         {
          elmlist[elmlist.length] = doc.forms[i].elements[j];
        }
      }

      if (doc.forms[i].name && name == doc.forms[i].name) {
        elmlist[elmlist.length] = doc.forms[i];
      }
    }

    for (i = 0; i < doc.anchors.length; ++i) {
      if (doc.anchors[i].name && name == doc.anchors[i].name)
      {
        elmlist[elmlist.length] = doc.anchors[i];
      }
    }

    for (i = 0; i < doc.links.length; ++i)
    {
      if (doc.links[i].name && name == doc.links[i].name)
      {
        elmlist[elmlist.length] = doc.links[i];
      }
    }

    for (i = 0; i < doc.applets.length; ++i)
    {
      if (doc.applets[i].name && name == doc.applets[i].name)
      {
        elmlist[elmlist.length] = doc.applets[i];
      }
    }

    for (i = 0; i < doc.embeds.length; ++i)
    {
      if (doc.embeds[i].name && name == doc.embeds[i].name)
      {
        elmlist[elmlist.length] = doc.embeds[i];
      }
    }

    for (i = 0; i < doc.layers.length; ++i)
    {
      if (doc.layers[i].name && name == doc.layers[i].name)
      {
        elmlist[elmlist.length] = doc.layers[i];
      }
        
      subdoc = doc.layers[i].document;
      nav4FindElementsByName(subdoc, name, elmlist);
    }
  }

  xbGetElementById = function (id, windowRef)
  {
    if (!windowRef)
      windowRef = window;

    return nav4FindLayer(windowRef.document, id);
  };

  xbGetElementsByName = function (name, windowRef)
  {
    if (!windowRef)
      windowRef = window;

    var elmlist = new Array();

    nav4FindElementsByName(windowRef.document, name, elmlist);

    return elmlist;
  };

}
else 
{
  xbGetElementById = 
  function (id, windowRef) 
  { 
    return null; 
  };

  xbGetElementsByName = 
  function (name, windowRef) 
  { 
    return new Array(); 
  };
}

function xbGetPageScrollX(windowRef)
{
  if (!windowRef) 
  {
    windowRef = window; 
  }

  if (typeof(windowRef.pageXOffset) == 'number')
  {
    return windowRef.pageXOffset;
  }

  if (typeof(windowRef.document.body && windowRef.document.body.scrollLeft) == 'number')
  {
    return windowRef.document.body.scrollLeft;
  }

  return 0;
}

function xbGetPageScrollY(windowRef)
{
  if (!windowRef) 
  {
    windowRef = window; 
  }

  if (typeof(windowRef.pageYOffset) == 'number')
  {
    return windowRef.pageYOffset;
  }

  if (typeof(windowRef.document.body && windowRef.document.body.scrollTop) == 'number')
  {
    return windowRef.document.body.scrollTop;
  }

  return 0;
}

if (document.layers)
{
  xbSetInnerHTML = 
  function (element, str) 
  { 
    element.document.write(str); 
    element.document.close(); 
  };
}
else 
{
  xbSetInnerHTML = function (element, str) 
  { 
    if (typeof(element.innerHTML) != 'undefined') 
    {
      element.innerHTML = str; 
    }
  };
}

// eof: xbDOM.js

function trim(str) {
    strlength = str.length;
    startind=0;
    endind=strlength;
    ind = -1;
    while (++ind < strlength) {
	    if (str.charAt(ind) == ' ') {
		    startind=ind+1;
	    } else break;
    }
    ind = strlength;
    while (--ind > 0) {
	    if (str.charAt(ind) == ' ') {
		    endind=ind;
	    } else break;
    }
    res=str.substr(startind, endind-startind); 
    return res;
}

//functions to manipulate the object.style.display 
function hide(obj) {
	if(obj) {
		obj.style.display="none";
	}
}

function show(obj) {
	if(obj) {
		obj.style.display="";
	}
}

function dispn(objname, boolval) {
	var o = xg(objname);
	if(o) o.style.display = boolval ? "" : "none";
}

function dispo(obj, boolval) {
	if(obj) obj.style.display = boolval ? "" : "none";
}


//for Morphologic targets
function hun_escape(word){
    word = escape(word);
    while (word.indexOf("%u0171") > -1) word = word.replace("%u0171","%FB");
    while (word.indexOf("%u0151") > -1) word = word.replace("%u0151","%F5");
    while (word.indexOf("%u0170") > -1) word = word.replace("%u0170","%DB");
    while (word.indexOf("%u0150") > -1) word = word.replace("%u0150","%D5");
    return word;
}

function encURI(x) {
	return encodeURIComponent(x);
}

//We use it after AJAX requests to load dynamically an external javascript file. First usage is the hypertree.
//External javascripts cheats IE not to use the "Click to activte and use this control" restriction for applets, flashes (<object>-s), .
function dhtmlLoadScript(url){
   var e = document.createElement("script");
   e.src = url;
   e.type="text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e); 
}

