//
// Just Like Ed AJAX Response class
// By eFlare (Just Like Ed.net)
//
function JLEAJAXRepsonse(req) {
	var m_ResponseText;
	var m_ResponseXML;
	var m_ResponseInfo;
	var m_ResponseData;
	var m_ResponseInfoType;
	var m_ResponseInfoContext;
	
	try {
		m_ResponseText = req.responseText;
		m_ResponseXML = req.responseXML;
		m_ResponseHXML = new JLEXMLNode(m_ResponseXML);
		m_ResponseInfo = m_ResponseHXML.GetChildNode("ResponseInfo");
		m_ResponseData = m_ResponseHXML.GetChildNode("ResponseData");
		m_ResponseInfoType = m_ResponseInfo.GetChildNode("Type").GetValue();
		m_ResponseInfoContext = m_ResponseInfo.GetChildNode("Context").GetValue();
	} catch(e) { }
		
	this.IsValid = function() {
		try {
			return (m_ResponseXML.getElementsByTagName("JLEAJAXResponse").length == 1);
		} catch(e) {
			return false;
		}
	};
	
	this.GetText = function() {
		return m_ResponseText;
	};
		
	this.IsError = function() {
		return (m_ResponseInfoType == "Error");
	};
	
	this.GetErrorMessage = function() {
		return m_ResponseData.GetChildNode("Message").GetValue();
	};
	
	this.GetErrorCode = function() {
		return m_ResponseData.GetChildNode("Code").GetValue();
	};
	
	this.GetResponseType = function() {
		return m_ResponseInfoType;
	};
	
	this.GetResponseContext = function() {
		return m_ResponseInfoContext;
	};
	
	this.GetData = function() {
		return m_ResponseData;
	};

}

function JLEXMLNode(xmlobject) {
	var m_Object = xmlobject;
	
	this.GetNodeName = function() {
		return m_Object.nodeName;
	};
	
	this.GetValue = function() {
		return _getInnerText(m_Object);
	};
	
	this.GetChildNode = function(nodename) {
		return new JLEXMLNode(m_Object.getElementsByTagName(nodename)[0]);
	};
	
	this.GetChildNodes = function(nodename) {
		var arr = [];
		var elements = m_Object.getElementsByTagName(nodename);
		for(var i = 0; i < elements.length; i++) {
			arr[i] = new JLEXMLNode(elements[i]);
		}
		return arr;
	};
	
	this.GetAttribute = function(attrname) {
		return m_Object.getAttribute(attrname);
	};
	
	this.CountChildNodes = function() {
		return m_Object.childNodes.length;
	};
	
	function _getInnerText(node) {
		if (typeof node.text != 'undefined') {
			return node.text;
		} else if (typeof node.textContent != 'undefined') {
			return node.textContent;
		} else if (typeof node.innerText != 'undefined') {
			return node.innerText;
		} else {
			switch (node.nodeType) {
				case 3:
				case 4:
					return node.nodeValue;
					break;
				case 1:
				case 11:
					var innerText = '';
					for (var i = 0; i < node.childNodes.length; i++) {
						innerText += _getInnerText(node.childNodes[i]);
					}
					return innerText;
					break;
			default:
				return '';
			}
		}
	}
	
};