/*
 jquery±íµ¥ÑéÖ¤²å¼þ¡£last edit by rongrong 2011.03.29
 */

var __check_form_last_error_info = [];

var check_form = function (formname, opt) {
    var formobj;				//Ö¸¶¨µ±Ç°±íµ¥
    var e_error = "";
    var focus_obj = "";			//µÚÒ»¸ö³öÏÖ´íÎó¶ÔÏñ
    var error = [];
    var _temp_ajax = new Array;	//ajaxÐ£ÑéÇëÇóµÄ»º´æ
    var opt = opt || {};		//Ñ¡Ïî
    opt = $.extend({
        type: 'form',			//Ð£Ñé¶ÔÏó form ,elem
        trim: true,				//×Ô¶¯trim
        errtype: 0,				//·µ»Ø´íÎóÐÅÏ¢£º0 È«²¿ 1 ×îºóÒ»Ìõ 2 µÚÒ»Ìõ
        showtype: 0				//´íÎóÏÔÊ¾·½Ê½ 0 µ¯³ö  1 ²»µ¯³ö
    }, opt);
    if (opt['type'] == 'elem') {
        formobj = $(formname);
        formname = $('body');
    }
    else {
        formobj = $('input,textarea,select,button', formname);
    }
    formobj.each(function (i) {
        var formobj_i = $(this);
        var jscheckflag = formobj_i.attr("jscheckflag");
        var obj_tag = formobj_i.attr("tagName");
        jscheckflag = jscheckflag == undefined ? 1 : jscheckflag;
        if (jscheckflag == 0)
            return true;
        var jscheckrule = formobj_i.attr("jscheckrule") || '';
        var jscheckerror = formobj_i.attr("jscheckerror");
        var jschecktitle = formobj_i.attr("jschecktitle") || '±íµ¥';
        var jsmaxlen = formobj_i.attr("maxLength");

        //Ö´ÐÐ×Ô¶¯trim
        if (jscheckrule.indexOf('trim=0') == -1 && opt['trim']) {
            cf_trim(formobj_i, 1);
        }

        //³õÊ¼»¯jscheckrule
        //Ð£ÑéHTML
        if (jscheckrule.indexOf('html=') == -1) {
            jscheckrule += ';html=0';
        }

        var jsvalue = formobj_i.val();
        var errflag = 0;//´íÎó±ê¼Ç
        if (obj_tag == 'TEXTAREA' && (jsmaxlen > 0 ? (jsvalue.length > jsmaxlen ? 1 : 0) : 0)) {
            jscheckerror = (jschecktitle ? jschecktitle : formobj_i.attr('name')) + " ²»µÃ³¬¹ý " + jsmaxlen + " ×Ö";
            errflag = 1;
        }
        else if (jscheckrule) {
            errflag = docheck(formobj_i, jscheckrule) ? errflag : 1;
        }
        if (errflag) {
            e_error = jscheckerror ? jscheckerror : jschecktitle + ' ' + formobj_i.data('jscheckerror');
            error.push(e_error);
            if (focus_obj == "") focus_obj = formobj_i;									//¾Û½¹µÚÒ»¸ö³öÏÖ´íÎó¶ÔÏó
            if (this.oldback == undefined) this.oldback = this.style.backgroundColor;	//¼ÇÂ¼Ô­µ×É«
            this.style.backgroundColor = '#FFFF00';									//¸ßÁÁ´íÎó±íµ¥¶ÔÏó
        } else {
            if (this.oldback != undefined) this.style.backgroundColor = this.oldback;		//»¹Ô­µ×É«
        }
    });
    //´¦ÀíajaxÐ£Ñé
    if (!error) {
        for (var i in _temp_ajax) {
            var obj = _temp_ajax[i].obj;
            var str = _temp_ajax[i].str;
            var jscheckerror = obj.attr('jscheckerror');
            var jschecktitle = obj.attr('jschecktitle');
            if (_cf_ajax.call(obj.get(0), obj, str) != true) {
                e_error = jscheckerror ? jscheckerror : jschecktitle + ' ' + obj.data('jscheckerror');
                error.push(e_error);
                if (focus_obj == "") focus_obj = obj;											//¾Û½¹µÚÒ»¸ö³öÏÖ´íÎó¶ÔÏó
                if (obj.get(0).oldback == undefined) obj.get(0).oldback = obj.get(0).style.backgroundColor;	//¼ÇÂ¼Ô­µ×É«
                obj.get(0).style.backgroundColor = '#FFFF00';							//¸ßÁÁ´íÎó±íµ¥¶ÔÏó
                break;
            }
        }
    }
    if (error.length > 0) {
        __check_form_last_error_info = error;
        if (opt.showtype == 0)
            alert("±íµ¥´æÔÚÒÔÏÂ´íÎó:\n" + error.join("\n"));

        try {
            focus_obj.focus()
        } catch (e) {
        }
        ;
        return false;
    }
    return true;
    //Ð£ÑéÖ÷º¯Êý
    function docheck(el, jscheckrule) {
        var jscheckrule = jscheckrule || el.attr("jscheckrule");
        var e_rules = jscheckrule.split(";");
        var e_rule, e_rules_len = e_rules.length;
        for (var k = 0; k < e_rules_len; k++) {
            var rule_index = e_rules[k].indexOf("=");
            if (rule_index > -1) {
                e_rule = [e_rules[k].substr(0, rule_index), e_rules[k].substr(rule_index + 1)];
                if (e_rule.length == 2) {
                    //e_rule_para = e_rule_para.replace(new RegExp("\'","gm"),"\\'");
                    var cf_func = "cf_" + e_rule[0] + "(el,e_rule[1])";
                    try {
                        if (!eval(cf_func)) return false;
                    } catch (e) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    //¼ì²âÖÐÓ¢ÎÄ»ìºÏ³¤¶È
    function strLen(s) {
        var i, str1, str2, str3, nLen;
        str1 = s;
        nLen = 0;
        for (i = 1; i <= str1.length; i++) {
            str2 = str1.substring(i - 1, i)
            str3 = escape(str2);
            if (str3.length > 3) {
                nLen = nLen + 2;
            }
            else {
                nLen = nLen + 1;
            }
        }
        return nLen;
    }

    //////////////////////////////////////////**** ¼ì²â¹¦ÄÜº¯Êý×é****///////////////////////////////////
    //×Ô¶¯trimµô¿Õ¸ñ£¬Ò»°ã¼ÓÔÚ¹æÔò×îÇ°Ãætrim=1
    function cf_trim(obj, flag) {
        if (flag == 1) {
            var str = obj.val();
            str = $.trim(str);
            try {
                obj.val(str);
            } catch (e) {
            }
            ;//fileÊ±»á³ö´í
        }
        return true;
    }

    //ÅÐ¶ÏÊÇ·ñÎª¿Õ
    function cf_null(obj, cannull) {
        if (cannull == 1) return true;
        var obj_type = obj.attr('type'), str;
        if (obj_type == 'checkbox' || obj_type == 'radio') {
            var objname = obj.attr('name');
            str = formobj.filter(':' + obj_type + '[name=' + objname + '][checked]').map(function () {
                return this.value;
            }).get().join(',');
        }
        else {
            str = obj.val();
        }
        str = str && typeof(str) == 'object' ? str.join(',') : str;
        if (cannull == 0) {
            str = $.trim(str);
        }
        if (cannull == 1 || str != "") return true;
        obj.data('jscheckerror', '²»ÄÜÎª¿Õ');
        return false;
    }

    //×î´ó³¤¶È
    function cf_maxlen(obj, num) {
        var str = obj.val();
        if (str == "" || strLen(str) <= num) return true;
        obj.data('jscheckerror', '³¤¶È²»ÄÜ´óÓÚ ' + num + ' ×Ö½Ú');
        return false;
    }

    //×îÐ¡³¤¶È
    function cf_minlen(obj, num) {
        var str = obj.val();
        if (str == "" || strLen(str) >= num) return true;
        obj.data('jscheckerror', '³¤¶È²»ÄÜÐ¡ÓÚ ' + num + ' ×Ö½Ú');
        return false;
    }

    //×î´óÖµ
    function cf_maxnum(obj, num) {
        var str = obj.val();
        if (str * 1 <= num * 1) return true;
        obj.data('jscheckerror', '²»ÄÜ´óÓÚ ' + num);
        return false;
    }

    //×î´ó×ÖÊý
    function cf_maxlencn(obj, num) {
        var str = obj.val();
        if (str == "" || str.length <= num) return true;
        obj.data('jscheckerror', '×ÖÊý²»ÄÜ´óÓÚ ' + num + ' ×Ö');
        return false;
    }

    //×îÐ¡×ÖÊý
    function cf_minlencn(obj, num) {
        var str = obj.val();
        if (str == "" || str.length >= num) return true;
        obj.data('jscheckerror', '×ÖÊý²»ÄÜÐ¡ÓÚ ' + num + ' ×Ö');
        return false;
    }

    //Ö¸¶¨×ÖÊý
    function cf_lencn(obj, num) {
        var str = obj.val();
        if (str == "" || str.length == num) return true;
        obj.data('jscheckerror', '×ÖÊý±ØÐëµÈÓÚ ' + num + ' ×Ö');
    }

    //×îÐ¡Öµ
    function cf_minnum(obj, num) {
        var str = obj.val();
        if (str == "" || str * 1 >= num * 1) return true;
        obj.data('jscheckerror', '²»ÄÜÐ¡ÓÚ ' + num);
        return false;
    }

    //Ö¸¶¨³¤¶È
    function cf_len(obj, num) {
        var str = obj.val();
        if (str == "" || strLen(str) == num) return true;
        obj.data('jscheckerror', '³¤¶È±ØÐëµÈÓÚ ' + num + ' ×Ö½Ú');
    }

    //ÊÇ·ñÓÊ¼þµØÖ·
    function cf_email(obj, mustcheck) {
        var str = obj.val();
        if (str == "" || !mustcheck) return true;
        rx = "^([\\w\\_\\.])+@([\\w]+\\.)+([\\w])+$";
        if (cf_regexp(obj, rx)) return true;
        obj.data('jscheckerror', '±ØÐëÎª EMAIL ¸ñÊ½');
        return false;
    }

    //ÓëÁíÒ»¶ÔÏóµÄÖµÒ»ÖÂ
    function cf_sameto(obj, el) {
        var str = obj.val();
        if (str == "" || str == formobj.filter("#" + el).val()) return true;
        el = formobj.filter("#" + el).attr('jschecktitle') || el;
        obj.data('jscheckerror', '±ØÐëµÈÓÚ ' + el + ' µÄÖµ');
        return false;
    }

    //ÓëÁíÒ»¶ÔÏóµÄÖµ²»Ò»ÖÂ
    function cf_differentto(obj, el) {
        var str = obj.val();
        if (str == "" || str == formobj.filter("#" + e1).val()) return true;
        obj.data('jscheckerror', '±ØÐë²»µÈÓÚ ' + el + ' µÄÖµ');
        return false;
    }

    //²»ÄÜÎªÄ³Ð©Öµ
    function cf_nosame(obj, para) {
        var str = obj.val();
        if (str == "") return true;
        var p_arr = para.split(",");
        var p_arrlen = p_arr.length;
        for (var l = 0; l < p_arrlen; l++) {
            if (p_arr[l] == str) {
                obj.data('jscheckerror', '²»ÄÜµÈÓÚ ' + para.join(' »ò '));
                return false;
                break;
            }
        }
        return true;
    }

    //ÔÊÐí×Ö·û·¶Î§
    function cf_charset(obj, para) {
        var str = obj.val();
        if (str == "") return true;
        var c_rule = '';
        var p_arr = para.split(",");
        var p_arrlen = p_arr.length;
        var para_arr = [];
        for (var l = 0; l < p_arrlen; l++) {
            if (p_arr[l] == 'en') {
                c_rule += "a-zA-Z";
                para_arr[l] = '×ÖÄ¸';
            }
            else if (p_arr[l] == 'num') {
                c_rule += "0-9";
                para_arr[l] = 'Êý×Ö';
            }
            else if (p_arr[l] == 'fl') {
                c_rule += "0-9\\.0-9";
                para_arr[l] = 'Ð¡Êý';
            }
            else if (p_arr[l] == 'cn') {
                c_rule += "\\u4E00-\\u9FA5";
                para_arr[l] = 'ÖÐÎÄ';
            }
            else if (p_arr[l] == 'ul') {
                c_rule += "_";
                para_arr[l] = 'ÏÂ»®Ïß';
            }
        }
        if (c_rule == "")    return true;
        else {
            var t_rule = "^[" + c_rule + "]*$";
            if (cf_regexp(obj, t_rule)) return true;
            obj.data('jscheckerror', '±ØÐëÎª[' + para_arr.join(',') + ']');
            return false;
        }
    }

    //×Ô¶¨ÒåÕýÔòÆ¥Åä
    function cf_regexp(obj, rx) {
        var str = obj.val();
        if (str == "") return true;
        if (rx == "")return true;
        var r_exp = new RegExp(rx, "ig");
        if (r_exp.test(str)) return true;
        obj.data('jscheckerror', 'º¬ÓÐ·Ç·¨×Ö·û');
        return false;
    }

    //Ð£ÑéHTML±êÇ©<HTML>
    function cf_html(obj, flag) {
        var str = obj.val();
        if (str == "" || str == null) return true;
        if (flag == 1) return true;
        if (str.indexOf('<') == -1 && str.indexOf('>') == -1) return true;
        obj.data('jscheckerror', 'º¬ÓÐhtml×Ö·û');
        return false;
    }

    //ajaxÐ£Ñé
    function cf_ajax(obj, str) {
        _temp_ajax = _temp_ajax.concat({obj: obj, str: str});
        return true;
        //return _cf_ajax.call(obj.get(0),obj,str);
    }

    function _cf_ajax(obj, str) {
        var str_obj = eval('(' + str + ')');
        var resp = $.ajax({
            url: str_obj.url,
            async: false,
            data: str_obj.data
        }).responseText;
        if (resp === 'true') return true;
        obj.data('jscheckerror', resp);
        return false;
    }

    function get_param(str) {
        return eval('(' + str + ')');
    }

    //µ÷º¯Êý call = {func:[this.value,1,2,3]}
    function cf_call(obj, str) {
        var str_obj = get_param.call(obj.get(0), str);
        for (var func in str_obj) {
            var resp = window[func].apply(undefined, str_obj[func]);
            if (resp !== true) {
                obj.data('jscheckerror', resp);
                return false;
            }
        }
        return true;
    }
}

check_form.get_error = function () {
    return __check_form_last_error_info;
};
