/*
	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:
		Although you can use the properties of the object as given above, the following CSS rules let you go even further
			.Cal: The calendar 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
			.calTable: 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+)
			.calTitle: The cell that holds the name of the month and year currently displayed
			.calTitleLink: 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 emptyDate(el)
{
	//oEl = "id" + el;
	//document.all[oEl].value=""
	toggleCal(el)
}

function putDate(el, m,d,y)
{
	//oEl = "id" + el;
	//document.all[oEl].value=d+"/"+(m+1)+"/"+y
	toggleCal(el)
	if (this.station!='') {sid='&sid='+this.station;}
	if (this.selection!='') {lc='&lc='+this.selection;}
	document.location.href='default.asp?pid='+this.pageId+'&gid='+this.geoId+sid+'&la='+this.langId+lc+'&nDate='+d+"/"+(m+1)+"/"+y
}
function toggleCal(el)
{
	cal = eval(el + "Cal");
	//alert(cal.filters.length);
	//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");
// 		if(oSelect.length>0)
// 			for(i=0; i<oSelect.length; i++)
// 				oSelect[i].style.visibility="hidden"
// 		
// 		oSelect = document.body.getElementsByTagName("IFRAME");
// 		if(oSelect.length>0)
// 			for(i=0; i<oSelect.length; i++)
// 			{	oSelect[i].style.display="none"
// 				
// 			}
		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"
// 				
// 		oSelect = document.body.getElementsByTagName("IFRAME");
// 		if(oSelect.length>0)
// 			for(i=0; i<oSelect.length; i++)
// 			{	oSelect[i].style.display="block"
// 				
// 			}
	}
	//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-1;
	NextMonth = m+1;
	if(NextMonth>11)	// ie year changed
	{
		NextYear = y+1;
		NextMonth = 0;
	}
	else
		NextYear = y+1;
	s1=drawCal(el);
	cal = eval(el+"Cal");
	cal.innerHTML = s1;
}

function createDateField(sName, Path, Value)
{
	//document.write("<input readonly onClick=\"toggleCal('" + sName +"')\" class=fldDateVal id=id" + sName + " name=" + sName + " type=text value='" + Value +"'>");
	//document.write("<BUTTON class=fldDateDown id='" + sName + "Down' onClick=\"toggleCal('" + sName +"')\" title='Click to toggle calendar'>");

 	if(nobreak!=1){
 		document.write("<IMG src='" + Path + "ico-down.gif' width='7' height='7'></BUTTON><br>");		
 	}else{
 		document.write("<IMG src='" + Path + "ico-down.gif' width='7' height='7'></BUTTON>");
 	}
	if(nobreak!=1){
		document.write("<div class=calImg><IMG class=fldDateDown id='" + sName + "Down' onClick=\"toggleCal('" + sName +"')\" title='Click to toggle calendar' src='" + Path + "calendar1.gif' width='19' height='20'></div><br><br>");		
	}else{
		document.write("<div class=calImg><IMG class=fldDateDown id='" + sName + "Down' onClick=\"toggleCal('" + sName +"')\" title='Click to toggle calendar' src='" + Path + "calendar1.gif' width='19' height='20'></div>");
	}
	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=calTable cellpadding=" + Padding + " cellspacing=" + Spacing + " width=" + Width + ">"


s+="<tr class=calTitle >"

s+="<td align=center>"

// Prev Month Nav

monthToLink = PrevMonth;
if(PrevMonth==11)
	yearToLink = PrevYear
else
	yearToLink = Year

if ((monthToLink>=currentMonth-monthsBack && currentMonth!=0 && Year==currentYear) || ( monthToLink>11+currentMonth-monthsBack && Year==currentYear-1 )){
s+= "<a class=calNavLink href='javascript:redrawCal(" + monthToLink
s+="," + yearToLink + ",\"" + el + "\")' title='Previous Month'>"

if(LeftNav=="&lt;")
	s+=LeftNav
else
	s+="<img src='" + ImagePath + LeftNav + "' border=0>"

s+="</a>"
}else{
s+= "<a class=calNavLink href='javascript:redrawCal(" + monthToLink
s+="," + yearToLink + ",\"" + el + "\")' title='Previous Month'>"

if(LeftNav=="&lt;")
	s+=LeftNav
else
	s+="<img src='" + ImagePath + LeftNav + "' border=0>"

s+="</a>"
}

s+="</td>"

// Draw month & year
s+="<td colspan=4 align=center nowrap>"

s+=" <a title='Return to current month & year' class='calTitleLink' href='javascript:redrawCal(" + testDate.getMonth() + ","+ testDate.getYear() + ",\"" + el + "\")'>"
if(MonthType==0)
	s+=MonthShortNames[Month]
else
	s+=MonthFullNames[Month]
s+=" " + Year + "</a></td>"

// Next Month Nav

monthToLinkNext = NextMonth;
if(NextMonth==0)
	yearToLink = NextYear
else
	yearToLink = Year
s+="<td align=center>"
if ((monthToLinkNext<=currentMonth && currentMonth!=11 && Year==currentYear) || Year==currentYear-1){
s+="<a class=calNavLink href='javascript:redrawCal(" + monthToLinkNext
s+="," + yearToLink + ",\"" + el +"\")' title='Next Month'>"

if(RightNav=="&gt;")
	s+=RightNav
else
	s+="<img src='" + ImagePath + RightNav + "' border=0>"

s+="</a>";
}
s+="</td>";

// Draw Separator
s+="<td><a class=calNavLink href='javascript:emptyDate(\""+el+"\");'>x</a></td></tr>";

 s+="<tr class=calTitle >"
 
 s+="<td colspan=3 align=center nowrap>"
 
 // Prev Month Nav
 
 prev= PrevMonth;
 //if ((prev>=currentMonth-1 && currentMonth!=0 && Year==currentYear) || (currentMonth==0 && prev==11 && Year==currentYear-1 )){
 //s+= "<a class=calNavLink href='javascript:redrawCal(" + PrevMonth
 //s+="," + Year + ",\"" + el + "\")' title='Previous Month'>"
 ////}
 //if(LeftNav=="&lt;")
// 	s+=LeftNav
 //else
// 	s+="<img src='" + ImagePath + LeftNav + "' border=0>"
 ////if ((prev>=currentMonth-1 && currentMonth!=0) || (currentMonth==0 && prev==11 && prevy==currentYear-1 )){
 //s+="</a>"
 //}
// 
// 
 // Draw month
 s+=" <a title='Return to current month & year' class='calTitleLink' href='javascript:redrawCal(" + testDate.getMonth() + ","+ testDate.getYear() + ",\"" + el + "\")'>"
 if(MonthType==0)
 	s+=MonthShortNames[Month]
 else
 	s+=MonthFullNames[Month]
 s+="</a> "
 
 // Next Month Nav
 next= NextMonth;
 if ((next<=currentMonth && currentMonth!=11 && Year==currentYear) || (currentMonth==11 && next==0 && Year==currentYear )){
 s+="<a class=calNavLink href='javascript:redrawCal(" + NextMonth
 s+="," + Year + ",\"" + el +"\")' title='Next Month'>"
 //}
 if(RightNav=="&gt;")
 	s+=RightNav
 else
 	s+="<img src='" + ImagePath + RightNav + "' border=0>"
 //if ((next<=currentMonth && currentMonth!=11 && Year==currentYear) || (currentMonth==11 && next==0 && nexty==currentYear )){
 s+="</a>";
 }
// 
// 
// // Draw Separator
 s+="</td><td><a class=calNavLink href='javascript:emptyDate(\""+el+"\");'>x</a></td> <td colspan=3 align=center nowrap>";
// 
// // Prev year Nav
 if(currentMonth==0){
 	prev= PrevMonth;
 	if (Year==currentYear){
 		s+="<a class=calNavLink href='javascript:redrawCal(" + Month
 		s+="," + (PrevYear) + ",\"" + el +"\")' title='Previous Year'>"
 	}
 	if(LeftNav=="&lt;")
 		s+=LeftNav
 	else
 		s+="<img src='" + ImagePath + LeftNav + "' border=0>"
 	if (Year==currentYear){
 		s+="</a>";
 	}
 }
 
// // Draw Year
 s+="<a title='Return to current month & year' class='calTitleLink' href='javascript:redrawCal(" + testDate.getMonth() + ","+ testDate.getYear() + ",\"" + el + "\")'>"
 s+=" " + Year + "</a> "
 
// // Next Year Nav
 if(currentMonth==0){
 	if (Year==currentYear-1){
 		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>"
 	if (Year==currentYear-1){
 		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;
}

do
{
	s+="<tr>"
	for (i=0; i<=6; i++)
	{
		tempDay = new Date(Year, Month, currDay)
		if (tempDay.getDay()==i && tempDay.getMonth()==Month)
		{
			s+="<td class=calDay valign=top>"
			if(Month != currentMonth || (Month == currentMonth && currDay<=Value)){
				s+="<a href='javascript:putDate(\"" + el +"\"," + Month + "," + currDay + "," + Year + ")' class=calDayLink>"
			}
			s+=currDay
			if(Month != currentMonth || (Month == currentMonth && currDay<=Value)){
				s+="</a>"
			}
			s+="</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(Month + "/" + currDay + "/" + 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, br,page,gid,la,sid,sel,back)
{
	testDate=new Date();
	Month=testDate.getMonth();
	Year=testDate.getFullYear();
	currentMonth = Month;
	currentYear = Year;
	monthsBack=back;
	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;
	nobreak = br;
	pageId = page;
	geoId = gid;
	langId = la;
	station= sid;
	selection=sel;
		
	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);
}
