var feeback = new clsFeedback();
$(feeback.load);

function clsFeedback()
{
    var me = this;
    this.load = function()
    {
        $("#email").glInputDefText(2);
        gUser.onLoggin(function(){
            $("#email").val(gUser.data.email);
            $("#email").trigger("blur");
        });
        gUser.onLogout(function(){
            $("#email").val("");
            $("#email").trigger("blur");
        });
        $("#submit_feedback").click(me.onSubmit);
    }

    this.onSubmit = function()
    {
        if (!me.validate())
            return;

        if ($("#email").val()=="Please enter email address")
        {
            var cont = "<p>You didn't leave us your email. This means we won&#8216;t be able to contact you back. Do you want to continue?</p>";
            messageBox.showDialog(
            {
                cont:cont,
                title:"What, no email address?",
                width:350,
                btn:
                {
                    "OK":function()
                    {
                        messageBox.hideDialog();
                        me.submit();
                    },
                    "Cancel":function()
                    {
                        messageBox.hideDialog();    
                    }
                }
            });
        } else
            me.submit();
    }

    this.submit = function()
    {
        messageBox.showWait()
        $.ajax({
             type: "POST",
             url: "feedback.ajax.php",
             data: "subject="+$("#subject").val()+"&email="+$("#email").val()+"&comments="+$("#comments").val()+"&platform="+$("#platform").val(),
             complete: function(h)
             {
//                 $.c(h.status)
                 if(h.status == 200)
                     me.callback(h.responseText)
             },
            error:function()
            {
                messageBox.hideWait()
                messageBox.showAlert({title:"Error",msg:'Cannot connect to server, Please try again later'})
            }
        });
    }


    this.validate = function()
    {
        cl = new gcList();
        (!$("#subject").val())?cl.addItem("Please select a topic"):"";
        (!$("#platform").val())?cl.addItem("Please specify a platform"):"";
        if (($("#email").val()) && ($("#email").attr("def") != "true"))
            ((!glEmailCheck($("#email").val())))?cl.addItem("Email should be a proper email address"):"";
        if (!$("#comments").val())
            cl.addItem("Please fill your feedback")
        if(cl.count)
        {
            messageBox.showAlert({title:"Error",msg:'Please enter following item(s): ' + cl.getList()})
            return false;
        }
        return true;
    }

    this.callback = function(s)
    {
        messageBox.hideWait()
        if (s == 1)
        {
            messageBox.showAlert({title:"Success!",msg:"Thanks for your feedback"})
            setTimeout(me.feedGoToHome, 3000);
        }
        else
            messageBox.showAlert({title:"Error",msg:"Please try again later"})
    }


    this.feedGoToHome =  function()
    {
        location.href = "/";
    }
}

