﻿$(document).ready(function() {
jQuery.extend($.xs.forum, {
    search: {
        setup: function() {
            $('#xs_mPh_Subject').focus();
            
            $('#xs_mPh_Subject').keypress(function(e) { if (e.which == 13) { e.preventDefault(); $('#Save').trigger('click'); } });
            $('#xs_mPh_Message').keypress(function(e) { if (e.which == 13) { e.preventDefault(); $('#Save').trigger('click'); } });
            
            $('#Save').click(function() {
                if ($('#xs_mPh_Subject').val().length == 0 && $('#xs_mPh_Message').val().length == 0) {
                    return false;
                }
                
                if ($('#xs_mPh_Message').val().length == 0) {
                    $.xs.redirect('/forum/search/?forum='+$('#xs_mPh_Forum').val()+'&subject='+$.xs.urlEncode($('#xs_mPh_Subject').val()));
                }
                
                else {
                    $.xs.redirect('/forum/search/?forum='+$('#xs_mPh_Forum').val()+'&message='+$.xs.urlEncode($('#xs_mPh_Message').val()));
                }
            });
        }
    },
    topics: {
        topic: {
            post: {
                setup: function(intPostId, blnDisableQuote) {
                    if (blnDisableQuote) {
                        $('#PostOptionsQuote'+intPostId).attr('disabled',true);
                    }
                    
                    $('#PostOptionsMemberEdit'+intPostId).click(function() { $.xs.forum.topics.topic.post.edit(intPostId); });
                    $('#PostOptionsQuote'+intPostId).click(function() { $.xs.forum.topics.topic.post.quote(intPostId); });
                },
                edit: function(intPostId) {
                    if ($.xs.isDisabled($('#PostOptionsMemberEdit'+intPostId))) return false;
                    
                    $('#PostOptionsMemberEdit'+intPostId).attr('disabled',true);
                    
                    var strHtml = '';
                    strHtml += '<form id="PostOptionsEditForm'+intPostId+'">';
                    strHtml += '<textarea name="Post" style="width:595px; height:250px">'+$('#PostMessage'+intPostId).val()+'</textarea>';
                    strHtml += '<div class="b_spacing_top">';
                    strHtml += '<br /><input id="PostOptionsEditSave'+intPostId+'" class="button" type="submit" value="versturen" /> <input id="PostOptionsEditCancel'+intPostId+'" class="button" type="button" value="annuleren" />';
                    strHtml += '</div>';
                    strHtml += '</form>';
                    
                    $('#PostPh'+intPostId).hide();
                    $('#PostPh'+intPostId).after(strHtml);
                    $('#PostOptionsEditForm'+intPostId).validate({
                        submitHandler: function(form) {
                            if ($.xs.isDisabled($('#PostOptionsEditSave'+intPostId))) return false;
                            
                            $('#PostOptionsEditSave'+intPostId).attr('disabled',true);
                            var strPostData = 'PostId='+intPostId+'&'+$('#PostOptionsEditForm'+intPostId).serialize();
                            
                            $.ajax({
                                type: 'POST',
                                url: '/xseno/_site/pages/forum/_post.aspx?id=3',
                                data: strPostData,
                                success: function(data) {
                                    if (parseFloat(data) == -5) {
                                        alert('Er is een (tijdelijke) forum ban geactiveerd op dit profiel. Het is daarom niet mogelijk om te posten. Neem contact op voor meer informatie.');
                                        return false;
                                    }
                                                                  
                                    if (!$.xs.error.checker(parseFloat(data))) { $('#PostOptionsEditSave'+intPostId).attr('disabled',false); return false; }
                                    window.location.reload();
                                }
                            });
                        },
                        focusInvalid: true,
                        rules: {
                            Post: { required:true, maxlength:3000 }
                        },
                        messages: {
                            Post: '<div class="b_misc_error">Een post is verplicht.</div>'
                        }
                    });
                    
                    $('#PostOptionsEditCancel'+intPostId).click(function() {
                        $('#PostOptionsEditForm'+intPostId).hide(); $('#PostPh'+intPostId).show();
                        $('#PostOptionsMemberEdit'+intPostId).attr('disabled',false);
                    });
                },
                quote: function(intPostId) {
                    if ($.xs.isDisabled($('#PostOptionsQuote'+intPostId))) return false;
                    $('#Message').insertAtInput('[quote]');
                    $('#Message').insertAtInput($('#PostHeaders'+intPostId).val()+'\n');
                    $('#Message').insertAtInput($('#PostMessage'+intPostId).val());
                    $('#Message').insertAtInput('[/quote]\n\n');
                }
            },
            setup: function(intTopicId) {
                $('#NewPostForm').validate({
                    submitHandler: function(form) {
                        if ($.xs.isDisabled($('#Save'))) return false;
                        $('#Save').attr('disabled',true);
                        
                        $.ajax({
                            type: 'POST',
                            url: '/xseno/_site/pages/forum/_post.aspx?id=2',
                            data: 'Message='+$.xs.urlEncode($('#Message').val())+'&TopicId='+intTopicId,
                            success: function(data) {
                                if (parseFloat(data) == -5) {
                                    alert('Er is een (tijdelijke) forum ban geactiveerd op dit profiel. Het is daarom niet mogelijk om te posten. Neem contact op voor meer informatie.');
                                    return false;
                                }
                                
                                if (!$.xs.error.checker(parseFloat(data))) { $('#Save').attr('disabled',false); return false; }
                                $.xs.redirect('/forum/topic/'+intTopicId+'/');
                            }
                        });
                    },
                    focusInvalid: true,
                    rules: {
                        Message: { required:true, minlength:2, maxlength:3000 }
                    },
                    messages: {
                        Message: '<div class="b_misc_error">Een post is verplicht.</div>'
                    }
                });
                
                $('#xs_mPh_ForumJump').change(function() {
                    if ($('#xs_mPh_ForumJump').val() != 0) {
                        $.xs.redirect('/forum/'+$('#xs_mPh_ForumJump').val()+'/');
                    }
                });
            },
            edit: function(intTopicId) {
                if ($.xs.isDisabled($('#TopicEdit'))) return false;
                
                $('#TopicEdit').attr('disabled',true);
                $('#EditTopicPh').show();
                
                $('#EditTopicForm').validate({
                    submitHandler: function(form) {
                        if ($.xs.isDisabled($('#EditTopicSave'))) return false;
                        
                        $('#EditTopicSave').attr('disabled',true);

                        $.ajax({
                            type: 'POST',
                            url: '/xseno/_site/pages/forum/_post.aspx?id=4',
                            data: 'TopicId='+intTopicId+'&'+$('#EditTopicForm').serialize(),
                            success: function(data) {
                                if (!$.xs.error.checker(parseFloat(data))) { $('#EditTopicSave').attr('disabled',false); return false; }
                                window.location.reload();
                            }
                        });
                    },
                    focusInvalid: true,
                    rules: {
                        xs$mPh$Forum: { required:true },
                        xs$mPh$EditTopicSubject: { required:true, minlength:2, maxlength:40 },
                        xs$mPh$EditTopicMessage: { required:true, minlength:2, maxlength:3000 }
                    },
                    messages: {
                        xs$mPh$Forum: '<div class="b_misc_error">Kies een forum uit de lijst.</div>',
                        xs$mPh$EditTopicSubject: {
		                    required: '<div class="b_misc_error">Een onderwerp is verplicht.</div>',
		                    minlength: '<div class="b_misc_error">Een onderwerp is verplicht.</div>'
		                },
                        xs$mPh$EditTopicMessage: '<div class="b_misc_error">Een eerste post is verplicht.</div>'
                    }
                });
            }
        },
        newtopic: {
            setup: function() {
                $('#NewTopicForm').validate({
                    submitHandler: function(form) {
                        if ($.xs.isDisabled($('#Save'))) return false;
                        
                        $('#Save').attr('disabled',true);
                        
                        $.ajax({
                            type: 'POST',
                            url: '/xseno/_site/pages/forum/_post.aspx?id=1',
                            data: $('#NewTopicForm').serialize(),
                            success: function(data) {
                                if (parseFloat(data) == -5) {
                                    alert('Er is een (tijdelijke) forum ban geactiveerd op dit profiel. Het is daarom niet mogelijk om te posten. Neem contact op voor meer informatie.');
                                    return false;
                                }
                                
                                var intResponseId = parseFloat(data);
                                if (isNaN(intResponseId)) { alert('Het verwerken is niet gelukt, probeer het nogmaals (fout: '+data+').'); }
                                else if (intResponseId < 1) {
                                    $.xs.error.checker(intResponseId)
                                    $('#Save').attr('disabled',false); return false;
                                }
                                else {
                                    $.xs.redirect('/forum/topic/'+intResponseId+'/');
                                }
                            }
                        });
                    },
                    focusInvalid: true,
                    rules: {
                        xs$mPh$Forum: { required:true },
                        Subject: { required:true, minlength:2, maxlength:40 },
                        Message: { required:true, minlength:2, maxlength:3000 }
                    },
                    messages: {
                        xs$mPh$Forum: '<div class="b_misc_error">Kies een forum uit de lijst.</div>',
                        Subject: {
		                    required: '<div class="b_misc_error">Een onderwerp is verplicht.</div>',
		                    minlength: '<div class="b_misc_error">Een onderwerp is verplicht.</div>'
		                },
                        Message: '<div class="b_misc_error">Een eerste post is verplicht.</div>'
                    }
                });
            }
        }
    }
});
});