// much of the code that follows is modified from
// Pop-it menu- By Dynamic Drive
// For full source code and more DHTML scripts, visit http://www.dynamicdrive.com
// This credit MUST stay intact for use
  
  // we build the menu anew each time the show request is processed
  function cbo_showmenu(e, txtMenu, target){
	var strWidth = 0;
	
    if (!document.all&&!document.getElementById&&!document.layers)
	return

	cbo_clearhidemenu()

	menuobj=browser.ie4or5? document.all.popmenu : browser.ns6? document.getElementById("popmenu") : browser.ns4or5? document.popmenu : ""
	menuobj.thestyle=(browser.ie4or5||browser.ns6)? menuobj.style : menuobj

	
	if (browser.ie4or5||browser.ns6){
		menuobj.thestyle.height = ""
		menuobj.thestyle.overflow = "visible"
		menuobj.innerHTML=txtMenu
	}else{
		menuobj.document.write('<layer name=gui bgColor=#E6E6E6 width=165 onmouseover="cbo_clearhidemenu()" onmouseout="cbo_hidemenu()">'+ txtMenu +'</layer>')
		menuobj.document.close()
	}
	menuobj.contentwidth=(browser.ie4or5||browser.ns6)? menuobj.offsetWidth : menuobj.document.gui.document.width
	menuobj.contentheight=(browser.ie4or5||browser.ns6)? menuobj.offsetHeight  : menuobj.document.gui.document.height
	eventX=browser.ie4or5? event.clientX : browser.ns6? e.clientX : e.x
	eventY=browser.ie4or5? event.clientY : browser.ns6? e.clientY : e.y
	
	//align with left edge of parent table cell/textbox
	if ( browser.ie4or5){
		var obj = event.srcElement;
		eventX = 0;
  		while (obj.offsetParent != null) {
  			  if(obj.id == "combotable") {
  				eventX = 0;
  				strWidth = obj.offsetWidth;
  			  }
  			  eventX += obj.offsetLeft;
    		  obj = obj.offsetParent;
  		}
  		eventX += obj.offsetLeft - document.body.scrollLeft;
	
		obj = event.srcElement;
	    eventY = 0;
		while (obj.offsetParent != null) {
			  if(obj.id == "combotable") eventY=event.srcElement.height + 6;	
    		  eventY += obj.offsetTop;
    		  obj = obj.offsetParent;
  		}
  		eventY += obj.offsetTop - document.body.scrollTop;
	}
	else {
		eventX -= 100
		eventY += 20
	}

	//Find out how close the mouse is to the corner of the window
	var rightedge=browser.ie4or5? document.body.clientWidth-eventX : window.innerWidth-eventX
	var bottomedge=browser.ie4or5? document.body.clientHeight-eventY : window.innerHeight-eventY
	var topedge=browser.ie4or5? eventY : eventY

	//if the horizontal distance isn't enough to accomodate the width of the context menu
	if (rightedge<menuobj.contentwidth)
		//move the horizontal position of the menu to the left by it's width
		menuobj.thestyle.left=browser.ie4or5? document.body.scrollLeft+eventX-menuobj.contentwidth : browser.ns6? window.pageXOffset+eventX-menuobj.contentwidth : eventX-menuobj.contentwidth
	else
		//position the horizontal position of the menu where the mouse was clicked
		menuobj.thestyle.left=browser.ie4or5? document.body.scrollLeft+eventX : browser.ns6? window.pageXOffset+eventX : eventX
	
	//same concept with the vertical position
	if (bottomedge>menuobj.contentheight){
		menuobj.thestyle.height = "";
			menuobj.thestyle.top=browser.ie4or5? document.body.scrollTop+eventY : browser.ns6? window.pageYOffset+eventY : eventY;
	}
	else {
		if  (topedge>menuobj.contentheight){
			menuobj.thestyle.top=browser.ie4or5? document.body.scrollTop+eventY-menuobj.contentheight-event.srcElement.height -6 : browser.ns6? window.pageYOffset+eventY-menuobj.contentheight : eventY-menuobj.contentheight
		} else {
			menuobj.thestyle.height = eventY - 20;
			menuobj.thestyle.overflow = "auto"
			menuobj.thestyle.top=browser.ie4or5? document.body.scrollTop  - event.srcElement.height -6 +20 : browser.ns6? window.pageYOffset+eventY-menuobj.contentheight : eventY-menuobj.contentheight
		}
	}
	// fix the width
	if (browser.ie5 || browser.ie4){
		if (menuobj.contentwidth < strWidth + event.srcElement.width){
			menuobj.thestyle.width = strWidth 
			menuobj.all.combotable.width = strWidth -20
		}
	}
	
	menuobj.thestyle.visibility="visible"
	targetField = target
	return false;
}

function contains_ns6(a, b) {
	//Determines if 1 element in contained in another- by Brainjar.com
	while (b.parentNode)
	if ((b = b.parentNode) == a)
	return true;
	return false;
}

function cbo_hidemenu(){
	if(fDontHide){	
		fDontHide= false
		return
	}
	if (window.menuobj)
	menuobj.thestyle.visibility=(browser.ie4or5||browser.ns6)? "hidden" : "hide"
}

function cbo_dynamichide(e){
//	if (browser.ie4or5&&!menuobj.contains(e.toElement))
//		cbo_hidemenu()
//	else if (browser.ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
//		cbo_hidemenu()
}

function cbo_clearhidemenu(){
	if (window.delayhide)
		clearTimeout(delayhide)
}

function cbo_highlightmenu(e,state){
	if (document.all)
		source_el=event.srcElement
	else if (document.getElementById)
		source_el=e.target
		if (source_el.className=="comboitems"){
			source_el.id=(state=="on")? "mouseoverstyle" : ""
		}
		else{
			while(source_el.id!="popmenu"){
			source_el=document.getElementById? source_el.parentNode : source_el.parentElement
			if (source_el.className=="comboitems"){
				source_el.id=(state=="on")? "mouseoverstyle" : ""
			}
		}
	}
}

// this is the div into which the list is written
// it is the object that is shown and hidden by the logic above
function cbo_createpopup(){
	document.write('<div id="popmenu" class="comboitemskin" onMouseover="cbo_clearhidemenu();cbo_highlightmenu(event,\'on\')" onMouseout="cbo_highlightmenu(event,\'off\');cbo_dynamichide(event)"></div>')
}


// the user clicked on an item - copy the value and text back to the form
function cbo_setValue(strValue, strText){
	var fldTarget = eval('targetField.form.v_' + targetField.name )
	var s =new String();
	s=unescape(strText);
	s=s.replace("+"," ");
	s=s.replace("+"," ");
	s=s.replace("+"," ");  
	targetField.value = unescape(strValue);
	fldTarget.value = s;
	checkField(fldTarget);
	targetField = null;
	cbo_hidemenu()
}

// this multidimensinoal array saves the content of all of the lists for this page
var cbo_Items = new Array()

// an object to hold the term(value and text) - will be put in the cbo_Items array
function cbo_term(strDisplay, strValue){
	this.text = strDisplay
	this.value = strValue	
}

// the target field object is put here so we can remember where to put the data back to
var targetField;
// skip the onclick event if we are trying to show the menu
var fDontHide = false
if (browser.ie4or5||browser.ns6) document.onclick=cbo_hidemenu
