﻿//
// easyAjax  :
// Author    : Carlos A. Leguizamón
// Company   : Classic Software, Inc. (Widegroup Interactive Outsourcing)
// Versión   : 1.0
// Copyright : (C)2006
//
//<![CDATA[
easyAjax = function() {
	this.createHttpRequest = function() {
		var oHttpReq = false;
		if (window.XMLHttpRequest) { // if Mozilla, Safari etc
			oHttpReq = new XMLHttpRequest();
			if (oHttpReq.overrideMimeType)
				oHttpReq.overrideMimeType('text/xml');
		} else if (window.ActiveXObject) { // if IE
			try {
				oHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					oHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (E) { oHttpReq = false; };
			}
		}
		return oHttpReq;
	};
	//
	this.openXml = function(szUrl, callBackFunction) {
		var m_oAjax = this.createHttpRequest();
		if (!m_oAjax)
			alert("ERROR:\nCannot create HttpRequest object.\nVerify your navigator config and try again.");
		else {
			m_oAjax.onreadystatechange = function() {
				if (m_oAjax.readyState == 4) {
					if (m_oAjax.status == 200) {
						callBackFunction(m_oAjax.responseXML);
					} else {
						alert('Have troubles with the request.');
					}
				}
			};
			m_oAjax.open("GET", szUrl, true);
			m_oAjax.send(null);
		}
	};
	//
	this.openText = function(szUrl, callBackFunction) {
		var m_oAjax = this.createHttpRequest();
		if (!m_oAjax)
			alert("ERROR:\nCannot create HttpRequest() object.\nVerify your navigator config and try again.");
		else {
			m_oAjax.onreadystatechange = function() {
				if (m_oAjax.readyState == 4) {
					if (m_oAjax.status == 200) {
						callBackFunction(m_oAjax.responseText);
					} else {
						alert("Have troubles with the request.");
					}
				}
			};
			m_oAjax.open("GET", szUrl, true);
			m_oAjax.send(null);
		}
	};
	//
}
//
// Modifico los prototipos para HttpRequest //
if (typeof XMLDocument != "undefined") { // Para Mozilla, Firefox, Sfari, etc...
	Element.prototype.getAttribute = function(szAttributeName) {
		if (this.attributes.getNamedItem(szAttributeName).value)
			return this.attributes.getNamedItem(szAttributeName).value;
		return "ERROR: invalid attribute name";
	};
	Element.prototype.innerText = function() {
		return this.textContent;
	};
	Element.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
	Document.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
	XMLDocument.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
	//
	// Creo la función 'selectSingleNode' dentro del prototypo de XMLDocument y Element
	XMLDocument.prototype.selectSingleNode = function(szXPath) {
		var xPathResult = this.evaluate(szXPath, this, this.createNSResolver(this.documentElement), 9, null);
		if (xPathResult.singleNodeValue) {
			return xPathResult.singleNodeValue;
		}
		return (new DOMParser()).parseFromString("<empty/>", "text/xml").documentElement;
	};
}
//]]>
