﻿var Forum = {
    id: 4,
    currentPage: 1,
    newPostData: { parent_id: 0, post_id: 0, title: '', text: '', URL: '', Type: 1 },

    SetPostType: function(type) {

        this.newPostData.Type = type;
        if (type == 1) {
            $('postActionTitle').innerHTML = 'Ask question';
        } else {
            $('postActionTitle').innerHTML = 'Answer question';
        }

    },
    IsSinglePost: function() {

        if (window.location.pathname.indexOf('/QA/Posts/', 0) != -1) {
            return true;
        }
        return false;

    },
    AddPost: function(parent_id) {



        if (!IsConnected()) {
            RegisterNotify();
            return;
        }

        this.CleanError();

        this.newPostData.parent_id = parent_id;
        this.newPostData.post_id = 0;
        $('txtTitle').value = '';

        $('txtContent').value = '';
        if (parent_id == 2000) {
            $('postTypeDIV').style.display = "none";
            this.SetPostType(1);
        } else {
            $('postTypeDIV').style.display = "";
            this.SetPostType(2);
        }
        $('txtFileURL').value = '';


        ShowItemEditor('ForumPostEditor');



    },
    SavePost: function() {
        this.CleanError();
        this.newPostData.title = $('txtTitle').value;
        this.newPostData.text = $('txtContent').value;
        this.newPostData.URL = $('txtFileURL').value;
        if (Trim(this.newPostData.title) == '') {
            this.ShowError('Please Enter a title');
            return;
        }


        var res = DojitAjax.ForumPostSave(this.newPostData.post_id, this.newPostData.parent_id, this.newPostData.title, this.newPostData.text, this.newPostData.URL, this.id, this.newPostData.Type);

        if (res.error != null) {
            alert(res.error.description);
            return;
        }
        if (Forum.IsSinglePost()) {
            window.location.reload();
        } else {
            this.ShowPage(this.currentPage);
        }
        CloseItemEditor();

    },
    TogglePostContent: function(id) {
        elemPost = $('post_content_' + id);
        if (elemPost.style.display == 'none') {
            elemPost.style.display = '';
        } else elemPost.style.display = 'none';
    },
    ShowPage: function(pageId) {

        var res = DojitAjax.QAForumGetTableById(this.id, pageId);

        if (res.error != null) {
            alert(res.error.description);
        }

        this.currentPage = pageId;
        $('ForumHolder').innerHTML = res.value;
    },
    Search: function() {

        var query = $('txtSearch').value;
        if (!query) return;
        var res = DojitAjax.QAForumSearch(this.id, query);

        if (res.error != null) {
            alert(res.error.description);
        }

        this.currentPage = 1;
        $('ForumHolder').innerHTML = res.value;
    },

    SearchOnEnter: function(event) {

        var key;

        if (window.event) {
            key = window.event.keyCode; //IE
        }
        else {
            key = event.which; //firefox
        }

        if (key == 13) {

            Forum.Search();
            return false;

        }


    },
    ShowError: function(msg) {
        $('spnPostErrorMsg').innerHTML = msg;
    }
,
    CleanError: function() {

        $('spnPostErrorMsg').innerHTML = '';
    },
    DeletePost: function(postId, Ajaxreload) {


        if (confirm("Are you sure you want to delete Post #" + postId)) {


            var res = DojitAjax.ForumPostDelete(postId);


            if (res.error != null) {
                alert(res.error.description);
                return; 1
            }


            if (Ajaxreload) {

                if (Forum.IsSinglePost()) {
                    window.location.reload();
                } else {
                    this.ShowPage(this.currentPage);
                }

            } else {
                window.location.reload();
            }

        }

    },
    ForumPostVote: function(postId) {

        if (!IsConnected()) {
            RegisterNotify();
            return;
        }

        var res = DojitAjax.ForumPostVote(postId);
        if (res.error != null) {
            alert(res.error.description);
            return;
        }
        $('PostVote_' + postId).innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;Thank you for voting';

    },
    SendMailToUser: function(userID) {

        if (!IsConnected()) {
            RegisterNotify();
            return;
        }
        SendMailToUser(userID);

    },
    EditPost: function(postId) {



        var res = DojitAjax.ForumPostGetById(postId);


        if (res.error != null) {
            alert(res.error.description);
        }

        this.CleanError();
        $('txtTitle').value = res.value['subject'];
        $('txtContent').value = res.value['text'];

        this.newPostData.post_id = postId;


        $('postActionTitle').innerHTML = 'Edit post';

        $('txtFileURL').value = res.value['filename'];


        var postType = res.value['post_type'];
        var parentID = res.value['parent_id'];


        if (parentID == 2000) {
            $('postTypeDIV').style.display = "none";
            this.SetPostType(1);
        } else {

            $('postTypeDIV').style.display = "";
            this.SetPostType(postType);
            $('chkPostType' + postType).checked = true;
        }


        ShowItemEditor('ForumPostEditor');



    }, DeletePostFile: function() {

        var res = DojitAjax.ForumPostClearFile(this.newPostData.post_id);
        if (res.error != null) {
            alert(res.error.description);
        }
        this.ShowPage(this.currentPage);
        $('currentFileDiv').innerHTML = "<span class=\"SuccessMessage\">File deleted</span>";

    }



}