// Copyright IBM Corp. 2002, 2007  All Rights Reserved.

function searchWriteBaseInfo(suffix, queryName, scopeName, generateFields, isInPortlet, isBidi, defId, defTitle, defDesc, defIcon, menuUrl, searchLabel, scopeLabel, actionsIcon, tabIndex) {
	if (tabIndex == null) {
		tabIndex="";
	} 
	
	if (tabIndex != "") {
		tabIndex=" tabindex=\"" + tabIndex + "\" ";
	}
	
	//JPE
	try{
		//siteScope & locale from themes
		searchMenuUrl=menuUrl+"&siteScope="+_siteScope+"&locale="+_locale;
	} catch(err)
	{
		//no siteScope & locale from themes
		searchMenuUrl=menuUrl;
	}
	//end JPE
	
	var imgOffset="-7px";
	var dirRTL="";
	
	if (isBidi) {
		dirRTL=" dir=\"rtl\" ";
		imgOffset="7px";
	}
	
	//JPE
	try{
		//overwrite from themes
		defId = _defId;
		defTitle = _defTitle;
		defDesc = _defDesc;
		defIcon = _defIcon;
		
	} catch(err)
	{
		//no overwrite from themes
	}
	//end JPE
	
	
	searchSetDefaultScopeMenuItem(defId, defTitle, defDesc, defIcon);
	
	document.write("<table " + dirRTL + " border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
	document.write("<tr>");
	document.write("<td " + dirRTL + " nowrap=\"nowrap\">");
	document.write("<a " + tabIndex + " id=\"searchMenuAnchor" + suffix + "\" href=\"javascript: void showAsyncSearchMenu(&quot;" + suffix + "&quot;,&quot;" + queryName + "&quot;,&quot;" + scopeName + "&quot;," + isInPortlet + ")\">");
	document.write("<img width=\"" + searchIconWidth + "\" height=\"" + searchIconHeight + "\" alt=\"" + defDesc + "\" id=\"searchMenuIconAnchor" + suffix + "\" border=\"0\" src=\"" + defIcon + "\" />");
	document.write("<img style=\"position: relative; top: 4px; left: " + imgOffset + ";\" border=\"0\" src=\"" + actionsIcon + "\" />");
	document.write("</a>");
	document.write("</td>");
	
	document.write("<td " + dirRTL + " nowrap=\"nowrap\">");
	
	if (generateFields) {
		//JPE
		document.write("<input value=\"\" type=\"hidden\" name=\"" + queryName + "\" id=\"" + queryName + "\" />");
		document.write("<input value=\"" + defId + "\" type=\"hidden\" name=\"" + scopeName + "\" id=\"" + scopeName + "\" />");
		//End JPE
	}
	
	document.write("<input value=\"" + defTitle + "\" type=\"hidden\" name=\"scopeDisplayFieldName" + suffix + "\" id=\"scopeDisplayFieldName" + suffix + "\" />");
	document.write("<label style=\"display:none\" for=\"searchDisplayFieldName" + suffix + "\">" + searchLabel + "</label>");
	document.write("<input " + tabIndex + " value=\"\" class=\"" + searchDisabledFieldClass + "\" size=\"30\" id=\"searchDisplayFieldName" + suffix + "\"  name=\"searchDisplayFieldName" + suffix + "\"  type=\"text\" onfocus=\"searchClearText(this, &quot;" + queryName + "&quot;, &quot;" + suffix + "&quot;);\" onclick=\"searchClearText(this, &quot;" + queryName + "&quot;, &quot;" + suffix + "&quot;);\" onBlur=\"searchSetQuery(this, &quot;" + queryName + "&quot;, &quot;" + suffix + "&quot;);searchSetText(this, &quot;" + queryName + "&quot;, &quot;" + suffix + "&quot;);\" onkeypress=\"return searchDisplayKeyPress(event)\" />");
	document.write("</td></tr></table>");
	
	document.write("<script type=\"text/javascript\" language=\"Javascript\">");
	document.write("searchInitJsControlField(\"" + queryName + "\", \"" + scopeName + "\", \"" + suffix + "\");");
	document.write("</script>");
};

var searchDynMenu=function() {
	return false
};

function showAsyncSearchMenu(uniqueId, queryName, scopeName, isInPortlet) {
	searchDynMenu=function () {
		searchShowMenu("searchMenuAnchor" + uniqueId, uniqueId, queryName, scopeName, isInPortlet);
	};
		
	if (searchLoadDynamicMenus(false)) {
		searchDynMenu();
	}
};
	
function searchInitJsControlField(queryName, scopeName, uniqueId) {
	var elquery=searchGetField(queryName);
	var elscope=searchGetField(scopeName);
	var elquerydisp=searchGetField("searchDisplayFieldName" + uniqueId);
	var elscopedisp=searchGetField("scopeDisplayFieldName" + uniqueId);
	var elanchor=searchGetField("searchMenuIconAnchor" + uniqueId);
	
	if (!arrSearchMenuControls) {
		arrSearchMenuControls=new Array();
	}

	arrSearchMenuControls[arrSearchMenuControls.length]=new searchMenuControl(elquery, elscope, elquerydisp, elscopedisp, elanchor);
	
	var xitm=searchGetDefaultScopeItem();
	
	if (xitm && elscope && elscopedisp) {
		if (elscope.value.isValid() || (xitm && xitm.id.isValid())) {
			ret=searchLoadDynamicMenus(true);
			searchAppendContextualScopes();
			xitm=getSearchScopeMenuItem(elscope.value);
		}
	}
	
	if (xitm) {
		searchSetScope(xitm.id, queryName, scopeName, uniqueId);
		searchClearText(null, queryName, uniqueId);
		searchSetText(null, queryName, uniqueId);
	}
	
	if (elquery && elquerydisp && elquery.value.isValid()) {
		elquerydisp.value=elquery.value;
	}
	
	if (elquery && elquerydisp && elquery.value.isValid()) {
		elquerydisp.className=searchEnabledFieldClass;
	} 
	else {
		elquerydisp.className=searchDisabledFieldClass;
	}
	
	copyPopulatedControlAround();
};

function copyPopulatedControlAround() {
	var populatedMenuControl=getPopulatedControl(false);
	
	if (populatedMenuControl && arrSearchMenuControls) {
		for (var i=0; i < arrSearchMenuControls.length; i++) {
			var menucontrol=arrSearchMenuControls[i];
		
			if (!menucontrol.queryfield.value.isValid() || !menucontrol.scopefield.value.isValid()) {
				menucontrol.queryfield.value = populatedMenuControl.queryfield.value;
				menucontrol.scopefield.value=populatedMenuControl.scopefield.value;
				menucontrol.querydisplayfield.value = populatedMenuControl.querydisplayfield.value;
				menucontrol.scopedisplayfield.value = populatedMenuControl.scopedisplayfield.value;
				menucontrol.querydisplayfield.className = populatedMenuControl.querydisplayfield.className;
				menucontrol.anchorfield.src = populatedMenuControl.anchorfield.src;
				menucontrol.anchorfield.title = populatedMenuControl.anchorfield.title;
			}
		};
	}
};

function getPopulatedControl(firstIfNone) {
	if (firstIfNone == null) {
		firstIfNone=false;
	}
	
	var populatedMenuControl=null;
	
	if (arrSearchMenuControls) {
		for (var i=0; i < arrSearchMenuControls.length; i++) {
			var menucontrol=arrSearchMenuControls[i];
			if (menucontrol.queryfield.value.isValid()) {
				populatedMenuControl=arrSearchMenuControls[i];break;
			}
		};
		
		if (firstIfNone) {
			populatedMenuControl=arrSearchMenuControls[0];
		}
	}
	
	return populatedMenuControl;
};

var searchHttpRequest=false;

function searchLoadDynamicMenus(loadnow) {
	if (searchNeedsMenuItems()) {
		searchScopeMenuArray=new Array();
		searchLinkMenuArray=new Array();
		document.body.style.cursor="progress";
		
		if (loadnow) {
			return searchRunHttpRequest(true);
		} 
		else {
			setTimeout("searchRunHttpRequest(false)",1);
		};
		
		return false;
	} 
	else {
		return true;
	};
};

function searchRunHttpRequest(loadnow) {
	if (window.XMLHttpRequest) {
		searchHttpRequest=new XMLHttpRequest();
	} 
	else if (window.ActiveXObject) {
		try {
			searchHttpRequest=new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (ex) {
			try {
				searchHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (ex) {};
		};
	}
	
	if (!searchHttpRequest) {
		document.body.style.cursor="auto";
		return false;
	}
	
	searchHttpRequest.open("GET", searchMenuUrl, !loadnow);
	
	if (!loadnow) {
		searchHttpRequest.onreadystatechange=searchProcessDynamicMenus;
	}
	
	searchHttpRequest.send(null);
	
	if (loadnow) {
		searchProcessDynamicMenus(true);
		return !searchNeedsMenuItems();
	} 
	else {
		return true;
	};
};

function searchProcessDynamicMenus(loadnow) {
	if (loadnow == null) 
		loadnow=false;
		
	if (searchHttpRequest.readyState == 4) {
		if (searchHttpRequest.status == 200) {
			searchEvalResults(searchHttpRequest.responseText);
			
			if (!loadnow) {
				searchDynMenu();
			}
		}

		document.body.style.cursor="auto";
	}
};
	
function searchNeedsMenuItems() {
	return (!searchEditMenuItem);
};

function searchSetDefaultScopeMenuItem(sid, stext, sdesc, sicon) {
	if (!searchAllScopeMenuArray) {
		searchAllScopeMenuArray=new Array();
		searchAllScopeMenuArray[searchAllScopeMenuArray.length]=new searchScopeMenuItem(sid, stext, sdesc, sicon);
	}
};

function searchEvalResults(txt) {
	var startscript="<MENUSCRIPT>";
	var endscript="</MENUSCRIPT>";
	var idxCheck=0;
	
	if (txt.indexOf(startscript) > -1) {
		txt=txt.substring(txt.indexOf(startscript) + startscript.length);
		idxCheck++;
	}
	
	if (txt.indexOf(endscript) > -1) {
		txt=txt.substring(0, txt.indexOf(endscript));
		idxCheck++;
	}
	
	if (idxCheck == 2) {
		eval(txt);
	}
};

function searchClearText(elem, queryName, uniqueId) {
	var elquery=searchGetField(queryName, elem);
	var elquerydisp=searchGetField("searchDisplayFieldName" + uniqueId,elem);
	
	if (elquery && elquerydisp) {
		if (elquery.value == "") {
			elquerydisp.value="";
			elquerydisp.className=searchEnabledFieldClass;
		}
	}
};

function searchSetText(elem, queryName, uniqueId) {
	var elquery=searchGetField(queryName,elem);
	var elquerydisp=searchGetField("searchDisplayFieldName" + uniqueId,elem);
	var elscopedisp=searchGetField("scopeDisplayFieldName" + uniqueId,elem);
	
	if (elquerydisp && elscopedisp && elquery) {
		if (elquery.value == "") {
			elquerydisp.value=elscopedisp.value;
			elquerydisp.className=searchDisabledFieldClass;
		}
	}
};

function getSearchScopeMenuItem(sid) {
	for (var iSearchLoop=0; iSearchLoop < searchAllScopeMenuArray.length; iSearchLoop++) {
		if (searchAllScopeMenuArray[iSearchLoop].id == sid) {
			return searchAllScopeMenuArray[iSearchLoop];
		}
	};
	
	if (searchDynScopeMenuArray && searchDynScopeMenuArray.length > 0) {
		for (var i=0; i < searchDynScopeMenuArray.length; i++) {
			if (searchDynScopeMenuArray[i].id == sid) {
				return searchDynScopeMenuArray[i];
			}
		};
	}
	
	return null;
};

function searchSetQuery(elem, queryName, uniqueId) {
	var elquery=searchGetField(queryName, elem);
	var eldisp=searchGetField("searchDisplayFieldName" + uniqueId,elem);
	
	if (elquery && eldisp) {
		var queryvalue=eldisp.value;
		
		elquery.value=queryvalue.trim();
		
		if (elquery.value == "") 
			searchSetText();
	}
};

var isSearchMenuOpen=false;
var searchCurrentField="";

function searchShowMenu(reftagid, uniqueId, queryName, scopeName, isInPortlet) {
	searchBuildMenu("searchMenuId" + uniqueId, queryName, scopeName, uniqueId, isInPortlet);

	//if (!isSearchMenuOpen) { // FRED
	if(true) { // FRED
		getTop_orig=getTop;
		getLeft_orig=getLeft;
		getTop=searchGetTop;
		getLeft=searchGetLeft;

		onmousedown_prev=document.onmousedown;
		
		isSearchMenuOpen=true;
		searchCurrentField="searchDisplayFieldName" + uniqueId;
		searchShowContextMenu("searchMenuId" + uniqueId, reftagid);
		document.onmousedown=searchHideCurrentContextMenu;
	} 
	else {
		searchHideCurrentContextMenu();
		setTimeout("searchShowMenu(\"" + reftagid + "\",\"" + uniqueId + "\",\"" + queryName + "\",\"" + scopeName + "\"," + isInPortlet + ");",100);
	};
};

function searchHideCurrentContextMenu() {
	if (isSearchMenuOpen) {

		// onmousedown_prev(); // FRED
		
		document.onmousedown=onmousedown_prev;
		getTop=getTop_orig;
		getLeft=getLeft_orig;
		
		if (window.event) {
			var evt=window.event;
			var elem=(evt.srcElement) ? evt.srcElement : evt.target;elem.focus();
		}
		
		isSearchMenuOpen=false;
		
		searchCurrentField="";
	}
};

function searchGetField(id,elem) {
	var ret;
	
	if (elem == null && window.event) {
		var evt=window.event;
		
		elem=(evt.srcElement) ? evt.srcElement : evt.target;
	}
	
	if (elem && elem.form) {
		ret=elem.form[id];
	}
	
	if (ret == null) {
		ret=document.getElementById(id);
	}
	
	if (ret == null) {
		var els=document.getElementsByTagName("INPUT");
		
		for (var i=0; i < els.length; i++) {
			if (els[i].name == id) {
				ret=els[i];
				break;
			}
		};
	}
	
	return ret;
};

function openExternalSearch(url, queryName) {
	var querytext="";
	var elquery=searchGetField(queryName);
	
	if (elquery) {
		if (window.URLEncoder) {
			querytext=URLEncoder(elquery.value);
		} 
		else {
			querytext=escape(elquery.value);
		};
	}
	
	window.open(url + querytext);
};

function searchSetScope(id, queryName, scopeName, uniqueId) {
	var itm=getSearchScopeMenuItem(id);
	var val=itm.title;
	var icon=itm.icon;
	var desc=itm.description;
	var elscopedisp=searchGetField("scopeDisplayFieldName" + uniqueId);
	var elanchor=searchGetField("searchMenuIconAnchor" + uniqueId);
	var elquery=searchGetField(queryName, elscopedisp);
	var elscope=searchGetField(scopeName, elscopedisp);
	
	if (elscopedisp) 
		elscopedisp.value=val;
	
	if (elscope) 
		elscope.value=id;
	
	if (elquery && elquery.value == "") 
		searchSetText(null, queryName, uniqueId);
		
	if (elanchor) {
		elanchor.src=icon;
		elanchor.title=desc;
	}
};

function searchAddMenuItem(menu, text, id, callback, icon) {
	var menuItm=new UilMenuItem(text, true,"",callback , null, icon, false, "main-menu-item", "main-menu-item-selected");
	
	menuItm.create_orig=menuItm.create;
	menuItm.create=UilMenuItemCreate_search;
	menu.add(menuItm);
};

function UilMenuItemCreate_search(menuHasIcon, menuHasSubmenu) {
	this.create_orig();
	searchFixImageAlt(this.itemTag);
};

function searchFixImageAlt(elem) {
	if (elem.childNodes && elem.childNodes.length) {
		for (var i=0; i < elem.childNodes.length; i++) {
			searchFixImageAlt(elem.childNodes[i]);
		};
	} 
	else {
		if (elem.src && elem.src.indexOf(searchCheckmarkIcon) > -1) {
			elem.alt=searchAdjustImageAlt(elem.alt);
			elem.title=searchAdjustImageAlt(elem.title);
		}
	};
};

function searchAdjustImageAlt(src) {
	src=searchRemoveTags(src);
	
	if (searchCheckmarkAlt && searchCheckmarkAlt.indexOf("{0}") > -1) {
		src=searchCheckmarkAlt.replace("{0}", src);
	}
	
	return src;
};

function searchRemoveTags(src) {
	while (src.indexOf("<") > -1 && src.indexOf(">")) {
		src=src.substring(0, src.indexOf("<")) + src.substring(src.indexOf(">") + 1);
	};
	
	while (src.indexOf("&nbsp;") > -1) {
		src=src.replace("&nbsp;"," ");
	};
	
	return src.trim();
};

function searchAddScopeMenuItem(menu, text, id, callback, icon, scopeName, uniqueId) {
	var elscope=searchGetField(scopeName, searchGetField("scopeDisplayFieldName" + uniqueId));
	
	if (elscope && elscope.value == id) {
		icon=searchCheckmarkIcon;
	} 
	else {}
	
	searchAddMenuItem(menu, text, id, callback , icon);
};

function searchGetMenuText(sText, sIcon, sDesc) {
	if (sDesc == null) 
		sDesc=sText;
		
	sText=sText.replace(" ", "&nbsp;");
	
	var ret="<img width=\"" + searchIconWidth + "\" height=\"" + searchIconHeight + "\" alt=\"" + sDesc + "\" title=\"" + sDesc + "\" src=\"" + sIcon + "\" border=\"0\" /> ";
	
	return ret + sText;
};

function searchAppendContextualScopes() {
	if (searchDynScopeMenuArray && searchDynScopeMenuArray.length > 0) {
		for (var i=0; i < searchDynScopeMenuArray.length; i++) {
			var bOK=true;
			
			for (var j=0; j < searchAllScopeMenuArray.length; j++) {
				if (searchDynScopeMenuArray[i].id == searchAllScopeMenuArray[j].id || 
					searchDynScopeMenuArray[i].process == searchAllScopeMenuArray[j].process) {
					bOK=false;
				}
			};
			
			if (bOK) {
				searchAllScopeMenuArray[searchAllScopeMenuArray.length]=searchDynScopeMenuArray[i];
			}
		};
	}
};

function searchGetDefaultScopeItem() {
	var ret=null;
	
	for (var j=0; j < searchAllScopeMenuArray.length; j++) {
		var def="" + searchAllScopeMenuArray[j].isDefault;
		
		if (def.isTrue()) {
			ret=searchAllScopeMenuArray[j];break;
		}
	};
	
	if (searchDynScopeMenuArray && searchDynScopeMenuArray.length > 0 && ret == null) {
		for (var j=0; j < searchDynScopeMenuArray.length; j++) {
			var def="" + searchDynScopeMenuArray[j].isDefault;
			
			if (def.isTrue()) {
				ret=searchDynScopeMenuArray[j];
				break;
			}
		};
	}
	
	if (ret == null) {
		ret=searchAllScopeMenuArray[0];
	}
	
	return ret;
};

function searchBuildMenu(menuID, queryName, scopeName, uniqueId, isInPortlet) {
	var isLTR=searchIsLTR;
	var menu=getContextMenu(menuID);
	
	if (menu && menu.name) {
		uncacheContextMenu(menu);
	}
	
	menu=createContextMenu(menuID, isLTR, null, "main-menu-border", "main-menu");
	
	var scid;
	var sctitle;
	var scdesc;
	var scicon;
	var sclink;
	
	searchAppendContextualScopes();
	
	for (var iSearchLoop=0; iSearchLoop < searchAllScopeMenuArray.length; iSearchLoop++) {
		scid		=	searchAllScopeMenuArray[iSearchLoop].id;
		sctitle		=	searchAllScopeMenuArray[iSearchLoop].title;
		scdesc		=	searchAllScopeMenuArray[iSearchLoop].description;
		scicon		=	searchAllScopeMenuArray[iSearchLoop].icon;
		scmenutext	=	searchGetMenuText(sctitle, scicon, scdesc);
		
		searchAddScopeMenuItem(menu, scmenutext, scid, "javascript:searchSetScope(\"" + scid + "\",\"" + queryName + "\",\"" + scopeName + "\",\"" + uniqueId + "\")" , null, scopeName, uniqueId);
	};
	
	// Fred
	if ( (searchHelpUrl!=null && searchHelpUrl!="") 
				|| (searchUserDN!=null && searchEditUrl!=null && searchUserDN!="" && searchEditUrl!="")) {
		menu.addSeparator();
	}
	 
	// Fred
	if (searchUserDN!=null && searchEditUrl!=null && searchUserDN!="" && searchEditUrl!="") {
		searchAddMenuItem(menu, searchGetMenuText(searchEditMenuItem.title, searchEditMenuItem.icon), searchEditMenuItem.id, searchEditMenuItem.link , null);
	}
	
	// Fred
	if (searchHelpUrl!=null && searchHelpUrl != "") {
		searchAddMenuItem(menu, searchGetMenuText(searchHelpMenuItem.title, searchHelpMenuItem.icon), searchHelpMenuItem.id, searchHelpMenuItem.link , null);
	}
	
	var isFirst=true;
	
	for (var iSearchLoop=0; iSearchLoop < searchLinkMenuArray.length; iSearchLoop++) {
		if (isFirst) {
			menu.addSeparator();
			isFirst=false;
		}
		
		scid		=	searchLinkMenuArray[iSearchLoop].id;
		sctitle		=	searchLinkMenuArray[iSearchLoop].title;
		scdesc		=	searchLinkMenuArray[iSearchLoop].description;
		scicon		=	searchLinkMenuArray[iSearchLoop].icon;
		sclink		=	searchLinkMenuArray[iSearchLoop].link;
		scmenutext	=	searchGetMenuText(sctitle, scicon);
		
		searchAddScopeMenuItem(menu, scmenutext, scid, "javascript:openExternalSearch(\"" + sclink + "\",\"" + queryName + "\")" , null, scopeName, uniqueId);
	};
	
	// Fred
	if ((searchReturnUrl!=null && searchReturnUrl!="") && (!isInPortlet)) {
		if (!isFirst) {
			menu.addSeparator();
		}
		
		scid		=	searchReturnMenuItem.id;
		sctitle		=	searchReturnMenuItem.title;
		scdesc		=	searchReturnMenuItem.description;
		scicon		=	searchReturnMenuItem.icon;
		sclink		=	searchReturnMenuItem.link;
		scmenutext	=	searchGetMenuText(sctitle, scicon);
		
		searchAddMenuItem(menu, scmenutext, scid, sclink , null);
	}
	
	return menu;
};

function searchShowContextMenu(name, reftagid) {
	var reftag;
	var offtag;
	
	if (reftagid == null) {
		reftag=window.event.srcElement;
	} 
	else {
		reftag=searchGetField(reftagid);
	}

	if (reftag) 
		contextMenuShow(name, null, null, reftag, true);

	clearMenuTimer();
};

function searchGetTop(tag, recurse, istop) {
	if (istop == null) 
		istop=true;
		
	var size=0;
	
	if (recurse && tag.offsetParent != null) {
		size+=getTop(tag.offsetParent, recurse, false);
	}
	
	if (tag != null) {
		size+=tag.offsetTop;
	}
	
	if (istop) {
		return size + getHeight(tag);
	} 
	else {
		return size;
	};
};

function searchGetLeft(tag, recurse, istop) {
	if (istop == null) 
		istop=true;
		
	var size=0;
	
	if (recurse && tag.offsetParent != null) {
		size+=getLeft(tag.offsetParent, recurse, false);
	}
	
	if (tag != null) {
		size+=tag.offsetLeft;
	}
	
	if (istop) {
		return size - getWidth(tag);
	} 
	else {
		return size;
	};
};

function searchDisplayKeyPress(evt) {
	var kcode;
	
	evt=(evt) ? evt : ((window.event) ? window.event : null);
	
	if (evt) {
		var kcode=(evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : evt.charCode);
		
		if (kcode == 13) {
			var elem=(evt.srcElement) ? evt.srcElement : evt.target;
			
			elem.blur();
			
			setTimeout("var frm=document." + elem.form.name + ";if (frm.onsubmit) {if (frm.onsubmit() == true) {frm.submit();}} else {frm.submit();};",200);
			
			return false;
		}
	}
};

function searchGetThemeForm() {
	return document.forms["searchFromThemeForm"];
};

function searchSubmitThemeForm(notextmsg) {
	var frm; 
	var fld;
	
	frm=searchGetThemeForm();
	
	if (frm) {
		fld=frm["clearifblank"];
	}
	
	if (fld && fld.value && fld.value == "0") {
		return true;
	} 
	else {
		fld=frm["query"];
		
		if (fld.value.length == 0) {
			alert(notextmsg);
			return false;
		} 
		else {
			fld=frm["scope"];
			if (fld.value.length > 200) {
				frm.method="post";
				
				if (frm.action.indexOf("#") > -1) {
					frm.action=frm.action.substring(0, frm.action.indexOf("#"));
				}
				
				if (frm.action.indexOf("?") > -1) {
					frm.action=frm.action.substring(0, frm.action.indexOf("?"));
				}
			}
			
			return true;
		};
	}
};

function searchReturn_To_Results() {
	searchShow_Location(searchReturnUrl);
};

function searchShow_Prefs() {
	searchShow_Popup(searchEditUrl, "portletEditWindow", null, 300);
};

function searchShow_Help() {
	searchShow_Popup(searchHelpUrl, "portletHelpWindow");
};

function searchShow_Location(surl) {
	if (typeof searchGetThemeForm == "function") {
		var frm=searchGetThemeForm();
		
		if (frm) {
			var fld=frm["clearifblank"];
			
			if (fld) {
				fld.value="0";
			}
			
			frm.action=surl;
			frm.submit();
		}
	} 
	else {
		window.location.href=surl;
	};
};

function searchShow_Popup(surl, windowname, newwidth, newheight, newtop, newleft) {
	if (newwidth == null) 	newwidth=815;
	if (newheight == null) 	newheight=600;
	if (newtop == null) 	newtop=10;
	if (newleft == null) 	newleft=10;
	
	var newWindow=window.open(surl,windowname,"resizable=yes,scrollbars=yes,menubar=no,toolbar=yes,status=no,width=" + newwidth + ",height=" + newheight + ",screenX=" + newtop + ",screenY=" + newleft + ",top=" + newtop + ",left=" + newleft + "");
	
	newWindow.focus();
	return false;
};

String.prototype.trim=function() {
	return this.replace(/^\s+|\s+$/, '');
};

String.prototype.isTrue=function() {
	return (this == "1") || (this.toLowerCase() == "yes") || (this.toLowerCase() == "y");
};

String.prototype.isValid=function() {
	return (this != null && this.length > 0);
};

function searchMenuControl(queryfield, scopefield, querydisplayfield, scopedisplayfield, anchorfield) {
	this.queryfield=queryfield;
	this.scopefield=scopefield;
	this.querydisplayfield=querydisplayfield;
	this.scopedisplayfield=scopedisplayfield;
	this.anchorfield=anchorfield;
};

function searchScopeMenuItem(sid, stext, sdesc, sicon, sisdef, sproc) {
	this.id=sid;
	this.title=stext;
	this.description=sdesc;
	this.icon=sicon;
	this.isDefault=sisdef;
	this.process=sproc;
};

function searchLinkMenuItem(sid, stext, sdesc, sicon, slink) {
	this.id=sid;
	this.title=stext;
	this.description=sdesc;
	this.icon=sicon;
	this.link=slink;
};

function addSearchDynamicScopeItem(sid, sname, sdesc, sicon, sisdef, sproc) {
	if (!searchDynScopeMenuArray) {
		searchDynScopeMenuArray=new Array();
	}
	
	var bOktoAdd=true;
	
	for (var i=0; i < searchDynScopeMenuArray.length; i++) {
		if (searchDynScopeMenuArray[i].id == sproc) {
			bOkToAdd=false;break;
		}
	};
	
	if (bOktoAdd) {
		searchDynScopeMenuArray[searchDynScopeMenuArray.length]=new searchScopeMenuItem(sproc, sname, sdesc, sicon, sisdef, sid);
		
		if (sisdef.isTrue()) {
			var popcontrol=getPopulatedControl(true);
			
			if (popcontrol) {
				var uniqueId=popcontrol.anchorfield.id;
				
				uniqueId=uniqueId.substring(("searchMenuIconAnchor").length);
				
				searchSetScope(sproc, popcontrol.queryfield.name, popcontrol.scopefield.name, uniqueId);
				
				copyPopulatedControlAround();
			}
		}
	}
};

var onmousedown_orig;
var getTop_orig;
var getLeft_orig;
var arrSearchMenuControls;
var searchAllScopeMenuArray;
var searchScopeMenuArray;
var searchDynScopeMenuArray;
var searchDefaultScopeMenuItem;
var searchLinkMenuArray;
var searchEditMenuItem;
var searchEditUrl;
var searchHelpMenuItem;
var searchHelpUrl;
var searchReturnMenuItem;
var searchReturnUrl;
var searchMenuUrl;
var searchIsLTR;
var searchCheckmarkIcon;
var searchCheckmarkAlt;
var searchUserDN;
var searchEnabledFieldClass="wpsPortletToolbarText";
var searchDisabledFieldClass="wpsPortletToolbarDisabledText";
var searchIconWidth="16";
var searchIconHeight="16";
