// To use Ajax remote scripting, create a remote class and pass the name of the remote URL, 
// and a parameter for the callback function (which the callback function will use to determine which function was called)
// eg:
// var x = new AjaxRemote("myRemoteUrl.asp","setAuth");
//
// then invoke a remote method with parameters like this:
//
// x.setParams = 'Fn=GetCoyDetails&Coy=U&Ndx='+iIndex;
// x.executeRemote();
//
// on return, conponent x calls the function AjaxCallBack(callBackFnName, req.responseXML);
//
// so write a function called AjaxCallBack(), which checks the callBackFnName parameter, 
// and deals with the data in req.responseXML. (Sample AjaxCallBack() function included)
//
// To activate Debug mode (which text-boxes the returned XML as text) code like this:
//	myAjax.setParams(params);
//	myAjax._debugMode = true; <== this line turns DEBUG mode ON. It is false by default
//	myAjax.executeRemote();
//
// On the server side, if you want to return < or > as part of the return_value, then this needs to be encoded
// eg: to return: "<BR>" use: "&lt;BR&gt;"
// special XML character encoding is:
// 		< = &lt;
// 		> = &gt;
// 		& = &amp;
//		" = &quot;
//		' = &apos;


function AjaxRemote(remoteURL, callBackFnName){
	if (remoteURL != undefined){
		this._remoteURL = remoteURL;
	}
	if (callBackFnName != undefined){
		this._callBackFnName = callBackFnName;
	}
	this._params = "";
}
// set defaults
AjaxRemote.prototype._remoteURL = "AjaxRemote.asp";
AjaxRemote.prototype._callBackFnName = "default";

//var to hold an instance of the XMLHTTPRequest object
AjaxRemote.prototype._request = undefined;

//var to hold debug mode
AjaxRemote.prototype._debugMode = false;

// Public Functions
AjaxRemote.prototype.setParams = function(params){
	this._params = params;
}
AjaxRemote.prototype.executeRemote = function(){
	this._request = getXMLHTTPRequest();

	if (this._request){
		var _this = this;
		this._request.onreadystatechange=function(){_this._onRSChange(_this._callBackFnName)};
		this._request.open("POST",this._remoteURL,true);
		this._request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		this._request.send(this._params);
	}
}
// Private Callback Function
AjaxRemote.prototype._onRSChange = function(callBackFnName){
	if (this._request.readyState==4){
		if (this._request.status==200){
			if (this._request.responseXML != null){
				//Debug function to show returned result as text
				if (this._debugMode){
					alert(this._request.responseText);
					//debugWindow(this._request.responseText);
				}
				AjaxCallBack(callBackFnName, this._request.responseXML);
				//this._callBackFnName.call(this);
			} else {
				alert("Callback Error: responseXML received was null!");
			}
		} else {
			alert("Callback Error: Page Load Error: " + this._request.status);
		}
		delete this._request;
	}
}

// Function to get Request object
function getXMLHTTPRequest(){
	var xReq = null;
	if (window.XMLHttpRequest){
		xReq = new XMLHttpRequest();
	} else if (window.ActiveXObject){
		try {
			xReq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xReq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xReq = null;
			}
		}
	}
	return xReq;
}	

function URLEncode(inText){
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = inText;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} 
	return encoded;
}

// =========================== DEBUG CODE START =========================================//
var DebugText;
var DebugWindow;

function debugWindow(textToShow) {
	var w = 480; // default screen width
	var h = 340; // default screen height
	var popWidth = 395; // popup window width
	var popHeight = 155; // popup window height 
	if (document.all || document.layers || document.getElementById) {
		w = screen.availWidth; h = screen.availHeight;
	}
	var leftPos = (w-popWidth)/2, topPos = (h-popHeight)/2;
	DebugWindow = window.open("","","alwaysRaised,dependent,title,width=" + popWidth +",height="+popHeight);
	DebugWindow.moveTo(leftPos,topPos);
	DebugWindow.focus();
	DebugText = textToShow;
	setTimeout("showDebugWindow('Ajax Return Text')", 50);
}

function showDebugWindow(sTitle){
	sText = DebugText
	var output = "<HTML><HEAD><TITLE>" + sTitle + "</TITLE>";
	output +="<BODY>";
	output +=sText;
	output +="</BODY></HTML>";

	DebugWindow.document.write(output);
	DebugWindow.document.close();
}
// =========================== DEBUG CODE END =========================================//



