// 为 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;
}

//字符串trim方法
String.prototype.trim = function()
{
    // 用正则表达式将前后空格用空字符串替代。
    return this.replace( /(^\s*)|(\s*$)/g, "" );
}

// 执行正则表达式
function executeExp( re, s )
{
	return re.test( s );
}

//字母数字
function isAlphaNumeric( strValue )
{
	// 只能是 A-Z a-z 0-9 之间的字母数字 或者为空
	return executeExp( /^\w*$/gi, strValue );
}

//日期判断
function isDate( strValue )
{
	if( isEmpty( strValue ) ) return true;

	if( !executeExp( /^\d{4}-[01]?\d-[0-3]?\d$/g, strValue ) ) return false;
	
	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 )
{
	if( isEmpty( strValue ) ) return true;
	
	var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
	return executeExp( pattern, strValue );
	
}

//字符串是否为空
function isEmpty( strValue )
{
	if( strValue == "" )
		return true;
	else
		return false;
}
//字段是否为空
function checkEmpty(field)
{
	if(document.all(field)!=null)
	{
		if(document.all(field).value.trim()=="")
			return true;
	}
	return false;
}
//是否为数字
function isNumeric( strValue )
{
	return executeExp( /^\d*$/g, strValue );
}

//货币
function isMoney( strValue )
{
	if( isEmpty( strValue ) ) return true;
	return executeExp( /^[+-]?\d+(,\d{3})*(\.\d+)?$/g, strValue );
}

//是否为电话
function isPhone( strValue )
{
	if( isEmpty( strValue ) ) return true;
	return executeExp( /(^\(\d{3,5}\)\d{6,8}(-\d{2,8})?$)|(^\d+-\d+$)|(^(130|131|135|136|137|138|139|157|158|188|189|186|151|152|153|154)\d{8}$)/g, strValue );
}

//否邮政编码
function isPostalCode( strValue )
{
	return executeExp( /(^$)|(^\d{6}$)/gi, strValue )
}

//是否为合法的URL
function isURL( strValue )
{
	if( isEmpty( strValue ) ) return true;
	var pattern = /^(http|https|ftp):\/\/(\w+\.)+[a-z]{2,3}(\/\w+)*(\/\w+\.\w+)*(\?\w+=\w*(&\w+=\w*)*)*/gi;
	return executeExp( pattern, strValue );
}

//检查字符的长度
function checkStrLength( 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;
}

//判断类型
function checkValid( obj, strDescription, strType)
{
	var strMsg = "";
	if(!document.all(obj))
	{
		strMsg = strDescription + " 对象不存在";
		window.alert(strMsg);
		return;
	}
	var strValue = document.all(obj).value.trim();
	
	switch( strType )
	{
		case "Date" :	// 日期
			if( !isDate( strValue ) ) 
				strMsg = '"' + strDescription + '" 请输入正确的日期格式\n';
			break;
				
		case "AlphaNumeric" :	// 字母数字
			if( !isAlphaNumeric( strValue ) )
				strMsg = '"' + strDescription + '" 请输入字母或数字！\n';
			break;
			
		case "NotEmpty" :	// 不许空值
			if( isEmpty( strValue ) )
				strMsg = '"' + strDescription + '" 不能为空！\n';
			break;
				
		case "Email" :	// 电子邮件
			if( !isEmail( strValue ) )
				strMsg = '"' + strDescription + '" 请输入正确的邮件格式\n';
			break;
				
		case "Money" :	//货币
			if( !isMoney( strValue )  )
				strMsg = '"' + strDescription + '" 请输入正确的货币格式\n';
			break;
					
		case "Numeric" :	//数字
			if( !isNumeric( strValue )  )
				strMsg = '"' + strDescription + '" 请输入数字！\n';
			break;
		
		case "Phone" :	// 电话
			if( !isPhone( strValue ) )
				strMsg = '"' + strDescription + '" 请输入正确的电话格式\n';
			break;
			
		case "PostalCode" :	// 邮政编码
			if( !isPostalCode( strValue ) )
				strMsg = '"' + strDescription + '" 请输入6位数字！\n';
			break;
			
		case "URL" :	// URL
			if( !isURL( strValue ) )
				strMsg = '"' + strDescription + '" 请输入正确的URL格式！\n';
			break;
				
		default :	// 其他
			strMsg = '错误的 "' + strDescription + '" 类型 "' + strType + '" 不能识别！\n';
			break;
	}
	
	return strMsg;
}


// 确认删除
function confirm_delete( url )
{
	if( confirm( "您确实要删除吗？" ) )
	{
		window.location = ( url )
	}
}
//
// 确认删除
function confirm_deleteAll( url )
{
	if( confirm( "您确定要清空日志吗？" ) )
	{
		window.location = ( url )
	}
}
// 链接转向
function goToURL( url )
{
	window.location = url;
}

//判断用户是否选择了要操作的记录，如果是
//则自动提交表单
//form:表单名称
//action:要进行的操作
//listName:多选field字称
function ListCheck(formName,action,listName,msg)
{
	var flag = 0;
	var form = document.all(formName);

	if (form==null)
		return false;

	if(action=='')
		return false;

	if(listName=='')
		return false;
	
	var field = document.all(listName);

	if(field.length == null)	//处理可能只有一条记录的Bug
	{
		if(field.checked == true)
		{
			flag=1;
		}		
	}
	else
	{
		for(i = 0; i < field.length; i++)
		{
			if (field[i].disabled != true)
			{
				if(field[i].checked == true)
				{
					flag=1;
				}
			}
		}
	}

	if(flag==1)
	{
		document.all("function").value = action;
		if (window.confirm("你确定要["+msg+"]吗？"))
		{
			form.submit();
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		alert("请选择记录！");
		return false;
	}

	return false;
}
function Confirm(msg)
{
	return window.confirm("你确定要" + msg + "吗？");
}

oncontextmenu="window.event.returnValue=false";