/**************************************************
* * 前端注册校验
* * danxinju ,2009-5-23
* *************************************************/
var Validator = {};

/*@校验数据
[{
	id:{控件的ID:String},
	exps:{校验表达式:Array}[
		{rule:{校验规则:Function} ,msg:{提示信息:String}}
	] ,
	errorTip:{错误提示层:HTMLElement},
	rightTip:{正确提示层:HTMLElement}
}]
*/
Validator.Data = [];

/*@添加校验表达式*/
Validator.addExp = function(controlId ,exp)
{
	var data = Validator.Data;
	for(var i=0;i<data.length;i++)
	{
		if(data[i].id == controlId)
		{
			data[i].exps.push(exp);
			return;
		}
	}

	data.push({
		id: controlId ,
		exps: [exp]
	});
};

/*@校验项目*/
Validator.Integration = {};

/*校验基类*/
Validator.Integration.Base = function()
{
	this.controlId = null;
	this.msg = null;
	this.rule = null;
	this.start = null;
	this.end = null;

	this.doCheck = function(){
		Validator.blur( document.getElementById(this.controlId) );
	};

	this.init = function(controlId ,msg){
		this.controlId = controlId;
		this.msg = msg;

		Validator.addExp(this.controlId ,{
			rule:  this.rule,
			msg: this.msg ,
			start: this.start ,
			end: this.end
		});
	};
};

/*@为空校验*/
Validator.Integration.EmptyFilter = function(controlId ,msg)
{
	this.init(controlId ,msg);
};
Validator.Integration.EmptyFilter.prototype = new Validator.Integration.Base();
Validator.Integration.EmptyFilter.prototype.rule = function(val)
{
	return val != "";
};

/*@email校验*/
Validator.Integration.EmailFilter = function(controlId ,msg)
{
	this.init(controlId ,msg);
};
Validator.Integration.EmailFilter.prototype = new Validator.Integration.Base();
Validator.Integration.EmailFilter.prototype.rule = function(val)
{
	return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(val);
};

/*@Date校验*/
Validator.Integration.DateFilter = function(controlId ,msg)
{
	this.init(controlId ,msg);
};
Validator.Integration.DateFilter.prototype = new Validator.Integration.Base();
Validator.Integration.DateFilter.prototype.rule = function(val)
{
	return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(val);
};

/*@长度校验*/
Validator.Integration.LengthFilter = function(controlId ,msg ,start ,end)
{
	Validator.Integration.LengthFilter.prototype.start = start;
	Validator.Integration.LengthFilter.prototype.end = end;
	this.init(controlId ,msg);
};
Validator.Integration.LengthFilter.prototype = new Validator.Integration.Base();
Validator.Integration.LengthFilter.prototype.rule = function(val)
{
	var length = val.replace(/[^\x00-\xff]/g,"oo").length;
	return length <= this.end && length >= this.start;
}; 

/*@数字校验*/
Validator.Integration.NumberFilter = function(controlId ,msg)
{
	this.init(controlId ,msg);
};
Validator.Integration.NumberFilter.prototype = new Validator.Integration.Base();
Validator.Integration.NumberFilter.prototype.rule = function(val)
{
	return /^[0-9]{1,}$/.test(val);
};

/*@表单校验*/
Validator.Integration.FormFilter = function(formId ,callback)
{
	document.getElementById(formId).onsubmit = function(){
		return Validator.submit(callback);
	};
};

/*@获取对象位置*/
Validator.getElementPos = function(element)
{
	var offsetTop = element.offsetTop;
	var offsetLeft = element.offsetLeft;

	while(element = element.offsetParent) 
	{
		offsetTop += element.offsetTop;
		offsetLeft += element.offsetLeft;
	}
	
	return {x:offsetLeft ,y:offsetTop};
};

/*@根据Id返回当前校验数据*/
Validator.getCurrData = function(id)
{
	var data = Validator.Data;

	for (var i=0;i<data.length ;i++ )
	{
		if (id == data[i].id)
		{
			return data[i];
		}
	}
	return null;
};

/*@显示错误信息*/
Validator.showErrorTip = function(errorTip ,msg)
{
	errorTip.innerHTML = msg;
	errorTip.style.display = "";
}

/*@显示正确信息*/
Validator.showRightTip = function(rightTip ,msg)
{
	rightTip.style.display = "";
}

/*@隐藏错误信息*/
Validator.hideErrorTip = function(errorTip)
{
	errorTip.style.display = "none";
}

/*@隐藏正确信息*/
Validator.hideRightTip = function(rightTip)
{
	rightTip.style.display = "none";
}

/*@光标定位*/
Validator.focus = function(current)
{
	
};

/*@光标离开*/
Validator.blur = function(current)
{
	var data = Validator.getCurrData(current.id);
	
	var result = Validator.check(data.exps ,current.value);
	
	if (result == "")
	{
		Validator.showRightTip(data.rightTip);
		Validator.hideErrorTip(data.errorTip);
		return true;
	}
	else
	{
		Validator.hideRightTip(data.rightTip);
		Validator.showErrorTip(data.errorTip ,result);
	}
	
	return false;
};

/*@检查合法性*/
Validator.check = function(exps ,val)
{
	for(var i=0;i<exps.length;i++)
	{
		if( !exps[i].rule(val) )	
		{
			return exps[i].msg;
		}
	}

	return "";
};

/*@检查表单*/
Validator.submit = function(callback)
{
	var ret = true;
	var data = Validator.Data;

	for(var i=0;i<data.length;i++)
	{
		if( !ret )
		{
			Validator.blur( document.getElementById(data[i].id) )
		}
		else
		{
			ret = Validator.blur( document.getElementById(data[i].id) );
		}
	}
	
	if (callback && ret)
	{
		return callback();
	}
	
	return ret;
};

/*@复制节点*/
Validator.cloneNode = function(dataIntegration ,node ,attrName ,current)
{
	var temp = node.cloneNode(true);
	var pos = Validator.getElementPos(current);
	temp.style.top = (pos.y -1) +"px";
	temp.style.left = (pos.x + current.offsetWidth) +"px";

	document.body.appendChild(temp);
	dataIntegration[attrName] = temp;

	temp = null;
};

/*@注册事件*/
Validator.regEvent = function()
{
	var data = Validator.Data;
	var current = null;

	var errorTip = document.createElement("div");
	errorTip.className = "absolute wrongLayer";
	errorTip.style.display = "none";
	document.body.appendChild(errorTip);

	var rightTip = document.createElement("div");
	rightTip.className = "absolute rightMark";
	rightTip.style.display = "none";
	document.body.appendChild(rightTip);

	for(var i=0;i<data.length;i++)
	{
		
		current = document.getElementById(data[i].id);
		if(current)
		{
			current.onfocus = function(){
				Validator.focus(this);
			};
			current.onblur = function(){
				Validator.blur(this);
			};
			Validator.cloneNode(data[i] ,errorTip ,"errorTip" ,current);
			Validator.cloneNode(data[i] ,rightTip ,"rightTip" ,current);
		}
	}

	errorTip = null;
	rightTip = null;
};

Validator.clearTip = function()
{
	var data = Validator.Data;
	for(var i=0;i<data.length;i++)
	{
		Validator.hideErrorTip(data[i].errorTip);
	}
}
