﻿function XmlHttpRequest(url, parameters)
{
	// Private Vars
	var rq = null;
	var isIE = (window.ActiveXObject ? 1 : 0);	
	
	// Public Properties
	this.url			  = (typeof(url) == 'undefined' ? null : url);	
	this.parameters		  = (typeof(parameters) == 'undefined' ? null : parameters);
	this.requestResponse  = null;
	this.async			  = true;
	this.caller			  = null;
	this.request		  = null;
	this.target			  = null;
	this.onComplete		  = null;
	
	// Public Methods
	this.create = _create;
	
	
	// Internal Handling
	function _create()
	{		
		var xml = null; // Response
				
		if(isIE) // Internet Explorer
			rq = new ActiveXObject('MSXML2.XmlHttp');		
		else // Other
			rq = new XMLHttpRequest();	
												
		rq.open("POST", this.url, this.async);			
		if(this.async) rq.onreadystatechange = this.requestResponse;
		rq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');			
		rq.send(this.parameters);		
		this.request = rq;
	}				
}

XmlHttpRequest.prototype.setLoader = __loader;
XmlHttpRequest.prototype.setContent = __setContent;

function __setContent(text)
{
	if(typeof(this.target) != 'undefined' && this.target != null)
		this.target.innerHTML = text;
}

function __loader()
{	
	if(typeof(this.target) != 'undefined' && this.target != null)
	{
		var restore = 0; var previous = '';
		// Fix position property
		if(this.target.style.position == '')
		{
			var previous = this.target.style.position;
			this.target.style.position = 'relative';
			restore = 1;
		}
		
		// Get Height
		var height = this.target.offsetHeight - 30;
		
		// Restore position property
		if(restore)
			this.target.style.position = previous;
		
		// Create Loader
		var table = '<table width="100%" style="height: ' + height + 'px;" bgColor="#F9F9F9">' +
					'<tr><td height="100%" align="center"><img src="../images/loading.gif" border="0" />' + 
					'</td></tr></table>';
					
		// Set Loader
		this.target.innerHTML = table;
	}		
}

// Get Text
function __nodeText(xml, xPath)
{	
	var text = '';
	if(isOpera)
	{
		var nodes = xml.getElementsByTagName(xPath);
		if(nodes.length > 0)
			text = nodes[0].firstChild.nodeValue;		
	}
	else
	{
		var node = xml.selectSingleNode('//' + xPath);		
		text = isIE ? node.text : node.textContent;
	}
	return ___getText(text);
}

function ___getText(text)
{
	var s = document.createElement('div'); 
	s.innerHTML = text;
	if(s.getElementsByTagName('div').length > 0)
		return s.getElementsByTagName('div')[1].innerHTML; 
	else
		return s.innerHTML;
}