﻿$(function () {
    $("form.feedback").each(function () {
        var form = $(this);
        var classid = $(this).attr("classid") - 0;
        var error = $(this).attr("error");
        var success = $(this).attr("success");
        var field = $(this).attr("rel").split(',');
        for (i in field) {
            tempvalue = '';
            $("[name='" + field[i] + "']", $(this)).blur(function () {
                tempvalue = $(this).val();
                if ($(this).hasClass("required") && isNull(tempvalue)) {
                    layer.msg($(this).attr("msg"));
                    $(this).focus();
                    return false;
                }
                if (
                    ($(this).hasClass("email") && !isEmail(tempvalue)) ||
                    ($(this).hasClass("phone") && !isPhone(tempvalue))) {
                    layer.msg($(this).attr("title") + "格式不正确");
                    $(this).focus();
                    return false;
                }
            });
        }
        $(".codeimg", form).click(function () {
            $(this).attr("src", "/ashx/checkcode.ashx?" + new Date().getTime());
        });
        var isSubmit = false;
        $(this).submit(function () {
            if (isSubmit)
                return false;

            var obj = {}, code = '';
            obj.ClassId = classid;
            for (i in field) {
                temp = $("[name='" + field[i] + "']", $(this));
                obj[field[i]] = temp.val();
                if (temp.hasClass("required") && isNull(obj[field[i]])) {
                    layer.msg(temp.attr("msg"));
                    return false;
                }
                if (
                    (temp.hasClass("email") && !isEmail(obj[field[i]])) ||
                    (temp.hasClass("phone") && !isPhone(obj[field[i]]))) {
                    layer.msg(temp.attr("title") + "格式不正确");
                    return false;
                }
            }
            if ($(".code", $(this)).length > 0) {
                code = $(".code", $(this)).val();
                if (isNull(code) || code.length < 4) {
                    layer.msg("验证码不正确");
                    return false;
                }
            }
            isSubmit = true;
            if (typeof Feedback == "object") {
                Submit(Feedback, obj, code);
            } else {
                $.getScript("/ajax/Feedback.ashx", function () {
                    Submit(Feedback, obj, code);
                });
            }
            return false;
        });
        function Submit(Feedback, obj, code) {
            var layindex = layer.load();
            Feedback.SendFeedback(obj, code, function (res) {
                layer.close(layindex);
                isSubmit = false;
                if (res && res.value) {
                    if (res.value.state) {
                        layer.msg(success, 1, 9);
                        form[0].reset();
                    } else {
                        layer.msg(res.value.msg);
                    }
                } else {
                    layer.msg(error);
                }
            })
        }
    });

    function isEmail(str) {
        var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,6})+$/;
        return reg.test(str);
    }
    function isPhone(str) {
        var reg = /^(13[0-9]|14[0-9]|15[0-9]|170|18[0-9])\d{8}$/;
        return reg.test(str);
    }
    function isNull(str) {
        if (str != null && str != undefined && str.length > 0)
            return false;
        return true;
    }
});
