// globale Variablen
var xmlHttp=false;
var container='';

// Konstanten
var REQUEST_GET=0;
var REQUEST_POST=2;
var REQUEST_HEAD=1;
var REQUEST_XML=3;

function request(data) {
    //anfrage an eine HTML oder Textdatei
    //sendRequest('ajax_testanfrage.html','','','');

    // anfrage an eine dynamische Seite (z.B. php)
    //sendRequest(domain_url+'ajax.php','sessionID='+sessionID+'&'+data,'','');

}


function getXMLRequester() {
    var xmlHttp=false;

    //try to create a new instance of the xmlhttprequest object
    try {

        if(window.ActiveXObject) {
            // IE
            for(var i=5;i;i--) {
                try {
                    //loading of a newer version of msxml dll (msxml3- msxml5) failed
                    // use fallback solution
                    // old style msxml version independent, deprecated
                    if(i==2) {
                        xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    else {
                        // try to use latest msxml dll
                        xmlHttp= new ActiveXObject("Msxml2.XMLHTTP."+i+".0");
                    }
                    break;
                }
                catch(excNotLoadable) {
                    xmlHttp=false;
                }
            }
        }
        else if(window.XMLHttpRequest) {
            //MOzilla, Opera, Safari
            xmlHttp= new XMLHttpRequest();
        }
    }
    //loading of xmlhttp object failed
    catch(excNotLoadable) {
        xmlHttp=false;
    }
    return xmlHttp;
}

function sendRequest(strSource,strData,intType,intID) {
    // sendet HTTP-Request an Server
    // @param strSource, String, Datenquelle, z.B. data.php
    // @param strData, String, Daten, die an den Server gesendet werden sollen, optional
    // @param intType, Integer, request type, mögliche Werte: REQUEST_GET, REQUEST_POST, REQUEST_HEAD, REQUEST_XML; default: REQUEST_GET
    // @param intID, Integer, ID der Anfrage, ist nötig für die registrierung beim Event handler 'onreadystatechange', optional
    // @return String, angefragte Daten bzw. Datenquelle

    if(!strData) {
        strData='';
    }
    if(isNaN(intType)) {
        // 0=GET, 1=XML, 2=POST
        intType=0; // GET
    }

    // vorherige Anfrage noch nicht beendet => Anfrage abbrechen, bevor neue Abfrage gestartet wird
    if(xmlHttp && xmlHttp.readyState) {
        xmlHttp.abort();
        xmlHttp=false;
    }

    // neue Instanz des xmlhttprequest Objektes erstellen
    // wenn 's nicht klappt => return
    if(!xmlHttp) {
        xmlHttp=getXMLRequester();
        if(!xmlHttp) {
            return;
        }
    }

    // parse query string
    if(!intType !=1 && (strData && strData.substr(0,1)=='&' || strData.substr(0,1)=='?')) {
        strData=strData.substring(1,strData.length);
    }

    // data für POST generieren
    var dataReturn=strData ? strData : strSource;

    switch(intType) {
        case 1:
            //XML
            strData="xml="+strData;
        case 2:
            // POST
            // Verbindung aufbauen
            xmlHttp.open("POST",strSource,true);
            xmlHttp.requestHeader('Content-Type','application/x-www-form-urlencoded');
            xmlHttp.requestHeader('Content-length',strData.length);
            break;
        case 3:
            // HEAD
            // Verbindung aufbauen
            xmlHttp.open("HEAD",strSource,true);
            strData=null;
            break;
        default:
            //GET
            // Verbindung aufbauen
            var strDataFile=strSource+(strData? '?' + strData: '');
            xmlHttp.open("GET",strDataFile,true);
            strData=null;
    }

    // set onload data event handler
    xmlHttp.onreadystatechange= new Function("","processResponse(" + intID +")");;

    // Anfrage an Server schicken
    xmlHttp.send(strData); //param = POST data

    return dataReturn;
}


function processResponse(intID) {
    // Verarbeiten der Antwort-Daten (Response) vom Server
    // @param intID, Integer, ID der Antwort (response)

    switch(xmlHttp.readyState) {
        case 0:
            // UNINITIALIZED: open ist noch nicht initialisiert
        case 1:
            // LOADING:send() wurde aufgerufen, aber noch nicht abgearbeitet
        case 2:
            // LOADED: send() wurde aufgerufen, Header und status ist verfügbar
        case 3:
            // INTERACTIVE: Daten werden empfangen, sind unvollständig
            break;
        case 4:
            // COMPLETED: alle Schritte sind abgeschlossen
            // http status prüfen

            if(xmlHttp.status==200) {
                // erfolgreich
                processData(xmlHttp,intID);
            }
            else {
                // Datenempfang war nicht erfolgreich, z.B. Seite nicht erreichbar
                if(window.handleAJAXError) {
                    handleAJAXError(xmlHttp,intID);
                }
                else {
                    alert("ERROR\n HTTP status = "+ xmlHttp.status + "\n"+xmlHttp.statusText);
                }
            }
    }
}

function handleAJAXError(xmlHttp,intID) {
    // verabreitung von Fehlermeldungen
}


function processData(xmlHttp,intID) {
    // Daten vom Server verarbeiten
//alert(xmlHttp.responseText);
//alert(intID);

    data=xmlHttp.responseText.split('WWQQWW');

    if(data[0]=='konsole') {
        document.getElementById("thumb1").innerHTML=data[3];
        // document.getElementById("thumb1").innerHTML=xmlHttp.responseText;
    }
    else if(data[0]=='gal') {
        galBuilder(data);
    }
    else if(data[0]=='gal_redbox') {
        galBuilder_redbox(data);
    }
    else if(data[0]=='thumb') {
        display_thumb(data);
    }
    else {
        alert('Es ist ein Fehler aufgetreten. Bitte Webseite neuladen ('+xmlHttp.responseText+')');
    }

    if(data[1]!=0) {
//        document.getElementById("info").innerHTML=document.getElementById("info").innerHTML+data[1];
    }
    if(data[2]!=0) {
//        document.getElementById("meldung").innerHTML=document.getElementById("info").innerHTML+data[2];
    }
}
