//捕获控制类型
var ctl_type = "text,textarea,password,checkbox,select-one,select-multiple";



//多提示框设置
var tip_multiple = true;

//指定提示框
var tip_id = "_tip";
/*
tip_multiple的值决定tip_id的用法
true:
程序自动将[控件.name + tip_id]组合成字符串id并寻找该对象, 设置其innerHTML
如: form中有名为username的text控件, tip_id='_tip', username的提示框是: id为username_tip的对象
执行代码: document.getElementById(this.name + tip_id)

false:
所有控件的提示内容使用tip_id指定的对象显示.
执行代码: document.getElementById(tip_id)
//*/

//提示框样式
var tip_area_cls = new Array();
tip_area_cls["default"] = "ajax_line_right";
tip_area_cls["active"] = "ajax_line_right_active";
tip_area_cls["err"] = "ajax_line_right_error";
tip_area_cls["ok"] = "ajax_line_right_ok";

//提示
var tip_str = {};
tip_str[0] = {
"default":"register_line_right",
"active":"register_line_right_active",
"err":"register_line_right_error",
"ok":"register_line_right_ok" };


//CSS
var ctl_class = new Array();
ctl_class["default"] = "default_input";
ctl_class["active"] = "active_input";
ctl_class["err"] = "error_input";
ctl_class["ok"] = "ok_input";

//检测控件类型
function _inColl(s)
{
	if (ctl_type=="") return true; //捕获所有input类型
	var v = ","+ctl_type+",";
	s = ","+s.toLowerCase()+",";
	return (v.indexOf(s)!=-1);
}

//设置控件背景色
function _setBC(obj,state)
{
	if (typeof(obj) != "object") return;
	if ((typeof(ctl_class[state])!="undefined") && ctl_class[state]!="")
	{
		$(obj).addClass( ctl_class[state] );
	}
}

//显示提示信息
function _showInfo(obj,state)
{
	//alert("showInfo: " + obj.name + " " + state);
	if (typeof(obj) != "object") return;
	if (typeof(tip_str[obj.name]) != "object") return;
	var str_info = "";
	try
	{
		if (typeof(tip_str[obj.name][state]) == "undefined")
		{
			if (typeof(tip_str[obj.name]["default"]) == "undefined")
			{
				return;
			}
			else
			{
				str_info = tip_str[obj.name]["default"];
			}
		}
		else
		{
			str_info = tip_str[obj.name][state];
		}
		tip_info = (tip_multiple)?obj.name + tip_id:tip_id;
		$("#" + tip_info).html(str_info);
		$("#" + tip_info).addClass(tip_area_cls[state]);
	}
	catch(e)
	{
		//alert("showInfo: "+e.description);
	}
}

//自定义提示信息
function showInfo(obj_id, msg, cls)
{
	var obj = document.getElementById(obj_id);
	if ((typeof(obj) != "object")||(obj==null)) return;
	obj.innerHTML = msg;
	obj.className = cls;
}

//数据验证
function _checkValid(obj)
{
	if (typeof(obj) != "object") return;
	var flag = "undefined";
	
	try
	{
		var a = eval("check_"+obj.name);
		//alert("checkValid: check_"+obj.name+"() " + typeof(a));
		if (typeof(a)=="function") flag = a(obj); //调用check_控件名()函数验证数据
	}
	catch(e)
	{
		//alert("checkValid: " + e.number + " " + e.description);
		_setBC(obj,"ok");
		_showInfo(obj,"ok");
		return;
	}
	
	switch(flag)
	{
		case "undefined":
		case true:
			_setBC(obj,"ok");
			_showInfo(obj,"ok");
			break;
		
		case false:
			_setBC(obj,"err");
			_showInfo(obj,"err");
			break;
		
		default:
			break;
	}
}

//指定需要特效的Form
function setCtlBehavior(id)
{
	var theForm = document.getElementById(id);
	if (typeof(theForm)!="object") return;
	for(j=0;j<theForm.length;j++)
	{
		if ( _inColl(theForm[j].type))
		{
			theForm[j].onblur = function() { _checkValid(this); }
			theForm[j].onfocus = function() { _setBC(this,"active"); _showInfo(this,"active"); }
			_setBC(theForm[j],"default");
			_showInfo(theForm[j],"default");
		}
	}
}