﻿/// <reference path="Templet/CN/js/jquery-1.3.2.js" />

$(document).ready(function() {
    //提交
    $('#btnSubEmail').click(function() {

        var email = $('#txtEmail').val();
        var vCode = $('#txtVCode').val();
        if (email.length == 0) {
            alert('请输入Email地址!');
            $('#txtEmail').focus();
            return;
        }

        if (!ValidateEmail(email)) {
            alert('输入的Email地址错误!');
            $('#txtEmail').focus();
            return;
        }

        if (vCode.length == 0) {
            alert('请输入验证码!');
            $('#txtVCode').focus();
            return;
        }

        $.ajax({
            type: 'POST',
            url: 'EmailSub.aspx?action=sub',
            dataType: 'text',
            data: 'txtEmail=' + email + '&txtVCode=' + vCode,
            success: function(msg) {
                if (msg == 0) {
                    alert('订阅成功!请到邮箱点击链接确认!');
                }
                else if (msg == -1) {
                    alert('验证码错误!');
                }
                else if (msg == -2) {
                    alert('邮箱地址错误!');
                } 
                else if (msg == -3) {
                    alert('此邮箱地址已经订阅!');
                }
            }
        });

    });

    //验证码
    $('#btnVCode').click(function() {
        $('#btnVCode').attr('src', '/ValidateImage.aspx?t=' + (new Date()).getTime());
    });
});


function ValidateEmail(j) {
    var emailReg = /^([a-zA-Z0-9_\-\.\+]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    return emailReg.test(j);
}
