function Bool(b) {
	this.wert = new Boolean(b);

	this.getValue = function() {
		return this.wert.valueOf();
	}

	this.setValue = function(b) {
		this.wert = new Boolean(b);
	}
}

function JConfirm(abfrage, functionNameIfOK) {
	this.abfrage = abfrage.replace("\n", "<br>\n");
	this.abfrage = this.abfrage.replace("[b]", "<b>");
	this.abfrage = this.abfrage.replace("[/b]", "</b>");
	this.background = null;
	this.box = null;
	this.body = document.getElementsByTagName("body")[0];

	this.show = function() {
		// Background
		this.background = document.createElement("div");
		this.background.id = "bg_jconfirm_" + Math.round(Math.random()*1000);
		this.background.style.backgroundColor = "#5577BB";
		this.background.style.position = "absolute";
		this.background.style.top = "0px";
		this.background.style.left = "0px";
		this.background.style.right = "0px";
		this.background.style.width = "100%";
		this.background.style.height = "100%";
		this.background.style.textAlign = "center";
		this.background.style.opacity = "0.5";
		this.background.innerHTML = "&nbsp;";

		this.body.appendChild(this.background);


		// Box
		this.box = document.createElement("div");
		this.box.id = "box_jconfirm_" + Math.round(Math.random()*1000);
		this.box.style.width = "40%";
		this.box.style.position = "absolute";
		this.box.style.top = "40%";
		this.box.style.left = "30%";
		this.box.style.textAlign = "center";
		this.box.style.border = "3px #333355 solid"; //groove
		this.box.style.backgroundColor = "#F5F5F5";
		this.box.style.padding = "20px";
		this.box.innerHTML = this.abfrage + "<br /><br />"
			+ "<input type=\"button\" value=\"Abbrechen\" onclick=\"document.getElementById('" + this.box.id + "').style.display = 'none'; document.getElementById('" + this.background.id + "').style.display = 'none';\">"
			+ "<input type=\"button\" value=\"OK\" onclick=\"document.getElementById('" + this.box.id + "').style.display = 'none'; document.getElementById('" + this.background.id + "').style.display = 'none';" + functionNameIfOK + "\" style=\"margin-left:15px\">";

		this.body.appendChild(this.box);
	}
	/*
	this.prototype.close = function() {
		alert("grins");
	}
	*/
	this.show();
}


/**
 * Opens calendar window.
 *
 * @param string zielID
 */
function kalender(zielID) {
    window.open("./kalender.php?zielID=" + zielID, "calendar", "width=250,height=160,status=yes");
}

function submit(element) {
	// Rekursiv
	if(element.nodeName == "FORM") {
		element.submit();
	}
	else {
		submit(element.parentNode);
	}
}

/**
 * Loescht eine bestimmte Zeile in irgendeiner HTML-Tabelle in document
 *
 * Uebergeben werden kann ein string oder number. Die Zeile mit der entsprechenden ID wird dann aus
 * der HTML-Tabelle geloescht.
 *
 * @param number rowID
 */
function deleteRowByID(rowID) {
	zeile = document.getElementById("" + rowID);
	tbody = zeile.parentNode;

	for(var i=0; i<tbody.rows.length; i++) {
		if(tbody.rows[i].id == rowID)
			tbody.deleteRow(i);
	}
}

function clock(element, hours, minutes, seconds)
{
    element = $(element);
    var clock_time = new Date();

    clock_time.setHours(hours, minutes, seconds);

    var clock_hours = clock_time.getHours();
    var clock_minutes = clock_time.getMinutes();
    var clock_seconds = clock_time.getSeconds();
    if (clock_hours < 10)
    {
        clock_hours = "0" + clock_hours;
    }
    if (clock_minutes < 10)
    {
        clock_minutes = "0" + clock_minutes;
    }
    if (clock_seconds < 10)
    {
        clock_seconds = "0" + clock_seconds;
    }
    element.innerHTML = clock_hours + ":" + clock_minutes + ":" + clock_seconds;
    setTimeout("clock('" + element.id + "'," + clock_time.getHours() + "," + clock_time.getMinutes() + "," + (clock_time.getSeconds() + 1) + ")", 990);
}

Playlist = new Playlist();

function Playlist()
{
    this.playlistUntersuchen = function() {
        var ul = document.getElementById("playlistUl");

        var visibleListItems = ul.childNodes.length;

		for(var i=0; i<ul.childNodes.length; i++) {
            if(ul.childNodes[i].id == "") {
                visibleListItems--;
            }
        }
        if(visibleListItems == 0) {
            ul.innerHTML = "<li style=\"padding-left:3px;\">Die Playlist ist leer.</li>";
            Playlist.makeDroppable();
            Playlist.makeSortable();
        }
    }

    this.mp3Entfernen = function (mp3Id) {
        Effect.Fade("playlist_mp3_" + mp3Id);
        setTimeout("document.getElementById(\"playlist_mp3_"+mp3Id+"\").id = \"\"", 1005);
        setTimeout("Playlist.playlistUntersuchen()", 1006);
    }

	this.mp3sEntfernen = function() {
		var ul = document.getElementById("playlistUl");

		for(var i=0; i<ul.childNodes.length; i++) {
            if(ul.childNodes[i].nodeName == "LI" && ul.childNodes[i].id != "") {
                var li = ul.childNodes[i];

                var checkbox = li.getElementsByTagName("input")[0];

                if(checkbox != null && checkbox.checked == true) {
                    var mp3Id = checkbox.id.replace("checkbox_", "");

                    new Ajax.Request(
                        "ajax.php",
                        {
                            parameters: {option: "delete", id: mp3Id},
                            onSuccess: Playlist.mp3Entfernen(mp3Id)
                        }
                    )
                }
            }
		}
		meldungen.neu(707);
	}

    this.show = function(draggable) {

		// ID des MP3s (ID aus der Datenbank)
		var id = draggable.id.replace('mp3_', '');

		var ul = document.getElementById("playlistUl");

        // namen der MP3 Datei raussuchen
        var name = "";
        for(var i=0; i<draggable.childNodes.length; i++) {
            if(draggable.childNodes[i].nodeName == "#text")
                name += draggable.childNodes[i].nodeValue;
        }

		var vorhanden = false;

        for(var i=0; i<ul.childNodes.length; i++) {
            // Ueberpruefen, ob Dateiname schon vorhanden ist.
            if(ul.childNodes[i].id == "playlist_" + draggable.id) {
                vorhanden = true;
            }
            // Damit sowas wie "Die Playlist ist leer." unsichtbar wird.
            if(ul.childNodes[i].id == "") {
                ul.childNodes[i].style.display = "none";
            }
        }

		// Ab jetzt wird die Variable zeilen nicht mehr geaendert, also kann die reihenfolge festgelegt werden
		var reihenfolge = ul.childNodes.length + 1;

		// nur wenn noch nicht vorhanden
		if(!vorhanden) {
			ul.innerHTML += "<li id=\"playlist_" + draggable.id + "\" class=\"odd\">"
                          + "<input type=\"checkbox\" id=\"checkbox_" + draggable.id.replace('mp3_', '') + "\""
                          + " name=\"mp3playlist[]\" value=\"" + draggable.id.replace('mp3_', '') + "\">"
                          + draggable.innerHTML
                          + "</li>"

			meldungen.neu(705,name);
		}
		else
			meldungen.neu(706,name);

		this.makeDroppable();
		this.makeSortable();
	}

    this.makeDroppable = function()
    {
		Droppables.add(
            "playlistUl",
            {
                onDrop: function(element)
                {
                    if(element.className == "mp3")
                    {
                        new Ajax.Request(
                            "ajax.php",
                            {
                                parameters: {option: "add", id: element.id.replace('mp3_', '')},
                                onSuccess: Playlist.show(element)
                            }
                        );
                    }
                    else if(element.className == "ordner")
                    {
                        div = document.getElementById("playlistDiv");
                        ul = document.getElementById("playlistUl");
                        div.removeChild(ul);
                        div.innerHTML += "<ul id=\"playlistUl\">"
                                       +   "<li style=\"padding:10px; text-align:center;\">"
                                       +     "<img src=\"" + TEMPLATE_PFAD + "img/ajax-loader.gif\" alt=\"laden...\" />"
                                       +   "</li>"
                                       + "</ul>";
                        Playlist.makeDroppable();
                        Playlist.makeSortable();
                        new Ajax.Request(
                            "ajax.php",
                            {
                                parameters: {option: "addordner", id: element.id.replace('ordner_', '')},
                                onSuccess: function(transport) {
                                    setTimeout("PlaylistVerwaltung.ladePlaylist(\"{tmpl_var name='playlist_id'}\")", 10);
                                }
                            }
                        );
                    }
                }
            }
        );
    }

	this.makeSortable = function ()
    {
		Sortable.create(
            "playlistUl",
            {
                onUpdate:function(){Playlist.updateTitleOrder("playlistUl")}
            }
        );
	}

    this.updateTitleOrder = function(elementId)
    {
        new Ajax.Request(
            "ajax.php",
            {
                parameters: "option=sort&array_name=" + elementId + "&" + Sortable.serialize(elementId)
            }
        )
        meldungen.neu(703);
    }
}

PlaylistVerwaltung = new PlaylistVerwaltung();

function PlaylistVerwaltung()
{
    this.ladePlaylistZeiten = function (streamserverId)
    {
        div = document.getElementById("playlistZeitenDiv");
        div.innerHTML = "<div style=\"text-align:center;\">"
                      + "<img src=\"" + div.className + "img/ajax-loader.gif\" />"
                      + "</div>";

        new Ajax.Request(
            "ajax.playlistverwaltung.php",
            {
                parameters:
                {
                    controller: "playlistverwaltung",
                    action: "ladePlaylistZeiten",
                    streamserverId: streamserverId
                },
                onSuccess: function(transport)
                {
                    document.getElementById("playlistZeitenDiv").innerHTML = transport.responseText;
                }
            }
        );
    }

    this.ladeWochenplan = function (streamserverId)
    {
        iframe = $("wochenplanIFrame");
        iframe.src = 'wochenplan.php?serverId=' + streamserverId;
    }

    //this.Playlist = new Playlist();

    this.oeffneUnterordner = function(ordnerId) {
        var ul = document.getElementById("unterordnerUl" + ordnerId);
        if(ul == null) {
            var ordnerSpan = document.getElementById("ordner_" + ordnerId);

            var li = document.getElementById("unterordnerLi" + ordnerId);

            var safe = li.innerHTML;
            li.innerHTML += "<ul id=\"unterordnerUl" + ordnerId + "\" width=\"100%\">"
                          + "<li style=\"text-align:center; padding:10px;\">"
                          + "<img src=\"" + TEMPLATE_PFAD + "img/ajax-loader.gif\" alt=\"laden...\" />"
                          + "</li></ul>";

            // der Ordner, der geoeffnet wurde, muss auch nochmal zu draggable
            // gemacht werden
            if(ordnerSpan != null)
                new Draggable(ordnerSpan.id,{revert:true});

            // Daten des zu oeffnenden Ordners holen
            new Ajax.Request(
                "ajax.playlistverwaltung.php",
                {
                    parameters:
                    {
                        controller: "playlistverwaltung",
                        ordner: ordnerId,
                        action: "getUnterordner"
                    },
                    onSuccess: function(transport) {
                        ul = document.getElementById("unterordnerUl" + ordnerId);
                        li.removeChild(ul);
                        li.innerHTML += transport.responseText;
                        // mache aus geholten MP3s draggables
                        var lis = li.getElementsByTagName("li");
                        for(var i=0; i<lis.length; i++) {
                            // copySubmit Button anzeigen
                            document.getElementById("copySubmit").style.display = "inline";

                            var span = lis[i].getElementsByTagName("span")[0];

                            if(lis[i].className == "mp3") {
                                if(span.id.substr(0, 3) == "mp3") {
                                    new Draggable(span.id,{revert:true});
                                }
                            }
                            else if(lis[i].className == "ordner") {
                                if(span.id.substr(0, 6) == "ordner") {
                                    new Draggable(span.id,{revert:true});
                                }
                            }
                        }
                        // der Ordner, der geoeffnet wurde, muss auch nochmal zu draggable
                        // gemacht werden
                        if(ordnerSpan != null)
                            new Draggable(ordnerSpan.id,{revert:true});
                    }
                }
            )
        }
        else {
            ul.style.display = "";
        }
        // aendern des Links und der Bildquelle von dem OrdnerOeffner
        try {
            var a = document.getElementById("unterordnerA" + ordnerId);
            a.href = "javascript:PlaylistVerwaltung.schliesseUnterordner("+ ordnerId + ");";

            var img = document.getElementById("unterordnerImg" + ordnerId);
            img.src = TEMPLATE_PFAD + "img/minus.png";
        }
        catch(e) { }
    }

    this.schliesseUnterordner = function(ordnerId) {
        var li = document.getElementById("unterordnerLi" + ordnerId);
        unterordnerUl = li.getElementsByTagName("ul")[0];
        unterordnerUl.style.display = "none";

        var a = document.getElementById("unterordnerA" + ordnerId);
        a.href = "javascript:PlaylistVerwaltung.oeffneUnterordner("+ ordnerId + ");";

        var img = document.getElementById("unterordnerImg" + ordnerId);
        img.src = TEMPLATE_PFAD + "img/plus.png";
    }

    this.ladePlaylist = function(playlistId)
    {
        div = document.getElementById("playlistDiv");
        ul = document.getElementById("playlistUl");
        div.removeChild(ul);
        div.innerHTML += "<ul id=\"playlistUl\">"
                       +   "<li style=\"padding:10px; text-align:center;\">"
                       +     "<img src=\"" + TEMPLATE_PFAD + "img/ajax-loader.gif\" alt=\"laden...\" />"
                       +   "</li>"
                       + "</ul>";
        new Ajax.Request(
            "ajax.playlistverwaltung.php",
            {
                parameters:
                {
                    controller: "playlistverwaltung",
                    action: "ladePlaylist",
                    playlist: playlistId
                },
                onSuccess: function(transport) {
                    ul = document.getElementById("playlistUl");
                    div = ul.parentNode;
                    div.removeChild(ul);
                    div.innerHTML += transport.responseText;
                    Playlist.makeDroppable();
                    Playlist.makeSortable();
                }
            }
        );
    }

    this.checkAllCheckboxes = function(containerElement, sourceElement)
    {
        var inputs = $(containerElement).getElementsByTagName("input");
        for(var i=0; i < inputs.length; i++)
        {
            if(inputs[i].type == "checkbox")
            {
                inputs[i].checked = sourceElement.checked;
            }
        }
    }

    this.deletePlaylist = function(playlistId)
    {
        var conf = confirm("Soll die Playlist wirklich geloescht werden?\n"
                        + "Die MP3s gehen dabei nicht verloren aber alle eingetragenen Abspieltermine werden geloescht.");
        if(conf)
            window.location.href = "index.php?page=playlistverwaltung&action=deleteplaylist&id=" + playlistId;
    }

    this.aktiviereTag = function (ppzId, wochenTag)
    {
        new Ajax.Request(
            "ajax.playlistverwaltung.php",
            {
                parameters:
                {
                    controller: "playlistverwaltung",
                    periodischePlaylistZeitId: ppzId,
                    tag: wochenTag,
                    status: 1
                }
            }
        );
        var img = document.getElementById(ppzId + wochenTag + "Img");
        img.src = img.src.replace("button_deactivated.png", "button_activated.png");
        img.parentNode.href = img.parentNode.href.replace("aktiviereTag", "deaktiviereTag");
        img.parentNode.title = "Deaktivieren";
    }

    this.deaktiviereTag = function (ppzId, wochenTag)
    {
        new Ajax.Request(
            "ajax.playlistverwaltung.php",
            {
                parameters:
                {
                    controller: "playlistverwaltung",
                    periodischePlaylistZeitId: ppzId,
                    tag: wochenTag,
                    status: 0
                }
            }
        );
        var img = document.getElementById(ppzId + wochenTag + "Img");
        img.src = img.src.replace("button_activated.png", "button_deactivated.png");
        img.parentNode.href = img.parentNode.href.replace("deaktiviereTag", "aktiviereTag");
        img.parentNode.title = "Aktivieren";
    }

    this.loeschePeriodischePlaylistZeit = function (ppzId)
    {
        var conf = confirm("Soll der Eintrag wirklich geloescht werden?");
        if(conf)
        {
            window.location.href = window.location.href.replace("action=editplaylist", "action=deleteppz")
                                        + "&ppzid=" + ppzId;
        }
    }

    this.loescheZeitbedingtePlaylistZeit = function (zpzId)
    {
        var conf = confirm("Soll der Eintrag wirklich geloescht werden?");
        if(conf)
        {
            window.location.href = window.location.href.replace("action=editplaylist", "action=deletezpz")
                                        + "&zpzid=" + zpzId;
        }
    }
}

MP3Verwaltung = new MP3Verwaltung();

function MP3Verwaltung()
{
    this.loescheOrdner = function(ordnerId)
    {
        var conf = confirm("Soll der Ordner und sein gesamter Inhalt wirklich geloescht werden?");
        if(conf)
        {
            window.location.href += "&deleteordner=" + ordnerId;
        }
    }

    this.checkAllMp3s = function(checkAllCheckbox)
    {
        var checkboxes = document.getElementsByName("mp3auswahl[]");
        for(var i=0; i < checkboxes.length; i++)
        {
            checkboxes[i].checked = checkAllCheckbox.checked;
        }
    }
}

ServerVerwaltung = new ServerVerwaltung();

function ServerVerwaltung()
{
    this.getStatistiken = function(dateString)
    {
        $("statistiken").innerHTML = "<img src=\"" + TEMPLATE_PFAD + "img/ajax-loader.gif\" />";
        new Ajax.Request(
            "ajax.serververwaltung.php",
            {
                parameters:
                {
                    controller: "serververwaltung",
                    action: "getStatistiken",
                    date: dateString
                },
                onSuccess: function(transport)
                {
                    $("statistiken").innerHTML = transport.responseText;
                }
            }
        );
    }

    this.changePlaylist = function(pPlaylistId, riseMeldung)
    {
        new Ajax.Request(
            "ajax.php",
            {
                parameters:
                {
                    action: "changePlaylist",
                    playlistId: pPlaylistId
                },
                onSuccess: function()
                {
                    if(riseMeldung)
                    {
                        meldungen.neu(84);
                    }

                    var buttonTable = $("buttonTable");
                    if(pPlaylistId > 0)
                    {
                        buttonTable.style.display = "";
                    }
                    else
                    {
                        buttonTable.style.display = "none";
                    }
                }
            }
        );
    }
}

ContentHostingVerwaltung = new ContentHostingVerwaltung();

function ContentHostingVerwaltung()
{
    this.changeWebhostingAccess = function()
    {
        var res = confirm("Moechtest Du wirklich den aktuellen Inhalt Deiner Webseite loeschen und den Zugang aendern?");
        if(res)
        {
            window.location.href = 'index.php?page=webhosting&action=changeWebhostingAccessType';
        }
    }
}

Bestellvorgang = new Bestellvorgang();

function Bestellvorgang()
{
    this.RabattAuswahl = function(laufzeit, preis, rabatt)
    {
        var laufzeitSpan = $("laufzeitSpan");
        laufzeitSpan.innerHTML = laufzeit;

        var monatSpan = $("monatSpan");
        if(laufzeit == 1)
        {
            monatSpan.innerHTML = '';
        }
        else
        {
            monatSpan.innerHTML = 'e';
        }

        var preisInklusiveRabattSpan = $("preisInklusiveRabattSpan");
        var neuerPreis = (preis * (1 - rabatt)).toFixed(2);
        preisInklusiveRabattSpan.innerHTML = neuerPreis.replace('.', ',');
        
        var gespartSpan = $("gespartSpan");
        var gespartLabelSpan = $("gespartLabelSpan");
        if(rabatt == 0)
        {
            gespartLabelSpan.style.display = "none";
            gespartSpan.innerHTML = "";
        }
        else
        {
            gespartLabelSpan.style.display = "";
            gespartSpan.innerHTML = ((preis - neuerPreis) * laufzeit).toFixed(2).replace('.', ',') + "&nbsp;&euro;";
        }
    }

    this.isUsernameAvailable = function(username)
    {
        var checkUsername = $("checkUsername");
        var usernameAvailable = $("usernameAvailable");
        var usernameUnavailable = $("usernameUnvailable");
        var usernameException = $("usernameException");

        checkUsername.style.display = "";

        new Ajax.Request(
            "ajax.bestellung.php",
            {
                parameters:
                {
                    action: "isUsernameAvailable",
                    username: username
                },
                onSuccess: function(transport)
                {
                    var ok = transport.responseText;

                    checkUsername.style.display = "none";
                    usernameAvailable.style.display = "none";
                    usernameUnavailable.style.display = "none";
                    usernameException.style.display = "none";
                    if(ok == 1)
                    {
                        usernameAvailable.style.display = "";
                    }
                    else if(ok == 0)
                    {
                        usernameUnavailable.style.display = "";
                    }
                    else
                    {
                        usernameException.style.display = "";
                    }
                }
            }
        );
    }
}

KundenVerwaltung = new KundenVerwaltung();

function KundenVerwaltung()
{
    this.deactivateCustomer = function(kundenId)
    {
        if(confirm("Soll der Kunde wirklich DEaktiviert werden?"))
        {
			var uri = 'index.php?page=kundenbearbeitung&deaktivieren=' + kundenId;
			window.location.href = uri;
		}
    }
    
    this.activateCustomer = function(kundenId)
    {
        if(confirm("Soll der Kunde wirklich aktiviert werden?"))
        {
			var uri = 'index.php?page=kundenbearbeitung&aktivieren=' + kundenId;
			window.location.href = uri;
		}
    }

    this.showEmailWindow = function()
    {
        Effect.AppearOrFade("entryBoxDiv");
    }

    this.deleteKundenDatei = function(kundenId, dateiId)
    {
        var conf = confirm("Soll die Datei wirklich geloescht werden?");
        if(conf)
        {
            window.location.href = "index.php?page=kundenbearbeitung&bearbeiten=" + kundenId + "&deleteDatei=" + dateiId;
        }
    }

    this.showInvoiceClassDiv = function()
    {
        var invoiceClassTarifRadioButton = $("invoiceClassTarifRadioButton");

        var invoiceClassTarifDl = $("invoiceClassTarifDl");
        var invoiceClassOtherDl = $("invoiceClassOtherDl");

        if(invoiceClassTarifRadioButton.checked)
        {
            if(invoiceClassOtherDl.getStyle('display') != 'none' & invoiceClassOtherDl.getOpacity() != 0.0)
            {
                Effect.BlindUp(invoiceClassOtherDl);
            }
            Effect.BlindDown(invoiceClassTarifDl, {queue: 'end'});
        }
        else
        {
            if(invoiceClassTarifDl.getStyle('display') != 'none' & invoiceClassTarifDl.getOpacity() != 0.0)
            {
                Effect.BlindUp(invoiceClassTarifDl);
            }
            Effect.BlindDown(invoiceClassOtherDl, {queue: 'end'});
        }
    }

    this.deleteConfixxAccount = function(kundenId, contentHostingId)
    {
        conf = confirm("Soll das gesamte Webhosting-Paket des Kunden geloescht werden?");
        if(conf)
        {
            window.location.href = "index.php?page=kundenbearbeitung&bearbeiten=" + kundenId + "&deleteConfixxAccount=" + contentHostingId;
        }
    }

    this.deleteDomain = function(kundenId, contentHostingId)
    {
        conf = confirm("Soll die Domain des Kunden wirklich gekuendigt werden?");
        if(conf)
        {
            window.location.href = "index.php?page=kundenbearbeitung&bearbeiten=" + kundenId + "&deleteDomain=" + contentHostingId;
        }
    }

    this.invoiceDispatching = function(customerId, dispatch)
    {
        var conf = true;
        if(dispatch)
        {
            conf = confirm("Soll der Rechnungsversand wirklich aktiviert werden?");
            dispatch = 1;
        }
        else
        {
            conf = confirm("Soll der Rechnungsversand wirklich deaktiviert werden?");
            dispatch = 0;
        }

        if(conf)
        {
            window.location.href = "index.php?page=kundenbearbeitung&bearbeiten=" + customerId + "&rechnung_ausstellen=" + dispatch;
        }

    }

    this.rescheduleTarifOrderDate = function(tarifId, kundenId)
    {
        do
        {
            var days = prompt("Um wie viele Tage soll das Datum verschoben werden?", 5);
            if(days == null)
            {
                return;
            }
            else if(days > 7)
            {
                days = -1
                alert("Das Bestellungsdatum darf maximal um 7 Tage verschoben werden!");
            }
        }
        while(days < 0);

        window.location.href = "index.php?page=kundentarife&action=rescheduleOrderDate&kundenid="+kundenId+"&tarifId="+tarifId+"&days="+days;
    }
}

TarifUebersicht = new TarifUebersicht();

function TarifUebersicht()
{
    this.showBankConnectionEntryBox = function(tarifId)
    {
        var tarifIdHidden = $("tarifIdHidden");
        tarifIdHidden.value = tarifId;
        Effect.AppearOrFade("entryBoxDiv");
    }
}

ModeratorSeite = new ModeratorSeite();

function ModeratorSeite()
{
    this.changePlaylist = function(pPlaylistId, riseMeldung)
    {
        new Ajax.Request(
            "ajax.php",
            {
                parameters:
                {
                    action: "changePlaylist",
                    playlistId: pPlaylistId
                },
                onSuccess: function()
                {
                    if(riseMeldung)
                    {
                        meldungen.neu(84);
                    }

                    var buttonTable = $("buttonTable");
                    if(pPlaylistId > 0)
                    {
                        buttonTable.style.display = "";
                    }
                    else
                    {
                        buttonTable.style.display = "none";
                    }
                }
            }
        );
    }
}

TranslationInterface = new TranslationInterface();

function TranslationInterface()
{
    /**
     * Bla
     */
    this.translationArray = null;

    this.selectIndexByValue = function(elementId, value)
    {
        element = $(elementId);
        var selectIndex = 0;
        if (element.nodeName == "SELECT")
        {
            for(var i=0; i<element.options.length; i++)
            {
                if(element.options[i].value == value)
                {
                    selectIndex = i;
                }
            }
            element.selectedIndex = selectIndex;
        }
        else
        {
            alert("Given element is no HTML 'select' field. Is '"+elementId+"' the right element ID?");
        }
    }

    this.decodeJsonTranslationArray = function(jsonArray)
    {
        this.translationArray = {array: jsonArray};
        this.translationArray = this.translationArray.array;
        // Zugriff:
        // alert(this.translationArray.array[1].string);
    }

    this.showTranslationString = function(translationStringElementId)
    {
        var translationStringCode = $(translationStringElementId);
        var languageSelect =        $("translation_languages_id_fk");
        var sectionSelect =         $("translation_section_id_fk");
        var shortStringSelect =     $("translation_short_strings_id_fk");

        var languageId =    languageSelect.options[languageSelect.selectedIndex].value;
        var sectionId =     sectionSelect.options[sectionSelect.selectedIndex].value;
        var shortStringId = shortStringSelect.options[shortStringSelect.selectedIndex].value;

        translationStringCode.innerHTML = "";
        
        if(languageId > 0 && sectionId > 0 && shortStringId > 0)
        {
            for(var i=0; i < this.translationArray.length; i++)
            {
                if(languageId == this.translationArray[i].languageId
                    && sectionId == this.translationArray[i].sectionId
                    && shortStringId == this.translationArray[i].shortStringId)
                {
                    translationStringCode.innerHTML = this.translationArray[i].string;
                    return;
                }
            }
        }
    }

    this.changeSelectedIndex = function(languageCodeElementId, sectionElementId, shortStringElementId)
    {
        var languageSelect =    $(languageCodeElementId);
        var sectionSelect =     $(sectionElementId);
        var shortStringSelect = $(shortStringElementId);

        var languageId =    languageSelect.options[languageSelect.selectedIndex].value;
        var sectionId =     sectionSelect.options[sectionSelect.selectedIndex].value;
        var shortStringId = shortStringSelect.options[shortStringSelect.selectedIndex].value;

        //if(sectionId > 0)
        {
            // Short String Liste leeren
            for(var j = shortStringSelect.options.length - 1; j >= 0; j--)
            {
                shortStringSelect.remove(j);
            }
            // leeres Element einfuegen
            var optionElement = document.createElement("option");
            optionElement.value = 0;
            optionElement.text = " ";
            shortStringSelect.add(optionElement, null);

            for(var i=0; i < this.translationArray.length; i++)
            {
                if(sectionId == 0 || sectionId == this.translationArray[i].sectionId)
                {
                    optionElement = document.createElement("option");
                    optionElement.value = this.translationArray[i].shortStringId;
                    optionElement.text = this.translationArray[i].shortString;

                    shortStringSelect.add(optionElement, null);
                }
            }
            // Index auswaehlen
            this.selectIndexByValue(shortStringSelect, shortStringId);
        }

        this.showTranslationString("translation_string");
    }

    this.deleteString = function(stringId)
    {
        var conf = confirm("Soll der String wirklich geloescht werden?");
        if(conf)
        {
            window.location.href = "index.php?page=translation&action=deleteString&stringId="+stringId;
        }
    }

    this.editString = function(stringId, stringText)
    {
        Effect.AppearOrFade("entryBoxDiv");

        $("entryBoxForm").action = "index.php?page=translation&action=editString";

        $("entryBoxHeader").innerHTML = "Translation String";
        $("entryBoxSubheader").innerHTML = "";
        $("entryBoxDescription").innerHTML = "In was soll der String geaendert werden?";

        $("entryBoxHidden").value = stringId;
        $("entryBoxHidden").name  = "stringId";

        var entryBoxTextarea = $("entryBoxTextarea");
        entryBoxTextarea.innerHTML = atob(stringText);
        entryBoxTextarea.name      = "stringText";
        entryBoxTextarea.style.width = "100%";
        entryBoxTextarea.style.height = "200px";
    }
}

var MessageView = new MessageView()

function MessageView()
{
    this.deleteMessage = function(messageId, confirmText)
    {
        var conf = confirm(confirmText);
        if(conf)
        {
            window.location.href = "index.php?page=nachricht_ansehen&action=delete&id="+messageId;
        }
    }
}

function base64_encode(inp)
{
    var key="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var chr1,chr2,chr3,enc3,enc4,i=0,out="";
    while(i<inp.length)
    {
        chr1=inp.charCodeAt(i++);if(chr1>127) chr1=88;
        chr2=inp.charCodeAt(i++);if(chr2>127) chr2=88;
        chr3=inp.charCodeAt(i++);if(chr3>127) chr3=88;
        if(isNaN(chr3)) {enc4=64;chr3=0;} else enc4=chr3&63
        if(isNaN(chr2)) {enc3=64;chr2=0;} else enc3=((chr2<<2)|(chr3>>6))&63
        out+=key.charAt((chr1>>2)&63)+key.charAt(((chr1<<4)|(chr2>>4))&63)+key.charAt(enc3)+key.charAt(enc4);
    }
    return encodeURIComponent(out);
}


// script.aculo.us Erweiterung

Effect.AppearOrFade = function(element)
{
    element = $(element);
    if(element.getStyle('display') == 'none' | element.getOpacity() == 0.0)
        Effect.Appear(element);
    else
        Effect.Fade(element);
}

Effect.BlindDownOrUp = function(element)
{
    element = $(element);
    if(element.getStyle('display') == 'none' | element.getOpacity() == 0.0)
        Effect.BlindDown(element);
    else
        Effect.BlindUp(element);
}

// A J A X

function createXMLHttpRequest() {
    var reqmaker;
    if(window.XMLHttpRequest) {
        try {
            reqmaker = new XMLHttpRequest();
        } catch(e) {
            reqmaker = false;
        }
    } else if(window.ActiveXObject) {
        try {
            reqmaker = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
            reqmaker = false;
        }
    }
    req = reqmaker;
    return reqmaker;
}

function sendRequest(str, url, successFunc) {
	req = createXMLHttpRequest();
	req.open('post', url, true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	if(successFunc != undefined) {
    	req.onreadystatechange = function () {
		    if(req.readyState == 4){
				eval(successFunc);
		    }
    	}
    }
    else {
        req.onreadystatechange = function () {
            //eval("alert(req.readyState)");
        }
    }
    req.send(str);
}

function handleResponse(successFunc) {
    if(req.readyState == 4){
		successFunc;
    }
}