var RegEmail = new RegExp('^[\\w\\.\\-]+@[\\w\\-]+\\.[\\w\\.\\-]+$', '');
var RegSN = new RegExp('^\\w{4}\-\\w{4}\-\\w{4}\-\\w{4}$', '');

var ErrField;

function testMail() {
    var Email = document.getElementById("Email").value;

    if(Email == "") {
        alert("您尚未輸入 Email");
        document.getElementById("Email").focus();
        return;
    } else if(! RegEmail.test(Email)) {
        alert("Email 格式錯誤");
        document.getElementById("Email").focus();
        return;
    }
    
    var Msg = "本系統將會立即寄送一封測試用信件至 " + Email + " 信箱，內容如下...\n\n" +
              "主旨: A test mail form boshiamy.com\n\n" +
              "!! This is a test mail from boshiamy.com !!\n" +
              "--------------------------------------------------\n\n" +
              "This mail was sent by server automatically so do not reply this mail directly.\n" +
              "Please mail to liu@liu.com.tw if you have any questions.\n\n" +
              "這是來自 行易有限公司::嘸蝦米輸入法 的測試信件。\n\n" +
              "此為自動發信系統，請勿直接回覆。\n" + 
              "如有任何疑問，請來信: liu@liu.com.tw。\n\n" +
              "--------------------------------------------------\n\n" +
              "The infomation of the applicant:\n" +
              "Time: (now) (UTC+8)\n" +
              "IP: (your ip)\n\n" +
              "您確定要繼續嗎？";
    
    if (confirm(Msg)) {
        window.open(unescape("testmail.php?Email=" + Email) ,""," width=400,height=200,top=200,left=200,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,cannelmode=no,fullscreen=no");
    }
}

function checkfield(id, msgtype, regex) {
    if(document.getElementById(id).value == '') {
        document.getElementById("Msg" + id).innerHTML = (msgtype == 0) ? "尚未輸入" : "尚未選取";
        if(ErrField == '') ErrField = id;
    } else if(regex && ! regex.test(document.getElementById(id).value)) {
        document.getElementById("Msg" + id).innerHTML = '不正確的格式';
        if(ErrField == '') ErrField = id;
    } else {
        document.getElementById("Msg" + id).innerHTML = "";
        return true;
    }
    
    return false;
}

function checkDate(yy, mm, dd) {
    switch(parseInt(mm)) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        return (dd >= 1 && dd <= 31);
    case 4:
    case 6:
    case 9:
    case 11:
        return (dd >= 1 && dd <= 30);
    case 2:
        if(yy % 400 == 0 || ((yy % 4 == 0) && (yy % 100 != 0))) {
            return (dd >= 1 && dd <= 29);
        } else {
            return (dd >= 1 && dd <= 28);
        }
        
        break;
    default:
        return false;
    }
}

function focusColor(obj) {
    obj.style.borderColor='#7F9DB9';
    obj.style.backgroundColor='#FFFFFF';
}

function blurColor(obj) {
    obj.style.borderColor='#CCCCCC';
    obj.style.backgroundColor='#F3F3F3';
}

function dl_checkform() {
    if(document.getElementById("Name2").value == '') {
        alert("請輸入使用者姓名！");
        document.getElementById("Name2").focus();
        return false;
    } else if(document.getElementById("KeyString2").value == '') {
        alert("請輸入授權序號！");
        document.getElementById("KeyString2").focus();
        return false;
    } else if(! RegSN.test(document.getElementById("KeyString2").value)) {
        alert("序號格式不正確");
        document.getElementById("KeyString2").focus();
        return false;
    }
    
    return true;
}

function dl_submitform() {
    if(dl_checkform()) {
        document.getElementById("Name").value = document.getElementById("Name2").value;
        document.getElementById("Name2").value = "";
        
        document.getElementById("KeyString").value = document.getElementById("KeyString2").value;
        document.getElementById("KeyString2").value = "";
        
        document.getElementById("login").submit();
    }
}
