
Date.prototype.ParseYMD_CZ = function ( YMD )
{
	if ( YMD == undefined )
		return false;
		
	if ( YMD == '' || YMD == '---' )
		return false;
	
	var dateParts = YMD.split('.');
	
	var Ds = dateParts[0];
	var Ms = dateParts[1];
	var Ys = dateParts[2];
	
	if ( Ys.length != 4 || Ms.length > 2 || Ms.length < 1 || Ds.length > 2 || Ds.length < 1 )
		return false; 
	
	Y = parseInt ( Ys , 10 );
	M = parseInt ( Ms , 10 );
	D = parseInt ( Ds , 10 );
	
	this.setTime ( 0 );	
	this.setYear ( Y );
	this.setMonth ( M - 1 );
	this.setDate ( D );

	return true;
}


Date.prototype.ParseYMD = function ( YMD )
{
	if ( YMD == undefined )
		return false;
		
	if ( YMD == '' || YMD == '---' )
		return false;
	
	var dateParts = YMD.split('-');
	var Ys = dateParts[0];
	var Ms = dateParts[1];
	var Ds = dateParts[2];
	
	if ( Ys.length != 4 || Ms.length > 2 || Ms.length < 1 || Ds.length > 2 || Ds.length < 1 )
		return false; 
	
	Y = parseInt ( Ys , 10 );
	M = parseInt ( Ms , 10 );
	D = parseInt ( Ds , 10 );
	
	this.setTime ( 0 );	
	this.setYear ( Y );
	this.setMonth ( M - 1 );
	this.setDate ( D );

	return true;
}

Date.prototype.myGetYear = function ()
{
	var Y = this.getYear ();
	if ( Y < 1970 )
		Y += 1900;
	return Y;
}

Date.prototype.myGetMonth = function ()
{
	return this.getMonth () + 1;
}

Date.prototype.GetYMD = function ()
{
	var Y = this.myGetYear ();
	var M = this.myGetMonth ();
	var D = this.getDate ();

	if ( M < 10 )
		M = '0' + M;
	if ( D < 10 )
		D = '0' + D;
	
	return Y + '-' + M + '-' + D;
}

Date.prototype.GetYMD_CZ = function ()
{
	var Y = this.myGetYear ();
	var M = this.myGetMonth ();
	var D = this.getDate ();
	
	return D + '.' + M + '.' + Y;
}


Date.prototype.DayOfWeek = function ()
{
	var DOW = this.getDay();
	
	if ( DOW == 0) 
		return 7;
	
	return DOW;
}

Date.prototype.DayOfWeekCZ = function ()
{
	var DOWNo = this.DayOfWeek();
	
	switch ( DOWNo )
	{
		case 1: return 'Pondělí';
		case 2: return 'Úterý';
		case 3: return 'Středa';
		case 4: return 'Čtvrtek';
		case 5: return 'Pátek';
		case 6: return 'Sobota';
		case 7: return 'Neděle';
	}
	return '';
}

Date.prototype.GetCzechLabel = function ()
{
	var D = this.getDate();
	var M = GetMonthName ( this.myGetMonth() );
	var Y = this.myGetYear();
	var T = this.DayOfWeekCZ();
	
	return T + ': ' + D + '. ' + M + ' ' + Y;	
}



function FirstDayInMonth ( Y, M )
{
	nd = new Date();
	nd.setTime(0);
	nd.setYear( Y);
	nd.setMonth (M - 1);
	nd.setDate (1);
	return nd.DayOfWeek();
}

function MissingWeekDaysBefore ( Y, M )
{
	return FirstDayInMonth ( Y, M ) - 1 ;
}

function MissingWeekDaysAfter ( Y, M )
{
	var mwda = 8 - FirstDayInMonth ( Y, M + 1 );
	if ( mwda == 7 )
		return 0;
	return mwda;
}

function DaysInMonth ( Y, M )
{
	switch ( M )
	{
		 case 1:
		 case 3:
		 case 5:
		 case 7:
		 case 8:
		 case 10:
		 case 12:
		 	return 31;
		 	break;
		 	
		 case 4:
		 case 6:
		 case 9:
		 case 11:
		 	return 30;
		 	break;
	}
	
	/* ignoruji neprestupna staleti */ 
	if ( ( Y % 4 ) == 0 )
		return 29;
	return 28;
}


function GetMonthName ( M )
{
	switch ( M )
	{
		 case 1: return 'Leden';
		 case 2: return 'Únor';
		 case 3: return 'Březen';
		 case 4: return 'Duben';
		 case 5: return 'Květen';
		 case 6: return 'Červen';
		 case 7: return 'Červenec';
		 case 8: return 'Srpen';
		 case 9: return 'Září';
		 case 10: return 'Říjen';
		 case 11: return 'Listopad';
		 case 12: return 'Prosinec';
	}
}

function RenderCalendar ( Y, M, SelectedDate, Min, Max, MinAlt, MaxAlt, RenderTo )
{
	// min/max jsou ty co maji zajezdy v ramci nextofferu
	// minAlt/maxAlt jsou pro vsechny zajezdy / ignore nextoffer
	// a cunek je idiot a necti nam zdrojaky!!
	
	/* BOUNDS: MIN MAX CLEAN */
	var dMin = null;
	var dMax = null;
	if ( Min != '' && Min !='---' && Max != '' && Max != '' )
	{
		dMin = new Date();
		var resMin = dMin.ParseYMD ( Min );
		
		dMax = new Date();
		var resMax = dMax.ParseYMD ( Max );
		
		if ( ! ( resMin && resMax ) )
		{
			dMin = null;
			dMax = null;
		}
	}

	/* BOUNDS: MIN MAX ALT */
	var dMinAlt = null;
	var dMaxAlt = null;
	if ( MinAlt != '' && MinAlt !='---' && MaxAlt != '' && MaxAlt != '' )
	{
		dMinAlt = new Date();
		var resMinAlt = dMinAlt.ParseYMD ( MinAlt );
		
		dMaxAlt = new Date();
		var resMaxAlt = dMaxAlt.ParseYMD ( MaxAlt );
		
		if ( ! ( resMinAlt && resMaxAlt ) )
		{
			dMinAlt = null;
			dMaxAlt = null;
		}
	}	

	
	MWDB = MissingWeekDaysBefore ( Y, M );
	MWDA = MissingWeekDaysAfter ( Y, M );
	
	var RenderedCells = 0;
	
	var N_M = M+1;
	var N_Y = Y;
	
	if (N_M > 12 )
	{
		N_M -= 12;
		N_Y ++;
	}
	
	var P_M = M-1;
	var P_Y = Y;
	
	if (P_M < 1 )
	{
		P_M += 12;
		P_Y --;
	}
	
	var PickedDayOfMonth = null;
	
	if ( SelectedDate !='' && SelectedDate != '---' )
	{
		nD = new Date();
		nD.ParseYMD ( SelectedDate );
	
		if ( Y == nD.myGetYear() && M == nD.myGetMonth() )
			PickedDayOfMonth = nD.getDate();
	}	
	
	
	var MuzemeDoleva = ! ( ( dMinAlt.myGetYear() == Y ) && ( dMinAlt.myGetMonth() == M ) );
	var MuzemeDoprava = ! ( ( dMax.myGetYear() == Y ) && ( dMaxAlt.myGetMonth() == M ) );
		
	
	var HTML  = '<table class="mCalendar" cellspacing="0">';
	
	HTML += '<tr>';
	
	if ( MuzemeDoleva )
		HTML += '<td onclick="RenderCalendar(' + P_Y + ','+ P_M + ',\'' + SelectedDate +'\',\'' + Min +'\',\'' + Max +'\',\'' + MinAlt +'\',\'' + MaxAlt +'\',\'' +RenderTo + '\');" class="Jump">&laquo;</td>';
	else
		HTML += '<td class="NoJump">&laquo;</td>';
	HTML += '<td colspan="5" class="MonthName">' + GetMonthName ( M ) + ' ' + Y + '</td>';
	
	if (MuzemeDoprava)
		HTML += '<td onclick="RenderCalendar(' + N_Y + ','+ N_M + ',\'' + SelectedDate +'\',\'' + Min +'\',\'' + Max +'\',\'' + MinAlt +'\',\'' + MaxAlt +'\',\'' +RenderTo + '\');"  class="Jump">&raquo;</td>';
	else
		HTML += '<td class="NoJump">&raquo;</td>';
	HTML += '</tr>';
	
	HTML += '<tr class="DOW">';
	HTML += '<td>Po</td>';
	HTML += '<td>Út</td>';
	HTML += '<td>St</td>';
	HTML += '<td>Čt</td>';
	HTML += '<td>Pá</td>';
	HTML += '<td>So</td>';
	HTML += '<td>Ne</td>';
	HTML += '</tr>';
		
	
	HTML += '<tr>';
	var InTR  = true;
	
	if ( MWDB > 0 )
	{
		HTML += '<td colspan="' + MWDB + '" colspan="Filler">&nbsp;</td>';
		RenderedCells = MWDB;
	}
	
	var CellClass;
	
	var RenderedDays = 0;
	while ( RenderedDays < DaysInMonth ( Y, M ) )
	{
		if ( ! InTR )
		{
			HTML +='<tr>';
			InTR = true;
		}
		
		RenderedDays++;
		
		
		CellClass = '';
		
		if ( PickedDayOfMonth == RenderedDays )
			CellClass = 'Picked';
		else if ( dMin != null && dMax != null )
		{
			var cellDate = new Date ();
			cellDate.setTime(0);
			cellDate.setYear(Y);
			cellDate.setMonth(M-1);
			cellDate.setDate(RenderedDays);
			
			if ( cellDate < dMin )
			{
				if ( dMinAlt != null && cellDate >= dMinAlt )
					CellClass = 'Alt';  
				else
					CellClass = 'Before';
			}
			if ( cellDate > dMax )
			{
				if ( dMaxAlt != null && cellDate <= dMaxAlt )
					CellClass = 'Alt';  
				else
					CellClass = 'After';
			}
				
			delete cellDate;
		}
		
	 	/* Vikendy */		
		if ( ( RenderedCells % 7 ) > 4 )
			CellClass +='WkEnd';
			
		var FullDate = Y  + '-' + M + '-' + RenderedDays;
		//var FullDate = RenderedDays + '.' +M + '.' + Y;
		
		if ( CellClass == '' )
			HTML += '<td class="Active" onclick="return SetValue ( \''+ RenderTo+'\', \''+ FullDate +'\' );" style="cursor:pointer;">'+RenderedDays+'</td>';
		else if ( CellClass == 'WkEnd' )
			HTML += '<td class="ActiveWkEnd" onclick="return SetValue ( \''+ RenderTo+'\', \''+ FullDate +'\' );" style="cursor:pointer;">'+RenderedDays+'</td>';
		else
		{
			HTML += '<td class="' + CellClass + '"';
			if ( ( CellClass == 'Alt' ) || ( CellClass == 'Picked' ) || ( CellClass == 'AltWkEnd' ) || ( CellClass == 'PickedWkEnd' ) )
				HTML += 'onclick="return SetValue ( \''+ RenderTo+'\', \''+ FullDate +'\' );" style="cursor:pointer;"';
				
			HTML += '>'+RenderedDays+'</td>';
		}		
		RenderedCells++;

		if ( ( RenderedCells % 7 ) == 0 )
		{
			HTML +='</tr>';
			InTR = false;
		}
	}
	
	if ( MWDA > 0 )
	{
		HTML += '<td colspan="' + MWDA + '" colspan="Filler">&nbsp;</td>';
		HTML += '</tr>';
	}
	
	HTML += '<tr><td colspan="7" class="CloseCalendar"><span class="CloseCalendar" onclick="CloseCalendar(' + RenderTo + ');">Zavřít kalendář</span></td></tr>';
	
	HTML += '</table>';
	
	document.getElementById('box_c' + RenderTo).innerHTML = HTML;
}

function CloseCalendar ( ID )
{
	document.getElementById('box_c' + ID).style.display='none';
	
	/* MSIE - zobraz dropdowny*/
	document.getElementById ( 'zl-no-crit' + '12' ).style.display='';
	document.getElementById ( 'zl-no-crit' + '6' ).style.display='';
	document.getElementById ( 'zl-no-crit' + '7' ).style.display='';
	document.getElementById ( 'zl-no-crit' + '15' ).style.display='';
	document.getElementById ( 'zl-no-crit' + '9' ).style.display='';
	document.getElementById ( 'zl-no-search-btn' ).style.display='';
	
}