//  SET ARRAYS
var day_of_week = new Array('S','M','T','W','T','F','S');
var month_of_year = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

//  DECLARE AND INITIALIZE VARIABLES
var Calendar = new Date();

//var year = Calendar.getFullYear();	    // Returns year
//var month = Calendar.getMonth();    // Returns month (0-11)
var today = Calendar.getDate();    // Returns day (1-31)
var weekday = Calendar.getDay();    // Returns day (0-6)

var DAYS_OF_WEEK = 7;    // "constant" for number of days in a week
var DAYS_OF_MONTH = 31;    // "constant" for number of days in a month
var cal;    // Used for printing

Calendar.setDate(1);    // Start the calendar day at '1'

//CHANGE THE CALENDAR BASED ON URL

URL = window.location.href;

var indexformonth1 = URL.indexOf('start_date=20') + 16;
var indexformonth2 = URL.indexOf('start_date=20') + 17;

var getthemonth1 = URL.charAt(indexformonth1);
var getthemonth2 = URL.charAt(indexformonth2);

var month = (getthemonth1 + getthemonth2);

if (month < 10)
        {
                month = getthemonth2 -1;
        } else if (month >= 10) {
                month = month - 1;
        } else {
                month = Calendar.getMonth();
        }
Calendar.setMonth(month);   

var indexforyear = URL.indexOf('start_date=20') + 14;
var indexforyearcontrol = URL.indexOf('start_date=20') + 14;
var gettheyear = URL.charAt(indexforyear);
//var year = '200' + gettheyear;  /// remed by vjoy - don't know what the above 3 statements are trying to achieve.

var year=Calendar.getFullYear();

//alert(gettheyear);

var alpha = /[a-zA-Z]$/;

if (gettheyear.match(alpha))
        {      
//alert("no number here!");
		year = Calendar.getFullYear(); 
        }
Calendar.setYear(year);   



// TURN THE MONTH INTO TWO DIGIT NUMBER
var twodigmonth = month + 1;
if (twodigmonth < 10)
	{
		twodigmonth = '0' + twodigmonth;
	}


/* VARIABLES FOR FORMATTING
NOTE: You can format the 'BORDER', 'BGCOLOR', 'CELLPADDING', 'BORDERCOLOR'
      tags to customize your calendar's look. */

var TR_start_month = '<TR>';
var TR_start = '<TR class="cal-start">';
var TR_end = '</TR>';
var highlight_start = '<TD><TABLE CELLSPACING=0 BORDER=0><TR><TD WIDTH=24 class=highlightcell><CENTER>';
var highlight_end   = '</CENTER></TD></TR></TABLE>';
var TD_start = '<TD class="day">';
var TD_start_day = '<TD class="dayofweek">';
var TD_end = '</TD>';

/* BEGIN CODE FOR CALENDAR
NOTE: You can format the 'BORDER', 'BGCOLOR', 'CELLPADDING', 'BORDERCOLOR'
tags to customize your calendar's look.*/

// this first table is for the outer table
// cal =  '<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 width=177 BGCOLOR="#4f6501"><TR><TD>';


// This table contains the Month and Year and the line
// cal += '<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>';

// for a spacer above the year
// cal += TR_start_month + '<TD COLSPAN="' + DAYS_OF_WEEK + 'class="tablecolor">' + TD_end + TR_end;

// PRINT THE MONTH AND YEAR
// cal += TR_start_month + '<TD COLSPAN="' + DAYS_OF_WEEK + 'class="tablecolor"><DIV CLASS="month">&nbsp;&nbsp;';
// cal += month_of_year[month]  + '  ' + year + '</DIV>' + TD_end + TR_end;
cal = '<div><div class="calendar_top"><h4>'+month_of_year[month] +' '+year+'</h4></div><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>';

// for a spacer below the year
cal += TR_start_month + '<TD COLSPAN="' + DAYS_OF_WEEK + 'class="tablecolor">' + TD_end + TR_end;

// for the line
// cal += TR_start;
//cal += '<TD COLSPAN="' + DAYS_OF_WEEK + '" height="1"><img src="/images/Schedule/calimages/thinline.gif" width="187" height="1">';
// cal += TR_end;
// end the line

// for a spacer below the line
//cal += TR_start + '<TD COLSPAN="' + DAYS_OF_WEEK + ' class="tablecolor"><img src="/images/Schedule/calimages/spacer.gif" width="1" height="5">' + TD_end + TR_end;

cal += '</TABLE>';
// and now the table


// Start the new table containing days of week and dates
cal += '<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 class="cal_sche">';//bgcolor="#E1EBFF">';


cal += TR_start;

//   DO NOT EDIT BELOW THIS POINT  //
// LOOPS FOR EACH DAY OF WEEK
for(index=0; index < DAYS_OF_WEEK; index++)
{

// BOLD TODAY'S DAY OF WEEK
//if(weekday == index)
cal += TD_start_day + '<B>' + day_of_week[index] + '</B>' + TD_end;

// PRINTS DAY
//else
//cal += TD_start + day_of_week[index] + TD_end;
}


cal += TD_end + TR_end;
cal += TR_start;


// FILL IN BLANK GAPS UNTIL TODAY'S DAY
for(index=0; index < Calendar.getDay(); index++)
cal += TD_start + '  ' + TD_end;

// LOOPS FOR EACH DAY IN CALENDAR
for(index=0; index < DAYS_OF_MONTH; index++)
{
    if( Calendar.getDate() > index )
	{
	    // RETURNS THE NEXT DAY TO PRINT
	    week_day =Calendar.getDay();
	    
	    // START NEW ROW FOR FIRST DAY OF WEEK
	    if(week_day == 0)
		cal += TR_start;
	    
	    if(week_day != DAYS_OF_WEEK)
		{
		    
		    // SET VARIABLE INSIDE LOOP FOR INCREMENTING PURPOSES
		    var day  = Calendar.getDate();
		    var twodigday = day;
		    if (twodigday < 10)
			{
			    twodigday = '0' + twodigday;
			}

		    cal += TD_start + '<a class="cal" href="/app/Schedule/index.php?start_date=' + year + '-' + twodigmonth + '-' + twodigday + '">';

		    // HIGHLIGHT TODAY'S DATE
		    //alert(today);
		    if( today==Calendar.getDate() )
			{
			    cal += ' <B> ' + day + ' </B> ';
			}		    
		    // PRINTS DAY
		    else
			{
			    cal += day + '</a>';
			}

		    cal += TD_end;
		    
		    
		}

	    // END ROW FOR LAST DAY OF WEEK
	    if(week_day == DAYS_OF_WEEK){
		cal += TR_end;
	    }
	    //alert(cal);
	}

    // INCREMENTS UNTIL END OF THE MONTH
    Calendar.setDate(Calendar.getDate()+1);
    
}// end for loop

/* THIS IS THE SPACER BELOW THE LAST DAY OF THE MONTH
cal += '</TD></TR>';
cal += TR_start + '<TD COLSPAN="' + DAYS_OF_WEEK + '><img src="/images/Schedule/calimages/spacer.gif" width="1" height="4">' + TD_end + TR_end;
*/

cal += '</TABLE></TABLE></div>';

//  PRINT CALENDAR
document.write(cal);
