/*
	Generic HTML Form Date-entry field popup
		Pops up a small calendar for a user to select a date into a date entry field like in Outlook 2000+
	Version: 1.4.0
	Author: Vinod Unny (vinod@isquaretech.com)
	Company: iSquare Technologies Pvt. Ltd. (www.isquaretech.com)
	Create Date: April 1, 2001
	Last Modified: April 20, 2001
	Notes:
		To use this "component", simply include this file in the HTML page and create an object in the following format:
<script>
objectVar = new DateField(fldName, Border, Padding, Spacing, Width, Value, LeftNav, RightNav, MonthType, DayType, ImagePath)
</script>
		where
			fldName: The name to give the input field which will be used to retrieve the date value
			Border: The border size in pixels for the calendar.
			Padding: The padding for each date value
			Spacing: The space between each date value
			Width: The width of the calendar. If smaller than required, it will adjust automatically
			Value: A value to be pre-filled in the date field
			LeftNav: has to be either "&lt;" for "<" or path to an image
			RightNav: has to be either "&gt;" for ">" or path to an image
			MonthType: 0 for short names (Jan, Feb...), 1 for full names (January, February...) as title
			DayType: 0 for small (S,M,T...), 1 for short (Sun,Mon...), 2 for full (Sunday...) for day titles
			ImagePath: the path to the folder where the ICO-DOWN.GIF file is stored. Default, current folder
	Example:
			<script>oCal1 = new DateField("test1",1,2,0,100,"","&lt;","&gt;",1,0, "../images/")</script>
	Customization:
		titlehough you can use the properties of the object as given above, the following CSS rules let you go even further
			.Cal: The calendat itself. Use filter transitions form 1-23 for special effects. See POPCALENDAR2.HTM
			.fldDateVal: the input element that'll hold the date
			.fldDateDown: the form down button
			.ctitleable: the entire calendar. Good for setting fonts and backgrounds
			.calNavL, .CalNavR: The cells holding the left and right navigation icons	(Redundant in 1.4.0+)
			.ctitleitle: The cell that holds the name of the month and year currently displayed
			.ctitleitleLink: The link properties for the month and year displayed (New in 1.4.0)
			.calNavLink: The links of the navigation and title
			.calDayNames: The row with the Weekday names
			.calDay: The cells that hold days
			.calDayLink: The links for the days
			.calBlank: for cells not having days
*/

function putDate(el, d,m,y)
{
	oEl = "id" + el;
	document.all['SearchHolidays_'+el+'Day'].value=d
	document.all['SearchHolidays_'+el+'Month'].value=(m+1)
	document.all['SearchHolidays_'+el+'Year'].value=y
	toggleCal(el)
}
function toggleCal(el)
{
	cal = eval(el + "Cal");
	if(cal.filters.length>0)
		cal.filters[0].Apply();
	cal.style.display=(cal.style.display=="none"?"":"none");
	if(cal.style.display=="")
	{
		oSelect = document.body.getElementsByTagName("SELECT");
		var bIsBelow = false;
		var count = 1;
		if(oSelect.length>0)
			for(i=0; i<oSelect.length; i++)
			{
				if(bIsBelow)
				{
					oSelect[i].style.visibility="hidden"
				}
				strName = oSelect[i].id
				strName = strName.substr(15,6)
				if(el == strName)
				{
					if(count==3)
						bIsBelow =true
					else
						count++;
				}	
			}
		redrawCal(this.Month, this.Year, el)
	}
	else
	{
		oSelect = document.body.getElementsByTagName("SELECT");
		if(oSelect.length>0)
			for(i=0; i<oSelect.length; i++)
				oSelect[i].style.visibility="visible"
	}
	if(cal.filters.length>0)
		cal.filters[0].Play();
}

function redrawCal(m, y, el)
{
	Month = m;
	Year = y;
	PrevMonth = m-1;
	if(PrevMonth<0)		// ie year changed
	{
		PrevYear = y-1;
		PrevMonth=11;
	}
	else
		PrevYear = y;
	NextMonth = m+1;
	if(NextMonth>11)	// ie year changed
	{
		NextYear = y+1;
		NextMonth = 0;
	}
	else
		NextYear = y;
	s1=drawCal(el);
	cal = eval(el+"Cal");
	cal.innerHTML = s1;
}

function createDateField(sName, Path, Value)
{

	document.write("<IMG style='cursor:hand' src='" + Path + "calendar.gif' onClick=\"toggleCal('" + sName +"');window.event.cancelBubble=true\" alt='Click to toggle calendar'><br>");
	document.write("<span id='" + sName + "Cal' class='Cal' style='position:absolute; z-index:1000; display:none'></span>");
}


function drawCal(el)
{
s=""
s+="<table border=" + Border + " class=ctitleable cellpadding=" + Padding + " cellspacing=" + Spacing + " width=" + Width + ">"
s+="<tr class=ctitleitle >"

s+="<td colspan=3 align=center>"

// Prev Month Nav
s+= "<a class=calNavLink href='javascript:redrawCal(" + PrevMonth
s+="," + PrevYear + ",\"" + el + "\")' title='Previous Month'>"
if(LeftNav=="&lt;")
	s+=LeftNav
else
	s+="<img src='" + ImagePath + LeftNav + "' border=0>"

s+="</a>"

// Draw month
s+=" <a title='Return to current month & year' class='ctitleitleLink' href='javascript:redrawCal(" + testDate.getMonth() + ","+ testDate.getYear() + ",\"" + el + "\")'>"
if(MonthType==0)
	s+=MonthShortNames[Month]
else
	s+=MonthFullNames[Month]
s+="</a> "

// Next Month Nav
s+="<a class=calNavLink href='javascript:redrawCal(" + NextMonth
s+="," + NextYear + ",\"" + el +"\")' title='Next Month'>"
if(RightNav=="&gt;")
	s+=RightNav
else
	s+="<img src='" + ImagePath + RightNav + "' border=0>"
s+="</a>";

// Draw Separator
s+="</td><td>&nbsp;</td> <td colspan=3 align=center>";

// Prev year Nav
s+="<a class=calNavLink href='javascript:redrawCal(" + Month
s+="," + (PrevYear-1) + ",\"" + el +"\")' title='Previous Year'>"
if(LeftNav=="&lt;")
	s+=LeftNav
else
	s+="<img src='" + ImagePath + LeftNav + "' border=0>"
s+="</a>";

// Draw Year
s+=" <a title='Return to current month & year' class='ctitleitleLink' href='javascript:redrawCal(" + testDate.getMonth() + ","+ testDate.getYear() + ",\"" + el + "\")'>"
s+=" " + Year + "</a> "

// Next Year Nav
s+="<a class=calNavLink href='javascript:redrawCal(" + Month
s+="," + (NextYear) + ",\"" + el +"\")' title='Next Year'>"
if(RightNav=="&gt;")
	s+=RightNav
else
	s+="<img src='" + ImagePath + RightNav + "' border=0>"
s+="</a>"

s+="</td></tr>"

s+="<tr>"
if(DayType==0)			// small names (S,M,T etc)
	for(i=0; i<=6; i++)
		s+="<td class=calDayNames width='14%'>"+DaySmallNames[i]+"</td>"
else
  if(DayType==1)		// Short names (Sun, Mon, Tue, etc.)
	for(i=0; i<=6; i++)
		s+="<td class=calDayNames width='14%'>"+DayShortNames[i]+"</td>"
  else
	for(i=0; i<=6; i++)	// Full names (Sunday, Monday etc)
		s+="<td class=calDayNames width='14%'>"+DayFullNames[i]+"</td>"


currDay = 1
for(i=28; i<=31; i++)
{
	tempDay = new Date(Year, Month, i);
	if (tempDay.getMonth()==Month)
		intLastDay = i;
}

now = new Date;
today = now.getDate();
todayMonth = now.getMonth();
todayYear = now.getYear();

do
{
	s+="<tr>"
	for (i=0; i<=6; i++)
	{
		tempDay = new Date(Year, Month, currDay)
		if (tempDay.getDay()==i && tempDay.getMonth()==Month)
		{
			if((currDay == today) && (Month == todayMonth) && (Year == todayYear))
			{
				s+="<td class=ctitleoday valign=top><a href='javascript:putDate(\"" + el +"\"," + currDay + "," + Month + "," + Year + ")' class=calendarLink>" + currDay + "</a></td>";
			}
			else
			{
				//if ((tempDay.getDay()==0) || (tempDay.getDay()==6))
				//	s+="<td class=calWeekend valign=top>" + currDay + "</td>";
				//else
					s+="<td class=calDay valign=top><a href='javascript:putDate(\"" + el +"\"," + currDay + "," + Month + "," + Year + ")' class=calendarLink>" + currDay + "</a></td>";
			}
			currDay = currDay + 1
			if(!Date(Month + "/" + currDay + "/" + Year))
				break;
		}
		else
			s+="<td class=calBlank>&nbsp;</td>"
	}
	tempDay2 = new Date(NextYear, NextMonth, 1)
	d = tempDay2.getDay()
	if (!Date(currDay + "/" + Month + "/" + Year) && d==1)
		for(j=d; j<=6; j++)
			s+="<td class=calBlank>&nbsp;</td>"

	s+="</tr>"
}
while (currDay < intLastDay+1)
return(s)
}

function DateField(n,b,p,s,w,v,l,r,mt,dt,ip)
{

	testDate=new Date();
	Month=testDate.getMonth();
	Year=testDate.getFullYear();
	var PrevYear;
	var PrevMonth;
	var NextYear;
	var NextMonth;
	Name=n;
	Padding=p;
	Spacing=s;
	Width=w;
	Value=v;
	LeftNav=l;
	RightNav=r;
	Border=b;
	MonthType=mt;	// 0: short, 1: long
	DayType=dt;		// 0: small, 1: short, 2: long
	ImagePath=ip;

	MonthShortNames = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	MonthFullNames =  new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	DaySmallNames = new Array("S", "M", "T", "W", "T", "F", "S");
	DayShortNames = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
	DayFullNames =  new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

	this.drawCal=drawCal;
	this.redrawCal=redrawCal;
	this.toggleCal=toggleCal;
	this.putDate=putDate;
	this.createDateField=createDateField;

	createDateField(Name, ImagePath, Value);
}