var today = new Date(); // for Set date functions
var tomorrow = new Date(today.getTime() + (1000*60*60*24)); 

// ************************************************************************
function SetYearOpt(rangeOpt, yrfld, monfld, dayfld) 
{
yrfld.options.length = 0; //remove all current options

//alert("Adding year " + tomorrow.getFullYear());

if (rangeOpt == 1) // focus to future
{
	AddOptElem(yrfld,tomorrow.getFullYear(),null);
	AddOptElem(yrfld,tomorrow.getFullYear()+1,null);
}
else // focus to past
{
	AddOptElem(yrfld,today.getFullYear(),null);
	AddOptElem(yrfld,today.getFullYear()-1,null);
}

//alert("year val before [" +yrfld.value + "] opt is " + yrfld.selectedIndex + " opt0 is " + yrfld.options[0].selected);

//yrfld.options[1].selected = true; // select first option

//alert("year val after [" +yrfld.value + "] opt is " + yrfld.selectedIndex);

SetMonOpt(rangeOpt,yrfld,monfld,dayfld);
} // SetYearOpt

// ************************************************************************
function SetMonOpt(rangeOpt, yrfld, monfld, dayfld)
{
var basemon;
var maxmon;

if (rangeOpt == 1) // focus to future
{
	//alert('yr ' + yrfld.value +  ' tom ' + tomorrow.getFullYear());
	if (yrfld.value == tomorrow.getFullYear())
		basemon = tomorrow.getMonth()+1;
	else
		basemon = 1;
	maxmon = 12;
}
else // focus to past
{
	basemon = 1;
	if (yrfld.value == today.getFullYear())
		maxmon = today.getMonth()+1;
	else
		maxmon = 12;
}

monfld.options.length = 0;
AddOptElem(monfld,"",""); // first option is blank

//alert('adding mons from ' + basemon);

for (var mon=basemon; mon <= maxmon ; mon++ ) {
	AddOptElem(monfld,getMonLetters(mon),mon);
	}
dayfld.options.length = 0; //clear day field
} // SetMonOpt

// ************************************************************************
function SetDayOpt(rangeOpt,yrfld, monfld, dayfld)
{
var baseday;
var daymax;
var monval = parseInt(monfld.value); // make sure month interpreted as number

if (rangeOpt == 1)
{
	if (yrfld.value == tomorrow.getFullYear() && monval == tomorrow.getMonth()+1)
		baseday = tomorrow.getDate();
	else
		baseday = 1;
	daymax = 0;
}
else
{
	baseday = 1;
	if (yrfld.value == today.getFullYear() && monval == today.getMonth()+1)
		daymax = today.getDate();
	else
		daymax = 0;
}

if (daymax == 0)
{
	if (monval == 2) {
		if (yrfld.value % 4 == 0) 	{
			if (Math.floor(yrfld.value % 400 / 100) > 0)
				daymax = 28;
			else
				daymax = 29;
			}
		else {
			daymax = 28;
			}
		}
	else {
		if ((((3 * ((monval + 10) % 12)) + 2) % 5) < 3)
			daymax = 31;
		else
			daymax = 30;
		}
}

//alert('mon val ' + monfld.value + ' day max ' + daymax + ' sum ' + (monfld.value + 10) +  ' eq ' + (((3 * monnum) + 2) % 5));
dayfld.options.length = 0;
AddOptElem(dayfld,"",""); // first option is blank
for (var day=baseday; day <= daymax ; day++ ) {
	AddOptElem(dayfld,day,null);
	}
} // SetDayOpt

// ************************************************************************
function PopUpCalendar()
// pseudo-class, instantiated to popCalendar global variable, only in forms
// that actually use it
{
// methods
this.popup = OpenCalendar;
this.return_date = null; // assigned when OpenCalendar is called

// calendar options
this.year_scroll = true;
} // END PopUpCalendar

// ************************************************************************
function OpenCalendar(getInput, setOutput)
{ // request to open the popup calendar
	popCalendar.return_date = setOutput;

	var dt_targval;
	if (getInput)
		dt_targval = getInput();
	if (!dt_targval)
		dt_targval = new Date();

	// in spite of status=no and location=no, depending on security settings, browsers will still display status and address bar,
	// although address bar will probably be readonly
	var obj_calwindow = window.open(
		'calendar.html?targ=' + dt_targval.valueOf(),
		'Calendar', 'width=200,height=190,status=no,location=no,resizable=no,top=200,left=200,dependent=yes,alwaysRaised=yes');
	obj_calwindow.opener = window;
	obj_calwindow.focus();
} // END OpenCalendar


