//Current date in form script
//By Lee Hinder (lee.hinder@ntlworld.com)
//Visit http://javascriptkit.com for this script and more

//set todays date

Now = new Date();
NowDay = Now.getDate();
NowMonth = Now.getMonth();
NowYear = Now.getYear();
if (NowYear < 2000) NowYear += 1900; //for Netscape


//function for returning how many days there are in a month including leap years

function DaysInMonth(WhichMonth, WhichYear)
{
	var DaysInMonth = 31;

	if (WhichMonth == "Apr" || WhichMonth == "Jun" || WhichMonth == "Sep" || WhichMonth == "Nov") DaysInMonth = 30;

	if (WhichMonth == "Feb" && (WhichYear/4) != Math.floor(WhichYear/4))	DaysInMonth = 28;

	if (WhichMonth == "Feb" && (WhichYear/4) == Math.floor(WhichYear/4))	DaysInMonth = 29;

	return DaysInMonth;
}


//function to change the available days in a months

function ChangeOptionDays(Which)
{
	DaysObject = eval("document.theForm." + Which + "Day");
	MonthObject = eval("document.theForm." + Which + "Month");
	YearObject = eval("document.theForm." + Which + "Year");

	Month = MonthObject[MonthObject.selectedIndex].text;
	Year = YearObject[YearObject.selectedIndex].text;

	DaysForThisSelection = DaysInMonth(Month, Year);

	CurrentDaysInSelection = DaysObject.length;

	if (CurrentDaysInSelection > DaysForThisSelection)
	{
		for (i=0; i<(CurrentDaysInSelection-DaysForThisSelection); i++)
		{
			DaysObject.options[DaysObject.options.length - 1] = null
		}
	}

	if (DaysForThisSelection > CurrentDaysInSelection)
	{
		for (i=0; i<(DaysForThisSelection-CurrentDaysInSelection); i++)
		{
			NewOption = new Option(DaysObject.options.length + 1);
			DaysObject.add(NewOption);
		}
	}

	if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;
}


//function to set options to today

function SetToToday(Which)
{

	DaysObject = eval("document.theForm." + Which + "Day");
	MonthObject = eval("document.theForm." + Which + "Month");
	YearObject = eval("document.theForm." + Which + "Year");


	YearObject[0].selected = true;
	MonthObject[NowMonth].selected = true;

	ChangeOptionDays(Which);

	DaysObject[NowDay-1].selected = true;
}


//function to write option years plus x

function WriteYearOptions(YearsAhead)
{
	line = "";

	for (i=0; i<YearsAhead; i++)
	{
		line += "<OPTION>";
		line += NowYear + i;
	}

	return line;
}

/////////////////////////////////////////////////////////////////////////////////////////////

function makeValidDate() {
  year = document.theForm.selYear.options[ document.theForm.selYear.selectedIndex ].value;
  month = document.theForm.selMonth.options[ document.theForm.selMonth.selectedIndex ].value;
  day = document.theForm.selDay.options[ document.theForm.selDay.selectedIndex ].value;
  maxDay = 31;
  if ( month == 4 || month == 6 || month == 9 || month == 11 ) {
    maxDay = 30;
  } else if ( month == 2 ) {
    if ( year%100 != 0 && year%4 == 0 ) {
      maxDay = 29;
    } else {
      maxDay = 28;
    }
  }
  document.theForm.selDay.selectedIndex = Math.min(day, maxDay)-1;
}

function isBrowserSupp() {
// ****************************************************************
// *    Author: Geraldine Healy         Date:   25/08/1998
// *    Description: Checks if browser is Netscape 2.0 since the options 
// *                            array properties don't work with Netscape 2.0x
// ****************************************************************

    // Get the version of the browser
    version =  parseFloat( navigator.appVersion );

    if ( ( version >= 2.0 ) && ( version < 2.1 ) && ( navigator.appName.indexOf( "Netscape" ) != -1 ) ) {
        return false;
    }
    else {
        return true;
    }                  
}


function isLeapYear(yrStr)
{
// ****************************************************************
// *    Author: Geraldine Healy         Date:   25/08/1998
// *    Description:    Checks if Year selected is a leap year
// ****************************************************************
var leapYear=false;
// every fourth year is a leap year
if ((parseInt(yrStr, 10)%4) == 0)
        {
        leapYear=true;
        }
return leapYear;
}

function getDaysInMonth(mthIdx, YrStr)
// ****************************************************************
// *    Author: Geraldine Healy         Date:   25/08/1998
// *    Description:    Retrieves the number of days in a given month
// ****************************************************************
{
//Default number of days in a month is 31
var maxDays=31
// expect Feb. 
if (mthIdx==2) 
        {
        if (isLeapYear(YrStr))
                {
                maxDays=29;
                }
        else 
                {
                maxDays=28;
                }
        }
// All the rest of the months have 30 days
if (mthIdx==4 || mthIdx==6 || mthIdx==9 || mthIdx==11)
        {
        maxDays=30;
        }
return maxDays;
}


function adjustDate(mthIdx, Dt, Yr) 
// ****************************************************************
// *    Author: Geraldine Healy         Date:   25/08/1998
// *    Description:    Adjusts the format of the Date
// ****************************************************************
{
var value=0;            
var numDays=getDaysInMonth(mthIdx, Yr.options[Yr.options.selectedIndex].value);

if (mthIdx==2) 
        {
        if (Dt.options.selectedIndex < numDays)
                {
                return 0;
                }
        else 
                {
                //check for leap year
                Dt.options.selectedIndex=numDays;
                if (numDays==29)
                        {
                        return 99;
                        }
                else 
                        {
                        return 1;
                        }
                }
        }
if (Dt.options.selectedIndex < numDays)
        {
        value=0;
        }
else 
        {
        if (Dt.options.selectedIndex > numDays)
                {
                Dt.options.selectedIndex;
                value=3;
                }
        else 
                {
                //index is 31 or 30
                value=2;
                }
        }
return value;
}



function parseMonth(mth, inM)
// ****************************************************************
// *    Author: Geraldine Healy         Date:   25/08/1998
// *    Description:    Parses a string and returns a month value
// ****************************************************************
{
var i=1;
var retval =1;
for (i=1;i<=12;i++)
        {
        if (mth == inM.options[i].value)
                {
                retval=i;       
                break;
                }       
        }
        return retval;
}

function parseDay(day, inD)
// ****************************************************************
// *    Author: Geraldine Healy         Date:   25/08/1998
// *    Description:    Parses a string and returns a day value
// ****************************************************************
{
var i=1;
var retval =1;
for (i=1;i<=31;i++)
        {
        if (day == inD.options[i].value)
                {
                retval=i;       
                break;
                }       
        }
return retval;
}

function parseYear(year, inY)
// ****************************************************************
// *    Author: Geraldine Healy         Date:   25/08/1998
// *    Description:    Parses a string and returns a year value
// ****************************************************************
{
var retval=0;
var i=0;
     for (i=0; i<=5; i++)
     {
   
        if (year == inY.options[i].value)
                {
                retval=i;       
                break;
                }       
     }
return retval;
}

//Calendar Section
//calculation functions
function nextMonth(month)
{
if (month==12)
        {
        return 1;
        }
else
        {
        return (month+1);
        }
}


function prevMonth(month) 
{
var prevMonth = (month-1)
if (month==1)
        {
        prevMonth = 12;
        }
return prevMonth
}

function changeYear(direction,month,year)
{
var theYear = year
if (direction=="next")
        {
        if (month == 12)
                {
                theYear = (year+1)
                }
        }
if (direction=="prev")
        {
        if (month == 1)
                {
                theYear = (year-1)
                }
        }
return theYear
}


function createCalendar(month,year,io) 
{
if (!isBrowserSupp())
        {
        alert("Your browser is outdated and does not support this feature")
        return;
        }
if (navigator.appVersion.indexOf("Mac",0) != -1) 
        {
        calendarWindow = window.open("","Calendar","width=165,height=200,resizable=yes,scrollbars=no");
        } 
else 
        {
        calendarWindow = window.open("","Calendar","width=165,height=200,resizable=yes,scrollbars=no");
        }
        var mthIdx = month.options.selectedIndex
        var mthVal = month.options[mthIdx].value
        var yearVal = year.options[year.options.selectedIndex].value
        //call the function to populate the window
        generateCalendar(calendarWindow,mthVal,yearVal,io)
}


//generates the meat of the calendar
function generateCalendar(target,month,year,io)
// ****************************************************************
// *    Author: Geraldine Healy         Date:   25/08/1998
// *    Description:    generates the contents of the calender window
// **************************************************************** 
{
if (!isBrowserSupp())
        {
        return;
        }       
var monthName = new Array ("January","February","March","April","May","June","July","August","September","October","November","December")

//begin table for calendar
target.document.open()
calendar = "<html><head><title>calendar</title></head>"
calendar +="<link rel=\"stylesheet\" href=\"c/style.css\" type=\"text/css\">"
calendar +="<body bgcolor=000000 link='#000080'>"
calendar +="<table border=0 cellspacing=1 cellpadding=3 width=105 bgcolor=FF9900>"
calendar +="<tr valign=top>"

//The parseInt function parses the string argument as a signed decimal integer. 
var mthIdx = parseInt(month);
var endday = getDaysInMonth(mthIdx, year)

//month header
calendar +="<td colspan=7 align=center class='JSCalendarTitle'>"
var index = (mthIdx-1)
calendar +="<b><span class='BElarge'>" + monthName[index] + " " + year + "</span></b></td></tr>"
calendar +="</tr>"

//writes in the day of the week labels
calendar +="<tr align=center>"
calendar +="<td width=15 class='JSCalendarWeekend'><span class='BEsmall'>&nbsp;<b>S</b></font></td>"
calendar +="<td width=15 class='JSCalendarWeek'><span class='BEsmall'>&nbsp;<b>M</b></font></td>"
calendar +="<td width=15 class='JSCalendarWeek'><span class='BEsmall'>&nbsp;<b>T</b></font></td>"
calendar +="<td width=15 class='JSCalendarWeek'><span class='BEsmall'>&nbsp;<b>W</b></font></td>"
calendar +="<td width=15 class='JSCalendarWeek'><span class='BEsmall'>&nbsp;<b>T</b></font></td>"
calendar +="<td width=15 class='JSCalendarWeek'><span class='BEsmall'>&nbsp;<b>F</b></font></td>"
calendar +="<td width=15 class='JSCalendarWeekend'><span class='BEsmall'>&nbsp;<b>S</b></font></td>"
calendar +="</tr>"

wholeDate = month + "/01/" + year
thedate = new Date(wholeDate)
firstDay = thedate.getDay()

selectedmonth = mthIdx;
var today = new Date();
var thisyear = today.getYear() + 1900;
selectedyear = year

var lastDay = (endday + firstDay+1)
var lastCalspace = 42

var NoDays = (lastDay - (firstDay + 1))
var TotalDaysFilled = NoDays + firstDay;

calendar +="<tr>"
for (var i = 1; i <= lastCalspace; i++)
        {
		
        if (i <= firstDay)
                {
                // 'empty' boxes prior to first day
                calendar +="<td class='JSCalendarDays'><img src='images/trans.gif' height=1 width=1></td>"
                }
        if (i > firstDay && i <= TotalDaysFilled)
                {
                // enter date number
                calendar +="<td align=center class='JSCalendarDays'><a href='JavaScript:self.close();opener.closeCalendar"+io+"("+(i-firstDay) + ");' class='calendar'> "+(i-firstDay)+"</a></td>"
				}
		if (i > TotalDaysFilled && i <= lastCalspace) 
				{
				// 'empty' boxes after TotalDaysFilled
				calendar +="<td class='JSCalendarDays'><img src='images/trans.gif' height=12 width=1></td>"
				}
        //must start new row after each week
        if (i % 7 == 0 &&  i != lastCalspace)
                {
                calendar +="</tr>"
				calendar +="<tr>"
                }
        }
calendar +="</tr>"

//prev month - next month controls table
calendar +="<tr><td colspan=7 align=center class='JSCalendarTitle'>"

//next month and previous month buttons
var goPrevMonth = prevMonth(mthIdx)
var goNextMonth = nextMonth(mthIdx)
var nextYear = changeYear("next",parseInt(month),parseInt(year))
var prevYear = changeYear("prev",parseInt(month),parseInt(year))

if(navigator.userAgent.indexOf('MSIE',0) != -1)
        {
		calendar +="<table cellpadding=0 cellspacing=0 border=0 width=100%>"
        calendar +="<tr><td align=left class='JSCalendarTitle'><a href='javascript:opener.generateCalendar(self,"+goPrevMonth+","+prevYear+",\""+io+"\")' class='calcontrol'>« Prev</a></td>"
        calendar +="<td align=right class='JSCalendarTitle'><a href='javascript:opener.generateCalendar(self,"+goNextMonth+","+nextYear+",\""+io+"\")' class='calcontrol'>Next »</a></td></tr>"
		calendar +="</table>"
		calendar +="</td></tr>"
		calendar +="</table></body></html>"
        target.document.close()
        }
else
        {
		calendar +="<table cellpadding=0 cellspacing=0 border=0 width=100%>"
        calendar +="<fo"+"rm><tr><td align=left class='JSCalendarTitle'><input type='button' value=' < ' onClick='document.clear();opener.generateCalendar(opener.calendarWindow,\'"+goPrevMonth+"\',\'"+prevYear+"\',\'"+io+"\')'></td>"
        calendar +="<td align=right class='JSCalendarTitle'><input type='button' value=' > '"+"onClick='document.clear();opener.generateCalendar(opener.calendarWindow,"+goNextMonth+","+nextYear+",\""+io+"\")'></td></tr></form>"
		calendar +="</table>"
		calendar +="</td></tr>"
        calendar +="</table></body></html>"
        }
target.document.write(calendar);
target.document.close() 
}

function closeCalendar(day) {
        var yrIdx = parseYear(selectedyear,document.theForm.selYear );

        // Decrement index for day and month, because code assumes 
        // that we have an extra defaultvalue at the start.
        document.theForm.selMonth.options.selectedIndex=selectedmonth-1;
        document.theForm.selYear.options.selectedIndex= yrIdx;
        document.theForm.selDay.options.selectedIndex=parseInt(day)-1;
}