//Javascript HTMLDropdown Module

//configuration variables
$HDR_SELECT_REPLACEMENT = 'select';
$HDR_OPTION_REPLACEMENT = 'option';

$HDR_ATTRNAME_TYPE = 'hdr_type';
$HDR_ATTRNAME_SUBTYPE = 'hdr_stype';

$HDR_MAINCLASSNAME = 'HTMLDropdown'

$HDR_DEFAULT_WIDTH = 100;
$HDR_DEFAULT_HEIGHT = 150;
$HDR_DEFAULT_LINEHEIGHT = 20;

$HDR_GLOBALOBJECT = "HTMLDropDowns";
$IMG_NAME = '';

// some utility functions

function HDR_univalLoad(e) {
	if (!e) e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	var sels = document.getElementsByTagName($HDR_SELECT_REPLACEMENT);
	if (typeof window[$HDR_GLOBALOBJECT] == "undefined") {
		window[$HDR_GLOBALOBJECT] = {
			'objects': [], 
			'addedwindowevent': 0
		}
	}
	for ( var i = 0; i < sels.length; i++) {
		var tmp = [];
		if (sels[i].getAttribute($HDR_ATTRNAME_TYPE) != null && sels[i].getAttribute($HDR_ATTRNAME_SUBTYPE) != null) {
			if (sels[i].getAttribute($HDR_ATTRNAME_TYPE).toLowerCase() =='widget' && sels[i].getAttribute($HDR_ATTRNAME_SUBTYPE).toLowerCase() == 'html') {
				tmp[tmp.length] = new HTMLDropdown(sels[i]);
			}
		}
	}
	//UNI_attachEvent(document, 'click', hndlr_htmldropdown_click, 1);
}
var clickCount = 0;
function myClick(e){
	var targetObjectCounter = false;
	var o = DOM_setEventVars(e);
	var startEl = o.targ;
	while(startEl){
		if((targetObjectCounter=startEl.getAttribute("counter"))!=null){
			break;
		}
		startEl = startEl.parentElement;
	}

	if(targetObjectCounter!=null){
		DOM_cancelEvent(e);
		if(!window[$HDR_GLOBALOBJECT]['objects'][targetObjectCounter].oldselect.disabled){
			var obj = window[$HDR_GLOBALOBJECT]['objects'][targetObjectCounter];
			if ( pageLoded() && obj.owner.toolbar.checkHelp(obj.oldselect, o.e)) {
				obj.owner.toolbar.showHelp(obj.oldselect.getAttribute("help_id"));
				return false;
			}
			obj.toggleDisplay();
		}
	}
}
function hndlr_htmldropdown_click(e){
	o = DOM_setEventVars(e);
	for (var i = 0; i < window[$HDR_GLOBALOBJECT]['objects'].length; i++) {
		var obj = window[$HDR_GLOBALOBJECT]['objects'][i];
		if (obj.open == 0)
			continue;
		else {
			//test if the event is in this object, else close it
			var p = DOM_getAbsolutePos(obj.select);
			var corner1x = p.x, corner1y = p.y;
			var corner2x = obj.select.offsetWidth + corner1x, corner2y = obj.select.offsetHeight + corner1y;
			if (corner1x <= o.posx && corner2x >= o.posx 
			 && corner1y < o.posy && corner2y > o.posy) { //event in x limits, event in y limits
				return true;	
			} else {
				//alert(sprintf('event outside, close it!!! %d.%d', o.posx, o.posy));
				obj.toggleDisplay(0);
			}
		}
	}
	return true;
}
function HTMLDropdown(el) {
	this.oldselect = el;
	var kon = this.oldselect.getAttribute("ktml_object_name");
	this.owner = window[kon];
	if(typeof this.owner.htmldropdowns=="undefined"){
		this.owner.htmldropdowns = new Array();
	}
	this.owner.htmldropdowns[this.owner.htmldropdowns.length] = this;
	var tmpel = el;
	while (tmpel.tagName.toLowerCase() != 'form' && tmpel.tagName.toLowerCase() != 'body')
		tmpel = tmpel.parentNode;
	this.form = tmpel;
	
	this.oldposition = DOM_getAbsolutePos(el);
	this.select = document.createElement('SPAN');
	this.imagesize = {
		width: 16,
		height: 15
	};

	var originalWidth = parseInt(this.oldselect.offsetWidth);
	if(isNaN(originalWidth)){
		originalWidth = $HDR_DEFAULT_WIDTH;
	}
	this.width = originalWidth-6;
	
	var originalHeight = parseInt(this.oldselect.offsetHeight);
	if(isNaN(originalHeight)){
		originalHeight = $HDR_DEFAULT_HEIGHT;
	}
	this.height = 14;//originalHeight;

	this.lineheight = parseInt(DOM_getStyleProperty(this.oldselect, 'lineHeight')) || $HDR_DEFAULT_LINEHEIGHT;

	this.selectedIndex = -1;
	this.highlightedIndex = -1;
	this.mustResize = true;
	this.initialize();
	if (this.select.offsetWidth == 0) {
		this.rendered = false;
	} else {
		this.rendered = true;
	}
	this.render();

	//divTag.addEventListener("mouseover",changeMe,false);
	window[$HDR_GLOBALOBJECT]['objects'].push(this);
}

function hndlr_htmldropdown_key(e){
	var o = DOM_setEventVars(e);
	//detect tab, down, up, esc, cr
	//alert(sprintf('keyCode: %d, character: %s, altKey: %d, ctrlKey: %d, shiftKey: %d', o.e.keyCode, String.fromCharCode(o.e.keyCode), o.e.altKey, o.e.ctrlKey, o.e.shiftKey));
	var targetObjectCounter = null;
	if(Ktml_ie){
		targetObjectCounter = o.targ.firstChild.getAttribute("counter");
	}else{
		targetObjectCounter = o.targ.obj.counter;
	}
	var obj = window[$HDR_GLOBALOBJECT]['objects'][targetObjectCounter];
	//i could have reached here from two places "select" or "div", or other tag so find the object
//	window.status = "pressed : "+ o.e.keyCode;//alert(DOM_getStyleProperty(obj.optcontainer, 'display'));

	//treat each key differently	
	//TAB
	if (o.e.keyCode == 9) {
		//DOM_cancelEvent(o.e);
		obj.toggleDisplay(0);
		obj.owner.util_restoreSelection();
	}
	//ESC
	if (o.e.keyCode == 27) {
		obj.toggleDisplay(0);
		DOM_cancelEvent(o.e);
		obj.owner.util_restoreSelection();
	}
	//keyup
	if (o.e.keyCode == 38) {
		DOM_cancelEvent(o.e);
		var newindex = obj.highlightedIndex - 1;
		if (newindex < 0)
			newindex = 0;
		var tr = obj.setHighlightedIndex(newindex);

		//window.status = sprintf('selectedIndex: %d', obj.highlightedIndex);

		if (DOM_getStyleProperty(obj.optcontainer, 'display')!= 'none') {
			var c = obj.getScrollCoords();

			var min_display_y = parseInt(c.divpos.y) + parseInt(obj.optcontainer.scrollTop);
			var max_display_y = min_display_y + parseInt(obj.optcontainer.offsetHeight);
			var min_tr_y = parseInt(c.trpos.y) - parseInt(obj.optcontainer.scrollTop);
			var max_tr_y = min_tr_y + parseInt(obj.optContainerTable.rows[obj.highlightedIndex].offsetHeight);

			if (min_tr_y < min_display_y) {
				obj.optcontainer.scrollTop = -parseInt(c.divpos.y) + parseInt(c.trpos.y);
			}
		}
	}
	//keydown
	if (o.e.keyCode == 40) {
		DOM_cancelEvent(o.e);
		var newindex = obj.highlightedIndex + 1;
		if (newindex >= (obj.oldselect.options.length-1))
			newindex = (obj.oldselect.options.length-1);
		var tr = obj.setHighlightedIndex(newindex);
//		window.status = sprintf('selectedIndex: %d', obj.highlightedIndex);

		if (DOM_getStyleProperty(obj.optcontainer, 'display')!= 'none') {
			var c = obj.getScrollCoords();

			var min_display_y = parseInt(c.divpos.y);
			var max_display_y = min_display_y + parseInt(obj.optcontainer.offsetHeight);
			var min_tr_y = parseInt(c.trpos.y);
			var max_tr_y = min_tr_y + parseInt(obj.optContainerTable.rows[obj.highlightedIndex].offsetHeight);

			if (max_display_y < max_tr_y) {
				obj.optcontainer.scrollTop = max_tr_y - max_display_y;
			}
		}
	}
	//CR
	if (o.e.keyCode == 13) {
		if (DOM_getStyleProperty(obj.optcontainer, 'display')!= 'none') {
			obj.owner.util_restoreSelection();
			obj.setSelectedIndex(obj.highlightedIndex, true);
			obj.toggleDisplay(0);
			DOM_cancelEvent(o.e);
		}
	}
	return false;
}
function HTMLDropdown_setFocusSelect() {
	if (window.event) {
		this.optcontainer.focus();
	} else {
		this.oldselect.focus();
	}
}
function HTMLDropdown_getScrollCoords() {
	var tr = this.optContainerTable.rows[this.selectedIndex];
	var divpos = DOM_getAbsolutePos(this.optcontainer);
	var trpos = DOM_getAbsolutePos(tr);
	return {
		'trpos': trpos, 
		'divpos': divpos
	};
}

function HTMLDropdown_initialize() {
	this.options = [];
	var opts = this.oldselect.options;
	for (var i = 0; i < opts.length; i++) {
		if (opts[i].nodeType == 1) {
			if (opts[i].tagName.toLowerCase() == $HDR_OPTION_REPLACEMENT) {
				this.options.push(opts[i].getAttribute("HTMLValue"));
			}
		}
	}
}
if(typeof htmlDropDownCounter=="undefined"){
	var htmlDropDownCounter=-1;
}

function stopm(e){
	var o = DOM_setEventVars(e);
	//TODO: close dropdown when loosing focus
	if(typeof window[$HDR_GLOBALOBJECT]['openObjectCounter']!="undefined" && (theShadow==o.targ)){
		window[$HDR_GLOBALOBJECT]['objects'][window[$HDR_GLOBALOBJECT]['openObjectCounter']].toggleDisplay(0);
	}
	if(Ktml_ie){
		var newEl=document.elementFromPoint(o.e.x, o.e.y);
		if(newEl.getAttribute("counter")!=null){
			if(window[$HDR_GLOBALOBJECT]['openObjectCounter']!=newEl.counter){
				window[$HDR_GLOBALOBJECT]['objects'][newEl.counter].toggleDisplay(1);
			}
		}
	}else{
		var box = null;
		var st=document.body.scrollTop;
		var realY = o.e.clientY+st;
		for(var i=0; i< window[$HDR_GLOBALOBJECT]['objects'].length; i++){
			box = document.getBoxObjectFor(window[$HDR_GLOBALOBJECT]['objects'][i].img);
		//alert(st+":"+box.y+":"+);
			if(box.x<=o.e.clientX && o.e.clientX<=(box.x+box.width) && box.y<=realY && realY<=(box.y+box.height) ){
				if(window[$HDR_GLOBALOBJECT]['openObjectCounter']!=i){
					window[$HDR_GLOBALOBJECT]['objects'][i].toggleDisplay(1);
				}
				break;
			}
		}
		//document.body.scrollTop=st;
	}
}
function os(e){
alert("in");
}
var theShadow = null;
function HTMLDropdown_render() {
	this.counter = ++htmlDropDownCounter;
	if(theShadow==null){
		theShadow = document.createElement("DIV");
		theShadow = document.body.appendChild(theShadow);
		theShadow.innerHTML = '<a href="#"></a>';
		theShadow.className = "shadow";
		theShadow.onclick=stopm;//theShadow.firstChild.onblur=
		
	}
	// remove all children
	while (this.select.firstChild) 
		this.select.removeChild(this.select.lastChild);

	//HUGE OPERA BUG
	var additional_style_bug = '';
	if (typeof document.addEventListener == 'function') {
		additional_style_bug = 'padding: 1px'; 
	}
	//alert(this.oldselect.currentStyle.margin);
	var box = (Ktml_ie?this.oldselect.getBoundingClientRect():document.getBoxObjectFor(this.oldselect));
	var buttonWidth = ((Ktml_ie?box.right-box.left:box.width)-5);
	if (buttonWidth < 0) {
		buttonWidth = 0;
	}
	calc1 = this.width - this.imagesize.width;

	innerhtml = '<table  id="theTable'+this.counter+'"  border="0" cellspacing="0" cellpadding="0" style="width: ' + buttonWidth + 'px; margin:0px; padding:0px; " align="left">\
	<tr valign="middle">\
		<td  nowrap="true" ><div style="width:'+(calc1+(Ktml_ie?1:-4))+'px; overflow:hidden; padding:0px 2px;font:menu;">'+this.options[0]+'</div></td>\
		<td bgcolor="buttonface"><img src="'+NEXT_ROOT+'images/editor_images/arrow.gif" height="'+this.imagesize.height+'px" width="'+this.imagesize.width+'px" border="0" vspace="0" hspace="0"/></td>\
	</tr>\
</table>\
<img onclick="myClick(event)" counter="'+this.counter+'" src="'+NEXT_ROOT+'images/editor_images/s.gif" border="0" vspace="0" hspace="0" style="position:relative;top:-1px;left:-'+buttonWidth+'px;width:'+buttonWidth+'px; height:17px;margin-right:-'+(buttonWidth+5)+'px;margin-bottom:-2px;">';

	this.select.className = "lcontainer";
	this.select.style.width = buttonWidth+'px';
	this.select.style.height = "15px";
	this.select = this.oldselect.parentNode.insertBefore(this.select, this.oldselect);
	this.select.counter = this.counter;
	this.select.innerHTML = innerhtml;
	this.img=this.select.lastChild;
	this.table0 = this.select.getElementsByTagName('table')[0];
	//this.img.src=this.img.ssrc;


	var theContainer = document.createElement("DIV");
	theContainer.id = "theOptContainer"+this.counter;
	
	innerhtml = '<table counter="'+this.counter+'" cellspacing="0" cellpadding="0">';
	for ( var i = 0; i < this.options.length; i++) {
		innerhtml += '<tr><td nowrap="true" onmouseover="rowHi(this)" onclick="set_selected_index_and_close(event)"><div class="opt">'+this.options[i]+'</div></td></tr>';
	}
	innerhtml += '</table>';


	//theContainer = document.body.insertBefore(theContainer, document.body.firstChild);
	theContainer = theShadow.appendChild(theContainer);

	theContainer.className = "optcontainer";
	theContainer.style.width = "300px";//this.width+"px";
	theContainer.style.height = "150px";
	theContainer.innerHTML=innerhtml;

	this.oldselect.style.display = 'none';
	//this.oldselect = document.body.insertBefore(this.oldselect, document.body.firstChild);
	this.oldselect.style.position = "absolute";
	this.oldselect.style.left = "-10000px";
	this.oldselect.obj = this;

	theContainer.style.zIndex=99;

	this.optcontainer = theContainer;
	this.optContainerTable = theContainer.childNodes[0];
	if (window.event) {
		UNI_attachEvent(this.optcontainer, 'keydown', hndlr_htmldropdown_key, 1);
	} else {
		UNI_attachEvent(this.oldselect, 'keydown', hndlr_htmldropdown_key, 1);
	}

}

function rowHi(targEl){
	var wantedHighlightedIndex = -1;
	var obj=null;
	var wantedObjectCounter = null;
	if(typeof targEl=="number"){
		wantedHighlightedIndex = targEl;
		obj = this;
	}else if(typeof targEl=="undefined"){
		targEl = event.srcElement;
		while(targEl && (!(wantedObjectCounter=targEl.getAttribute("counter"))) ){
			if(targEl.tagName=="TR"){
				wantedHighlightedIndex = targEl.rowIndex;
			}
			targEl = targEl.parentElement;
		}
		if(wantedObjectCounter===null){
			return;
		}
		obj = window[$HDR_GLOBALOBJECT]['objects'][wantedObjectCounter];
	}else if(Ktml_mozilla){
		//'DOM_cancelEvent(o.e);
		if(typeof(targEl.target)!="undefined"){
			targEl = targEl.target;
		}
//alert(typeof targEl);
		while(targEl && (!(wantedObjectCounter=targEl.getAttribute("counter"))) ){
			if(targEl.tagName=="TR"){
				wantedHighlightedIndex = targEl.rowIndex;
			}
			targEl = targEl.parentElement;
		}
		if(wantedObjectCounter===null){
			return true;
		}
		obj = window[$HDR_GLOBALOBJECT]['objects'][wantedObjectCounter];
	}else{
		DOM_cancelEvent(window.event);
		while(targEl && (!(wantedObjectCounter=targEl.getAttribute("counter"))) ){
			if(targEl.tagName=="TR"){
				wantedHighlightedIndex = targEl.rowIndex;
			}
			targEl = targEl.parentElement;
		}
		if(wantedObjectCounter===null){
			return;
		}
		obj = window[$HDR_GLOBALOBJECT]['objects'][wantedObjectCounter];
	}
	if(typeof(obj.highlightedIndex)!="undefined" && obj.highlightedIndex!=wantedHighlightedIndex){
		obj.unsetHighlightedIndex(obj.highlightedIndex);
	}

	obj.highlightedIndex = wantedHighlightedIndex;
	if(0<=obj.highlightedIndex && obj.highlightedIndex<obj.optContainerTable.rows.length){
		obj.optContainerTable.rows[obj.highlightedIndex].cells[0].firstChild.style.margin = '0px';
		obj.optContainerTable.rows[obj.highlightedIndex].cells[0].firstChild.style.border = '1px solid black';
	}
}
function rowLow(wantedHilightedIndex){
	if(0 <= wantedHilightedIndex && wantedHilightedIndex < this.optContainerTable.rows.length){
		this.optContainerTable.rows[wantedHilightedIndex].cells[0].firstChild.style.margin = '1px';
		this.optContainerTable.rows[wantedHilightedIndex].cells[0].firstChild.style.border = '0px solid black';
	}
}

function HTMLDropdown_pickValue(idx){
	var tbl = this.optContainerTable;
	var cell0 = this.select.getElementsByTagName('table')[0].rows[0].cells[0];
	var toret = null;
	this.selectedIndex=idx;
	if(Ktml_ie){
		var newText = tbl.rows[idx].cells[0].innerText;
		cell0.childNodes[0].innerText = newText;
	}else{
		var newText = tbl.rows[idx].cells[0].textContent;
		cell0.childNodes[0].textContent = newText;
	}
	cell0.childNodes[0].title = newText;
	cell0.parentElement.parentElement.parentElement.lastHi = tbl.rows[idx].cells[0];
}

function HTMLDropdown_setSelectedIndex(idx, setLogic) {
	if (this.selectedIndex < 0) {
		this.selectedIndex = 0;
	}
	if (setLogic) {
		this.selectedIndex = idx;
	}
	//set selected index and scroll to it !!!TODO
	var tbl = this.optContainerTable;
	var cell0 = this.select.getElementsByTagName('table')[0].rows[0].cells[0];
	var toret = null;
	for (var i = 0; i < tbl.rows.length; i++) {
		if (i == idx) {
			tbl.lastHi = tbl.rows[i].cells[0];
			toret = tbl.rows[i];
			this.setHighlightedIndex(i);
			if (setLogic) {
				cell0.childNodes[0].textContent = cell0.childNodes[0].innerText = cell0.childNodes[0].title = Ktml_ie?tbl.rows[i].cells[0].innerText:tbl.rows[i].cells[0].textContent;
				this.owner.util_restoreSelection();
				this.oldselect.selectedIndex=this.selectedIndex;

				//inherit original select object onchange event
				if(Ktml_ie){
					this.oldselect.fireEvent("onchange");
				}else{
					var me = document.createEvent("Events");
					me.initEvent('change',0,0);
					this.oldselect.dispatchEvent(me);
				}
			}
		}
	}
	return toret;
}


function sizeToContent(counter){
	var obj=window[$HDR_GLOBALOBJECT]['objects'][counter];

	theShadow.style.height=(document.body.scrollHeight)+"px";
	theShadow.style.width=(document.body.scrollWidth)+"px";
	if(obj.mustResize){
		var oldtop = obj.optcontainer.style.top;
		obj.optcontainer.style.top="-10000px";
	}
	theShadow.style.display = "block";
	obj.optcontainer.style.display = 'block';

	if(obj.mustResize){
		var nw = Math.max(
			obj.width,
			obj.optContainerTable.offsetWidth+(obj.optContainerTable.offsetHeight<=obj.optcontainer.offsetHeight?0:0)
		);
		if(obj.optContainerTable.offsetHeight<obj.optcontainer.offsetHeight){
			obj.optcontainer.style.height = (obj.optContainerTable.offsetHeight+(Ktml_ie?(document.compatMode=="BackCompat"?2:0):0))+"px";
			nw+=(Ktml_ie?(document.compatMode=="BackCompat"?2:0):0);;
		}else{
			nw+=obj.optcontainer.offsetWidth-obj.optcontainer.clientWidth+(Ktml_ie?(document.compatMode=="BackCompat"?0:-2):-2);
		}
		obj.optcontainer.style.width = (nw)+"px";
		obj.optcontainer.style.top=oldtop;
		obj.mustResize = false;
	}

	obj.setHighlightedIndex(obj.selectedIndex);
	if(Ktml_ie){
		obj.optcontainer.focus();
	}else{
		obj.oldselect.style.display = '';
		window.setTimeout(function (){obj.oldselect.focus();}, 1);
	}
}
//mode : 0 "musthide" 1 "mustshow"
//set selected index and scroll to it !!!TODO
function show(el, obj) {
	if(typeof(window[$HDR_GLOBALOBJECT]['openObjectCounter'])!="undefined"){
		window[$HDR_GLOBALOBJECT]['objects'][window[$HDR_GLOBALOBJECT]['openObjectCounter']].toggleDisplay(0);
	}
	window[$HDR_GLOBALOBJECT]['openObjectCounter'] = obj.counter;

	var box = (Ktml_ie?obj.select.getBoundingClientRect():document.getBoxObjectFor(obj.select));
	if (Ktml_ie) {
		if(document.compatMode == 'CSS1Compat') {
			var toadd_left = document.documentElement.scrollLeft;
			var toadd_top = document.documentElement.scrollTop;
		} else {
			var toadd_left = document.body.scrollLeft;
			var toadd_top = document.body.scrollTop;
		}
	} else {
			var toadd_left = 0;
			var toadd_top = 0;
	}
	var leftPos = (Ktml_ie?box.left-2 + toadd_left:box.x-1);
	var topPos = (Ktml_ie?box.bottom-3 + toadd_top:box.y+box.height-2);
	el.style.left = leftPos+"px";
	el.style.top = topPos+"px";

	sizeToContent(obj.counter);
	obj.open = 1;
}

function hide(el, obj) {
	el.style.display = 'none';
	theShadow.style.display = "none";

	if (window.event) {
		//obj.select.blur();
	} else {
		obj.oldselect.style.display = 'none';
		//obj.oldselect.blur();
	}
	obj.open = 0;

}

function HTMLDropdown_toggleDisplay() {
	if (this.selectedIndex < 0) {
		this.selectedIndex = 0;
	}

	var el = this.optcontainer;

	var val = DOM_getDisplay(el);

	if (arguments.length == 0) {
		if (val == 'none') {
			this.owner.util_saveSelection();
			show(el, this);
		} else if (val == 'block' || val == '') {
			hide(el, this);
		}
	} else {
		var mode = arguments[0];
		if (mode == 1) {
			show(el, this);
		} else if (mode == 0) {
			hide(el, this);
		}
	}
}


function HTMLDropdown_tabIn() {
	this.select.getElementsByTagName('table')[0].rows[0].cells[0].style.backgroundColor = '#0000ff';
	if (window.event) {
		this.select.focus();
	} else {
		this.oldselect.selectedIndex = 0;
		this.oldselect.focus();
	}
}

function HTMLDropdown_toggleSpecialTags(mode) {
	var hide_tags = ['select'];
	var p = DOM_getAbsolutePos(this.select);
	var corner1x = p.x, corner1y = p.y;
	var corner2x = this.select.offsetWidth + corner1x, corner2y = this.select.offsetHeight + corner1y;
	for (var i = 0; i < hide_tags.length; i++) {
	
		var arr = document.getElementsByTagName(hide_tags[i]);

		for (var j = 0; j < arr.length; j++) {
			if (arr[j] != this.oldselect) {
				
				elpos = DOM_getAbsolutePos(arr[j]);
				
				var elcorner1x = elpos.x, elcorner1y = elpos.y;
				var elcorner2x = arr[j].offsetWidth + elcorner1x, elcorner2y = arr[j].offsetHeight + elcorner1y;
				
				if (mode == 1 && 0) {
					alert(sprintf('comparing %d.%d %d.%d with %d.%d %d.%d ', 
						corner1x, corner1y, corner2x, corner2y, 
						elcorner1x, elcorner1y, elcorner2x, elcorner2y 
						));
				}
				if (mode == 0 || 
					(corner1x > elcorner2x) || (corner2x < elcorner1x) 
					||
					(corner1y > elcorner2y) || (corner2y < elcorner1y) 
				) {
					//restore
					if (!arr[j].oldvisibility) {
						arr[j].oldvisibility = DOM_getVisibility(arr[j]);
					}
					arr[j].style.visibility = arr[j].oldvisibility;
				} else {
					//overlaps, must hide
					if (!arr[j].oldvisibility) {
						arr[j].oldvisibility = DOM_getVisibility(arr[j]);
					}
					arr[j].style.visibility = 'hidden';
				}
			}
		}
	}
}
HTMLDropdown.prototype.setFocusSelect = HTMLDropdown_setFocusSelect;
HTMLDropdown.prototype.getScrollCoords = HTMLDropdown_getScrollCoords;
HTMLDropdown.prototype.initialize = HTMLDropdown_initialize;
HTMLDropdown.prototype.render = HTMLDropdown_render;
HTMLDropdown.prototype.add = HTMLDropdown_add;
HTMLDropdown.prototype.pickValue = HTMLDropdown_pickValue;
HTMLDropdown.prototype.setHighlightedIndex = rowHi;
HTMLDropdown.prototype.unsetHighlightedIndex = rowLow;
HTMLDropdown.prototype.setSelectedIndex = HTMLDropdown_setSelectedIndex;
HTMLDropdown.prototype.toggleDisplay = HTMLDropdown_toggleDisplay;
HTMLDropdown.prototype.tabIn = HTMLDropdown_tabIn;
HTMLDropdown.prototype.toggleSpecialTags = HTMLDropdown_toggleSpecialTags;

function HTMLDropdown_add(value, text){
	var newOptionRow = this.optContainerTable.insertRow(this.optContainerTable.rows.length);
	var newOptionCell = newOptionRow.insertCell(0);
	newOptionCell.noWrap="true";
	newOptionCell.onmouseover=rowHi;
	newOptionCell.onclick=set_selected_index_and_close;
	newOptionCell.innerHTML = '<div class="opt">'+text+'</div>';
	this.mustResize = true;
}
function find_object_for_div(el) { //ALWAYS PASS THE UPPERMOST DIV!!!!!!!!!!!!!
	var toret = null;
	for ( var i = 0; i < window[$HDR_GLOBALOBJECT]['objects'].length; i++) {
		if (window[$HDR_GLOBALOBJECT]['objects'][i].select == el) {
			toret = window[$HDR_GLOBALOBJECT]['objects'][i];
			break;
		}
	}
	return toret;
}

function find_object_for_select(el) { //ALWAYS PASS THE HIDDEN SELECT!!!!!!!!!!!!!
	var toret = null;
	for ( var i = 0; i < window[$HDR_GLOBALOBJECT]['objects'].length; i++) {
		if (window[$HDR_GLOBALOBJECT]['objects'][i].oldselect == el) {
			toret = window[$HDR_GLOBALOBJECT]['objects'][i];
			break;
		}
	}
	return toret;
}

function set_selected_index_and_close(e) {
	var targetObjectCounter = null;
	var o = DOM_setEventVars(e);
	DOM_cancelEvent(o.e);
	var startEl = o.targ;
	while(startEl){
		if((targetObjectCounter=startEl.getAttribute("counter"))!=null){
			break;
		}
		startEl = startEl.parentElement;
	}
	if(targetObjectCounter!=null){
		var obj = window[$HDR_GLOBALOBJECT]['objects'][targetObjectCounter];
	}else{
		return;
	}

	var tbl = o.targ;
	var td = o.targ;
	//find the td and the table
	while (tbl.tagName.toLowerCase() != 'table') {
		if (tbl.tagName.toLowerCase() == 'td') {
			td = tbl;
		}
		tbl = tbl.parentNode;
	}

	var idx = 0;
	//find the index from the td
	for ( var i = 0; i < tbl.rows.length; i++) {
		if (tbl.rows[i].cells[0] == td) {
			idx = i;
			break;
		}
	}
	obj.setSelectedIndex(idx, true);
	obj.toggleDisplay(0);
}

function toggle_visible(e) {
	o = DOM_setEventVars(e);
	var found = o.targ;
	while (found.tagName.toLowerCase() != 'table') {
		found = found.parentNode;
	}
	var obj = find_object_for_div(found.parentNode);
	obj.toggleDisplay();
}


