﻿LeadingMIS.UI.Validate = function(){}

LeadingMIS.UI.Validate.eventObject = null;
//[~][`][!][@][#][$][%][^][&][*][(][)][_][-][+][=][|][\][{][[][}][]][:][;]["]['][<][,][>][.][?][/]
//LeadingMIS.UI.Validate.nonlicetChar = "~`$%^\\:;\"'<>";
LeadingMIS.UI.Validate.nonlicetChar = "%\\\"'<>";
//LeadingMIS.UI.Validate.nonlicetCharRegExp = /~/gim;
LeadingMIS.UI.Validate.maxVarChar = 8000;
LeadingMIS.UI.Validate.minInt = -Math.pow(2,31);
LeadingMIS.UI.Validate.maxInt = Math.pow(2,31) - 1;
LeadingMIS.UI.Validate.maxNumber = 38;
LeadingMIS.UI.Validate.maxDecimal = 0;
LeadingMIS.UI.Validate.minDate = "1900-01-01";
LeadingMIS.UI.Validate.maxDate = "2079-06-06";

LeadingMIS.UI.Validate.validateEventObject = function(eventWindow)
{
	if(typeof(eventWindow) == "undefined")
	{
		return this.validateObject(window.event.srcElement);
	}
	else
	{
		if(LeadingMIS.UI.isWindow(eventWindow) == true)
		{
			return this.validateObject(eventWindow.event.srcElement);
		}
		else
		{
			LeadingMIS.Error.setInfo(0,this.validateEventObject,"传入的参数[eventWindow]不是[window]对象","LeadingMIS.UI.Validate.validateEventObject","");
			return false;
		}
	}
}

LeadingMIS.UI.Validate.validateBody = function(tempObject,eventWindow)
{
	if(typeof(eventWindow) == "undefined")
	{
		eventWindow = window;
	}
	else
	{
		if(LeadingMIS.UI.isWindow(eventWindow) == false)
		{
			LeadingMIS.Error.setInfo(0,this.validateBody,"传入的参数[eventWindow]不是[window]对象","LeadingMIS.UI.Validate.validateBody","");
			return false;
		}
	}
	if(typeof(tempObject) == "undefined")
	{
		tempObject = eventWindow.document.body.all;
	}
	else
	{
		if(LeadingMIS.UI.isObjectArray(tempObject) == false)
		{
			LeadingMIS.Error.setInfo(0,this.validateBody,"传入的参数[tempObject]不是[object[]]对象","LeadingMIS.UI.Validate.validateBody","");
			return false;
		}
	}
	
	for(var i = 0; i < tempObject.length; i++)
	{
		if("vMode" in tempObject[i])
		{
			if(this.validateObject(tempObject[i]) == false)return false;
		}
	}
	return true;
}

LeadingMIS.UI.Validate.validateObject = function(tempObject)
{
	if(typeof(tempObject) != "object")
	{
		LeadingMIS.Error.setInfo(0,this.validateObject,"传入的参数[tempObject]不是[object]对象","LeadingMIS.UI.Validate.validateObject","");
		return false;
	}
	else
	{
		this.eventObject = tempObject;
	}
	if(!("vMode" in tempObject))
	{
		LeadingMIS.Error.setInfo(0,this.validateObject,"传入的参数[tempObject]不包含[vMode]属性(此属性为校验必要属性)","LeadingMIS.UI.Validate.validateObject","");
		return false;
	}    
	else if(!("vType" in tempObject))
	{
		LeadingMIS.Error.setInfo(0,this.validateObject,"传入的参数[tempObject]不包含[vType]属性(此属性为校验必要属性)","LeadingMIS.UI.Validate.validateObject","");
		return false;
	}
	else if(!("vInfo" in tempObject))
	{
		LeadingMIS.Error.setInfo(0,this.validateObject,"传入的参数[tempObject]不包含[vInfo]属性(此属性为校验必要属性)","LeadingMIS.UI.Validate.validateObject","");
		return false;
	}
//	else if(!("vDefaultCaption" in tempObject))
//	{
//		LeadingMIS.Error.setInfo(0,this.validateObject,"传入的参数[tempObject]不包含[vDefaultCaption]属性(此属性为校验必要属性)","LeadingMIS.UI.Validate.validateObject","");
//		return false;
//	}
//	else if(!("vEmpty" in tempObject)){return false;}
//	else if(!("vMessage" in tempObject)){return false;}
//	else if(!("vForCaption" in tempObject)){return false;}
	else
	{
	var tempMode = tempObject.vMode.trim().toLowerCase();
		var tempType = tempObject.vType.trim().toLowerCase();
		//注:表达式不能以[,]结尾!如:1[,],2和2,1[,]都不行;也可以将特殊符号用escape()进行压缩
	//	var tempInfoStr = (tempObject.vInfo.trim()).match(/[^,]*(\[,\])*[^,]+/g);
	//	var tempInfo = tempObject.vInfo.trim().split(",");
	//	for(var i = 0; i < tempInfo.length; i++)
	//	{
	//		tempInfo[i] = unescape(tempInfo[i]);
	//	}
		var tempInfo = tempObject.vInfo.trim();
		//注:可以设置对象的getValue()获得对象的值,只支持String类型
		try
		{
			var tempValue = ("getValue" in tempObject)?(tempObject.getValue().trim()):(tempObject.value.trim());
		}
		catch(e)
		{
			LeadingMIS.Error.setInfo(0,tempObject,"对象的getValue()方法必须返回String类型的值","LeadingMIS.UI.Validate.validateObject","");
			return false;
		}
		if("vForCaption" in tempObject)
		{
			if(window.document.all(tempObject.vForCaption,0))
			{
				var tempCaption = window.document.all(tempObject.vForCaption,0).innerText.trim();
			}
			else
			{
				if("vDefaultCaption" in tempObject)
				{
					var tempCaption = tempObject.vDefaultCaption.trim();
				}
				else
				{
					var tempCaption = "";
				}
			}
		}
		else
		{
			var tempCaption = tempObject.vDefaultCaption.trim();
		}
		var tempEmpty = ("vEmpty" in tempObject)?tempObject.vEmpty.trim():"true";
		var tempMessage = ("vMessage" in tempObject)?tempObject.vMessage.trim():"";
	
		if(this.validateType(tempValue,tempType,tempInfo,tempEmpty) == false)
		{
		//	if(tempMessage == "")
		//	{
		//		tempMessage = LeadingMIS.Error.Message;
		//	}
			tempMessage = LeadingMIS.Error.Message + "\n" + tempMessage;
			if(tempMode == "warning")
			{
				window.alert(tempCaption + ":" + tempMessage);
			}
			else
			{
				LeadingMIS.UI.showPopup(tempCaption + ":" + tempMessage,tempObject);
			}
			LeadingMIS.UI.selectObject(tempObject);
			return false;
		}
		else
		{
			if(tempValue.trim().length > 0)
			{
				//格式化值
				LeadingMIS.UI.formatValue(tempObject);
			}
			return true;
		}
	}
}

//varChar			字符串
//int				整数
//posInt			正整数
//nonPosInt			非正整数
//negInt			负整数
//nonNegInt			非负整数
//decimal			小数
//posDecimal		正小数
//nonPosDecimal		非正小数
//negDecimal		负小数
//nonNegDecimal		非负小数
//datetime			日期时间
//date				日期
//time				时间
//
//test				WebService校验（示例）
//=====注意=====
//如果需要验证bigInt,smallInt等类型需要扩展

LeadingMIS.UI.Validate.validateType = function(tempValue,tempType,tempInfo,tempEmpty)
{
	if((tempValue.trim() == "") && (tempEmpty.toBool() == false))
	{
		LeadingMIS.Error.setInfo(0,this.validateType,"不允许为空","LeadingMIS.UI.Validate.validateType","");
		return false;
	}
	else if((tempValue.trim() == "") && (tempEmpty.toBool() == true))
	{
		return true;
	}
	else
	{
		switch(tempType.toLowerCase())
		{
			case "varchar"://VarChar
				return this.validateVarChar(tempValue,tempInfo);
				break;
			case "int"://Int
				return this.validateInt(tempValue,tempInfo);
				break;
			case "posint"://PosInt
				return this.validatePosInt(tempValue);
				break;
			case "nonposint"://NonPosInt
				return this.validateNonPosInt(tempValue);
				break;
			case "negint"://NegInt
				return this.validateNegInt(tempValue);
				break;
			case "nonnegint"://NonNegInt
				return this.validateNonNegInt(tempValue);
				break;
			case "decimal"://Decimal
				return this.validateDecimal(tempValue,tempInfo);
				break;
			case "posdecimal"://PosDecimal
				return this.validatePosDecimal(tempValue,tempInfo);
				break;
			case "nonposdecimal"://NonPosDecimal
				return this.validateNonPosDecimal(tempValue,tempInfo);
				break;
			case "negdecimal"://NegDecimal
				return this.validateNegDecimal(tempValue,tempInfo);
				break;
			case "nonnegdecimal"://NonNegDecimal
				return this.validateNonNegDecimal(tempValue,tempInfo);
				break;
			case "datetime"://DateTime
				return this.validateDateTime(tempValue,tempInfo);
				break;
			case "date"://Date
				return this.validateDate(tempValue,tempInfo);
				break;
			case "time"://Time
				return this.validateTime(tempValue,tempInfo);
				break;
			case "test"://Test
				return LeadingMIS.WebService.Test(tempValue,tempInfo);
				break;
			default:
				LeadingMIS.Error.setInfo(0,this.validateType,"校验类型非法","LeadingMIS.UI.Validate.validateType","");
				return false;
				break;
		}
	}
}

LeadingMIS.UI.Validate.validateVarChar = function(tempValue,tempInfo)
{
	var objectInfo = LeadingMIS.ValueType.ToObject(tempInfo,["max","check"]);
	objectInfo["max"] = LeadingMIS.ValueType.ToInt(objectInfo["max"],this.maxVarChar);
	objectInfo["check"] = unescape(LeadingMIS.ValueType.FromNull(objectInfo["check"],"")).trim();
	
	if(tempValue.isCoverChar(this.nonlicetChar) == true)
	{
		LeadingMIS.Error.setInfo(0,this.validateVarChar,"不允许包含非法字符\n如：" + this.nonlicetChar,"LeadingMIS.UI.Validate.validateVarChar","");
		return false;
	}
	else
	{
		if(objectInfo["check"] == "")
		{
			return LeadingMIS.ValueType.SQL.isVarChar(tempValue,objectInfo["max"]);
		}
		else
		{
			if(tempValue.isMatchRegExp(objectInfo["check"].toRegExp()) == true)
			{
				return LeadingMIS.ValueType.SQL.isVarChar(tempValue,objectInfo["max"]);
			}
			else
			{
				LeadingMIS.Error.setInfo(0,this.validateVarChar,"不符合指定格式","LeadingMIS.UI.Validate.validateVarChar","");
				return false;
			}
		}
	}
}

LeadingMIS.UI.Validate.validateInt = function(tempValue,tempInfo)
{
	var objectInfo = LeadingMIS.ValueType.ToObject(tempInfo,["min","max","format"]);
	objectInfo["min"] = LeadingMIS.ValueType.ToInt(objectInfo["min"],this.minInt);
	objectInfo["max"] = LeadingMIS.ValueType.ToInt(objectInfo["max"],this.maxInt);
	objectInfo["format"] = unescape(LeadingMIS.ValueType.FromNull(objectInfo["format"],"")).trim();
	
	if(objectInfo["format"] == "")
	{
		return LeadingMIS.ValueType.isInt(tempValue.toInt(null),objectInfo["min"],objectInfo["max"]);
	}
	else
	{
		return LeadingMIS.ValueType.isInt(tempValue.replace(objectInfo["format"],"").toInt(null),objectInfo["min"],objectInfo["max"]);
	}
}

LeadingMIS.UI.Validate.validatePosInt = function(tempValue)
{
	if(parseInt(tempValue,10) > 0)
	{
		return LeadingMIS.ValueType.SQL.isInt(tempValue.toInt(null));
	}
	else
	{
		LeadingMIS.Error.setInfo(0,this.validatePosInt,"请输入正整数","LeadingMIS.UI.Validate.validatePosInt","");
		return false;
	}
}

LeadingMIS.UI.Validate.validateNonPosInt = function(tempValue)
{
	if(parseInt(tempValue,10) <= 0)
	{
		return LeadingMIS.ValueType.SQL.isInt(tempValue.toInt(null));
	}
	else
	{
		LeadingMIS.Error.setInfo(0,this.validateNonPosInt,"请输入非正整数","LeadingMIS.UI.Validate.validateNonPosInt","");
		return false;
	}
}

LeadingMIS.UI.Validate.validateNegInt = function(tempValue)
{
	if(parseInt(tempValue,10) < 0)
	{
		return LeadingMIS.ValueType.SQL.isInt(tempValue.toInt(null));
	}
	else
	{
		LeadingMIS.Error.setInfo(0,this.validateNegInt,"请输入负整数","LeadingMIS.UI.Validate.validateNegInt","");
		return false;
	}
}

LeadingMIS.UI.Validate.validateNonNegInt = function(tempValue)
{
	if(parseInt(tempValue,10) >= 0)
	{
		return LeadingMIS.ValueType.SQL.isInt(tempValue.toInt(null));
	}
	else
	{
		LeadingMIS.Error.setInfo(0,this.validateNonNegInt,"请输入非负整数","LeadingMIS.UI.Validate.validateNonNegInt","");
		return false;
	}
}

LeadingMIS.UI.Validate.validateDecimal = function(tempValue,tempInfo)
{
	var objectInfo = LeadingMIS.ValueType.ToObject(tempInfo,["max","decimal","format"]);
	objectInfo["max"] = LeadingMIS.ValueType.ToInt(objectInfo["max"],this.maxNumber);
	objectInfo["decimal"] = LeadingMIS.ValueType.ToInt(objectInfo["decimal"],this.maxDecimal);
	objectInfo["format"] = unescape(LeadingMIS.ValueType.FromNull(objectInfo["format"],"")).trim();
	
	if(objectInfo["format"] == "")
	{
		return LeadingMIS.ValueType.isDecimal(tempValue.toFloat(null),objectInfo["max"],objectInfo["decimal"]);
	}
	else
	{
		return LeadingMIS.ValueType.isDecimal(tempValue.replace(objectInfo["format"],"").toFloat(null),objectInfo["max"],objectInfo["decimal"]);
	}
}

LeadingMIS.UI.Validate.validatePosDecimal = function(tempValue,tempInfo)
{
	var objectInfo = LeadingMIS.ValueType.ToObject(tempInfo,["max","decimal"]);
	objectInfo["max"] = LeadingMIS.ValueType.ToInt(objectInfo["max"],this.maxNumber);
	objectInfo["decimal"] = LeadingMIS.ValueType.ToInt(objectInfo["decimal"],this.maxDecimal);
	
	if(parseFloat(tempValue) > 0)
	{
		return LeadingMIS.ValueType.isDecimal(tempValue.toFloat(null),objectInfo["max"],objectInfo["decimal"]);
	}
	else
	{
		LeadingMIS.Error.setInfo(0,this.validatePosDecimal,"请输入正数","LeadingMIS.UI.Validate.validatePosDecimal","");
		return false;
	}
}

LeadingMIS.UI.Validate.validateNonPosDecimal = function(tempValue,tempInfo)
{
	var objectInfo = LeadingMIS.ValueType.ToObject(tempInfo,["max","decimal"]);
	objectInfo["max"] = LeadingMIS.ValueType.ToInt(objectInfo["max"],this.maxNumber);
	objectInfo["decimal"] = LeadingMIS.ValueType.ToInt(objectInfo["decimal"],this.maxDecimal);
	if(parseFloat(tempValue) <= 0)
	{
		return LeadingMIS.ValueType.isDecimal(tempValue.toFloat(null),objectInfo["max"],objectInfo["decimal"]);
	}
	else
	{
		LeadingMIS.Error.setInfo(0,this.validateNonPosDecimal,"请输入非正数","LeadingMIS.UI.Validate.validateNonPosDecimal","");
		return false;
	}
}

LeadingMIS.UI.Validate.validateNegDecimal = function(tempValue,tempInfo)
{
	var objectInfo = LeadingMIS.ValueType.ToObject(tempInfo,["max","decimal"]);
	objectInfo["max"] = LeadingMIS.ValueType.ToInt(objectInfo["max"],this.maxNumber);
	objectInfo["decimal"] = LeadingMIS.ValueType.ToInt(objectInfo["decimal"],this.maxDecimal);
	
	if(parseFloat(tempValue) < 0)
	{
		return LeadingMIS.ValueType.isDecimal(tempValue.toFloat(null),objectInfo["max"],objectInfo["decimal"]);
	}
	else
	{
		LeadingMIS.Error.setInfo(0,this.validateNegDecimal,"请输入负数","LeadingMIS.UI.Validate.validateNegDecimal","");
		return false;
	}
}

LeadingMIS.UI.Validate.validateNonNegDecimal = function(tempValue,tempInfo)
{
	var objectInfo = LeadingMIS.ValueType.ToObject(tempInfo,["max","decimal"]);
	objectInfo["max"] = LeadingMIS.ValueType.ToInt(objectInfo["max"],this.maxNumber);
	objectInfo["decimal"] = LeadingMIS.ValueType.ToInt(objectInfo["decimal"],this.maxDecimal);
	
	if(parseFloat(tempValue) >= 0)
	{
		return LeadingMIS.ValueType.isDecimal(tempValue.toFloat(null),objectInfo["max"],objectInfo["decimal"]);
	}
	else
	{
		LeadingMIS.Error.setInfo(0,this.validateNonNegDecimal,"请输入非负数","LeadingMIS.UI.Validate.validateNonNegDecimal","");
		return false;
	}
}

LeadingMIS.UI.Validate.validateDateTime = function(tempValue,tempInfo)
{
	var objectInfo = LeadingMIS.ValueType.ToObject(tempInfo,["min","max"]);
	objectInfo["min"] = LeadingMIS.ValueType.FromNull(objectInfo["min"],this.minDate);
	objectInfo["max"] = LeadingMIS.ValueType.FromNull(objectInfo["max"],this.maxDate);
	
	return LeadingMIS.ValueType.isDateTime(tempValue,objectInfo["min"],objectInfo["max"]);
}

LeadingMIS.UI.Validate.validateDate = function(tempValue,tempInfo)
{
	var objectInfo = LeadingMIS.ValueType.ToObject(tempInfo,["min","max"]);
	
	objectInfo["min"] = LeadingMIS.ValueType.FromNull(objectInfo["min"],this.minDate);
	objectInfo["max"] = LeadingMIS.ValueType.FromNull(objectInfo["max"],this.maxDate);
	
	return LeadingMIS.ValueType.isDateTime(tempValue,objectInfo["min"],objectInfo["max"]);
}

LeadingMIS.UI.Validate.validateTime = function(tempValue,tempInfo)
{
	var objectInfo = LeadingMIS.ValueType.ToObject(tempInfo,["min","max"]);
	objectInfo["min"] = LeadingMIS.ValueType.FromNull(objectInfo["min"],this.minDate);
	objectInfo["max"] = LeadingMIS.ValueType.FromNull(objectInfo["max"],this.maxDate);
	
	return LeadingMIS.ValueType.isDateTime(tempValue,objectInfo["min"],objectInfo["max"]);
}
