include("ajax.js");

var Feedback = function(caller){
    var progressBar = "#loadingbar";
    this.ajax = new Ajax("module", "Feedback", progressBar, caller);
}

Feedback.prototype.sendMsg = function(){
    var obj = "#callbackWindow";
    this.ajax.serializeForm(obj);

	return this.ajax.request("sendMsg",
        function(response){
            if (response == 1){
              $(obj).hide();
            }
            else {
                var tmp = $.evalJSON(response);
                $("#err").text(tmp.message);
            }
            initFeedback();
        }
    );
}

Feedback.prototype.updateCaptcha = function(captcha){
	return this.ajax.request("update",
        function(response){
            $(captcha).attr("src",response);
            initFeedback();
        }
    );
}

function initFeedback(){
    $("#fSend").unbind();
    $("#feedCaptcha").unbind();
    $("#fSend").click(function(){
        var feedback = new Feedback(this);
        feedback.sendMsg();
    });
    $("#feedCaptcha").click(function(){
        $(this).unbind();
        var feedback = new Feedback(this);
        feedback.updateCaptcha("#feedCaptcha");
    });
}

$(document).ready(
    function(){        
        initFeedback();
    }
);
