

function Calendar(){
	//this values need to be initalized from the server.
	this.month = getMonthIndex(smonth);
	this.date = sdate;
	this.year = syear;
	this.day = new Date(syear, smonth, sdate).getDay();
	
	this.days = new Array("Sun","Mon","Tues","Wed","Thu","Fri","Sat");
	this.months = new Array();
	this.months[0] = new Array("January", 31);
	if((this.cal_year % 4) == 0){
		this.months[1] = new Array("February", 29);
	}
	else{
		this.months[1] = new Array("February",28);
	}
	this.months[2] = new Array("March", 31);
	this.months[3] = new Array("April", 30);
	this.months[4] = new Array("May", 31);
	this.months[5] = new Array("June", 30);
	this.months[6] = new Array("July", 31);
	this.months[7] = new Array("August", 31);
	this.months[8] = new Array("September", 30);
	this.months[9] = new Array("October", 31);
	this.months[10] = new Array("November", 30);
	this.months[11] = new Array("December", 31);
	
	this.first = 1;
	this.event = new Array();
	
	this.makeMonth = makeMonth;	
	this.getPrevMonth = getPrevMonth;
	this.getNextMonth = getNextMonth;
	this.addEvents = addEvents;
	this.showEvents = showEvents;
			
	
}


function makeMonth(){
	//need to find first day of the month
	this.first = new Date(this.year, this.month, 1).getDay();
	
	var html = new Array();
	var index = 0;
	var day1 = true;
	html[index++] = "<table cellspacing='0' cellpadding='3' id='calendar'><tr><th class='month' id='right'><a href='javascript:mycal.getPrevMonth();'>&#171;</a></th><th class='month' id='month' colspan='5'>"+this.months[this.month][0]+" "+this.year+"</th><th class='month' id='left'><a href='javascript:mycal.getNextMonth();'>&#187;</a></th></tr>";
	html[index++] = "<tr id='days'><td class='days' id='sun'>Sunday</td><td class='days' id='mon'>Monday</td><td class='days' id='tues'>Tuesday</td><td class='days' id='wed'>Wednesday</td><td class='days' id='thr'>Thursday</td><td class='days' id='fri'>Friday</td><td class='days' id='sat'>Saturday</td></tr>";
	var weeks = 1;
	var tdays = 1;
//	var eom = false;
	for(var i = 1; i < 42; i++){
	 if(tdays > this.months[this.month][1])break;
//	 if(eom)break;
	 html[index++] = "<tr id='week"+weeks+"'>";
		for(var j = 0; j < 7; j++){
			todaysMonth = this.month+1;
			todaysID = (todaysMonth<10?'0'+todaysMonth:todaysMonth)+''+(tdays<10?'0'+tdays:tdays)+''+this.year;
			if(day1){
				if(j >= this.first){
					 html[index++] = "<td class='dayon'><div class='number'>"+(tdays)+"</div><div class='event' id='"+todaysID+"'></div></td>";
					 day1 = false;
					 tdays++	
				}
				else{
				//these days should blank
					html[index++] = "<td class='dayoff'>&nbsp;</td>";
				}
			}
			else if(!day1){
				if(tdays <= this.months[this.month][1]){
					 html[index++] = "<td class='dayon'><div class='number'>"+(tdays)+"</div><div class='event' id='"+todaysID+"'></div></td>";
					 tdays++	
				}
				else{
				//these days should blank
					html[index++] = "<td class='dayoff'>&nbsp;</td>";
					//eom = true;
				}
			}
		
		 i++;	
		}
	  
	  weeks++;	
	  html[index++] = "</tr>";	
	}
	html[index++] = "</table>";
	
	document.getElementById("calendarContainer").innerHTML =  html.join("");
	this.showEvents();
	return;
}

function getPrevMonth(){
	if(this.month == 0){
		this.month = 11;
		this.year = parseInt(this.year) - 1;
	}
	else{
		this.month = this.month - 1;
		this.year = this.year;
	}
	this.date = this.date;
	this.day = new Date(this.year, this.month, this.date).getDay();
	this.makeMonth();
	return;
}

function getNextMonth(){
	if(this.month == 11){
		this.month = 0;
		this.year = parseInt(this.year) + 1;
	}
	else{
		this.month = this.month + 1;
		this.year = this.year;
	}
	this.date = this.date;
	this.day = new Date(this.year, this.month, this.date).getDay();
	this.makeMonth();
	return;
}

var numevents = 0;
function Event(date, title, desc, lk, lk_type){
	this.date = date;
	this.title = title;
	this.desc = desc;
	this.lk = lk;
	this.id = "desc_"+this.date+numevents++;
	var target = "";
	if(lk){
		if(lk_type == "document"){
			target = "_blank";
		}
		else if(lk_type == "tpv"){
			lk = 'javascript:showWarning("'+lk+'");';
		}
	}
	var moreinfo = lk ? "<br /><br /><p class='moreinfo'><a href='"+lk+"' target='"+target+"' class='moreinfo'>More Information</a><br /><br /><a href=\"javascript:hideDisplay('"+this.id+"');\">Close Display</a></p>" : "<br /><br /><p><a href=\"javascript:hideDisplay('"+this.id+"');\">Close Display</a></p>";

	document.getElementById("events").innerHTML += "<div id='"+this.id+"' class='desc'>"+desc+moreinfo+"</div>";
}

/**********
param@ date - Date of the event
param@ title - Title of the event
param@ desc - Short descript that should display onmouseover or onclick
param@ lk - If needed, a link over to more verbose description


*********/
var ecount = 0;
function addEvents(date, title, desc, lk){
	this.event[ecount++] = new Event(date,title, desc, lk);
}

function showEvents(){

	var evt = new Array();
	var index = 0;
	var edates = new Array();
	var e = 0;
	for(var i = 0; i<this.event.length; i++){
		if(document.getElementById(this.event[i].date)){
			if(evt[this.event[i].date] == null){
				evt[this.event[i].date] = "<li><a href=\"javascript:openEvent('"+this.event[i].id+"',this);\">"+this.event[i].title+"</a></li>";	
				edates[e++] = this.event[i].date;
			}
			else{
				evt[this.event[i].date] += "<li><a href=\"javascript:openEvent('"+this.event[i].id+"',this);\">"+this.event[i].title+"</a></li>";	
			}	
		}
	}

	for(var j = 0; j < edates.length; j++){
		document.getElementById(edates[j]).innerHTML =  "<ul>"+evt[edates[j]]+"</ul>";
	}

}
/**********
param@ id - ID of the div to be shown
param@ ele - The a element's object being passed in.

*********/
function openEvent(id, ele){

    var divs = document.getElementsByTagName('div');
    var divid;
    
    for (var i = 0; i < divs.length; i++)
    {
        if (divs[i].id.indexOf("desc_") >= 0)
        {
           // alert(divs[i].id);
           divs[i].style.display = "none";
        }
    }
	document.getElementById(id).style.display = "block";
	document.getElementById(id).style.top = findPosY(document.getElementById("coe"));
	document.getElementById(id).style.left = findPosX(document.getElementById("coe"))+200;

}
/**********
param@ id - ID of the div to be shown
param@ ele - The a element's object being passed in.
Hides the display
*********/
function hideDisplay(id){
	document.getElementById(id).style.display = "none";
}

function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getMonthIndex(mth){
return mth;
 if(mth == "01"){
    return 0;
 }
 else if(mth == "02"){
    return 1;
 }
 else if(mth == "03"){
    return 2;
 }
 else if(mth == "04"){
    return 3;
 }
 else if(mth == "05"){
    return 4;
 }
 else if(mth == "06"){
    return 5;
 }
 else if(mth == "07"){
    return 6;
 }
 else if(mth == "08"){
    return 7;
 }
 else if(mth == "09"){
    return 8;
 }
 else if(mth == "10"){
    return 9;
 }
 else if(mth == "11"){
    return 10;
 }
 else if(mth == "12"){
    return 11;
 }




}

//end of file

