var one_day=1000*60*60*24;
var dim = [31,0,31,30,31,30,31,31,30,31,30,31];
var todayD = new Date();
var cMonth = todayD.getMonth()+1 //get current month (1-12)
var cYear = todayD.getFullYear() //get current year
var mn = ['JANUARY','FEBRUARY','MARCH','APRIL','MAY','JUNE','JULY','AUGUST','SEPTEMBER','OCTOBER','NOVEMBER','DECEMBER'];
var mnAb = ['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'];

// eventDates moved out of file into base layout pages
// Can now be populated by Class files..  

/* ===============================================================
 *
 *  buildCal(month, year)
 *
 *  - Called when building the calendar 
 *	+ Sets Up Table, Prints Days Of Week, Then cycles to print dates
 *	+ Makes all Dates >= Today Active
 *	+ Creates DOM containers for each date
 *
 * ===============================================================
 */

buildCal = function(m, y, width){	

	var oD = new Date(y, m-1, 1); 
	oD.od = oD.getDay()+1; 

	var todaysdate = new Date() 
	var scanfortoday = ( y == todaydate.getFullYear() && m==todaydate.getMonth()+1) ? todaydate.getDate() : 0	// Returns a number

	dim[1] = (((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;

	backMonth = m-2;
	backYear = y;
	if(backMonth<0){
		backMonth=11;
		backYear--;
	}
	fwdMonth = m;
	fwdYear = y;
	if(fwdMonth>11){
		fwdMonth=0;
		fwdYear++;
	}
	

	var backLink = '<a href="#" onclick="updateCal('+backMonth+','+backYear+','+width+');return false;">&lt;</a> ';
	var forwardLink = ' <a href="#" onclick="updateCal('+fwdMonth+','+fwdYear+','+width+');return false;">&gt;</a>';

	var t = '<div id="calHeader">'+backLink+ mn[m-1]+' ' +y+ forwardLink+'</div>';
		t+= '<table cellpadding="0" cellspacing="0" width="'+width+'" border="0">';
	    t+= '<tr>';


	// Write Days of Week
	for(s=0;s<7;s++)
	    t+='<th>'+"SMTWTFS".substr(s,1)+'</th>';

	    t+='</tr><tr>';

	for(i=1;i<=42;i++){
		var x=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : '&nbsp;';
		var dd = x;
		var mm = m;
		if(dd<10){
			dd = "0" + dd;
		}
		if(mm<10){
			mm = "0" + mm;
		}

// Creates span for the calendar date
		span = "";
		span+= ''+y+'-'+mm+'-'+dd+'';

		var tempDate = new Date();
		tempDate.setFullYear(y, m-1, x);
		var today = new Date();
		tx = today.getDate();
		today.setFullYear(y, m-1, scanfortoday-1);

/* ===============================================================
 *  Simple Build Script Just Makes all dates in the past inactive
 * ===============================================================
 */ 
		tableCell = "<td";

		classForCell = "";
		if( (tempDate) && (tempDate==today)){
			alert("hi");
			classForCell = "today";
		}
		if( (tempDate) && (tempDate < today)){
			classForCell = "past";
		}
		
		//if(eventDates.indexOf(span)>-1){
		if(eventDates.indexOf(span+"|")>-1){
			classForCell = "event";
			x='<a href="index.php?page_id=115&calendar_day=' + span + '">'+x+'</a>';
		}

// Set up unique span ID for each element

		t+=''+tableCell+' id="'+span+'" class="'+classForCell+'">'+x+'</td>';

		if(((i)%7==0)&&(i<36))t+='</tr><tr>';		
	}

	return t+='</tr></table>';
}


updateCal = function(newMonth, newYear, width){
	todaydate 	= new Date();
	curmonth 	= newMonth+1; 
	curyear 	= newYear;
	
	txt = buildCal(curmonth, curyear, width);
	//alert(txt);

	document.getElementById('theCalendar').innerHTML = txt;
}

parseYear = function(dateString) {
	year = dateString.substring(0, dateString.indexOf('-'));
	return year;
}

parseMonth = function(dateString) {
	day = dateString.substring((dateString.indexOf('-')+1), dateString.length);
	month = eval(day.substring(0, day.indexOf('-'))-1);
	return month;
}

parseDay = function(dateString) {
	s = dateString.substring((dateString.indexOf('-')+1), dateString.length);
	s = s.substring((s.indexOf('-')+1), s.length);
	return s;
}	

