﻿/* 	
	------------------------------------------------------------------------------
	/framework/classes/js/xmlrequest.php
	
	Is een simpel object dat omgezet wordt naar xml en dan via XmlHttpRequest
	doorgestuurd wordt naar de server
	
  ------------------------------------------------------------------------------
*/



// RPC Klasse
// ------------------------------------------------------------------------------
var oRPCRequest = [];
var oRPCSuccessFunction = [];
var oRPCFailedFunction = [];

function XmlRequest() {

	// standard properties
	this.url = webroot + "/rpc.php";	 // de url waar we naar gaan posten
	
	if (typeof(appName) != "undefined")
		this.appName = appName;
	else
		this.appName = "";
	if (typeof(appType) != "undefined")
		this.appType = appType;
	else
		this.appType = "";
		
	this.functionName = "";
	this.params = null;
	this.onsuccess = "";
	this.onfailed = "";
	this.callingObject = "";
	
	this.requestText = "";
	
	// methods
	this.initRequest = XmlRequest_initRequest;
	this.start = XmlRequest_start;
}


function XmlRequest_start() {
	// eerst de xml samenstellen die we naar de server gaan posten - dit kan ook met DOM, maar dit is nog simpeler eigenlijk
	xml  = "";
	xml += "<request>\n";
	xml += "  <app_name>" + this.appName + "</app_name>\n";
	xml += "  <app_type>" + this.appType + "</app_type>\n";
	xml += "  <function_name>" + this.functionName + "</function_name>\n";
	xml += "  <calling_object>" + this.callingObject + "</calling_object>\n";
	xml += "  <params>\n";
	for (key in this.params) {
		// for some reason, to get all fancy stuff going, prototype.js breaks the simple associative array
		if (String(this.params[key]).indexOf(") {") == -1) 
			xml += '    <' + key + '><![CDATA[' + this.params[key] + ']]></' + key + '>\n';
	}
	xml += "  </params>\n";
	xml += "</request>\n";
	
	encodedxml = encodeURIComponent(xml);
	this.requestText = xml;
	//alert (encodedxml + "\n\n------------\n\n" + encodeURIComponent(xml));
	//alert ("this is the xml now : " + xml);
	//alert ("this will the xml be: " + encodeURI(xml));
	//alert("rpc_start");
	
	
	// we use a javascript feature here called "inner functions"
	// using these means the local variables retain their values after the outer function
	// has returned. this is useful for thread safety, so
	// reassigning the onreadystatechange function doesn't stomp over earlier requests.
	function ajaxBindCallback(){
		
		if (ajaxRequest.readyState == 4) {
			debug.write("ajaxBindCallback readystate is now 4...", "XmlRequest_start");	
			
			oXMLResponse = new XmlResponse();
			oXMLResponse.document = ajaxRequest.responseXML;
			oXMLResponse.responseText = ajaxRequest.responseText;
			//alert(ajaxRequest.responseText);
			oXMLResponse.start();
			
			
			//var obj = oRPCRequest[oRPCRequest.length-1];
			
			ajaxCallback(oXMLResponse);
			//eval(ajaxCallback + "(oXMLResponse)");
			
			/*if (ajaxRequest.status == 200) {
				if (ajaxCallback){
					ajaxCallback(ajaxRequest.responseXML);
				} else {
					alert('no callback defined');
				}
			} 
			else {	
				alert("There was a problem retrieving the xml data:\n" + ajaxRequest.status + ":\t" + ajaxRequest.statusText + "\n" + ajaxRequest.responseText);
			}
			*/
		}
	}
	
	
	// use a local variable to hold our request and callback until the inner function is called...
	var ajaxRequest = null;
	var ajaxCallback = this.onsuccess;
	
	// bind our callback then hit the server...
	if (window.XMLHttpRequest) {
		// moz et al
		
		ajaxRequest = new XMLHttpRequest();
		ajaxRequest.onreadystatechange = ajaxBindCallback;
		ajaxRequest.open("POST", this.url, true); 
		//ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
		ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=iso-8859-1');
		ajaxRequest.send("xml=" + encodedxml);	
		
	} 
	else if (window.ActiveXObject) {
		// ie
		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		if (ajaxRequest) {
			ajaxRequest.onreadystatechange = ajaxBindCallback;
			ajaxRequest.open("POST", this.url, true); 
			//ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
			ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=iso-8859-1');
	
			ajaxRequest.send("xml=" + encodedxml);
		}
	}
	debug.write("ajaxRequest sent", "XmlRequest_start");
	
	
	// TODO: een timeout starten die een functie oproept die na een bep. tijd gaat checken of de request al state 4 heeft
	//       indien state niet 4 kan de this.onfailed functie opgeroepen worden, zodat de klant een foutboodschap kan krijgen	
 }


// initialization of asynchronous javascript function		
function XmlRequest_initRequest() { 
	var oXmlHttp;
	/*@cc_on 
	@if (@_jscript_version >= 5) 
		try { 
			oXmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
		} catch (e) { 
			try { 
				oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
			} 
			catch (e) { 
				oXmlHttp = false; 
			} 
		} 
	@else 
		oXmlHttp = false; 
	@end @*/  
	if (!oXmlHttp && typeof XMLHttpRequest != 'undefined') { 
		try { 
			oXmlHttp = new XMLHttpRequest(); 
		} 
		catch (e) { 
			oXmlHttp = false; 
		} 
	} 
	return oXmlHttp;
} 







