//
//	24-01-2004 CLM Save this js as UTF-8 for German characters
//	20-01-2004 CLM Add German month names
//
/* DateInputField:

	generates 3 HTML select boxes (Day, Month, Year) for a convenient way to input dates.
	The result date is set in a hidden HTML input field as YYYY-MM-DD
			
		
	Programmer: S. de Koning
	Date:		12-08-2001
*/




// INIT MAIN VARIABLES AT STARTUP

var dateField = new Array();


// List of months and it's number of days ( Name, Number of days, Value)
var months = new Array(
					new Array( ' ',        ' ',31,  ' ' ),
					new Array( 'Januari',   31, 1,  'Januar' ), 
					new Array( 'Februari',   0, 2,  'Februar' ),	// 0, Feb is a special case
					new Array( 'Maart',     31, 3,  "März" ), // &#228;
					new Array( 'April',     30, 4,  'April' ),
					new Array( 'Mei',       31, 5,  'Mai' ),
					new Array( 'Juni',      30, 6,  'Juni' ),
					new Array( 'Juli',      31, 7,  'Juli' ),
					new Array( 'Augustus',  31, 8,  'August' ),
					new Array( 'September', 30, 9,  'September' ),
					new Array( 'Oktober',   31, 10, 'Oktober' ),
					new Array( 'November',  30, 11, 'November' ),
					new Array( 'December',  31, 12, 'Dezember' )
					);



// basic function.... trims a string 
function Trim(os)
{
	var s = new String(os), start = 0, end = s.length-1;
	
	while ((s.charAt(start) == ' ') && (start < s.length)) start++;
	
	if (start != s.length-1)
	{
		while (s.charAt(end) == ' ') end--;
	} else return '';

	return s.substring(start, end+1);
}


// another basic function, adds zeroes at the start of the string to reach length l
function addZeroes(os, l)
{
	var s  = new String(os);
		ns = new String;
	for (i=0; i < l-s.length; i++) ns += '0';
	
	return ns+s;
}



// determines how many days there should be shown in the Day Select Box; it depends on the selected month and year
function DetermineNDays(nPos)
{
	with (dateField[nPos])
	{
		// retrieves the selected monthand the selected year
		selM = document.forms[form].elements[name+'_Month'].selectedIndex;
		with (document.forms[form].elements[name+'_Year'])
		{
			selY = selectedIndex;
			if (selY > 0)
				valY = options[selY].value;
		}
		
		// return 31 when no year or month is selected
		if ((selM <= 0) || (selY <= 0))
			return 31;
		else
		{
			if (selM != 2)
				return months[selM][1];
			else
			{
				// February has 29 days in any year evenly divisible by four,
				// EXCEPT for centurial years which are not also divisible by 400.
	      		return (  ((valY % 4 == 0) && ( (!(valY % 100 == 0)) || (valY % 400 == 0) ) ) ? 29 : 28 );
			}				
		}
	}
}




// Populates the Day selectbox;
function CreateSelectDay(nPos)
{
	
	with (dateField[nPos])
	{
		if (hideday) return 0;
		
		nDays = DetermineNDays(nPos);
		with (document.forms[form].elements[name+'_Day'])
		{
			currDays = options.length;
			if (nDays > currDays)
			{
				if (currDays == 0) 
				{
					options[0] = new Option(' ', 0);
					currDays++;
				}
				for (i=currDays; i <= nDays; i++)
				{
					options[i] = new Option(i, i);
				}
			} else
			{
				for (i=currDays; i > nDays; i--)
				{
					options[i] = null;
				}
			}
			
			if (selectedIndex == -1) selectedIndex = 0;
		}
	}
}




// Populates the Month selectbox;
function CreateSelectMonth(nPos,cLang)
{

	if ( cLang == 'dut' )
		arnr = 0;
	else if ( cLang == 'ger' )
		arnr = 3;

	with (dateField[nPos])
	{
		if (monthasnumber)
			arnr = 2;
		with (document.forms[form].elements[name+'_Month'])
		{
			for (i=0; i < months.length; i++)
			{
/*				if (monthasnumber)
					arnr = 2;
				else
					arnr = 0;*/
				options[i] = new Option(months[i][arnr], months[i][2]);
			}
			
			if (selectedIndex == -1) selectedIndex = 0;
		}
	}
}



// Populates the Year selectbox;
function CreateSelectYear(nPos)
{
	with (dateField[nPos])
	{
		with (document.forms[form].elements[name+'_Year'])
		{
			options[0] = new Option( '', '' );
			for (i=Trim(startyear); i<=Trim(endyear); i++)
			{
				options[i-startyear+1] = new Option(i, i);
			}	
			
			if (selectedIndex == -1) selectedIndex = 0;
		}
	}
}





// gathers the day, month and year from the selectboxes and puts it in a readable format in the hidden field:
function UpdateHiddenField(nPos)
{
	with (document.forms[dateField[nPos].form])
	{
		if (!dateField[nPos].hideday)
			 day = addZeroes(elements[dateField[nPos].name+'_Day'].selectedIndex, 2);
		else day = '01';
			
		month = addZeroes(elements[dateField[nPos].name+'_Month'].selectedIndex, 2);
		
		with (elements[dateField[nPos].name+'_Year'])
			year = addZeroes(options[selectedIndex].value, 4);
	}
	
	with (dateField[nPos])
	{
		if ((day != '00') && (month != '00') && (year != '0000'))
		{
			if (outputmask.toUpperCase() == 'YYYY-MM-DD')
			{
				document.forms[form].elements[name].value = year+'-'+month+'-'+day;
			} else 
				if (outputmask.toUpperCase() == 'DD-MM-YYYY')
				{
					document.forms[form].elements[name].value = day+'-'+month+'-'+year;
				}
		} else
			document.forms[form].elements[name].value = '';
	}
}





// is called after a change of selection in one of the three boxes
function UpdateInputField(nPos)
{
	// the amount of days differs per month and year, so:
	CreateSelectDay(nPos);
	
	// also the value of the hidden field may be changed
	UpdateHiddenField(nPos);
	
	// perform onchange scripts 
	if (dateField[nPos].onchange != '') eval(dateField[nPos].onchange);
}




function setDateInputField(nPos, os)
{
	var s = new String(os);
	if (s.charAt(7) == '-')
	{
		year 	= s.substring(0,4);
		month 	= s.substring(5,7);
		day 	= s.substring(8,10);
	} else
		if (s.charAt(3) == '-')
		{
			year 	= s.substring(6,10);
			month 	= s.substring(3,5);
			day 	= s.substring(0,2);
		}
	
	with (document.forms[dateField[nPos].form])
	{
		if (!dateField[nPos].hideday)
			elements[dateField[nPos].name+'_Day'].selectedIndex = day;
		elements[dateField[nPos].name+'_Month'].selectedIndex = month;
		with (elements[dateField[nPos].name+'_Year'])
			for (i=0; i<options.length; i++)
				if (options[i].value == year) selectedIndex = i;
	}
}




function CreateNewDateField()
{
	nPos = dateField.length;
	dateField[nPos] = new Object();
	
	dateField[nPos].startyear 		= 1910;
	dateField[nPos].endyear			= 2010;
	dateField[nPos].monthasnumber  	= 0;
	dateField[nPos].hideday			= 0;
	dateField[nPos].form			= 'form';
	dateField[nPos].outputmask		= 'YYYY-MM-DD';
	dateField[nPos].startdate		= '';
	dateField[nPos].onchange		= '';
	
	return nPos;
}



function CreateDateInputField(iName, iAttr, cClassName, cLang )
{
	nPos = CreateNewDateField();
//	dateField[nPos] = new Object();
	dateField[nPos].name = iName;

	if ( cLang == null )
		cLang = 'dut';

//	alert( cLang );
	
	if (cClassName == null) cClassName = '';
	
	Attr = iAttr.split(',');
	for (i=0; i < Attr.length; i++)
	{
		if (Trim(Attr[i]) != '')
		{
			ParN = Trim(Attr[i].split('=')[0]).toLowerCase();
			ParV = Attr[i].split('=')[1];
			eval('dateField[nPos].'+ParN + '=\'' + ParV + '\'');
		}
	}
	
	onCh = 'onChange="UpdateInputField('+nPos+')"';

	with (dateField[nPos])
	{	
		if (!hideday) document.write('<SELECT NAME="'+name+'_Day" '+onCh+' class='+cClassName+'></SELECT>');
		document.write('<SELECT NAME="'+name+'_Month" '+onCh+' class='+cClassName+'></SELECT>');
		document.write('<SELECT NAME="'+name+'_Year" '+onCh+' class='+cClassName+'></SELECT>');
		
		document.write('<INPUT TYPE="HIDDEN" NAME="'+name+ '" VALUE="">');
	}
		
	CreateSelectDay(nPos);
	CreateSelectMonth(nPos,cLang);
	CreateSelectYear(nPos);
	
	if (Trim(dateField[nPos].startdate) != '') setDateInputField(nPos, dateField[nPos].startdate);
	UpdateHiddenField(nPos);
	
}
