var mSelectedDay;
var mSelectedMonth
var mSelectedYear;
var mHighlightedDay = 0;
var mCurrentMonth;
var mCurrentYear;
var mOnHideFunction;

function setSelectedDates(dateString)
{
	var currentSelectedDate = new Date(dateString);
	if (isNaN(currentSelectedDate))
	{
		currentSelectedDate = new Date();
	}
	
	mSelectedDay = currentSelectedDate.getDate();
	mSelectedMonth = currentSelectedDate.getMonth();
	mSelectedYear = currentSelectedDate.getFullYear();
	mCurrentMonth = mSelectedMonth;
	mCurrentYear = mSelectedYear;
}

function generateCalendar(month, year, theDivTag)
{
	//-- Arrows and Month
	var headingHTML = "<table class='monthHeadingTable' border='0'>";
	headingHTML += "<tr><td class='changeMonthTD'>";
	headingHTML += "<img src='/aspnet_client/socket/calendar/images/prev.gif' title='Show Previous Month' ";
	headingHTML += "onclick='goPreviousMonth(\"";
	headingHTML += theDivTag + "\")'></td>";
	headingHTML += "<td class='monthHeadingTD'>";
	headingHTML += getMonthByName(month) + " " + year + "</td>";
	headingHTML += "<td class='changeMonthTD'>";
	headingHTML += "<img src='/aspnet_client/socket/calendar/images/next.gif' title='Show Next Month' ";
	headingHTML += "onclick='goNextMonth(\"";
	headingHTML += theDivTag + "\")'></td></tr></table>";
	//-- Days
	var tableInnerHTML = "";
	tableInnerHTML = "<tr class='dayHeadTR'><TD class='dayHeadTD'>S</td>";
	tableInnerHTML += "<td class='dayHeadTD'>M</td>";
	tableInnerHTML += "<td class='dayHeadTD'>T</td>"
	tableInnerHTML += "<td class='dayHeadTD'>W</td>";
	tableInnerHTML += "<td class='dayHeadTD'>T</td>";
	tableInnerHTML += "<td class='dayHeadTD'>F</td>";
	tableInnerHTML += "<td class='dayHeadTD'>S</td>";
	tableInnerHTML += "</tr>"
	var elementIndex = 1;
	var iCellCounter;
	var iStartDayOfMonth = getStartDayOfMonthNumber(month,year);
	var iLastDayOfMonth = getDaysInAMonth(month,year);
	var tableRows = 6;
	for (weekIndex = 1; weekIndex <= tableRows; weekIndex++)
	{
		tableInnerHTML = tableInnerHTML + "<tr>";
		var dayCounter;
		
		for (dayCounter = 1; dayCounter <= 7; dayCounter++)
		{
			if (elementIndex <= iStartDayOfMonth ||elementIndex > (iLastDayOfMonth + iStartDayOfMonth) )
			{
				tableInnerHTML += "<td class='emptyTD' id='dayTD"; 
				tableInnerHTML += elementIndex;
				tableInnerHTML += "'>&nbsp</td>";
			}
			else
			{
				tableInnerHTML += "<td class='dayTD' onclick='updateSelectedDate("; 
				tableInnerHTML += (elementIndex - iStartDayOfMonth); 
				tableInnerHTML += "," + month + ","; 
				tableInnerHTML += year + "); ";
				tableInnerHTML += " hideCalendar(\"" + theDivTag; 
				tableInnerHTML += "\"); "; 
				tableInnerHTML += "'id='dayTD" + elementIndex +"'>"; 
				tableInnerHTML += (elementIndex - iStartDayOfMonth); 
				tableInnerHTML += "</td>";
			}
			elementIndex++;
		}
		tableInnerHTML = tableInnerHTML + "</tr>";
	}
	
	var calendarDivObject = getTagById(theDivTag)
	calendarDivObject.innerHTML = headingHTML + 
	"<table style='calendarTable' cellspacing='0'><tbody>" + tableInnerHTML + "</tbody></table>" + 
	"<a href='javaScript:cancelCalendar(\"" + theDivTag + "\")' class='cancelHyperlink'>Cancel</a>";
	if (mCurrentMonth == mSelectedMonth && mCurrentYear == mSelectedYear)
	{
		highlightDay(mSelectedDay, mSelectedMonth, mSelectedYear);
	}
}

function getTagById(tagId)
{
	if(document.getElementById)
	{
		return document.getElementById(tagId);
	}
	if (document.all)
	{
	 	return document.all.item(tagId);
	}
	return null;
}

function getStartDayOfMonthNumber(month,year)
{
	 var newDate = new Date();
	 newDate.setMonth(month)
	 newDate.setFullYear(year);
	 newDate.setDate(1);
	
	 return newDate.getDay();
}

function getDaysInAMonth(month, year)
{
	var nowDate;
	month = parseInt(month)
	year = parseInt(year)
	var monthDays = new Array(31,29,31,30,31,30,31,31,30,31,30,31)
	
	// If Feb then need to check for leap years
	if (month == 1)
	{
		nowDate = new Date();
		nowDate.setMonth(1);
		nowDate.setYear(year);
		nowDate.setDate(29);
		if (nowDate.getMonth() == 2)
		{
		   monthDays[1] = 28;
		}
	}
	
	return monthDays[month];
}

function getMonthName(month)
{
	var months = new Array("01","02","03","04","05","06","07","08","09","10","11","12");
	return months[month];
}
function getMonthByName(month)
{
	var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	return months[month];
}

function highlightDay(day, month, year)
{
	var idOfCellToHighlight = day + getStartDayOfMonthNumber(month,year);
	idOfCellToHighlight = "dayTD" + idOfCellToHighlight;
	getTagById(idOfCellToHighlight).style.backgroundColor = 'lightgrey';
}

function updateSelectedDate(day,month,year)
{
	mSelectedDay = day;
	mSelectedMonth = month;
	mSelectedYear = year;
}

function goPreviousMonth(theDivTag)
{
	mCurrentMonth--;
	if (mCurrentMonth < 0)
	{
		mCurrentMonth = 11;
	 	mCurrentYear--;
	}
	
	generateCalendar(mCurrentMonth, mCurrentYear,theDivTag);
}

function goNextMonth(theDivTag)
{
	mCurrentMonth++;
	if (mCurrentMonth > 11)
	{
	   mCurrentMonth = 0;
	   mCurrentYear++;
	}
	
	generateCalendar(mCurrentMonth, mCurrentYear,theDivTag);
}

function cancelCalendar(theDivTag)
{
	//-- I can't get the visibility to work so we have to hide it offscreen for now
	getTagById(theDivTag).style.left = "-100px";
	getTagById(theDivTag).style.top = "-2000px";
	 
	//var vis = (getTagById(theDivTag).style.visibility == 'hidden') ? 'visible'	: 'hidden';
	//alert(vis);
	//getTagById(theDivTag).style.visibility = vis;
}

function hideCalendar(theDivTag)
{
  	cancelCalendar(theDivTag);
  	mOnHideFunction(mSelectedDay, getMonthName(mSelectedMonth), mSelectedYear);
}

function showCalendar(xPos, yPos, theDivTag, onHideFunction)
{
	if(document.getElementById || document.all)
	{
		getTagById(theDivTag).style.left = xPos;
		getTagById(theDivTag).style.top = yPos;
		mOnHideFunction = onHideFunction;
	}
}


//-- The textfield and div tag need to be global
var TextField ;
var DivTag ;
 //-- Err: Null is null or not an object - Add the Div or form tag dummy.
function setDate(day, month, year, textField)
{
	var doc = document.forms[0];
	doc.elements[TextField].value = month + "/" + day + "/" + year;
}

function pickDate(event, textField, divTag)
{
	if(document.getElementById || document.all)
	{
		DivTag = divTag;
		TextField = textField
		var doc = document.forms[0];
		
		if(doc == null)	alert("You forgot the form tag dummy!");
		if(getTagById(DivTag)== null) alert("You forgot the div tag dummy!");
			
	    var dateString = doc.elements[TextField].value;
	    
	    setSelectedDates(dateString);
	    generateCalendar(mCurrentMonth, mCurrentYear, DivTag);
	    showCalendar(event.clientX, event.clientY, DivTag, setDate);
	}
}
