if (typeof (Tody) === "undefined") var Tody = {};

(function($) {

    Tody.Feedback = new function() {

        var self = this;

        self.init = function() {

            $('#btnSendFeedback').click(function() {
                if ($("#ClientQuestionary_QuestionaryId").length == 0) {
                    self.getSendFeedbackForm();
                }
            });

        };

        self.getSendFeedbackForm = function() {

            Boxy.load(actionUrl('/SendFeedback'), {
                title: " DESCHIDETI O SESIZARE !",
                draggable: false,
                modal: true,
                show: true,
                afterDrop: redirectToHome,
                afterHide: redirectToHome,
                beforeUnload: redirectToHome
            });

            $('#Name').val("");
            $('#Company').val("");
            $('#City').val("");
            $('#Email').val("");
            $('#TaskTitle').val("");
            $('#TaskDescription').val("");

        };

        var redirectToHome = function() {
            window.location = _basePath;
        };

        self.sendFeedback = function(button) {


            if ($('#Name').val() == "") {
                $('#spanError').html("Completati numele si prenumele.");
                return;
            }
            if ($('#Company').val() == "") {
                $('#spanError').text("Completati numele companiei.");
                return;
            }
            if ($('#City').val() == "") {
                $('#spanError').text("Completati numele orasului.");
                return;
            }
            if ($('#Email').val() == "") {
                $('#spanError').text("Completati adresa e-mail.");
                return;
            }

            if (!self.validateEmail($('#Email').val())) {
                $('#spanError').text("Completati o adresa e-mail valida.");
                return;
            }

            if ($('#TaskTitle').val() == "") {
                $('#spanError').text("Completati titlul sesizarii.");
                return;
            }

            if ($('#TaskDescription').val() == "") {
                $('#spanError').text("Completati descrierea sesizarii.");
                return;
            }

            $('#ReturnUrl').val(window.location);
            $('#formSendFeedback').submit();
            $(button).attr("onClick", "").find("img:eq(0)").attr("src", imageUrl("/working-button.gif"));

            //            var dataForPost = { name: $('#Name').val(),
            //                company: $('#Company').val(),
            //                city: $('#City').val(),
            //                email: $('#Email').val(),
            //                tasktitle: $('#TaskTitle').val(),
            //                taskdescription: $('#TaskDescription').val()
            //            };

            //            $(button).attr("disabled", true).find("img:eq(0)").attr("src", imageUrl("/working-button.gif"))
            //            //$('#spanError').text("Mesajul a fost trimis cu succes.");
            //            //setTimeout('Boxy.get($("#btnSendF")[0]).hideAndUnload()', 1);
            //            $.ajax({
            //                type: "POST",
            //                url: actionUrl('/SendFeedback/SendFeedback'),
            //                data: dataForPost,
            //                dataType: "json",
            //                success: function(result) {
            //                    //$(button).setWorking(false);

            //                    if (result.Messages && result.Messages.length > 0) {
            //                        $('#spanError').text(result.Messages[0].Message);
            //                        $('#spanError').css('color', 'red');
            //                        // $(button).setWorking(false);
            //                    } else {
            //                        $('#spanError').text("Mesajul a fost trimis cu succes.");
            //                        $('#spanError').css('color', 'green');
            //                        //$(button).setWorking(false);
            //                    }

            //                    redirectToHome();

            //                },
            //                error: function(msg) {
            //                    //$(button).setWorking(false);

            //                    Boxy.alert(msg.status + " : " + msg.statusText, null, {
            //                        title: 'Atentie',
            //                        closeOnButtonClick: true
            //                    });
            //                }

            //            });
        };


        self.validateEmail = function(email) {

            var at = email.lastIndexOf("@");

            // Make sure the at (@) sybmol exists and  
            // it is not the first or last character
            if (at < 1 || (at + 1) === email.length)
                return false;

            // Make sure there aren't multiple periods together
            if (/(\.{2,})/.test(email))
                return false;

            // Break up the local and domain portions
            var local = email.substring(0, at);
            var domain = email.substring(at + 1);

            // Check lengths
            if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
                return false;

            // Make sure local and domain don't start with or end with a period
            if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
                return false;

            // Check for quoted-string addresses
            // Since almost anything is allowed in a quoted-string address,
            // we're just going to let them go through
            if (!/^"(.+)"$/.test(local)) {
                // It's a dot-string address...check for valid characters
                if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
                    return false;
            }

            // Make sure domain contains only valid characters and at least one period
            if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
                return false;

            return true;
        };

        //onclick = 'Tody.Feedback.sendFeedback(this);'

    };

})(jQuery);

jQuery(document).ready(function() {
    Tody.Feedback.init();
}); 
