﻿var page = null;
initPage = function(activitiesEndDate) {
    page = new activity();
    page.initilization(activitiesEndDate);
}
activity = function() {
    var self = this;
    this.initilization = function(activitiesEndDate) {
        $("#nav div a").mouseover(function() { $("#nav div").removeClass("current"); $(this).parent().addClass("current"); });
        if (activitiesEndDate == self.currentDate()) {
            $("#nav div a").click(function() { $("#login_false").show(); });
            $("#btn").click(function() { window.location.href = 'http://www.rexue.com'; });
        } else {
            $("#nav div a").click(function() { $("#login").show(); });
            //$('#txtName').keydown(function(e) { if (e.keyCode == 13) return false; }).keyup(function(e) { if (e.keyCode == 13) { self.loginLottery(); return false; } });
            //$('#txtEmail').keydown(function(e) { if (e.keyCode == 13) return false; }).keyup(function(e) { if (e.keyCode == 13) { self.loginLottery(); return false; } });
        }
    }
    this.loginLottery = function() {
        var _name = $("#txtName").val().replace(/(^\s*)|(\s*$)/g, "");
        var _email = $("#txtEmail").val().replace(/(^\s*)|(\s*$)/g, "");
        if (_name == "") { $("#txtName").focus(); alert("请输入姓名"); return; }
        if (_email == "") { $("#txtEmail").focus(); alert("请输入E-mail"); return; }
        if (!self.checkEmail(_email)) { $("#txtEmail").focus(); alert("E-mail输入错误"); return; }
        var data = DragonBoatFestival._Default.loginLottery(_name, _email).value;
        if (data != null && data.IsSuccess) {
            if (data.Tag == -1) {
                $("#errInfo").html("真遗憾，您没有吃到什么宝贝:(");
                $("#login_false").show();
            } else {
                $("#cardPwd").html(data.Tag);
                $("#login_suc").show();
            }
        } else {
            $("#errInfo").html(data.ExecMessage);
            $("#login_false").show();
        }
        $("#btn").click(function() { $('#login_false').hide(); $('#login').hide() });
        $("#txtName").val(""); $("#txtEmail").val("");
    }
    this.checkEmail = function(email) { return self.checkMatch(email, /^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/gi); }
    this.checkMatch = function(str, reg) { var match = str.match(new RegExp(reg)); return match == null ? false : true; }
    this.currentDate = function() {
        var d, s = "";
        d = new Date();
        s += d.getFullYear() + "-";
        s += (d.getMonth() + 1) + "-";
        s += d.getDate();
        return s;
    }
}
