// Save created XMLHttpRequest object for future use.
var cspXMLHttp = null;

// Set this variable to control is GET or POST method used to send request.
var cspUseGetMethod = false;

// Track which XMLHttpRequest object we are using
var cspMozilla = false;

function cspFindXMLHttp()
{
	if (cspXMLHttp != null && !cspMozilla) return cspXMLHttp;

	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
 	 	cspMozilla = false;
		try {
 	 		cspXMLHttp=new ActiveXObject("Msxml2.XMLHTTP");
 		} catch (e) {
			try {
				cspXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				cspXMLHttp=null;
			}
		}
		if (cspXMLHttp != null) return cspXMLHttp;
	@end @*/

	try {
		cspXMLHttp = new XMLHttpRequest();
 	 	cspMozilla = true;
		//cspXMLHttp.overrideMimeType("text/xml");
	} catch (e) {
		cspXMLHttp=null
	}

	return cspXMLHttp;
}

function cspIntHttpServerMethod(method, args)
{
	if (cspFindXMLHttp() == null) {
		err='Unable to locate XMLHttpObject.';
		if (typeof cspRunServerMethodError == 'function')
			return cspRunServerMethodError(err);
		alert(err);
		return null;
	}

	var arg;
	var i;
	var err;
	var cookie = cspFindCookie('CSPSESSIONID');

	// Setup the IFrame with the same session cookie
	//if (cookie != null ) {
	//	iframe.cookie = 'CSPSESSIONID=' + escape(cookie);
	//}

	var url = "%25CSP.Broker.cls";
	var n;
	var data = "WARGC=" + (args.length - 1) + "&WEVENT=" + method;
	for (i = 1; i < args.length; i++) {
		arg = args[i];
		if (typeof arg != "object") {
			// Convert boolean to Cache value before sending
			if (typeof arg == "boolean") arg = cspMakeBoolean(arg);

			if ((arg != null) && (arg.length > 32000)) {
				alert( 'Argument ' + i + ' too long.');
			}
			data = data + "&WARG_" + i + "=" + cspEncodeUTF8(arg);
		} else if (arg != null) {
			n = 0;
			for (var el in arg) {
				data = data + "&W" + i + "=" + cspEncodeUTF8(arg[el]);
				n = n + 1;
			}
			data = data + "&WLIST" + i + "=" + n;
		}
	}

	try {
		if (cspUseGetMethod && !cspMozilla) {
			cspXMLHttp.open("GET", url+"?"+data, false);
			cspXMLHttp.send();
		} else {
			cspXMLHttp.open("POST", url, false);
			cspXMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			cspXMLHttp.send(data);
		}
	} catch (e) {
		err='Http object request failed, unable to process HyperEvent.';
		if (typeof cspRunServerMethodError == 'function')
			return cspRunServerMethodError(err);
		alert(err);
		return null;
	}

	var result=cspXMLHttp.responseText;
	var lines = result.split("\r\n")
	var ok = 0;
	var len=lines.length;
	if (lines[len-1]=="") len=len-1;
	for (i=2; i<len; i++) {
		if (lines[i]=="#OK") {
			ok = i;
			break;
		}
	}

	if ((ok==0) || ((lines[1]!="#R") && (lines[1]!="#V"))) {
		err='Http object response incomplete or invalid.'+ok+","+lines[1];
		if (typeof cspRunServerMethodError == 'function')
			return cspRunServerMethodError(err);
		alert(err);
		return null;
	}

	if (ok > 2) {
		var js = new Array(lines[2]);
		for (i = 3; i < ok; i++) js.push("\r\n" + lines[i]);
		var bidding = new Function('CSPPage', js.join('\r\n'));
		bidding(self);
	}

	var result = null;
	if (lines[1] == "#R") {
		var result = "";
		if (ok+1 < len) {
			result = lines[ok+1];
			for (i = ok + 2; i < len; i++) {
				result = result + "\r\n" + lines[i];
			}
		}
	}

	// Allow caller to display a debug window.
	if ((typeof cspRunServerDebugWindow != "undefined") && cspRunServerDebugWindow) {
		var debugwin=window.open("","DebugWindow","scrollbars,resizable,height=400,width=700");
		if (debugwin.document.f == null) {
			debugwin.document.write("<html><head></head><body>");
			debugwin.document.title="HyperEvent Debug Window";
			debugwin.document.write('<form name="f">')
			debugwin.document.write('<br>Javascript<br><textarea name="js" cols="80" rows="12">')
			debugwin.document.write(js);
			debugwin.document.write('</textarea>');
			debugwin.document.write('<br>Return Value<br><textarea name="r" cols="80" rows="2">')
			debugwin.document.write(result);
			debugwin.document.write('</textarea>');
			debugwin.document.write('</form>')
			debugwin.document.write("</body></html>");
		} else {
			debugwin.document.f.js.value = js;
			debugwin.document.f.r.value = result;
		}
	}

	return result;
}

function cspHttpServerMethod(method)
{
	return cspIntHttpServerMethod(method,cspHttpServerMethod.arguments);
}

function cspSelectServerMethod(method)
{
	if (cspFindXMLHttp() != null) {
		return cspIntHttpServerMethod(method,cspSelectServerMethod.arguments);
	} else {
		return cspIntRunServerMethod(method,cspSelectServerMethod.arguments);
	}
}
