XWK.Ajax = function() {
	this.id = XWK.Ajax.list.length;
	XWK.Ajax.list[this.id] = this;
};

XWK.Ajax.list = [];

XWK.Ajax.wraper = function(func,id) {
	XWK.Ajax.list[id][func]();
}

XWK.Ajax.sendForm = function(form) {
	var arr = [];
	for (var i=0; i<form.elements.length; i++) {
		var e = form.elements[i];
		if (e.name == "")
			continue;
		if (e.type == "submit") {
			continue;
		}
		if (e.type == "button") {
			continue;
		}
		if (e.type == "image") {
			continue;
		}
		if (e.type == "radio" && !e.checked) {
			continue;
		}
		arr.push([e.name,e.value]);
	}
	return arr;
}

var _p = XWK.Ajax.prototype = {};

_p.setCallback = function(f,a) {
	this._cfunc = f;
	this._cargs = a;
}

_p.sendRequest = function(url) {
	var u = new XWK.Url(url);
	u.setProporty("ajax","1");
	u.setProporty('anticache',new Date().getTime())
	var xmlreq = false;
	if (XWK.exists('window.XMLHttpRequest')) {
		xmlreq = new window.XMLHttpRequest();
		if (xmlreq.overrideMimeType) {
			xmlreq.overrideMimeType('text/plain');
		}
	} else if (XWK.exists('window.ActiveXObject')) {
		try {
			xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (xmlreq) {
		this.xmlreq = xmlreq;
		var id = this.id;
		xmlreq.onreadystatechange =  function() {
			XWK.Ajax.wraper('jeckXMLRequest',id);
		};
		xmlreq.open('GET', u.toUrl(), true);
		xmlreq.send(null);
	} else {
		var temp = document.getElementsByTagName("body");
		var body = temp[0];
	    var helper = document.createElement("iframe");
	    helper.style.height = "1px";
	    helper.style.width = "1px";
	    helper.style.display = "none";
		helper.id = "xwk_ajax_helper"+this.id;
		helper.name = "xwk_ajax_helper"+this.id;
//		xwk_ajax_helper.onload = "jeck()";
		this.hinterval = setInterval("XWK.Ajax.wraper('jeckHelper',"+this.id+")",50);
		body.appendChild(helper);
		u.setProporty("ajax","iframe");
		helper.src = u.toUrl();
		return false;
	}
}

_p.jeckHelper = function() {	
	if (!XWK.exists("window.frames.xwk_ajax_helper"+this.id+".document")) {
 		return;
 	}
 	
 	var doc = window.frames["xwk_ajax_helper"+this.id].document;
 	var x = doc.getElementById('eof');
 	if (!x) {
 		return;
 	}
 	
    clearInterval(this.hinterval);
    this.data = eval('('+doc.getElementById('ajaxdata').value+')');
	this.fireOnLoaded();
}

_p.jeckXMLRequest = function() {
	var h = this.xmlreq;
	if (h.readyState == 4) {
            if (h.status == 200) {
                this.data =  eval('('+h.responseText+')');
                /*
                var xmldoc = xmlreq.responseXML;
var root_node = xmldoc.getElementsByTagName('root').item(0);
alert(root_node.firstChild.data);
                */
            } else {
//				alert('Bei dem Request ist ein Problem aufgetreten.');
            }
            this.fireOnLoaded();
        }
}

_p.fireOnLoaded = function() {
	XWK.sendCallback(this._cfunc,[this.data]);
}
