/* 	
	------------------------------------------------------------------------------
	/framework/classes/js/xmlresponse.js
	
	Interpreteert de resultaten(xml) die we terugkrijgen van de RPC calls en zet
	ze om in een gemakkelijk te gebruiken object
  ------------------------------------------------------------------------------
*/

function XmlResponse() {
	// properties
	this.document 		= null;
	
	
	this.statusCode 	= null; // statuscode
	this.status 		= null; // statusbericht (altijd engels)
	this.message 		= new Array(); // tabel van messages (associatieve tabel met als key de taal)
	this.data			= new Array(); // tabel met de data erin
	this.callingObject  = null; // eventueel het object waar er iets mee gedaan moet worden (b.v. als een dropdownbox een rpc call heeft gedaan)
	this.responseText   = "";
	
	this.numDataRows = 0;
	this.numDataCols = 0;
	
	// methods
	this.start = XmlResponse_start;
}

function XmlResponse_start() {
	this.status = -1;
	//alert("msg: " + this.message);
	
	
	// oResponse is een tabel van alle tags die de naam response hebben - bij ons dus maar 1 element in die tabel -> oResponse[0]
	//alert("d: " + this.document);
	//alert("XmlResponse = " + this.document);;
	//alert("response: " + this.document.toString());
	
	var oResponse = this.document.getElementsByTagName("response");
	//alert(oResponse + " - " + oResponse[0]);
	
	if (oResponse[0] == null) {
		//alert("this is an empty response");
		this.statusCode = 3000;
		this.status = "EMPTY_XML_RESPONSE";
		this.message["nl"] = "De XmlRequest is mislukt!";
		this.message["en"] = "The XmlRequest has failed!";
		
	}
	else {
	
		// met de for-lus gaan we alle tags (childnodes) af die onder de response zitten: statuscode, status, message, ...
		for (i=0; i < oResponse[0].childNodes.length; i++) {
			// alleen nodetype = 1 -> tags moeten we hebben (andere zijn literalcontrols b.v. in sommige browsers)
			if (oResponse[0].childNodes[i].nodeType != 1) continue; 
			
			// oTag is de childtag die we bereikt hebben (statuscode, status, message, ...)
			oTag = oResponse[0].childNodes[i];
			
			//alert(oTag.nodeName + " - " + oTag.firstChild.nodeValue);
			
			// hier gaan we onze properties invullen (die kunnen dan uitgelezen worden door de applicatie
			
			// statuscode is een numerieke value
			if (oTag.nodeName == "statuscode") {
				if (oTag.firstChild) {
					this.statusCode = oTag.firstChild.nodeValue;
				}
			}
			// status is een tekstvalue (met een engelse tekst)
			else if (oTag.nodeName == "status") {
				if (oTag.firstChild) {
					this.status = oTag.firstChild.nodeValue;
				}
			}
			// callingObject is een tekstvalue 
			else if (oTag.nodeName == "calling_object") {
				if (oTag.firstChild) {
					this.callingObject = oTag.firstChild.nodeValue;
				}
			}
			// number of data rows
			else if (oTag.nodeName == "num_data_rows") {
				if (oTag.firstChild) {
					this.numDataRows = oTag.firstChild.nodeValue;
				}
			}
			// number of data cols
			else if (oTag.nodeName == "num_data_cols") {
				if (oTag.firstChild) {
					this.numDataCols = oTag.firstChild.nodeValue;
				}
			}
			// message bevat een aantal messages, verschillend per taal
			else if (oTag.nodeName == "message") {
				for (j=0; j < oTag.childNodes.length; j++) {
					// enkel tags
					if (oTag.childNodes[j].nodeType != 1) continue; 
					// dit is de message
					oMessage = oTag.childNodes[j];
					// associatieve array opvullen
					if (oMessage != null && oMessage.firstChild != null) {
						this.message[oMessage.nodeName] = oMessage.firstChild.nodeValue;
					}
				}
			}
			else if (oTag.nodeName == "data") {
				num_records = 0;
				
				for (j=0; j < oTag.childNodes.length; j++) {
					if (oTag.childNodes[j].nodeType != 1) continue; 
					oData = oTag.childNodes[j];
					// elke record is een associatieve array
					aItems = new Array();
					
					for (k=0; k < oData.childNodes.length; k++) {
						if (oData.childNodes[k].nodeType != 1) continue; 
						oItem = oData.childNodes[k];
						// associatieve array opvullen
						//alert(oItem.nodeName + " - " + oItem.firstChild.nodeValue);
						if (oItem.firstChild) {
							aItems[oItem.nodeName] = oItem.firstChild.nodeValue;
						}
						else {
							aItems[oItem.nodeName] = "";
						}
					}
					
					// we kunnen j niet gebruiken voor de index - j wordt ook opgehoogd als het element een literal is
					// daarom num_records, als het een literal is komen we hier toch niet door de continue hierboven
					this.data[num_records] = aItems;
					num_records++;
					
				}
			}
		}
	}
	
}