/*
*
* ajax helper
* 
* @copyright (c) 2007 NonFood Werbeagentur Unit 2 GmbH
* @author Andreas Habel
* 
* $Id: fajax.js,v 1.7 2010/06/21 09:51:15 virus-d Exp $
*
*/

	function fAjax() {};
	fAjax.prototype = {
		
		debug:false,
		
		/* callback methods */
		onsubmit: null,
		onsuccess: null,
		onerror: null,
		
		request: function(url, id, params) {
			var xhttp=fScript.xmlHttpRequest();
			
			if (typeof this.onsubmit == 'function') {
				this.onsubmit();
			}
			
			var myself = this;
			xhttp.onreadystatechange = function() { myself.__resultHandler.call(myself,xhttp,id); };
			xhttp.open('POST',url,true);
			
			data = this.buildParamList(params);
			xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xhttp.send(data);
			return true;
		},
		
		__resultHandler: function(xhttp,id) {
			if (xhttp.readyState!=4) {
				return;
			}
			
			if (this.debug) {
				alert('targetID: '+id+' -- Service reply: '+xhttp.responseText);
			}
			
			var nodes=xhttp.responseXML.documentElement.getElementsByTagName('div')[0];
			if (nodes.childNodes.length == 0) {
				if (typeof this.onerror == 'function') {
					this.onerror(xhttp.responseXML);
				}
				return false;
			}
			
			if (typeof this.onsuccess == 'function') {
				this.onsuccess(xhttp.responseXML);
			}
			
			if (id!='') {
				this.__mergeIntoContent(xhttp, id);
			}
			
			return true;
		},
		
		__mergeIntoContent: function(xhttp, id) {
			target = document.getElementById(id);
			if (!target) {
				throw "target not found";
			}
			
			var blank;
         if (fScript.isIE) {
            var x;
            if (xhttp.responseXML.childNodes.length == 0) {
               var tempString = xhttp.responseText;
               var start = 0, end = 0;
               start = tempString.indexOf('<body>')+6;
               end = tempString.indexOf('</body>');
               tempString = tempString.substring(start, end);
               tempNode = new ActiveXObject('Microsoft.XMLDOM');
               tempNode.async = false;
               tempNode.loadXML(tempString);
               x = tempNode.getElementsByTagName('div')[0];
            } else {
               x=xhttp.responseXML.documentElement.getElementsByTagName('div')[0];
            }
            x.removeAttribute('id');
            var tmp = document.importNode(x,true);
            
            target.innerHTML='';
            target.appendChild(tmp);
            target.innerHTML=target.innerHTML;  
            
         } else {
            blank = target.cloneNode(false);
				var x=xhttp.responseXML.documentElement.getElementsByTagName('div')[0];
				x.removeAttribute('id');
				var tmp = document.importNode(x,true);
				blank.appendChild(tmp);
            target.parentNode.replaceChild(blank,target);
         }
         
			return true;
		},
		
		buildParamList: function(params) {
			var list=new Array();
			for(x in params) {
				if (typeof params[x] != 'function') {
					var fnc = encodeURIComponent || escape;
					var value = fnc(params[x]);
					list.push(x+'='+value);
				}
			}
			return list.join('&');
		}
	}//core
	
	
	
	
