// JavaScript Document


/*******************************************************************************
15.01.2009
Vitaly Brazhens aka vvb

*******************************************************************************/
function getEndMonthDay ( year, month )
{
	if ( month == 2 )
	{
		if ( year%400 == 0 || ( year % 4 == 0 && year % 100 != 0 ))
			return 29;		
		return 28; 
	}

	if ( month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || 
		month == 10 || month == 12 )
		return 31;

	return 30;
}
/*******************************************************************************
15.01.2009
Vitaly Brazhens aka vvb
This function prepare body of table for selected month 
*******************************************************************************/
function PrepareMonthArr ( endDate, startWday, year, month, data )
{

  var retstr = sDatsHeadSmp;
  var iWeekQ = 0;
  var iDaysQ = 0;
  
  while ( iDaysQ < endDate )
  { 
    retstr += '<tr>';
  
    for ( iWeekD = 0; iWeekD < 7; iWeekD ++ )
    {
    
      if (( iWeekQ == 0 && iWeekD + 1 < startWday ) || ( iDaysQ >= endDate ))
      {
        retstr += '<td class=jsc_cellfree>&nbsp;</td>';
      }
      else
      {
        iDaysQ ++;
        if ( data.search ( ''+year+'-'+month+'-'+iDaysQ+';' ) != -1 ) 
        	retstr += '<td class=jsc_cellbusy>'+ iDaysQ +'</td>';
        else
        	retstr += '<td class=jsc_cellfree>'+ iDaysQ +'</td>';
      }
    }
    iWeekQ ++;
    retstr += '</tr>';
  }
  return retstr;
	

}
/*******************************************************************************
15.01.2009
Vitaly Brazhens aka vvb

*******************************************************************************/
function getmonthcalend ( year, month, outplace )
{
	
	if ( month > 12 || month < 1 )
		return;
		
	var StartDate = new Date ();
	StartDate.setFullYear ( year, month - 1, 1 );
	
	var stWeekDay = StartDate.getDay ();
	if ( stWeekDay == 0 )
    stWeekDay = 7;
      
	var endMonthDay = getEndMonthDay ( year, month );
	
	var data = document.getElementById ( outplace + '_value' ).value;
	
	var ret = PrepareMonthArr ( endMonthDay, stWeekDay, year, month, data );  

	var out = document.getElementById ( outplace );	
	var outnext = document.getElementById ( outplace + '_next' );
	var outprev = document.getElementById ( outplace + '_prev' );
	
	
	if ( month == 1 )
		prevmonth = 12;
	else
		prevmonth = month - 1;
		
	if ( prevmonth == 12 )
		prevyear = year - 1;
	else
		prevyear = year;
	
	if ( month == 12 )
		nextmonth = 1;
	else
		nextmonth = month + 1;

	if ( nextmonth == 1 )
		nextyear = year + 1;
	else 
		nextyear = year; 
	

	outnext.innerHTML = '<a href="javascript: getmonthcalend ( ' + nextyear + ', ' + nextmonth + ', \'' + outplace + '\');">&nbsp;&nbsp;=&gt;&gt;</a>';
	outprev.innerHTML = '<a href="javascript: getmonthcalend ( ' + prevyear + ', ' + prevmonth + ', \'' + outplace + '\');">&lt;&lt;=&nbsp;&nbsp;</a>';
	
  
  out.innerHTML = '<table class=jsc_tab><tr><td colspan=7 class=jsc_head>'+aMonthNames [month - 1]+' ' + year +'</td></tr>' + ret;	

}