/**	<name>IAjaxProcess</name>                                               **/
/**	<summary>Ajax Processor</summary>                                       **/

/**
 * Couche Ajax Bas Niveau
 */
function getDomDocumentPrefix() {
	if (getDomDocumentPrefix.prefix)
		return getDomDocumentPrefix.prefix;
	
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var o;
	for (var i = 0; i < prefixes.length; i++) {
		try {
			// try to create the objects
			o = new ActiveXObject(prefixes[i] + ".DomDocument");
			return getDomDocumentPrefix.prefix = prefixes[i];
		}
		catch (ex) {};
	}
	
	throw new Error("Could not find an installed XML parser");
}

function getXmlHttpPrefix() {
	if (getXmlHttpPrefix.prefix)
		return getXmlHttpPrefix.prefix;
	
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var o;
	for (var i = 0; i < prefixes.length; i++) {
		try {
			// try to create the objects
			o = new ActiveXObject(prefixes[i] + ".XmlHttp");
			return getXmlHttpPrefix.prefix = prefixes[i];
		}
		catch (ex) {};
	}
	
	throw new Error("Could not find an installed XML parser");
}


/**
 * Xml Http Factory
 */
function XmlHttp() {}

XmlHttp.create = function () {
	try {
		if (window.XMLHttpRequest) {
			var req = new XMLHttpRequest();
			
			// some versions of Moz do not support the readyState property
			// and the onreadystate event so we patch it!
			if (req.readyState == null) {
				req.readyState = 1;
				req.addEventListener("load", function () {
					req.readyState = 4;
					if (typeof req.onreadystatechange == "function")
						req.onreadystatechange();
				}, false);
			}
			
			return req;
		}
		if (window.ActiveXObject) {
			return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
		}
	}
	catch (ex) {}
	// fell through
	throw new Error("Your browser does not support XmlHttp objects");
};

/**
 * Xml Document Factory
 */
function XmlDocument() {}

XmlDocument.create = function () {
	try {
		// DOM2
		if (document.implementation && document.implementation.createDocument) {
			var doc = document.implementation.createDocument("", "", null);
			
			// some versions of Moz do not support the readyState property
			// and the onreadystate event so we patch it!
			if (doc.readyState == null) {
				doc.readyState = 1;
				doc.addEventListener("load", function () {
					doc.readyState = 4;
					if (typeof doc.onreadystatechange == "function")
						doc.onreadystatechange();
				}, false);
			}
			
			return doc;
		}
		if (window.ActiveXObject)
			return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument");
	}
	catch (ex) {}
	throw new Error("Your browser does not support XmlDocument objects");
};

// Create the loadXML method and xml getter for Mozilla
if (window.DOMParser &&
	window.XMLSerializer &&
	window.Node && Node.prototype && Node.prototype.__defineGetter__) {

	// XMLDocument did not extend the Document interface in some versions
	// of Mozilla. Extend both!
	XMLDocument.prototype.loadXML = 
	Document.prototype.loadXML = function (s) {
		
		// parse the string to a new doc	
		var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
		
		// remove all initial children
		while (this.hasChildNodes())
			this.removeChild(this.lastChild);
			
		// insert and import nodes
		for (var i = 0; i < doc2.childNodes.length; i++) {
			this.appendChild(this.importNode(doc2.childNodes[i], true));
		}
	};
	
	
	/*
	 * xml getter
	 *
	 * This serializes the DOM tree to an XML String
	 *
	 * Usage: var sXml = oNode.xml
	 *
	 */
	// XMLDocument did not extend the Document interface in some versions
	// of Mozilla. Extend both!
	XMLDocument.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
	Document.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
}

/*****************************************************************************/
/**  IAjaxProcess                                                           **/
/*****************************************************************************/
/**  This object was used to run ajax process.                              **/
/**  This object was created in order to separate Ajax Process & Functional **/
/**              process.                                                   **/
/** _url ..: http ressource url.                                            **/
/** _func .: the function process called in ajax call                       **/
/**                                                                         **/
/*****************************************************************************/
function IAjaxProcess(_url, _func) {
	this.url = _url;
	this.func = _func;
	this.xmlHttp = XmlHttp.create();
	this.run=_run;
}

/**
 * This method was used to execute specific functional process.
 */
function _run() {
	with(this) {
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = function () {
			if (xmlHttp.readyState == 4) {
				func(xmlHttp, xmlHttp.status);
			} 
		}
		// call in new thread to allow ui to update
		window.setTimeout(function () {xmlHttp.send(null);}, 10);
	}
}

function IAjaxServiceProcess(_url, _func, _joService) {
	this.url = _url;
	this.func = _func;
	this.service = _joService;
	
	this.setUrl = function (_url) {this.url = _url;}

	this.setFunc = function (_func) {this.func = _func;}

	this.setService = function (_service) {this.service = _service;}

	this.getUrl = function () {return this.url;}

	this.getService = function() {return this.service;}
	
	this.getFunc = function () {return this.func;}

	this.run = function _runService() {
		with (this) {
			var xmlHttp = XmlHttp.create();
	
			getService().notifyProgess(0);

			xmlHttp.open("GET", getUrl(), true);
			xmlHttp.onreadystatechange = function () {
			
				if (xmlHttp.readyState == 4) {
					var localFunc = getFunc();
					var oTempService = getService();
					oTempService.notifyProgess(10);
					localFunc(oTempService, xmlHttp, xmlHttp.status);
				} 
			}
			window.setTimeout(function () {xmlHttp.send(null);}, 10);
		}
	}

}

/*****************************************************************************/
/**  DOM Retrieve Process  ***************************************************/
/*****************************************************************************/

/**
 * This method was used to retrieve a child node value.
 * oParent .: The parent node.
 * sNodeName .: The child node name.
 */
function getChildNodeValue(oParent, sNodeName) {
	try {
		if (oParent == null || oParent == undefined || sNodeName == null || sNodeName == undefined) {
			return null;
		}
		var oChildNode = getChild(oParent, sNodeName);
		if(oChildNode == null || oChildNode == undefined) {
			return null;
		}
		return getNodeValue(oChildNode);
	} catch (e) {
		return null;
	}
}

/**
 * This method was used to retrieve a node value.
 * oNode .: The node
 */
function getNodeValue(oNode) {
	try {
		if(oNode == undefined || oNode == null) {
			return null;
		}
		if(oNode.firstChild == undefined || oNode.firstChild == null) {
			return null;
		}
		if(oNode.firstChild.nodeValue == undefined || oNode.firstChild.nodeValue == null) {
			return null;
		}
		return oNode.firstChild.nodeValue;
	} catch (e) {
		return null;
	}
}

/**
 * This method was used to retrieve an attribute value
 * oNode ..........: The node
 * sAttributeName .: The attribute node name.
 */
function getAttributeValue(oNode, sAttributeName) {
	try {
		if(oNode == undefined || oNode == null) {
			return null;
		}
		if(oNode.getAttribute(sAttributeName) == undefined) {
			return null;
		}
		return oNode.getAttribute(sAttributeName);

	} catch (e) {
		return null;
	}
}

/*
 * This method was used to retrieve a child node
 * If parent got more than one child node with the name, return first found node.
 * oParent ....: The parent node
 * sChildName .: The child node name
 */
function getChild(oParent, sChildName) {
	try {
		var oChild = oParent.getElementsByTagName(sChildName)[0];
		if (oChild == undefined) {
			return null;
		}
		return oChild;
	
	} catch(e) {
		return null;
	}
}		

/*
 * This method was used to retrieve a child node for a specific attribute value
 * If parent got more than one child node with the same name & the same attribute name & value, return first found node.
 * oParent .........: The parent node
 * sChildName ......: The child node name
 * sAttributeName ..: The attribute name
 * sAttributeValue .: The attribute value
 */
function getChildWithAttribute(oParent, sChildName, sAttributeName, sAttributeValue) {
	var k = 0;
	try {
		var oChildren = oParent.getElementsByTagName(sChildName);
		for (k=0; k<oChildren.length; k++) {
			var sValue = getAttributeValue(oChildren[k], sAttributeName);
			if(sValue == sAttributeValue) {
				return oChildren[k];
			}
		}
		return null;
	
	} catch(e) {
		return null;
	}
}

/*
 * This method was used to retrieve a child node for a specific attribute value
 * If parent got more than one child node with the same name & the same attribute name & value, return first found node.
 * oParent .........: The parent node
 * sChildName ......: The child node name
 * sAttributeName ..: The attribute name
 */
function getChildWithAnAttribute(oParent, sChildName, sAttributeName) {
	var k = 0;
	try {
		var oChildren = oParent.getElementsByTagName(sChildName);
		for (k=0; k<oChildren.length; k++) {
			var sValue = getAttributeValue(oChildren[k], sAttributeName);
			if(sValue != null) {
				return oChildren[k];
			}
		}
		return null;
	
	} catch(e) {
		return null;
	}
}

