﻿//add by dong qifei 2005/07/21
//当前日期
//<input type="text" size="10" name="datbegin" value="<%=formatdatetime(dateandtime_start,2)%>" maxlength="10"   readonly  onclick="fpopcalendar(datbegin,datbegin);return false">日<%h_time(int(hour(dateandtime_start))),23,"dathourbegin"%>时<%h_time(int(minute(dateandtime_start))),59,"datminutebegin"%>分(日期格式:yyyy-mm-dd)
function getnowdate()
{
   var nn=new date();
   year1=nn.getyear();
   mon1=nn.getmonth()+1;
   date1=nn.getdate();
   var monstr1;
   var datestr1
   if(mon1<10) 
    monstr1="0"+mon1;
   else
    monstr1=""+mon1;
     
   if(date1<10) 
     datestr1="0"+date1;
   else
     datestr1=""+date1;
   return year1+"-"+monstr1+"-"+datestr1;
}
//目标日期
function getlastweekdate()
{
   var nn=new date();
   year1=nn.getyear();
   mon1=nn.getmonth()+1;
   date1=nn.getdate();
   
   var mm=new date(year1,mon1-1,date1);
   var tmp1=new date(2000,1,1);
   var tmp2=new date(2000,1,15);
   var ne=tmp2-tmp1;
   var mm2=new date();
   mm2.settime(mm.gettime()-ne);
   
   
   year2=mm2.getyear();
   mon2=mm2.getmonth()+1;
   date2=mm2.getdate();
    
    
     if(mon2<10) 
    monstr2="0"+mon2;
   else
    monstr2=""+mon2;
     
   if(date2<10) 
     datestr2="0"+date2;
   else
     datestr2=""+date2;
 
   
    return year2+"-"+monstr2+"-"+datestr2;
}

function gettime()
{
   var date = new date();
   var hour = date.gethours();
   var minute = date.getminutes();
   var senond = date.getseconds();
   if(hour<10){
   	hour = "0"+hour;	
   }
   if(minute<10){
   	minute = "0"+minute;	
   }
   if(senond<10){
   	senond = "0"+senond;	
   }

    return hour+":"+minute+":"+senond;
}

var needtime = false;
var gdctrl = new object();
var goselecttag = new 
array();
var gcgray   = "#808080";
var gctoggle = "#fb8664";
var gcbg = 
"#e5e6ec";
var previousobject = null;
var gdcurdate = new date();
var giyear = gdcurdate.getfullyear();
var gimonth = gdcurdate.getmonth()+1;
var giday = gdcurdate.getdate();

function fsetdate(iyear, imonth, iday){
  vicpopcal.style.visibility = "hidden";
  if ((iyear == 0) && (imonth == 0) && (iday == 0)){
  	gdctrl.value = "";
  }else{
  	imonth = imonth + 100 + "";
  	imonth = imonth.substring(1);
  	iday   = iday + 100 + "";
  	iday   = iday.substring(1);
  	if(gdctrl.tagname == "input"){
  		if(needtime){
  	  		gdctrl.value = iyear+"-"+imonth+"-"+iday+" "+gettime();
  	  	} else {
  	  		gdctrl.value = iyear+"-"+imonth+"-"+iday;
  	  	}
  	}else{
  		if(needtime){
  	  		gdctrl.innertext = iyear+"-"+imonth+"-"+iday+" "+gettime();
  	  	} else{
  	  	        gdctrl.innertext = iyear+"-"+imonth+"-"+iday;
  	  	}
  	}
  }
  
  for (i in goselecttag)
  	goselecttag[i].style.visibility = "visible";
  goselecttag.length = 0;
  
  window.returnvalue=gdctrl.value;
  //window.close();

}

function hiddendiv()
{
	var i;
  vicpopcal.style.visibility = "hidden";
  for (i in goselecttag)
  	goselecttag[i].style.visibility = "visible";
  goselecttag.length = 0;

}
function fsetselected(acell){
  var ioffset = 0;
  var iyear = parseint(tbselyear.value);
  var imonth = parseint(tbselmonth.value);
  
  acell.bgcolor = gcbg;
  with (acell.children["celltext"]){
  	var iday = parseint(innertext);
  	if (color==gcgray)
		ioffset = (victor<10)?-1:1;
	imonth += ioffset;
	if (imonth<1) {
		iyear--;
		imonth = 12;
	}else if (imonth>12){
		iyear++;
		imonth = 1;
	}
  }
  fsetdate(iyear, imonth, iday);
}

function point(ix, iy){
	this.x = ix;
	this.y = iy;
}

function fbuildcal(iyear, imonth) {
  var amonth=new array();
  for(i=1;i<7;i++)
  	amonth[i]=new array(i);
  
  var dcaldate=new date(iyear, imonth-1, 1);
  var idayoffirst=dcaldate.getday();
  var idaysinmonth=new date(iyear, imonth, 0).getdate();
  var ioffsetlast=new date(iyear, imonth-1, 0).getdate()-idayoffirst+1;
  var idate = 1;
  var inext = 1;

  for (d = 0; d < 7; d++)
	amonth[1][d] = (d<idayoffirst)?-(ioffsetlast+d):idate++;
  for (w = 2; w < 7; w++)
  	for (d = 0; d < 7; d++)
		amonth[w][d] = (idate<=idaysinmonth)?idate++:-(inext++);
  return amonth;
}

function fdrawcal(iyear, imonth, icellheight, sdatetextsize) {
  var weekday = new array("日","一","二","三","四","五","六");
  var styletd = " bgcolor='"+gcbg+"' bordercolor='"+gcbg+"' valign='middle' align='center' height='"+icellheight+"' style='font:bold arial "+sdatetextsize+";";            //coded by hcy email:hcy110@263.net

  with (document) {
	write("<tr>");
	for(i=0; i<7; i++){
		write("<td "+styletd+"color:maroon' >"+ weekday[i] + "</td>");
	}
	write("</tr>");

  	for (w = 1; w < 7; w++) {
		write("<tr>");
		for (d = 0; d < 7; d++) {
			write("<td id=calcell "+styletd+"cursor:hand;' onmouseover='this.bgcolor=gctoggle' onmouseout='this.bgcolor=gcbg' onclick='fsetselected(this)'>");
			write("<font id=celltext victor='hcy_flag'> </font>");			
			write("</td>")
		}
		write("</tr>");
	}
  }
}

function fupdatecal(iyear, imonth) {
  mymonth = fbuildcal(iyear, imonth);
  var i = 0;
  for (w = 0; w < 6; w++)
	for (d = 0; d < 7; d++)
		with (celltext[(7*w)+d]) {
			victor = i++;
			if (mymonth[w+1][d]<0) {
				color = gcgray;
				innertext = -mymonth[w+1][d];
			}else{
				color = ((d==0)||(d==6))?"red":"black";
				innertext = mymonth[w+1][d];
			}
		}
}

function fsetyearmon(iyear, imon){
  tbselmonth.options[imon-1].selected = true;
  for (i = 0; i < tbselyear.length; i++)
	if (tbselyear.options[i].value == iyear)
		tbselyear.options[i].selected = true;
  fupdatecal(iyear, imon);
}

function fprevmonth(){
  var imon = tbselmonth.value;
  var iyear = tbselyear.value;
  
  if (--imon<1) {
	  imon = 12;
	  iyear--;
  }
  
  fsetyearmon(iyear, imon);
}

function fnextmonth(){
  var imon = tbselmonth.value;
  var iyear = tbselyear.value;
  
  if (++imon>12) {
	  imon = 1;
	  iyear++;
  }
  
  fsetyearmon(iyear, imon);
}

function ftoggletags(){
  with (document.all.tags("select")){
 	for (i=0; i<length; i++)
 		if ((item(i).victor!="won")&&ftaginbound(item(i))){
 			item(i).style.visibility = "hidden";
 			goselecttag[goselecttag.length] = item(i);
 		}
  }
}

function ftaginbound(atag){
  with (vicpopcal.style){
  	var l = parseint(left);
  	var t = parseint(top);
  	var r = l+parseint(width);
  	var b = t+parseint(height);
	var ptlt = fgetxy(atag);
	return !((ptlt.x>r)||(ptlt.x+atag.offsetwidth<l)||(ptlt.y>b)||(ptlt.y+atag.offsetheight<t));
  }
}

function fgetxy(atag){
  var otmp = atag;
  var pt = new point(0,0);
  do {
  	pt.x += otmp.offsetleft;
  	pt.y += otmp.offsettop;
  	otmp = otmp.offsetparent;
  } while(otmp.tagname!="body");
  return pt;
}

// main: popctrl is the widget beyond which you want this calendar to appear;
//       datectrl is the widget into which you want to put the selected date.
// i.e.: <input type="text" name="dc" style="text-align:center" readonly><input type="button" value="v" onclick="fpopcalendar(dc,dc);return false">
function show_cele_date(popctrl, needtime, null, datectrl, strdate){
  if (popctrl == previousobject){
	  	if (vicpopcal.style.visibility == "visible"){
  		hiddendiv();
  		return true;
  	}
  	
  }
  needtime = needtime;
  previousobject = popctrl;
  gdctrl = datectrl;
  finitialdate(strdate);
  fsetyearmon(giyear, gimonth); 
  var point = fgetxy(popctrl);
  with (vicpopcal.style) {
  	left = point.x;
	top  = point.y+popctrl.offsetheight;
	width = vicpopcal.offsetwidth;
	 //
	height = vicpopcal.offsetheight;
	ftoggletags(point); 	
	visibility = 'visible';
  }
}

// added by han chen
function finitialdate(strdate){
	if( strdate == null || strdate.length != 10 )
		return false;

	var syear  = strdate.substring(0,4);
	var smonth = strdate.substring(5,7);
	var sday   = strdate.substring(8,10);

	if( smonth.charat(0) == '0' ) { smonth = smonth.substring(1,2); }
	if( sday.charat(0)   == '0' ) { sday   = sday.substring(1,2);   }

	var nyear  = parseint(syear );
	var nmonth = parseint(smonth);
	var nday   = parseint(sday  );
	
	if ( isnan(nyear ) )	return false;
	if ( isnan(nmonth) )	return false;
	if ( isnan(nday  ) )	return false;

	var arrmon = new array(12);
	arrmon[ 0] = 31;	arrmon[ 1] = nyear % 4 == 0 ? 29:28;
	arrmon[ 2] = 31;	arrmon[ 3] = 30;
	arrmon[ 4] = 31;	arrmon[ 5] = 30;
	arrmon[ 6] = 31;	arrmon[ 7] = 31;
	arrmon[ 8] = 30;	arrmon[ 9] = 31;
	arrmon[10] = 30;	arrmon[11] = 31;

	if ( nyear  < 1900 || nyear > 2100 )			return false;
	if ( nmonth < 1 || nmonth > 12 )				return false;
	if ( nday < 1 || nday > arrmon[nmonth - 1] )	return false;

	giyear  = nyear;
	gimonth = nmonth;
	giday   = nday;
	return true;
}

var gmonths = new array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");

with (document) {
write("<div id='vicpopcal' style='position:absolute;visibility:hidden;border:1px ridge;z-index:100;padding:5;background-color:#efefef'>");
write("<table border='0' cellpadding=0 cellspacing=0 bgcolor='#cccccc'>");
write("<tr>");
write("<td valign='middle' align='center'><input type='button' name='prevmonth' value='<' style='height:20;width:20;font:bold' onclick='fprevmonth()'>");
write("&nbsp;<select name='tbselyear' onchange='fupdatecal(tbselyear.value, tbselmonth.value)' victor='won'>");
for(i=1950;i<2015;i++)
	write("<option value='"+i+"'>"+i+" 年</option>");
write("</select>");
write("&nbsp;<select name='tbselmonth' onchange='fupdatecal(tbselyear.value, tbselmonth.value)' victor='won'>");
for (i=0; i<12; i++)
	write("<option value='"+(i+1)+"'>"+gmonths[i]+"</option>");
write("</select>");
write("&nbsp;<input type='button' name='prevmonth' value='>' style='height:20;width:20;font:bold' onclick='fnextmonth()'>");
write("</td>");
write("</tr><tr>");
write("<td align='center'>");
write("<div style='background-color:teal'><table width='100%' border='0' cellpadding=0 cellspacing=1>");
fdrawcal(giyear, gimonth, 20, '12');
write("</table></div>");
write("</td>");
write("</tr><tr><td align='center'>");
write("<table width='100%'><tr><td align='center'>");
write("<b style='cursor:hand' onclick='fsetdate(0,0,0)' onmouseover='this.style.color=gctoggle' onmouseout='this.style.color=0'>清空</b>");
write("</td><td algin='center'>");
write("<b style='cursor:hand' onclick='fsetdate(giyear,gimonth,giday)' onmouseover='this.style.color=gctoggle' onmouseout='this.style.color=0'>今天: "+giyear+"-"+gimonth+"-"+giday+"</b>");
write("</td></tr></table>");
write("</td></tr>");
write("</table></div>");
}

function init(){}






















