wajax=new Function();
wajax.prototype.tags=new Array();
wajax.prototype.ajaxpath=document.location;
wajax.prototype.actionpage="";
wajax.prototype.method="post";
wajax.prototype.preloaderimg="/admin/img/ajax-loader.gif";

wajax.prototype.preloader=function(parameters) {
	var newloader=document.getElementById("AJAX-preloader");
	
	if (!newloader) {
		var sw=document.body.clientWidth;
		var sh=document.body.clientHeight;
		var newloader=document.createElement("img");
		newloader.id="AJAX-preloader";
		newloader.style.position="absolute";
		newloader.style.marginLeft=(sw/2-110)+"px";
		newloader.style.marginTop=(sh/2-10-100)+"px";
		newloader.style.zIndex=200;
		newloader.alt="";
		newloader.src="/admin/img/ajax-loader.gif";
	}
	else {
		newloader.style.display='';
		newloader.style.zIndex=200;
	}
	
	document.body.insertBefore(newloader,document.body.firstChild);
	
	request=this.ajax_request(parameters)
	
	newloader.style.display='none';	
	
	return request;
}

wajax.prototype.ajax_request=function(parameters) {
	var http_request;
	
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        http_request=new XMLHttpRequest();
        if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
        }
    } 
    else if (window.ActiveXObject) { // IE
        try {
            http_request=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				http_request=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}

	http_request.open(this.method,this.ajaxpath+this.actionpage,false);	
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection","close");
	http_request.send(parameters);
	return this.ajax_alertContents(http_request);
}

wajax.prototype.ajax_alertContents=function(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			if (http_request.responseText) return http_request.responseText;
			else return "";
		}
	}
}

wajax.prototype.action=function(parameters,showrequest) {
	request=this.preloader(parameters+"&actiontype=ajax");
	if (showrequest) { alert(request); return false; }
	var process=this.getXMLdata(request,"process");
	if (process.length) {
		if (process[0].childNodes[0].nodeValue=="errors") {
			var errors=this.getXMLdata(request,"error");
			if (errors.length) {
				var errormsg="";
				for (var i=0;i<errors.length;i++) errormsg+=errors[i].childNodes[0].nodeValue+"\n";
				alert(errormsg);
				return false;
			}
		}
		if (process[0].childNodes[0].nodeValue=="confirm") {
			var context=this.getXMLdata(request,"context");
			if (context.length) {
				if (confirm(context[0].childNodes[0].nodeValue)) {
					var ifyes=this.getXMLdata(request,"ifyes");
					if (ifyes.length) {
						if (ifyes[0].childNodes[0].nodeValue=="resend") {
							this.action(parameters+"&resended=yes");
						}
					}
				}
				else {
					return false;
				}
			}
		}
	}	
	
	return true;
}

wajax.prototype.getcontent=function(content,target,parameters) {
	var request=this.preloader("action=getcontent&content="+content+"&actiontype=ajax"+(parameters?parameters:""));

	var process=this.getXMLdata(request,"process");
	if (process.length) {
		if (process[0].childNodes[0].nodeValue=="code") {
			var html=this.getXMLdata(request,"html")[0].childNodes[0].nodeValue;
			if (target) document.getElementById(target).innerHTML=html;
			else return html;
		}
		if (process[0].childNodes[0].nodeValue=="xml") if (this.getXMLdata(request,"xml")[0].childNodes.length) return this.getXMLdata(request,"xml")[0].childNodes[0].nodeValue; else return false;
	}
}

wajax.prototype.getXMLdata=function(xmltext,tagname) {
	parser=new DOMParser();
	xmldoc=parser.parseFromString(xmltext,"text/xml");
	return xmldoc.getElementsByTagName(tagname);
}
