//  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>';
var TR_end = '</tr>';
var highlight_start = '<td><table cellspacing="0" border="0"><tr><td width="24" class="highlightcell">';
var highlight_end   = '</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="1" width="187"><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"><img src="/images/Schedule/calimages/spacer.gif" width="1" height="4" />' + TD_end + TR_end;

// PRINT THE MONTH AND YEAR
cal += TR_start_month + '<td colspan="' + DAYS_OF_WEEK + 'class="tablecolor" height="20px"><div class="month">&nbsp;&nbsp;';
cal += month_of_year[month]  + '  ' + year + '</div>' + TD_end + TR_end;

// for a spacer below the year
cal += TR_start_month + '<td colspan="' + DAYS_OF_WEEK + 'class="tablecolor"><img src="/images/Schedule/calimages/spacer.gif" width="1" height="4" />' + 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="2" cellpadding="1" >';//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 + '<strong>' + day_of_week[index] + '</strong>' + 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;
      }

  // HIGHLIGHT TODAY'S DATE
  //if( today==Calendar.getDate() )
  //cal += highlight_start + day + highlight_end + TD_end;

  // PRINTS DAY
  //else
  cal += TD_start + '<a class="cal" href="/app/Schedule/?start_date=' + year + '-' + twodigmonth + '-' + twodigday + '">' + day + '</a>' + TD_end;
 
  }

  // END ROW FOR LAST DAY OF WEEK
  if(week_day == DAYS_OF_WEEK)
  cal += TR_end;
  }

  // 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>';

//  PRINT CALENDAR
document.write(cal);
