﻿var showCategory=null;
var showForum=null;
var showThread=null;
var receivingDiv=null;

// Function

function forumDisplay(src,category,forum,thread,unexpand) {
    if (src==null) {
        docUrl="/inside/Admin.ashx?cmd=forum&displayForum=all";
        receivingDiv=null;
    }else{
        docUrl="/inside/Admin.ashx?cmd=forum&displayForum=1";
        document.getElementById(src).style.borderLeftColor="#f00";
        receivingDiv=src;
    }
    showCategory=category;
    showForum=forum;
    showThread=thread;
    if (category!=null) docUrl+="&category="+category;
    if (forum!=null) docUrl+="&forum="+forum;
    if (thread!=null) docUrl+="&thread="+thread;
    if (unexpand!=null) docUrl+="&unexpanded=1";
    sendRequest(docUrl,forumReceived);
}
function forumReceived(reply) {
    if (receivingDiv==null) {
        setInnerHTML("forumContainer",reply);
    }else{
        document.getElementById(receivingDiv).style.borderLeftColor="#fff";
        //fader(document.getElementById(receivingDiv),100);
        if (document.getElementById(receivingDiv).innerHTML!=reply) setInnerHTML(receivingDiv,reply);
    }
}
function displayForm(reply) {
    if (receivingDiv==null) {
        startFadeUp();
        pasteRawHTML(returnRoundedBox("catAdmin","",reply,true,5,null,true,false),document.body);
        centerObject(document.getElementById("catAdmin"));
    }else{
        //setInnerHTML(receivingDiv.id,reply);
        receivingDiv.innerHTML=reply;
        initiateMCE();
    }
}
function refreshForum(reply) {
    //alert(reply);
    forumDisplay(null,showCategory,showForum,showThread);
}

// Categories

function createCategory() {
    receivingDiv=null;
    sendRequest("/inside/Admin.ashx?cmd=forum&newCategory=1",displayForm);
}
function submitCategory() {
    showCategory=null;
    showForum=null;
    showThread=null;
    addCategory(encodeSafe(document.getElementById("catName").value),document.getElementById("imgfile").value);
    removeTheNode("catAdmin");
    startFadeDown();
}
function editCategory(catid) {
    showCategory=null;
    showForum=null;
    showThread=null;
    sendRequest("/inside/Admin.ashx?cmd=forum&editCategory="+catid,displayForm);
}
function deleteCategory(catid) {
    showCategory=null;
    showForum=null;
    showThread=null;
    //alert("delete cat "+catid);
    sendRequest("/inside/Admin.ashx?cmd=forum&deleteCategory="+catid,refreshForum);
}
function addCategory(name,imagefile) {
    showCategory=null;
    showForum=null;
    showThread=null;
    sendPostRequest("/inside/Admin.ashx","cmd=forum&newCategory="+name.replace("&","(amp)")+"&image="+imagefile,refreshForum);
}
function updateCategory(id) {
    showCategory=catid;
    showForum=null;
    showThread=null;
    sendPostRequest("/inside/Admin.ashx","cmd=forum&editCategory="+id+"&name="+document.getElementById("catName").value+"&image="+document.getElementById("imgfile").value,refreshForum);
    removeTheNode("catAdmin");
    startFadeDown();
}

// Forums

function createForum(cat) {
    receivingDiv=null;
    sendRequest("/inside/Admin.ashx?cmd=forum&newForum="+cat,displayForm);
}
function submitForum(catid) {
    addForum(catid,encodeSafe(document.getElementById("catName").value),encodeSafe(document.getElementById("imgfile").value));
    removeTheNode("catAdmin");
    startFadeDown();
}
function updateForum(id) {
    showForum=null;
    showThread=null;
    sendPostRequest("/inside/Admin.ashx","cmd=forum&editForum="+id+"&name="+document.getElementById("catName").value+"&image="+document.getElementById("imgfile").value,refreshForum);
    removeTheNode("catAdmin");
    startFadeDown();
}
function deleteForum(forumid) {
    showForum=null;
    showThread=null;
    sendRequest("/inside/Admin.ashx?cmd=forum&deleteForum="+forumid,refreshForum);
}
function editForum(forumid) {
    sendRequest("/inside/Admin.ashx?cmd=forum&editForum="+forumid,displayForm);
}
function addForum(catid,name,imagefile) {
    showCategory=catid;
    showForum=null;
    showThread=null;
    sendPostRequest("/inside/Admin.ashx","cmd=forum&newForum="+name.replace("&","(amp)")+"&image="+imagefile+"&catid="+catid,refreshForum);
}

// Threads

function addThread(forumid,name) {
    showForum=forumid;
    showThread=null;
    sendPostRequest("/inside/Admin.ashx","cmd=forum&newThread="+name.replace("&","(amp)")+"&forumid="+forumid,refreshForum);
}
function createThread(forumid) {
    receivingDiv=null;
    sendRequest("/inside/Admin.ashx?cmd=forum&newThread="+forumid,displayForm);
}
function submitThread(forumid) {
    addThread(forumid,encodeSafe(document.getElementById("catName").value));
    removeTheNode("catAdmin");
    startFadeDown();
}
function deleteThread(threadid) {
    showThread=null;
    sendRequest("/inside/Admin.ashx?cmd=forum&deleteThread="+threadid,refreshForum);
}

// Posts
function deletePost(postid) {
    sendRequest("/inside/Admin.ashx?cmd=forum&deletePost="+postid,refreshForum);
}
function addPost(threadid,content) {
    showThread=threadid;
    //tinyMCE=null;
    sendPostRequest("/inside/Admin.ashx","cmd=forum&newPost="+threadid+"&posting="+content,refreshForum);
}
function createPost(threadid) {
    receivingDiv=null;
    openEditor(document.getElementById("addToThread"+threadid),"PostEditor"+threadid,"");
    //receivingDiv.innerHTML="<div style=\"padding:10px;border:1px solid #98ae6e;background-color:#fff\">Loading...</div>";
    //sendRequest("/inside/Admin.ashx?cmd=forum&newPost="+threadid,displayForm);
}
function submitPost(threadid,thePostContent) {
    addPost(threadid,encodeSafe(thePostContent));
}
function showHTML(src) {
    toShow=src.innerHTML.replace(/TEXTAREA/g,"textedarea");
    alert("<textarea col=\"80\" rows=\"20\">"+toShow+"</textarea>");
}
var favButton=null;
function setFavThread(src,threadid,state) {
    favButton=src.parentNode;
    favButton.innerHTML="Saving...";
    if (state==1) {
        sendPostRequest("/inside/Admin.ashx","cmd=forum&setFaveOn=1&threadid="+threadid,favSet);
    }else{
        sendPostRequest("/inside/Admin.ashx","cmd=forum&setFaveOff=1&threadid="+threadid,favSet);
    }
}
function favSet(reply) {
    if (favButton!=null) {
        favButton.innerHTML=reply;
        favButton=null;
    }
}
function searchForum() {
    terms=document.getElementById("searchTerms").value.replace(" ",";");
    //alert("/forum.aspx?search="+terms);
    gotoUrl("/forum.aspx?search="+terms);
}
function checkAlphaNumericKeys(e) {
    var key;
    var keychar;
    reply=true;
    if (window.event) {
       key = window.event.keyCode;
    } else if (e) {
       key = e.which;
    } else {
        reply=true;
    }
    keychar = String.fromCharCode(key);
    keychar = keychar.toLowerCase();

    // control keys
    if ((key==null) || (key==0) || (key==8) || (key==9) || (key==27) ) {
        reply=true;
    } else if (key==13) {
        searchForum();
        reply=false;
    // alphas and numbers
    } else if ((("abcdefghijklmnopqrstuvwxyz0123456789 ").indexOf(keychar) > -1)) {
        reply=true;
    } else {
        reply=false;
    }
    return reply;
}