// Version: "4.0.0"
// Copyright (c) Kore Technologies LLC .  All rights reserved.
var htmlCtl = null;
//var ksCollectPostBackActions;


function ksSearchReg(dhtmlCtl) {
	htmlCtl = dhtmlCtl;
}

function ksSearchEnd(col_s, selectedValue_s, stopOnError) {
	var searchDebug = false;
	try {
		if (searchDebug) alert("ksSearchEnd: begin");
		window.opener.ksCollectPostBacks(true);
		//if (stopOnError!=false) stopOnError = true;
		stopOnError = false;
		var collist = new String(col_s).split(String.fromCharCode(221));
		var vallist = new String(selectedValue_s).split(String.fromCharCode(221));

		var nxtItm = null;
		//alert("Search called for: " + col_s + " with " + selectedValue_s);		
		for (nxtItm in collist) {
			// Page call back to get control
			if (searchDebug) {
				alert("ksSearchEnd: Getting search HTML element: " + collist[nxtItm]);
			}
			htmlCtl = window.opener.ksSearchPick(collist[nxtItm], stopOnError);

			if (htmlCtl==null) {  // callback should have registered
				if (stopOnError==true) {
					alert("ksSearchEnd: Search pick column " + collist[nxtItm] + " not registered with form.");
					return;
				} else {
					continue;
				}
			}
		
			if (searchDebug) {
				alert("ksSearchEnd: Saving search value " + vallist[nxtItm] + " into HTML element: " + collist[nxtItm]);
			}
			// stuff search value into control
			
			// enable the control if needed so it will accept data
			try
			{
				if (htmlCtl.disabled) {
					htmlCtl.disabled = false;
				}
				if (htmlCtl.readonly) {
					htmlCtl.readonly = false;
				}				
			} catch (e) {}
			
			
			htmlCtl.value=vallist[nxtItm];
		}

		// Is there an onchange event tied to the inputs, if so call them.
		// If the events pass through the form action the submit will not be 
		// called, rather the event name to send to the server will be stored 
		// in the toolbar and comma separated if needed.  This is due to mozilla/ie
		// differences, mozilla sends only first post but IE will send them all 
		// this event collecting mechanism also cuts down on postbacks increasing perf.
		// get the handle to toolbar now or value will not be appendable, bug in IE?
		var evtCollObj = window.opener.document.getElementsByTagName("toolbar_action");
		if (evtCollObj==null) {
			alert("ksSearchEnd: Cannot get handle to local toolbar");
		}
		var evtResults = "";
		for (nxtItm in collist) {
			// Page call back to get control
			htmlCtl = window.opener.ksSearchPick(collist[nxtItm], stopOnError);
			if (htmlCtl!=null && htmlCtl.onchange!=null) {
				if (searchDebug) {
					alert("ksSearchEnd: Calling registered onchange event for " + collist[nxtItm]+ ": " + String(htmlCtl.onchange));
				}
				htmlCtl.onchange();
				if (evtResults!="") evtResults += ",";
				evtResults += evtCollObj.value; 

			}
		}
		// save the collected events into variable before it gets clobbered
		if (searchDebug) alert("ksSearchEnd: Collected events - " + evtResults );

		// Set the event name for postback
		var evtObj = window.opener.ksSearchPick("toolbar_action");
		
		if (evtObj!=null) {  
			if (evtResults!="") evtResults += ",";
			evtResults += "ksSearchEnd_"+col_s;
			if (searchDebug) {
				alert("ksSearchEnd: Setting event name into toolbar - " + evtResults);
			}
			if (evtObj.value==null) {
				evtObj.value = evtResults;
			} else {
				evtObj.value += evtResults;
			}
		}
		
		// Post the form to get the new value
		if (!window.opener.ksIsFilter()) {
			if (searchDebug) {
				alert("ksSearchEnd: Submitting form");
			}
			window.opener.ksGetDataForm().submit();
		}
	} catch(e) {
		alert("Error on ksSearchEnd: " + e.message);
	} finally {
		if (searchDebug) alert("ksSearchEnd: end");
		window.opener.ksCollectPostBacks(false);
		window.close();
	}
}




