﻿<!--
    function forumPostComponent(forumTopicId, applicationSubPath) {
        var prevParentForumPostId = 0;
        var updateState = 0;
        this.forumTopicId = forumTopicId;
        this.applicationSubPath = applicationSubPath;
        
        function show(parentForumPostId) {
            if (parentForumPostId != prevParentForumPostId) {
                //copy content
                document.getElementById('divForumPostContainer' + parentForumPostId).innerHTML = document.getElementById('divForumPostContainer' + prevParentForumPostId).innerHTML;
                //clear old
                document.getElementById('divForumPostContainer' + prevParentForumPostId).innerHTML = '';
                //remember container
                prevParentForumPostId = parentForumPostId;
            }           
            //show
            openDiv('divForumPostEditor');
             //clear
            document.getElementById('taForumPostBody').value = '';
            //focus
            document.getElementById('taForumPostBody').focus();
            //
            updateState = 0;
        }
        
        function hide() {
            closeDiv('divForumPostEditor');
        }
        
        function add() {
            var eleForumPostBody = document.getElementById('taForumPostBody');

            if (eleForumPostBody.value.length == 0 || eleForumPostBody.value == null) {                
                alert('Proszę wprowadzić treść.');
                eleForumPostBody.focus();
                return;
            }  
      
            document.getElementById('btnForumPostAdd').disabled = 'true';
            if (updateState == 0)
                wsComponent.sendRequest(applicationSubPath + '/Portal/WebServices/ForumPostService.asmx', 'Add', '<forumTopicId>' + forumTopicId + '</forumTopicId><body>' + encodeURIComponent(eleForumPostBody.value) + '</body>', true, wsComponent.execOnSuccess(addCallback));
            else
                wsComponent.sendRequest(applicationSubPath + '/Portal/WebServices/ForumPostService.asmx', 'Update', '<forumPostId>' + updateState + '</forumPostId><body>' + encodeURIComponent(eleForumPostBody.value) + '</body>', true, wsComponent.execOnSuccess(updateCallback));
        }
        
        function addCallback(xmlHttpReq) {
            document.getElementById('btnForumPostAdd').disabled = '';
            if (wsComponent.getNodeValue(xmlHttpReq, 'AddResult') == 'Accepted') {
                hide();
                document.getElementById('taForumPostBody').value = '';
                
                var h = window.location.href;
                if (h.lastIndexOf('#ForumPost') > 0)
                    h = h.substr(0, h.lastIndexOf('#ForumPost'));
                if (h.lastIndexOf('#PageBottom') > 0)
                    h = h.substr(0, h.lastIndexOf('#PageBottom'));
                if (h.lastIndexOf('&Random=') > 0)
                    h = h.substr(0, h.lastIndexOf('&Random='));
                window.location.href = h + '&Random=' + Math.random() + '#PageBottom';
            } else if (wsComponent.getNodeValue(xmlHttpReq, 'AddResult') == 'Waiting') {
                hide();
                document.getElementById('taForumPostBody').value = '';
                alert('Wiadomość oczekuje na moderacją.');
            } else {
                alert('Wystąpił błąd.');
            }        
        }
        
        function updateCallback(xmlHttpReq) {
            document.getElementById('btnForumPostAdd').disabled = '';
            if (wsComponent.getNodeValue(xmlHttpReq, 'UpdateResult') == 'true') {
                hide();
                document.getElementById('taForumPostBody').value = '';
                
                var h = window.location.href;
                if (h.lastIndexOf('#ForumPost') > 0)
                    h = h.substr(0, h.lastIndexOf('#ForumPost'));
                if (h.lastIndexOf('#PageBottom') > 0)
                    h = h.substr(0, h.lastIndexOf('#PageBottom'));
                if (h.lastIndexOf('&Random=') > 0)
                    h = h.substr(0, h.lastIndexOf('&Random='));
                window.location.href = h + '&Random=' + Math.random() + '#ForumPost' + updateState;
            } else {
                alert('Wystąpił błąd.');
            }        
        }
        
        function quote() {
            wsComponent.sendRequest(applicationSubPath + '/Portal/WebServices/ForumPostService.asmx', 'Quote', '<parentForumPostId>' + prevParentForumPostId + '</parentForumPostId>', true, wsComponent.execOnSuccess(quoteCallback));
        }
        
        function quoteCallback(xmlHttpReq) {
            document.getElementById('taForumPostBody').value = wsComponent.getNodeValue(xmlHttpReq, 'QuoteResult') + '\n';
        }
        
        function updateable(forumPostId) {
            wsComponent.sendRequest(applicationSubPath + '/Portal/WebServices/ForumPostService.asmx', 'Updateable', '<forumPostId>' + forumPostId + '</forumPostId>', true, wsComponent.execOnSuccess(updateableCallback));
        }
        
        function updateableCallback(xmlHttpReq) {
            var result = wsComponent.getNodeValue(xmlHttpReq, 'UpdateableResult')
            if (result != '') {
                showDiv('imgEdit' + result);
            }
        }
        
        function updateBody(forumPostId) {
            wsComponent.sendRequest(applicationSubPath + '/Portal/WebServices/ForumPostService.asmx', 'UpdateBody', '<forumPostId>' + forumPostId + '</forumPostId>', true, wsComponent.execOnSuccess(updateBodyCallback));
            // ustaw flagę
            updateState = forumPostId;
        }
        
        function updateBodyCallback(xmlHttpReq) {
            document.getElementById('taForumPostBody').value = wsComponent.getNodeValue(xmlHttpReq, 'UpdateBodyResult') + '\n';            
        }
    
        this.show = show;
        this.hide = hide;
        this.add = add;
        this.quote = quote;
        this.updateable = updateable;
        this.updateBody = updateBody;
    }   
//-->
