﻿function Status(oXML)
{
	if (typeof oXML != "undefined")
		this.init(oXML);
}
Status.prototype.init = function(oXML)
{
	this.parseStatusResult(oXML.getData());
};
Status.prototype.getNodeValue = function(oNode)
{
	if (oNode)
	{
		//   textnode               cdata
		if (oNode.nodeType == 3 || oNode.nodeType == 4)
			return oNode.nodeValue;
		return arguments.callee(oNode.firstChild);
	}
	return false;
};
Status.prototype.getSiblingByName = function(oNode, sName)
{
	if (oNode)
	{
		while ((oNode.nodeType != 1 || (oNode.nodeName && oNode.nodeName.toLowerCase() != sName.toLowerCase())) && oNode.nextSibling)
			oNode = oNode.nextSibling;

		if (oNode.nodeType == 1 && (oNode.nodeName && oNode.nodeName.toLowerCase() == sName.toLowerCase()))
			return oNode;
	}
	return false;
};
Status.prototype.parseStatusResult = function(oDataXML)
{
	if (oDataXML)
	{
		var oReply = this.getSiblingByName(oDataXML.firstChild, "reply");
		if (oReply)
		{
			var sStatus = "error";
			if (typeof oReply.attributes[0] != "undefined" && oReply.attributes[0].name.toLowerCase() == "status")
				sStatus = oReply.attributes[0].value;
			this.success = sStatus == "OK";
			this.status = sStatus;
			this.message = unescape(this.trim(this.getNodeValue(this.getSiblingByName(oReply.firstChild, "message"))));
			this.content = this.xmlToObject(this.getSiblingByName(oReply.firstChild, "content"));
			return true;
		}
	}
	return false;
};
Status.prototype.trim = function(s)
{
	if (typeof s == "string")
	{
		for (var i = 0; i < s.length; ++i)
			if (s.charCodeAt(i) > 32)
			break;
		for (var j = s.length - 1; j >= 0; --j)
			if (s.charCodeAt(j) > 32)
			break;
		return s.substr(i, (j + 1) - i);
	}
	return s;
};
Status.prototype.xmlToObject = function(oXML)
{
	var oReturn = new Object();
	if (oXML.childNodes && oXML.childNodes.length > 0)
		for (var i = 0; i < oXML.childNodes.length; ++i)
		if (oXML.childNodes[i].nodeType == 3 || oXML.childNodes[i].nodeType == 4)
		return unescape(oXML.childNodes[i].nodeValue);
	else
		oReturn[oXML.childNodes[i].nodeName] = arguments.callee(oXML.childNodes[i]);
	return oReturn;
};

