/********************************************/
/* F o r u m   J a v a s c r i p t          */
/* Author: Kieran Goodary                   */
/* Date: 20th Feb 2009                      */
/********************************************/

function Forum (accessLevel, scriptFile) {

    forumID = null;
    threadID = null;
    userAccessLevel = accessLevel;
    coreScript = scriptFile;
    
}

function showSettings () {

    alert (userAccessLevel + coreScript);
    
}
    
    /*********************************/
    /* Render the forum              */
    /*********************************/
    
    function renderForum (obj) 
    {
    
        $.ajax({
        
            type: "POST",
            url: coreScript,
            data: "action=renderForum",
            success: function(msg){
            
                $("#forumContainer").hide();
                
                if(document.getElementById("forumContainer")==null)
                {
                	//alert('soemthing is null'); -- some debug here???
                }
                
                document.getElementById("forumContainer").innerHTML = msg;
                
                $("#forumContainer").slideDown();
                
            }
            
        });
        
        $("#threadToolbar").slideUp();
        $("#postToolbar").slideUp();
    
    }
    
    /*********************************/
    /* Render the forum toolbar      */
    /*********************************/
    
    function renderToolbar () {
        
        $("#postToolbar").slideUp();
        $("#threadToolbar").slideUp();
        
        $.ajax({
        
            type: "POST",
            url: coreScript,
            data: "action=renderToolbar",
            success: function(msg){
            
                $("#forumToolbar").hide();
                document.getElementById("forumToolbar").innerHTML = msg;
                $("#forumToolbar").slideDown();
                
            }
            
        });

    }
    
    
    /*********************************/
    /* Dialog creator                */
    /*********************************/

    function openDialog (title, height, width, script) {

            $("#dialog").dialog({
            
                title: title,
                height: height,
                width: width,
                modal: true
                
            });
            
            document.getElementById("dialog").innerHTML = "<div style=\"padding: 6px; background-color: #EFEFEF; height: 100%;\">Please wait...</div>";

            $.ajax({

                type: "POST",
                url: coreScript,
                data: "action=" + script,
                success: function(msg){
                    
                    document.getElementById("dialog").innerHTML = msg;
                    $("#dialog").slideDown();
                    
                }
                
            });

    }
    
    /*********************************/
    /* Create Forum                  */
    /*********************************/    
    
    function closeDialog() {
        
            $("#dialog").dialog("close"); 
        
    }

    /*********************************/
    /* Create Forum                  */
    /*********************************/    

    function doCreateForum(name) {
    
        if (name == "") {
        
            alert ("Please type a name for the forum!");
        
        } else {

            $.ajax({

                type: "POST",
                url: coreScript,
                data: "action=doCreateForum&name=" + name,
                success: function(msg){
                
                    document.getElementById("createForumPane").innerHTML = msg;
                    renderForum();
                    /* setTimeout('closeDialog()', 1000); */
                    setTimeout('$(\'#dialog\').dialog(\'close\')', 1000); 
                    
                }
                
            });
        
        }

    }    
    
    /*********************************/
    /* Render Thread Toolbar         */
    /*********************************/    
    
    function renderThreadToolbar () {

        $("#postToolbar").slideUp();   

        $.ajax({

            type: "POST",
            url: coreScript,
            data: "action=renderThreadToolbar",
            success: function(msg){
            
                $("#threadToolbar").hide()

                document.getElementById("threadToolbar").innerHTML = msg;

                $("#threadToolbar").slideDown();
                
            }
            
        });
        
    }
    
    /*********************************/
    /* Render Post Toolbar           */
    /*********************************/ 

    function renderPostToolbar () {

        /*
        document.getElementById("postToolbar").innerHTML = "<div>Go back to topic list</div>";
        $("#postToolbar").slideDown();
        */
        
        $.ajax({

            type: "POST",
            url: coreScript,
            data: "action=renderPostToolbar",
            success: function(msg){
            
                $("#postToolbar").hide();
                
                document.getElementById("postToolbar").innerHTML = msg;
                
                $("#postToolbar").slideDown();
                
            }
            
        });
        
        
    }
    
    /*********************************/
    /* Render Thread List            */
    /*********************************/ 
    
    function renderThreadList (forumid) {

        if (!forumid) {
        
            forumid = forumID;
            
        } else {
        
            forumID = forumid;
            
        }
        
        $.ajax({

            type: "POST",
            url: coreScript,
            data: "action=renderThreadList&forumid=" + forumid,
            success: function(msg){
            
                $("#forumContainer").hide();
                
                document.getElementById("forumContainer").innerHTML = msg;
                
                $("#forumContainer").fadeIn(); 
                renderThreadToolbar();
                
            }
            
        });

    }

    /*********************************/
    /* Create a new thread           */
    /*********************************/ 

    function doCreateThread (threadName, threadDescription) {
    
    if (userAccessLevel > 2) {
    
        alert ("NOTICE: \nYour topic may be moderated - for this reason, your topic may not be displayed immediately.");
    
    }
    
        if (threadName == "") {
        
            alert ("Please type a topic name!");
        
        } else {
        
        var caughtThreadName = threadName;
        var caughtThreadDescription = threadDescription;
        
        document.getElementById("createThreadPane").innerHTML = "Creating thread, please wait...";
        
        /* DEBUG */
        /* alert (caughtThreadName + " : " + caughtThreadDescription); */
        /* alert (threadName + " : " + threadDescription); */
        
        $.ajax({

            type: "POST",
            url: coreScript,
            data: "action=doCreateThread&forumid=" + forumID + "&threadName=" + caughtThreadName + "&threadDescription=" + caughtThreadDescription,
            success: function(msg){

                document.getElementById("createThreadPane").innerHTML = msg;
                renderThreadList(forumID);
                setTimeout('$(\'#dialog\').dialog(\'close\')', 1000); 
               
            }
            
        });
        
        }

    }            
    
    /*********************************/
    /* Delete a Forum                */
    /*********************************/ 
    
    function deleteForum (id) {

        var answer = confirm("Are you sure you want to delete this forum?");
        
        if (answer) {

            $.ajax({

                type: "POST",
                url: coreScript,
                data: "action=deleteForum&forumid=" + id,
                success: function(msg){

                    /* DEBUG */
                    /* alert (msg); */
                    renderForum();
                   
                }
                
            });    
        
        }        
            
    }
    
    /*********************************/
    /* Delete a Thread               */
    /*********************************/ 
    
    function deleteThread (id) {
        
        if (!id) {
        
            id = threadID;
            
        }

        var answer = confirm("Are you sure you want to delete this thread?");
        
        if (answer) {

            $.ajax({

                type: "POST",
                url: coreScript,
                data: "action=deleteThread&topicid=" + id,
                success: function(msg){
                                                        
                    /* DEBUG */
                    /* alert (msg); */
                    renderThreadList(forumID);
                   
                }
                
            });    
        
        }        
            
    }
    
    /*********************************/
    /* Show Thread                   */
    /*********************************/ 
    
    function showThread(threadid) {

        threadID = threadid;

        $("#threadToolbar").slideUp();
        
            $.ajax({

                type: "POST",
                url: coreScript,
                data: "action=showThread&threadid=" + threadid,
                success: function(msg){

                    $("#forumContainer").hide();
                    
                    document.getElementById("forumContainer").innerHTML = msg;
                    
                    $("#forumContainer").slideDown();
                                             
                }
                
            });
          
        renderPostToolbar(); 
                    
    }
    
    /*********************************/
    /* Make a Post                   */
    /*********************************/
    
    function doPost(threadid, postTitle, post) {

        if (post == "") {
        
            alert ("Please type a post!");
        
        } else {
    
        document.getElementById("newPostContainer").innerHTML = "Please wait...";
        
            $.ajax({

                type: "POST",
                url: coreScript,
                data: "action=makePost&threadid=" + threadid + "&postTitle=" + postTitle + "&postText=" + post,
                success: function(msg){

                        document.getElementById("forumContainer").innerHTML = msg;
                        
                }
                
            });   

        }
    }
    
    
    
    /*********************************/
    /* Disapprove a Post             */
    /*********************************/
    
    function disapprovePost(postid) {

        $("#pID" + postid).hide(); 
        document.getElementById("pID" + postid).innerHTML = "Disapproving...";
        $("#pID" + postid).fadeIn();
        
            $.ajax({

                type: "POST",
                url: coreScript,
                data: "action=disapprovePost&postid=" + postid,
                success: function(msg){

                    document.getElementById("pID" + postid).innerHTML = msg;
                        
                }
                
            }); 

    }
    
    /*********************************/
    /* Approve a Post                */
    /*********************************/

    function approvePost (postid) {

        $("#pID" + postid).hide();
         
        document.getElementById("pID" + postid).innerHTML = "Approving...";
        
        $("#pID" + postid).fadeIn();
        
            $.ajax({

                type: "POST",
                url: coreScript,
                data: "action=approvePost&postid=" + postid,
                success: function(msg){

                        document.getElementById("pID" + postid).innerHTML = msg;
                         setTimeout('$(\'#pID' + postid + '\').slideUp()', 3000);
                        
                }
                
            });

    }
    
    /*********************************/
    /* Delete a Post                 */
    /*********************************/    
    
    function deletePost (postid) {

        var answer = confirm("Are you sure you want to delete this post permanently? This action cannot be undone!")
        if (answer){

            $("#pID" + postid).hide(); 
            document.getElementById("pID" + postid).innerHTML = "Deleting...";
            $("#pID" + postid).fadeIn();
            
            $.ajax({

                type: "POST",
                url: coreScript,
                data: "action=deletePost&postid=" + postid,
                success: function(msg){

                         $("#pID" + postid).fadeOut(2500); 
                        
                }
                
            });
                
        }

    }
    
    /*********************************/
    /* Edit Forum                    */
    /*********************************/ 
    
    function doEditForum (forumname, forumid) {
    
        if (forumname == "") {
        
            alert ("Please type a forum name!");
        
        } else {

            $.ajax({

                type: "POST",
                url: coreScript,
                data: "action=doEditForum&newForumName=" + forumname + "&forumID=" + forumid,
                success: function(msg){

                         document.getElementById("editForumPane").innerHTML = msg;
                         renderForum();
                         setTimeout('$(\'#dialog\').dialog(\'close\')', 1000);
                         
                        
                }
                
            });
            
        }

    }
    
    /*********************************/
    /* Edit Thread                   */
    /*********************************/ 
    
    function doEditThread (threadname, threadid) {
    
        if (threadname == "") {
        
            alert ("Please type a topic name!");
        
        } else {

            $.ajax({

                type: "POST",
                url: coreScript,
                data: "action=doEditThread&newThreadName=" + threadname + "&threadID=" + threadid,
                success: function(msg){

                         document.getElementById("editThreadPane").innerHTML = msg;
                         setTimeout('$(\'#dialog\').dialog(\'close\')', 1000); 
                        
                }
                
            });
            
        }

    }
    
    /*********************************/
    /* Approve Topic                 */
    /*********************************/ 
    
    function approveTopic (topicid) {

        $("#tID" + topicid).hide(); 
        document.getElementById("tID" + topicid).innerHTML = "Approving...";
        $("#tID" + topicid).fadeIn();
        
            $.ajax({

                type: "POST",
                url: coreScript,
                data: "action=approveTopic&topicid=" + topicid,
                success: function(msg){

                         $("#tID" + topicid).slideUp(); 
                        
                }
                
            });

    }
    
    /*********************************/
    /* Disapprove Topic              */
    /*********************************/
    /* WARNING: YOUR ARE DELETING!   */ 
    
    function disapproveTopic (topicid) {
    
        var answer = confirm("Are you sure you want to delete this topic permanently? This action cannot be undone!")
        if (answer){

        $("#tID" + topicid).hide(); 
        document.getElementById("tID" + topicid).innerHTML = "Deleting...";
        $("#tID" + topicid).fadeIn();
        
            $.ajax({

                type: "POST",
                url: coreScript,
                data: "action=deleteThread&topicid=" + topicid,
                success: function(msg){

                         $("#tID" + topicid).fadeOut(2500); 
                        
                }
                
            });
               
        }

    }
    
    
    function checkForOther(selectedValue) {
        
        if (selectedValue == "other") {
            
            document.getElementById("other_box").innerHTML = "<div>Please enter your other role below:</div><div><input type='text' name='other_role' id='other_role'>*</div>";
            
        } else {
            
            document.getElementById("other_box").innerHTML = "";
            
        }
        
    }
    
    function launchTermsAndConditions() {
        
        $("#dialog").dialog({
            
                title: "House Rules",
                height: 400,
                width: 550,
                modal: true,
                
                buttons:    {
                    
                    "Close": function() { 
                
                        $(this).dialog("close"); 
                        
                    } 
                
                }
                
        });
        
        $("#dialog").dialog("open");
        
        document.getElementById("dialog").innerHTML = "<div style=\"padding: 6px;\">Getting terms and conditions....</div>";
        
            $.ajax({
     
                type: "POST",
                url: "forum.php",
                async: false,
                data: "action=getTC",
                success: function(msg){
             
                    document.getElementById("dialog").innerHTML = msg;
                 
                 }

            });
        
              
        
    }
    
    function show_registration_form() {
        
        /* $("#login_container").slideUp(); */
        $("#register_button").slideUp();
        $("#register_container").slideDown();   
        
    }

    function checkRequiredFields() {
        
        document.getElementById("submit_button").value = "Validating information...";
        document.getElementById("submit_button").disabled = true;
        
        /**
        Initialise
        */
        
        error = 0;
        problem_fields = "";
        captcha_error = 0;
        
        /**
        First check reCAPTCHA
        */
        
        var field_recaptcha = document.getElementById("recaptcha_response_field").value;
        var field_recaptcha_challenge = document.getElementById("recaptcha_challenge_field").value;    
        
        if (field_recaptcha) {
            
            document.getElementById("submit_button").value = "Validating reCAPTCHA...";
            
            $.ajax({
     
                type: "POST",
                url: "forum.php",
                async: false,
                data: "action=check_reCAPTCHA&recaptcha_response_field=" + field_recaptcha + "&recaptcha_challenge_field=" + field_recaptcha_challenge,
                success: function(msg){
             
                    if (msg == "1") {
                            
                        captcha_error = 0;                            
                        
                    } else {
                        
                        
                        captcha_error = 1;   
                            
                    }
                 
                 }

            });
         
        }
               
        
        var username = document.getElementById("username").value;   
        var password = document.getElementById("password").value;   
        var firstname = document.getElementById("firstname").value;   
        var surname = document.getElementById("surname").value;   
        var email = document.getElementById("email").value;
        var tc = document.getElementById("terms_and_conditions").checked;
        
        /**
        Check to see if "other" was selected as a role
        */
        
        if (document.getElementById('role').value == "other") {
        
            var role = document.getElementById('other_role').value;
            if (!role) { error = 1; problem_fields += "Other Role \n"; };
        
        }   
        
        if (!username) { error = 1; problem_fields += "Username \n"; };
        if (!password) { error = 1; problem_fields += "Password \n"; };
        if (!firstname) { error = 1; problem_fields += "Firstname \n"; };
        if (!surname) { error = 1; problem_fields += "Surname \n"; };
        if (!email) { error = 1; problem_fields += "Email \n"; };
        if (!field_recaptcha) { error = 1; problem_fields += "reCAPTCHA Anti-Spam \n"; };
        if (!tc) { error = 1; problem_fields += "You must agree to the Terms and Conditions \n"; };
        
        if (captcha_error == 1) { error = 1; problem_fields += "reCAPTCHA Anti-Spam Result: The words you entered were not correct. Please try again!\n"; document.getElementById("help_information").innerHTML = "<div style=\"border: 2px solid #FFEB8F; background-color: #FFFFEF; margin-top: 6px; padding: 6 6 6 12;\"><div><b>Having problems with reCAPTCHA Anti-Spam?</b></div><div>If you are experiencing problems with the anti-spam system, please click on the reload icon inside the reCAPTCHA box to request another set of words, or the audio speaker icon to hear the words.</div></div>"; } else { document.getElementById("help_information").innerHTML = ""; };
        
        if (error == 1) {
            
            document.getElementById("submit_button").value = "You have errors in your form!";

            alert ("You have errors in one or more fields! \n\nPlease ensure that all the fields marked with an asterisk (*) are filled in, and try again.\nReported missing fields:\n\n" + problem_fields);
            
            document.getElementById("submit_button").value = "Register";
            document.getElementById("submit_button").disabled = false;
            
            return false;
            
        } else {
            
            document.getElementById("submit_button").value = "Submitting for registration...";
            document.getElementById("submit_button").disabled = true;
            
            document.reg_form_submit.submit();
            return true;
            
        }

    }


    
    
    
    function checkForOther(selectedValue) {
        
        if (selectedValue == "other") {
            
            document.getElementById("other_box").innerHTML = "<div>Please enter your other role below:</div><div><input type='text' name='other_role' id='other_role'>*</div>";
            
        } else {
            
            document.getElementById("other_box").innerHTML = "";
            
        }
        
    }
    
    function launchTermsAndConditions() {
        
        $("#dialog").dialog({
            
                title: "House Rules",
                height: 400,
                width: 550,
                modal: true,
                
                buttons:    {
                    
                    "Close": function() { 
                
                        $(this).dialog("close"); 
                        
                    } 
                
                }
                
        });
        
        $("#dialog").dialog("open");
        
        document.getElementById("dialog").innerHTML = "<div style=\"padding: 6px;\">Getting terms and conditions....</div>";
        
            $.ajax({
     
                type: "POST",
                url: "forum.php",
                async: false,
                data: "action=getTC",
                success: function(msg){
             
                    document.getElementById("dialog").innerHTML = msg;
                 
                 }

            });
        
              
        
    }
    
    function show_registration_form() {
        
        /* $("#login_container").slideUp(); */
        $("#register_button").slideUp();
        $("#register_container").slideDown();   
        
    }

    function checkRequiredFields() {
        
        document.getElementById("submit_button").value = "Validating information...";
        document.getElementById("submit_button").disabled = true;
        
        /**
        Initialise
        */
        
        error = 0;
        problem_fields = "";
        captcha_error = 0;
        
        /**
        First check reCAPTCHA
        */
        
        var field_recaptcha = document.getElementById("recaptcha_response_field").value;
        var field_recaptcha_challenge = document.getElementById("recaptcha_challenge_field").value;    
        
        if (field_recaptcha) {
            
            document.getElementById("submit_button").value = "Validating reCAPTCHA...";
            
            $.ajax({
     
                type: "POST",
                url: "forum.php",
                async: false,
                data: "action=check_reCAPTCHA&recaptcha_response_field=" + field_recaptcha + "&recaptcha_challenge_field=" + field_recaptcha_challenge,
                success: function(msg){
             
                    if (msg == "1") {
                            
                        captcha_error = 0;                            
                        
                    } else {
                        
                        
                        captcha_error = 1;   
                            
                    }
                 
                 }

            });
         
        }
               
        
        var username = document.getElementById("username").value;   
        var password = document.getElementById("password").value;   
        var firstname = document.getElementById("firstname").value;   
        var surname = document.getElementById("surname").value;   
        var email = document.getElementById("email").value;
        var tc = document.getElementById("terms_and_conditions").checked;
        
        /**
        Check to see if "other" was selected as a role
        */
        
        if (document.getElementById('role').value == "other") {
        
            var role = document.getElementById('other_role').value;
            if (!role) { error = 1; problem_fields += "Other Role \n"; };
        
        }   
        
        if (!username) { error = 1; problem_fields += "Username \n"; };
        if (!password) { error = 1; problem_fields += "Password \n"; };
        if (!firstname) { error = 1; problem_fields += "Firstname \n"; };
        if (!surname) { error = 1; problem_fields += "Surname \n"; };
        if (!email) { error = 1; problem_fields += "Email \n"; };
        if (!field_recaptcha) { error = 1; problem_fields += "reCAPTCHA Anti-Spam \n"; };
        if (!tc) { error = 1; problem_fields += "You must agree to the Terms and Conditions \n"; };
        
        if (captcha_error == 1) { error = 1; problem_fields += "reCAPTCHA Anti-Spam Result: The words you entered were not correct. Please try again!\n"; document.getElementById("help_information").innerHTML = "<div style=\"border: 2px solid #FFEB8F; background-color: #FFFFEF; margin-top: 6px; padding: 6 6 6 12;\"><div><b>Having problems with reCAPTCHA Anti-Spam?</b></div><div>If you are experiencing problems with the anti-spam system, please click on the reload icon inside the reCAPTCHA box to request another set of words, or the audio speaker icon to hear the words.</div></div>"; } else { document.getElementById("help_information").innerHTML = ""; };
        
        if (error == 1) {
            
            document.getElementById("submit_button").value = "You have errors in your form!";

            alert ("You have errors in one or more fields! \n\nPlease ensure that all the fields marked with an asterisk (*) are filled in, and try again.\nReported missing fields:\n\n" + problem_fields);
            
            document.getElementById("submit_button").value = "Register";
            document.getElementById("submit_button").disabled = false;
            
            return false;
            
        } else {
            
            document.getElementById("submit_button").value = "Submitting for registration...";
            document.getElementById("submit_button").disabled = true;
            
            document.reg_form_submit.submit();
            return true;
            
        }

    }
