$(document).ready(function(){
    //feedback form
    
    $('.lnkSendFeedback').click(function(){
        $('#FeedbackForm').showFeedBackForm();
    });
    $('#btnSendFeedback').click(function(){
        $('#FeedbackForm').sendFeedback();
    });
    $('#lnkCloseFeedback').click(function(){
        $('#FeedbackForm').resetFeedbackForm();
        $('#FeedbackForm').hide();
    });
    
    //message form
    $('.lnkSendMessage').click(function(){
        $('#SendMessageForm').showMsgForm();
    });
    $('#btnSendMessage').click(function(){
        $('#SendMessageForm').sendMessage();
    });
    $('#lnkCloseSendMessage').click(function(){
        $('#SendMessageForm').resetMsgForm();
        $('#SendMessageForm').hide();
    });
});

//feedback
$.fn.resetFeedbackForm = function() {
    var el = this;

    var inputs = $(el).find('input');
    var textareas = $(el).find('textarea');
    
    $(inputs).val(''); $(textareas).val('');
    return false;
}
$.fn.showFeedBackForm = function() {
    var el = this;
    
    $(el).resetFeedbackForm();

    $(el).show();
}
$.fn.sendFeedback = function() {
    var el = this;
    
    if ( $(el).formNotEmpty() ) {
        $.post(
            URLS.send_feedback,
            $('#frmFeedback').serialize(),
            function () {
                $(el).hide();
            }
        );
    }
    return false;
}

//message
$.fn.resetMsgForm = function() {
    var el = this;
    
    var recipient = $(el).find('#MessageFormRecipient');
    var inputs = $(el).find('input');
    var textareas = $(el).find('textarea');
    
    $(recipient).empty(); $(inputs).val(''); $(textareas).val('');
    return false;
}
$.fn.showMsgForm = function(recipient, title) {
    if ( !recipient ) return false;
    var el = this;
    
    $(el).resetMsgForm();
    
    var msgTitle = title || '';
    
    $('#MessageFormRecipient').text(recipient);
    $('#MessageTitle').val(msgTitle);
    var hid_input = $(el).find("input[name='recipient']");
    $(hid_input).val(recipient);
    
    $(el).show();
}
$.fn.sendMessage = function(recipient) {
    var el = this;

    if ( $(el).formNotEmpty() ) {
        $.post(
            URLS.send_message,
            $('#frmSendMsg').serialize(),
            function () {
                $(el).hide();
            }
        );
    }
    
    return false;
}
//common
$.fn.formNotEmpty = function() {
    var el = this;
    
    var title = $(el).find("input[name='title']");
    var text = $(el).find("textarea[name='text']");

    if ( $(title).val() == '' ) {alert('Заполните все поля'); $(title).focus(); return false;}
    if ( $(text).val() == '' ) {alert('Заполните все поля'); $(text).focus(); return false;}
    return true;
}
