/*----------------------------------------------------------------------------\
|                             AJAXHelper class 0.2                            |
|-----------------------------------------------------------------------------|
| Created 2007-04-19 | jmartinez@adasasistemas.com       | Updated 2008-06-16 |
\----------------------------------------------------------------------------*/

//
function AJAXHelper(callback, params, xmlmode)
{
	// properties
	this.xmlHttp = this.getXmlHttpObject();
	this.callback = callback;
	this.parameters = params;
	this.useXML = xmlmode;
}

//
AJAXHelper.prototype.getXmlHttpObject = function()
{
	var objXMLHttp = null;
	if (window.XMLHttpRequest)
	{
		objXMLHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
};

//
AJAXHelper.prototype.request = function(urlin, cache)
{
	if (this.xmlHttp == null)
	{
		alert("AJAXHelper: No se pudo obtener el objeto XMLHTTP");
		return;
	}

	var url = urlin;
	if (!cache)
	{
		var nexus = (url.indexOf('?') == -1) ? '?' : '&';
		url += nexus + "nocache=" + new Date().getTime();
		
	}
	
	var obj = this;

	this.xmlHttp.onreadystatechange = function() { obj.onResponseReceived() };
	this.xmlHttp.open("GET", url, true);
	this.xmlHttp.send(null);
};

//
AJAXHelper.prototype.onResponseReceived = function()
{
	if ((this.xmlHttp.readyState==4) || (this.xmlHttp.readyState=="complete"))
	{
		var response = this.useXML ? this.xmlHttp.responseXML : eval('(' + this.xmlHttp.responseText + ')');
		this.callback.call(this, response, this.parameters);
	}
};


// --- XML helper functions

// constants
var ELEMENT_NODE_TYPE = 1;

//
function getChildren(node, childName)
{
	var result = new Array();
	var children = node.childNodes;
	for (var i=0; i<children.length; i++)
	{
		var child = children[i];
		if ((child.nodeType == ELEMENT_NODE_TYPE) && (!childName || (child.nodeName == childName)))
		{
			result.push(child);
		}
	}
	return result;
}

//
function getChild(node, childName)
{
	var arr = getChildren(node, childName);
	var result = (arr.length == 0) ? null : arr[0];
	return result;
}
