/*-------------------
客户端脚本容错处理
*	2001.12.13	添加客户端容错处理
*-------------------*/
//function NoErr(){
//	return(true);
//} 
//onerror=NoErr;


/*-------------------------------------------------------------
* 验证客户端输入数据
*
* 参考资料：
*	CheckInput.vbs
*	互联网
*	MSDN Regular Expression 
*
*
* 修改纪录：
*	2001.8.20	增加 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;
}
// Star SR Enland Date Validation 

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}
//Felix Added
function isEnglandDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	//orignal to verify date format mm/dd/yyyy, we need verify dd/mm/yyyy
	//var strMonth=dtStr.substring(0,pos1)
	//var strDay=dtStr.substring(pos1+1,pos2)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		//alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}

//End Engliand Date validate

// 验证是否 Email
function isEmail( strValue )
{
	// Email 必须是 x@a.b.c.d 等格式 或者为空
	if( isEmpty( strValue ) ) return true;
	
	//return checkExp( /^\w+@(\w+\.)+\w+$/gi, strValue );	//2001.12.24测试出错 检查 jxj-xxx@114online.com时不能通过
	//Modify By Tianjincat 2001.12.24
	var pattern = /^([a-zA-Z0-9_\.-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
	return checkExp( pattern, strValue );
	
}
//Felix isValidEmail

function isValidEmail(str) {

		
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		  // alert("Invalid E-mail ID")
		   return false

		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		  // alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   // alert("Invalid E-mail Address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   // alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   // alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   // alert("Invalid E-mail Address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		   // alert("Invalid E-mail Address")
		    return false
		 }

		
 		 return true					
	}

// 验证是否 为空
function isEmpty( strValue )
{
	if( strValue == "" )
		return true;
	else
		return false;
}

// 验证是否 数字
function isNumeric( strValue )
{
	// 数字必须是 0123456789 或者为空
	
	return checkExp( /^\d*$/g, strValue );
}

// 验证是否 货币
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.alpha.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;
}

// 检查输入数据长度的合法性（字符长度不能大于**个字符）
//
//	输入参数
//		strName		字段对象
//		strDescription	字段描述
//		strLength	字段长度
//
function ValidateMaxLength( strName, strDescription, strLength) {
	var strMsg = "";
	var strValue = document.all( strName ).value.trim();
	var strMaxLength = "L<" + strLength;
	if( !checkLength( strValue, strMaxLength ))
	strMsg = '"' + strDescription + '" must be less than'+ strLength + 'characters.\n';
	return strMsg;
}

// 检查输入数据长度的合法性（字符长度不能小于**个字符）
//
//	输入参数
//		strName		字段对象
//		strDescription	字段描述
//		strLength	字段长度
//
function ValidateMinLength( strName, strDescription, strLength) {
	var strMsg = "";
	var strValue = document.all( strName ).value.trim();
	var strMaxLength = "L>" + strLength;
	if( !checkLength( strValue, strMaxLength ))
	strMsg = '"' + strDescription + '" must have at least '+ (parseInt(strLength)+1) + ' characters.\n';
	return strMsg;
}

// 检查输入数据长度的合法性（字符长度等于**个字符）
//
//	输入参数
//		strName		字段对象
//		strDescription	字段描述
//		strLength	字段长度
//
function ValidateEquLength( strName, strDescription, strLength) {
	var strMsg = "";
	var strValue = document.all( strName ).value.trim();
	var strMaxLength = "L=" + strLength;
	if( !checkLength( strValue, strMaxLength ))
	strMsg = '"' + strDescription + '" must be '+ strLength + ' characters.\n';
	return strMsg;
}

// 检查输入数据的合法性（应用在离开字段时）
//
//	输入参数
//		obj		字段对象
//		strDescription	字段描述
//		strType	字段类型
//
function CheckValid( obj, strDescription, strType)
{
	var strMsg = "";
	var strValue = obj.value.trim();
	
	switch( strType )
	{
		case "AlphaNumeric" :	// 字母数字
			if( !isAlphaNumeric( strValue ) )
				strMsg = '"' + strDescription + '" must be letter or number\n';
			break;
			
		case "Date" :	// 日期
			if( !isDate( strValue ) ) 
				strMsg = '"' + strDescription + '" must be in correct format e.g. 20-19-2008\n';
			break;
				
		case "Email" :	// 电子邮件
			if( !isEmail( strValue ) )
				strMsg = '"' + strDescription + '" must be in correct email format e.g. xx@yy.com\n';
			break;
				
		case "NotEmpty" :	// 不许空值
			if( isEmpty( strValue ) )
				strMsg = '" + strDescription + " is a required field.\n';
			break;
				
		case "Numeric" :	//数字
			if( !isNumeric( strValue )  )
				strMsg = '"' + strDescription + '" must be number\n';
			break;
		
		case "Money" :	//货币
			if( !isMoney( strValue )  )
				strMsg = '"' + strDescription + '" must be in correct currency format e.g.-123,456.789\n';
			break;
					
		case "Phone" :	// 电话
			if( !isPhone( strValue ) )
				strMsg = '"' + strDescription + '" must be in correct format e.g (65)98547896\n';
			break;
			
		case "PostalCode" :	// 邮政编码
			if( !isPostalCode( strValue ) )
				strMsg = '"' + strDescription + '" must be 6 digitals\n';
			break;
			
		case "URL" :	// URL
			if( !isURL( strValue ) )
				strMsg = '"' + strDescription + '" must be in correct URL format e.g.\n';
			break;
				
		default :	// 其他
			if( arrType[i].charAt( 0 ) == 'L' )
			{
				if( !checkLength( strValue, arrType[i] ) )
					strMsg = '"' + strDescription + '" should be the length of' + arrType[i].substr(1) + '\n';
			}
			else
				strMsg = 'Error: "' + strDescription + '" format "' + strType + '" cannnot be recognized\n';
	}
	
	if( strMsg != "" ) 
	{
		window.alert( strMsg );
		obj.focus();
	}
	
	return;
}

// 验证输入数据的合法性
//
//	输入参数
//		值
//		strDescription	字段描述
//		strType	字段类型
//
//	输出参数
//		空串	通过验证
//		非空	未通过验证
//
function ValidateKernel(strValue,strDescription,strType)
{
	var strMsg = "";
	var arrType = strType.split( " " );
	
	for( var i = 0; i < arrType.length; i++ )
	switch( arrType[i] )
	{
		case "AlphaNumeric" :	// 字母数字
			if( !isAlphaNumeric( strValue ) )
				strMsg = '"' + strDescription + '" must be letter or number！\n';
			break;
		
		case "Date" :	// 日期
			if( !isEnglandDate( strValue ) ) 
				strMsg =strDescription + ' is invalid. The date format should be dd/mm/yyyy.\n';
			break;
			
		//case "Email" :	// 电子邮件
		//	if( !isEmail( strValue ) )
		//		strMsg = 'Invalid '+strDescription+'.\n';
		//	break;
		
		//updated by felix. Multiple email is not allowed	
		case "Email" :	// 电子邮件
			if( !isValidEmail( strValue ) )
				strMsg = 'Invalid '+strDescription+'.\n';
			break;
			
		case "NotEmpty" :	// 不许空值
			if( isEmpty( strValue ) )
				strMsg =  strDescription + ' is required.\n';
			break;
			
		case "Numeric" :	//数字
			if( !isNumeric( strValue )  )
				strMsg =strDescription + 'must be a number.\n';
			break;
			
		case "Money" :	//货币
			if( !isMoney( strValue )  )
				strMsg = '"' + strDescription + '" must be correct currency formt e.g. -123,456.789\n';
			break;
				
		case "Phone" :	// 电话
			if( !isPhone( strValue ) )
				strMsg = '"' + strDescription + '" must be correct format e.g. (65)98874896\n';
			break;
		
		case "PostalCode" :	// 邮政编码
			if( !isPostalCode( strValue ) )
				strMsg = '"' + strDescription + '" at least 6 digitals.\n';
			break;
			
		case "URL" :	// URL
			if( !isURL( strValue ) )
				strMsg = '"' + strDescription + '" is an invalid URL address！\n';
			break;
			
		default :	// 其他
			if( arrType[i].charAt( 0 ) == 'L' )
			{
				if( !checkLength( strValue, arrType[i] ) )
					strMsg = '"' + strDescription + '" should be in length of ' + arrType[i].substr(1) + '\n';
			}
			else
				strMsg = 'Error:"' + strDescription + '" format "' + strType + '" cannot be recognized\n';
	}

	return strMsg;
}
// 验证输入数据的合法性
//
//	输入参数
//		strName	字段名
//		strDescription	字段描述
//		strType	字段类型
//
//	输出参数
//		空串	通过验证
//		非空	未通过验证
//
function Validate( strName, strDescription, strType)
{
	var strMsg = "";
	var strValue = document.all( strName ).value.trim();

	strMsg = ValidateKernel(strValue,strDescription,strType);

	return strMsg;
}
// 验证输入数据的合法性
//
//	输入参数
//		fldVal 值
//		strDescription	字段描述
//		strType	字段类型
//
//	输出参数
//		空串	通过验证
//		非空	未通过验证
//
function ValidateFld( fldVal, strDescription, strType)
{
	var strMsg = "";
	var strValue = fldVal;

	strMsg = ValidateKernel(strValue,strDescription,strType);

	return strMsg;
}


// 确认删除
function confirm_delete( url )
{
	if( confirm( "Are you sure to delete?" ) )
	{
		window.location = ( url )
	}
}

// 链接转向
function goToURL( url )
{
	window.location = url;
}

// 打开新窗口
function openNewWin( url, width, height )
{
	var newwin = window.open( url, "NewWin", "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=yes,width=" + width + ",height=" + height +"" );
	newwin.focus();
	return false;
}

// 对话框窗口
function openDialog( url, width, height)
{	
	showModalDialog( url, "NewWin","dialogWidth:"+ width +";dialogHeight:"+ height +";dialogTop:100;dialogLeft:200;status:no;");
}

//检查CheckBox列表是否存在被选择项
function hasCheckBoxToSelected(field){
	var isSelected = false;
	if(field.length != null){
		for(i=0;i<field.length;i++){
			if(field[i].checked == true){
				isSelected = true;
				break;
			}
		}
	}else{
		if(field.checked == true){
			isSelected = true;
		}
	}
	return isSelected; 
}

//功能：checkBox的全选和全不选
var checkboxflag = "false";
function check(field) 
{
	if (checkboxflag == "false") 
	{
		if(field.length == null)	//处理可能只有一条记录的Bug
		{
			if (field.disabled != true)
			{
				field.checked = true;
			}
		}
		else
		{
			for (i = 0; i < field.length; i++) 
			{
				if (field[i].disabled != true)	//如果是disabled则不修改萁状态 Modify By tianjincat 2002-03-26
				{
					field[i].checked = true;
				}
			}
		}
		checkboxflag = "true";
		return "Cancel"; 
	}
	else 
	{
		if(field.length == null)	//处理可能只有一条记录的Bug
		{
			if (field.disabled != true)
			{
				field.checked = false;
			}
		}
		else
		{
			for (i = 0; i < field.length; i++) 
			{
				if (field[i].disabled != true)	//如果是disabled则不修改萁状态 Modify By tianjincat 2002-03-26
				{
					field[i].checked = false; 
				}	
			}
		}
		checkboxflag = "false";
		return "Select All"; 
	}
}

//功能：checkBox的反选择
//Added By tianjincat 2002-04-01
function chkinverse(field)
{
	if(field.length == null)	//处理可能只有一条记录的Bug
	{
		if (field.disabled != true)
		{
			if(field.checked == true)
			{
				field.checked = false;
			}
			else
			{
				field.checked = true;
			}
		}	
	}
	else
	{
		
		for(i = 0; i < field.length; i++)
		{
			if (field[i].disabled != true)
			{
				if(field[i].checked == true)
				{
					field[i].checked = false;
				}
				else
				{
					field[i].checked = true;
				}	
			}
		}
	}	
	return "Select Inverse"
}

//选择记录提示
//form 提交的FORM名称	msg  提示信息	field CheckBox的名称
//
function ActionConfirm(form,msg,field) 
{
	var flag=0;
	var truthBeTold;

	if(field==null)
		return;

	for(i = 0; i < field.length; i++)
		{
			if (field[i].disabled != true)
			{
				if(field[i].checked == true)
				{
					flag=1;
				}
			}
		}

	if(field.length == null)	//处理可能只有一条记录的Bug
	{
		if(field.checked == true)
		{
			flag=1;
		}		
	}

	if (flag==0)
	{
		alert("Please select a record");
		if(typeof(form.actionId) != "undefined")
			form.actionId.value = "";
	}
	else
	{
		truthBeTold =window.confirm("Are you sure to ["+msg+"]?");
		if (truthBeTold) {
			//form.DoType.value=msg;
			form.submit();
			flag = 1;
		} 
		else
		{
			if(typeof(form.actionId) != "undefined")
				form.actionId.value = "";
		}
	}

}