var pw_xoffset=16;
var req=null;
var console=null;
var anchname="";
var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;
var _px = document.layers ? "" : "px";

function lookupGlossary(url,anch,params,httpMethod) {
	if (popupExists(anch)) { remPopup(anch);
	} else {
		if (params) { params += "&"; }
		params += "anch="+ anch;
		coord=getAnchorPosition(anch);
		openRequest(url,params,httpMethod,anch,coord.x,coord.y,240,320,true,"popbrd","<p class=\"popttl\">&nbsp;"+loading+"...</p><p>&nbsp;</p>");
	}
}

function openRequest(url,params,httpMethod,id,x,y,w,h,creat,cls,ldg) {
	if (!httpMethod) { httpMethod="POST"; }
	anchname = id;
	req = initXMLHTTPRequest();
	if (req) {
		if ((creat) && (!popupExists(anchname))) { createPopup(anchname,ldg,x,y,w,h,cls); }
		req.onreadystatechange=onReadyState;
		req.open(httpMethod,url,true);
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		req.send(params);
	}
}

function initXMLHTTPRequest() {
	var xRequest=null;
	if (window.XMLHttpRequest) {
		xRequest = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xRequest = new ActiveXObject ("Microsoft.XMLHTTP");
	} 
	return xRequest;
}

function onReadyState() {
	var ready=req.readyState;
	var data=null;
	var coord=null;
	if (ready==READY_STATE_COMPLETE) {
	        var el=getElement(anchname);
		if (el) {el.innerHTML=req.responseText;}
	} 
}

function createPopup(name,text,x,y,w,h,cls){
        if (!cls) { cls = "popbrd"; }
	this.bdy=document.createElement("div");
	this.bdy.className=cls;
	this.bdy.id=name;
	this.bdy.name=name;
	this.bdy.style.zIndex=900;
	this.bdy.style.position = "absolute";
	this.bdy.style.visibility = "visible";
	var xo = x-(w/2)+30; if (x<0) {x=0;}
	this.bdy.style.top=(y+pw_xoffset)+_px; bdy.style.left=(xo)+_px; bdy.style.width=w+_px;
	this.bdy.innerHTML=text;
	document.body.appendChild(this.bdy);
	return this.bdy;
}

// getAnchorPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the page.
function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (use_gebi && document.all) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
	}
	else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
	}
 	else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
	}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
		}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
		}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
	}
	else {
		x=0; y=0;
	}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// getAnchorWindowPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the window
function getAnchorWindowPosition(anchorname) {
	var coordinates=getAnchorPosition(anchorname);
	var x=0;
	var y=0;
	if (document.getElementById) {
		if (isNaN(window.screenX)) {
			x=coordinates.x-document.body.scrollLeft+window.screenLeft;
			y=coordinates.y-document.body.scrollTop+window.screenTop;
			}
		else {
			x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
			y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
			}
		}
	else if (document.all) {
		x=coordinates.x-document.body.scrollLeft+window.screenLeft;
		y=coordinates.y-document.body.scrollTop+window.screenTop;
		}
	else if (document.layers) {
		x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
		y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
	}
function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
	}	
function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
	}
function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
	}

function beginDrag(etd, e) {
   var x = parseInt(etd.style.left);
   var y = parseInt(etd.style.top);
   var dx = e.clientX - x;
   var dy = e.clientY - y;
   if (document.addEventListener) {
     document.addEventListener("mousemove", moveHandler, true);
     document.addEventListener("mouseup", upHandler, true);
   }
   else if (document.attachEvent) {
     document.attachEvent("onmousemove", moveHandler);
     document.attachEvent("onmouseup",  upHandler);
   }
   else {
     var oldmhdl = document.onmousemove;
     var olduhdl = document.onmouseup;
     document.onmousemove = moveHandler;
     document.onmouseup   = upHandler;
   }
   if (e.stopPropagation) { e.stopProgagation(); } else { e.cancelBubble = true; }
   if (e.preventDefault)  { e.preventDefault(); } else { e.returnValue = true; }

  function moveHandler(e) {
     if (!e) e = window.event;
     var newx = e.clientX - dx; if (newx<0) {newx=0;}
     var newy = e.clientY - dy; if (newy<0) {newy=0;}
     etd.style.left = (newx) + _px;
     etd.style.top  = (newy) + _px;
     if (e.stopPropagation) { e.stopProgagation(); } else { e.cancelBubble = true; }
  }
  
  function upHandler(e) {
     if (!e) e = window.event;
     if (document.removeEventListener) {
       document.removeEventListener("mouseup", upHandler, true);
       document.removeEventListener("mousemove", moveHandler, true);
     }
     else if (document.detachEvent) {
       document.detachEvent("onmouseup", upHandler);
       document.detachEvent("onmousemove", moveHandler);
     }
     else {
       document.onmouseup = olduhdl;
       document.onmousemove = oldmhdl;
     }
     if (e.stopPropagation) { e.stopProgagation(); } else { e.cancelBubble = true; }
  }
} // beginDrag

var glry$num = 0;
var auto$num = 0;
var auto$numop=0;

function genGlryEntry(user,key,file,text) {
  if (!key) { return; }
  glry$num++;
  var txt = (text) ? text : key;
  var fil = (file) ? ("&file=" + escape(file)) : "";
  var gid = "glry$"+glry$num;
  document.write("<a id=\"",gid,"\" name=\"",gid,"\" class=\"wa2\" title=\"",lookup,"\" onmouseover=\"mover(this);\" onmouseout=\"mout(this);\" onclick=\"lookupGlossary(\'",glryprog,"','",gid,"','user=",escape(user),"&key=",escape(key),fil,"'); return true;\">",txt,"</a>");
  opt[oClsAl]=1;
}

function expBoxBegin(txt,lnkcls,divcls,cascade) {
  if (!cascade) { while (auto$numop>0) { expBoxEnd(); } }
  auto$num++;
  if (!txt) { txt = "?????????"; }
  if (!divcls) { divcls = "autopnl"; }
  if (!lnkcls) { lnkcls = "llnk1"; }
  document.write( "<a class=\"",lnkcls,"\" title=\"",tglbox,"\" onmouseover=\"mover(this);\" onmouseout=\"mout(this);\" onclick=\"parent.togglepnl(\'auto$",auto$num,"\'); return true;\">" );
  document.write( "<img src=\"",imgpath,"expand.gif\" alt=\"",tglbox,"\" border=\"0\" id=\"auto$",auto$num,"$mod\" />&nbsp;",txt,"</a>" );
  document.write( "<div id=\"auto$",auto$num,"\" class=\"",divcls,"\" style=\"display: none;\">");
  opt[oClsAl]=1; opt[oOpnAl]=1;
  auto$numop++;
}

function expBoxEnd() {
  if (auto$numop<1) { return; }
  document.write("</div>");
  auto$numop--;
}

function togglepnl(idVariable){
  var ea=window.document.body.getElementsByTagName("div");
  var ar=null;
  for (var n=0;(n<ea.length);n++){
     if(ea[n].id.length>0){
        if(ea[n].id==idVariable){
           var expand=(ea[n].style.display=="none");
           ea[n].style.display=(expand ? "" : "none");
           ar=window.document.getElementById(ea[n].id+'$mod');
           if(ar){ar.src=imgpath+(expand ? "collapse.gif" : "expand.gif");
           break;
         }
       }
     }
  }
}
function mover(o){o.style.backgroundColor="#ffff66"; o.style.color="#990000"; status=tglbox;}
function mout(o){o.style.backgroundColor="transparent"; o.style.color="#003399"; status='';}
function popupExists(id) {
   var ea=window.document.body.getElementsByTagName("div");
   var res=false;
   for (var n=0;(n<ea.length);n++){
      if(ea[n].id.length>0){
         if(ea[n].id==id){
            res=true;
            break;
         }
      }
   }
   return res;
}

function getElement(id) {
   var ea=window.document.body.getElementsByTagName("div");
   var res=false;
   for (var n=0;(n<ea.length);n++){
      if(ea[n].id.length>0){
         if(ea[n].id==id){
            return ea[n];
            break;
         }
      }
   }
   return null;
}

function remPopup(id) {
   var ea=window.document.body.getElementsByTagName("div");
   for (var n=0;(n<ea.length);n++){
      if(ea[n].id.length>0){
         if(ea[n].id==id){
            document.body.removeChild(ea[n]);
            break;
         }
      }
   }
}

