
/*******************************************************************
 ***
 ***  kleine AJAX-Library - FAJAX v1.2
 ***  (c) 2007, Markus Feineis
 ***
 *******************************************************************/
 
  
 
    // URL für aufzurufendes PHP-Skript 
    var _ajaxUrl = "ajaxUpdate.php";
    
    // ID's für Loading-Box und evtl. Background
    var _ajaxLoading   = "ajaxLoading";
    var _ajaxLoadingBg = "ajaxLoadingBg";
     
    
    var _requested;
    var _alert = false; // Debug-Meldungen als alert()
    
    

    
    function createRequestObject() {    	
    	var xmlhttp;
    	try {
    	    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
        	    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
        	    xmlhttp = null;
            }
        }
        if(!xmlhttp && typeof XMLHttpRequest!="undefined") {
            xmlhttp=new XMLHttpRequest();
        }
    	return xmlhttp;
    }
    
    
    
    // action:                 id
    // arr:                    an php zu übergebendes Query-Array
    // showProcessStatus:      ajaxLoading anzeigen?
    // afterResponseFunction:  nach erfolgreichem Response eine Funktion ausführen?
    function ajaxContent(action, arr, showProcessStatus, responseFunction, afterResponseFunction) {

        var http = createRequestObject();                     
        if (showProcessStatus==null) { showProcessStatus = true; }
        if (responseFunction==null) responseFunction = ajaxResponse;

        
        // Querystring erzeugen
        var query = '';
        if (typeof(arr)=='string') { 
            query = '&sub_action='+arr;
        } else if (typeof(arr)=='object') { 
            for (var t=0; t<arr.length; t++) {                           
                query += '&' + arr[t][0] + '=' + encodeURIComponent(arr[t][1]);              
            }
        } 

        try {
            _requested = 2;           
            if (showProcessStatus) { _processing(); }
            query = 'action='+action.replace(/\+/g, "_") + query;     
            http.open('get', _ajaxUrl + '?' + query); 
            
            
            http.setRequestHeader('Content-Type',  "text/xml; charset=iso-8859-1");            
            http.onreadystatechange = function(){responseFunction(http, afterResponseFunction, showProcessStatus);}
      		http.send(null);             
    	} catch (e){ // Error?    	    		
    		if (showProcessStatus) { _deprocessing(afterResponseFunction); }
    		if (_alert) { alert('Error: Request konnte nicht gesendet werden :-('); }
    	}
    	finally {}
    }
    
    
    
    
    function ajaxResponse(http, afterResponseFunction, showProcessStatus) {
    	try {
            if ((http.readyState == 4) && (http.status == 200)) {
           	    var resp = http.responseXML.documentElement;
           	    
                // alle XML-Tags von response durchlaufen und im HTML-Dokument ersetzen
                replaceAllTags(resp);
                _postprocessing(afterResponseFunction);
            } 
            
        } catch (e) { // Error? 
            if (showProcessStatus) { _postprocessing(afterResponseFunction); } 
    		if (_alert) { alert("Error: Response konnte nicht gelesen werden!"); }
    	} 
    	finally {}
    	
    }





    // ersetzt alle XML-Tags von xml im HTML-Dokument    
    function replaceAllTags(xml) {
        var root = xml.getElementsByTagName('ajax')[0];
        if (root!=null) {
            var idNode = root.firstChild;
        }
        while (idNode!=null) {  
            var doc;
            var id = idNode.nodeName;
            if (idNode.nodeType==1) {
                var cNode = idNode.cloneNode(true);
                var string = serialization(cNode);
                
                // noch schnell die unschönen xml-Tags entfernen...
                string = string.replace('<'+id+'>', '');
                string = string.replace('<'+'/'+id+'>', '');
                string = string.replace('__LOWERTHAN__', '<');
                string = string.replace('__GREATERTHAN__', '>');
                    
                if (document.getElementById(id) != null) {  // HTML-Tags direkt ersetzen

                    var elem = document.getElementById(id);
                    
                    if (idNode.getAttribute('add')) {
                        if (elem.tagName.toUpperCase() == "INPUT") {
                            document.getElementById(id).value += string.trim();
                        } else {
                            document.getElementById(id).innerHTML += string.trim();
                        } 
                    } else { 
                        if (elem.tagName.toUpperCase() == "INPUT") {
                            document.getElementById(id).value = string.trim();
                        } else {
                            document.getElementById(id).innerHTML = string.trim();
                        }
                    }
                
                } else if (id == 'javascript') {  // javascript direkt ausführen
                    eval(string);
                }
            }
            idNode = idNode.nextSibling;            
        }
    }
    
    
  
  
  
    // liefert XMLSerialization zurück, bzw. eine Alternative dazu für IE
    function serialization(node) {
        var string = false;
        try {  // Mozilla, ...
            var serializer = new XMLSerializer();
            string = serializer.serializeToString(node);
        } catch (e) {
            try { // IE
                string = node.xml;
            } catch (e) {}
        }
        return string;
    }
    
    
    
    
    
        
    // init
    function fajax(url, ajaxL, ajaxLBG) {
        _ajaxUrl = url;
        _ajaxLoading = ajaxL;
        _ajaxLoadingBg = ajaxLBg;
    }
    
    
  
    
    
    /*****************************************************
    **  Loading-Boxes
    ***/

    function showLoadingBox(id, text) {      
        if (!id) { id = _ajaxLoading; }
        try {
            var item = document.getElementById(id);
            var item_root = document.getElementById("content_selection");
            if (text) {
                item.innerHTML = text;
            }
            item_root.style.visibility = 'hidden';
            item.style.display = 'block';
        } catch (e) {}
    }
    function hideLoadingBox(id) {
        if (!id) { id = _ajaxLoading; }
        try {
            var item = document.getElementById(id);
            var item_root = document.getElementById("content_selection");
            item_root.style.visibility = 'visible';
            item.style.display = 'none';
        } catch (e) {}
    }
    function showLoadingBg(id) {
        if (!id) { id = _ajaxLoadingBg; }
        try {
            var item = document.getElementById(id);            
            fadeIn(id, 8, 0, .9);
        } catch (e) {} 
    }
    function hideLoadingBg(id) {
        if (!id) { id = _ajaxLoadingBg; }
        try {
            var item = document.getElementById(id);
          //  item.style.display = 'none';
            fadeOut(id, 5, 0, .9);            
        } catch (e) {} 
    }
    
    
    
    
    /*****************************************************
    **  diverse Effekte
    ***/
    
    var _functionAfter = "alert('test')";
    
    // lässt html-Elemente sanft einblenden
    function fadeIn(id, delay, min, max, functionAfter) {
        _functionAfter = functionAfter;
        _fade(id, 0, 1, delay, min, max);
    }
    // lässt html-Elemente sanft verschwinden
    function fadeOut(id, delay, min, max, functionAfter) {
        _functionAfter = functionAfter;   
        _fade(id, 0, 0, delay, min, max);
    }   
    
    // rollt ein Element nach unten aus
    function rollDown(id, delay) {
        _roller(id, 0, 1, delay);
    }
    

    
    
    
    
    /*****************************************************
    **  sonstiger private-Kram
    ***/
         
    // Schritte, die vor dem Request ausgeführt werden sollen,
    // z.B. diable von Buttons oder ajaxLoading-Box
    function _processing() {
        showLoadingBox();
        showLoadingBg();
    }
    
    // Schritte, die bei erfolglosem Request ausgeführt werden sollen
    function _deprocessing(afterResponseFunction) {        
        hideLoadingBox();
        hideLoadingBg();
        if (afterResponseFunction!=null) {
            eval(afterResponseFunction);
        }
    }
    
    // Schritte, die nach erfolgreicher Response ausgeführt werden sollen
    function _postprocessing(afterResponseFunction) {
        hideLoadingBox(); 
        hideLoadingBg();
        if (afterResponseFunction!=null) {
            eval(afterResponseFunction);
        }
    }

    // zum Ein-/Ausfaden von Elementen (opacity)
    function _fade(id, counter, direction, del, mi, ma) {
    //    if (_requested>1) {
            var val = counter/10;
            var item = document.getElementById(id);
            var delay = del;
            var min = mi;
            var max = ma;       
            if (del==null) { delay = 40; }
            if (mi==null) { min = 0; }
            if (ma==null) { max = 1; }
            var step = Math.round(20/delay*10)/10;
            var newVal = counter + step;
            if (item!=null) {
                if (direction==0) { // fade out
                    val = max-val;
                    newVal = counter + step;
                }
                val = Math.round(val*10)/10; 
                if (val<=max && val>=min) {
                    _opac(item, val);
                    window.setTimeout("_fade('" + id + "', " + newVal + ", " + direction + ", " + delay + ", " + min +  ", " + max + ");", delay);
                }            
               // if (direction==1) { item.style.display = 'block'; }
               // if (direction==0 && val<=min) { item.style.display = 'none'; _requested--; }      
            }
            if (counter == 10 && _functionAfter != "" && _functionAfter != null) {
                var f = new Function(_functionAfter);
                f(); 
            }
    //    }             
    }
    
    function _opac(item, val) {
        item.style['opacity'] = val;
        item.style['-moz-opacity'] = val;
        item.style['filter'] = "alpha(opacity="+(val*110)+"), finishopacity=0, style=2";
    }
    
    
    var _height;
    var _width;
    
    // zum Auf-/Abrollen von Elementen
    function _roller(id, counter, direction, del) {
        var item = document.getElementById(id);                
        if (counter==null || counter==0) {
            _height = item.offsetHeight;
            _width = item.offsetWidth;
        }
        var delay = del;
        if (delay==null) { delay = 5; }      
        counter = counter + 40;
        var newVal = counter; 
        if (item!=null) { 
            if (direction==0) { // up
                newVal = _height - counter;
            } 
            if (counter<=_height) {
               item.style.height = counter + 'px';            
               _opac(item, newVal/_height);
               window.setTimeout("_roller('" + id + "', " + newVal + ", " + direction + ", " + delay + ");", delay);
            } else { 
                item.style.height = _height + 'px';
            }
        }
    }
    
    
    
    // lässt die Ladeboxen beim Browser-Scroll mit nach unten scrollen; 
    // damit Umgehung von IE-Bug "position:fixed"
    window.onscroll = scroll_a_weng;
    function scroll_a_weng(e) { 
        var m1 = document.getElementById('ajaxLoading');
        var m2 = document.getElementById('ajaxLoadingBg');         
        var y = (document.all) ? window.event.y + document.body.scrollTop : e.pageY;
        var offset = document.body.clientHeight / 2 - 20;
        if (m1!=null) {
            m1.style.pixelTop = y + offset;
        }
        if (m2!=null) {
            m2.style.pixelTop = y;
        }       
    } 
    

    
    
    
    /* kleineres spezifisches Zeug */
    
    var timeout;
    function submitIt(id, elem) {
        ajaxContent(id, elem);      
  
    }   
    function resetCounter(ms, id, elem) {        
        window.clearTimeout(timeout);
        var func = function(){submitIt(id, elem);}
        //var func = "submitIt("+id+", "+elem+", "+file+")";
        timeout = window.setTimeout(func, ms);
    }    
    function typeout(id, elem) {
        resetCounter(700, id, elem);
    }


