function createCalendar(day,month,year,calendarName)
{
	var MonthName=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	CalendarName=calendarName+"_CalendarSpan";
	HoldingName=calendarName+"_holdingSpan";
	//number of days in current month
	maximum = monthDays(day, month+1, year);
	firstDate=new Date();
	//year=2003;
	firstDate.setFullYear(year);
	firstDate.setDate(1);
	firstDate.setMonth(month);
	
	firstDay=parseInt(firstDate.getDay());
	//number of days in previous month
	maximumL=monthDays(day, month, year);
	//get todays date
	now =new Date();
	//build the calendar
	buildCal="<Table class='CalendarTable' style='font-size:8px;' cellspacing=0 cellpadding=3 border=1 rules=groups>";
	buildCal+="<Thead class='monthyear'><tr><td colspan=6>"+MonthName[firstDate.getMonth()]+", "+firstDate.getFullYear()+"</td><td align=right colspan=1><img class='CalendarButton'  onclick=window.document.getElementById(CalendarName.toString()).style.display='none' src='../../Formatting/Images/buttonClose.gif'></img></td></tr></thead>";
	buildCal+="<tr class=weekdays><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td></tr>";
	for (j=firstDay;j!=0;j--)
	{
		num=parseInt(j)-1;
		actualNum=parseInt(maximumL)-num;
		buildCal+="<td class=lastmonth><span class=calday>"+actualNum+"</span></td>";
	}
	for (i=1;i<=maximum;i++)
	{
		if (i==now.getDate() && month==now.getMonth() && year==now.getFullYear())
		{
			buildCal+="<td onclick=SetDate("+i+","+month+","+year+",'"+calendarName+"') class=TodaysDate><span class=calday>" + i + "</span></td>";				
		}
		else
		{
			buildCal+="<td onclick=SetDate("+i+","+month+","+year+",'"+calendarName+"')><span class=calday>" + i + "</span></td>";
		}
		
		if (i<=7-firstDay)
		{
		
			switch (firstDay)
			{
			case 0:
				if ((i%7==0))
				{
					buildCal+="<tr>";
				}
				break
			case 1:
				if ((i%6==0))
				{
					buildCal+="<tr>";
				}
				break
			case 2:
				if ((i%5==0))
				{
					buildCal+="<tr>";
				}
				break
			case 3:
				if ((i%4==0))
				{
					buildCal+="<tr>";
				}
				break
			case 4:
				if ((i%3==0))
				{
					buildCal+="<tr>";
				}
				break
			case 5:
				if ((i%2==0))
				{
					buildCal+="<tr>";
				}
				break
			case 6:
				if ((i%1==0))
				{
					buildCal+="<tr>";
				}
				break
			
		}
		
					
		}
			else
		{
			if (((i+firstDay)%7==0) && (i!=maximum))
			{
				buildCal+="<tr>";
			}
		}
	}	
	
	//Check to see if need to add empty row underneath so that next and prev always in the 
	//same position
	if (firstDay <5)
	{
		buildCal+="<tr><td>&nbsp;</td></tr>"
	} 
	else if (maximum==28)
	{
		buildCal+="<tr><td>&nbsp;</td></tr>"
	}
	else if ((firstDay==5) && (maximum==30))
	{
		buildCal+="<tr><td>&nbsp;</td><td>&nbsp;</td></tr>"
	}
	
	

//	buildCal+="<tr class=nextprev><td colspan=2><Span class=nextprev onclick=javascript:GetNewDate("+day+","+ month+","+year+",'prev','"+calendarName+"')><< PREV</span></td><td></td><td></td><td></td><td></td><td align=right colspan=2><span class=nextprev onclick=javascript:GetNewDate("+day +","+ month+","+year+",'next','"+calendarName+"')>NEXT >></span></td></tr>"
	
		buildCal+="<tr class=nextprev><td colspan=7><table cellpadding=0 cellspacing=0 width='100%'><tr><td><Span class=nextprev onclick=javascript:GetNewDate("+day+","+ month+","+year+",'prev','"+calendarName+"')><< PREV</span></td><td align=right><span class=nextprev onclick=javascript:GetNewDate("+day +","+ month+","+year+",'next','"+calendarName+"')>NEXT >></span></td></tr></table></td></tr>"
	
	window.document.getElementById(CalendarName.toString()).style.display="block";
	window.document.getElementById(CalendarName.toString()).innerHTML=buildCal;
	window.document.getElementById(CalendarName.toString()).style.position="absolute";
	window.document.getElementById(CalendarName.toString()).style.top=20;
	window.document.getElementById(CalendarName.toString()).style.left=0;
	

}
function SetDate(day,month,year,calendarName)
{
	dayControl=calendarName+"__ctl1"
	//alert(dayControl);
	month=month+1;
	monthControl=calendarName+"__ctl2"
	yearControl=calendarName+"__ctl3"
	calSpan=calendarName+"_CalendarSpan";
	window.document.getElementById(dayControl.toString()).value=day;
	window.document.getElementById(monthControl.toString()).value=month;
	window.document.getElementById(yearControl.toString()).value=year;
	window.document.getElementById(calSpan.toString()).style.display="none";
	window.document.getElementById(calendarName.toString()).setAttribute("CalStatus",0,false);
	
	
}




		
function GetNewDate(day,month,year,action,calendarName)
{
	maximumDate=window.document.getElementById(calendarName).getAttribute("MaxDate");
	maxDate=new Date(maximumDate);
	allowNext=true;
	if ((year==maxDate.getFullYear()) && (month==11))
	{
		allowNext=false
	}
	minimumDate=window.document.getElementById(calendarName).getAttribute("MinDate");
	minDate=new Date(minimumDate);
	allowPrev=true;
	if ((year==minDate.getFullYear()) && (month==0))
	{
		allowPrev=false;
	}
	
	
		
	//sets calendar to correct month after next or prev button is pressed
	if (action=='next')
	{
		month=parseInt(month)+1;
		if (allowNext==false)
		{
			return;
		}
		if ((month==12))
		{
			
			year=year+1;
			month=0;
		}
	}
	else if (action=='prev')
	{
		if (allowPrev==false)
		{
			return;
		}
		month=parseInt(month)-1;
		if (month==-1)
		
		{
			year=year-1;
			month=11;
		}
	}
	else
	{
		month=parseInt(month);
	}
	createCalendar(day,month,year,calendarName);
}

function SetCalStatus(calName, action)
{
calSpan=calName+"_CalendarSpan";
	if (action=="out")
	{
		
		if (window.document.getElementById(calSpan.toString()).style.display=="block")
		{
			window.document.getElementById(calName.toString()).setAttribute("CalStatus",1,false);
		}
	}
	else if (action=="over")
	{
		
		if (window.document.getElementById(calSpan.toString()).style.display=="block")
		{
			window.document.getElementById(calName.toString()).setAttribute("CalStatus",0,false);
		}
	}
}

