
/* AJAX FUNCTIONS */


/*

	Functions for MenuItems

*/
function editTitleAjax(id,title,suffix) {
    var tmpl = $("#tmpl_changeTitle").val();
    $("div#messageBox").html(tmpl);
    $("#tmpl_changeTitle_title").attr('value',title);
    $("#tmpl_changeTitle_suffix").attr('value',suffix);
    $("#tmpl_changeTitle_id").attr('value',id);
    tb_show("Seitentitel editieren","#TB_inline?height=155&width=300&inlineId=messageBox","");
    $("#tmpl_changeTitle_title").focus();
}

function editKeywordsAjax(id,keywords) {
    var tmpl = $("#tmpl_keywords").val();
    $("div#messageBox").html(tmpl);
    $("#tmpl_keywords_keywords").html(keywords.replace(" , ","\n"));
    $("#tmpl_keywords_id").attr('value',id);
    tb_show("Suchwörter editieren","#TB_inline?height=155&width=300&inlineId=messageBox","");
    $("#tmpl_keywords_keywords").focus();

}
function saveKeywordsAjax() {
    var id = $("#tmpl_keywords_id").val();
    var title = $("#tmpl_keywords_keywords").val();
    title = title.replace("\n"," , ");
    var data = new Object ();
    data["id"] = id;
    data["value"] = title;
    doAjaxAction("saveKeywords",data);
}

function saveAuthorAjax(id) {
    $("div#messageBox").html("<p align='center' style='padding-top:45px'>Speichere Author</p><p style='text-align:center;'><img src='/media/resources/loadinganimation.gif'></p>");
    tb_show("Nachricht","#TB_inline?height=155&width=300&inlineId=messageBox&modal=true",null);
    var author = $("#site_author").val();
    var data = new Object ();
    data["id"] = id;
    data["value"] = author;
    doAjaxAction("saveAuthor",data);
}

function saveEditTitleAjax() {
    var id = $("#tmpl_changeTitle_id").val();
    var title = $("#tmpl_changeTitle_title").val();
    var suffix = $("#tmpl_changeTitle_suffix").val();
    var data = new Object ();
    data["id"] = id;
    data["value"] = title;
    data["suffix"] = suffix;
    doAjaxAction("editTitle",data);
    $("#menuitem_"+id).html(title);
}

function addChildAjax(id) {
    var tmpl = $("#tmpl_addChild").val();
    $("div#messageBox").html(tmpl);
    $("#tmpl_addChild_id").attr('value',id);
    tb_show("Ein Kind hinzufügen","#TB_inline?height=155&width=300&inlineId=messageBox","");
    $("#tmpl_addChild_title").focus();
}

function saveAddChildAjax() {
    var id = $("#tmpl_addChild_id").val();
    var title = $("#tmpl_addChild_title").val();
    var suffix = $("#tmpl_addChild_suffix").val();
    var data = new Object ();
    data["id"] = id;
    data["value"] = title;
    data["suffix"] = suffix;
    doAjaxAction("addChild",data);
}


function addNeighbourAjax(id) {
    var tmpl = $("#tmpl_addNeighbour").val();
    $("div#messageBox").html(tmpl);
    $("#tmpl_addNeighbour_id").attr('value',id);
    tb_show("Einen Nachbarn hinzufügen","#TB_inline?height=155&width=300&inlineId=messageBox",null);
    $("#tmpl_addNeighbour_title").focus();
}

function saveAddNeighbourAjax() {
    var id = $("#tmpl_addNeighbour_id").val();
    var title = $("#tmpl_addNeighbour_title").val();
    var suffix = $("#tmpl_addNeighbour_suffix").val();
    var data = new Object ();
    data["id"] = id;
    data["value"] = title;
    data["suffix"] = suffix;
    doAjaxAction("addNeighbour",data);
}

function addRootAjax() {
    var tmpl = $("#tmpl_addRoot").val();
    $("div#messageBox").html(tmpl);
    tb_show("Eine Wurzel hinzufügen","#TB_inline?height=155&width=300&inlineId=messageBox",null);
    $("#tmpl_addRoot_title").focus();
}

function saveAddRootAjax() {
    var title = $("#tmpl_addRoot_title").val();
    var suffix = $("#tmpl_addRoot_suffix").val();
    var data = new Object ();
    data["value"] = title;
    data["suffix"] = suffix;
    doAjaxAction("addRoot",data);
}


function removeItemAjax(id) {
    var tmpl = $("#tmpl_removeItem").val();
    $("div#messageBox").html(tmpl);
    $("#tmpl_removeItem_id").attr('value',id);
    tb_show("Eine Seite entfernen","#TB_inline?height=155&width=300&inlineId=messageBox",null);
}


function saveRemoveItemAjax() {
    var id = $("#tmpl_removeItem_id").val();
    var data = new Object ();
    data["id"] = id;
    doAjaxAction("removeItem",data);
}

/*

	Functions for preferences

*/



function savePublicStatusAjax(id,field,setValue) {
    var value = document.getElementById(field).checked;
    document.getElementById(field).checked = !value;
    if (setValue==true) {
        document.getElementById(field).checked = value;
    }
    $("div#messageBox").html("<p align='center' style='padding-top:45px'>Speichere Status</p><p style='text-align:center;'><img src='/media/resources/loadinganimation.gif'></p>");
    tb_show("Nachricht","#TB_inline?height=155&width=300&inlineId=messageBox&modal=true",null);
    var data = new Object ();
    data["id"] = id;
    data["value"] = value;
    doAjaxAction("savePublicStatus",data);
}

function saveInMenuStatusAjax(id,field,setValue) {
    var value = document.getElementById(field).checked;
    document.getElementById(field).checked = !value;
    if (setValue==true) {
        document.getElementById(field).checked = value;
    }
    $("div#messageBox").html("<p align='center' style='padding-top:45px'>Speichere Status</p><p style='text-align:center;'><img src='/media/resources/loadinganimation.gif'></p>");
    tb_show("Nachricht","#TB_inline?height=155&width=300&inlineId=messageBox&modal=true",null);
    var data = new Object ();
    data["id"] = id;
    data["value"] = value;
    doAjaxAction("saveInMenuStatus",data);
}

function saveInTopMenuStatusAjax(id,field,setValue) {
    var value = document.getElementById(field).checked;
    document.getElementById(field).checked = !value;
    if (setValue==true) {
        document.getElementById(field).checked = value;
    }
    $("div#messageBox").html("<p align='center' style='padding-top:45px'>Speichere Status</p><p style='text-align:center;'><img src='/media/resources/loadinganimation.gif'></p>");
    tb_show("Nachricht","#TB_inline?height=155&width=300&inlineId=messageBox&modal=true",null);
    var data = new Object ();
    data["id"] = id;
    data["value"] = value;
    doAjaxAction("saveInTopMenuStatus",data);
}

function saveRestricedStatusAjax(id,field,setValue) {
    var value = document.getElementById(field).checked
    document.getElementById(field).checked = !value;
    if (setValue==true) {
        document.getElementById(field).checked = value;
    }
    $("div#messageBox").html("<p align='center' style='padding-top:45px'>Speichere Status</p><p style='text-align:center;'><img src='/media/resources/loadinganimation.gif'></p>");
    tb_show("Nachricht","#TB_inline?height=155&width=300&inlineId=messageBox&modal=true",null);
    var data = new Object ();
    data["id"] = id;
    data["value"] = value;
    doAjaxAction("saveRestrictedStatus",data);
}


function saveForwardingAjax(id,field,setValue) {
    var value = document.getElementById(field).checked;
    document.getElementById(field).checked = !value;
    if (setValue==true) {
        document.getElementById(field).checked = value;
    }
    $("div#messageBox").html("<p align='center' style='padding-top:45px'>Speichere Weiterleitung</p><p style='text-align:center;'><img src='/media/resources/loadinganimation.gif'></p>");
    tb_show("Nachricht","#TB_inline?height=155&width=300&inlineId=messageBox&modal=true",null);
	
    var data = new Object ();
    data["id"] = id;
    data["value"] = value;
    doAjaxAction("saveForwarding",data);
}


function saveModuleAjax() {
    var module = document.getElementById('moduleName').value;
    $("div#messageBox").html("<p align='center' style='padding-top:45px'>Speichere Modul</p><p style='text-align:center;'><img src='/media/resources/loadinganimation.gif'></p>");
    tb_show("Nachricht","#TB_inline?height=155&width=300&inlineId=messageBox&modal=true",null);
	
    var data = new Object ();
    data["id"] = siteID;
    data["value"] = module;
    doAjaxAction("saveModule",data);
}


function saveBannerAjax() {
    var banner = document.getElementById('bannerName').value;
    $("div#messageBox").html("<p align='center' style='padding-top:45px'>Speichere Modul</p><p style='text-align:center;'><img src='/media/resources/loadinganimation.gif'></p>");
    tb_show("Nachricht","#TB_inline?height=155&width=300&inlineId=messageBox&modal=true",null);

    var data = new Object ();
    data["id"] = siteID;
    data["value"] = banner;
    doAjaxAction("saveBanner",data);
}


function saveGeneralAjax() {
    var title = document.getElementById('general_title').value;
    document.getElementsByTagName("title")[0].value = title;
    var footText = document.getElementById('general_footText').value;
    $("#footText").html(footText);
    var author = document.getElementById('general_author').value;
    $("meta[name='author']").attr('content',author);
    var keywords = document.getElementById('general_keywords').value;
    $("meta[name='keywords']").attr('content',keywords);
    var pagetype = document.getElementById('general_pagetype').value;
    $("meta[name='page-topic']").attr('content',pagetype);
    var pagetopic = document.getElementById('general_pagetopic').value;
    $("meta[name='page-type']").attr('content',pagetopic);
    var decription = document.getElementById('general_description').value;
    $("meta[name='description']").attr('content',decription);
    var dateformat = document.getElementById('general_dateformat').value;
    //alert(decription);
    //cleanMessyCode('general_banner');
    //var banner = document.getElementById('general_banner').value;
    //$("#bannerImage").attr('src',banner);

    var theme = document.getElementById('sitestyleSelector').value;
    var email = document.getElementById('general_email').value;
	
    var data = new Object ();
    data["title"] = title;
    data["footText"] = footText;
    data["author"] = author;
    data["keywords"] = keywords;
    data["pagetype"] = pagetype;
    data["pagetopic"] = pagetopic;
    data["decription"] = decription;
    //data["banner"] = banner;
    data["theme"] = theme;
    data["email"] = email;
    data["dateformat"] = dateformat;
	
    doAjaxAction("saveGeneral",data);
}

/*

	Functions for Text

*/

function saveTextAjax(id,value) {
    $("div#messageBox").html("<p align='center' style='padding-top:45px'>Speichere Text</p><p style='text-align:center;'><img src='/media/resources/loadinganimation.gif'></p>");
    tb_show("Nachricht","#TB_inline?height=155&width=300&inlineId=messageBox&modal=true",null);
    var data = new Object ();
    data["id"] = id;
    data["value"] = value;
    doAjaxAction("saveText",data);
}

function savePasswordAjax() {
	
    var data = new Object ();
	
    data["oldPass"] = $('#adminPasswordChange #oldPass').val();
    data["newPass"] = $('#adminPasswordChange #newPass').val();
    data["confirmNewPass"] = $('#adminPasswordChange #confirmNewPass').val();
    doAjaxAction("changePass",data);
}


function doBackup() {
	
    var data = new Object ();
    doAjaxAction("backup",data,"backup");
}


function doRestore() {
	
    var data = new Object ();
    data["date"] = $("#adminBackupSelect").val();
    doAjaxAction("restore",data,"backup");
}



/*

	AJAX POST FUNCTION

*/

function doAjaxAction (action, data, file) {
    if (arguments.length==2) {
        file ="actionJSON";
    }
    $("#TB_ajaxContent").ajaxStart(function() {
        /*$(this).html("<p style='text-align:center;padding-top:55px'>Bitte warten ...<br><img src='/media/resources/loadinganimation.gif'></p>");*/
        $(this).html("<table width='100%' height='100%'><tr align='center' valign='middle'><td id='innerMsg'>Bitte warten ...<br><img src='/media/resources/loadinganimation.gif'></td></tr></table>");
    });
 	
    data["action"] = action;

    $.ajax({
        type: 		"POST",
        url:		base_path+"lib/"+file+".php",
        data:		data,
        dataType:	"json",
        success:	function(result) {
            if (!result.success) {
                $("#innerMsg").html("<p>"+result.msg+"</p>");
                $("#innerMsg").append("<p><button onClick='tb_remove_custom()'>schliessen</button>");
            } else {
                $("#innerMsg").html("<p>"+result.msg+"</p>");
                switch (result.action) {
                    case "changeValue":
                        document.getElementById(result.key).value = eval(result.value);
                        window.setTimeout("tb_remove_custom()",1500);
                        break;
                    case "changeKey":
                        document.getElementById(result.key).checked = eval(result.value);
                        window.setTimeout("tb_remove_custom()",1500);
                        break;
                    case "reload":
                        window.setTimeout("self.location.href='"+siteURLPath+"';",1500);
                        break;
                    case "gotoPage":
                        window.setTimeout("self.location.href='"+result.page+"'",1500);
                        break;
                    case "updateGeneral":
                        window.setTimeout("self.location.href='"+siteURLPath+"';",1500);
                        /*
						window.setTimeout("tb_remove()",1500);
						changeTheme(result.theme);
						changeBanner(result.banner);
						*/
                        break;
                    case "home":
                        if (!isAdmin)
                            window.setTimeout("self.location.href='"+base_path+"'",1500);
                        else
                            window.setTimeout("self.location.href='"+base_path+"admin/'",1500);

                        break;

                    default:
                        window.setTimeout("tb_remove_custom()",1500);
                }
            /*
				if (result.key) {
					$("#"+result.key).text(result.value);
				}
				*/
            }
        },
        error:	function(XMLHttpRequest, textStatus, errorThrown) {
            $("#innerMsg").html("<p>Beim übertragen ist ein Fehler aufgetreten.<br>Fehler: "+textStatus+"</p>");
            $("#innerMsg").append("<p style='text-align:center;'><button onClick='tb_remove()'>schliessen</button>");
        }
    });
}

