﻿var whitespace = " \t\n\r";


function isWhitespace(s) {
    var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isEmail(s) {
    var patrn = /^([0-9a-zA-Z]([-.\w\+]*[0-9a-zA-Z_-])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
    if (!patrn.exec(s)) {
        return false;
    }
    return true;
}

// Check whether string s is empty.

function isEmpty(s) {
    return ((s == null) || (s.length == 0))
}


function ShowErrMessageBox(FoucsObj, title, content) {
    if (isWhitespace(content))
        return;
    var err_msg = "";
    if (isWhitespace(title)) {
        err_msg += "提交的数据出现错误！\n";
        err_msg += "请检查以下错误，并重新提交.\n";
        err_msg += "______________________________________________________\n"
    }
    else {
        err_msg += title + "\n";
        err_msg += "请检查以下错误，并重新提交.\n";
        err_msg += "______________________________________________________\n"
    }
    err_msg += content + "\n";
    alert(err_msg);
    if (FoucsObj != null)
        FoucsObj.focus();
}

function VerifySubscribe(emailFormID) {
    var subEmail = document.getElementById(emailFormID);
    var emailAddress = subEmail.value;
    var msg;
    var errors = "";

    if (isWhitespace(emailAddress)) {
        errors += "\n      - 你的Email地址不能为空！";
    } else if (!isEmail(emailAddress)) {
        errors += "\n      - 你的Email地址格式错误！";
    }

    if (!errors) return true;

    ShowErrMessageBox(subEmail, "你的订阅出现错误！", errors)
    return false;
}

function verifyKeyWord(txtID, DefaultWord) {
    var KeyWord = document.getElementById(txtID);
    var txtKeyWord = KeyWord.value;
    var msg;
    var empty_fields = "";
    var errors = "";
    var error_msg = "";

    if (isWhitespace(txtKeyWord) || txtKeyWord == DefaultWord) {
        errors += "\n      - 您必须输入关键字信息，谢谢！";
    }

    if (!errors) return true;
    ShowErrMessageBox(KeyWord, "您的查询出现错误！", errors)
    return false;
}

function KeyWordColor(txtID, DefaultWord) {
    var KeyWord = document.getElementById(txtID);
    var txtKeyWord = KeyWord.value;
    if (txtKeyWord == DefaultWord) {
        KeyWord.style.color = '#c0c0c0';
    }
    else {
        KeyWord.style.color = '#000000';
    }
}
