/*===================================================================
    File        : UtilityFormSubmit.js
    Description : Classes to use when submit the form
    Author      : Raymond Chiu
    Require     : UtilityForm.js, ETLang_xx_xx.js
===================================================================*/

if (!this._ET_include_UtilityForm) {
    document.write('<script type="text/javascript" language="JavaScript" src="/js/UtilityForm.js"></script>');
}
if (!this._ET_include_ETLang) {
    //dynamic load a default js
    document.write('<script type="text/javascript" language="JavaScript" src="/js/lang/ETLang_zh_hk.js"></script>');
}

if (!this._ET_include_UtilityFormSubmit) { //---- check whether FormValidator is loaded

/*
    Class       : FormValidator
    Description : class to validate the form
    Parameters  : frmControl - the form control that to be validate
    Return      : instance of FormValidator
*/
function FormValidator(frmControl) {
    this.frmControl = frmControl;

    //default error message
    this.msgMandatory = _ETLANG.MSG['FRM_FILLFIELD'];
    this.msgNonSpecialChar = _ETLANG.MSG['FRM_NOSPECHAR'];
    this.msgNumeric = _ETLANG.MSG['FRM_NUMERIC'];
    this.msgMinValue = _ETLANG.MSG['FRM_MINVAL'];
    this.msgMaxValue = _ETLANG.MSG['FRM_MAXVAL'];
    this.msgMinLength = _ETLANG.MSG['FRM_MINLEN'];
    this.msgMaxLength = _ETLANG.MSG['FRM_MAXLEN'];
    this.msgMaxByteLength = _ETLANG.MSG['FRM_MAX_BYTE_LEN'];
    this.msgMixByteLength = _ETLANG.MSG['FRM_MIN_BYTE_LEN'];

    //default RegExp
    this.REGEXP_DATE   = /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/;     //Should be informat yyyy-MM-dd
    this.REGEXP_EMAIL  = /^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$/;
    this.REGEXP_TEL    = /(^(.*)[0-9]{0,3})(.*)([0-9]{4})(.*)([0-9]{4}$)/;
    this.REGEXP_USRID  = /^[_\.0-9a-z-]{6,20}$/;
    this.REGEXP_NONXML = /^[^<>&\']+$/;

    this.mandatoryFields = new Array();
    this.nonSpecialCharFields = new Array();
    this.alphaNumericFields = new Array();
    this.numericFields = new Array();
    this.limitLengthFields = new Array();
    this.limitValueFields = new Array();
    this.specialFormatFields = new Array();
    this.compareEq = new Array();
    this.limitByteLengthFields = new Array();

    this.addMandatoryField = function(elementName, displayName) {
        this.mandatoryFields[elementName] = displayName;
    }
    this.addNonSpecialCharField = function(elementName, displayName) {
        this.nonSpecialCharFields[elementName] = displayName;
    }
    this.addAlphaNumericField = function(elementName, displayName) {
        this.alphaNumericFields[elementName] = displayName;
    }
    this.addNumericField = function(elementName, displayName) {
        this.numericFields[elementName] = displayName;
    }
    this.addLimitLengthField = function(elementName, displayName, minLength, maxLength) {
        this.limitLengthFields[elementName] = [displayName, minLength, maxLength];
    }
    this.addLimitByteLengthField = function(elementName, displayName, minLength, maxLength) {
        this.limitByteLengthFields[elementName] = [displayName, minLength, maxLength];
    }
    this.limitValueFields = function(elementName, displayName, minLength, maxLength) {
        this.limitValueFields[elementName] = [displayName, minLength, maxLength];
    }
    this.addSpecialFormatField = function(elementName, displayName, fieldRegExp, errorMsg) {
        this.specialFormatFields[elementName] = [displayName, fieldRegExp, errorMsg];
    }
    this.addCompareEquation = function(elementName, equation, errorMsg) {
        this.compareEq[++this.compareEq.length-1] = [elementName, equation, errorMsg];
    }

    this.alertMsg = function(msg, args) {
        if (typeof(args)=='string') {
            msg = msg.replace(new RegExp('\\$ARG0', 'g'), args);
        } else {
            for (var i=0; i<args.length; i++) {
                msg = msg.replace(new RegExp("\\$ARG" + i,"g"), args[i]);
            }
        }
        alert(msg);
    }

    this.validate = function() {
        var oneForm = this.frmControl;
        EtFormUtil.trimAllTextFields(oneForm);

        var formElementNames = new Object();
        for (var i=0; i<oneForm.elements.length; i++) {
            formElementNames[oneForm.elements[i].name] = 'dummyValue';
        }

        for (var elementName in formElementNames) {
            var oneElement = oneForm.elements[elementName];
            var values = EtFormUtil.getElementValues(oneElement);

            //check mandatory
            if (this.mandatoryFields[elementName] != null) {
                if (values.length == 0) {
                    this.alertMsg(this.msgMandatory, [this.mandatoryFields[elementName]]);
                    focusControl(oneElement);
                    return false;
                }
                for (var j=0; j<values.length; j++) {
                    var tmpValue = values[j];
                    if (tmpValue == null || tmpValue.length == 0) {
                        this.alertMsg(this.msgMandatory, [this.mandatoryFields[elementName]]);
                        focusControl(oneElement);
                        return false;
                    }
                }
            }

            //check special char
            if (this.nonSpecialCharFields[elementName] != null) {
                for (var j=0; j<values.length; j++) {
                    var tmpValue = values[j];
                    if (tmpValue.length > 0 && !EtFormUtil.isNonSpecialChar(tmpValue)) {
                        this.alertMsg(this.msgNonSpecialChar, [this.nonSpecialCharFields[elementName]]);
                        focusControl(oneElement);
                        return false;
                    }
                }
            }

            //check alphaNumeric
            if (this.alphaNumericFields[elementName] != undefined) {
                for (var j=0; j<values.length; j++) {
                    var tmpValue = values[j];
                    if (tmpValue.length > 0 && !EtFormUtil.isAlphaNumeric(tmpValue)) {
                        this.alertMsg(this.msgNumeric, [this.alphaNumericFields[elementName]]);
                        focusControl(oneElement);
                        return false;
                    }
                }
            }

            //check numeric
            if (this.numericFields[elementName] != undefined) {
                for (var j=0; j<values.length; j++) {
                    var tmpValue = values[j];
                    if (tmpValue.length > 0 && !EtFormUtil.isNumeric(tmpValue)) {
                        this.alertMsg(this.msgNumeric, [this.numericFields[elementName]]);
                        focusControl(oneElement);
                        return false;
                    }
                }
            }

            //check value range
            if (this.limitValueFields[elementName] != undefined) {
                for (var j=0; j<values.length; j++) {
                    var tmpValue = values[j];
                    var tmpValueInt = parseInt(tmpValue);
                    var minValue = this.limitValueFields[elementName][1];
                    var maxValue = this.limitValueFields[elementName][2];
                    if (tmpValue.length > 0 && maxValue!="MAX" && tmpValueInt > maxValue) {
                        this.alertMsg(this.msgMaxValue, [this.limitValueFields[elementName][0], maxValue]);
                        focusControl(oneElement);
                        return false;
                    }
                    if (tmpValue.length > 0 && minValue!="MIN" && tmpValueInt < minValue) {
                        this.alertMsg(this.msgMinValue, [this.limitValueFields[elementName][0], minValue]);
                        focusControl(oneElement);
                        return false;
                    }
                }
            }

            //check size
            if (this.limitLengthFields[elementName] != undefined) {
                for (var j=0; j<values.length; j++) {
                    var tmpValue = values[j];
                    var tmpValueLength = tmpValue.length;
                    var minValue = this.limitLengthFields[elementName][1];
                    var maxValue = this.limitLengthFields[elementName][2];
                    if (tmpValue.length > 0 && tmpValueLength!="MAX" && tmpValueLength> maxValue) {
                        this.alertMsg(this.msgMaxLength, [this.limitLengthFields[elementName][0], maxValue]);
                        focusControl(oneElement);
                        return false;
                    }
                    if (tmpValue.length > 0 && minValue!="MIN" && tmpValueLength < minValue) {
                        this.alertMsg(this.msgMinLength, [this.limitLengthFields[elementName][0], minValue]);
                        focusControl(oneElement);
                        return false;
                    }
                }
            }

            if (this.limitByteLengthFields[elementName] != undefined) {
                for (var j=0; j<values.length; j++) {
                    var tmpValue = values[j];
                    var tmpValueLength = EtUtil.getByteLen(tmpValue);
                    var minValue = this.limitByteLengthFields[elementName][1];
                    var maxValue = this.limitByteLengthFields[elementName][2];
                    if (tmpValueLength!="MAX" && tmpValueLength> maxValue) {
                        this.alertMsg(this.msgMaxByteLength, [this.limitByteLengthFields[elementName][0], maxValue]);
                        focusControl(oneElement);
                        return false;
                    }
                    if (minValue!="MIN" && tmpValueLength < minValue) {
                        this.alertMsg(this.msgMinLength, [this.limitByteLengthFields[elementName][0], minValue]);
                        focusControl(oneElement);
                        return false;
                    }
                }
            }

            //check special format fields -- [displayName, fieldRegExp, errorMsg];
            if (this.specialFormatFields[elementName] != undefined) {
                for (var j=0; j<values.length; j++) {
                    var tmpValue = values[j];
                    var fieldRegExp = this.specialFormatFields[elementName][1];
                    if (tmpValue.length > 0 && tmpValue.search(fieldRegExp)==-1) {
                        //not match
                        var tmpErrMsg = this.specialFormatFields[elementName][2];
                        var tmpDspName= this.specialFormatFields[elementName][0];
                        this.alertMsg(tmpErrMsg, tmpDspName);
                        focusControl(oneElement);
                        return false;
                    }
                }
            }
        }

        for (var i=0; i<this.compareEq.length; i++) {
            var oneElement = oneForm.elements[this.compareEq[i][0]];
            var compareEq = this.compareEq[i][1];
            var errorMsg  = this.compareEq[i][2];
            compareEq = compareEq.replace(/\{([a-zA-Z0-9_]+)\}/g,"oneForm.$1.value");
            var compareResult = eval(compareEq);
            if (!compareResult) {
                alert(errorMsg);
                focusControl(oneElement);
                return false;
            }
        }

        return true;
    }
}

//---- end check whether FormValidator is loaded
_ET_include_UtilityFormSubmit=true;
}

