/****************************************************************
*					TDS Date Picker								*
*																*
*	Created By:		Tom Smith	tsmith@tdscode.com			   	*
*	Created Date:	10/25/2000									*
*																*
*	Modified By:	Tom Smith									*
*	Modified Date:	02/06/2003									*
*	Modified Description:										*
*		Updated a problem with the change month.  This occurred	*
*		when the current date was larger than the next months.	*
*		Example: Today 1-31-2003 would fail when going to Feb	*
*		because Feb only had 28 days.  Also fixed the leap year	*
*		problem. Thanks to Bonny Silva for finding these bugs.	*
*****************************************************************
*	For updated releases/changes visit www.tdscode.com			*
*																*
*	This code may be used freely as long as this header is 		*
*	displayed at the top of this file.							*
*																*
*	The calendar was designed for Netscape 4.x - higher	and		*
*	Internet Explorer 4.x - higher.								*
*																*
*	For instructions on using the calendar in visit 			*
*	www.tdscode.com.  Comments and suggestions are welcome.		*
*	Dowloaded code from "http://www.tdscode.com/mainapps.jsp?page=admin_list.jsp&menu=apps_1&exp=1"
****************************************************************/

//Global Variables

//Define the different browser types that are supported
var NS4 = (navigator.appName=="Netscape")&&(parseInt(navigator.appVersion)==4);
var NS6 = (navigator.appName=="Netscape")&&(parseInt(navigator.appVersion)==5);
var IE4 = document.all;

//Calendar objects
var calNS4;
var calNS6;
var calIE4;

//Holding Variables
var CALENDAR_HTML = "";
var DISPLAY_DATE = "";
var CURRENT_DATE_FIELD = "";
var FORM_NAME = "";
var HLD_DIV = "";
var BUILT = "no";
var DAYNAME_FNT_COLOR =  "white";
var DAY_FNT_COLOR = "black";
var HDR_FNT_COLOR = "#6666CC";
//var HDR_BG_COLOR =  "#6699CC";
var HDR_BG_COLOR =  "#31619C";

var CAL_BOR_COLOR = "black";
var CAL_LIGHT_COLOR= "white";
//var WEEK_BOR_COLOR =  "#6699CC";//"#A3A19D";
//var DAY_BOR_COLOR =  "#6699CC";
var WEEK_BOR_COLOR =  "#31619C";
var DAY_BOR_COLOR =  "#31619C";

var PREV_COLOR = "black";
var NEXT_COLOR = "black";
var MONTH_BGCOLOR = "#F0F8FF";//"#C9C4C1";
var PNI_MOCOLOR = "#CCCCFF";
var DAY_MOCOLOR = "#CCCCFF";
//var WEEK_BGCOLOR =  "#6699CC";
var WEEK_BGCOLOR =  "#31619C";


var DAY_BGCOLOR = "#F0F8FF"; //"#C9C7C1";
var CUR_DAY_BGCOLOR = "#9999FF";
//End of Global Variables


//Write out the style sheets
document.write("<style>");
document.write(".txtDayname { font-size: 9px; color: " + DAYNAME_FNT_COLOR + "; font-family: verdana; text-decoration: none; font-weight:bold;}");
document.write(".txtDay { font-size: 9px; color: " + DAY_FNT_COLOR + "; font-family: verdana; text-decoration: none;}");
document.write(".txtMonth { font-size: 11px; color: " + HDR_FNT_COLOR + "; font-family: verdana; text-decoration: none;}");
document.write(".tblCalendar { border: 1px solid " + CAL_BOR_COLOR + "; }");
//document.write(".tblCalendar {bgcolor:"+ CAL_BOR_COLOR + ";}");
//document.write(".tblCalendarlight {bgcolor:"+ CAL_LIGHT_COLOR + ";}");
document.write(".tdMonth { padding: 2px; }");
document.write(".tdDayW { padding: 2px; border: 1px solid " + WEEK_BOR_COLOR + "; }");
document.write(".tdDay { padding: 2px; border: 1px solid " + DAY_BOR_COLOR + "; }");
document.write(".btnPrev { font-size: 14px; color: " + PREV_COLOR + "; font-family: webdings; font-weight:bold; text-decoration: none;}");
document.write(".btnNext { font-size: 14px; color: " + NEXT_COLOR + "; font-family: webdings; font-weight:bold; text-decoration: none;}");
document.write("</style>");

//This function is used to display the calendar
function showCalendar(formname,field,date,even)
{
var xPos="";
var yPos="";
  //Set the Date field to use
  CURRENT_DATE_FIELD = field;

//Set the form name to use
  FORM_NAME=formname;
	
	
//Set the Display Date to null
  DISPLAY_DATE = "";
  var form=eval('document.'+FORM_NAME+'.'+CURRENT_DATE_FIELD);
  var txtvalue=form.value;
if(txtvalue=='Today')
  {
  DISPLAY_DATE='';
  txtvalue='';
  }
  else
  DISPLAY_DATE=txtvalue;

//  DISPLAY_DATE=txtvalue;


		
	if (BUILT == "yes")
	{
           chgCalendarDate(txtvalue);
		if (document.layers){
			this.calNS4.pageX = even.pageX;
			this.calNS4.pageY =(even.pageY-100);
			calNS4.visibility = 'visible';
                }
		else if (document.all){
			calIE4.style.top=(even.clientY+document.body.scrollTop)+'px';
                        calIE4.style.left=(even.clientX+document.body.scrollLeft+7)+'px';
			calIE4.style.visibility = 'visible';
		}
		else if (document.getElementById &&!document.all){
			calNS6.style.top=(even.clientY+document.body.scrollTop)+'px';
                        calNS6.style.left=(even.clientX+document.body.scrollLeft+7)+'px';
			calNS6.style.visibility = 'visible';
		}
		return;
	}
	
     
	//Build the Calendar
	buildCalendar();
	
	if (!xPos)
		xPos = "";
	if (!yPos)
		yPos = "";
	
	//Display the Calendar on the Web page
	if (NS4)
	{
		calNS4 = new Layer(50,this);
		calNS4.document.write(CALENDAR_HTML);
		calNS4.document.close();
		if (xPos != "" || yPos != "")
		{
			this.calNS4.pageX = xPos;
			this.calNS4.pageY = (yPos - 95);
		}
		else
		{
			this.calNS4.pageX = even.pageX;
			this.calNS4.pageY =(even.pageY-100);
		}
		calNS4.visibility = 'visible';
	}
	else if (IE4)
	{
		calIE4 = document.all[date];
				calIE4.innerHTML = CALENDAR_HTML;
		if (xPos != "" || yPos != "")
		{
			calIE4.style.posLeft = xPos;
			calIE4.style.posTop = yPos;
		}
		calIE4.style.visibility = 'visible';
	}
	else if (NS6)
	{
		calNS6 = document.getElementById(date);
		
		calNS6.innerHTML = CALENDAR_HTML;
		if (xPos != "" || yPos != "")
		{
			calNS6.style.postop = yPos;
			calNS6.style.posleft = xPos;
		}

		calNS6.style.visibility = 'visible';
	}
	
	BUILT = "yes";
}

//This function is used to hide the calendar
function hideCalendar()
{
	if (BUILT == "no")
		return;
		
	if (NS4)
		calNS4.visibility = 'hidden';
	else if (IE4)
		calIE4.style.visibility = 'hidden';
	else if (NS6)
		calNS6.style.visibility = 'hidden';
}

//This function is used to change the Calendar Date
function chgCalendarDate(dspDate)
{
	//Set the Display to the new Date
	DISPLAY_DATE = dspDate;

	//Build the Calendar
	buildCalendar();
	
	//Display the Calendar on the Web page
	if (NS4)
	{
		calNS4.document.write(CALENDAR_HTML);
		calNS4.document.close();
	}
	else if (IE4)
		calIE4.innerHTML = CALENDAR_HTML;
	else if (NS6)
		calNS6.innerHTML = CALENDAR_HTML;
}

//This function is used to change the Date field to the selected calendar date
function chgDate(txtDate)
{
	var form=eval('document.'+FORM_NAME+'.'+CURRENT_DATE_FIELD);
	
	//Change the Date field
	form.value = txtDate;
	//document.myform.datebox.value = txtDate;	
	
	//Hide the Calendar
	hideCalendar();
}

//This function is used to Build the Calendar
function buildCalendar(field)
{
	//Define the Month Names and Month Days Array
	monthNames 		= new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	monthDays 		= new Array(12);
	
	//Entry the Total Days for each Month
	monthDays[0]	= 31;
	monthDays[1]	= 28;
	monthDays[2]	= 31;
	monthDays[3]	= 30;
	monthDays[4]	= 31;
	monthDays[5]	= 30;
	monthDays[6]	= 31;
	monthDays[7]	= 31;
	monthDays[8]	= 30;
	monthDays[9]	= 31;
	monthDays[10]	= 30;
	monthDays[11]	= 31;

	//Determine the Date for the Calendar to Display
	if (DISPLAY_DATE == "")
		calendarDateObj	= new Date();
	else
		calendarDateObj = new Date(DISPLAY_DATE);
	
	//Obtain the Calendar Dates
	calendarDate	= calendarDateObj.getDate();
	calendarDay	= calendarDateObj.getDay();
	calendarMonth	= calendarDateObj.getMonth();
	calendarYear	= calendarDateObj.getYear();


       	//Obtain Todays Dates
/*	currentDateObj 	= new Date();
        currentDate 	= currentDateObj.getDate();
	currentDay 	= currentDateObj.getDay();
	currentMonth 	= currentDateObj.getMonth();
	currentYear	= currentDateObj.getYear();
*/
if(DISPLAY_DATE ==""){
        //Obtain Todays Dates
        currentDateObj  = new Date();
        currentDate     = currentDateObj.getDate();
        currentDay      = currentDateObj.getDay();
        currentMonth    = currentDateObj.getMonth();
        currentYear     = currentDateObj.getYear();

        }
        else{
         currentDateObj     = new Date(DISPLAY_DATE);
        currentDate     = currentDateObj.getDate();
        currentDay      = currentDateObj.getDay();
        currentMonth    = currentDateObj.getMonth();
        currentYear     = currentDateObj.getYear();
         }

	//*** Fix - TDS 2-06-2003 ***
	if ((calendarMonth == currentMonth) && (calendarYear == currentYear))
	{
		calendarDate = currentDate;
		calendarDay = currentDay;
	}
	
	if (LeapYear(calendarYear))
		monthDays[1] = 29;
	//*** End Fix - 2-06-2003 ***
	
	//Make sure the Year is in the correct century
	calendarYear 	= calendarYear % 100;
	calendarYear 	= ((calendarYear < 50) ? (2000 + calendarYear) : (1900 + calendarYear));


	//Calculate the Blank entries before the 1st of the month
	blankEntry = calendarDate;

	while (blankEntry > 7) 
		blankEntry-= 7;

	blankEntry = calendarDay - blankEntry + 1;
	
	if (blankEntry < 0) 
		blankEntry+= 7;
	
	//Calculate Next Months Date
	nextMonth = (calendarMonth + 2);
	currentMonth2 = calendarMonth + 1;
	
	if (nextMonth == 13)
	{
		nextMonth = 1;
		nextYear = (calendarYear + 1);
	} 
	else
		nextYear = calendarYear;
	nextYear2 = (calendarYear + 1);
	nextDate = nextMonth + "/1/" + nextYear;	//Navigating thru next months
	nextDate2 = currentMonth2 + "/1/" +  nextYear2;	//Navigating thru next years
	//Calculate Previous Months Date
	prevMonth = calendarMonth;
	
	if (prevMonth == 0)
	{
		prevMonth = 12;
		prevYear = (calendarYear - 1);
	} 
	else
		prevYear = calendarYear;
	
	prevYear2 = (calendarYear - 1);
	prevDate = prevMonth + "/1/" + prevYear;	//Navigating thru previous months
	prevDate2 = currentMonth2 + "/1/" +  prevYear2;	//Navigating thru pevious years
	//Build the Header for the Calendar
	CALENDAR_HTML = "<table class=\"tblCalendar\" cellpadding=0 cellspacing=0>";
	CALENDAR_HTML += "<tr>";

	CALENDAR_HTML += "<td bgcolor=" + MONTH_BGCOLOR + " align=center class=\"tdMonth\" onMouseOver=mOvr(this,'" + PNI_MOCOLOR + "') onMouseOut=mOut(this,'" + MONTH_BGCOLOR + "') onClick=chgCalendarDate('" + prevDate2 + "')><a href=javascript:chgCalendarDate('" + prevDate2 + "')><img src='../images/fastbackward.gif' border=0 alt='Previous Year'></img></a></td>";

	CALENDAR_HTML += "<td bgcolor=" + MONTH_BGCOLOR + " align=center class=\"tdMonth\" onMouseOver=mOvr(this,'" + PNI_MOCOLOR + "') onMouseOut=mOut(this,'" + MONTH_BGCOLOR + "') onClick=chgCalendarDate('" + prevDate + "')><a href=javascript:chgCalendarDate('" + prevDate + "')><img src='../images/backward.gif' border=0 alt='Previous Month'></img></a></td>";

	CALENDAR_HTML += "<td  bgcolor=" + MONTH_BGCOLOR + " align=center class=\"tdMonth\" colspan=3><font class=\"txtMonth\"><b>" + monthNames[calendarMonth] + " " + calendarYear + "</b></font></td>";

	CALENDAR_HTML += "<td bgcolor=" + MONTH_BGCOLOR + " align=center class=\"tdMonth\" onMouseOver=mOvr(this,'" + PNI_MOCOLOR + "') onMouseOut=mOut(this,'" + MONTH_BGCOLOR + "') onClick=chgCalendarDate('" + nextDate + "')><a href=javascript:chgCalendarDate('" + nextDate + "')><img src='../images/forward.gif' border=0 alt='Next Month'></img></a></td>";

	CALENDAR_HTML += "<td bgcolor=" + MONTH_BGCOLOR + " align=center class=\"tdMonth\" onMouseOver=mOvr(this,'" + PNI_MOCOLOR + "') onMouseOut=mOut(this,'" + MONTH_BGCOLOR + "') onClick=chgCalendarDate('" + nextDate2 + "')><a href=javascript:chgCalendarDate('" + nextDate2 + "')><img src='../images/fastforward.gif' border=0 alt='Next Year'></img></a></td>";

	CALENDAR_HTML += "</tr>";
	CALENDAR_HTML += "<tr>";
	CALENDAR_HTML += "<td bgcolor=" + WEEK_BGCOLOR + " align=center class=\"tdDayW\"><font class=\"txtDayname\">SUN</font></td>";
	CALENDAR_HTML += "<td bgcolor=" + WEEK_BGCOLOR + " align=center class=\"tdDayW\"><font class=\"txtDayname\">MON</font></td>";
	CALENDAR_HTML += "<td bgcolor=" + WEEK_BGCOLOR + " align=center class=\"tdDayW\"><font class=\"txtDayname\">TUE</font></td>";
	CALENDAR_HTML += "<td bgcolor=" + WEEK_BGCOLOR + " align=center class=\"tdDayW\"><font class=\"txtDayname\">WED</font></td>";
	CALENDAR_HTML += "<td bgcolor=" + WEEK_BGCOLOR + " align=center class=\"tdDayW\"><font class=\"txtDayname\">THR</font></td>";
	CALENDAR_HTML += "<td bgcolor=" + WEEK_BGCOLOR + " align=center class=\"tdDayW\"><font class=\"txtDayname\">FRI</font></td>";
	CALENDAR_HTML += "<td bgcolor=" + WEEK_BGCOLOR + " align=center class=\"tdDayW\"><font class=\"txtDayname\">SAT</font></td>";
	CALENDAR_HTML += "</tr>";
	CALENDAR_HTML += "<tr>";

	//Build a blank cell for each Blank Entry before the 1st of the Month
	for (s = 0;s < blankEntry;s++) 
		CALENDAR_HTML += "<td bgcolor=" + DAY_BGCOLOR + " align=center class=\"tdDay\"><font class=\"txtDay\">&nbsp;</font></td>";

	//Start the Day Counter
	dayCount = 1;

	//Loop through until the total days of calendar month have been reached
	while (dayCount <= monthDays[calendarMonth]) 
	{
		//Start a new table row each time through the loop except the first time
		if (dayCount > 1)
			CALENDAR_HTML += "<tr>";
		
		//Start at the next cell after the blank entries and entry the cells across the row.
		for (b = blankEntry;b < 7;b++) 
		{
			//If the Day Counter is greater than the Month total then add blank entries
			if (dayCount <= monthDays[calendarMonth])
			{
				var str_dayCount;
				if (dayCount<10) str_dayCount="0"+dayCount; else  str_dayCount=dayCount;
				if ((calendarMonth + 1)<10) str_month="0"+(calendarMonth + 1);else str_month=(calendarMonth + 1);	

				/*
				tdProperties = "onClick=chgDate('" + str_dayCount + "-" + str_month +  "-" + calendarYear + "') ";
				tdData = "<a href=javascript:chgDate('" + str_dayCount + "-" + str_month + "-"  + calendarYear + "')><font class=\"txtDay\">" + dayCount + "</font></a>";
				*/
				
				tdProperties = "onClick=chgDate('" + str_month + "/" + str_dayCount +  "/" + calendarYear + "') ";
				tdData = "<a href=javascript:chgDate('" + str_month + "/" + str_dayCount + "/"  + calendarYear + "')><font class=\"txtDay\">" + dayCount + "</font></a>";
				

				//If the current day is equal to the calendar day and the
				//current month is equal to the calendar month, check the dates
				//if ((currentDay == calendarDay) && (currentMonth == calendarMonth))
				{
					//If the day counter is equal to the current date then change the color
					if (dayCount == currentDate)
						tdProperties = tdProperties + "bgcolor=" + CUR_DAY_BGCOLOR + "";
					else 
						tdProperties = tdProperties + "onMouseOver=mOvr(this,'" + DAY_MOCOLOR + "') onMouseOut=mOut(this,'" + DAY_BGCOLOR + "') bgcolor=" + DAY_BGCOLOR + "";
				} 
				//else 
					//tdProperties = tdProperties + "onMouseOver=mOvr(this,'" + DAY_MOCOLOR + "') onMouseOut=mOut(this,'" + DAY_BGCOLOR + "') bgcolor=" + DAY_BGCOLOR + "";
			} 
			else
			{
				tdProperties = "bgcolor=" + DAY_BGCOLOR;
				tdData = "<font class=\"txtDay\">&nbsp;</font>";
			}
		
			//Build the table cells
			CALENDAR_HTML += "<td align=center class=\"tdDay\" " + tdProperties + ">" + tdData + "</td>";
		
			//Add to the Day counter
			dayCount++;
		}
	
		//End the table row
		CALENDAR_HTML += "</tr>";
		
		//Set the Blank entries to zero
		blankEntry=0;
	}

	//End the table
	CALENDAR_HTML += "<tr>";
	CALENDAR_HTML += "<td bgcolor=" + WEEK_BGCOLOR + " colspan=7 class=\"tdMonth\" align=center><a href=javascript:hideCalendar();><font class=\"txtDayname\">CLOSE&nbsp;CALENDAR</font></a></td>";
	CALENDAR_HTML += "</tr>";
	CALENDAR_HTML += "</table>";
}

//*** Fix - TDS 2-06-2003 ***
//This fuction checks for the Leap Year
function LeapYear(year) 
{
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}
//*** End Fix - 2-06-2003 ***

//This function is used to control the mouse over event.
function mOvr(src,clrOver)
{ 
//alert(event.fromElement);
//	if (!src.contains(event.fromElement))
//	{ 
		src.style.cursor = 'hand'; 
		src.bgColor = clrOver;
//	} 
}

//This function is used to control the mouse out event.
function mOut(src,clrIn)
{ 
//	if (!src.contains(event.toElement))
//	{ 
		src.style.cursor = 'default'; 
		src.bgColor = clrIn; 
//	} 
}
