﻿/* Forum scripting
--------------------------  */

function previewtopic(type) {

    // Disable button and show animation
    disableform(document.getElementById("replyform"));
    document.getElementById("ajaxloader").className = "";
    
    // Validate the form entries
    if (!validate()) {
        enableform(document.getElementById("replyform"));
        document.getElementById("ajaxloader").className = "hidden";
        return false;
    }
    
    // Show the preview
    if (type == "question") {
        document.getElementById("previewtitle").innerHTML = "<span>The Scent ... </span>" + document.getElementById("toptitle").value.trimend("?") + "?";
    }
    else {
        document.getElementById("previewtitle").innerHTML = "<span>The Scent ... </span>" + document.getElementById("toptitle").value;
    }

    // Check for quotes
    var postval = document.getElementById("topdesc").value
    postval = ParseHTML(postval)

    document.getElementById("previewdetails").innerHTML = postval.replace(/\n/g, "<br />");
    enableform(document.getElementById("replyform"));
    document.getElementById("ajaxloader").className = "hidden";
    document.getElementById("replyform").className = "hidden";
    document.getElementById("previewform").className = "forum topic";
}

function edittopic() {
    // Show the form
    document.getElementById("replyform").className = "forum topic";
    document.getElementById("previewform").className = "hidden";
}

function addtopic(type) {

    var topic, details, catid;

     // Validate the form entries again
    if (!validate()) {
        edittopic();
        return false;
    }

    if (type == "question") {
        type = "2";
        topic = document.getElementById("toptitle").value.trimend("?");
    }
    else {
        type = "1";
        topic = document.getElementById("toptitle").value;
    }

    details = document.getElementById("topdesc").value;
    details = ParseHTML(details);
    catid = document.getElementById("topcat").value;

    // Save details
    TheSniffa.Add_Forum_Topic(topic, details, catid.toString(), type, addtopiccallback);

}

function addtopiccallback(response)
{
    // Check for errors
    if(response.error != null) {
    	alert("A system error occurred. Please try again. Error message: " + response.error.Message);
        // Enable button and hide animation
        enableform(document.getElementById("replyform"));
        document.getElementById("ajaxloader").className = "hidden";
		return;
	}
    if (response.value == "error timeout") {
        window.location.href = "/";
    }
    else {
        // Get the response value
        window.location.href = response.value;
    }

}

function canceladdtopic(cat) {
    if (cat == "") {
        window.location.href = "/forum.aspx";
    }
    else {
        window.location.href = "/forum/" + cat + "/topics.aspx";
    }
}

function cancelupdatetopic(tid, cat) {
    if (tid == "" || cat == "") {
        window.location.href = "/forum.aspx";
    }
    else {
        window.location.href = "/forum/" + cat + "/topic.aspx?t=" + tid;
    }
}

function updatetopic(tid) {

    var details;

     // Validate the form entries again
    if (!validate()) {
        edittopic();
        return false;
    }

    details = document.getElementById("topdesc").value;
    details = ParseHTML(details);

    // Save details
    TheSniffa.Edit_Forum_Topic(tid, details, addtopiccallback);

}

function previewpost() {
    // Disable button and show animation
    disableform(document.getElementById("replyform"));
    document.getElementById("ajaxloader").className = "";
    
    // Validate the form entries
    if (!validate()) {
        enableform(document.getElementById("replyform"));
        document.getElementById("ajaxloader").className = "hidden";
        return false;
    }
    
    // Check for quotes
    var postval = document.getElementById("replydets").value
    postval = ParseHTML(postval)

    // Show the preview
    document.getElementById("previewdetails").innerHTML = postval.replace(/\n/g, "<br />");
    enableform(document.getElementById("replyform"));
    document.getElementById("ajaxloader").className = "hidden";
    document.getElementById("replyform").className = "hidden";
    document.getElementById("previewform").className = "forum topic";
}

function postreply(tid, pid, catid, type) {

    var details;

     // Validate the form entries again
    if (!validate()) {
        edittopic();
        return false;
    }

    details = document.getElementById("replydets").value;
    details = ParseHTML(details);

    // Save details
    TheSniffa.Add_Forum_Post(details, tid.toString(), pid.toString(), catid.toString(), type.toString(), postreplycallback);

}

function updatepostreply(pid) {

    var details;

     // Validate the form entries again
    if (!validate()) {
        edittopic();
        return false;
    }

    details = document.getElementById("replydets").value;
    details = ParseHTML(details);

    // Save details
    TheSniffa.Edit_Forum_Post(details, pid.toString(), postreplycallback);
}

function postreplycallback(response)
{
    // Check for errors
    if(response.error != null) {
    	alert("A system error occurred. Please try again. Error message: " + response.error.Message);
        // Enable button and hide animation
        enableform(document.getElementById("replyform"));
        document.getElementById("ajaxloader").className = "hidden";
		return;
	}
    if (response.value == "error timeout") {
        window.location.href = "/";
    }
    else {
        // Get the response value
        window.location.href = response.value;
    }

}

function cancelpostreply(cat, tid) {
    window.location.href = "/forum/" + cat + "/topic.aspx?t=" + tid;
}

function deletepost(pid, type) {
    // confirm
    var posttext = "post";
    if (type == 2) {
        posttext = "answer";
    }
    if (confirm("Confirm Deletion\r\n\r\nAre you sure you would like to delete your " + posttext + "?\r\nThe " + posttext + " will be permanently deleted.")) {
        // Delete photo
        TheSniffa.Delete_Forum_Post(pid.toString(), postreplycallback);
    }
}

// Set up my regular expressions that will match the HTML tags and attributes that I want to allow
var reAllowedAttributes = /^(linked)$/i
var reAllowedHTMLTags = /^(QUOTE)$/i

function ParseHTML(theHTML){
    // Start off with a test to match all HTML tags and a group for the tag name which we pass in as an extra parameter
    theHTML = theHTML.replace(/<[/]?([^> ]+)[^>]*>/g, function(match,HTMLTag)
    {
        // if the HTML tag does not match our list of allowed tags return empty string which will be used as a
        // a replacement for the pattern in our inital test.
        if (!reAllowedHTMLTags.test(HTMLTag)) {
            return "";
        }
        else {
            // The HTML tag is allowed so check attributes with the tag

            /*/ Certain attributes are allowed so we do another replace statement looking for attributes and using another
            // function for the replacement value.
            match = match.replace(/ ([^=]+)="[^"]*"/g, function(match2, attributeName)
            {
                // If the attribute matches our list of allowed attributes we return the whole match string
                // so we replace our match with itself basically allowing the attribute.
                if (reAllowedAttributes.test(attributeName)){
                    return match2;
                }
                else {
                    return "";  // not allowed so return blank string to wipe out the attribute value pair
                }
            });
            */
            if (match.indexOf("linked") > -1) {
                var quotefrom = document.getElementById("hdnorigposter").value.split(",");
                return "<p class=\"quote\"><span>Quote from <a href=\"#" + quotefrom[1] + "\">" + quotefrom[0] + "</a></span>"
            }
            else if (match.startswith("</")) {
                return "</p>";
            }
            else {
                return "<p class=\"quote\">";
            }

        }
        return match;
    }); //end of the first replace

    //return our cleaned HTML
    return theHTML;
}

