$(document).ready(function()
{


    $("[class*=showAnswerForm]").click(function(){
            var questionId =  $(this).attr('id').replace('showAnswerForm_', '');

            $('#answerFormBox_'+questionId).toggle();
            /*if ($(this).html() == 'Cancel') {
                $(this).html('Answer');
                $(this).hide();
            }
            else {
                $(this).html('Cancel');
            }*/
            $(this).hide();
    });
    $("[class*=hideAnswerForm]").click(function(){
            var questionId =  $(this).attr('id').replace('hideAnswerForm_', '');

            $('#answerFormBox_'+questionId).toggle();
            $('#showAnswerForm_'+questionId).toggle();
    });


    $('[class*=questionFormBox] form').submit(function(data) {
        $.post('/ajax/comment-add-question', {
            site_id: $('#site_id').val(),
            comment_category_id: $('#comment_category_id').val(),
            identifier_id: $('#identifier_id').val(),
            name: $('#name').val(),
            content: $('#content').val(),
            email: $('#email').val(),
            email_address: $('#email_address').val(),
            url: document.location.href
        }, function(json){
                if (json.result == 'true') {
                    $('#commentQuestion').css('text-align', 'center');
                    $('#commentQuestion').html('<h2>Thank You!</h2><p>Pending approval, questions and answers are reviewed and published within 24 - 48 hours.<p>');
                }
                else if (json.result.content.length > 0) {
                    alert('Error submitting question. Please enter a question before you submit.');
                }
                else if (json.result.email.length > 0) {
                    alert('Error submitting question. Please enter a valid email before you submit.');
                }

            }, 'json');
        data.preventDefault();
    });

    $('[class*=answerFormBox] form').submit(function(data) {
        var questionId =  $(this).attr('id').replace('commentAnswer_', '')

        $.post('/ajax/comment-add-answer', {
            question_id: questionId,
            name: $('#name_'+questionId).val(),
            location: $('#location_'+questionId).val(),
            content: $('#content_'+questionId).val(),
            email_address: $('#email_address_'+questionId).val()
        }, function(json){
                if (json.result == 'true') {
                    $('#commentAnswer_'+questionId).css('text-align', 'center');
                    $('#commentAnswer_'+questionId).html('<h2>Thank You!</h2><p>Pending approval, questions and answers are reviewed and published within 24 - 48 hours.<p>');
                    $('#showAnswerForm_'+questionId).css('display', 'none');
                }
                else if (json.result.content.length > 0) {
                    alert('Error submitting answer. Please enter an answer before you submit.');
                }
                else if (json.result.email.length > 0) {
                    alert('Error submitting answer. Please enter a valid email before you submit.');
                }
            }, 'json');
        data.preventDefault();
    });
});


