//===============================CALENDAR START

//udf_month_of_year,udf_day_of_week
if(typeof(udf_day_of_week)=='undefined' || udf_day_of_week.length!=7){
	var day_of_week = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
}
else{
	var day_of_week = udf_day_of_week;
}

if(typeof(udf_month_of_year)=='undefined' || udf_month_of_year.length!=12){
	var month_of_year = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
}
else{
	var month_of_year = udf_month_of_year;
}

if(typeof(udf_title_format)=='undefined' || udf_title_format==''){
	var title_format = 'month,  year';
}
else{
	var title_format = udf_title_format;
}

//  DECLARE AND INITIALIZE VARIABLES
if(isDate(calyear,calmonth+1,calday)){
	var Calendar = new Date(calyear, calmonth, calday);
}
else{
	var Calendar = new Date();
}
//var year = Calendar.getYear();	    // Returns year
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 (1-31)

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'
Calendar.setMonth(month);    // Start the calendar month at now

/* VARIABLES FOR FORMATTING*/

var TR_start = '<tr class="blog_cal_tr">';
var TR_end = '</tr>';
var TD_start = '<td class="blog_cal_td_normal">';
var TD_end = '</td>';

/* BEGIN CODE FOR CALENDAR */

cmonth = month+1;
preyearmonth = (cmonth==1)?((year-1)+'12'):(year.toString()+(((cmonth-1)<10)?('0'+(cmonth-1)):(cmonth-1)));
nextyearmonth = (cmonth==12)?((year+1)+'01'):(year.toString()+(((cmonth+1)<10)?('0'+(cmonth+1)):(cmonth+1)));

cal_title = title_format.replace('month', month_of_year[month]);
cal_title = cal_title.replace('year', year);

cal = '<TABLE class="blog_cal_table">' + TR_start;
cal += '<TD class="blog_cal_th_arrow"><a href="'+cal_url+preyearmonth+'"><<</a>'+TD_end;
cal += '<TD COLSPAN="5" class="blog_cal_th_title">';
cal += cal_title + TD_end;
cal += '<TD class="blog_cal_th_arrow"><a href="'+cal_url+nextyearmonth+'">>></a>'+TD_end;
cal += TR_end + TR_start;

// 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 class="blog_cal_th_header">' + day_of_week[index] + TD_end;

	// PRINTS DAY
	else
		cal += '<TD class="blog_cal_th_header">' + 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 class="blog_cal_td_blank">' + '  ' + TD_end;


// LOOPS FOR EACH DAY IN CALENDAR
for(index=0; index < DAYS_OF_MONTH; index++)
{
	if( Calendar.getDate() > index )
	{
	  week_day =Calendar.getDay();

	  if(week_day == 0)
	  cal += TR_start;

	  if(week_day != DAYS_OF_WEEK)
	  {
		  var day_data  = Calendar.getDate();
		  if(cd[day_data]==1){
		  	lcalmonth = ((calmonth+1)<10)?('0'+(calmonth+1)):(calmonth+1);
		  	lday_data = (day_data<10)?('0'+day_data):day_data;
				day_data = '<a href="'+cal_url+calyear+lcalmonth+lday_data+'"><u>'+day_data+'</u></a>';
				tTD_start = '<TD class="blog_cal_td_link">';
		  }else{
		  	tTD_start = TD_start;
			}

		  if( today==Calendar.getDate() )
		  	cal += '<TD class="blog_cal_td_today">' + day_data + TD_end;
		  else
		  	cal += tTD_start + day_data + TD_end;
	  }

	  if(week_day == DAYS_OF_WEEK)
	  cal += TR_end;
  }
  // INCREMENTS UNTIL END OF THE MONTH
  Calendar.setDate(Calendar.getDate()+1);

}
// end for loop

cal += '</TD></TR></TABLE>';
//===============================CALENDAR END

