﻿/************************************************************************************************
 * File: checkForm.js
 * -------------------
 * Usage: check form before submiting it in client browser.
 *
 * Author: 小王子@苏州大学
 * 
 * Created Date: 2007-3-21
 *
 * Note: 此文件引用了 MyTable 对象，需要将RelationSelect.js文件包含在相应的网页文件里。
 ************************************************************************************************/
 
 /**
  * Class CheckForm
  * ---------------
  * 
  */
 function CheckForm(notNullIds, notEqualIds, notLegalIds, notLegalStr) {
    this.notNullObj = new Array();
    this.notNullErrMarks = new Array();
    this.notEqualObj = new Array();
    this.notEqualErrMarks =new Array();
    this.notLegalObj = new Array();
    this.notLegalErrMarks = new Array();
    this.notLegalStr = notLegalStr;
    this.errors = new MyTable();                // 维护一张错误标签id和错误信息的表
    this.err = new Errors();
        
    try {
        // get all objects that passed into by Ids
        for (var i = 0; i < notNullIds.length; i++) {
            if (notNullIds[i].length > 0 && notNullIds[i] != "") {
                this.notNullObj.push(document.getElementById(notNullIds[i]));
                this.notNullErrMarks.push(notNullIds[i] + "ErrMsg");
            }
        }
        for (var i = 0; i < notEqualIds.length; i++) {
            if (notEqualIds[i].length > 0 && notEqualIds[i] != "") {
                this.notEqualObj.push(document.getElementById(notEqualIds[i]));
                this.notEqualErrMarks.push(notEqualIds[i] + "ErrMsg");
            }
        }
        for (var i = 0; i < notLegalIds.length; i++) {
            if (notLegalIds[i].length > 0 && notLegalIds[i] != "") {
                this.notLegalObj.push(document.getElementById(notLegalIds[i]));
                this.notLegalErrMarks.push(notLegalIds[i] + "ErrMsg");
            }
        }

    } catch (e) {
        alert("errors occured in\n" + CheckForm + " :\n >> " + e);
    }
 }
 
 CheckForm.prototype.clearErrMsgs = function() {
    try {
        //for (var i = 0; i < this.errors.length; i++) {
            //this.errors.edit(this.errors.key[i], "");
        this.errors.clearAll();
        //}
    } catch (e) {
        alert("errors occured in\n" + this.clearMsgs + " :\n >> " + e);
    }
 }
 
 CheckForm.prototype.checkEmpty = function() {
    try {
        for (var i = 0; i < this.notNullObj.length; i++) {
            if (this.notNullObj[i].value == "" || this.notNullObj[i].value.length <= 0) {
                this.errors.edit(this.notNullErrMarks[i], ((this.errors.isExsist(this.notNullErrMarks[i]) == true) ? this.errors.findObj(this.notNullErrMarks[i]) : "") + "&nbsp;&nbsp;&nbsp;&nbsp;" + this.err.getErrMsg(this.err.NOTNULL));
            } else {            // 删除一条错误信息
                this.errors.edit(this.notNullErrMarks[i], "");
            }
        }
    } catch (e) {
        alert("errors occured in \n" + this.checkEmpty + " :\n >> " + e);
    }
 }
 
 CheckForm.prototype.checkEqual = function() {
    
    try {
        for (var i = 0; i < this.notEqualObj.length - 1; i++) {
        //var i =0;
            if (this.notEqualObj[i].value != this.notEqualObj[i+1].value) {
                this.errors.edit(this.notEqualErrMarks[i], ((this.errors.isExsist(this.notEqualErrMarks[i]) == true) ? this.errors.findObj(this.notEqualErrMarks[i]) : "") + "&nbsp;&nbsp;&nbsp;&nbsp;" + this.err.getErrMsg(this.err.NOTEQUAL));
                this.errors.edit(this.notEqualErrMarks[i+1], ((this.errors.isExsist(this.notEqualErrMarks[i+1]) == true) ? this.errors.findObj(this.notEqualErrMarks[i+1]) : "") + "&nbsp;&nbsp;&nbsp;&nbsp;" + this.err.getErrMsg(this.err.NOTEQUAL));
                break;
            }
        }
    } catch (e) {
        alert("errors occured in \n" + this.checkEmpty + " :\n >> " + e);
    }
 }
 
 // 检查是输入框中是否有非法字符：
 CheckForm.prototype.checkLegal = function() {
    try {
        for (var i = 0; i < this.notLegalObj.length; i++) {
            for (var j = 0; j < this.notLegalStr.length; j++) {
                if (this.notLegalObj[i].value.indexOf(this.notLegalStr.charAt(j)) >= 0) {
                    this.errors.edit(this.notLegalErrMarks[i], ((this.errors.isExsist(this.notLegalErrMarks[i]) == true) ? this.errors.findObj(this.notLegalErrMarks[i]) : "") + "&nbsp;&nbsp;&nbsp;&nbsp;" + this.err.getErrMsg(this.err.NOTLEGAL));
                    break;
                }
            }
        }
    } catch (e) {
        alert("errors occured in \n" + this.checkLegal + " :\n >> " + e);
    }
 }
 
 // 设置焦点位于第一个发生错误的input框：
 // 找到则说明有错，返回false
 // 否则返回true。
 CheckForm.prototype.setFocus = function() {
    for (var i = 0; i < this.errors.length; i++) {
        if (this.errors.obj[i].length > 0) {
            document.getElementById(this.errors.key[i].split("ErrMsg")[0]).focus();
            document.getElementById(this.errors.key[i].split("ErrMsg")[0]).select();
            //alert(this.errors.key[i]);
            return false;       // 找到可以插入焦点的位置，说明有错误发生
        }
    }
    return true;                // 没有错误发生，不用设焦点
 }
 
 // 检查表单，有错误返回false， 没有则返回 true.
 CheckForm.prototype.check = function() {
    // 每次做check前先清掉以前的错误信息:
    this.clearErrMsgs();
    //this.setFocusStyle();
    this.checkEmpty();  // 检查是否为空
    this.checkEqual();  // 检查密码是否相同
    //alert("checkLegal() start");
    this.checkLegal();  // 检查是否含有非法字符
    this.setErrMsgs();  // 将错误信息显示在网页上。
    // 将焦点定位于第一个错误的输入框内
    return (this.setFocus());
 }
 
 // 设定输入框在获得焦点时的样式
 /*
 CheckForm.prototype.setFocusStyle = function() {
     for (var i = 0; i < this.notNullObj.length; i++) {
        this.notNullObj[i].onfocus = function() {
            this.className += "OnFocus";
        };
        this.notNullObj[i].onblur = function() {
            this.className 
        }
     }
 }*/
 
 CheckForm.prototype.setErrMsgs = function() {
    //alert(this.errors.length);
    for (var i = 0; i < this.errors.length; i++) {
        document.getElementById(this.errors.key[i]).innerHTML = this.errors.obj[i];
        if (this.errors.obj[i] == "" || this.errors.obj[i].length <= 0)
            document.getElementById(this.errors.key[i]).style.position = "";
        else
            document.getElementById(this.errors.key[i]).style.position = "absolute";
    }
 }
 
 /** end Class CheckForm */
 
/**
 * Class Errors:
 */
function Errors() {
    //错误类型：
    this.NOTNULL = 0;    // 不能为空
    this.NOTEQUAL = 1;   // 两次输入的密码不相等
    this.NOTLEGAL = 2;   // 输入不合法
    
    // 错误信息
    this.errMsg = new Array("此项不能为空，请输入！",
                            "两次输入的密码不相同，请重新输入！",
                            "输入的内容含有不允许的字符，请重新输入！"
                );
}

Errors.prototype.getErrMsg = function(errType) {
    try {
        return this.errMsg[errType];
    } catch (e) {
        alert("errors occured in " + Errors.prototype.getErrMsg + " :\n >> " + e);
    }
}

// 检查长度
function checkLength(id, minLen, maxLen) {
    var checkItem = document.getElementById(id).value;
    if (checkItem.length < minLen || checkItem.length > maxLen) {
        document.getElementById(id + "ErrMsg").innerHTML += "此项的长度必须在 " + minLen + " 到 " + maxLen + " 之间! 您输入了 " + checkItem.length + " 位。"
        document.getElementById(id + "ErrMsg").style.position = "absolute";
        document.getElementById(id).focus();
        document.getElementById(id).select();
        return false;
    } else {
        document.getElementById(id + "ErrMsg").style.position = "";
        return true;
    }
}

// 检查输入中是否含有汉字
function checkZh_Cn(id) {
    var checkItem = document.getElementById(id).value;
    
    if (/[^\x00-\xff]/g.test(checkItem)) {
        document.getElementById(id + "ErrMsg").innerHTML += "此项不能含有汉字！"
        return false;
    } else {
        return true;
    }
}

// 检查E-mail。格式正确返回true， 不正确则返回 false
function checkEmail(checkItem) {
    if (/(\S)+[@]{1}(\S)+[.]{1}(\w)+/.test(checkItem)) {
        return true;
    } else {
        return false;
    }
}

//检查是否全为数字
// 全为数字返回true, 否则返回 false
function isAllDigit(checkItem) {
  
    if (/[^0-9]/g.test(checkItem)) {
        return false;
    } else {
        return true;
    }
}

// 检查网址格式是否合法，合法返回 true, 否则返回 false.
function checkSiteAddress(checkItem) {
    
    if (/^(http:\/\/[A-Za-z0-9\./=\?%\-&_~`@':+!]+)/g.test(checkItem)) {
        return true;
    } else {
        return false;
    }
}
