function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
} 

function listMoveSelectedItems(sourceName, targetName, valueFieldName, hiddenFieldName) {
	var fromElem = MM_findObj(sourceName);
	var toElem = MM_findObj(targetName);
	var shouldAddItem = true;
	
	// add the selected to the target
	for(i=0; i<fromElem.length; i++) {	
		shouldAddItem = true;
		
		if(fromElem.options[i].selected) {
			//skip blank/helper elements
			if(fromElem.options[i].value != "") {
				
				// skip exising elements (they have the same values)
				for(j=0; j<toElem.length; j++) {
					if(fromElem.options[i].value == toElem.options[j].value) {
						shouldAddItem = false;
						break;
					}
				}
				if(shouldAddItem) {
					toElem.options[toElem.length] = new Option(fromElem.options[i].text, fromElem.options[i].value);
				}
			}
		}
	}
	
	// delete the selected from the source
	for(i=fromElem.length-1; i>=0; i--) {
		if(fromElem.options[i].selected){
			//skip blank/helper elements
			if(fromElem.options[i].value != "") {
				fromElem.options[i] = null;
			}
		}
	}
	
	listUpdateHiddenField(valueFieldName, hiddenFieldName);
}

function listMoveAll(sourceName, targetName, valueFieldName, hiddenFieldName) {
	var fromElem = MM_findObj(sourceName);
	var toElem = MM_findObj(targetName);
	
	// add them to the target
	for(i=0; i<fromElem.length; i++) {
		//skip blank/helper elements
		if(fromElem.options[i].value != "") {
			toElem.options[toElem.length] = new Option(fromElem.options[i].text, fromElem.options[i].value);
		}
	}
	
	// delete from the source
	for(i=fromElem.length=1; i>=0; i--) {
		//skip blank/helper elements
		if(fromElem.options[i].value != "") {
			fromElem.options[i] = null;
		}
	}
	
	listUpdateHiddenField(valueFieldName, hiddenFieldName);
}

function listClear(targetName, valueFieldName, hiddenFieldName) {
	var targetElem = MM_findObj(targetName);
	
	for(i=targetElem.length=1; i>=0; i--) {
		//skip blank/helper elements
		if(fromElem.options[i].value != "") {
			targetElem.options[i] = null;
		}
	}
	
	listUpdateHiddenField(valueFieldName, hiddenFieldName);
}

function listUpdateHiddenField(sourceName, hiddenFieldName) {
	var fromElem = MM_findObj(sourceName);
	var hiddenField = MM_findObj(hiddenFieldName);
	
	//reset it
	hiddenField.value = "";
	
	for(i=0; i<fromElem.length; i++) {
		if(i==0) {
			hiddenField.value = fromElem.options[i].value;
		}
		else {
			// In order to avoid adding any blank elements
			if(hiddenField.value == "") {
				hiddenField.value += fromElem.options[i].value;
			}
			else {
				hiddenField.value = hiddenField.value + "," + fromElem.options[i].value;
			}
		}
	}
}


function listAddElement(id, value, targetName, hiddenFieldName) {
	var toElem = window.opener.document.getElementById(targetName);
	
	toElem.options[toElem.length] = new Option(value, id);
	listUpdateHiddenField(targetName, hiddenFieldName);
}





function Browser(){
	this.ver=navigator.appVersion;
	this.agent=navigator.userAgent;
	this.dom=document.getElementById? true : false;
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)? true : false;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)? true : false;
	this.ie4=(document.all && !this.dom)? true : false;
	this.ie = this.ie4||this.ie5||this.ie6;
	this.mac=this.agent.indexOf("Mac")>-1;
    this.ns6=(this.dom && parseInt(this.ver) >= 5) ? true : false;
	this.ns4=(document.layers && !this.dom) ? true : false;
    this.ns = this.ns6 || this.ns4;
	this.op=this.agent.indexOf("Opera")>-1 ? true : false;
}

var bwr = new Browser();

function Page() {
	this.width  =  bwr.ns ? window.innerWidth : window.document.body.offsetWidth;
	this.height =  bwr.ns ? window.innerHeight : window.document.body.offsetHeight;
}

function ObjectStyle(obj) {return eval("obj" + (bwr.ns4 ? "" : ".style"));}

function ObjCoordinates (strObj) { //v.1.0
	var o = MM_findObj(strObj); var x=0, y=0, p=new Object(), doc="";
	if (document.layers && o) {
		for (var i=0; i<document.layers.length; i++){
			var doc = "document." + document.layers[i].name;
			if (eval(doc+".document."+strObj)) {x+=(eval(doc+".left"));y+=(eval(doc+".top"));break;}
		}
		p.x=x+o.x; p.y=y+o.y;
	    return p;
	} 
	else if (o) {
		if(document.all) doc = "document.all['"+strObj+"']";
		if(document.getElementById) doc = document.getElementById(strObj) ? "document.getElementById('"+strObj+"')" : "document.getElementsByName('"+strObj+"')[0]";
		var offPar=".offsetParent";
		while(eval(doc+offPar)){x+=eval(doc+offPar+".offsetLeft"); y+=eval(doc+offPar+".offsetTop"); offPar += ".offsetParent";}
		p.x=x+o.offsetLeft; p.y=y+o.offsetTop;
		return p;
	}
}

function ToggleLayerVisibility(objStr, bool) {
	if( (o = MM_findObj(objStr)) != null){
		var oStyle = ObjectStyle(o);
		if(bwr.ns4) oStyle.visibility = bool ? "show" : "hide";
		else oStyle.visibility = bool ? "visible" : "hidden";
	}
}

function MoveObjectTo(objStr, x, y) {
    var p = ObjCoordinates(objStr);
	var oL = MM_findObj(objStr);
	var oStyle = ObjectStyle(oL);
	if(bwr.ns4 || bwr.o5) {
		oStyle.left = x;
		oStyle.top = y;
	}
	else {
		oStyle.left = x + "px";
		oStyle.top = y + "px";
	}
}

function clearVisibleMenus() {
	var arMenus = new Array("QuickSearch","MenuCloser");
	for(var i = 0; i<arMenus.length; i++) {
		if(MM_findObj(arMenus[i]) == null)
			continue;
			
		ToggleLayerVisibility(arMenus[i], false);
		
	}
}

function showMenu(triggerName, menuName) {
	clearVisibleMenus();
	if(MM_findObj(menuName) == null) {
		return;
	}
	var imgTriggerPt = ObjCoordinates(triggerName);
	var mnuX = imgTriggerPt.x - 27;
	var mnuY = imgTriggerPt.y + 30;
	MoveObjectTo(menuName, mnuX, mnuY);
	ToggleLayerVisibility(menuName, !isVisible(menuName));
}

function closeMenu(menuName) {
	ToggleLayerVisibility(menuName, false);
}

function isVisible(menuName) {
	if( (o = MM_findObj(menuName)) != null){
		var oStyle = ObjectStyle(o);
		if(bwr.ns4)	return oStyle.visibility ==  "show"
		else return oStyle.visibility == "visible";
	}
}
