﻿var Forum = {
    id: 1,
    currentPage: 1,

    newPostData: { parent_id: 0, post_id: 0, title: '', text: '', URL: '' },
    AddPost: function(parent_id) {


        RequireConnection();

        this.CleanError();

        this.newPostData.parent_id = parent_id;
        this.newPostData.post_id = 0;
        $('txtTitle').value = '';

        $('txtContent').value = '';
        $('postActionTitle').innerHTML = 'Add new post';
        $('txtFileURL').value = '';

        //$('divForumFile').innerHTML = '<input type="file" name="attached_file" />';



        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);

        if (res.error != null) {
            alert(res.error.description);
            return;
        }
        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.ForumGetTableById(this.id, pageId);

        if (res.error != null) {
            alert(res.error.description);
        }

        this.currentPage = pageId;
        $('ForumHolder').innerHTML = res.value;
    },
    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) {
                this.ShowPage(this.currentPage);
            } else {
                window.location.reload();
            }

        }

    },
    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'];
        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>";

    }



}