//每月的天数
//var Days_in_Month = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

// Non-Leap year Month days..
Days_in_Month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Leap year Month days..
Days_in_Month_00 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
//月份
var Month_Label = new Array('一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月');

var myObj;

//创建日历层及相关的样式
function createdCalendar()
{
	var reHtml = "";
	var today = new Date();
	//要用到的样式visibility: hidden;
	reHtml += "<style type=\"text/css\">";
	reHtml += "<!--";
	reHtml += ".table{border-collapse: separate;}";
	reHtml += ".dateSelectDiv {border: 1px solid #9966FF;position: absolute;z-index:1000;overflow:visible ;Height: 150;width: 200;left: 10px;top: 10px;font-size: 9pt;background-color: #FFFFFF;padding : 3px 3px 3px 3px;}";
	reHtml += ".dateSelectTextSize {font-size:9pt;}";
	reHtml += ".dateSelectHeadSize {font-size:9pt;}";
	reHtml += "a.dateSelectLink:link {color: #660099;text-decoration: none;}";
	reHtml += "a.dateSelectLink:visited {color: #0000CC;text-decoration: none;}";
	reHtml += "a.dateSelectLink:hover {color: #FF0000;text-decoration: none;POSITION: relative;TOP: 1px; }";
	reHtml += "a.dateSelectLink:active {color: #660099;text-decoration: none;}";
	reHtml += ".dateSelectTextField{font-size:9pt;border: 1px solid #BBB9B9;background-color : transparent;}";
	reHtml += ".dateSelectSpan{font-size:9pt;border: 1px solid #BBB9B9;background-color : #ffffff;position:absolute;overflow:hidden;}";
	reHtml += ".dateSelectSelectField{margin:-2px;font-size: 9pt;}";

	reHtml += "-->";
	reHtml += "</style>";

	//开始新建层
	reHtml += "<div id=\"dateSelectDiv\" class=\"dateSelectDiv\">";

	//标题行
	reHtml += "<table STYLE=\"border-collapse: separate\"; width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" class=\"dateSelectTextSize\"><tr><td>请选择日期/时间</td>";
	reHtml += "         <td align=\"right\"><a href=\"javascript:closeDateSelectDiv();\" class=\"dateSelectLink\">关闭</a></td></tr></table>";

	//年月选择
	reHtml += "<table STYLE=\"border-collapse: separate\"; width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" class=\"dateSelectHeadSize\">";
	reHtml += "  <tr> ";
	reHtml += "    <td>&nbsp;&nbsp;</td><td><a href=\"javascript:changeYear('-')\" class=\"dateSelectLink\" title=\"前一年\"><<</a></td>";
	reHtml += "    <td valign=\"baseline\">";
	reHtml += "      <input type=\"text\" class=\"dateSelectTextField\" size=\"4\" maxlength=\"4\" name=\"dateSelectYear\" onFocus=\"this.select();\" onBlur=\"yearChecked(this);\">&nbsp;年";
	reHtml += "        <span class=\"dateSelectSpan\" style=\"width:30px;\" ><select name=\"dateSelectMonth\" class=\"dateSelectSelectField\" onChange=\"Make_Calendar(document.all.dateSelectYear.value - 0,this.value - 0 -1);\">";
	for(var i=1;i<13;i++)	reHtml += "        <option value=\"" + (i<10?"0"+i:i+"") + "\">" + (i<10?"0"+i:i+"") + "月</option>";
	reHtml += "        </select></span>";
	reHtml += "    </td>";
	reHtml += "    <td>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:changeYear('+');\" class=\"dateSelectLink\" title=\"后一年\">>></a></td>";
	reHtml += "    <td>&nbsp;</td>";
	reHtml += "  </tr> ";
	reHtml += "</table>";

	//日历抬头
	reHtml += "<table STYLE=\"border-collapse: separate\"; border=\"1\" cellspacing=\"0\" cellpadding=\"3\" bordercolor=\"#000000\" bordercolordark=\"#ffffff\" bordercolorlight=\"#000000\" class=\"dateSelectTextSize\">";
	reHtml += "   <tr align=\"center\" valign=\"middle\" bgcolor=\"#EEEEEE\"> ";
	reHtml += "     <td width=\"40\" height=\"25\"><b>日</b></td>";
	reHtml += "     <td width=\"40\" height=\"25\"><b>一</b></td>";
	reHtml += "     <td width=\"40\" height=\"25\"><b>二</b></td>";
	reHtml += "     <td width=\"40\" height=\"25\"><b>三</b></td>";
	reHtml += "     <td width=\"40\" height=\"25\"><b>四</b></td>";
	reHtml += "     <td width=\"40\" height=\"25\"><b>五</b></td>";
	reHtml += "     <td width=\"40\" height=\"25\"><b>六</b></td>";
	reHtml += "  </tr>";
	reHtml += "</table>";

	//插入日历的地方
	reHtml += "<span id=\"dataSelectDaySelectList\"></span>";

	//当前日期
	reHtml += "<table STYLE=\"border-collapse: separate\"; width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" class=\"dateSelectTextSize\">";
	reHtml += "    <tr> ";
	reHtml += "      <td align=\"center\">当前日期：<a href=\"javascript:Make_Calendar(" + today.getFullYear() + "," + today.getMonth() + ")\" class=\"dateSelectLink\">" + today.getFullYear() + "年" + (today.getMonth()<9?"0"+(today.getMonth()+1):today.getMonth()+1) + "月</a></td>";
	reHtml += "    </tr>";
	reHtml += "</table>";

	//时间输入
	reHtml += "<table STYLE=\"border-collapse: separate\"; width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" class=\"dateSelectTextSize\" id=\"dateSelectInputTime\">";
	reHtml += "    <tr> ";
	reHtml += "      <td valign=\"bottom\">请输入时间：";
	reHtml += "        <span class=\"dateSelectSpan\" style=\"left:80px;width:19px;Height:18px;\"><select id=\"dateSelectHour\" name=\"dateSelectHour\" class=\"dateSelectSelectField\">";
	for(var i=0;i<24;i++)	reHtml += "        <option value=\"" + (i<10?"0"+i:i+"") + "\">" + (i<10?"0"+i:i+"") + "</option>";
	reHtml += "        </select></span>";
	reHtml += "        <div class=\"dateSelectSpan\" style=\"left:98px;width:19px;Height:18px;\">：</div>";
	reHtml += "        <span class=\"dateSelectSpan\" style=\"left:116px;width:19px;Height:18px;\"><select id=\"dateSelectMinute\" name=\"dateSelectMinute\" class=\"dateSelectSelectField\">";
	for(var i=0;i<60;i+=1)	reHtml += "        <option value=\"" + (i<10?"0"+i:i+"") + "\">" + (i<10?"0"+i:i+"") + "</option>";
	reHtml += "        </select></span>";
	reHtml += "        <div class=\"dateSelectSpan\" style=\"left:132px;width:19px;Height:18px;\">：</div>";
	reHtml += "        <span class=\"dateSelectSpan\" style=\"left:148px;width:19px;Height:18px;\"><select id=\"dateSelectSecond\" name=\"dateSelectSecond\" class=\"dateSelectSelectField\">";
	for(var i=0;i<60;i+=1)	reHtml += "        <option value=\"" + (i<10?"0"+i:i+"") + "\">" + (i<10?"0"+i:i+"") + "</option>";
	reHtml += "        </select></span>";
	reHtml += "      </td>";
	reHtml += "    </tr>";
	reHtml += "</table>";

	reHtml += "</div>";
	document.write(reHtml);
	Make_Calendar(today.getFullYear(), today.getMonth());
	document.all.dateSelectDiv.style.visibility = "hidden";
}
createdCalendar();

//根据年值确定选用的月份数组
function MyCalendar_get_daysofmonth( _year ) {
	/*
	Check for leap year ..
	1.Years evenly divisible by four are normally leap years, except for...
	2.Years also evenly divisible by 100 are not leap years, except for...
	3.Years also evenly divisible by 400 are leap years.
	*/
	if ((_year % 4) == 0) {
		if ((_year % 100) == 0 && (_year % 400) != 0)
			return Days_in_Month;

		return Days_in_Month_00;
	} else
		return Days_in_Month;
}

//根据年、月插入日历
function Make_Calendar(Year, Month) {
	var First_Date = new Date(Year, Month, 1); 	//根据年、月获取该月第一天（a）的日期形式
//	var Heading = Header(Year, Month); 	//获取抬头，及该年份2月的天数
	var First_Day = First_Date.getDay() + 1; 	//获取a是星期几：1（星期日） 到 7（星期六）

  	var today = new Date();

	//组合日期到HTML代码
	var dayflag = First_Day;	//当前星期（换行标记）
	var reHtml = "";	//返回的HTML代码

	var daysofmonth = MyCalendar_get_daysofmonth(Year);

	for(var i=1;i<=daysofmonth[Month];i++)
	{
		if(dayflag%7==1)	reHtml += "<tr align=\"center\" valign=\"middle\">";	//如果当前为星期天，则前面加起始行号
		reHtml += "<td width=\"40\" height=\"25\" onMouseOver=\"dateSelectOnMouseEvent(this)\" onMouseOut=\"dateSelectOnMouseEvent(this)\" onClick=\"dateSelectOnMouseEvent(this)\" style=\"cursor:Hand;";
	  	if((Year==today.getFullYear())&&(Month==today.getMonth())&&(i==today.getDate()))	reHtml += "font-weight: bolder;color: #FF0000;";
		reHtml += "\">" + i;
	  	reHtml += "</td>";
	  	if(dayflag%7==0)	reHtml += "</tr>";	//如果当前为星期六，则后面加结束行号

  	  	dayflag++;
  	}

	//补齐显示的天数
	if(First_Day != 1)
 	{	//向前一月补
  		var tempstr = "";
  		var tempmonth = (Month - 1)>0?(Month - 1):0;
  		var tempint = daysofmonth[tempmonth] - 0;
	  	for(var j=First_Date.getDay();j>0;j--)
  		{
  			tempstr = "<td width=\"40\" height=\"25\" onMouseOver=\"dateSelectOnMouseEvent(this)\" onMouseOut=\"dateSelectOnMouseEvent(this)\" onClick=\"dateSelectOnMouseEvent(this,'-')\" style=\"cursor:Hand\"><font color=\"blue\">" + tempint + "</font></td>" + tempstr;
	  		tempint--;
  		}
  	 	reHtml = "<tr align=\"center\" valign=\"middle\">" + tempstr + reHtml;
	}
	dayflag--;

	if(dayflag%7 != 0)
	{	//向下一个月补
 		var tempstr = "";
	  	var tempint = 7 - dayflag%7;
  		for(var k=1;k<=tempint;k++)
  		{
  			tempstr += "<td width=\"40\" height=\"25\" onMouseOver=\"dateSelectOnMouseEvent(this)\" onMouseOut=\"dateSelectOnMouseEvent(this)\" onClick=\"dateSelectOnMouseEvent(this,'+')\" style=\"cursor:Hand\"><font color=\"blue\">" + k + "</font></td>";
  		}
  	 	reHtml = reHtml + tempstr + "</tr>";
	}

	reHtml = "<table STYLE=\"border-collapse: separate\"; border=\"1\" cellspacing=\"0\" cellpadding=\"3\" bordercolor=\"#000000\" bordercolordark=\"#ffffff\" bordercolorlight=\"#000000\" class=\"dateSelectTextSize\">" + reHtml + "</table>";

	dataSelectDaySelectList.innerHTML = reHtml;
	document.all.dateSelectYear.value = Year;
	document.all.dateSelectMonth.value = (Month<9?"0"+(Month+1):(Month+1)+"");
}

//日期单元格的鼠标事件响应
function dateSelectOnMouseEvent(ele,tp)
{
	if(event.type=="mouseover")
	{	//鼠标悬停
		ele.style.backgroundColor = "#EEEEEE";
	}else if(event.type=="mouseout"){	//鼠标移开
		ele.style.backgroundColor = "";
	}else if(event.type=="click"){
		if(tp=="+")	SkipBy("+",1);
		else if(tp=="-")	SkipBy("-",1);
		else	getSelectTime(ele);
	}
}

//向前、向后翻指定个月份
function SkipBy(Direction,nub) {

	var Selected_Year = document.all.dateSelectYear.value - 0;
	var Selected_Month = document.all.dateSelectMonth.value - 0 - 1;

	for(var i=0;i<nub;i++)
	{

		if (Direction == '+')
		{
			if (Selected_Month == 11)
			{
				Selected_Month = 0;
				Selected_Year++;
			}else {
				Selected_Month++;
			}
		}else {
			if (Selected_Month == 0)
			{
				Selected_Month = 11;
				Selected_Year--;
			}else {
				Selected_Month--;
			}
		}
	  	document.all.dateSelectYear.value = Selected_Year;
		document.all.dateSelectMonth.value = Selected_Month + 1;
	}
	Make_Calendar(Selected_Year, Selected_Month);
}

//根据输入的年份改变显示的日历
function changeYear(tp) {
	var Year = document.all.dateSelectYear.value;
	if(tp=="+")
	{	//后翻一年
		Make_Calendar(Year - 0 + 1, document.all.dateSelectMonth.value - 0 -1);
	}else if(tp=="-"){
		//前翻一年
		Make_Calendar(Year - 0 - 1, document.all.dateSelectMonth.value - 0 -1);
	}else{
		//文本框输入
		if(Year.length < 4)
		{
	  		alert("请输入完整的年份！");
	  		document.all.dateSelectYear.focus();
			document.all.dateSelectYear.select();
			return;
		}else if(Year.length == 4) {
			Make_Calendar(Year - 0, document.all.dateSelectMonth.value - 0 -1);
		}
	}
}

//显示日历

function showDatePickerWithIFrame(withTime,obj,defaultValue,y,x){
   riliShow(withTime,obj,defaultValue,y,x);
   var DivRef = document.getElementById('dateSelectDiv');
   var IfrRef = document.getElementById('dateSelectDivFrame');
   if(DivRef.style.display != "block") {  
     if(IfrRef == null){
	  IfrRef = document.createElement("<iframe id='dateSelectDivFrame' scrolling='no' frameborder='0' style='position:absolute; top:0px; left:0px; display:none;' src='../publicJsp/none.jsp'></iframe>")
	  document.body.appendChild(IfrRef);
     }
     DivRef.style.display = "block";
     IfrRef.style.width = DivRef.offsetWidth;
     IfrRef.style.height = DivRef.offsetHeight;
     IfrRef.style.top = DivRef.style.top;
     IfrRef.style.left = DivRef.style.left;
     IfrRef.style.zIndex = 999;
     IfrRef.style.display = "block";
   }else{
     closeDateSelectDiv();
   }
}


function riliShow() {
	/*
		tp：标志	0－显示时间	1－不显示时间
		obj: 目标对象
		params：参数，可以是数组或字符串(用于存放输入框内的信息，从而弹出窗口将显示该信息，不然显示的为当前日期)
			字符串时以 “－” 进行分割
		dstop：window.event.y
		dsleft：window.event.x
	*/

	tp = arguments[0];
	obj = arguments[1];
	params = arguments[2];
	dstop = arguments[3];
	dsleft = arguments[4];

	if(obj!=null){
		this.myObj = obj;
	}
	if(document.all.dateSelectDiv.style.visibility == "hidden")
	{
		var today = new Date();
		var showTop,showLeft;
		if(dstop==null)	showTop = window.event.y + document.body.scrollTop ;
		else showTop = dstop - 0;
		if(dsleft==null)	showLeft = window.event.x + document.body.scrollLeft - 200;
		else showLeft = dsleft - 0;
		document.all.dateSelectDiv.style.left = showLeft;
		document.all.dateSelectDiv.style.top = showTop;
		document.all.dateSelectDiv.style.visibility = "visible";

		if(tp==1)
		{
			dateSelectInputTime.style.display = "none";
		}else{
			dateSelectInputTime.style.display = "";
		}

		if(params==null)
		{
			/*
			document.all.dateSelectHour.value = (today.getHours()<10?"0"+today.getHours():today.getHours()+"");
			var myMinutes = (today.getMinutes()<10?"0"+today.getMinutes():today.getMinutes()+"");
			var tp = document.getElementById("dateSelectMinute");
				myMinutes = (myMinutes - myMinutes%5);
			var ep = (myMinutes<10?"0"+myMinutes:myMinutes+"");
			tp.value = ep;
			*/
			document.all.dateSelectHour.value = (today.getHours()<10?"0"+today.getHours():today.getHours()+"");
			document.all.dateSelectMinute.value = (today.getMinutes()<10?"0"+today.getMinutes():today.getMinutes()+"");
			document.all.dateSelectSecond.value = (today.getSeconds()<10?"0"+today.getSeconds():today.getSeconds()+"");

			Make_Calendar(today.getFullYear(),today.getMonth());
		}else if(params.constructor==Array){
			//alert("Array\n"+params)
			var tyear,tmonth,tdate,thour,tminute,tsecond;
			if(params.length>0)
			{
				if(params[0].length==4)	tyear = params[0] - 0;
				else	tyear = today.getFullYear();
			}
			if(params.length>1){
				if(params[1].length==2)	tmonth = params[1] - 0 - 1;
				else	tmonth = today.getMonth();
			}
			if(params.length>2){
				if(params[2].length==2)	tdate = params[2] - 0;
				else	tdate = today.getDate();
			}
			if(params.length>3){
				if(params[3].length==2)	thour = params[3] - 0;
				else	thour = today.getHours();
			}
			if(params.length>4){
				if(params[4].length==2)	tminute = params[4] - 0;
				else	tminute = today.getMinutes();
			}
			if(params.length>5){
				if(params[5].length==2)	tsecond = params[5] - 0;
				else	tsecond = today.getSeconds();
			}
			//设置小时
			var tp = document.getElementById("dateSelectHour");
			var ep = (thour<10?"0"+thour:thour+"");
			tp.value = ep;
			//设置分钟
			tp = document.getElementById("dateSelectMinute");
			//tminute = (tminute - tminute%5);
			ep = (tminute<10?"0"+tminute:tminute+"");
			tp.value = ep;

			//设置秒
			tp = document.getElementById("dateSelectSecond");
			//tsecond = (tsecond - tsecond%5);
			ep = (tsecond<10?"0"+tsecond:tsecond+"");
			tp.value = ep;

			Make_Calendar(tyear,tmonth);
		}else if(params.constructor==String){
			//alert("String\n"+params)
			var tpa = params.split("-");
			var tyear,tmonth,tdate,thour,tminute;
			if(tpa.length>0)
			{
				if(tpa[0].length==4)	tyear = tpa[0] - 0;
				else	tyear = today.getFullYear();
			}else	tyear = today.getFullYear();
			if(tpa.length>1){
				if(tpa[1].length==2|tpa[1].length==1)	tmonth = tpa[1] - 0 - 1;
				else	tmonth = today.getMonth();
			}else	tmonth = today.getMonth();
			if(tpa.length>2){
				if(tpa[2].length==2|tpa[2].length==1)	tdate = tpa[2] - 0;
				else	tdate = today.getDate();
			}else	tdate = today.getDate();
			if(tpa.length>3){
				if(tpa[3].length==2|tpa[3].length==1)	thour = tpa[3] - 0;
				else	thour = today.getHours();
			}else	thour = today.getHours();
			if(tpa.length>4){
				if(tpa[4].length==2|tpa[4].length==2)	tminute = tpa[4] - 0;
				else	tminute = today.getMinutes();
			}else	tminute = today.getMinutes();
			if(tpa.length>5){
				if(tpa[5].length==2|tpa[5].length==2)	tsecond = tpa[5] - 0;
				else	tsecond = today.getSeconds();
			}else	tsecond = today.getSeconds();

			var tp = document.getElementById("dateSelectHour");
			var ep = (thour<10?"0"+thour:thour+"");
			tp.value = ep;

			tp = document.getElementById("dateSelectMinute");
			//tminute = (tminute - tminute%5);
			ep = (tminute<10?"0"+tminute:tminute+"");
			tp.value = ep;

			tp = document.getElementById("dateSelectSecond");
			//tsecond = (tsecond - tsecond%5);
			ep = (tsecond<10?"0"+tsecond:tsecond+"");
			tp.value = ep;

			Make_Calendar(tyear,tmonth);
		}
	}else{
		document.all.dateSelectDiv.style.visibility = "hidden";

	}
}

//获取选取的时间
function getSelectTime(ele)
{
	setSelectTime(ele);
	closeDateSelectDiv();
	
}

//缺省的设置时间
function setSelectTime(ele){

	var year = document.all.dateSelectYear.value;
	var year_2 = new String(year.substr(2,2));		//两位年
	var month = document.all.dateSelectMonth.value;
	var day = (ele.innerHTML.length<2?"0"+ele.innerHTML:ele.innerHTML);
	var tp = document.getElementById("dateSelectHour");
	var hour = tp.value;

	tp = document.getElementById("dateSelectMinute");
	var minute = tp.value;

	tp = document.getElementById("dateSelectSecond");
	var second = tp.value;

	dateformat = this.myObj.getAttribute("dateformat");

	switch (dateformat) {
		case "YYYY-MM-DD HH:MI:SS" :
			vData =  year + "-" + month + "-" + day + " "+hour+":"+minute+":"+second;
			break;
		case "YYYY-MM-DD HH:MI" :
			vData =  year + "-" + month + "-" + day + " "+hour+":"+minute;
			break;
		case "YYYY-MM-DD" :
			vData =  year + "-" + month + "-" + day;
			break;
		case "YYYY-MM" :
			vData =  year + "-" + month;
			break;

		case "YYYY" :
			vData =  year;
			break;
		case "YYYYMM" :
			vData = year+ month;
			break;
		case "YYYYMMDD" :
			vData = year+ month + day;
			break;
		case "YYYYMMDD HH:MI" :
			vData = year+ month + day+ " "+hour+":"+minute;
			break;
		case "YYYYMMDD HH:MI:SS" :
			vData = year+ month + day+ " "+hour+":"+minute+":"+second;
			break;

		case "MM-DD-YYYY HH:MI:SS" :
			vData = month + "-" + day + "-" + year+ " "+hour+":"+minute+":"+second;
			break;
		case "MM-DD-YYYY" :
			vData = month + "-" + day + "-" + year;
			break;

		case "DD-MM-YYYY HH:MI:SS" :
			vData = day + "-" + month + "-" + year+ " "+hour+":"+minute+":"+second;
			break;
		case "DD-MM-YYYY" :
			vData = day + "-" + month + "-" + year;
			break;

		default :
			vData =  year + "-" + vMonth + "-" + day;
		}
		myObj.value=vData;
}

function closeDateSelectDiv()
{
	document.all.dateSelectDiv.style.visibility = "hidden";
	
	document.all.dateSelectDiv.style.display = "none";
        var IfrRef = document.getElementById('dateSelectDivFrame');
   	if(IfrRef != null)
	  IfrRef.style.display = "none";

}

//校验年份
function yearChecked(ele)
{
	var temp = ele.value;
	if(temp=="")
	{
		alert("请输入年份！");
		ele.focus();
		ele.select();
		return;
	}
	if(temp.length!=4)
	{
		alert("请输入4位数字的年份！");
		ele.focus();
		ele.select();
		return;
	}
	var ep = temp.match(/\D/g);
	if(ep!=null)
	{
		alert("输入的年份应为正整数！");
		ele.focus();
		ele.select();
		return;
	}
	changeYear();
}


function beginDrag(elementToDrag,event)  {

	//计算元素原左上角与鼠标的距离
	//moveHandler要这值

	var delatX=event.clientX-parseInt(elementToDrag.style.left);
	var delatY=event.clientY-parseInt(elementToDrag.style.top);

	//注册响应mousemove和mousedown事件后的mouseup事件的处理程序

	if(document.addEventListener) {  //2级DOM事件模型
	//注册捕捉事件处理程序。
	document.addEventListener("mousemove",moveHandler,true);
	document.addEventListener("mouseup",upHandler,true);
	}
	else if(document.attachEvent) { //IE5+ 的事件模型
	//在IE事件模型中，我们不能捕捉事件，所以只有当事件起泡到这些处理程序时,
	//它们才被触发. 假设不存在干涉元素, 处理了事件后它们就停止传播
	document.attachEvent("onmousemove",moveHandler);
	document.attachEvent("onmouseup",upHandler);
	}
	else { //IE4事件模型
	//IE4我们不能使用attachEvent方法,所以存储了以前赋予的处理
	//程序后,直接赋予新的事件处理程序,这样可以恢复旧的处理程序.
	//注意,这样依赖于事件起泡.
	var oldmovehandler=document.onmousemove;
	var olduphandler=document.onmouseup;
	document.onmousemove=moveHandler;
	document.onmouseup=upHandler;
	}

	//我们处理了该事件,不要再让其他元素看见.
	if(event.stopPropagation) event.stopPropagation(); //2 级DOM
	else event.cancelBubble=true;	//IE

	//下面禁止执行默认动作
	if(event.preventDefault) event.preventDefault();  //2级DOM
	else event.returnValue=false;	//IE

	/*  这是元素被拖动时捕捉mousemove事件的处理程序.
	 *  它负责移动元素
	 */

	function moveHandler(e)  {
	if(!e) e=window.event;	//IE事件模型;
	//把元素移动到鼠标当前的位置,根据初始鼠标点击的偏移量进行调整
	elementToDrag.style.left=(e.clientX-delatX)+"px";
	elementToDrag.style.top=(e.clientY-delatY)+"px";
	//不要再让其他元素看到该事件.
	if(e.stopPropagation) e.stopPropagation();  //2级DOM
	else e.cancelBubble=true;	//IE
	}

	/*  这是捕捉拖移结束最后发生的mouseup事件的处理程序.
	 */
	function upHandler(e) {
	if(!e) e=window.event;	//IE事件模型.
	//注销捕捉事件程序.
	if(document.removeEventListener) {	//DOM事件模型
	document.removeEventListener("mouseup",upHandler,true);
	document.removeEventListener("mousemove",moveHandler,true);
	}
	else if(document.detachEvent) {	//IE5+ 事件模型
	document.detachEvent("onmouseup",upHandler);
	document.detachEvent("onmousemove",moveHandler);
	}
	else  {	//IE事件模型
	document.onmouseup=olduphandler;
	document.onousemove=oldmovehandler;
	}
	//不要再让事件进一步传播.
	if(e.stopPropagation) e.stopPropagation();  //2级DOM
	else e.cancelBubble=true;	//IE
   }
}
