var gdCtrl = new Object();
var goSelectTag = new Array();
var gcGray = "#e9e8e4";
var gcToggle = "#99d05f";
var gcBG = "#e9e8e4";	//cccccc
var gdCurDate = new Date();
var giYear = gdCurDate.getFullYear();
var giMonth = gdCurDate.getMonth() + 1;
var giDay = gdCurDate.getDate();
var showCal = 0;

function fSetDate( iYear, iMonth, iDay )
{
	VicPopCal2 = document.getElementById( "VicPopCal" );
	VicPopCal2.style.visibility = "hidden";
	sMonth = new String( iMonth );
	if( sMonth.length < 2 )
	{
		sMonth = '0' + sMonth;
	}
	sDay = new String( iDay );
	if( sDay.length < 2 )
	{
		sDay = '0' + sDay;
	}
	gdCtrl.value = iYear + "-" + sMonth + "-" + sDay; //Here, you could modify the locale as you need !!!!
	/*for( i in goSelectTag )
	{
		goSelectTag[i].style.visibility = "visible";
	}
	goSelectTag.length = 0;*/
}

function fSetSelected( aCell, number )
{
	var iOffset = 0;
	tbSelYear2 = document.getElementById( "tbSelYear" );
	tbSelMonth2 = document.getElementById( "tbSelMonth" );
	var iYear = parseInt( tbSelYear2.value );
	var iMonth = parseInt( tbSelMonth2.value );

	//self.event.cancelBubble = true;
	aCell.bgColor = gcBG;
	//aCell2 = aCell.childNodes;
	aCell2 = document.getElementById( "cellText"+number+"" );
	//with ( aCell.children["cellText"] )
	with( aCell2 )
	{
		//alert( "--" + aCell2.innerText + "--" );
		var iDay = parseInt( aCell2.innerHTML );
		if( aCell2.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, iDateTextSize )
{
	//var WeekDay = new Array( "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" );
	//var styleTD = ' bgcolor="'+gcBG+'" bordercolor="'+gcBG+'" style="font-weight: bold; font-size: '+iDateTextSize+'px; width: 35px; height: 25px; vertical-align: middle; text-align: center;';            //Coded by Liming Weng(Victor Won)  email:victorwon@netease.com
	var styleTD = ' style="background-color: '+gcBG+'; border: 0px; font-weight: bold; font-size: '+iDateTextSize+'px; width: 35px; height: 25px; vertical-align: middle; text-align: center;';            //Coded by Liming Weng(Victor Won)  email:victorwon@netease.com
	var styleTD2 = ' style="background-color: #96906c; border: 0px; font-weight: bold; font-size: '+iDateTextSize+'px; width: 35px; height: 25px; vertical-align: middle; text-align: center;';            //Coded by Liming Weng(Victor Won)  email:victorwon@netease.com
	with( document )
	{
		write( '<tr onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\'>' );
		for( i=0; i < 7; i++ )
		{
			write( '<td '+styleTD2+' color: #e5ded6;" onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\'>' );
			write( ''+WeekDay[i]+'</td>' );
		}
		write("</tr>");
		for( w = 0; w < 6; w++ )
		{
			write( '<tr style="background-color: #aaa483;" onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\'>' );
			for( d = 0; d < 7; d++ )
			{
				numCell = ( ( 7 * w ) + d );
				mouseOn = 'onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\' onclick="javascript: fSetSelected( this, '+numCell+' );"';// onMouseOver="javascript: this.style.backgroundColor=gcToggle;" onMouseOut="javascript: this.style.backgroundColor=gcBG;"
				write( '<td id="calCell'+numCell+'" '+styleTD+' cursor: pointer;" '+mouseOn+'>' );
				write( 	'<span id="cellText'+numCell+'" Victor="Liming Weng" style="color: #000000;" onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\'> </span>' );
				write( '</td>' )
			}
			write( "</tr>" );
		}
	}
}

function fUpdateCal()
{
	iYear = document.getElementById( "tbSelYear" ).value;
	iMonth = document.getElementById( "tbSelMonth" ).value;
	myMonth = fBuildCal( iYear, iMonth );
	var i = 0;
	for( w = 0; w < 6; w++ )
	{
		for( d = 0; d < 7; d++ )
		{
			//with( cellText[(7*w)+d] )
			//{
			num = ((7*w)+d);
			cellX = document.getElementById( "calCell"+num+"" )
			cellTextx = document.getElementById( "cellText"+num+"" )
			cellTextx.Victor = i++;
			if( myMonth[w+1][d] < 0 )
			{
				cellTextx.style.color = '#96906c';
				cellX.style.backgroundColor = '#aaa483';
				cellTextx.innerHTML = -myMonth[w+1][d];
			}
			else
			{
				cellTextx.style.color = ( ( d == 0 ) || ( d == 6 ) ) ? "#000000":"#96906c";
				cellX.style.backgroundColor = ( ( d == 0 ) || ( d == 6 ) ) ? "#ffffff":"#e9e8e4";
				cellTextx.innerHTML = myMonth[w+1][d];
			}
			//}
		}
	}
}

function fSetYearMon( iYear, iMon )
{
	tbSelMonth2 = document.getElementById( "tbSelMonth" );
	tbSelYear2 = document.getElementById( "tbSelYear" );
	tbSelMonth2.options[iMon-1].selected = true;
	for( i = 0; i < tbSelYear2.length; i++ )
	{
		if( tbSelYear2.options[i].value == iYear )
		{
			tbSelYear2.options[i].selected = true;
		}
	}
	fUpdateCal();
}

function fPrevMonth()
{
	tbSelMonth2 = document.getElementById( "tbSelMonth" );
	tbSelYear2 = document.getElementById( "tbSelYear" );
	var iMon = tbSelMonth2.value;
	var iYear = tbSelYear2.value;
	if( --iMon < 1 )
	{
		iMon = 12;
		iYear--;
	}
	fSetYearMon( iYear, iMon );
}

function fNextMonth()
{
	tbSelMonth2 = document.getElementById( "tbSelMonth" );
	tbSelYear2 = document.getElementById( "tbSelYear" );
	var iMon = tbSelMonth2.value;
	var iYear = tbSelYear2.value;
	if( ++iMon > 12 )
	{
		iMon = 1;
		iYear++;
	}
	fSetYearMon( iYear, iMon );
}

function fToggleTags()
{
	var agent = new String( navigator.userAgent );
	if( agent.indexOf("Opera") > -1 )
	{
		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);
				}
			}
			*/
		}
	}
	else
	{
		with( document.body.getElementsByTagName("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 )
{
	VicPopCal2 = document.getElementById( "VicPopCal" );
	with( VicPopCal2.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;
}

function fPopCalendar( popCtrl, dateCtrl )
{
	gdCtrl = dateCtrl;
	fSetYearMon( giYear, giMonth );
	var point = fGetXY( popCtrl );
	VicPopCal2 = document.getElementById( "VicPopCal" );
	with( VicPopCal2.style )
	{
		left = new String( point.x ) + 'px';
		top  = new String( point.y + popCtrl.offsetHeight + 1 ) + 'px';
		//width = VicPopCal2.offsetWidth;
		//height = VicPopCal2.offsetHeight;
		fToggleTags( point );
		visibility = 'visible';
	}
	document.getElementById( 'tbSelMonth' ).style.visibility = 'visible';
	document.getElementById( 'tbSelYear' ).style.visibility = 'visible';
	//alert( VicPopCal2.style.left + ' ' + VicPopCal2.style.top + ' ' + VicPopCal2.style.width + ' ' + VicPopCal2.style.height );
	tbSelMonth2 = document.getElementById( "tbSelMonth" );
	//VicPopCal2.focus();
	tbSelMonth2.focus();
}

function fHideCal()
{
	if( oE = window.event )
	{
		if( ( clX = oE.clientX ) && ( clY = oE.clientY ) )
		{
		//alert( "X="+clX+"; Y="+clY+";" );
		//var oE = window.event;
		//var oE = event;
		//if( ( oE.clientX > 0 ) && ( oE.clientY > 0 ) && ( oE.clientX < document.body.clientWidth ) && ( oE.clientY < document.body.clientHeight ) )
		//if( ( clX > 0 ) && ( clY > 0 ) && ( clX < document.body.clientWidth ) && ( clY < document.body.clientHeight ) )
		//{
			//var oTmp = document.elementFromPoint( event.clientX, event.clientY );
			if( oTmp = document.elementFromPoint( clX, clY ) )
			{
				while( ( oTmp.tagName != "BODY" ) && ( oTmp.id != "VicPopCal" ) )
				{
					oTmp = oTmp.offsetParent;
				}
				if( oTmp.id == "VicPopCal" )
				{
					return;
				}
			}
		}
		VicPopCal2 = document.getElementById( "VicPopCal" );
		VicPopCal2.style.visibility = "hidden";
		document.getElementById( 'tbSelMonth' ).style.visibility = 'hidden';
		document.getElementById( 'tbSelYear' ).style.visibility = 'hidden';
	}
	else if( showCal == 0 )
	{
		VicPopCal2 = document.getElementById( "VicPopCal" );
		VicPopCal2.style.visibility = "hidden";
		document.getElementById( 'tbSelMonth' ).style.visibility = 'hidden';
		document.getElementById( 'tbSelYear' ).style.visibility = 'hidden';
	}
	//}
	//VicPopCal2 = document.getElementById( "VicPopCal" );
	//VicPopCal2.style.visibility = "hidden";
	/*for( i in goSelectTag )
	{
		goSelectTag[i].style.visibility = "visible";
	}
	goSelectTag.length = 0;*/
}

//var gMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

with( document )
{
	//write( "<div name='VicPopCal' id='VicPopCal' onblur='fHideCal();' onclick='focus();' style='position: absolute; visibility: hidden; border: 2px ridge #cccccc; width: 200px;'>" );
	write( '<table border="0" bgcolor="#96906c" name="VicPopCal" id="VicPopCal" onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\' onclick=\'javascript: if( this.style.visibility != "hidden" ){ focus(); }\' style="position: absolute; visibility: hidden; border: 1px solid #585439;">' );
	write( '<tr onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\'>' );
	write( 	'<td style="vertical-align: middle; text-align: center;" onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\'>' );
	write( 		'<input type="button" name="PrevMonth" value="&lt;" style="height: 20px; width: 20px; font-family: Fixedsys; font-size: 16px; background-color: #aaa483; color: #585439; border: 1px solid #585439;" onClick="javascript: fPrevMonth();" onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\'>' );
	write( 		'&nbsp;&nbsp;<select style="font-size: 90%;" name="tbSelMonth" id="tbSelMonth" onChange="javascript: fUpdateCal();" Victor="Won" onclick="javascript: event.cancelBubble=true;" onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\'>' );
	for ( i = 0; i < 12; i++ )
	{
		write(  	"<option style='font-size: 90%;' value='"+( i + 1 )+"'>"+gMonths[i]+"</option>" );
	}
	write( 		'</select>' );
	write( 		'&nbsp;&nbsp;<select style="font-size: 90%;" name="tbSelYear" id="tbSelYear" onChange="javascript: fUpdateCal();" Victor="Won" onclick="javascript: event.cancelBubble=true;" onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\'>' );
	for( i = 1995; i < 2021; i++ )
	{
		write( 		"<option style='font-size:90%;' value='"+i+"'>&nbsp;&nbsp;"+i+"&nbsp;&nbsp;</option>" );
	}
	write( 		'</select>' );
	write( 		'&nbsp;&nbsp;<input type="button" name="PrevMonth" value="&gt;" style="height: 20px; width: 20px; font-family: Fixedsys; font-size: 16px; background-color: #aaa483; color: #585439; border: 1px solid #585439;" onclick="javascript: fNextMonth();" onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\'>' );
	write( 	'</td>' );
	write( '</tr>' );
	write( '<tr onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\'>' );
	write( 	'<td style="text-align: center;" onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\'>' );
	write( 		'<div style="background-color: #96906c;" onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\'>' );
	write( 			'<table border="0" onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\'>' );
	fDrawCal( giYear, giMonth, 15, 10 );
	write( 			'</table>' );
	write( 		'</div>' );
	write( 	'</td>' );
	write( '</tr>' );
	write( '<tr onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\'>' );
	write( 	'<td style="text-align: center; color: #e5ded6;" onfocus="javascript: showCal=1;" onblur=\'javascript: showCal=0; window.setTimeout( "fHideCal()", 100 );\'>' );
	write( 		'<span style="cursor: pointer; font-size: 10px; color: #e5ded6;" onclick="javascript: fSetDate( giYear, giMonth, giDay); self.event.cancelBubble=true;" onMouseOver="javascript: this.style.color=\'#ffffff\';" onMouseOut="javascript: this.style.color=\'#e5ded6\';">' );
	write( 		'Dzisiaj:&nbsp;&nbsp;' + gMonths[giMonth-1] + '&nbsp;' + giDay + ',&nbsp;&nbsp;' + giYear );
	write( 		'</span>' );
	write( 	'</td>' );
	write( '</tr>' );
	write( '</table>' );
	/*write( "</div>" );*/
	document.getElementById( 'tbSelMonth' ).style.visibility = 'hidden';
	document.getElementById( 'tbSelYear' ).style.visibility = 'hidden';
}
