// 0.6 kry
// kry@vizkom.hu

var xmlhttp = new Object();

xmlhttp.READY_STATE_UNINITIALIZED=0;
xmlhttp.READY_STATE_LOADING=1;
xmlhttp.READY_STATE_LOADED=2;
xmlhttp.READY_STATE_INTERACTIVE=3;
xmlhttp.READY_STATE_COMPLETE=4;

xmlhttp.loader=function(onload, onerror, loadprogress, loadprogressstyle, loadprogresstext, loadprogressid) {
	this.req = null;
	this.progressId=(loadprogressid) ? loadprogressid : "loadProgress"; // az id -je annak a divnek ami kiirja hogy loading
	xmlhttp.currentLoader=this; // ??? ezt nem tudom mi lehet es miert kell...  
	this.onload= onload; // ez ami megfogja a visszjovo adatokat
	this.onerror=(onerror) ? onerror : this.defaultError; // ez a hibauzi
	this.loadProgress=(loadprogress) ? loadprogress : this.defaultloadProgress; // ez ami mutatja hogy loading avgy amit mutat
	this.loadProgressStyle=loadprogressstyle; // a css stylusa a loading nak 
	this.loadProgressText=loadprogresstext; // a szovege a loadingnak
	this.clearProgress=this.clearProgress; // ez torli azt hogy loading

	this.setloadparam=this.setLoadParam; // ez beallitja hogy honnan, milyen methoddal, milyen parameterekkel, milyen headerekkel
	this.load=this.load; // ez maga a betoltoke...
	this.celDiv = null; // kompatibiltas az elozo verziohoz
	// ezek this.valami = this.valami -k feltetelzhetoen feleslegesek... 

	this.onLoadCall=null;
}
xmlhttp.loader.prototype.setLoadParam=function(url, method, params, contentType) {
	this.url = url;
	this.method = method;
	this.params = params;
	this.contentType = contentType;
}
xmlhttp.loader.prototype.setdiv = function (aDiv) {
	this.celDiv = aDiv;
	this.onload = this.defaultLoad;
}
xmlhttp.loader.prototype.setonloadcall = function (fgv) {
	this.onLoadCall=fgv;
}
xmlhttp.loader.prototype.defaultLoad = function () {
	var div = document.getElementById(this.celDiv);
	var retText = this.req.responseText;
	div.innerHTML = retText;
	if (this.onLoadCall) {
		this.onLoadCall.call();
	}
}
xmlhttp.loader.prototype.load = function(url,method,params,contentType) {
	var turl = (url) ? url : this.url;
	var tmethod = (method) ? method : this.method;
	var tparams = (params) ? params : this.params;
	var tcontentType = (contentType) ? contentType : this.contetType;

	this.loadXMLDoc(turl, tmethod, tparams, tcontentType)
}
xmlhttp.loader.prototype.loadXMLDoc=function(url,method,params,contentType) {
	if (!method) {
		method="GET";
	}
	if(!contentType && method=="POST") {
		contentType='application/x-www-form-urlencoded; charset=ISO-8859-2';
		//contentType='application/x-www-form-urlencoded';
	}
	if (window.XMLHttpRequest) {
		this.req=new XMLHttpRequest();
	} 
	else if (window.ActiveXObject) {
		try {
			this.req = new ActiveXObject("Msxml2.XMLHTTP");
	}
		catch (err) {
			this.req = new ActiveXObject("Microsoft.XMLHHTP");
		}
	}
	if (this.req) {
		try {
			var ldr=this;
			this.req.onreadystatechange=function() {
				xmlhttp.loader.onReadyState.call(ldr);
			}
			this.req.open(method,url,true);
			this.req.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); // ezt kuldtuk az elozo verzioban is, biztos fontos volt... 
			if (contentType) {
				this.req.setRequestHeader('Content-Type', contentType);
			}
			this.req.send(params);
			this.loadProgress.call(this);
		} catch (err) {
			this.onerror.call(this);
		}
	}
}

xmlhttp.loader.onReadyState = function() {
	var req=this.req;
	var ready=req.readyState;
	try {
		var httpStatus=req.status;
		//alert(ready);
		if (ready==xmlhttp.READY_STATE_LOADING || ready==xmlhttp.READY_STATE_LOADED) {
			//this.loadProgress.call(this);
		}
		else if (ready==xmlhttp.READY_STATE_COMPLETE) {
			if (httpStatus==200 || httpStatus == 0) {
				if (req.responseText.match(/^AJAX_LOAD/)) {
					_url = req.responseText.replace(/^AJAX_LOAD (.*?)$/, "$1");
					if (_url!="") {
						this.clearProgress(this);
						this.load(_url);
					}
				}
				else {
					this.clearProgress(this);
					this.onload.call(this);
				}
			}
			else {
				this.clearProgress(this);
				this.onerror.call(this);
			}
		}
	} 
	catch(e){ }
}

xmlhttp.loader.prototype.defaultError=function(){
	alert("error fetching data!"
		+"\n\nreadyState: "+this.req.readyState
		+"\nstatus: "+this.req.status
		+"\nheaders: "+this.req.getAllResponseHeaders());
}

xmlhttp.loader.prototype.defaultloadProgress=function(){
	var text = (this.loadProgressText) ? this.loadProgressText : "Töltés..."
		
	var loading = document.createElement("div");
	document.body.appendChild(loading);
	var loadtxt = document.createTextNode(text);
	loading.appendChild(loadtxt);
	loading.id=this.progressId;
	
	if (this.loadProgressStyle) {
		loading.className=this.loadProgressStyle;
	}
	else {
		loading.style.border="1px";
		loading.style.borderColor="#f1eae4";
		loading.style.width="150px";
		loading.style.backgroundColor="#6d8891";
		loading.style.color="#ffffff";
		loading.style.fontFamily="Verdana"
		loading.style.fontSize = "8pt";
		loading.style.fontWeight="normal";
		loading.style.textAlign="center";
		loading.style.position="absolute";
		//loading.style._position="absolute";
		loading.style.zIndex="999";
		//loading.style.top="0";
		loading.style.top=eval(document.body.scrollTop);
		loading.style.left="0";
	}
}
xmlhttp.loader.prototype.clearProgress=function() {
	var elem = document.getElementById(this.progressId);
	if (elem) {
		elem.parentNode.removeChild(elem);
	}
}
