// Version: "4.0.0"
// Copyright (c) Kore Technologies LLC .  All rights reserved.

function ksSearchEnd(col_s, selectedValue_s, stopOnError, clientElName, submitWhenDone) {
	var searchDebug = false;

	try {
    if (submitWhenDone != false) submitWhenDone = true;
		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) 
		{
  		var htmlCtl = null;
			// Page call back to get control
			if (searchDebug) {
				alert("ksSearchEnd: Getting search HTML element: " + collist[nxtItm]);
			}
            
      //making the clientElName the priority higher than col_s
      //sanity check
			if (clientElName!=null && typeof(clientElName) != 'undefined' && clientElName.length > 0)
			{
        	if (searchDebug) alert("ksSearchEnd: Getting secondary search HTML element: " + clientElName);
			    htmlCtl = window.opener.ksSearchPick(clientElName);
			}

			if (htmlCtl == null || typeof(htmlCtl) == 'undefined')
			{
				if (searchDebug) alert("ksSearchEnd: HTML getting the primary search HTML Element: " + collist[nxtItm]);
  			htmlCtl = window.opener.ksSearchPick(collist[nxtItm], stopOnError);
  		}


  		if (searchDebug) alert("ksSearchEnd: htmlCtl: " + htmlCtl + ", clientElName: " + clientElName);


			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
      if (searchDebug) alert("ksSearchEnd: Checking for registered onchange event for " + collist[nxtItm]);
			htmlCtl = window.opener.ksSearchPick(collist[nxtItm], stopOnError);
			if (htmlCtl!=null && htmlCtl.onchange!=null) {
				if (searchDebug) alert("ksSearchEnd: Onchange event for " + collist[nxtItm]+ ": " + String(htmlCtl.onchange) + " found, calling registered handler");
				htmlCtl.onchange();
				if (typeof(evtResults) != 'undefined' && evtResults!="") evtResults += ",";
				if(typeof(evtCollObj.value) != 'undefined')
    				evtResults += evtCollObj.value; 
			}
		}
		
		if(!window.opener.ksIsFilter()){
		    // 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 == '') {
				    evtObj.value = evtResults;
			    } else {
				    evtObj.value += "," + evtResults;
			    }
		    }
		}
		
		// Post the form to get the new value
		if (!window.opener.ksIsFilter() && submitWhenDone) {
			if (searchDebug) alert("ksSearchEnd: Submitting form");
			
			var dataForm = window.opener.ksGetDataForm();
			if (searchDebug) alert("ksSearchEnd: GetDatForm: " + dataForm);
			
			try {
			    // try to exectute the ajax callback for the column, if there
			    eval("window.opener.__callback_" + clientElName + "();");
            } catch(e) {
                // no callback, or it failed, do a postback
			    dataForm.submit();
            }
		}
//	} catch(e) {
//		alert("Error on ksSearchEnd: " + e.message);

	} finally {
		if (searchDebug) alert("ksSearchEnd: end");
		window.opener.ksCollectPostBacks(false);
		window.close();
	}
}

function ksSearchPick(column_name){
    var searchpickdebug = false;
    if (searchpickdebug) alert("ksSearchPick called for column: " +column_name);
	return (document.getElementById(GetFormElementId(column_name)));
}

