/***********************************************************************************/
/****   GRID控件前台JS类   ***********************************************************/
/****   主要功能：插入，删除，隐藏一行；加亮显示一行；键盘移动焦点；拖动单元格宽度,下拉框列,日期列等×××××××××××/
/*************************************************************************************/
//   类名：EDITGRID
function EDITGRID()
{
//私有变量
var divAll=null;		//外面的总的DIV
var divTou=null;		//表头的DIV
var divTi=null;			//表体的DIV
var divSum=null;		//合计的DIV
var divTiLeftCol=null;	//表体左侧固定列
var divTouLeftCol=null;	//表头左侧固定列
var divSumLeftCol=null;	//合计左侧固定列
var inputRowCount=null;	//标识行数的隐藏域
var tabTou=null;		//表头的TABLE
var tabTi=null;			//表体的TABLE	
var tabSum=null;		//合计的TABLE
var oldFTr=null;			//第一行的拷贝
var curDivHeight="";	//当前表格体的高度	
var curTrHeight="";		//当前表格体行的高度	
var currentLine=null;	//当前操作的行
var isMouDown=false;	//标识鼠标按下的状态
var OldPlace=null;		//原来的位置
var nowPlace=null;		//新位置
var curTd=null;			//当前操作的TD,调整列宽
var curHr=null;			//当前GRID的竖线
var primaryKeyCol=-1;	//主键列
var deleteCol=-1;	//隐藏列
var indexCol=-1;	//序号列
var targetFormat="yyyy-MM-dd HH:mm";//转换的日期格式

//*******************************
//通用变量
//*******************************
var divDateAll=null;	//日历的总的DIV
var divDate=null;//显示日期的DIV
var divTime=null;//显示时间的DIV
var curDateTd=null;// 上次点击日历的TD
var dDate = new Date();
var month= dDate.getMonth()+1;
var year= dDate.getFullYear();
var day=dDate.getDate();
var hour=dDate.getHours();
var minute=dDate.getMinutes();
var oldday=null;//初始值的day,为白色单元格
var inputtextid=null;
var inputtext=null;
var innerhtml="";
var middle="-";
//微调
var date_interv = null;
var date_interv_ob = null;
var date_currob = null;

//公共变量
this.name=null;			//GRID的名称


//方法
//初始化,参数列表为外面DIV的ID,序号列，主键列，隐藏列，单元格的单击事件，双击事件
this.init=function(odivid,Index,primaryKey,Delete,clickEvent,dblclickEvent)
{
var screenWidth=document.body.clientWidth;//页面宽度
var divW=parseInt(screenWidth)*0.9;
divAll=document.all(odivid);
if(divAll!=null && divAll.tagName=="DIV")
	{
	divTou=divAll.children[0].children[0];
	divTi=divAll.children[1];
	divSum=divAll.children[2].children[0];
	divTiLeftCol=divAll.children[4];
	divTouLeftCol=divAll.children[0].children[1];
	divSumLeftCol=divAll.children[2].children[1];
	inputRowCount=divAll.children[3];
	divDateAll=divAll.children[5];
	//总数设位0,隐藏的行不计算
	//if(inputRowCount!=null)
		//inputRowCount.value=0; 
	curDivHeight=divTi.style.height;
	if(divTou!=null)
		tabTou=divTou.children[0];
	if(divTi!=null)
		tabTi=divTi.children[0];
	if(divSum!=null)
		tabSum=divSum.children[0];
	//总数设位0,隐藏的行不计算
	//if(inputRowCount!=null)
		//inputRowCount.value=0;
	curTrHeight=tabTi.rows[0].clientHeight;//当前表格体行的高度
	oldFTr=tabTi.rows[0];
	//插入一行
	//this.addLine();
	//主键
	if(primaryKey!="" && primaryKey!=null)
		primaryKeyCol=primaryKey;
	//隐藏列
	if(Delete!="" && Delete!=null)
		deleteCol=Delete;
	if(Index!="" && Index!=null)
	indexCol=Index;
	hidDelCol(deleteCol);
	//为divSum定位
	divSum.parentElement.style.top=parseInt(divTi.offsetTop)+parseInt(divTi.clientHeight)-(parseInt(divSum.clientHeight))+1;

	//由于出现了滚动条,重新给宽度赋值
		divTiLeftCol.style.paddingTop="21px";
		divTiLeftCol.style.height=divTi.clientHeight-20;
		divTou.style.width=divTi.clientWidth;
		divSum.style.width=divTi.clientWidth;
	//为页面添加一个竖线
	//this.addHr();
	//添加事件
	tabTi.attachEvent("onmousedown",this.doclick);
	tabTi.attachEvent("onkeydown",this.dokeydownTab);
	divTi.attachEvent("onscroll",this.doscroll);
	divTou.attachEvent("onmousedown",this.domousedown);
	divTou.attachEvent("onmouseup",this.domouseup);
	divTou.attachEvent("onmousemove",this.domousemove);
	divTou.attachEvent("onmouseout",this.domouseout);
	//tabTou.attachEvent("onmousedown",this.domousedown);
	//tabTou.attachEvent("onmouseup",this.domouseup);
	//tabTou.attachEvent("onmousemove",this.domousemove);
	//tabTou.attachEvent("onmouseout",this.domouseout);
	divTou.attachEvent("onselectstart",this.domouseselect);
	divAll.attachEvent("onresize",this.doresize);
	if(clickEvent!="" && clickEvent!=null)
	tabTi.attachEvent("onclick",clickEvent);
	if(dblclickEvent!="" && dblclickEvent!=null)
	tabTi.attachEvent("ondblclick",dblclickEvent);
//add by yao

	CountSum();
	rename();
	}
}

//获取Table对象
this.getTabTi=getTabTi;
function getTabTi(){
return tabTi;
}
//为页面添加一个竖线，调整列用
this.addHr=addHr;
function addHr()
	{
	var ohr=document.createElement("hr");
	ohr.width=1;
	ohr.size=20;
	ohr.color="black";
	ohr.style.position="absolute";
	ohr.style.display="none";
	document.body.appendChild(ohr);
	curHr=ohr;
	}
//隐藏标识为deleteCol得一列
this.hidDelCol=hidDelCol;
function hidDelCol(col)
	{
	if(col=="" || col==-1)
		return;
		if(tabTi==null&&tabTi.tagName!="TABLE") return;
	for(var i=0;i<tabTi.rows.length;i++)
		{
		var otd=tabTi.rows[i].cells[col];
		if(otd!=null) 
			otd.style.display="none";
		}

	}
//把一行中deleteCol列得文本框得值变为1
this.changeDelValue=changeDelValue;
function changeDelValue(otr)
	{
	if(tabTi==null&&tabTi.tagName!="TABLE") return;
	if(otr==""||otr==null||otr.tagName!="TR")
		return;
	if(deleteCol==-1)
		return;
	try
		{
		otr.cells[deleteCol].children[0].value=1;
		}
		catch(ex)
		{
		alert("错误！");
		}

	}
//为表体增加一行

this.addLine=addLine;
function addLine()
	{
	if(tabTi==null&&tabTi.tagName!="TABLE") return;
	var tr=tabTi.rows[0];
	//alert(tr.style.display)
	//tr.style.display = "block";
	if(tr!=null)
		{
		try
			{
			tabTi.rows[0].style.display="block";
			var ntr=tabTi.insertRow();
			for(var i=0;i<tabTi.rows[0].cells.length;i++)
				{
				var otd=ntr.insertCell();
				otd.innerHTML=tabTi.rows[0].cells[i].innerHTML;
				otd.width=tabTi.rows[0].cells[i].width;
				otd.index=tabTi.rows[0].cells[i].index;
				otd.style.display=tabTi.rows[0].cells[i].style.display;
				}
			if(currentLine!=null)
				delightLine(currentLine);
			//currentLine=ntr;
			currentLine=tabTi.rows[tabTi.rows.length-1];
			latd2=tabTi.rows[tabTi.rows.length-1];//倒算第二行
			lightLine(latd2);
			//tabTi.rows[0].setAttribute("IsHidden","1");
			if(inputRowCount!=null)
				{
				
			inputRowCount.value=parseInt(inputRowCount.value)+1;
			if(returnRowCount()!=inputRowCount.value)
						inputRowCount.value=returnRowCount();
				}
			rename();//改名
			//为左边固定列增加一行
			if(divTiLeftCol!=null && divTiLeftCol.children[0]!=null)
				{
				var tableft=divTiLeftCol.children[0];
				var row=tableft.insertRow();
				var otd=row.insertCell();
				var rowindex=row.rowIndex;
				otd.innerHTML="<span>"+(rowindex+1)+"</span>";
				}
			divTi.scrollTop=divTi.scrollHeight;
			//重新计算divSum的top
			//divSum.style.top=parseInt(divTi.offsetTop)+parseInt(divTi.offsetHeight)-(parseInt(divTi.style.height)-divTi.clientHeight)-curTrHeight;
			}
			catch(ex)
			{
				alert("增加一行失败！"+ex.description);
				return;
			}
		}
	}
//为表体增加高度
this.addHeight=addHeight;
function addHeight()
	{
	if(tabTi==null&&tabTi.tagName!="TABLE") return;
	try
		{
		//divTi.style.height=parseInt(curDivHeight)+parseInt(curTrHeight)+"px";
		curDivHeight=parseInt(curDivHeight)+parseInt(curTrHeight);
		}
		catch(ex)
		{
			alert("增加高度失败！");
			return;
		}
	}

//为表体增加高度
this.lowHeight=lowHeight;
function lowHeight()
	{
	if(tabTi==null&&tabTi.tagName!="TABLE") return;
	
	}

//为表体删除一行
this.delLine=function()
	{
		if(tabTi==null&&tabTi.tagName!="TABLE") return;
		try
			{
			if(currentLine!=null)
				{//alert(currentLine);
				var oldTr=currentLine.rowIndex;
				var isdel=currentLine.removeNode(true);
				var otr=this.findCurrentLine(oldTr);
					//var isdel=currentLine.removeNode(true);
				//var isdel=otr.removeNode(true);
				currentLine=otr;
				if(inputRowCount!=null)
					{
					
				inputRowCount.value=parseInt(inputRowCount.value)-1;
				if(returnRowCount()!=inputRowCount.value)
						inputRowCount.value=returnRowCount();
					}
				rename();//改名
				//为左边固定列删除一行
				if(divTiLeftCol!=null && divTiLeftCol.children[0]!=null)
				{
				var tableft=divTiLeftCol.children[0];
				if(tableft.rows.length>0)
				tableft.rows[tableft.rows.length-1].removeNode(true);
				}
				divTou.style.width=divTi.clientWidth;
				divSum.style.width=divTi.clientWidth;
				}
			}
			catch(ex)
			{
				alert("删除一行失败2！");
				return;
			}
	}
//为表体隐藏一行
this.hideLine=function(ctr)
	{
	if(tabTi==null&&tabTi.tagName!="TABLE") return;
	try
		{
		if(ctr==null)
			ctr=currentLine;
		ctr.style.display="none";
		changeDelValue(ctr);
		this.delightLine(ctr);
		var otr=this.findCurrentLine(ctr.rowIndex);
		currentLine=otr;

		//为左边固定列删除一行
				if(divTiLeftCol!=null && divTiLeftCol.children[0]!=null)
				{
				var tableft=divTiLeftCol.children[0];
				tableft.rows[tableft.rows.length-1].removeNode(true);
				}
				divTou.style.width=divTi.clientWidth;
				divSum.style.width=divTi.clientWidth;
		}
		catch(ex)
		{
			alert("隐藏一行失败！"+ex.description);
			return;
		}
	}
//删除一行,隐藏一行后查找当前行,返回TR
this.findCurrentLine=function(rowindex)
	{
	if(tabTi==null&&tabTi.tagName!="TABLE") return;
	if(rowindex!=null)
		{
		try
			{
					var ntr=null;
					if(rowindex>0)
						{
						for(var i=rowindex-1;i>0;i--)
							{
							var otr=tabTi.rows[i];
							if(otr!=null && otr.style.display!="none" )
								{
								ntr=otr;
								break;
								}
							}
						}
			if(ntr==null)
				{
				for(var i=1;i<tabTi.rows.length;i++)
							{
							var otr=tabTi.rows[i];
							if(otr!=null && otr.style.display!="none" )
								{
								ntr=otr;
								break;
								}
							}
				}
				this.lightLine(ntr);
				return ntr;
			}
			catch(ex)
			{
				//alert("查找当前行失败！"+ex.description);
				return;
			}
		}
	}

//加亮显示一行,参数为要加亮的行
this.lightLine=lightLine;
function lightLine(otr)
	{
		if(otr.rowIndex==0 )
		return;
	if(otr!=null && otr.tagName=="TR")
		{
		try
			{
				otr.className="lightSkin";
			}
			catch(ex)
			{return;}
		}
	}

//还原加亮的行，参数为要恢复的行
this.delightLine=delightLine;
function delightLine(otr)
	{
	if(otr!=null && otr.tagName=="TR")
		{
		try
			{
				otr.className="";
			}
			catch(ex)
			{return;}
		}
	}

//单击事件，选择当前行
this.doclick=doclick;
function doclick()
	{
	var obj=window.event.srcElement;
	if(obj==null) return ;
	var oTr=null;
	var otd=null;
	switch (obj.tagName)
	{
	case "TR":
		otd=obj.children[0];
		oTr=obj;
		break;
	case "TD":
		oTr=obj.parentElement;
		otd=obj;
		break;
	case "INPUT":
		oTr=obj.parentElement.parentElement;
		otd=obj.parentElement;
		break;
	case "IMG":
		oTr=obj.parentElement.parentElement;
		otd=obj.parentElement;
		break;
	case "SELECT":
		oTr=obj.parentElement.parentElement.parentElement;
		otd=obj.parentElement.parentElement;
		break;
	default :
		return;
	}
	try
		{
	if(currentLine!=null)
		delightLine(currentLine);
	if(oTr!=null)
		lightLine(oTr);
	currentLine=oTr;
		}
		catch(ex)
		{
			alert(ex.description);
		}
	}
//键盘事件，回车键换行，上下键上下移动
this.dokeydownTab=function()
	{
	var obj=window.event.srcElement;
	if(obj==null) return;
	var otd=null;
	if(obj.tagName=="SELECT")
		otd=obj.parentElement.parentElement;
	else
		otd=obj.parentElement;

	if(otd==null|| otd.tagName!="TD")
		return false;
	var otr=otd.parentElement;
	if(otr==null|| otr.tagName!="TR")
		return false;
	var tab=otr.parentElement.parentElement;
	if(tab==null||tab.tagName!="TABLE")
		return;
	try
		{
		if(window.event.keyCode=="13")
			{
			movetToRight(obj);
			}
		if(window.event.keyCode==38 && tab==tabTi)
			moveFocus(obj,-1);
		if(window.event.keyCode==40 && tab==tabTi)
			moveFocus(obj,1);
		if(window.event.keyCode==37 && tab==tabTi)
			movetToLeft(obj)
		if(window.event.keyCode==39 && tab==tabTi)
			movetToRight(obj)

		}
		catch(ex)
		{}
	}

//内部方法，判断当前控件是否是最后行的最后一个
this.IsLast=IsLast;
function IsLast(obj)
	{
	if(obj==null) return;
	var otd=null;
	if(obj.tagName=="SELECT")
		otd=obj.parentElement.parentElement;
	else
		otd=obj.parentElement;

	if(otd==null|| otd.tagName!="TD")
		return false;
	var otr=tabTi.rows[tabTi.rows.length-1];
	if(otr==null|| otr.tagName!="TR")
		return false;
	for(var i=otr.cells.length-1;i>0;i--)
		{
		var ntd=otr.cells[i];
		var nch=null;
		if(ntd.children[1]!=null&&ntd.children[1].tagName=="DIV")
			{
				nch=ntd.children[1].children[0];
			}
		else
			nch=ntd.children[0];
		if(nch==null || (nch.tagName!="INPUT" && nch.tagName!="SELECT"))
			continue;
		if(ntd.style.display=="none" || nch.readOnly)
			continue;
		if(ntd==otd)
		{return true;}
		else
		return false;
		}
	return false;
	}
//内部方法，判断当前控件是否是当前行的最后一个
this.IsLastInRow=IsLastInRow;
function IsLastInRow(obj)
	{
	if(obj==null) return;
	var otd=null;
	if(obj.tagName=="SELECT")
		otd=obj.parentElement.parentElement;
	else
		otd=obj.parentElement;

	if(otd==null|| otd.tagName!="TD")
		return false;
	var otr=otd.parentElement;
	if(otr==null|| otr.tagName!="TR")
		return false;
	for(var i=otr.cells.length-1;i>0;i--)
		{
		var ntd=otr.cells[i];
		var nch=ntd.children[0];
		if(nch==null )
			continue;
		if(ntd.style.display=="none" || nch.readOnly)
			continue;
		if(ntd==otd)
		{return true;}
		else
		return false;
		}
	return false;
	}

//左右键移动焦点
this.movetToRight = movetToRight;
function movetToRight(obj){
	if(obj==null) return;
	var otd=null;
	if(obj.tagName=="SELECT")
		otd=obj.parentElement.parentElement;
	else
		otd=obj.parentElement;
	if(otd==null|| otd.tagName!="TD")
		return false;
	var otr=otd.parentElement;
	if(otr==null|| otr.tagName!="TR")
		return false;
	var index = parseInt(otd.getAttribute("index"))+1;
	if(IsLast(obj))
		{
		addRow();
		var nobj=findFTd(otr.nextSibling).children[0];
		if(nobj!=null)
		nobj.focus();
		return ;
		}
	else
	{	
		if(IsLastInRow(obj))
		{
		var nnobj=findFTd(otr.nextSibling).children[0];
		if(nnobj!=null)
		nnobj.focus();
		return ;
		}
		var objNext = null;
		var tdindex=countOT(otd);
		var trindex=otr.rowIndex;
		var i;
		for( i=tdindex;i<otr.cells.length;i++){
			var objTemp= otr.cells[i];
			if(objTemp.display!="none" && objTemp.getAttribute("index")==index)
				{
				objNext = otr.cells[i];
				break;
				}

			}
			if(objNext.children[0].tagName=="DIV")
				objNext.children[0].focus();
			else
			objNext.children[0].focus();
	}
	}
this.movetToLeft = movetToLeft;
function movetToLeft(obj){
	
	if(obj==null) return;
	var otd=null;
	if(obj.tagName=="SELECT")
		otd=obj.parentElement.parentElement;
	else
		otd=obj.parentElement;
	if(otd==null|| otd.tagName!="TD")
		return false;
	var otr=otd.parentElement;
	if(otr==null|| otr.tagName!="TR")
		return false;
	var index = parseInt(otd.getAttribute("index"))-parseInt(1);
	var objNext = otr.cells[index];
	var tdindex=countOT(otd);
	var trindex=otr.rowIndex;
	var i;
	for( i=tdindex;i>=0;i--){
		var objTemp= otr.cells[i];
		if(objTemp.getAttribute("index")==null || objTemp.getAttribute("index")=="0")
		continue;
		if(parseInt(objTemp.getAttribute("index"))<=index){
			objNext = objTemp;
			break;
		}
	}
		/*

	if(i<0){
		if(trindex==1){
			//在第一行不移动
			return 
		}
		var nobjtr=otr.parentElement.rows[trindex-1]
		var nobj=nobjtr.children[nobjtr.cells.length-1].children[0];
		if(nobj!=null)
		nobj.focus();
		return ;
	}*/
	objNext.children[0].focus();
	}
//上下键移动焦点
this.moveFocus=moveFocus;
function moveFocus(obj,par)
	{

	if(obj==null) return;
	var otd=obj.parentElement;
	if(otd==null|| otd.tagName!="TD")
		return false;
	var otr=otd.parentElement;
	if(otr==null|| otr.tagName!="TR")
		return false;
	var trindex=otr.rowIndex;
	var tdindex=countOT(otd);
	if(otr.rowIndex==1 && par==-1)
		return;
		try
			{
			if(trindex+par >=tabTi.rows.length || trindex+par<1)
				return;
			var ntr=tabTi.rows[trindex+par];
			var ntd=ntr.cells[tdindex];
			delightLine(currentLine);
			lightLine(ntr);
			currentLine=ntr;
			if(ntd.children[0]!=null)
				ntd.children[0].focus();
			}
			catch(ex)
			{alert("发生错误！"+ex.description);}
		}
//返回TD是当前行的第几个，从0开始，防止隐藏列的问题
this.countOT=countOT;
function countOT(otd)
	{
	if(otd==null || otd.tagName!="TD")
		return;
	for(var i=0;i<otd.parentElement.cells.length;i++)
		{
		var td=otd.parentElement.cells[i];
		if(td==otd)
			return i;
		}

	}
//返回TR的第 一个可获得焦点的td，从0开始，防止隐藏列的问题
this.findFTd=findFTd;
function findFTd(otr)
	{
	if(otr==null || otr.tagName!="TR")
		return null;
	for(var i=0;i<otr.cells.length;i++)
		{
		var td=otr.cells[i];
		if(td==null)
			continue;
		if(td.style.display=="none" || td.children[0]==null || td.children[0].readOnly)
			continue;
		else
			return td;
		}

	}


//左右滚动表格体，表格头移动
this.doscroll=doscroll;
function doscroll()
	{
	if(divTi==null) return;
	if(divTou==null) return;
	if(divSum==null) return;
	var disWid=divTi.scrollLeft;
	divTou.scrollLeft=disWid;
	divSum.scrollLeft=disWid;
		//左侧固定列
	divTiLeftCol.scrollTop=divTi.scrollTop;

	}
//表格头鼠标按下事件
this.domousedown=domousedown;
function domousedown()
	{
	var obj=window.event.srcElement;
	var nextTd=null;
	if(obj==null || (obj.tagName!="SPAN" && obj.tagName!="TD"))
		return;
	if(obj.tagName=="SPAN")
		{
		obj=obj.parentElement;
		}
	nextTd=obj.previousSibling;//前一个TD
	while(nextTd.style.display=="none")
		{
		nextTd=nextTd.previousSibling;//前一个TD
		}
	var absLeft=0;
	var odiv=divAll;
	while(odiv!=document.body)
		{
		absLeft+=odiv.offsetLeft;
		odiv=odiv.offsetParent;
		}
	if(window.event.x + document.body.scrollLeft+divTi.scrollLeft >absLeft+obj.offsetLeft+obj.offsetWidth-13)
		{	
		curTd=obj;
		isMouDown=true;
		OldPlace=window.event.x+window.document.body.scrollLeft+divTi.scrollLeft;
	}
	else if( window.event.x + document.body.scrollLeft+divTi.scrollLeft >absLeft+obj.offsetLeft && window.event.x + document.body.scrollLeft+divTi.scrollLeft < absLeft+obj.offsetLeft+13)
		{
		if(nextTd.cellIndex==0) return;
		curTd=nextTd;
		isMouDown=true;
		OldPlace=window.event.x+window.document.body.scrollLeft+divTi.scrollLeft;
		}
		//alert(isMouDown);
		//curHr.style.display="block";
		//curHr.style.left=OldPlace;
		//curHr.size=obj.offsetHeight;
		//curHr.style.top=divAll.offsetTop;
		
	}

//表格头鼠标移动事件
this.domousemove=domousemove;
function domousemove()
	{
	try
		{
		//document.selection.empty();//取消选中文本效果
		var obj=window.event.srcElement;
		if(obj==null || (obj.tagName!="SPAN" && obj.tagName!="TD"))
			return;
		if(obj.tagName=="SPAN")
			{
			obj=obj.parentElement;
			}
		var absLeft=0;
		var odiv=divAll;
		while(odiv!=document.body)
			{
			absLeft+=odiv.offsetLeft;
			odiv=odiv.offsetParent;
			}
		if(window.event.x + document.body.scrollLeft+divTi.scrollLeft >absLeft+obj.offsetLeft+obj.offsetWidth-13 ||( window.event.x + document.body.scrollLeft+divTi.scrollLeft >absLeft+obj.offsetLeft && window.event.x + document.body.scrollLeft+divTi.scrollLeft < absLeft+obj.offsetLeft+13  && obj.cellIndex!=1) || isMouDown)
			{
			try
				{
				divTou.style.cursor="col-resize";
				}catch(ex)
				{divTou.style.cursor="move";}
			}
		else
			divTou.style.cursor="default";
		}catch(ex)
		{alert(ex.description);}
	}
//表格头鼠标事件
this.domouseup=domouseup;
function domouseup()
	{
	if(isMouDown)
		{
		try{
		nowPlace=window.event.x+document.body.scrollLeft+divTi.scrollLeft;
		if(isNaN(OldPlace-nowPlace))
			{
			isMouDown=false;
			return;
			}
		curTd.width=curTd.width-(OldPlace-nowPlace);
		//tabTou.width=tabTou.width-(OldPlace-nowPlace);
		isMouDown=false;
		var tdIndex=countOT(curTd);
		//alert(tdIndex);
		//改变表格体列宽
		//tabTi.width=tabTi.width-(OldPlace-nowPlace);
		for(var i=0;i<tabTi.rows.length;i++)
			{
			var otd=tabTi.rows[i].cells[tdIndex];
			if(otd!=null)
				{
				otd.width=otd.width-(OldPlace-nowPlace);
				}
			}
		//改变合计区列宽
		//tabSum.width=tabSum.width-(OldPlace-nowPlace);
		for(var i=0;i<tabSum.rows.length;i++)
			{
			var otd=tabSum.rows[i].cells[tdIndex];
			if(otd!=null)
				{
				otd.width=otd.width-(OldPlace-nowPlace);
				}
			}
		//curHr.style.display="none";
		divTiLeftCol.style.height=divTi.clientHeight-21;
		divTou.style.width=divTi.clientWidth;
		divSum.style.width=divTi.clientWidth;
		divSum.parentElement.style.top=parseInt(divTi.offsetTop)+parseInt(divTi.clientHeight)-(parseInt(divSum.clientHeight));

		//
		}
		catch(ex)
			{
			isMouDown=false;
			//curHr.style.display="none";
			//alert(ex.description);
			alert("您拖动的区域无效!");
			}
		}
	}
//表格头鼠标离开事件
this.domouseout=domouseout;
function domouseout()
	{
		document.body.style.cursor="default";
		//isMouDown=false;
	}
//调整页面大小
this.doresize=doresize;
function doresize()
	{
//调整页面大小带来的头，sum表格的宽度的变化
		divTou.style.width=divTi.clientWidth;
		divSum.style.width=divTi.clientWidth;
		divTiLeftCol.style.height=divTi.clientHeight-21;
		divSum.parentElement.style.top=parseInt(divTi.offsetTop)+parseInt(divTi.clientHeight)-(parseInt(divSum.clientHeight));
}

//拖动时不选中文本
this.domouseselect=domouseselect;
function domouseselect()
	{
	if(isMouDown)
		window.event.returnValue=false;
	}
//焦点到第row行，第col列
this.focus=focus;
function focus(row,col)
	{
	delightLine(currentLine);
	if(tabTi==null&&tabTi.tagName!="TABLE") return;
	var otr=tabTi.rows[row];
	if(otr==null) return;
	if(otr.style.display!="none" && otr.rowIndex!=0 &&  otr.rowIndex!=tabTi.rows.length)
		{
		lightLine(otr);
			currentLine=otr;
			try
			{
				var otd=otr.cells[col];
				if(otd!=null&&otd.children[0]!=null)
					otd.children[0].focus();
			}
			catch(ex)
			{alert("定位焦点失败！");}
		}
	}
//取得TabTi
this.getTabTi=getTabTi;
function getTabTi()
	{
	return tabTi;
	}
//取得第row行，第col列值
this.getCellValue=getCellValue;
function getCellValue(row,col)
	{	
	if(tabTi==null&&tabTi.tagName!="TABLE") return;
	var rowsnow=0;
	var oRow=null;
	for(var i=1;i<tabTi.rows.length;i++)
		{
		var otr=tabTi.rows[i];
		if(otr.style.display=="none")
			continue;
		rowsnow++;
		if(rowsnow==row)
			oRow=otr;
		}
	if(oRow==null) return;
		try
			{
				var otd=oRow.cells[col];
				if(otd!=null && otd.children[1]!=null&&otd.children[1]!=null && otd.children[1].tagName=="DIV")
				{
					return otd.children[1].children[0].value;							
				}
				if(otd!=null&&otd.children[0]!=null)
				{
				switch(otd.children[0].tagName)
					{
					case "INPUT":
								return(otd.children[0].value);
								break;
					case "SELECT":
								return(otd.children[0].options[otd.children[0].selectedIndex].value);
					}
							
				}
					
			}
			catch(ex)
			{return null;}
	}
this.getCellObject=getCellObject;
function getCellObject(row,col)
	{	
	if(tabTi==null&&tabTi.tagName!="TABLE") return;
		var rowsnow=0;
	var oRow=null;
	for(var i=1;i<tabTi.rows.length;i++)
		{
		var otr=tabTi.rows[i];
		if(otr.style.display=="none")
			continue;
		rowsnow++;
		if(rowsnow==row)
			oRow=otr;
		}
	if(oRow==null) return;
		try
			{
				var otd=oRow.cells[col];
				if(otd!=null&&otd.children[1]!=null&&otd.children[1]!=null&&otd.children[1].tagName=="DIV")
				{
				return otd.children[1].children[0];							
				}
				if(otd!=null&&otd.children[0]!=null)
				{
				return otd.children[0];							
				}
					
			}
			catch(ex)
			{return null;}

	}

//取得第row行，第col列值
this.setCellValue=setCellValue;
function setCellValue(row,col,value)
	{	
	if(tabTi==null&&tabTi.tagName!="TABLE") return;
var rowsnow=0;
	var oRow=null;
	for(var i=1;i<tabTi.rows.length;i++)
		{
		var otr=tabTi.rows[i];
		if(otr.style.display=="none")
			continue;
		rowsnow++;
		if(rowsnow==row)
			oRow=otr;
		}
	if(oRow==null) return;
	
		try
			{
				var otd=oRow.cells[col];
				if(otd!=null&&otd.children[0]!=null)
				{
				    
				switch(otd.children[0].tagName)
					{
					case "INPUT":
								otd.children[0].value=value;
								
								break;
					case "SELECT":
								var sel=otd.children[0];
								for(var i=0;i<sel.options.length;i++)
									{
									var opi=sel.options[i];
									if(opi.value==value)
										opi.selected=true;
									}
								break;
								//return(otd.children[0].options[otd.children[0].selectedIndex].value);
					}
							
				}
					
			}
			catch(ex)
			{alert("为单元格赋值失败！");}

	}
	//把html串set进第row行，第col列值,覆盖里面原有的内容
this.setCellHtml=setCellHtml;
function setCellHtml(row,col,value)
	{	
	if(tabTi==null&&tabTi.tagName!="TABLE") return;
var rowsnow=0;
	var oRow=null;
	for(var i=1;i<tabTi.rows.length;i++)
		{
		var otr=tabTi.rows[i];
		if(otr.style.display=="none")
			continue;
		rowsnow++;
		if(rowsnow==row)
			oRow=otr;
		}
	if(oRow==null) return;
	
		try
			{
				var otd=oRow.cells[col];
				if(otd!=null)
				{
					otd.innerHTML=value;
				}
					
			}
			catch(ex)
			{alert("为单元格赋值html失败！");}

	}

//检验第col列是否有重复值
this.checkHaveRepeat=checkHaveRepeat;
function checkHaveRepeat(col)
	{
	if(col=="") return;
	if(tabTi==null&&tabTi.tagName!="TABLE") return;
	var arrStr=new Array();
	for(var i=1;i<tabTi.rows.length;i++)
		{
		var otr=tabTi.rows[i];
		if(otr.style.display=="none")
			continue;
		var otd=otr.cells[col];
		if(otd!=null && otd.children[0]!=null)
			{
			arrStr[i-1]=otd.children[0].value;
			}
		}
	for(var j=0;j<arrStr.length;j++)
		{
		var aa=arrStr[j];
		for(k=j+1;k<arrStr.length;k++)
			{
			var bb=arrStr[k];
			if(bb==aa)
				return true;
			}
		}
	return false;
	}

//检验第col列是否有空值
this.checkHaveEmpty=checkHaveEmpty;
function checkHaveEmpty(col)
	{
	if(col=="") return;
	if(tabTi==null&&tabTi.tagName!="TABLE") return;
	for(var i=1;i<tabTi.rows.length;i++)
		{
		var otd=tabTi.rows[i].cells[col];
		
		if(otd!=null && otd.children[0]!=null)
			{
			if(otd.children[0].value=="")
			 return true;
			 
			}
		}
	return false;
	}

//重新排列序号列
this.index=index;
function index(col)
	{
	if(col=="") return;
	if(tabTi==null&&tabTi.tagName!="TABLE") return;
			var index=0;
	for(var i=1;i<tabTi.rows.length;i++)
		{
		if(tabTi.rows[i].style.display=="none")// || tabTi.rows[i].rowIndex==1 &&  tabTi.rows[i].rowIndex==tabTi.rows.length-1 )
			continue;
		var otd=tabTi.rows[i].cells[col];
		if(otd!=null && otd.children[0]!=null)
			{
			otd.children[0].value=index+1;
			}
		index++;
		}
	}
//返回某列的合计值
this.sum=sum;
function sum(col)
	{
	if(col=="") return;
	if(tabTi==null&&tabTi.tagName!="TABLE") return;
	var sum=0;
	for(var i=1;i<tabTi.rows.length;i++)// modify by yaoximing
		{
		if(tabTi.rows[i].style.display=="none" )
			continue;
		var otd=tabTi.rows[i].cells[col];
		if(otd!=null && otd.children[0]!=null)
			{
			var val=parseFloat(otd.children[0].value);
			if(isNaN(val))
				val=0;
			sum+=val;
			}
		}
		return sum;
	}

 //设置合计区域单元格的值
 this.setSumFieldValue=setSumFieldValue;
 function setSumFieldValue(row,col,value)
	{
		if(tabSum==null&&tabSum.tagName!="TABLE") return;
	var otr=tabSum.rows[row];
	if(otr==null) return;
	if(otr.style.display!="none")
		{
		try
			{
				var otd=otr.cells[col];
				if(otd!=null)
				{
					if(otd.children[0]!=null)
					{
					otd.children[0].innerText=value;
					}
					else
					otd.innerHTML=value;
				}
					
			}
			catch(ex)
			{alert("为单元格赋值失败！");}
		}

	}



//获得合计区域单元格的值
	this.getSumFieldValue=getSumFieldValue;
	function getSumFieldValue(row,col)
	{	
	if(tabSum==null&&tabSum.tagName!="TABLE") return;
	var otr=tabTi.rows[row];
	if(otr==null) return;
	if(otr.style.display!="none" && otr.rowIndex!=0 )
		{
		try
			{
				var otd=otr.cells[col];
				if(otd!=null)
				{
					if(otd.children[0]!=null)
					{
					return(otd.children[0].value);
					}
					else
					return(otd.innerText);
				}			
					
			}
			catch(ex)
			{return null;}
		}
	}
//获得网格字符串
this.getGridValue=getGridValue;
function getGridValue(col,tag1,tag2)
		{
		if(tabTi==null&&tabTi.tagName!="TABLE") return;
		if(tag1=="") tag1=",";
		if(tag2=="") tag2=";";
		var cellStr="";
		if(col!="")
			{
			var arrCol=col.split(",");
			for(var j=0;j<tabTi.rows.length;j++)
				{
				for(var i=0;i<arrCol.length;i++)
					{
					cellStr+=this.getCellValue(j,arrCol[i])+tag1;
					}
					if(cellStr.substring(cellStr.length-1,cellStr.length)==tag1)
						cellStr=cellStr.substring(0,cellStr.length-1);
					cellStr+=tag2;
				}
			}
		else
			{
			for(var j=0;j<tabTi.rows.length;j++)
				{
				for(var i=0;i<tabTi.rows[0].cells.length;i++)
					{
					cellStr+=this.getCellValue(j,i)+tag1;
					}
					if(cellStr.substring(cellStr.length-1,cellStr.length)==tag1)
						cellStr=cellStr.substring(0,cellStr.length-1);
					cellStr+=tag2;
				}
			}

			return cellStr;
		}
//获得网格xml字符串
this.getGridXmlValue=getGridXmlValue;
function getGridXmlValue(col)
		{
		if(col!="")
			{
			var arrCol=col.split(",");
			cellXmlStr="";
			for(var j=0;j<tabTi.rows.length;j++)
				{
				for(var i=0;i<arrCol.length;i++)
					{
					var otd=tabTi.rows[j].cells[arrCol[i]];
					var code=otd.getAttribute("xml");
					if(code==null)
						code="";
					var name=otd.name;
					if(name==null)
						name="";
					cellXmlStr+="<detail><code>"+code+"</code><name>"+name+"</name></detail>";
					}
				}
				return("<orderDetail>"+cellXmlStr+"</orderDetail>");
			}
		else
			{
			for(var j=0;j<tabTi.rows.length;j++)
				{
				for(var i=0;i<tabTi.rows[0].cells.length;i++)
					{
					var otd=tabTi.rows[j].cells[i];
					var code=otd.getAttribute("xml");
					if(code==null)
						code="";
					var name=otd.name;
					if(name==null)
						name="";
					cellXmlStr+="<detail><code>"+code+"</code><name>"+name+"</name></detail>";
					}					
				}
			return("<orderDetail>"+cellXmlStr+"</orderDetail>");
			}
		}

//接口，添加行
this.addRow=addRow;
function addRow()
	{
	addLine();
	if(primaryKeyCol!=-1)
		index(indexCol);
	CountSum();

	//由于出现了滚动条,重新给宽度赋值
		divTiLeftCol.style.height=divTi.clientHeight-21;
		divTou.style.width=divTi.clientWidth;
		divSum.style.width=divTi.clientWidth;
	}
//删除行
this.delRow=delRow;
function delRow()
	{
	//比较deleteCol的值
	try
		{
	if(tabTi==null&&tabTi.tagName!="TABLE" ) return;
	if(tabTi.rows.length==1) return;
	if(currentLine==null)
		return;
	//
	if(primaryKeyCol!=-1){

	if(currentLine.cells[primaryKeyCol].children[0].value!="")
		this.hideLine();
	else
		this.delLine();
	}else{
	    this.delLine(); 
		
    }
	
	if(primaryKeyCol!=-1)
		index(indexCol);
		}catch(ex)
		{
		alert(ex.description);
		}
	CountSum();
		//若表体不出现滚动条，则表头的宽度变大
		divTou.style.width=divTi.clientWidth;
		divSum.style.width=divTi.clientWidth;	
		}
//删除行
this.delCheckRow=delCheckRow;
function delCheckRow(checkcol)
	{
	//比较deleteCol的值
	try
		{
	if(tabTi==null&&tabTi.tagName!="TABLE" ) return;
	if(tabTi.rows.length==1) return;
	if(checkcol==null)
		return;
	//
	for(var i=tabTi.rows.length-1;i>0;i--)
			{
		var otr=tabTi.rows[i];
		if(otr.style.display=="none")
			continue;
		//alert(1+","+i);
		var otd=otr.cells[checkcol];
		if(otd==null) return;
		var ochk=otd.children[0];
		if(ochk==null || ochk.tagName!="INPUT")
			return;
		var ctr=null;
		if(ochk.checked)
			ctr=otr;
		else
			continue;
		//alert(i);
		if(ctr==null)
			continue;
			if(primaryKeyCol!=-1){
			if(ctr.cells[primaryKeyCol].children[0].value!="")
				{
				ctr.style.display="none";
				ochk.checked=false;
				
				}
			else
				otr.removeNode(true);//this.delLine();
			}else{
				otr.removeNode(true);//this.delLine(); 
			}
			//delightLine(currentLine);
			//为左边固定列删除一行
				if(divTiLeftCol!=null && divTiLeftCol.children[0]!=null)
				{
				var tableft=divTiLeftCol.children[0];
				tableft.rows[tableft.rows.length-1].removeNode(true);
				}
			}
	
	if(primaryKeyCol!=-1)
		index(indexCol);
		}catch(ex)
		{
		alert(ex.description);
		}
	CountSum();
		//若表体不出现滚动条，则表头的宽度变大
		divTou.style.width=divTi.clientWidth;
		divSum.style.width=divTi.clientWidth;	
		}
//计算合计区
this.CountSum=CountSum;
function CountSum()
	{
	if(tabSum==null && tabSum.tagName!="TABLE") return;
	for(var i=0;i<tabSum.rows[0].cells.length;i++)
		{
		var otd=tabSum.rows[0].cells[i];
		if(otd==null)
			continue;
		var script=otd.getAttribute("script");
		
		if(script!=null)
			eval(script);
		}
	}

//返回现在的总行数
this.returnRowCount=returnRowCount;
function returnRowCount()
	{
	
	//if(inputRowCount!=null)
		//return inputRowCount.value;
	if(tabTi==null || tabTi.tagName!="TABLE") return;
	var count=0;
		for(var i=1;i<tabTi.rows.length;i++)
		{
		var otr=tabTi.rows[i];
		if(otr.style.display!="none")
			count++;
		}
	return count;

	}


this.rename=rename;
function rename()
		{
		
			for(var j=1;j<tabTi.rows.length;j++)
				{
				for(var i=0;i<tabTi.rows[0].cells.length;i++)
					{
					var otd=null;
					if(tabTi.rows[j].cells[i].children[1]!=null && tabTi.rows[j].cells[i].children[1].tagName=="DIV")
						{
						otd=tabTi.rows[j].cells[i].children[1].children[0];
						}
					else
					otd=tabTi.rows[j].cells[i].children[0];
					if(otd==null)
						continue;
					var code=otd.getAttribute("id");
					var n = code.indexOf(".");
					var newName = code.substring(0,n)+"["+eval(j-1)+"]"+code.substring(n,code.length);
					//alert(newName)
					otd.name=newName;
					//otd.name=code+"["+j-1+"]";
					}
			
				}
		}
//得到当前行
this.getCurrentLine = getCurrentLine;
function getCurrentLine(){
	if(currentLine==null) return null;
	var hidRows=0;
	for(var j=1;j<tabTi.rows.length;j++)
	{
	if(tabTi.rows[j]==currentLine)
		break;
	if(tabTi.rows[j].style.display=="none")
		hidRows++;
	}
    return parseInt(currentLine.rowIndex)-hidRows;
}
//根据单元格中的控件得到行
this.getLineByObj = getLineByObj;
function getLineByObj(obj){
	if(obj==null) return null;
	var otr=obj;
	if(obj.tagName=="INPUT")
	{
	otr=obj.parentElement.parentElement;
	}
	if(obj.tagName=="SELECT")
	{
	otr=obj.parentElement.parentElement.parentElement;
	}
	if(otr!=null && otr.tagName=="TR")
	{
	var hidRows=0;
		for(var j=1;j<tabTi.rows.length;j++)
		{
		if(tabTi.rows[j]==otr)
			break;
		if(tabTi.rows[j].style.display=="none")
			hidRows++;
		}
	
    return parseInt(otr.rowIndex)-hidRows;
	}
}

//得到选中的行的复选框的value,用","分割
this.getCheckAll = getCheckAll;
function getCheckAll()
	{
		if(tabTi==null&&tabTi.tagName!="TABLE") return "";
		try
			{
			var col=null;
			for(var i=0;i<tabTi.rows[1].cells.length;i++)
				{
				var otd=tabTi.rows[1].cells[i].children[0];
				if(otd!=null && (otd.type=="checkbox"  || otd.type=="radio") )
					{
					col=i;
					break;
					}
				}
			if(col==null || isNaN(col)) return "";
			var str="";
			for(var j=1;j<tabTi.rows.length;j++)
				{
				if(tabTi.rows[j].cells[col].children[0].tagName=="INPUT")
					{
					if(tabTi.rows[j].cells[col].children[0].checked)
						{
						for(var k=1;k<tabTi.rows[j].cells.length;k++)
							{
							var ootd=tabTi.rows[j].cells[k];
							if(ootd.children[0]!=null)
								{
								var val="";
								val=ootd.children[0].value;
								if(val==null)
									val="";
								str+=val+",";
								}
							}
							str+="|";
						}
					}
				}

				return str.substring(0,str.length-1);
			}
			catch(ex)
			{	//alert(ex.description);
				return "";
			}
	}

//在行中判断商品是否有重复的函数 20040102 add by zhangll
//检查商品变好是否重复
this.checkProdId = checkProdId;
function checkProdId(prodId,row,col) {
	var countRows = this.returnRowCount();
		for (var i=1;i<countRows ;i++) {
		if(i == row )
			continue;
		var oInput = this.getCellValue(i,col);
		if ( oInput == prodId) {
			alert("商品编号不能重复");
			this.setCellValue(row,col,"");
			focus(row,col);
			return false;
		}
	}
	return true;
}

//根据行号，删除指定的行 add 20040107 
this.delRowByIndex = delRowByIndex;
function delRowByIndex(index)
	{
		if(tabTi==null&&tabTi.tagName!="TABLE") return;
		try
			{
			//if(currentLine!=null)
		//		{
				//if(parseInt(tabTi.offsetHeight) < parseInt(maxDivHeight) && parseInt(tabTi.offsetHeight)>parseInt(curTrHeight))
				//lowHeight();
				//curDivHeight=curDivHeight-parseInt(curTrHeight);
				//var otr=this.findCurrentLine();
				var otr = tabTi.rows[index];
				//currentLine.removeNode(true);
				otr.removeNode(true);
				//currentLine=otr;
				if(inputRowCount!=null)
				{
					
					inputRowCount.value=parseInt(inputRowCount.value)-1;
					if(returnRowCount()!=inputRowCount.value)
						inputRowCount.value=returnRowCount();
				}
				rename();//改名
			//	}
			}
			catch(ex)
			{
				alert("删除一行失败！");
				return;
			}
	}
//外部set方法setTouText(col,val)
this.setTouText=setTouText;
function setTouText(col,val)
	{
	if(tabTou==null) return;
	var otd=tabTou.rows[0].cells[col];
	var ospan=otd.children[0];
	ospan.innerHTML=val;
	}

//为grid中的select列加的附加函数
this.doTextFocusInGrid=doTextFocusInGrid;
function doTextFocusInGrid(obj){
if(obj==null || obj.tagName!="INPUT")
return;
var pobj=obj.parentElement.children[1];
if(pobj==null && pobj.tagName!="DIV") return;
obj.style.display="none";
pobj.style.display="block";
pobj.children[0].focus();
}
this.doSelBlurInGrid=doSelBlurInGrid;
function doSelBlurInGrid(obj){
if(obj==null || obj.tagName!="SELECT")
return;
var nobj=obj.parentElement.parentElement.children[0];
if(nobj==null || nobj.tagName!="INPUT") return;
var opt=obj.options[obj.selectedIndex];
nobj.value=opt.innerText;
obj.parentElement.style.display="none";
nobj.style.display="block";
}

//改变CHECKBOX的值
this.changeCheckboxValue=changeCheckboxValue;
function changeCheckboxValue()
	{
	var obj=event.srcElement;
	if(obj.type!="checkbox") return;
	if(obj.value==="0")
		obj.value="1";
	else
		obj.value="0";
	}


//日历部分
this._inchingswitch=_inchingswitch;
function _inchingswitch(obj,flag)
{
	var o,ob,ar;
	if(date_currob != null&&obj.parentElement.parentElement.contains(date_currob))
	{
		o = date_currob;
		ob = date_currob.value;
		ar = date_currob.conf.split(",");
	}
	else
	{
		o = obj.parentElement.nextSibling;//输入筐
		ob = obj.parentElement.nextSibling.value;//输入筐值
		ar = obj.parentElement.nextSibling.conf.split(",");//参数用逗号分割
	}

	var step = parseInt(ar[0]);
	var min = parseInt(ar[1]);
	var max = parseInt(ar[2]);
	var alph = ar[3];

	if(ob=="")
	ob=ar[1];//如果输入域为空则默认是最小值
	if(typeof(alph)!="undefined"&&alph!="")
	{
		if(alph==ob.substr(ob.length-2,ob.length-1))
			ob = ob.substr(0,ob.length-1);
	}
	var obvalue = parseInt(ob);
	if(isNaN(obvalue))
	{
//		alert("微调初始值是非数字！");
		obvalue = min;
	}
	if(isNaN(step))
		step = 1;
	if(flag=="+")
	{
		obvalue = obvalue + step;
		if(obvalue>max)
			return;
	}
	if(flag=="-")
	{
		obvalue = obvalue - step;
		if(obvalue<min)
			return;
	}
	o.value = obvalue+alph;
	o.select();
}
this._uper=_uper;
function _uper(ob)
{
	//alert(event.fromElement);
	if(date_interv_ob!=null)
		_inchingswitch(date_interv_ob,"+");
	else
		_inchingswitch(ob,"+");
	
}
this._conti_uper=_conti_uper;
function _conti_uper(obb)
{
	if(date_interv==null)
	{
		date_interv_ob = obb;
		date_interv=window.setInterval(this._uper, 200);
	}
}
this._stop_inch=_stop_inch;
function _stop_inch()
{
	if(date_interv!=null)
	{
		clearInterval(date_interv);
		date_interv=null;
		date_interv_ob = null;
	}
}
this._downer=_downer;
function _downer(ob)
{
	if(date_interv_ob!=null)
		_inchingswitch(date_interv_ob,"-");
	else
		_inchingswitch(ob,"-");

}
this._conti_downer=_conti_downer;
function _conti_downer(obb)
{
	if(date_interv==null)
	{
		date_interv_ob = obb;
		date_interv=window.setInterval(this._downer, 200);
	}
}
this._selthis=_selthis;
function _selthis(ob)
{
	ob.select();//alert(ob.tagName);
	date_currob = ob;
}
//微调结束
this.fGetDaysInMonth=fGetDaysInMonth;
function fGetDaysInMonth(iMonth, iYear) {
var dPrevDate = new Date(iYear, iMonth, 0);
return dPrevDate.getDate();
}
this.fBuildCal=fBuildCal;
function fBuildCal(iYear, iMonth, iDayStyle) {
var aMonth = new Array();
aMonth[0] = new Array(7);
aMonth[1] = new Array(7);
aMonth[2] = new Array(7);
aMonth[3] = new Array(7);
aMonth[4] = new Array(7);
aMonth[5] = new Array(7);
aMonth[6] = new Array(7);
var dCalDate = new Date(iYear, iMonth-1, 1);
var iDayOfFirst = dCalDate.getDay();
var iDaysInMonth = fGetDaysInMonth(iMonth, iYear);
var iVarDate = 1;
var i, d, w;
if (iDayStyle == 2) {
aMonth[0][0] = "Sunday";
aMonth[0][1] = "Monday";
aMonth[0][2] = "Tuesday";
aMonth[0][3] = "Wednesday";
aMonth[0][4] = "Thursday";
aMonth[0][5] = "Friday";
aMonth[0][6] = "Saturday";
} else if (iDayStyle == 1) {
aMonth[0][0] = "日";
aMonth[0][1] = "一";
aMonth[0][2] = "二";
aMonth[0][3] = "三";
aMonth[0][4] = "四";
aMonth[0][5] = "五";
aMonth[0][6] = "六";
} else {
aMonth[0][0] = "Su";
aMonth[0][1] = "Mo";
aMonth[0][2] = "Tu";
aMonth[0][3] = "We";
aMonth[0][4] = "Th";
aMonth[0][5] = "Fr";
aMonth[0][6] = "Sa";
}
for (d = iDayOfFirst; d < 7; d++) {
aMonth[1][d] = iVarDate;
iVarDate++;
}
for (w = 2; w < 7; w++) {
for (d = 0; d < 7; d++) {
if (iVarDate <= iDaysInMonth) {
aMonth[w][d] = iVarDate;
iVarDate++;
      }
   }
}
return aMonth;
}
this.fDrawCal=fDrawCal;
function fDrawCal(iYear, iMonth,iDayStyle) {
var myMonth;
myMonth = fBuildCal(iYear, iMonth, iDayStyle);

innerhtml+="<table border='0' width='100%'  class='date_body' cellspacing='0' cellpadding='0' ><tr class='date_title'>";
innerhtml+="<td width='10%' unselectable='on'>" + myMonth[0][0] + "</td>";
innerhtml+="<td width='10%' unselectable='on'>" + myMonth[0][1] + "</td>";
innerhtml+="<td width='10%' unselectable='on'>" + myMonth[0][2] + "</td>";
innerhtml+="<td width='10%' unselectable='on'>" + myMonth[0][3] + "</td>";
innerhtml+="<td width='10%' unselectable='on'>" + myMonth[0][4] + "</td>";
innerhtml+="<td width='10%' unselectable='on'>" + myMonth[0][5] + "</td>";
innerhtml+="<td width='10%' unselectable='on'>" + myMonth[0][6] + "</td>";
innerhtml+="</tr>";
for (w = 1; w < 6; w++) {
      innerhtml+="<tr class='date_row_odd'>"
      for (d = 0; d < 7; d++) {
            //innerhtml+="<td align='center' valign='top' width='" + iCellWidth + "' height='" + iCellHeight + "' id=calCell style='cursor:pointer' onclick=fSetSelectedDay(this)>");
            
            if(myMonth[w][d]==dDate.getDate() && year==dDate.getFullYear() && month==dDate.getMonth()+1)
                innerhtml+="<td   width='10%' bgcolor='#FFcc99' unselectable='on'>";
            else if(oldday!=null && myMonth[w][d]==oldday)
                innerhtml+="<td   width='10%'  bgcolor='#FFFFFF' unselectable='on'>";    
            else
                innerhtml+="<td   width='10%' unselectable='on'>";
            if (!isNaN(myMonth[w][d])) {
                   
                   innerhtml+="" + myMonth[w][d];
   
            } else {
                  //innerhtml+="<font id=calDateText style='cursor:pointer'>&nbsp;</font>";
            }
           innerhtml+="</td>"
       }
      innerhtml+="</tr>";
 }
innerhtml+="<tr  class='date_row_odd' unselectable='on'>"
for (d = 0; d < 7; d++) {
   innerhtml+="<td  width='10%'>";
if (!isNaN(myMonth[6][d])) {        
     innerhtml+="<p style='margin-left: 5; margin-right: 5;-top: 1'>" + myMonth[6][d];
   
} else {
}
innerhtml+="</td>"
}
innerhtml+="</tr>";
innerhtml+="</table>";
}

//*******************************
//
//*******************************
this.showSelectTable=showSelectTable;
function showSelectTable(flag){
   var obj=document.all("objcalendar");
   if(obj.style.display=="block")
        obj.style.display="none";
	else

   obj.style.position="absolute";
   
           	
}

this.parseInputTextValue=parseInputTextValue;
function parseInputTextValue(val){
  var value=checkinputtextvalue(val);
  var b=new Array();
  for(var i=0;i<2;i++){
   var a=value.indexOf(middle);
   if(value.substring(0,a)=="08"){
      b[i]=8;	
   }else{
   if(value.substring(0,a)=="09"){
      b[i]=9;	
   }else{
      b[i]=parseInt(value.substring(0,a));
   }
   }
   
   value=value.substring(a+1);
   }
   if(value.charAt(0)=='0'){
       value=value.substring(1);
    }
  b[2]=parseInt(value);
  year=b[0];
  month=b[1];
  day=b[2];
 changeYearAndMonth(3);    	
}

this.checkinputtextvalue=checkinputtextvalue;
function checkinputtextvalue(val){
   var flag=false;
   var i,j;
   var value,val1,val2;
   if(val.length!=10){
      flag=true;
   }
   if(flag){
      value=year+middle;
      if(month<10)
          value+="0"+month+middle;
       else
          value+=month+middle;
      if(day<10)
          value+="0"+day;
       else
          value+=day;
       if(hasform){
	  var obj1=eval(tableformname);
	  input=eval(obj1.name+"."+inputtextid);
       }
       else
	  input=document.all(inputtextid);
   }
   else
       value=val;

   return value;
}


this.OnMouseOver=OnMouseOver;
function OnMouseOver(){
  var obj=event.srcElement;
  if(obj!=curDateTd)
  obj.className="date_mouseover";
}

this.OnMouseOut=OnMouseOut;
function OnMouseOut(){
  var obj=event.srcElement;
  if(obj!=curDateTd)
	obj.className="";
}

this.OnMouseDown=OnMouseDown;
function OnMouseDown(){
  var obj=event.srcElement;
  if(curDateTd!=null)
	  curDateTd.className="";
	curDateTd=obj;
  day=obj.innerText;
  obj.className="date_select";
}
this.OnDbClick=OnDbClick;
function OnDbClick(){
  var obj=event.srcElement;
  if(curTd!=null)
	  curTd.className="";
	curTd=obj;
  day=obj.innerText;
  obj.className="date_select";
saveValue();
}

this.changeYearAndMonth=changeYearAndMonth;
function changeYearAndMonth(oyear,omonth){
	if(oyear!=null)
		year=oyear;
if(omonth!=null)
	month=omonth;
  innerhtml="";
  setJavaScript();
}
this.changeHourAndMinute=changeHourAndMinute;
function changeHourAndMinute(ohour,ominute){
	if(ohour!=null)
		hour=ohour;
if(ominute!=null)
	minute=ominute;
}

this.setJavaScript=setJavaScript;
function setJavaScript(){
  fDrawCal(year,month,1);
  var obj=divDate;
  obj.innerHTML=innerhtml;
  var tabDate=divDate.children[0];
  if(tabDate!=null)
	{
	for(var i=1;i<tabDate.rows.length;i++)
		{
		for(var j=0;j<tabDate.rows[i].cells.length;j++)
			{
			var otd=tabDate.rows[i].cells[j];
			if(otd.innerText=="") continue;
				otd.onmouseover=OnMouseOver;
				otd.onmouseout=OnMouseOut;
				otd.onmousedown=OnMouseDown;
				otd.ondblclick=OnDbClick;
			}
		}
	}
}

this.set=set;
function set(objid,splitStr){
if(objid!=null && objid!="")
   inputtextid=objid;
   if(splitStr!=null && splitStr!="")
   middle=splitStr;
   innerhtml="";
   setJavaScript(); 
}
//年输入框失去焦点
this.doYearBlur=doYearBlur;
function doYearBlur(obj)
{
if(obj==null) return;
var arr=obj.conf.split(",");
var objvalue=obj.value;
var minyear=arr[1];
var maxyear=arr[2];
if(isNaN(objvalue) || objvalue=="")
objvalue=year;
if(parseInt(objvalue)<parseInt(minyear))
objvalue=minyear;
if(parseInt(objvalue)>parseInt(maxyear))
objvalue=maxyear;
obj.value=objvalue;
changeYearAndMonth(objvalue);
}

//小时输入框失去焦点
this.doHourBlur=doHourBlur;
function doHourBlur(obj)
{
if(obj==null) return;
var arr=obj.conf.split(",");
var objvalue=obj.value;
var minyear=arr[1];
var maxyear=arr[2];
if(isNaN(objvalue) || objvalue=="")
objvalue=0;
if(parseInt(objvalue,10)<0)
objvalue="0";
if(parseInt(objvalue)>23)
objvalue=23;
obj.value=objvalue;
hour=obj.value;
}
//分钟输入框失去焦点
this.doMiniteBlur=doMiniteBlur;
function doMiniteBlur(obj)
{
if(obj==null) return;
var arr=obj.conf.split(",");
var objvalue=obj.value;
var minyear=arr[1];
var maxyear=arr[2];
if(isNaN(objvalue) || objvalue=="")
objvalue=0;
if(parseInt(objvalue,10)<0)
objvalue="0";
if(parseInt(objvalue)>59)
objvalue=59;
obj.value=objvalue;
minute=obj.value;
}

//初始化
this.initDate=initDate;
function initDate(iniyear,inimonth,iniday,inihour,iniminute)
	{
	if(divDateAll!=null && divDateAll.tagName=="DIV")
	{
	divDate=divDateAll.children[1];
	divTime=divDateAll.children[2].children[0];
	var obj=window.event.srcElement;
	if(obj!=null && obj.tagName=="BUTTON")
	inputtext=obj.previousSibling;
	}
	if(iniyear!=null) year=iniyear;
	if(inimonth!=null) month=inimonth;
	if(iniday!=null) day=iniday;
	if(inihour!=null) hour=inihour;
	if(iniminute!=null) minute=iniminute;
	set("",'-');
	setYearText();
	setMonthSel();
	}

//返回最后的值
this.saveValue=saveValue;
function saveValue()
	{
	var datestr=year+middle+month+middle+day;
	var timestr="";
	if(divTime!=null && divTime.style.display=="none")
		{//timestr=hour+":"+minute;
		hour="";
		minute="";
		}
	var ndate=new Date(year,month-1,day,hour,minute);
	var targetStr=targetFormat;
	if(divDateAll.targetFormat!=null && divDateAll.targetFormat!="")
	targetStr=divDateAll.targetFormat;
	inputtext.value=ndate.format(targetStr);
	//document.selection.empty();
	divDateAll.style.display="none";
	if(curDateTd)
		{
		curDateTd.className="";
		curDateTd=null;
		}
	}

this.setFormat=setFormat;
function setFormat(target){
	if (target!=null||target!="")
	{
		targetFormat=target;
	}
  
}
//显示/隐藏
this.showDate=showDate;
function showDate()
	{
	if(divDateAll==null) return;
	var odiv=divDateAll;
	if(odiv==null) return;
	var iniyear,inimonth,iniday,inihour,iniminute;
	var odate=null;
	defstr=divAll.children[0].children[0].value;
	if(defstr!=null && defstr!="")
		{
		try
			{
			//var regstr=/-/g;
			//defstr=defstr.replace(regstr,"/");
			defstr=parseIntputDate(defstr);
			//alert(defstr);
			odate=new Date(defstr);
			if(isNaN(odate))
				{
				alert("日期输入错误!");
				return;
				}
			oldday=odate.getDate();
		}catch(ex)
			{
			alert("日期输入错误!");
			}
		}
	else
		{
		odate=new Date();
		oldday=null;
		}		
	iniyear=odate.getFullYear();
	inimonth=odate.getMonth()+1;
	iniday=odate.getDate();
	inihour=odate.getHours();
	iniminute=odate.getMinutes();
	initDate(iniyear,inimonth,iniday,inihour,iniminute);
	odiv.style.left=inputtext.offsetParent.offsetLeft+inputtext.offsetLeft+divAll.offsetLeft-divTi.scrollLeft;
	//循环取高度DIV到BODY
	var i=0;
	var top=0;
	//var left=0;
	var ooodiv=divAll;
    while(ooodiv.offsetParent.tagName!="BODY")
	{
	if(i>50) return;
	ooodiv=ooodiv.offsetParent;
	top+=ooodiv.offsetTop;
	//left+=ooodiv.offsetLeft;
	}
//
	odiv.style.top=inputtext.offsetParent.offsetTop+inputtext.offsetTop+inputtext.offsetHeight+divAll.offsetTop-divTi.scrollTop+top;
	if(odiv.style.display=="none")
			odiv.style.display="block";
	else
			odiv.style.display="none";
	}


//SET方法
this.setDay=setDay;
function setDay(oday)
	{
	if(oday!=null && oday!="")
	day=oday;
	}

//把输入的日期转换成yyyy/MM/dd hh:mm
this.parseIntputDate=parseIntputDate;
function parseIntputDate(str)
	{
var sparr=str.split(" ");
var pararr=targetFormat.split(" ");
var len=sparr.length;
var datestr,timestr;
var pardate,partime;
if(len>1)
{
datestr=sparr[0];
timestr=sparr[1];
pardate=pararr[0];
partime=pararr[1];
}
else
{
datestr=str;
timestr="";

var targetStr=targetFormat;
	if(divDateAll.targetFormat!=null && divDateAll.targetFormat!="")
	targetStr=divDateAll.targetFormat;

pardate=targetStr;
partime="";
}
var regstr=/[^yyyyMMdd]/g;
var arr=pardate.match(regstr);
if(arr==null)
{
datestr=datestr.slice(0,4)+"/"+datestr.slice(4,6)+"/"+datestr.slice(6);
}
else
{
	for(var i=0;i<arr.length;i++)
	{
	datestr=datestr.replace(arr[i],"/");
	}
	var ldate=datestr.substr(datestr.length-1,1);
	if(isNaN(ldate))
	{
	datestr=datestr.substr(0,datestr.length-1);
	}
}
if(timestr!="")
{
	var regtime=/[^hhmm]/g;
	var arrt=partime.match(regtime);
	if(arrt==null)
	{
	timestr=timestr.slice(0,2)+":"+timestr.slice(2);
	}
	else
	{
		for(var i=0;i<arrt.length;i++)
		{
		timestr=timestr.replace(arrt[i],":");
		}
		var ltime=timestr.substr(timestr.length-1,1);
		if(isNaN(ltime))
		{
		timestr=timestr.substr(0,timestr.length-1);
		}
	}

}
return (datestr+" "+timestr);

}

//为年的文本框赋值
this.setYearText=setYearText;
function setYearText()
	{
	var yeartext=divDateAll.children[0].children[0].rows[0].cells[0].children[0].children[1];
	if(yeartext!=null && yeartext.tagName=="INPUT")
		{
		yeartext.value=year;
		}
	}
//为月的下拉框赋值
this.setMonthSel=setMonthSel;
function setMonthSel()
	{
	var monthSel=divDateAll.children[0].children[0].rows[0].cells[1].children[0];
	if(monthSel!=null && monthSel.tagName=="SELECT")
		{
		for(var i=0;i<monthSel.options.length;i++)
			{
			if(monthSel.options[i].value==month)
				monthSel.options[i].selected=true;
			}
		}
	}


//改变checkbox列的值 
this.doclickingrid=doclickingrid;
function doclickingrid(obj)
	{
	if(obj!=null && obj.tagName=="INPUT")
		{
		if(obj.checked)
			{
			obj.value=1;
			}
		else
			obj.value=0;
		}
	}

//改变行数,使行数隐藏域的值域returnRowCount()
this.setRowCount=setRowCount;
function setRowCount()
	{
	if(returnRowCount()!=inputRowCount.value)
		inputRowCount.value=returnRowCount();
	}




}
//扩充DATE
Date.prototype.format=function(fomatstr)
{
if(fomatstr==null || fomatstr=="")
	return ;
var nowYear=this.getFullYear();
var nowMonth=this.getMonth()+1;
var nowDay=this.getDate();
var nowHour=this.getHours();
var nowMinute=this.getMinutes();
if(nowYear!=null && nowYear!="")
	{
	fomatstr=fomatstr.replace("yyyy",nowYear);
	}
if(nowMonth!=null && nowMonth!="")
	{
nowMonth=this.numUtil(nowMonth);
	fomatstr=fomatstr.replace("MM",nowMonth);
	}
if(nowDay!=null && nowDay!="")
	{
	nowDay=this.numUtil(nowDay);
	fomatstr=fomatstr.replace("dd",nowDay);
	}
if(nowHour!=null && nowHour!="")
	{
	nowHour=this.numUtil(nowHour);
	var restr=/HH/i;
	fomatstr=fomatstr.replace(restr,nowHour);
	}
else
	{
	var restr=/hh[\S ]*mm/i;
	fomatstr=fomatstr.replace(restr,"");	
	}
if(nowMinute!=null && nowMinute!="")
	{
	nowMinute=this.numUtil(nowMinute);
	fomatstr=fomatstr.replace("mm",nowMinute);
	}
return fomatstr.replace(/\s*$/g,"");
}
Date.prototype.numUtil=function(num)
{
	if(parseInt(num)<10)
		return "0"+num;
	else
		return num;

}
