//' +------------------------------------------------------------------------------------------------------------------------+
//' | CHANGE CONTROL                                                                                                         |
//' +------------------------------------------------------------------------------------------------------------------------+
//' | OWNER: Core Team						                                                       DATE CREATED: --/--/----  |
//' | AJAX Support 							                                                                                 |
//' +------------------------------------------------------------------------------------------------------------------------+
//' | UPDATED BY: GW                                                                               DATE UPDATED: 20/04/2006  |
//' | Added ajaxExecuteScript (beginning conversion to new set of optimised functions; some cleaning up required)			 |
//' +------------------------------------------------------------------------------------------------------------------------+
//' | UPDATED BY: GRAHAM WEIR                                                                      DATE UPDATED: 18/01/2007  |
//' | CROSS-BROWSER SUPPORT AND RENAME                                                                          			 |
//' +------------------------------------------------------------------------------------------------------------------------+
//' | UPDATED BY: NEIL PENDLINGTON                                                                 DATE UPDATED: 25/01/2007  |
//' | ADDED ORCHIDNET_AJAX_SUCCESS CONSTANT                                                                                  |
//' +------------------------------------------------------------------------------------------------------------------------+
//' | UPDATED BY: PHILIP ALLEN                                                                     DATE UPDATED: 18/11/2008  |
//' | ADDED 2 FUNCTIONS TO MAKE JS SCRIPTS USABLE WHEN PASSED THROUGH FROM AJAX                                              |
//' +------------------------------------------------------------------------------------------------------------------------+
//' | UPDATED BY: GRAHAM WEIR                                                                      DATE UPDATED: 08/01/2009  |
//' | PHIL DID SOMETHING AND I CHANGED IT                                                                                    |
//' +------------------------------------------------------------------------------------------------------------------------+

var ORCHIDNET_AJAX_SUCCESS = "orchidnet__ajax__success";
var errorCollection = "";
var ajaxResponseText;	// text returned from called url

function ajaxExecuteScript( url, queryString, aSyncFlag ) {
	ajaxResponseText = ajaxExecuteScriptAndFunction( url, queryString, "", "", aSyncFlag );
	return ajaxResponseText;
}

function ajaxExecuteScriptAndFunction( url, queryString, handlerFunction, handlerFunctionArguments, aSyncFlag ) {
	var ajaxRequest;
	ajaxResponseText = "";
	
    if ( queryString.length > 0 ) {
        queryString = queryString + "&";
    }
	
    var randomstr = Math.random().toString().replace(".", "") + Math.random().toString().replace(".", "");
    queryString = queryString + "unique=" + randomstr;
		
	if (window.XMLHttpRequest)
        ajaxRequest = new XMLHttpRequest();     //ajaxRequest.overrideMimeType('text/xml');
	else
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		
	if ( aSyncFlag ) {
	    ajaxRequest.onreadystatechange = function() { ajaxReadResponseText( ajaxRequest, handlerFunction, handlerFunctionArguments ); };	
	}
	
	ajaxRequest.open("GET", url + "?" + queryString, aSyncFlag);
	ajaxRequest.send(null);
    if ( !aSyncFlag ) {
        ajaxReadResponseText( ajaxRequest, handlerFunction, handlerFunctionArguments );
    }    
    return ajaxResponseText;    //
}

function ajaxReadResponseText( ajaxRequest, handlerFunction, handlerFunctionArguments ) {
    var functionString;

  	if (ajaxRequest.readyState == 4) {
	    if (ajaxRequest.status == 200) {
	        ajaxResponseText = ajaxRequest.responseText;
	        if (handlerFunction.length > 0) {
	            if (handlerFunctionArguments)
	                functionString = handlerFunction + "(" + handlerFunctionArguments + ", ajaxResponseText)";
	            else
	                functionString = handlerFunction + "(ajaxResponseText)";

	            eval(functionString);
	        } else {
	            errorCollection += ';handlerFunction.length=0';
	        }
	    } else {
	        errorCollection += ';ajaxRequest.status:' + ajaxRequest.status;
	    }
	} else {
	    errorCollection += ';ajaxRequest.readyState:' + ajaxRequest.readyState;
    }
}

function ajaxPopulateDropdownBox(s_dropdownBox, s_preserveTopOption) {
	var selectBoxGUID, selectBoxName;
	var dropdownBox = eval(s_dropdownBox);
	var preserveTopOption = eval(s_preserveTopOption)
	var option, newOption;
	
	eval(ajaxResponseText); // populates our two arrays with data for teh drop-down		
	if (dropdownBox) {
		if (!preserveTopOption) {
			dropdownBox.length = 0; // truncate dropdown
		} else {
			dropdownBox.length = 1; // save top option (typically, ----Please Select---- or N/A)
			dropdownBox.selectedIndex.value = 1;
		}
		if (selectBoxGUID && selectBoxGUID.length > 0) {
			for (x = 0; x < selectBoxGUID.length; x++) {
				newOption = document.createElement("OPTION");
				dropdownBox.options.add(newOption);
				newOption.value = selectBoxGUID[x];
				newOption.text = selectBoxName[x];
			}
		}
	}
}

/*
function getValueFromSQLProcess(s_returnVariable)
{
	var sqlValue = ajaxResponseText;
	
	eval(s_returnVariable + " = " + sqlValue);
}

function AJAXgetValueFromSQL(returnVariable, selectCol, tables, where)
{
	handlerFunction = "getValueFromSQLProcess";
	//handlerFunctionArguments = returnVariable;
	handlerFunctionArguments = "'" + returnVariable + "'";
	openAjaxRequest();
		
	if(ajaxRequest && ajaxRequest.readyState < 4)
		ajaxRequest.abort();
	
	ajaxRequest.onreadystatechange = ajaxReadResponseText;	
	ajaxRequest.open("POST", "/app/common/ajax/getValueFromSQL.asp?selectCol=" + selectCol + "&tables=" + tables + "&where=" + where, false);
	ajaxRequest.send(null);	
}
*/

//*******************************************************************************************************
//getScripts(...) & parseScripts(...)
//The following 2 functions can be used if you have scripts in your ajax response html.
//Using magic, you can now get and then parse the scripts so you can use them in the page.
//*******************************************************************************************************
function getScripts(input_source,scripts_array)
{
	var source = input_source;
	
	// Strip out tags
	while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1)
	{
		var s = source.indexOf("<script");
		var s_e = source.indexOf(">", s);
		var e = source.indexOf("</script", s);
		var e_e = source.indexOf(">", e);
			
		// Add to scripts array
		scripts_array.push(source.substring(s_e+1, e));
		// Strip from source
		source = source.substring(0, s) + source.substring(e_e+1);
	}
	
	while(source.indexOf("<SCRIPT") > -1 || source.indexOf("</SCRIPT") > -1)
	{
		var s = source.indexOf("<SCRIPT");
		var s_e = source.indexOf(">", s);
		var e = source.indexOf("</SCRIPT", s);
		var e_e = source.indexOf(">", e);
			
		// Add to scripts array
		scripts_array.push(source.substring(s_e+1, e));
		// Strip from source
		source = source.substring(0, s) + source.substring(e_e+1);
	}

	// Return the cleaned source
	return source;
}

function parseScripts(scripts_array)
{
	// Loop through every script collected and eval it
	for(var i=0; i < scripts_array.length; i++) 
	{
		try 
		{
			eval(scripts_array[i]);
		}
		catch(ex) 
		{
			// do what you want here when a script fails
			//alert(ex.description);
		}
	}
}

