﻿/**
*   @version $Id: Calendar.js 23 2008-11-03 10:56:28Z paulinad $
*	@package frontend
*/
var oDate = new Date();
var eventPopTime = 0;

//Coded by Liming Weng(Victor Won)  email:victorwon@netease.com
//Modified by Piotr Jelinski
var gdCtrl = new Object();
var goSelectTag = new Array();
var gdCurDate = new Date();
var currYear = gdCurDate.getFullYear();
var currMonth = gdCurDate.getMonth() + 1;
var currDay = gdCurDate.getDate();
var showCal = 0;

var WeekDay = new Array( 'Nd', 'Pon', 'Wt', 'Śr', 'Cz', 'Pt', 'So' );
var gMonths = new Array( 'Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec', 'Sierpień', 'Wrzesień', 'Październik', 'Listopad', 'Grudzień' );
var DZISIAJ = 'Dzisiaj';
var calMinYear = oDate.getFullYear();
var calMaxYear = oDate.getFullYear()+10;
var giYear = oDate.getYear();
var giMonth = oDate.getMonth()+1;
var giDay = oDate.getDate();

var calColorTab = new Array( Array(8), Array(8), Array(8), Array(8), Array(8), Array(8) );
//var calColorTabOver = new Array( Array(7), Array(7), Array(7), Array(7), Array(7), Array(7) );
var calBgColorTab = new Array( Array(8), Array(8), Array(8), Array(8), Array(8), Array(8) );
var calBgColorTabOver = new Array( Array(8), Array(8), Array(8), Array(8), Array(8), Array(8) );
var calMainColorRGB = 'rgb(117, 132, 153)';
var calMainColor = '#3658a4';
var calMainBg = '#191865';
var calChosenColor = '#c5e4ff';
var calChosenBg = '#234195';
var calOverChosenColor = '#ffffff';
var calOverChosenBg = '#1280cf';
var calWeekendColor = '#c5e4ff';
var calWeekendBg = '#3f60a8';
var calOverWeekendColor = '#ffffff';
var calOverWeekendBg = '#1280cf';
var calcBg = '#191865';
var calcHeader = '#c7c6e8';
var calcTableBorder = '#191865';
var onBlurEvent = 'onblur="javascript: showCal=0; hideCalTime = window.setTimeout( \'fHideCal()\', 200 );"';
var onFocusEvent = 'onfocus="javascript: showCal=1; clearTimeout( hideCalTime );"';
var hideCalTime = 0;
var dayNumberCal = Array( 0, 1, 2, 3, 4, 5, 6, 0 );

function setCalColors()
{
	var i, j;
	for( i = 0; i < 6; i++ )
	{
		for( j = 1; j < 8; j++ )
		{
			calColorTab[i][j] = calMainColor;
			//calColorTabOver[i][j] = calMainColor;
			calBgColorTab[i][j] = calMainBg;
			calBgColorTabOver[i][j] = calMainBg;
		}
	}
}

function fSetDate( iYear, iMonth, iDay )
{
	VicPopCal2 = $( "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;
	giYear = iYear;
	giMonth = sMonth;
	giDay = sDay;
}

function fSetSelected( aCell, number )
{
	var iOffset = 0;
	tbSelYear2 = $( "tbSelYear" );
	tbSelMonth2 = $( "tbSelMonth" );
	var iYear = parseInt( tbSelYear2.value );
	var iMonth = parseInt( tbSelMonth2.value );

	aCell2 = $( "cellText"+number+"" );
	with( aCell2 )
	{
		var iDay = parseInt( aCell2.innerHTML );
		if( ( aCell2.style.color == calMainColor ) || ( aCell2.style.color == calMainColorRGB ) )
		{
			iOffset = ( Victor < 10 ) ? -1:1;
		}
		iMonth += iOffset;
		if( iMonth < 1 )
		{
			iYear--;
			iMonth = 12;
		}
		else if( iMonth > 12 )
		{
			iYear++;
			iMonth = 1;
		}
	}
	fSetDate( iYear, iMonth, iDay );
	showCal = 0;
	fHideCal();
}

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(8);
	}
	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;
	//var testC = '';
	for( d = 1; d < 8; d++ )
	{
		if( iDayOfFirst == 0 )
		{
			aMonth[1][d] = ( d < 7 ) ? -(iOffsetLast+d-7):iDate++;;
		}
		else
		{
			aMonth[1][d] = ( d < iDayOfFirst ) ? -(iOffsetLast+d):iDate++;
		}
		//testC += 'aMonth[1][d]='+aMonth[1][d]+';\n';
	}
	//alert( 'iDayOfFirst='+iDayOfFirst+'; iOffsetLast='+iOffsetLast+'; iDaysInMonth='+iDaysInMonth+';\n'+testC );
	for( w = 2; w < 7; w++ )
	{
		for( d = 1; d < 8; d++ )
		{
			aMonth[w][d] = ( iDate <= iDaysInMonth ) ? iDate++:-(iNext++);
		}
	}
	return aMonth;
}

function fDrawCal( iYear, iMonth, iCellHeight, iDateTextSize )
{
	var toRet = '';
	var styleTD = ' style="background-color: '+calMainBg+'; font-size: '+iDateTextSize+'px; font-weight: bold; width: 35px; height: 25px; vertical-align: middle; text-align: center;';
	var styleTD2 = ' style="background-color: '+calcBg+'; font-size: '+iDateTextSize+'px; font-weight: bold; width: 35px; height: 25px; vertical-align: middle; text-align: center;';
	with( document )
	{
		var mouseOn, numCell, i, d, w;
		toRet += '<tr '+onFocusEvent+' '+onBlurEvent+'>';
		for( i = 1; i < 8; i++ )
		{
			toRet += '<td '+styleTD2+' color: '+calcHeader+';" '+onFocusEvent+' '+onBlurEvent+'>';
			toRet += ''+WeekDay[dayNumberCal[i]]+'</td>';
		}
		toRet += '</tr>';
		for( w = 0; w < 6; w++ )
		{
			toRet += '<tr style="background-color: '+calChosenBg+';" '+onFocusEvent+' '+onBlurEvent+'>';
			for( d = 1; d < 8; d++ )
			{
				numCell = ( ( 7 * w ) + d );
				mouseOn = ''+onFocusEvent+' '+onBlurEvent+' onclick="javascript: fSetSelected( this, '+numCell+' );"';
				mouseOn += ' onmouseover="javascript: this.style.backgroundColor=calBgColorTabOver['+w+']['+d+'];" onmouseout="javascript: this.style.backgroundColor=calBgColorTab['+w+']['+d+'];"';
				toRet += '<td id="calCell'+numCell+'" '+styleTD+' cursor: pointer;" '+mouseOn+'>';
				toRet += 	'<span id="cellText'+numCell+'" Victor="Liming Weng" style="color: '+calBgColorTab[w][d]+';" '+onFocusEvent+' '+onBlurEvent+'> </span>';
				toRet += '</td>';
			}
			toRet += '</tr>';
		}
	}
	return toRet;
}

function fUpdateCal()
{
	//manageSelectsCal();
	setCalColors();
	iYear = $( "tbSelYear" ).value;
	iMonth = $( "tbSelMonth" ).value;
	myMonth = fBuildCal( iYear, iMonth );
	var i = 0;
	for( w = 0; w < 6; w++ )
	{
		for( d = 1; d < 8; d++ )
		{
			num = ( ( 7*w )+d );
			cellX = $( "calCell"+num+"" )
			cellTextx = $( "cellText"+num+"" )
			cellTextx.Victor = i++;
			if( myMonth[w+1][d] < 0 )
			{
				cellTextx.style.color = calMainColor;
				cellX.style.backgroundColor = calMainBg;
				calColorTab[w][d] = calMainColor;
				//calColorTabOver[w][d] = calMainColor;
				calBgColorTab[w][d] = calMainBg;
				calBgColorTabOver[w][d] = calMainBg;
				cellTextx.innerHTML = -myMonth[w+1][d];
			}
			else
			{
				if( ( dayNumberCal[d] == 0 ) || ( dayNumberCal[d] == 6 ) )
				{
					cellTextx.style.color = calWeekendColor;
					cellX.style.backgroundColor = calWeekendBg;
					calColorTab[w][d] = calWeekendColor;
					//calColorTabOver[w][d] = calOverWeekendColor;
					calBgColorTab[w][d] = calWeekendBg;
					calBgColorTabOver[w][d] = calOverWeekendBg;
				}
				else
				{
					cellTextx.style.color = calChosenColor;
					cellX.style.backgroundColor = calChosenBg;
					calColorTab[w][d] = calChosenColor;
					//calColorTabOver[w][d] = calOverChosenColor;
					calBgColorTab[w][d] = calChosenBg;
					calBgColorTabOver[w][d] = calOverChosenBg;
				}
				cellTextx.innerHTML = myMonth[w+1][d];
			}
		}
	}
}

function fSetYearMon( iYear, iMon )
{
	tbSelMonth2 = $( "tbSelMonth" );
	tbSelYear2 = $( "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()
{
	//manageSelectsCal();
	tbSelMonth2 = $( "tbSelMonth" );
	tbSelYear2 = $( "tbSelYear" );
	var iMon = tbSelMonth2.value;
	var iYear = tbSelYear2.value;
	if( --iMon < 1 )
	{
		iMon = 12;
		iYear--;
	}
	fSetYearMon( iYear, iMon );
}

function fNextMonth()
{
	//manageSelectsCal();
	tbSelMonth2 = $( "tbSelMonth" );
	tbSelYear2 = $( "tbSelYear" );
	var iMon = tbSelMonth2.value;
	var iYear = tbSelYear2.value;
	if( ++iMon > 12 )
	{
		iMon = 1;
		iYear++;
	}
	fSetYearMon( iYear, iMon );
}

function fTagInBound( aTag )
{
	VicPopCal2 = $( "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 )
{
	showCal = 1;
	manageSelectsCal();
	gdCtrl = dateCtrl;
	fSetYearMon( giYear, giMonth );
	var point = fGetXY( popCtrl );
	VicPopCal2 = $( "VicPopCal" );
	with( VicPopCal2.style )
	{
		left = new String( point.x ) + 'px';
		top  = new String( point.y + popCtrl.offsetHeight + 1 ) + 'px';
		visibility = 'visible';
	}
	$( 'tbSelMonth' ).style.visibility = 'visible';
	$( 'tbSelYear' ).style.visibility = 'visible';
	tbSelMonth2 = $( "tbSelMonth" );
	tbSelMonth2.focus();
}

function fHideCal()
{
	if( oE = window.event )
	{
		if( ( clX = oE.clientX ) && ( clY = oE.clientY ) )
		{
			if( oTmp = document.elementFromPoint( clX, clY ) )
			{
				while( ( oTmp.tagName != "BODY" ) && ( oTmp.id != "VicPopCal" ) )
				{
					oTmp = oTmp.offsetParent;
				}
				if( oTmp.id == "VicPopCal" )
				{
					return;
				}
			}
		}
		VicPopCal2 = $( "VicPopCal" );
		VicPopCal2.style.visibility = "hidden";
		$( 'tbSelMonth' ).style.visibility = 'hidden';
		$( 'tbSelYear' ).style.visibility = 'hidden';
	}
	else if( showCal == 0 )
	{
		VicPopCal2 = $( "VicPopCal" );
		VicPopCal2.style.visibility = "hidden";
		$( 'tbSelMonth' ).style.visibility = 'hidden';
		$( 'tbSelYear' ).style.visibility = 'hidden';
	}
	manageSelectsCal();
}

function initVicPopCal()
{
	var toRet = '';
	//with( document )
	//{
		setCalColors();
		toRet += '<div name="VicPopCal" id="VicPopCal" style="position: absolute; visibility: hidden; border: 1px solid '+calcTableBorder+'; z-index: 10; background-color: '+calcBg+';" '+onFocusEvent+' '+onBlurEvent+' onclick="javascript: if( this.style.visibility != \'hidden\' ){ focus(); }">';
		toRet += 	'<div style="vertical-align: middle; text-align: center; white-space: nowrap; padding-top: 4px;" '+onFocusEvent+' '+onBlurEvent+'>';
		toRet += 		'<input type="button" name="PrevMonth" value="&lt;" style="height: 20px; width: 20px; font-family: Fixedsys; font-size: 12px; line-height: 100%; background-color: #191865; color: #c5e4ff; border: 1px solid '+calcTableBorder+';" onClick="javascript: showCal=1; fPrevMonth(); showCal=1;" '+onFocusEvent+'>';
		toRet += 		'&nbsp;&nbsp;<select style="border: 0px; outline: none; background: #234195; color: #c5e4ff; width: 92px; font-size: 10px;" name="tbSelMonth" id="tbSelMonth" onChange="javascript: fUpdateCal();" Victor="Won" onclick="javascript: event.cancelBubble=true;" '+onFocusEvent+' '+onBlurEvent+'>';
		for ( var i = 0; i < 12; i++ )
		{
			toRet +=  		'<option style="border: 0px; outline: none; background: #234195; color: #c5e4ff; padding: 1px 2px; font-size: 10px;" value="'+( i + 1 )+'">'+gMonths[i]+'</option>';
		}
		toRet += 		'</select>';
		toRet += 		'&nbsp;&nbsp;<select style="border: 0px; outline: none; background: #234195; color: #c5e4ff; width: 92px; font-size: 10px;" name="tbSelYear" id="tbSelYear" onChange="javascript: fUpdateCal();" Victor="Won" onclick="javascript: event.cancelBubble=true;" '+onFocusEvent+' '+onBlurEvent+'>';
		for( i = calMinYear; i <= calMaxYear; i++ )
		{
			toRet += 		'<option style="border: 0px; outline: none; background: #234195; color: #c5e4ff; padding: 1px 2px; font-size: 10px;" value="'+i+'">&nbsp;&nbsp;'+i+'&nbsp;&nbsp;</option>';
		}
		toRet += 		'</select>';
		toRet += 		'&nbsp;&nbsp;<input type="button" name="PrevMonth" value="&gt;" style="height: 20px; width: 20px; font-family: Fixedsys; font-size: 12px; line-height: 100%; background-color: #191865; color: #c5e4ff; border: 1px solid '+calcTableBorder+';" onclick="javascript: showCal=1; fNextMonth(); showCal=1;" '+onFocusEvent+'>';
		toRet += 	'</div>';
		toRet += 	'<div style="background-color: '+calcBg+';" '+onFocusEvent+' '+onBlurEvent+'>';
		toRet += 		'<table style="width: 245px;" '+onFocusEvent+' '+onBlurEvent+'>';
		toRet += 		fDrawCal( giYear, giMonth, 15, 10 );
		toRet += 		'</table>';
		toRet += 	'</div>';
		toRet += 	'<div style="text-align: center; color: '+calcHeader+'; padding-bottom: 4px;" '+onFocusEvent+' '+onBlurEvent+'>';
		toRet += 		'<span style="cursor: pointer; font-size: 10px; color: '+calcHeader+';" onclick="javascript: fSetDate( currYear, currMonth, currDay);" onMouseOver="javascript: this.style.color=\'#ffffff\';" onMouseOut="javascript: this.style.color=\''+calcHeader+'\';">';
		toRet += 		DZISIAJ+':&nbsp;&nbsp;' + gMonths[currMonth-1] + '&nbsp;' + currDay + ',&nbsp;&nbsp;' + currYear;
		toRet += 		'</span>';
		toRet += 	'</div>';
		toRet += '</div>';
		$( 'VicPopCalContainer' ).innerHTML = toRet;
		$( 'tbSelMonth' ).style.visibility = 'hidden';
		$( 'tbSelYear' ).style.visibility = 'hidden';
		$( 'date' ).value = currYear+'-'+currMonth+'-'+currDay;
	//}
}

/**
*	Opis: funkcja ukrywa/odkrywa listy rozwijane
*/
function manageSelectsCal()
{
	if( oSel = document.getElementsByTagName("SELECT") )
	{
		for( i = 0; i < oSel.length; i++ )
		{
			if( ( oSel[i].id != 'tbSelMonth' ) && ( oSel[i].id != 'tbSelYear' ) )
			{
				if( showCal == 0 )
				{
					oSel[i].style.visibility = "visible";
				}
				else
				{
					oSel[i].style.visibility = "hidden";
				}
			}
		}
	}
}


