var onMouseClickSelectBool = false;
var onMouseClickSelectRow = null;
//回车单击
function enterKeyClick(obj)
{
	if(event.keyCode == 13) 
	{
		obj.click();
	}
}
//将回车键处理为Tab
function onKeyDownDefault()
{
	if (window.event.keyCode == 13 && window.event.ctrlKey == false && window.event.altKey == false)
	{
		if (window.event.srcElement.type != "button") 
		{
			if (window.event.srcElement.type != "textarea" && window.event.srcElement.type != "file")
			window.event.keyCode = 9;
		}
	}
}
//a-z  A-Z
function onKeyPressInputCharacter()
{
	var nKey = window.event.keyCode;

	if (!((nKey > 96 && nKey < 123) || (nKey > 64 && nKey < 91)))
		window.event.keyCode = 0;
}
//如果某个输入域为实数，那么过滤掉所有非数字字符（但包含小数点）
function onKeyPressInputFloat()
{
	var nKey = window.event.keyCode;

	if (nKey > 57 || (nKey != 46 && nKey < 48))
		window.event.keyCode = 0;
}
//如果某个输入域为整数，那么过滤掉所有非数字字符
function onKeyPressInputInteger()
{
	var nKey = window.event.keyCode;

	if (nKey < 48 || nKey > 57)
		window.event.keyCode = 0;
}
/************************** begin 数字校验 ***********************/
//绑定校验数字
function bindDigitToInput(objInput, isDisabled)
{
	objInput.disabled = isDisabled;
	objInput.onblur = onNumericCheckBlur;
}
//
function onNumericCheckBlur()
{
	var obj;
	
	if (arguments.length > 0 && typeof(arguments[0]) == "object")
		obj = arguments[0];
	else
		obj = window.event.srcElement;
	var num = obj.value;
	
	try
	{
		if(obj.value != "")
		{
			var max = isDefined(obj.max) ? parseFloat(obj.max) : MAX;
			var min = isDefined(obj.min) ? parseFloat(obj.min) : MIN;
			
			num = getFloatM(num, max, min);
			if(isDefined(obj.precision) && isDefined(obj.digit))
				num = getFloat(num, parseInt(obj.precision), parseInt(obj.digit));
		}
		obj.originValue = obj.value = num;
	}
	catch(e)
	{
		alert(e);
		if (isDefined(obj.originValue))
			obj.value = obj.originValue;
		else
			obj.value = "";
		obj.focus();
		return false;
	}
}
/************************** end 数字校验 ***********************/
/************************** begin textarea校验 ***********************/
//绑定 textarea 
function bindTextarea(objInput, isDisabled)
{
	objInput.disabled = isDisabled;
	objInput.onblur = onTextareaCheckBlur;
}
function onTextareaCheckBlur()
{
	var obj;
	var max = -1;
	
	if (arguments.length > 0 && typeof(arguments[0]) == "object")
		obj = arguments[0];
	else
		obj = window.event.srcElement;
	var str = obj.value;
	
	try
	{
		if(obj.value != "" && isDefined(obj.maxLength))
		{
			var max = parseInt(obj.value.length);
			str = obj.value.substring(0, parseInt(obj.maxLength));
		}
		obj.originValue = obj.value = str;
		if(max != -1 && max > obj.maxLength)
			throw "最多只能录入" + obj.maxLength + "个字符";
	}
	catch(e)
	{
		alert(e);
		if (isDefined(obj.originValue))
			obj.value = obj.originValue;
		else
			obj.value = "";
		obj.focus();
		return false;
	}
}
/************************** end textarea校验 ***********************/
//绑定所有校检及日期控制
function bindAllCheck(collection, isDisabled, isLimitNowDate)
{
	var len = collection.length;
	for(var i = 0; i < len; i++)
	{
		var currObj = collection(i);
		if(isUndefined(typeof(currObj.format)) == false)
		{
			if(currObj.format == DATE)
				bindCalenderToInput(currObj, isDisabled, isLimitNowDate)
			else if(currObj.format == DIGIT)
				bindDigitToInput(currObj, isDisabled);
			else if(currObj.format == TEXT)
				bindTextarea(currObj, isDisabled);
		}
	}
}
//当某个输入项处于激活状态
function onElementActive()
{
	var newElement = window.document.activeElement;
	if (oldElement != null)
		oldElement.className = oldElementClass;

	if (newElement)
	{
		if (newElement.tagName == "INPUT")
		{
			if ((newElement.type == "text" || newElement.type == "textarea" || newElement.type == "file" || newElement.type == "password"))
				if(newElement.readOnly == false)
				{
					
					oldElementClass = newElement.className;
					oldElement = newElement;
					newElement.className = oldElementClass +" activeElement";
				}
		}
		else if (newElement.tagName == "SELECT")
		{
			oldElementClass = newElement.className;
			oldElement = newElement;
		}
	}
}
//初始化绑定样式及事件
function initDocumentEvents()
{
	oldElement = null;
	oldElementClass = "";

	if (document.body.onkeydown == null)
		document.body.onkeydown = onKeyDownDefault;

	for (var i = 0; i < document.all.length; i++)
	{
		var obj = document.all(i);

		if ((obj.tagName == "INPUT" && (obj.type == "text" || obj.type == "file" || obj.type == "password")) || (obj.type == "textarea"))
		{
			if (obj.onfocus == null)
			{
				obj.onfocus = onElementActive;
			}
		}
		else if (obj.tagName == "SELECT")
		{
			if (obj.onfocus == null)
			{
				obj.onfocus = onElementActive;
			}
		}
	}
}
function onGridMouseOver()
{
	try
	{
		if(onMouseClickSelectBool == false)
		{
			var obj = getOwnerTR(window.event.srcElement);
			if (!((obj.oldClassName != null) && (typeof(obj.oldClassName) != "undefined")))
			{	
				obj.oldClassName = "gridOldClassNameRow";
			}
			if(obj.parentElement.parentElement.clickTR == null)
			{
				obj.className = "gridHighlight";
			}
			else
			{
				if(obj.parentElement.parentElement.clickTR == "none")
				{
				obj.className = "gridHighlight";
				}
			}
		}
	}
	catch(e)
	{
	}
}
function onGridMouseClick(cFun)
{
	try
	{
		var obj = getOwnerTR(window.event.srcElement);
		if(onMouseClickSelectRow == obj)
		{
			onMouseClickSelectBool = false;
			onMouseClickSelectRow = null;
		}
		else
		{
			if(onMouseClickSelectRow != null)
			{
				onMouseClickSelectRow.className = onMouseClickSelectRow.oldClassName;
			}
			onMouseClickSelectRow = obj;
			onMouseClickSelectRow.className = "gridHighlight";
			onMouseClickSelectBool = true;
		}
		if(arguments[0] != null)
		{
			cFun();
		}
	}
	catch(e)
	{	
	}
}
function onGridMouseOut()
{
	try
	{
		if(onMouseClickSelectBool == false)
		{
			var obj = getOwnerTR(window.event.srcElement);
			if ((obj.oldClassName != null) && (typeof(obj.oldClassName) != "undefined"))
			{
				if(obj.parentElement.parentElement.clickTR == null)
				{
					obj.className = obj.oldClassName;
				}
				else
				{
					if(obj.parentElement.parentElement.clickTR == "none")
					{
						obj.className = obj.oldClassName;
					}
				}
			}
		}
	}
	catch(e)
	{
	}
}
function onGridClick()
{
	var obj = getOwnerTR(window.event.srcElement);
	if ((obj.oldClassName != null) && (typeof(obj.oldClassName) != "undefined"))
	{
		if(obj.parentElement.parentElement.clickTR != "none")
		{
			obj.parentElement.parentElement.clickTR.className = obj.parentElement.parentElement.clickTR.oldClassName; 
		}
		obj.parentElement.parentElement.clickTR = obj;
		obj.parentElement.parentElement.clickTR.className = "gridHighlight";
	}
}
//查询
function searchClick()
{
	setHidden("searchEvent", "searchEvent", "searchEvent");
	document.forms[0].submit();
}
