var getTransferObject = function(){	return null;	};
if(window.ActiveXObject){
	// Check if the browser has support for ActiveXObject (IE Usually)  
	try{ 
		// Check for the new version of XMLHttp compoment  
		var x=  new ActiveXObject("MSXML2.XMLHTTP"); 
		getTransferObject = function(){ return new ActiveXObject("MSXML2.XMLHTTP"); }
	}catch(_ex){ 
		try{ // Check for late version of XMLHTTP compoment  
			var x = new ActiveXObject("Microsoft.XMLHTTP"); 
			getTransferObject = function(){ return new ActiveXObject("Microsoft.XMLHTTP"); }
		}catch(ex){
			// Otherwise the version of IE is too old  
		} 
	} 
}else if(window.XMLHttpRequest){
	// XMLHttpRequest object supported by Opera, Firefox and Safari - may too in IE 7.  
	getTransferObject = function(){ return new XMLHttpRequest(); }
}

/*
	* make an "Ajax Request".
	* @params: 
		- url : contains the target of the request.
		- params: Hash Table that contains a data that send with the request.
		- aOptions: Associative array that contains an optional property: method of sendinge, 
			2 optional function: 
				- onSuccess(param: RequestObject): will dispatch when the request get a "true" response (status-code=200)
				- onFailure(param: RequestObject): will dispatch when the request get a "false" response (status=code isn't 200)
			and an Hash Table of http headers named: httpHeaders
*/
function doAjaxRequest(url, params, aOptions){
	// get transfer object
	var oReq = getTransferObject();
	if(!oReq){
		// Check for XMLHttpRequest
		throw "Download the newer Firefox or upgrade your browser please.";
		return false;
	}

	// Add HTTP headers to the message
	for(var header in aOptions.httpHeaders){
		oReq.setRequestHeader(header,aOptions.httpHeaders[header]);
	}

	oReq.onreadystatechange = function(){
		if(oReq.readyState==4){
			if(oReq.status==200){
				if(aOptions.onSuccess) aOptions.onSuccess(oReq, aOptions.onSuccessParams);
			}else{
				if(aOptions.onFailure) aOptions.onFailure(oReq);
			}
		}
	}

	// make parameters string
	var _params = [];
	for(var p in params){
		_params.push(p+"="+encodeURIComponent(params[p]));
	}
	_params = _params.join("&");

	// Default send method: GET
	var method = aOptions.method?aOptions.method:"GET",
		bodyPost= null;


	if(method=="POST"){
		oReq.open(method, url, true);
		oReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		bodyPost = _params; // POST data send into the request
	}else{
		oReq.open(method, url+"?"+_params, true); // GET data send as part of the url
	}
	//send request
	oReq.send(bodyPost);
}

// mozXPath [http://km0ti0n.blunted.co.uk/mozxpath/] km0ti0n@gmail.com
// Code licensed under Creative Commons Attribution-ShareAlike License 
// http://creativecommons.org/licenses/by-sa/2.5/
if( document.implementation.hasFeature("XPath", "3.0") )
{
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++)
		{
			aResult[i] =  aItems.snapshotItem(i);
		}
		
		return aResult;
	}
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 )
		{
			return xItems[0];
		}
		else
		{
			return null;
		}
	}

	Element.prototype.selectNodes = function(cXPathString)
	{
		if(this.ownerDocument.selectNodes)
		{
			return this.ownerDocument.selectNodes(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

	Element.prototype.selectSingleNode = function(cXPathString)
	{	
		if(this.ownerDocument.selectSingleNode)
		{
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

}
