/*-------------------------------------------------------------
* 验证客户端输入数据
*
* 参考资料：
*	CheckInput.vbs
*	互联网
*	MSDN Regular Expression 
*
*
* 修改纪录：
*	2001.8.29	增加 isPostalCode
*	2001.8.16	增加 isURL
*	2001.8.15	改为使用正则表达式进行验证
*				并增加了 isMoney
*
*	2001.8.1	修改了 isDate，新增加 checkLength
*------------------------------------------------------------*/


// 为 Array 类增加一个 max 方法
Array.prototype.max = function()
{
	var i, max = this[0];
	
	for( i = 1; i < this.length; i++ )
	{
		if( max < this[i] )
		max = this[i];
	}
	
	return max;
}

// 为 String 类增加一个 trim 方法
String.prototype.trim = function()
{
    // 用正则表达式将前后空格用空字符串替代。
    return this.replace( /(^\s*)|(\s*$)/g, "" );
}

// 使用正则表达式，检测 s 是否满足模式 re
function checkExp( re, s )
{
	return re.test( s );
}

// 验证是否 字母数字
function isAlphaNumeric( strValue )
{
	// 只能是 A-Z a-z 0-9 之间的字母数字 或者为空
	return checkExp( /^\w*$/gi, strValue );
}

// 验证是否 日期
function isDate( strValue )
{
	// 日期格式必须是 2001-10-1/2001-1-10 或者为空
	if( isEmpty( strValue ) ) return true;

	if( !checkExp( /^\d{4}-[01]?\d-[0-3]?\d$/g, strValue ) ) return false;
	// 或者 /^\d{4}-[1-12]-[1-31]\d$/
	
	var arr = strValue.split( "-" );
	var year = arr[0];
	var month = arr[1];
	var day = arr[2];
	
	// 1 <= 月份 <= 12，1 <= 日期 <= 31
	if( !( ( 1<= month ) && ( 12 >= month ) && ( 31 >= day ) && ( 1 <= day ) ) )
		return false;
		
	// 润年检查
	if( !( ( year % 4 ) == 0 ) && ( month == 2) && ( day == 29 ) )
		return false;
	
	// 7月以前的双月每月不超过30天
	if( ( month <= 7 ) && ( ( month % 2 ) == 0 ) && ( day >= 31 ) )
		return false;
	
	// 8月以后的单月每月不超过30天
	if( ( month >= 8) && ( ( month % 2 ) == 1) && ( day >= 31 ) )
		return false;
	
	// 2月最多29天
	if( ( month == 2) && ( day >=30 ) )
		return false;
	
	return true;
}

// 验证是否 Email
function isEmail( strValue )
{
	// Email 必须是 x@a.b.c.d 等格式 或者为空
	if( isEmpty( strValue ) ) return true;
	
	return checkExp( /^\w+@(\w+\.)+\w+$/gi, strValue );
}

// 验证是否 为空
function isEmpty( strValue )
{
	if( strValue == "" )
		return true;
	else
		return false;
}

// 验证是否 数字
function isNumeric( strValue )
{
	// 数字必须是 0123456789 或者为空
	
	return checkExp( /^\d*$/g, strValue );
}

// 验证是否 浮点数
var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/

function isFloat (s)
{   if (isEmpty(s)) 
       if (isFloat.arguments.length == 1) return false;
       else return (isFloat.arguments[1] == true);

    return reFloat.test(s)
}


// 验证是否 货币
function isMoney( strValue )
{
	// 货币必须是 -12,345,678.9 等格式 或者为空
	if( isEmpty( strValue ) ) return true;
	
	return checkExp( /^[+-]?\d+(,\d{3})*(\.\d+)?$/g, strValue );
}

// 验证是否 电话
function isPhone( strValue )
{
	// 普通电话	(0755)4477377-3301/(86755)6645798-665
	// Call 机	95952-351
	// 手机		130/131/135/136/137/138/13912345678
	// 或者为空
	if( isEmpty( strValue ) ) return true;
	
	return checkExp( /(^\(\d{3,5}\)\d{6,8}(-\d{2,8})?$)|(^\d+-\d+$)|(^(130|131|135|136|137|138|139)\d{8}$)/g, strValue );
}

// 验证是否 邮政编码
function isPostalCode( strValue )
{
	// 邮政编码必须是6位数字
	return checkExp( /(^$)|(^\d{6}$)/gi, strValue )
}

// 验证是否 URL
function isURL( strValue )
{
	// http://www.yysoft.com/ssj/default.asp?Type=1&ArticleID=789
	if( isEmpty( strValue ) ) return true;
	
	var pattern = /^(http|https|ftp):\/\/(\w+\.)+[a-z]{2,3}(\/\w+)*(\/\w+\.\w+)*(\?\w+=\w*(&\w+=\w*)*)*/gi;
	// var pattern = /^(http|https|ftp):(\/\/|\\\\)(\w+\.)+(net|com|cn|org|cc|tv|[0-9]{1,3})((\/|\\)[~]?(\w+(\.|\,)?\w\/)*([?]\w+[=])*\w+(\&\w+[=]\w+)*)*$/gi;
	// var pattern = ((http|https|ftp):(\/\/|\\\\)((\w)+[.]){1,}(net|com|cn|org|cc|tv|[0-9]{1,3})(((\/[\~]*|\\[\~]*)(\w)+)|[.](\w)+)*(((([?](\w)+){1}[=]*))*((\w)+){1}([\&](\w)+[\=](\w)+)*)*)/gi;

	return checkExp( pattern, strValue );
	
}

// 检查字段长度
//
//	strValue	字符串
//	strParam	检查参数，形如：L<10, L=5, L>117
//
function checkLength( strValue, strParam )
{
	if( isEmpty( strValue ) )	return true;
	
	// 参数形如：L<10, L=5, L>117
	if( strParam.charAt( 0 ) != 'L' )	return false;
	
	var l = strValue.length;
	var ml = parseInt( strParam.substr( 2 ) );
	
	switch( strParam.charAt( 1 ) )
	{
		case '<' :
			if( l >= ml )
				return false;
			break;
			
		case '=' :
			if( l != ml )
				return false;
			break;
			
		case '>' :
			if( l <= ml )
				return false;
			break;
			
		default :
			return false
	}
	
	return true;
}

// 检查输入数据的合法性（应用在离开字段时）
//
//	输入参数
//		obj		字段对象
//		strDescription	字段描述
//		strType	字段类型
//
function CheckValid( obj, strDescription, strType)
{
	var strMsg = "";
	var strValue = obj.value.trim();
	
	switch( strType )
	{
		case "AlphaNumeric" :	// 字母数字
			if( !isAlphaNumeric( strValue ) )
				strMsg = '"' + strDescription + '" 必须是字母或数字！\n';
			break;
			
		case "Date" :	// 日期
			if( !isDate( strValue ) ) 
				strMsg = '"' + strDescription + '" 必须具有正确的日期格式，如 2001-10-01\n';
			break;
				
		case "Email" :	// 电子邮件
			if( !isEmail( strValue ) )
				strMsg = '"' + strDescription + '" 必须具有正确的邮件格式，如 xx@yy.com\n';
			break;
				
		case "NotEmpty" :	// 不许空值
			if( isEmpty( strValue ) )
				strMsg = '"' + strDescription + '" 不能为空！\n';
			break;
				
		case "Numeric" :	//数字
			if( !isNumeric( strValue )  )
				strMsg = '"' + strDescription + '" 必须是数字！\n';
			break;
		
		case "Float" :	//浮点数
			if( !isFloat( strValue )  )
				strMsg = '"' + strDescription + '" 必须是浮点数！\n';
			break;
		
		case "Money" :	//货币
			if( !isMoney( strValue )  )
				strMsg = '"' + strDescription + '" 必须具有正确的货币格式，如 -123,456.789\n';
			break;
					
		case "Phone" :	// 电话
			if( !isPhone( strValue ) )
				strMsg = '"' + strDescription + '" 必须具有正确的电话格式，如 (0755)1234567-999\n';
			break;
			
		case "PostalCode" :	// 邮政编码
			if( !isPostalCode( strValue ) )
				strMsg = '"' + strDescription + '" 必须是6位数字！\n';
			break;
			
		case "URL" :	// URL
			if( !isURL( strValue ) )
				strMsg = '"' + strDescription + '" 必须是正确的URL格式！\n';
			break;
				
		default :	// 其他
			if( arrType[i].charAt( 0 ) == 'L' )
			{
				if( !checkLength( strValue, arrType[i] ) )
					strMsg = '"' + strDescription + '" 的长度必须 ' + arrType[i].substr(1) + '\n';
			}
			else
				strMsg = '错误："' + strDescription + '" 的类型 "' + strType + '" 不能识别！\n';
	}
	
	if( strMsg != "" ) 
	{
		window.alert( strMsg );
		obj.focus();
	}
	
	return;
}

// 验证输入数据的合法性
//
//	输入参数
//		strName	字段名
//		strDescription	字段描述
//		strType	字段类型
//
//	输出参数
//		空串	通过验证
//		非空	未通过验证
//
function Validate( strName, strDescription, strType)
{
	var strMsg = "";
	var strValue = document.all( strName ).value.trim();
	var arrType = strType.split( " " );
	
	for( var i = 0; i < arrType.length; i++ )
		switch( arrType[i] )
		{
			case "AlphaNumeric" :	// 字母数字
				if( !isAlphaNumeric( strValue ) )
					strMsg = '"' + strDescription + '" 必须是字母或数字！\n';
				break;
			
			case "Date" :	// 日期
				if( !isDate( strValue ) ) 
					strMsg = '"' + strDescription + '" 必须具有正确的日期格式，如 2001-10-1\n';
				break;
				
			case "Email" :	// 电子邮件
				if( !isEmail( strValue ) )
					strMsg = '"' + strDescription + '" 必须具有正确的邮件格式，如 xx@yy.com\n';
				break;
				
			case "NotEmpty" :	// 不许空值
				if( isEmpty( strValue ) )
					strMsg = '"' + strDescription + '" 不能为空！\n';
				break;
				
			case "Numeric" :	//数字
				if( !isNumeric( strValue )  )
					strMsg = '"' + strDescription + '" 必须是数字！\n';
				break;

			case "Float" :	//浮点数
				if( !isFloat( strValue )  )
					strMsg = '"' + strDescription + '" 必须是浮点数！\n';
				break;
				
			case "Money" :	//货币
				if( !isMoney( strValue )  )
					strMsg = '"' + strDescription + '" 必须具有正确的货币格式，如 -123,456.789\n';
				break;
					
			case "Phone" :	// 电话
				if( !isPhone( strValue ) )
					strMsg = '"' + strDescription + '" 必须具有正确的电话格式，如 (0755)1234567-999\n';
				break;
			
			case "PostalCode" :	// 邮政编码
				if( !isPostalCode( strValue ) )
					strMsg = '"' + strDescription + '" 必须是6位数字！\n';
				break;
				
			case "URL" :	// URL
				if( !isURL( strValue ) )
					strMsg = '"' + strDescription + '" 必须是正确的URL格式！\n';
				break;
				
			default :	// 其他
				if( arrType[i].charAt( 0 ) == 'L' )
				{
					if( !checkLength( strValue, arrType[i] ) )
						strMsg = '"' + strDescription + '" 的长度必须 ' + arrType[i].substr(1) + '\n';
				}
				else
					strMsg = '错误："' + strDescription + '" 的类型 "' + strType + '" 不能识别！\n';
		}
	
	return strMsg;
}

// 确认删除
function confirm_delete( url )
{
	if( confirm( "您确实要删除吗？" ) )
	{
		window.location = ( url )
	}
}
