﻿commentLog = {};
commentLog = function() {
    this._btn = null;
    this._onSuccess = null;
    this._onFailure = null;
};
commentLog.prototype = {
    initialize: function(submitButton, onSuccess, onFailure) {
        this._onSuccess = onSuccess;
        this._onFailure = onFailure;
        var self = this;
        $(submitButton).bind("click", function() {
            try {
                var validated = false;
                //cheet by calling parent page function
                //issues with context
                validated = runValidation();
                if (validated == true) {
                    self.submitComment();
                }
            }
            catch (e) {
                alert(e.message);
            }
            return false;
        });

    },
    submitComment: function() {
        var submissions = {};
        $('.formation_input, .formation_textarea').each(function() {
            submissions[$(this).attr("name")] = $(this).val();
        });

        submissions["Referrer"] = window.location.pathname;
        var self = this;
        $.ajax({
            type: "POST",
            contentType: "application/x-www-form-urlencoded",
            url: "assets/comment_form/CommentSubmissions.asmx/SubmitAComment",
            data: submissions,
            dataType: "xml",
            success: function(returnObj) {
                successEmail = returnObj;
                if ($(successEmail).find("boolean").text() == 'true') {
                    self.commentSuccess();
                }
                else {
                    self.commentFailure();
                }
            },
            failure: function(err) {
                self.commentFailure();
            }
        });
    },
    commentSuccess: function() {
        if (this._onSuccess) {
            this._onSuccess();
        }
    },
    commentFailure: function() {
        if (this._onFailure) {
            this._onFailure();
        }
    }
};


