﻿/* Time Media Inc | Blu20 Property - Do Not Use */

var flynhog = {
    eMailLink: "",
    msg: "",
    from: "",

    init: function () {
        this.eMailLink = arguments[0];

        $("#rmi, #rmi2, .moreinfo").bind("click", function () {
            $("#innermodalri .input input").eq(2).val($(".requestinfo input").val());
            $("#rmimodal").modal();
            $("#rmimodal input").eq(0).blur();
        });

        $("input.inputtext, textarea").each(function () {
            $(this).val($(this).attr("title"));
        }).bind("focus", function () {
            if ($.trim($(this).val()) == $(this).attr("title")) {
                $(this).val("");
            }
        }).bind("blur", function () {
            if ($.trim($(this).val()) == "" || $.trim($(this).val()) == $(this).attr("title")) {
                $(this).val($(this).attr("title"));
            }
        });

        $(".innerlocation input").each(function () {
            $(this).bind("click", function () {
                var iClass = "." + $(this).attr("title");
                $(iClass).toggle();
            });
        });
    },

    sendMail: function () {
        var jsonData = "from=" + arguments[0] + "&subject=" + arguments[1] + "&message=" + arguments[2];
        $.ajax({
            url: flynhog.eMailLink,
            data: jsonData,
            type: "POST",

            success: function (data) {
                if (data.successful) {
                    alert("Form submitted!");
                }
                else {
                    alert("Form submission failed. Please try again later.");
                }
            },

            error: function () {
                alert("Form submission failed. Please try again later.");
            }
        });
    },

    suggestToAFriend: function () {
        this.resetLocal();
        this.checkInputs("#suggesttofriendmodal input");
        if (this.msg == '') {
            return false;
        }
        else {
            $.modal.close();
        }
    },

    emailSignUp: function () {
        this.resetLocal();
        this.checkInputs(".enewletter input"); if (this.msg == '') {
            return false;
        }
        else {
            this.sendMail(flynhog.from, "Fly-N-Hog Online Form :: Enewsletter Subscription Request", "I'd like to sign up for your Enewsletter");
        }
    },

    contactUs: function () {
        this.resetLocal();
        this.checkInputs("#" + arguments[0] + " input.inputtext");
        if (this.msg == '') {
            return false;
        }
        else {
            this.msg += '\nComments\n' + $("#" + arguments[0] + " textarea").val();
            this.sendMail(flynhog.from, "Fly-N-Hog Online Form :: Contact Us", flynhog.msg);
            $.modal.close();
        }
    },

    checkPopUp: function () {
        this.resetLocal();
        this.checkInputs("#rmimodal .input input");
        if (this.msg == '') {
            return false;
        }
        else {
            this.msg += '\nHow Did You Hear About Us?\n';
            $("#rmimodal .checkbox input").each(function () {
                flynhog.msg += $(this).attr("title") + ': ' + ($(this).attr("checked") ? 'Yes' : 'No') + '\n';
            });
            this.sendMail(flynhog.from, "Fly-N-Hog Online Form :: Request More Info", flynhog.msg);
            $.modal.close();
        }
    },

    checkInputs: function () {
        $(arguments[0]).each(function () {
            if ($.trim($(this).val()) == $(this).attr("title")) {
                alert("Please fill out: " + $(this).attr("title"));
                return false;
            }
            else {
                if ($(this).attr("title") == "E-mail Address") {
                    flynhog.from = $.trim($(this).val());
                }
                flynhog.msg += $(this).attr("title") + ': ' + $.trim($(this).val());
            }
            flynhog.msg += '\n';
        });
    },

    resetLocal: function () {
        this.msg = this.from = '';
    }
}
