//共占用3个变量名：myFun,addOne,checkedInfo
var checkInfo, addOne;
//////////////////////////////////////////////////////////////////////////////////////////
(function(window) {
    //倒计时
    function remtime(timesec, clearT, fun) {
        function countdown() {
            function formatTime(sec) {
                function fix(n) {
                    if (n < 10) return "0" + n.toString();
                    else return n;
                    // return n<10?'0'+n.toString():n;
                }
                return fix(Math.floor(sec / 60)) + ":" + fix(sec % 60);
            }

            timesec--;
            var timeTmp = formatTime(timesec);
            for (var e_array = document.getElementsByName('remtime'), i = e_array.length - 1; i >= 0; i--) {
                e_array[i].innerText = timeTmp;
            }

            if (!timesec) {
                clearTimeout(timer);
                $.isFunction(fun) && fun();
            }
            if (!clearT[0]) {
                clearTimeout(timer);
            }
        }
        var timer = setInterval(countdown, 1000);
    }
    //边框效果--移入
    function borderShow(obj) {
        var hh = $(obj).height() + 50,
            ww = $(obj).width() + 50;
        $(obj).find('.biankuang_1').stop(true).animate({
            height: hh
        }, 300);
        $(obj).find('.biankuang_2').stop(true).delay(300).animate({
            width: ww
        }, 300);
        $(obj).find('.biankuang_3').stop(true).animate({
            height: hh
        }, 300);
        $(obj).find('.biankuang_4').stop(true).delay(300).animate({
            width: ww
        }, 300);
    }
    //边框效果--移出
    function borderHidden(obj) {
        $(obj).find('.biankuang_1').stop(true).delay(100).animate({
            height: '0px'
        }, 100);
        $(obj).find('.biankuang_2').stop(true).animate({
            width: '0px'
        }, 100);
        $(obj).find('.biankuang_3').stop(true).delay(100).animate({
            height: '0px'
        }, 100);
        $(obj).find('.biankuang_4').stop(true).animate({
            width: '0px'
        }, 100);
    }
    window.myFun = [remtime, borderShow, borderHidden];
})(window);
//////////////////////////////////////////////////////////////////////////////////////////
$(document).ready(function() {
    var clearT = [1],
        //正则匹配规则
        theRegex = [
            /^[\u4e00-\u9fa5]{2,5}$/,
            /^((200|199)\d|201[0-7])\d{6}$/,
            /^[\u4e00-\u9fa5]{2,}$/,
            /^[\u4e00-\u9fa5]{2,}$/,
            /^1\d{10}$/
        ],
        eName = [
            "user_name",
            "student_id",
            "school",
            "userclass",
            "telephone"
        ],
        lableValue = new Array(),
        questionNum,
        questionsChecked = new Array();
    //初始化 questionNum
    questionNum = document.getElementById("ppp").getAttribute('ppp');
    //初始化数组 labelValue & questionChecked
    for (var i = eName.length - 1; i >= 0; i--) {
        lableValue[i] = $("#" + eName[i] + "_label").html();
    }

    for (var i = questionNum - 1; i >= 0; i--) {
        questionsChecked[i] = 0;
    }
    //倒计时器
    //name="remtime"
    //remtime(sec,callback);
    startremtime = function() {
        myFun[0](900, clearT,
            function() {
                var btn = document.getElementById("submit");
                $("#form1").ajaxSubmit({
                    beforeSubmit: function() {
                        if (!clearT[0]) {
                            return false;
                        }
                    },
                    success: function(responseText) {
                        clearT[0] = 0;
                        alert("时间到！");
                        alert(responseText);
                    }
                });
                btn.setAttribute('class', btn.getAttribute('class') + ' disabled');
            });
    }

    //当鼠标离开节点时调用此方法，验证节点内容是否符合注册规范
    checkInfo = function(node, num) {
        var label_id = "#" + eName[num] + "_label";
        console.log(node.value);
        if (!theRegex[num].test(node.value) && !node.value.length == 0) { //验证是否符合节点对应的正则表达式
            $(node).css("box-shadow", "0 1px 0 0 red");
            $(node).css("border-bottom", "1px solid red"); //不符合，相应内容变成红色
            $(label_id).html("请填写正确的" + lableValue[num] + "！");
            $(label_id).css("color", "red");
        } else {
            $(node).css("box-shadow", "0 1px 0 0 #26a69a");
            $(node).css("border-bottom", "1px solid #26a69a"); //符合，相应内容变成绿色
            $(label_id).html(lableValue[num]);
            $(label_id).css("color", "#9e9e9e");
        }
    };

    addOne = function(node) {
        var sum = 0;
        for (var i = questionNum; i > 0; i--) {
            if ($(node).attr('name') == "answer_" + i) {
                questionsChecked[i - 1] = 1;
                break;
            }
        }
        for (var i = questionNum - 1; i >= 0; i--) {
            sum += questionsChecked[i];
        }
        $(".determinate").css("width", 100 * sum / questionNum + "%");
    };

    //表单验证
    requestFirst = function() {
        var ifblock = new Array(),
            scroll_top_duration = 900; //ms
        for (var i = eName.length - 1; i >= 0; i--) {
            ifblock[i] = $.trim(document.getElementById(eName[i]).value);
        }

        //判断姓名和学好是否填写
        for (var i = 0; i <= ifblock.length - 1; i++) {
            if (ifblock[i].length == 0) {
                Materialize.toast(lableValue[i] + '不能为空！', 4000);
                $('body,html').animate({
                    scrollTop: 0
                }, 400);
                return false;
            } else if (!theRegex[i].test(ifblock[i])) {
                Materialize.toast(lableValue[i] + '不正确！', 4000);
                $('body,html').animate({
                    scrollTop: 0
                }, 400);
                return false;
            }
        }
        $("#information").hide();
        $("#hiddenDisplay").hide();
        $("#hiddenBefore").show();
        $("#submit").show();
        $("#theFooter").show();
        startremtime();
        return true;
    }

    function showRequest() {
        var ifblock = new Array(),
            scroll_top_duration = 900; //ms
        for (var i = eName.length - 1; i >= 0; i--) {
            ifblock[i] = $.trim(document.getElementById(eName[i]).value);
        }

        //判断姓名和学好是否填写
        for (var i = 0; i <= ifblock.length - 1; i++) {
            if (ifblock[i].length == 0) {
                Materialize.toast(lableValue[i] + '不能为空！', 4000);
                $('body,html').animate({
                    scrollTop: 0
                }, 400);
                return false;
            } else if (!theRegex[i].test(ifblock[i])) {
                Materialize.toast(lableValue[i] + '不正确！', 4000);
                $('body,html').animate({
                    scrollTop: 0
                }, 400);
                return false;
            }
        }
        //判断题目是非选择
        for (var i = 1; i <= questionNum; i++) {
            var answerno = "answer_" + i, //遍历每到题目
                isAutoSend = document.getElementsByName(answerno),
                nochecked = 0;
            for (var x = 0; x < isAutoSend.length; x++) {
                if (isAutoSend[x].checked) {
                    nochecked = 1;
                    break;
                }
            }
            if (nochecked == 0) {
                var question = "question_" + i,
                    obj = document.getElementsByName("div_e")[i - 1];
                Materialize.toast("第" + i + "题未选择", 3000);
                $('body,html').animate({
                        scrollTop: $(document.getElementById(question)).offset().top - 75
                    }, scroll_top_duration,
                    function() {
                        myFun[1](obj);
                        setTimeout(function() { myFun[2](obj) }, 1200);
                    }
                );
                return false;
            }
        }
        //alert("提交成功。谢谢参与我们的活动，请继续关注我们公众号bupt_stu的后续推送。现在你可以直接关闭页面了。"); //Alert("post success.");
        if (!clearT[0]) {
            return false;
        }
        return true;
    }

    function showResponse(responseText) {
        clearT[0] = 0;
        alert(responseText);
        window.location.href = "https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MjM5NzY1MjYyMg==&scene=124#wechat_redirect";

    }
    var options = {
        beforeSubmit: showRequest,
        success: showResponse
    };

    $("#form1").ajaxForm(options);
});