
var first_error		=	"";
var check_error		=	'';//Error:
var check_alt		=	'';
var check_alt_count	=	1;
var alert_num		=	6;
function autocheck(fm){	
	check_alt_count         =	1;
	check_alt           =	'';
	var check_rule 			=	new Array();
	check_rule['email']		=	/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;    
	check_rule['number']  		=	/^\d+$/;  
	check_rule['url']		=	/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
	check_rule['tel']		=	 /^((\(\d{2,3}\))|(\d{3}[\-]{0,1}))?(\(0\d{2,3}\)|0\d{2,3}[\-]{0,1})?[1-9]\d{6,7}([\-]{0,1}\d{1,4})?$/;
	check_rule['mobile']		=	/^((\(\d{3}\))|(\d{3}\-))?(13|15)\d{9}$/;
	check_rule['domain']		=	/^[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
	check_rule['idcard']		=	/^\d{15}(\d{2}[A-Za-z0-9])?$/;
	check_rule['money']		=	/^\d+(\.\d+)?$/;
	check_rule['zip']		=	/^[0-9]\d{5,6}$/;
	check_rule['oicq']		=	/^[0-9]\d{4,10}$/;
	check_rule['int']		=	/^[-\+]?\d+$/;
	check_rule['double']		=	/^[-\+]?\d+(\.\d+)?$/;
	check_rule['english']		=	/^[A-Za-z0-9\. ]+$/;
	check_rule['chinese']		=	/^[\u0391-\uFFE5]+$/;
	check_rule['username']		=	/^[a-z]\w{3,32}$/i;
	check_rule['password']		=	/^[A-Za-z0-9]\w{3,32}$/;
	check_rule['char']		=	/^[A-Za-z0-9_\-]+$/;


	for(var i = 0; i < fm.elements.length; i++){
		with(fm.elements[i]){ 
			var form_item	=	fm.elements[i];
			var label_model	=	form_item.getAttribute('model');
			var msg		=	form_item.getAttribute('msg');
			var area = form_item.getAttribute('area');//form_item.getAttribute('area');22-33
			var area_max = 0;
			var area_min = 0;
			if(new RegExp("^[0-9\-\.]{1,}$","g").test(area)){
				var tmp = area.match(new RegExp("^([0-9\.]*)[-]{0,1}([0-9\.]*)$"));
				area_min = parseFloat(tmp[1]);
				area_max = parseFloat(tmp[2]);
				if((form_item.type != 'checkbox' && form_item.type != 'multiple') && (area_max < form_item.value && area_max != 0 && area_max != 'NaN') || (area_min > form_item.value && area_min != 0 && area_min != 'NaN')) {
					add_error(form_item.name, msg);
					continue;
				}
			}
			var len = form_item.getAttribute('len');
			var len_max = 0;
			var len_min = 0;
			if(new RegExp("^[0-9\-]{1,}$","g").test(len)){
				var tmp = len.match(new RegExp("^([0-9]*)[-]{0,1}([0-9]*)$"));
				len_min = parseInt(tmp[1]);
				len_max = parseInt(tmp[2]);
				if(len_min == 0 && form_item.value == ''){
					continue;
				}
				if((len_max < strlen(form_item.value) && len_max != 0 && len_max != 'NaN') || (len_min > strlen(form_item.value) && len_min != 0 && len_min != 'NaN')){ 
					add_error(form_item.name, msg);
					continue;
				}
			}
			var to = form_item.getAttribute('to');
			if(typeof(label_model) != "string")  continue;
			switch(label_model){
				case 'repeat':
					if(to != null)
						if(fm.elements[to].value != form_item.value)
							add_error(form_item.name, msg);
					break;
				case 'define':
					var rule = form_item.getAttribute('rule');
					if(!new RegExp(rule,"g").test(form_item.value))
						add_error(form_item.name, msg);
					break;
				case 'select': 
					var check_label = document.getElementsByName(form_item.name);
					var checked_count = 0;
					for(var j = check_label.length - 1; j >= 0; j--){
						if(check_label[j].selected || check_label[j].value != '') checked_count = 1;
					}
					if(checked_count == 0) 
						add_error(form_item.name, msg);
					break;
				case 'multiple': 
					var check_label = document.getElementsByName(form_item.name);
					var checked_count = 0;
					for(var j = check_label.length - 1; j >= 0; j--)
						if(check_label[j].selected) checked_count++;
					if(checked_count > area_max || checked_count < area_min) 
						add_error(form_item.name, msg);
					break;
				case 'radio': 
					var check_label = document.getElementsByName(form_item.name);
					var checked_count = 0;
					for(var j = check_label.length - 1; j >= 0; j--)
						if(check_label[j].checked) checked_count = 1;
					if(checked_count == 0) 
						add_error(form_item.name, msg);
					break;
				case 'checkbox': 
					var check_label = document.getElementsByName(form_item.name);
					var checked_count = 0;
					for(var j = check_label.length - 1; j >= 0; j--)
						if(check_label[j].checked) checked_count++;
					if(checked_count > area_max || checked_count < area_min) 
						add_error(form_item.name, msg);
					break;
				case 'y-m-d h:i:s':
					var tmp = form_item.value.match(new RegExp("^([0-9]{4})[-./]{1}([0-9]{1,2})[-./]{1}([0-9]{1,2})[ ]{1}([0-9]{2})[:]{1}([0-9]{1,2})[:]{1}([0-9]{1,2})$"));
					if(tmp != null ) {//year-month-day tmp[1]-tmp[2]-tmp[3]
						if(!check_date(tmp[1], tmp[2], tmp[3]) || !check_time(tmp[4], tmp[5], tmp[6])) {
							add_error(form_item.name, msg);
						}
					}else {
						add_error(form_item.name, msg);
					}
					break;
				case 'h:i:s':
					var tmp = form_item.value.match(new RegExp("^([0-9]{2})[:]{1}([0-9]{1,2})[:]{1}([0-9]{1,2})$"));
					if(tmp != null ) {//year-month-day tmp[1]-tmp[2]-tmp[3]
						if(!check_time(tmp[1], tmp[2], tmp[3])) {
							add_error(form_item.name, msg);
						}
					}else {
						add_error(form_item.name, msg);
					}
					break;
				case 'y-m-d':
					var tmp = form_item.value.match(new RegExp("^([0-9]{4})[-./]{1}([0-9]{1,2})[-./]{1}([0-9]{1,2})$"));
					if(tmp != null ) {//year-month-day tmp[1]-tmp[2]-tmp[3]
						if(!check_date(tmp[1], tmp[2], tmp[3])) {
							add_error(form_item.name, msg);
						}
					}else {
						add_error(form_item.name, msg);
					}
					break;
				default:
					if(!check_rule[label_model] || check_rule[label_model] == 'undefined') {
						continue ;
					}else{
						if(!check_rule[label_model].test(form_item.value)){
							add_error(form_item.name, msg);
						}
					}
					break;
			}
		}
	}
	if(check_alt.length < 4) {
		for(var i = 0; i < fm.elements.length; i++){
		with(fm.elements[i]){ 
			if(fm.elements[i].type=='button' || fm.elements[i].type=='submit')
				fm.elements[i].disabled = true;
		}}
		return true;
	}
	alert(check_error + '\r\n' + check_alt);
	if(fm.elements[first_error].type != 'undefined' && fm.elements[first_error].type){
		fm.elements[first_error].focus();
	}
	first_error = "";
	return false;
}
function add_error(label, err){
	if(check_alt_count <= alert_num){
		check_alt += (check_alt_count + '.' + err + '\r\n');
		check_alt_count++;
		if(first_error.length < 1) 
			first_error = label;
	}
}
function check_date(y, m, d){
	if((y <= 0) || (m > 12 || m < 1) || (d > 31 || d < 1)) return false;
	return true;
}
function check_time(h, i, s){
	if((h > 23 || h < 1) || (i > 59 || i < 1) || (s > 59 || s < 1)) 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;
}
function set_rule(form_name, label_name, rule_str){
	var model = get_rule('model', rule_str);
	var msg = get_rule('msg', rule_str);
	var area = get_rule('area', rule_str);
	var len = get_rule('len', rule_str);
	var to = get_rule('to', rule_str);
	var rule = get_rule('rule', rule_str);
	if(document.forms[form_name] && document.forms[form_name][label_name]){
		var obj = document.forms[form_name][label_name];
		if(model) obj.setAttribute('model', model);
		if(msg) obj.setAttribute('msg', msg);
		if(area) obj.setAttribute('area', area);
		if(len) obj.setAttribute('len', len);
		if(to) obj.setAttribute('to', to);
		if(rule) obj.setAttribute('rule', rule);
	}else{
		alert( "document.forms[" + form_name + "][" + label_name + "] no defined!");
	}
}
function get_rule(attrib, rule_str){
	var preg = attrib + '=["|\']{1}([^"\']*)["|\']{1}';
	if(new RegExp(preg,"g").test(rule_str)){
		var tmp = rule_str.match(new RegExp(preg));
		return tmp[1];
	}else{
		return false;
	}
}
 

 
