/* $Id: validate.js,v 1.3 2006/02/21 05:57:40 nio Exp $ */
var ek = {
    Version: '0.1',
    Vendor : 'ek Studio',
    Id: '$Id: validate.js,v 1.3 2006/02/21 05:57:40 nio Exp $',
    prototypeVersion: parseFloat(Prototype.Version.split(".")[0] + "." + Prototype.Version.split(".")[1])
}

if((typeof Prototype=='undefined') || ek.prototypeVersion < 1.4)
      throw("ek JavaScript requires Prototype JavaScript framework >= 1.4.0");

/**
 * 获取包含汉字的字符串的长度。
 */
if (!String.prototype.length2) {
    String.prototype.length2 = function() {
        var cArr = this.match(/[^\x00-\xff]/ig);
        return this.length + (cArr == null ? 0 : cArr.length);
    }
}

ek.Validator = Class.create();

ek.Validator.Valid = Class.create();
ek.Validator.Valid.prototype = {

    initialize: function(options) {
        this.setOptions(options);
    },

    setOptions: function(options) {
        this.options = {
            regexp : null,
            handle : null,
            args   : new Array(),
            errmsg : ''
        }
        Object.extend(this, options || {});
    },
    
    inspect: function() {
        return 'ek.Validator.Valid';
    }
};

ek.Validator.prototype = {

    initialize: function() {
        this.valids   = $H();   //hash
        this.patterns = {
            'VALID_OPTIONAL'  : /.*/,
            'VALID_NOT_EMPTY' : /.+/,
            'VALID_NUMBER'    : /^[0-9]+$/,
            'VALID_EMAIL'     : /^([a-z0-9][a-z0-9_\-\.\+]*)@([a-z0-9][a-z0-9\.\-]{0,63}\.([a-z][a-z]|com|org|net|biz|info|name|net|pro|aero|coop|museum))$/i,
            'VALID_DATE'      : /^[12][0-9]{3}\-(0[1-9]|1[0-2])\-(0[1-9]|[12][0-9]|3[01])$/
        };
        this.field = null;
        this.errmsg = '';
    },

    add: function(field, valid) {
        if (this.isEmpty(field))
            this.valids[field] = new Array();
        this.valids[field].push(valid);
    },

    isEmpty: function(field) {
        keys = this.valids.keys();
        return (keys.indexOf(field) == -1);
    },

    isOptional: function(field) {
        if (this.isEmpty(field))
            return true;
        vs = this.valids[field];
        for (var i = 0; i < vs.length; i++) {
            if (vs[i].regexp == this.patterns['VALID_OPTIONAL'])
                return true;
        }
        return false;
    },

    validate: function() {
        var keys = this.valids.keys();
        for (var i = 0; i < keys.length; i++) {
            field = keys[i];
            this.field = $(field);
            value = $F(field);
            if (typeof value == 'undefined')
                value = '';
            vs = this.valids[field];
            if ( !value.length2() && this.isOptional(field) )
                continue;
            for (var j = 0; j < vs.length; j++) {
                valid = vs[j];
                if (valid.regexp) {
                    if (!valid.regexp.test(value))  {
                        this.errmsg = valid.errmsg;
                        return false;
                    }
                } else if (valid.handle) {
                    valid.args.unshift(field);
                    if (!valid.handle.apply(this, valid.args)) {
                        this.errmsg = valid.errmsg;
                        return false;
                    }
                }   //end if
            }   //end for
        }   //end for
        return true;
    },
    
    validateEqual: function(field1, field2) {
        return ( $F(field1) == $F(field2) );
    },

    validateNumberRange: function(field, min, max) {
        var val = parseInt($F(field));
        if (isNaN(val))
            return false;
        if (min != null && val < min)
            return false;
        if (max != null && val > max)
            return false;
        return true;
    },

    validateStringRange: function(field, min, max) {
        var len = $F(field).length2();
        if (min != null && len < min)
            return false;
        if (max != null && len > max)
            return false;
        return true;
    }
};