var aktsuchwoerter = '';
var akttags = '';
var aktautor = '';
var aktpage = 0;
var aktgesamtanzahl = 0;
var aktusertype = 0;
var aktsortierung = 0;
var http_request = null;
var http_request2 = null;
var E;

function weitersuchen(suchwoerter, tags, autor, page, anzahl, usertype, sortierung){
    // Variablen global vorbereiten
    //E = $('mb_frame');
    //E.startWaiting('bigWaiting');
    document.getElementById('suchhinweis').style.display = 'block';
    aktsuchwoerter = suchwoerter;
    akttags = tags;
    aktautor = autor;
    aktpage = page;
    aktusertyp = usertype;
    aktgesamtanzahl = anzahl;
    aktsortierung = sortierung;
    // XMLHttpRequest- bzw. htmlfile-Objekt erzeugen
    // Mozilla, Opera, Safari sowie Internet Explorer 7
    if (typeof XMLHttpRequest != 'undefined') {
        http_request = new XMLHttpRequest();
    }
    if (!http_request) {
        // Internet Explorer 6 und aelter
        try {
            http_request  = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                http_request  = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                http_request  = null;
            }
        }
    }
    if (http_request) {
        http_request.onreadystatechange = parseResponse;
        var link = '/suche/updatesuchen?suchwoerter=' + suchwoerter + '&tags=' + tags + '&autor=' + autor + '&page=' + page + '&usertype=' + usertype + '&sortierung=' + sortierung;
        //prompt("Hallo", link);
        http_request.open('GET', link, true);
        http_request.send(null);
    }
}
    
// Aktualisierung der Anzeige
function update(msg){
    var updateid = '';
    if (aktgesamtanzahl > aktpage * 10) {
        aktpage = aktpage + 1;
        weiterinhalt ='<a onClick="javascript:weitersuchen(\'' + aktsuchwoerter + '\',\'' + akttags + '\',\'' + aktautor + '\', ' + aktpage + ', ' + aktgesamtanzahl +  ', ' + aktusertyp + ', ' + aktsortierung + ')" style="text-decoration:underline;cursor:pointer;">weiter Suchergebnisse anzeigen</a>';
    }else{
        weiterinhalt='';
        aktpage = aktpage + 1;
    }
    if (aktusertyp == 1) {
        updateid='threadlistschueler';
        document.getElementById('weitersuchenschueler').innerHTML = weiterinhalt;
    }
    if (aktusertyp == 2) {
        updateid='threadliststudent';
        document.getElementById('weitersuchenstudent').innerHTML = weiterinhalt;
    }
    if (aktusertyp == 5) {
        updateid='threadlistsonstige';
        document.getElementById('weitersuchensonstige').innerHTML = weiterinhalt;
    }
    var beitraege=document.getElementById(updateid).innerHTML;
    if (msg.length > 1) {
        var inhalt = '';
        //alert(inhalt.length - inhalt.lastIndexOf("</tr>"));
        if (navigator.userAgent.indexOf('MSIE') == -1 || window.opera) {
            inhalt = beitraege.substring(0, beitraege.lastIndexOf("</table>"));
            inhalt = inhalt.substring(0 , inhalt.lastIndexOf("</tr>") + 5);
        }else{
            inhalt = beitraege.substring(0, beitraege.lastIndexOf("</TABLE>"));
            inhalt = inhalt.substring(0 , inhalt.lastIndexOf("</TR>") + 5);
        }
        //alert(msg);
        if (aktpage == 2) {
            inhalt = '<div id="thread_list">';
            inhalt = inhalt + '<table width="100%" cellspacing="2" cellpadding="0" border="0">' + msg + "</table></div>";
        }else{
            inhalt = inhalt + '\n' + msg + "</table></div>";
        }
        inhalt = inhalt.replace(/></g, ">\n<");
        //prompt("hallo", inhalt);
        //alert(inhalt);
        //alert(inhalt.substring(inhalt.length - 260, inhalt.length));
        document.getElementById(updateid).innerHTML = inhalt;
    }
    document.getElementById('suchhinweis').style.display = 'none';
}

function parseResponse(){
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            var txt = http_request.responseText;
            update(txt);
        }else{
            alert('Fehler bei XMLHttpRequest: (' + http_request.statusText + ')');
        }
        http_request = null;
    }
}

function decode_utf8(utftext) {
    var plaintext = ""; 
    var i=0; 
    var c=c1=c2=0;
    // while-Schleife, weil einige Zeichen uebersprungen werden
    while(i<utftext.length)
    {
        c = utftext.charCodeAt(i);
        if (c<128) {
            plaintext += String.fromCharCode(c);
            i++;
        }
        else if((c>191) && (c<224)) {
            c2 = utftext.charCodeAt(i+1);
            plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
            i+=2;
        } else {
            c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
            plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
            i+=3;
        }
    }
    return plaintext;
}

