/*
Common XMLHttpRequest Object Properties
---------------------------------------
onreadystatechange	Event handler for an event that fires at every state change
readyState 
					0 = uninitialized
					1 = loading
					2 = loaded
					3 = interactive
					4 = complete
responseText 		String version of data returned from server process
responseXML 		DOM-compatible document object of data returned from server process
status 				Numeric code returned by server, such as 404 for "Not Found" or 200 for "OK"
statusText 			String message accompanying the status code
*/

function uniqueID()
{
	var prefix="UID_";
	var uid=prefix+Math.floor(Math.random()*1000);
	while (document.getElementById(uid))
		uid=prefix+Math.floor(Math.random()*1000);
		
	return uid;
}
/* Promenna Br se nastavuje ve functions.js */
var ie=Br.IE;

function XMLHttpRequest()
{
	this.index		  = uniqueID();
	this.readyState   = 0;
	this.status		  = 200;
	this.statusText   = "OK";
	this.responseXML  = null;
	
	this.onreadystatechange=function(){
		return false;
	}
	
	this.open=function(method,url){
		var lname="XMLHttpRequest_"+this.index;
		this.iframeEl=document.createElement("iframe");
		this.iframeEl.setAttribute("id",lname);
		this.iframeEl.setAttribute("name",lname);
		this.iframeEl.setAttribute("src","about:blank");
		
		//this.iframeEl.style.display="none";
		this.iframeEl.style.width=1;
		this.iframeEl.style.height=1;
		this.iframeEl.style.visibility="hidden";

		document.body.appendChild(this.iframeEl);

		if (ie)
			this.iframe=document.frames[lname];
		else
			this.iframe=window.frames[lname];

		this.iframe.document.open();
		this.iframe.document.write("<html><head></head><body><form id='formdata' name='formdata' action='"+url+ (url.indexOf("DontParse") > -1 ? "" : "?DontParse=true") + "' method='post'><input id='inputdata' name='inputdata' /></form></body></html>");
		this.iframe.document.close();
		
		this.form=this.iframe.document.getElementById("formdata");
		this.inputdata=this.iframe.document.getElementById("inputdata");
		this.readyState = 1;
	}

	this.send=function(data){
		this.iframeEl.thisObject=this;

		if (ie)
			this.iframeEl.attachEvent("onload",this.load);
		else
			this.iframeEl.addEventListener("load",this.load,false);

		this.inputdata.value=data;
		
		if (ie)
			while (this.iframeEl.readyState!="complete") {}
		try
		{
			this.form.submit();
		}
		catch (e)
		{
			this.status = 404;
			this.statusText = "Not Found";
		}
	}

	this.load=function()
	{
		if (ie)
			event.srcElement.thisObject.iframeEl.detachEvent("onload",event.srcElement.thisObject.load);
		else
			this.removeEventListener("load",this.thisObject.load,false);

		if (ie)
		{
			event.srcElement.thisObject.readyState = 4;
			event.srcElement.thisObject.status = 200;
			event.srcElement.thisObject.statusText = "OK";
			
			if (typeof (event.srcElement.thisObject.iframe.dataobject)!="undefined")
			{
				event.srcElement.thisObject.ret_dataobject=event.srcElement.thisObject.iframe.dataobject;
				event.srcElement.thisObject.onreadystatechange(event.srcElement.thisObject.ret_dataobject);
			}
			else
			{
				event.srcElement.thisObject.responseXML = new ActiveXObject("Microsoft.XMLDOM");
				event.srcElement.thisObject.responseXML.loadXML(event.srcElement.thisObject.iframe.document.body.innerHTML);
				event.srcElement.thisObject.onreadystatechange(event.srcElement.thisObject.responseXML);
			}
		}
		else
		{
			this.thisObject.readyState = 4;
			this.thisObject.status = 200;
			this.thisObject.statusText = "OK";
		
			if (typeof (this.thisObject.iframe.dataobject)!="undefined")
			{
				this.thisObject.ret_dataobject=this.thisObject.iframe.dataobject;
				this.thisObject.onreadystatechange(this.thisObject.ret_dataobject);
			}
			else
			{
				this.thisObject.responseXML = document.implementation.createDocument("","",null);
				this.thisObject.responseXML.loadXML(this.thisObject.iframe.document.body.innerHTML);
				this.thisObject.onreadystatechange(this.thisObject.responseXML);
			}
		}

		if (ie)
		{
			event.cancelBubble=true;
			event.srcElement.src="about:blank";
		}
	}
}

function XMLHttpRequest2()
{
	this._callback;
	this.open=function(method,url)
	{
		this.iframe=document.createElement("iframe");
		this.iframe.setAttribute("src","about:blank");
		this.iframe.style.width=1;
		this.iframe.style.height=1;
		this.iframe.style.visibility="hidden";
		document.body.appendChild(this.iframe);

		this.iframedoc=this.iframe.contentWindow.document;
		this.iframedoc.open();
		this.iframedoc.write("<html><head></head><body><form id='formdata' name='formdata' action='"+url+"' method='"+method+"'></form></body></html>");
		this.iframedoc.close();
	}

	this.send=function(data,callback)
	{
		this.iframe._callback=callback;
	
		AddEventHandler(this.iframe,"load",this.frLoad);

		/*try
		{*/
			var formdata=this.iframedoc.getElementById("formdata");
			formdata.innerHTML=data;
			formdata.submit();
		/*}
		catch (e)
		{
			this.status=404;
			this.statusText="Not Found";
		}*/
	}
	
	this.frLoad=function(e)
	{
		var iframe=eventSrcElement(e);
		var iframedoc=iframe.contentWindow.document;
		iframe._callback(iframedoc);
		iframe.removeNode(true);
	}
	
	function eventSrcElement(e)
	{
		if (typeof(e.srcElement)=="undefined")
			return e.target;
		else
			return e.srcElement;
	}
}
