var gdCtrl = new Object();
var goSelectTag = new Array();
var gcGray = "#ffffff";
var gcToggle = "#999999";//font
var gcBG = "#e3ffcf";
var d = new Date();
var giYear = d.getFullYear();
var giMonth = d.getMonth()+1;
var giDay = d.getDate();

//****************************************************************************
// Param: 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" id=button1 name=button1>
//****************************************************************************
function fPopCalendar(popCtrl, dateCtrl){
	event.cancelBubble=true;
  	gdCtrl = dateCtrl;
 
  	if(gdCtrl.value!="")
  	{
	
	  	var tempDate=gdCtrl.value.toString();
	  	var tempMonth=tempDate.charAt(0)+tempDate.charAt(1);
		giMonth=parseInt(tempMonth);
		var tempYear=tempDate.charAt(gdCtrl.value.length-2)+tempDate.charAt(gdCtrl.value.length-1);
		if(tempDate.charAt(gdCtrl.value.length-2)=="9")// determine if /9 or /0 for year
		{	
			var strYear=tempDate.charAt(gdCtrl.value.length-2)+tempDate.charAt(gdCtrl.value.length-1)
			var strtempYear=("19".concat(strYear));
			giYear=parseInt(strtempYear);
		}
		else
		{
			var strYear=tempDate.charAt(gdCtrl.value.length-2)+tempDate.charAt(gdCtrl.value.length-1)
			var strtempYear=("20".concat(strYear));
			giYear=parseInt(strtempYear);
		}
	}
  	fSetYearMon(giYear, giMonth);// set the values of the selection boxes to today
  
  	var point = fGetXY(popCtrl);
  	with (DivPopCal.style) {
  		left = point.x;
		top  = point.y+popCtrl.offsetHeight+1;
		width = DivPopCal.offsetWidth;
		height = DivPopCal.offsetHeight;
		fToggleTags(point);
		visibility = 'visible';
  	}  
  	DivPopCal.focus();
}

function fResetDate(){
	var d = new Date();
	fSetDate(d.getFullYear(), d.getMonth()+1, d.getDate());
}

function fSetDate(iYear, iMonth, iDay){
	gdCtrl.value =iMonth+"/"+iDay+"/"+iYear; //Here, you could modify the locale as you need !!!!  
  	fHideCalendar();
}

function fHideCalendar(){
  DivPopCal.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 = (ipos<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, iCellWidth, iCellHeight, iTextSize) {
	with (document) {
  		for (var i = 0; i < 6; i++) {
			write("<tr>");
			for (var j = 0; j < 7; j++) {
				write("<td id=calCell bgcolor='"+gcBG+"' align='center' width='"+iCellWidth+"' height='"+iCellHeight+"' style='font:bold "+iTextSize+" Arial;cursor:hand;' onMouseOver='this.bgColor=gcToggle' onMouseOut='this.bgColor=gcBG' onclick='fSetSelected(this)'>");
				write("<font id=cellText ipos=''> </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]) {
			ipos = i++;
			if (myMonth[w+1][d]<0) {
				color = gcGray;
				innerText = -myMonth[w+1][d];
			}else{
				color = "black";
				if (d==0) color = "red";
				if (d==6) color = "blue";
				innerText = myMonth[w+1][d];
			}
		}
}

function fSetYearMon(iYear, iMon){
  	var yy = 2000;
	tbSelMonth.options[iMon-1].selected = true;  	
	tbSelYear.options[iYear-yy].selected = true;
  	fUpdateCal(iYear, iMon);
}

function fPrevMonth(){
  var iMon = tbSelMonth.value;
  var iYear = tbSelYear.value;

  if (--iMon<1) {
	  iMon = 12;
	  iYear--;
  }
  if (iYear < 2000)
  	iYear++;
  fSetYearMon(iYear, iMon);
}

function fNextMonth(){
  var iMon = tbSelMonth.value;
  var iYear = tbSelYear.value;

  if (++iMon>12) {
	  iMon = 1;
	  iYear++;
  }
  if (iYear > 2080)
  	iYear--;
  fSetYearMon(iYear, iMon);
}

function fToggleTags(){
  with (document.all.tags("SELECT")){
 	for (i=0; i<length; i++)
 		if ((item(i).ipos!="Won")&&fTagInBound(item(i))){
 			item(i).style.visibility = "hidden";
 			goSelectTag[goSelectTag.length] = item(i);
 		}
  }
}

function fTagInBound(aTag){
  with (DivPopCal.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;
}

//var gMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var gMonths = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var WeekDay = new Array("Su","Mo","Tu","We","Th","Fr","Sa");

with (document) {

	write("<div id='DivPopCal' onclick='event.cancelBubble=true' style='POSITION:absolute;visibility:hidden;border:2px ridge;width:10;z-index:100'>");
	write("<table border='0' bgcolor='#ffffff'>");
	write("<tr>");
	write("<td valign='middle' align='center'><nobr><a href='#' onClick='fPrevMonth()' title='Previous Month'><img src='images/arrow_left.gif' border='0'></a>");
	write("&nbsp;<select name='tbSelMonth' onChange='fUpdateCal(tbSelYear.value, tbSelMonth.value)' ipos='Won'>");
	for (i=0; i<12; i++)
		write("<option value='"+(i+1)+"'>"+gMonths[i]+"</option>");
	write("</SELECT>");
	write("&nbsp;<SELECT name='tbSelYear' onChange='fUpdateCal(tbSelYear.value, tbSelMonth.value)' ipos='Won'>");
	var y=2000;
	for(i=y;i<y+12;i++)
		write("<OPTION value='"+i+"'>"+i+"</OPTION>");
	write("</SELECT>");
	write("&nbsp;<a href='#' onClick='fNextMonth()' title='Next Month'><img src='images/arrow_right.gif' border='0'></a>");
	write("</nobr></td>");
	write("</tr><tr>");
	write("<td align='center'>");
	write("<TABLE STYLE='border:solid 1px; cell-padding:2; cell-spacing:2'>");
	write("<tr>");
	for(i=0; i<7; i++)
		write("<td><font size='2' face='Verdana'>" + WeekDay[i] + "</font></td>");
	write("</tr>");
//	write("<tr><td colspan='7' height='1px' width='100%' bgcolor='#ff0000'></td></tr>");
	fDrawCal(giYear, giMonth, 10, 10, 10);
	write("</table>");
	write("</td>");
	write("</tr><tr><td align='center'>");
	write("<span onclick='fResetDate()' style='cursor:hand;font-family:Verdana'><img src='images/btn_today.gif'></span>");
	write("</td></tr>");
	write("<tr><td align='center'>");
	write("<a href='javascript:fHideCalendar()'>Close Calendar</a>");
	write("</td></tr>");
	write("</table></div>");

}
