var _isIE, _isNS, _isDOM, _isSF,_isNS70;

var _divID = 101;

_isIE = (navigator.appName == "Microsoft Internet Explorer");
_isNS = (navigator.appName == "Mozilla" || navigator.appName == "Netscape");
_isSF = (navigator.userAgent.toLowerCase().indexOf('safari') != -1);
_isDOM = _isNS && (navigator.appVersion.indexOf("4.") == -1);
_isNS70 = _isNS && (navigator.userAgent.toLowerCase().indexOf('netscape/7.0') != -1);
/* In Safari browser _isDOM is also set as most DOM functions from this file
    work on safari 1.2. please add the _isSF conditions before the _isDOM elsif 
    if future overrides are required
     */
var allObjs = new Array();

function findParentControl(e) {
  var obj = findParentNodeWithProperty(e,'parentControl');
  if( obj != null )
  {
    return obj['parentControl'];
  }
  return null;
}

// This method is untested and may not work.
function parentOfType(e,tagName) {
    var src = ( e.target ) ? e.target : event.srcElement;

	if ( src == null ) {
		return; // this won't work, abort
	}

	while( src != null && ! src.tagName == tagName)
	{
		src = src.parentNode;
	}

	if (src == null) return null;
	return src;
}

function findParentNodeWithProperty(e,propName) {
    var src = ( e.target ) ? e.target : event.srcElement;

	if ( src == null ) {
		return; // this won't work, abort
	}

	while( src != null )
	{
		if ( src.nodeType == 1 && getTagAttribute(src, propName) != null) {
			return src;
		}
		src = src.parentNode;
	}

	return null;
}

function nextDocObjID() {
  return "_auto" + _divID++;
}

function dom_object(obj) {
	this.docObj = obj;
	this.name = obj.id;
	this.scrollTop=obj.scrollTop;
	this.hide = domHide;
	this.show = domShow;
	this.isShown = domIsShow;
	this.getHeight = domGetHeight;
	this.getWidth = domGetWidth;
	this.setTop = domSetTop;
	this.setLeft = domSetLeft;
	this.setInnerHTML = domSetInnerHTML;
	this.unDisplay = domUnDisplay;
	this.display = domDisplay;
    this.moveTo = commonMoveTo;
    this.setClass = commonSetClass;
    this.setTagAttribute=commonSetAttribute;
    this.getTagAttribute=commonGetAttribute;
}

function sf_object(obj) {
	this.docObj = obj;
	this.name = obj.id;
	this.scrollTop=obj.scrollTop;
	this.hide = domHide;
	this.show = domShow;
	this.isShown = domIsShow;
	this.getHeight = ieGetHeight;
	this.getWidth = ieGetWidth;
	this.setTop = domSetTop;
	this.setLeft = domSetLeft;
	this.setInnerHTML = domSetInnerHTML;
	this.unDisplay = domUnDisplay;
	this.display = domDisplay;
    this.moveTo = commonMoveTo;
    this.setClass = commonSetClass;
    this.setTagAttribute=commonSetAttribute;
    this.getTagAttribute=commonGetAttribute;
}


function ie_object(obj) {
	this.docObj = obj;
	this.name = obj.id;
	this.scrollTop=obj.scrollTop;
	this.hide = domHide;
	this.show = domShow;
	this.isShown = domIsShow;
	this.getHeight = ieGetHeight;
	this.getWidth = ieGetWidth;
	this.setTop = domSetTop;
	this.setLeft = domSetLeft;
	this.setInnerHTML = domSetInnerHTML;
	this.unDisplay = domUnDisplay;
	this.display = domDisplay;
        this.moveTo = commonMoveTo;
        this.setClass = commonSetClass;
    this.setTagAttribute=commonSetAttribute;
    this.getTagAttribute=commonGetAttribute;
}
/*
function ns_object(obj) {
	this.docObj = obj;
	this.name = obj.name;
	this.hide = nsHide;
	this.show = nsShow;
	this.setInnerHTML = nsSetInnerHTML;
	this.getHeight = nsGetHeight;
	this.setTop = nsSetTop;
	this.setLeft = nsSetLeft;
	this.unDisplay = nsHide;
	this.display = nsShow;
        this.moveTo = commonMoveTo;
        this.setClass = commonSetClass;
}
*/
//This function places the div dynamically

function ie_image(obj) {
	this.imgObj = obj;
	this.getTop = ieGetImgTop;
	this.getLeft = ieGetImgLeft;
	this.getHeight = ieGetImgHeight;
	this.getWidth = ieGetImgWidth;
}

function ns_image(obj) {
	this.imgObj = obj;
	this.getTop = nsGetImgTop;
	this.getLeft = nsGetImgLeft;
	this.getHeight = nsGetImgHeight;
	this.getWidth = nsGetImgWidth;
}

function ieGetImgTop() { return parseInt(this.imgObj.offsetTop); }
function ieGetImgLeft() { return parseInt(this.imgObj.offsetLeft); }
function ieGetImgHeight() { return parseInt(this.imgObj.offsetHeight); }
function ieGetImgWidth() { return parseInt(this.imgObj.offsetWidth); }
function ieGetHeight() { return parseInt(this.docObj.clientHeight); }
function ieGetWidth() { return parseInt(this.docObj.clientWidth); }
function domHide() { this.docObj.style.visibility = "hidden"; }
function domShow() { this.docObj.style.visibility = "visible"; }
function domIsShow() { return (this.docObj.style.visibility == "visible"); }
function domSetInnerHTML(s) { this.docObj.innerHTML = s; }
function domGetHeight() { return parseInt(document.defaultView.getComputedStyle(this.docObj, "").getPropertyValue("height")); }
function domGetWidth() { return parseInt(document.defaultView.getComputedStyle(this.docObj, "").getPropertyValue("width")); }
//function domSetHeight(y) { this.docObj.style.height = y + "px"; }
//function domSetWidth(y) { this.docObj.style.width = x + "px"; }
function domUnDisplay() { this.docObj.style.display = 'none'; }
function domDisplay() { this.docObj.style.display = ''; }
function domSetLeft(x) { this.docObj.style.left = x + "px"; }
function domSetTop (y) { this.docObj.style.top = y + "px"; }

function nsGetImgTop() { return parseInt(this.imgObj.y); }
function nsGetImgLeft() { return parseInt(this.imgObj.x); }
function nsGetImgHeight() { return parseInt(this.imgObj.height); }
function nsGetImgWidth() { return parseInt(this.imgObj.width); }
function nsHide() { this.docObj.visibility = "hidden"; }
function nsShow() { this.docObj.visibility = "inherit"; }
function nsGetHeight() { return this.docObj.document.height; }
function nsSetHeight(y) { this.docObj.clip.height = y; }
function nsSetWidth(x) { this.docObj.clip.width = x; }
function nsSetTop(y) { this.docObj.top = y; }
function nsSetLeft(x) { this.docObj.left = x; }
function nsUnDisplay() { this.docObj.clip.left = 0; this.docObj.clip.right = 0; this.docObj.clip.bottom = 0; this.docObj.clip.top = 0; }

function commonMoveTo(x,y) { this.setLeft(x); this.setTop(y); }
function commonSetClass(name) { this.docObj.className = name; }
function commonSetAttribute( attribute , value) {this.docObj.setAttribute(attribute , value); }
function commonGetAttribute( attribute ) {return this.docObj.getAttribute(attribute); }
function nsSetInnerHTML(s) {
	this.docObj.document.clear();
	this.docObj.document.write(s);
	this.docObj.document.close();
}

function findNSLayer(objName) {
   for (i = 0; i < document.layers.length; i++) {
     if (document.layers[i].name == objName) return document.layers[i];
   }
}

// Note: images not in the same DIV/doc are not
// currently supported under Netscape
function getImgObj(objName) {
  if (allObjs[objName]) return allObjs[objName];
  var theObj = null;
  if (_isIE || _isDOM) {
    if (!document.images[objName]) {
      return null;
    }
    theObj = new ie_image(document.images[objName]);
  } else if (_isNS) {
    if (!document.images[objName]) {
      // If required, add sub-doc search here
    }
    theObj = new ns_image(document.images[objName]);
  }
  allObjs[objName] = theObj;
  return theObj;
}

function getDocObj(objName) {
  if (allObjs[objName]) return allObjs[objName];
  var theObj = null;
  if (_isIE) {
    if (!document.all[objName]) {
      return null;
    }
    theObj = new ie_object(document.all[objName]);
  } else if(_isSF) {
    if (!document.getElementById(objName)) {
      return null;
    }
    theObj = new sf_object(document.getElementById(objName));    
  }
  else if (_isDOM) {
    if (!document.getElementById(objName)) {
      return null;
    }
    theObj = new dom_object(document.getElementById(objName));
  } else if (_isNS) {
    // unsupported browser.
    return null;
//   var nsLayer = document.layers[objName];
//   if (!nsLayer) {
//      nsLayer = findNSLayer(objName);
//      if (!nsLayer) {
//        return null;
//      }
//    }
//    theObj = new ns_object(nsLayer);
  }
  allObjs[objName] = theObj;
  return theObj;
}

function objFromNode(docObj) {
  var theObj = null;
  if (_isIE) {
    theObj = new ie_object(docObj);
  } else if (_isDOM) {
    theObj = new dom_object(docObj);
  } else if (_isNS) {
//  unsupported browser
//    theObj = new ns_object(docObj);
  }
  return theObj;
}

// checks if an object with the specified id exists
function doesObjExist(objName) {
  if (allObjs[objName]) return true;
  if (_isIE) {
    if (document.all[objName]) {
      return true;
    }
  } else if (_isDOM) {
    if (document.getElementById(objName)) {
      return true;
    }
  } else if (_isNS) {
// unsupported browser
//    var nsLayer = document.layers[objName];
//    if (nsLayer) {
//      return true;
//    }
  }
  return false;
}

function newDocObj(name, left, top, width, height, html) {
//  var debugBorders = ";border:1px solid red";
  var debugBorders = "";
  var heightString = "";
  if (height) {
    heightString = "height:" + height + "px;";
  }
  if (name == null) {
    name = nextDocObjID();
  }
  var nL;

	if (_isIE)
	{
		var divhtml = '<div id="' + name + '" style="visibility:hidden;left:' + left +
			'px;top:' + top + 'px;width:' + width +
			'px;' + heightString + 'position:absolute;'+debugBorders+'">' +
			html + '</div>';
		document.body.insertAdjacentHTML('beforeEnd', divhtml);
		nL = document.all[name].style;
	}
	else if (_isDOM)
	{
		var nL = document.createElement("DIV");
		nL.innerHTML = html;
		var mybody=document.body;
		mybody.appendChild(nL);
		nL.style.position = "absolute";
		nL.style.visibility = "hidden";
		nL.style.left = left;
		nL.style.top = top;
		nL.style.width = width;
                if (height) {
		  nL.style.height = height;
                }
        nL.id = name;
		nL = nL.style;
	}
	else if (_isNS)
	{
            return null;
// unsupported browser
/*		nL=new Layer(width);
		nL.name = name;
		nL.left=left;
		nL.top=top;
		nL.clip.width=width;
                if (height) {
  		  nL.clip.height=height;
                }
                if (zindex) {
                  nL.zIndex = zIndex;
                }
		nL.document.open();
		nL.document.write(html);
		nL.document.close();
*/
	}
    return getDocObj(name);
}

function getTagAttribute( obj, attr )
{
    var ret = null;
    if( obj != null && obj.nodeType == 1 )
    {
        ret = obj[attr];
        if( !ret )
        {
            ret = obj.getAttribute( attr );
            if( ret == "" )
            {
                ret = null;
            }
        }
    }
    return ret;
}

function getPageOffset() {
  var offset = 0;
  if (_isNS || _isDOM) {
    offset = window.pageYOffset;
  } else {
    offset = self.document.body.scrollTop;
  }
  if (offset > 0) {
    return offset;
  }
  return 0;
}

function getPageXOffset() {
  var offset = 0;
  if (_isNS || _isDOM) {
    offset = window.pageXOffset;
  } else {
    offset = self.document.body.scrollLeft;
  }
  if (offset > 0) {
    return offset;
  }
  return 0;
}

function scrollRestore(value) {
  window.scrollTo(0,value);
}

function getObjectScrollTop(obj) {
  return(getDocObj(obj).docObj.scrollTop);
  
}
function objectScrollRestore(obj,value) {
    getDocObj(obj).docObj.scrollTop = value;
}

// swapClass finds an ancestor with a CSS classname of oldClass and replaces with newClass.
function swapClass(e,oldClass,newClass) {
  var src = ( e.target ) ? e.target : event.srcElement;

  while( src != null && src.className != oldClass)
  {
	src = src.parentNode;
  }

  if (src != null) {
    src.className=newClass;
  }
}

