String.prototype.ltrim=function(){return this.replace(/(^\s+)/g,"")}
String.prototype.rtrim=function(){return this.replace(/\s+$/g,"")}
String.prototype.trim=function(){return this.replace(/(^\s+)|\s+$/g,"")}

var Page_Validator_Old;//存储原始页面校验值
<!--//将日期型字符串转为日期型变量-->
function ConvertStrToDate(_dateStr)
{
	var datePat = /^(\d{4})-(\d{1,2})-(\d{1,2})/; // Like '2005-10-9' patten; matchArray[1]~3 => year,month,day
	var matchArray = _dateStr.match(datePat); //date is the format ok?
	if (matchArray == null) 
	{
		//alert(_dateStr + " Date is not in a valid format.")
		return false;
	}
	_year = matchArray[1]; // parse date into variables
	_month = matchArray[2]-1; //month {0..11}
	_day = matchArray[3];

	var timePat = /(\d{1,2}):(\d{1,2})/;  // Like '18:20' patten; matchArray[1]~2 => hour,minute
	matchArray = _dateStr.match(timePat); //time is the format ok?
	if (matchArray == null) 
	{
		_hour=0;
		_minute=0;
	}
	else{
		_hour = matchArray[1]; // parse date into variables
		_minute = matchArray[2];
	}
	return new Date(_year,_month,_day,_hour,_minute);
}
//文本框中的数值以逗隔开的形式显示
function floatToStr(nStr)
{
	if(isNaN(nStr))
		return '0.00';
		
	nStr=nStr.toFixed(2);//precition
	nStr += '';//to string
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length>1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function floatToStr3(nStr)
{
	if(isNaN(nStr))
		return '0.000';
		
	nStr=nStr.toFixed(3);//precition
	nStr += '';//to string
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length>1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

<!--//将去掉数值中的逗号便于计算-->
function strToFloat(_strIn)
			{
				var _floatOut=0;
				while(_strIn.indexOf(',')>-1)
					_strIn=_strIn.replace(',','');

				_floatOut=parseFloat(_strIn);
				if(isNaN(_floatOut))
					_floatOut=0;

				return _floatOut;
			}
<!--//将日期型变量转换为标准字符串-->
function FormatDateToStr(_startTime)
{
	var _strDate = _startTime.getYear();
	_strDate =_strDate+'-'+(_startTime.getMonth()>8?_startTime.getMonth()+1:'0'+(_startTime.getMonth()+1));
	_strDate =_strDate+'-'+(_startTime.getDate()>9?_startTime.getDate():'0'+_startTime.getDate());
	_strDate =_strDate+' '+(_startTime.getHours()>9?_startTime.getHours():'0'+_startTime.getHours());
	_strDate =_strDate+':'+(_startTime.getMinutes()>9?_startTime.getMinutes():'0'+_startTime.getMinutes());
	return _strDate;
}

<!--//resetByPrefix : 对于给定前缀的所有Form1内元素置回默认值-->
function resetByPrefix(_prefix)
{
	var objField;
	//var srcs='';
	for(var i=0;i<document.forms[0].elements.length;i++)
	{
		objField = document.forms[0].elements[i];
		if(objField.id.indexOf(_prefix)==0)
		{
			//srcs += objField.id+'\t'+objField.type +'\n';
			if(objField.type == 'text')
				objField.value = '';
			else
				if(objField.type == 'select-one')
					objField.selectedIndex = 0;
				else
					if(objField.type == 'checkbox')
						objField.checked = false;
		}
	}
	//alert(srcs);
}

function OpenModalDialog(_url,_args,_width,_height)
{		
	if(_url!="")
	{	
		strFeatures = "dialogWidth:"+_width+"px;dialogHeight:"+_height+"px;resizable:yes;status:no;help:no;"
		var ReturnValue=showModalDialog(_url,_args,strFeatures);
		return ReturnValue;
	}
}

function OpenModelessDialog(_url,_args,_width,_height)
{		
	if(_url!="")
	{	
		strFeatures = "dialogWidth:"+_width+"px;dialogHeight:"+_height+"px;resizable:yes;status:no;help:no;"
		var ReturnValue=showModelessDialog(_url,_args,strFeatures);
		return ReturnValue;
	}
}

<!--//windowOpen: 根据URL,居中打开窗口-->
function windowOpen(_url,_width,_height)
{
	var winFeature='toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes';
		winFeature += ',width='+_width  +  ',height='+_height;
		winFeature += ',left='+(window.screen.availWidth-_width)/2  +  ',top='+(window.screen.availHeight-_height)/2;
	window.open(_url,'',winFeature,'');
}
<!--//windowOpen: 根据URL,居中打开窗口-->
function windowOpenById(_url,_width,_height)
{
	var _id =selectedID();
	if(_id != -1)
	{
		windowOpen(_url+'?id='+_id,_width,_height)
	}
}
<!--//windowOpenAsFull: 根据URL,居中打开窗口-->
function windowOpenAsFull(_url)
{
	windowOpen(_url,window.screen.availWidth,window.screen.availHeight);
}
<!--//getClientMsg:从txtMSG中取得对应消息内容-->
function getClientMsg(_msgName)
{
	if(document.all.txtMSG != undefined && document.all.txtMSG.value !='') //是否存储了消息内容?
	{
		var msgArray = document.all.txtMSG.value.split('^');
		for(var i=0;i<msgArray.length;i++)
		{
			if(msgArray[i].indexOf(_msgName) > -1)
				return msgArray[i].substring(msgArray[i].indexOf('|')+1,msgArray[i].length);
		}
	}
	return "Something error occured!";
}
<!--//hasSelected: 是否有选定id-->
function selectedID(_cellInId)
{
	if(_cellInId==undefined)
		_cellInId=0;
		
	if(document.all.dgrdList ==undefined)
	{
		window.alert(getClientMsg('CLTMSG_NO_SELECTED_ROW'));
		return -1;
	}
	
	for(var i=0;i<document.all.dgrdList.rows.length;i++)
	{
		if(document.all.dgrdList.rows[i].className == 'gridSelected')
			return document.all.dgrdList.rows[i].cells[_cellInId].innerText;
	}
	window.alert(getClientMsg('CLTMSG_NO_SELECTED_ROW'));
	return -1;
}

<!--//DoSelect: 点击选择按钮-->
function DoSelect() 
{
	for(var i=0;i<document.all.dgrdList.rows.length;i++)
	{
		if(document.all.dgrdList.rows[i].className == 'gridSelected')
			document.all.dgrdList.rows[i].className = 'gridItem'
	}
	window.event.srcElement.parentElement.parentElement.className = 'gridSelected';
}
<!--//DoDelete: 点击删除按钮的时,执行删除判断 ;isDirectPost 是否直接将id提交服务器?-->
function DoDelete(isDirectPost,isNotInGrid) 
{
	if(isNotInGrid==undefined)
		if(selectedID() == -1)
			return false;
	if (window.confirm(getClientMsg('CLTMSG_BE_DELETE_ROW')))
	{
		if(isDirectPost)
			document.location.href = document.location.href.substring(0,document.location.href.indexOf('.aspx'))+'.aspx?id='+selectedID();
		return true;
	}
	else
		return false;
}
<!--//DoSave, 保存之前的判断操作-->
function DoSave()
{
	if(document.all.txtID != undefined && document.all.txtID.value !='') //是否存储了主ID?
		return true;
	else
	{
		alert(getClientMsg('CLTMSG_NO_MASTER_DATA'));
		return false;
	}
}

<!--//DoValidate, 根据控件名前缀,校验-->
function DoValidate(preToValidate,ctlNoNull)
{
	if(ctlNoNull != undefined)
	{
		for(var j=0;j<ctlNoNull.length;j++)
			if(ctlNoNull[j] != undefined && ctlNoNull[j].value =='')
			{
				alert(getClientMsg('CLTMSG_NO_MASTER_DATA'));
				Page_Validators=undefined;
				return false;
			}
	}
	
	if(Page_Validator_Old == undefined )
		 Page_Validator_Old = Page_Validators;
	else
		Page_Validators = Page_Validator_Old;
		
	var New_Validators = new Array();
	for(var i=0; i<Page_Validators.length; i++)
	{
		if(Page_Validators[i].id.indexOf(preToValidate)>-1 || Page_Validators[i].id.indexOf('val_')>-1)
		{
			New_Validators.push(Page_Validators[i]);
		}
	}
	Page_Validators = New_Validators;
	return true;
}
<!--////传入Id, 变更其可见性,包括下列2个函数, 请在</Body>前加入loadVisibility(); 已加载////-->
function chgVisibility(_id)
{
	if(document.all[_id] != undefined)
	{
		_oldStyle = document.all[_id].style.display;
		_newStyle = (_oldStyle=='none')?'block':'none';
		
		if(document.all.txtVisibility!= undefined && document.all[_id] != undefined)//保存?
		{
			_txtVisibility = document.all.txtVisibility;
			
			
			if(_txtVisibility.value.indexOf(_id+'|'+_oldStyle+'^')>-1)//已保存? 替换
			{
				_txtVisibility.value=_txtVisibility.value.replace(_id+'|'+_oldStyle+'^',_id+'|'+_newStyle+'^');
			}
			else //新增
			{
				_txtVisibility.value+=_id+'|'+_newStyle+'^';
			}
		}
		document.all[_id].style.display=_newStyle;// 设置新样式
	}
}
//刷新页面时,重新加载
function loadVisibility()
{
	if(document.all.txtVisibility != undefined && document.all.txtVisibility.value != '') //是否存储了消息内容?
	{
		var msgArray = document.all.txtVisibility.value.split('^');
		for(var i=0;i<msgArray.length;i++)
		{
			_id=msgArray[i].substring(0,msgArray[i].indexOf('|'));
			_dis=msgArray[i].substring(msgArray[i].indexOf('|')+1,msgArray[i].length);
			if(document.all[_id] != undefined)
				document.all[_id].style.display=_dis;
		}
	}
}

function dyniframesize(iframename,srcname) 
{
	var objIFrame = null;
	if (document.getElementById)
		objIFrame = document.getElementById(iframename);
	else
		eval('objIFrame = ' + iframename + ';');

	if (objIFrame && objIFrame.src.indexOf(srcname)>-1)
	{
		objIFrame.style.display="block"
		if (objIFrame.contentDocument && objIFrame.contentDocument.body.offsetHeight)
			objIFrame.height = objIFrame.contentDocument.body.offsetHeight+FFextraHeight; //ns6 syntax
		else if (objIFrame.Document && objIFrame.Document.body.scrollHeight)
			objIFrame.height = objIFrame.Document.body.scrollHeight;//ie5+ syntax
	}
}


<!--//refreshParent, 刷新父页面,以同步数据-->
function refreshParent()
{
	_opener=window.top.opener;
	if(!_opener || _opener.closed) return;//parent closed?
    
    _opener.document.forms[0].submit();
}
<!--//////////////////////DataGrid 内编辑处理脚本//////////////////-->
//取得Grid内控件:grid如document.all['datagrid1'],ctlId如 'drp_Vessel'
function getCtlInGrid(grid,ctlId)
{
	for(var i=0;i<grid.rows.length;i++)
	{
		if(grid.rows[i].className == 'gridEdit')
		{
			for(var j=0;j<grid.rows[i].cells.length;j++)
			{
				for(var k=0;k<grid.rows[i].cells[j].childNodes.length;k++)
				{
					if(grid.rows[i].cells[j].childNodes[k].id != undefined 
						&& grid.rows[i].cells[j].childNodes[k].id.indexOf(ctlId)>-1)
						return grid.rows[i].cells[j].childNodes[k];
				}
			}
		}
	}
}
//当前编辑id
function getEditId(grid)
{
	for(var i=0;i<grid.rows.length;i++)
	{
		if(grid.rows[i].className == 'gridEdit')
			return grid.rows[i].cells[0].innerText;
	}
	return -1;
}
function DoDel(grid)
{
	if(getEditId(grid) == ' ')//新增的行
	{
		alert(getClientMsg('CLTMSG_BE_SAVE_FIRST'));
		return false;
	}
	if(getEditId(grid)!=-1)
		return window.confirm(getClientMsg('CLTMSG_BE_DELETE_ROW'));
	else
		alert(getClientMsg('CLTMSG_NO_SELECTED_ROW'));
	return false;
		
}

function DoUndo(grid)
{
	if(getEditId(grid) ==-1)
		return false;
		
	return true;
}

function PushValidate(_ctlId)
{
	if(document.all[_ctlId] == undefined)//no this control?
		return;
	if(Page_Validator_Old != undefined)
	{	
		for(var i=0; i<Page_Validators.length; i++)
		{
			if(Page_Validators[i].id.indexOf(_ctlId)>-1)//already exist?
				return;
		}
		Page_Validators.push(document.all[_ctlId]);//not exist , add!
	}
	////////////////////Page_Validator_Old/////////////////////////
	if(Page_Validator_Old != undefined)
	{
		for(var i=0; i<Page_Validator_Old.length; i++)
		{
			if(Page_Validator_Old[i].id.indexOf(_ctlId)>-1)//already exist?
				return;
		}
		Page_Validator_Old.push(document.all[_ctlId]);//not exist , add!
	}
	
}
function PopValidate(_ctlId)
{
	if(Page_Validators != undefined)
	{
		for(var i=0; i<Page_Validators.length; i++)
		{
			if(Page_Validators[i].id.indexOf(_ctlId)>-1)//exist?
			{
				Page_Validators.splice(i,1); //delete it
				break;
			}
		}
	}
	////////////////////Page_Validator_Old/////////////////////////
	if(Page_Validator_Old != undefined)
	{
		for(var i=0; i<Page_Validator_Old.length; i++)
		{
			if(Page_Validator_Old[i].id.indexOf(_ctlId)>-1)//exist?
			{
				Page_Validator_Old.splice(i,1); //delete it
				break;
			}
		}
	}
}
<!--//ChkValidate, 根据控件名前缀,校验-->
function ChkValidate(preToValidate,isMaster)
{
	if(isMaster==undefined || !isMaster)//非主数据,必须校验是否有主ID
		if(document.all.txtID != undefined && document.all.txtID.value=='') //是否存储了主ID?
		{
			alert(getClientMsg('CLTMSG_NO_MASTER_DATA'));
			return false;
		}
	//是否自动控件校验? ,是?!则是否有子控件? 有?!则未选中行
	if(document.all['div2_'+preToValidate] != undefined && document.all['div2_'+preToValidate].childNodes.length>0) 
	{
		alert(getClientMsg('CLTMSG_NO_SELECTED_ROW'));
		return false;
	}

	if(Page_Validator_Old == undefined )
		 Page_Validator_Old = Page_Validators;
	else
		Page_Validators = Page_Validator_Old;
		
	var New_Validators = new Array();
	for(var i=0; i<Page_Validators.length; i++)
	{
	
		if(Page_Validators[i].id.indexOf(preToValidate)>-1 || Page_Validators[i].id.indexOf('val_')>-1)
		{
			//手动校验?,如果被校验的控件仍在div中,则没有选择行
			if(Page_Validators[i].parentElement != undefined && Page_Validators[i].parentElement.id.indexOf('div_')>-1)
			{
				alert(getClientMsg('CLTMSG_NO_SELECTED_ROW'));
				return false;
			}
			New_Validators.push(Page_Validators[i]);
		}
	}
	Page_Validators = New_Validators;
	
	if (typeof(Page_ClientValidate) != 'function' ||  Page_ClientValidate()) 
		return true;
	else
		return false;
}
	
///////////////////////////页面控件值变更追踪///////////////////////////////////////////////////
	var isFormChanged=false;isGridChanged=false;//bool
	function DontLostChanges(_target)
	{
		var isTargetChanged=false;
		switch(_target)
		{
			case undefined:
				isTargetChanged=(isFormChanged || isGridChanged);
				break;
			case 'FORM':
				isTargetChanged=isFormChanged;
				break;
			case 'GRID':
				isTargetChanged=isGridChanged;
				break;
		}
		if(isTargetChanged)
		{
			if(window.confirm(getClientMsg('CLTMSG_BE_LOST_CHANGE')))
			{
				return true;
			}
			else
				return false;
		}
		else
			return true;
	}
	function DoOnClose()
	{
		isTargetChanged=(isFormChanged || isGridChanged);
		if(isTargetChanged && (window.event.clientX<0 || window.event.clientY<0))
			window.event.returnValue = getClientMsg('CLTMSG_BE_LOST_CHANGE');
		return;
	}
	function DoOnChange()
	{
		if(window.event.srcElement.id!=undefined && (window.event.srcElement.id.indexOf('_ctl')>0 || window.event.srcElement.id.indexOf('2_')>0))
			isGridChanged=true;
		else
			isFormChanged=true;
		switch(window.event.srcElement.type)
		{
			case 'text':
			case 'textarea':
				window.event.srcElement.style.border='1 solid #ff9933';
				break;
			case 'select-one':
				window.event.srcElement.style.background='#f0e68c';
				break;
			case 'checkbox':
				window.event.srcElement.style.border='1 solid #ff9933';
				break;
		}
	}
	function InsertChangeEvent(srcElem,newfunc)
	{
		if(srcElem.type== 'text' || srcElem.type== 'select-one' || srcElem.type== 'textarea' || srcElem.type== 'checkbox')
		{
			var oldfunc = srcElem.onchange;
			if (typeof(oldfunc) != 'function') 
			{
				srcElem.onchange = newfunc;
			}
			else 
			{
				srcElem.onchange = function() 
				{
					newfunc();
					oldfunc();
				}
			}
		}
	}
	function AttachOnClickEvent(srcElem,newfunc)
	{
		if(srcElem.onclick!=null)
			var oldfunc=srcElem.onclick.toString();
		else
			var oldfunc="";
		srcElem.onclick=new Function(newfunc+oldfunc.substring(oldfunc.indexOf('{')+1,oldfunc.lastIndexOf('}')));
	}
	function SetFormsOnChangeEvent()
	{
		var objForm=document.forms[0];
		for(var i=0;i<objForm.length;i++)
		{
			if(objForm[i].id !=undefined && objForm[i].id!='' && objForm[i].id.indexOf('p_')!=0)
			{
				
				InsertChangeEvent(objForm[i],DoOnChange);
			}
		}
	}
	//////////////////////////////////////////////////////////////////////////////

