function DateTimePicker()
{
}

// defenitions

// map each month number to the number of days in it
DateTimePicker.days_in_month = { 
		1  : 31, 
		2  : 28,
		3  : 31,
		4  : 30,
		5  : 31,
		6  : 30,
		7  : 31,
		8  : 31,
		9  : 30,
		10 : 31,
		11 : 30,
		12 : 31 };
		
DateTimePicker.month_names = { 
		1  : 'January', 
		2  : 'February',
		3  : 'March',
		4  : 'April',
		5  : 'May',
		6  : 'June',
		7  : 'July',
		8  : 'August',
		9  : 'September',
		10 : 'October',
		11 : 'November',
		12 : 'December' };
		
DateTimePicker.short_month_names = { 
		1  : 'Jan', 
		2  : 'Feb',
		3  : 'Mar',
		4  : 'Apr',
		5  : 'May',
		6  : 'Jun',
		7  : 'Jul',
		8  : 'Aug',
		9  : 'Sept',
		10 : 'Oct',
		11 : 'Nov',
		12 : 'Dec' };

DateTimePicker.changeDate = function(t)
{
	DateTimePicker.cur_date = t;
	DateTimePicker.update();
}

DateTimePicker.updateHours = function(t)
{
	DateTimePicker.cur_hour = t.value;
}

DateTimePicker.updateMinutes = function(t)
{
	DateTimePicker.cur_minute = t.value;
}

DateTimePicker.submit = function()
{
	getElement(DateTimePicker.control).value = DateTimePicker.short_month_names[DateTimePicker.cur_month + 1] + " "
																								+ DateTimePicker.cur_date + ", "
																								+ DateTimePicker.cur_year + " "
																								+ (DateTimePicker.cur_hour < 10 ? '0' : '') + DateTimePicker.cur_hour + ":" 
																								+ (DateTimePicker.cur_minute < 10 ? '0' : '') + DateTimePicker.cur_minute;
	getElement('dtpicker').style.display = 'none';
}

DateTimePicker.cancel = function()
{
	getElement('dtpicker').style.display = 'none';
}

DateTimePicker.scrollMonth = function(m)
{
	// scrolling  backwards
	if (m < 0)
	{
		if (DateTimePicker.cur_month == 0)
		{
			DateTimePicker.cur_month = 11;
			DateTimePicker.cur_year--;
		}
		else
		{
			DateTimePicker.cur_month--;
		}
	}
	
	// scrolling forwards
	else
	{
		if (DateTimePicker.cur_month == 11)
		{
			DateTimePicker.cur_month = 0;
			DateTimePicker.cur_year++;
		}
		else
		{
			DateTimePicker.cur_month++;
		}
	}
	
	// update push
	DateTimePicker.update();	
}

DateTimePicker.update = function()
{
	// create our current date
	d = new Date();
	d.setDate(DateTimePicker.cur_date);
	d.setYear(DateTimePicker.cur_year);
	d.setMonth(DateTimePicker.cur_month);
	
	// sort out the month
	m = d.getMonth();
	dim = DateTimePicker.days_in_month[m+1];
	
	// which month do we start on
	firstDay = new Date(DateTimePicker.month_names[m+1] + " 01, " + d.getFullYear());
	firstDay = firstDay.getDay();
	code = '';
	
	// the given month
	code+= '<form action="" method="post" onsubmit="DateTimePicker.submit(); return false" >';
	code+= '<table width="100%">';
	code+= '<tr>';
	code+= '<td ><img src="/static/icons/dt_prev.gif" alt="" onclick="DateTimePicker.scrollMonth(-1);" /></td>';
	code+= '<td style="color: #000000; font-weight: bold">' + DateTimePicker.month_names[DateTimePicker.cur_month+1] + " " + DateTimePicker.cur_year + ' </td>';
	code+= '<td ><img src="/static/icons/dt_next.gif" alt="" onclick="DateTimePicker.scrollMonth(1);" /></td>';
	code+= '</tr>';
	code+= '</table>';
		
	// header of table
	code+= '<table width="100%" cellspacing="0" class="tpad3" style="margin-bottom: 5px">';
	
	// date type
	days = { 0 : 'S', 
					 1 : 'M',  
					 2 : 'T',  
					 3 : 'W',  
					 4 : 'T',  
					 5 : 'F',  
					 6 : 'S' }
	
	// first row of day names
	code+= ('<tr>');
	for (i = 0; i < 7; i++)
		code+= '<td class="' + ((i == 0 || i == 6) ? 'dtWeekend' : 'dtWeekday') + 
					 '" style="width: 14%; font-weight: bold; border-bottom: 1px solid #000000;">' + days[i] + '</td>';
	code+= '</tr>';
	
	// second row; seperators
	code+= ('<tr>');
	for (i = 0; i < 7; i++)
		code+= '<td class="dtSeps ' + ((i == 0 || i == 6) ? 'dtWeekend' : 'dtWeekday') + '" style="font-size: 3px; padding: 0px;" >&nbsp;</td>';
	code+= '</tr>';
	
	// write each item
	printed = 1;
	opened = false;
	
	// print up to the first day
	if (firstDay != 0)
	{
		code+= ('<tr>');
		for (i = 0; i < firstDay; i++)
		{
			code+= '<td class="' + ((i == 0 || i == 6) ? 'dtWeekend' : 'dtWeekday') + '" >&nbsp;</td>';
			opened = true;
		}
	}
	
	while (printed <= dim)
	{
		// new row if required
		if (opened == false)
		{
			code+= ('<tr>');
			opened = true;
		}
		
		// style for the background
		var style = ' class="dtWeekday" ';
		if ((printed + firstDay) % 7 == 1 || (printed + firstDay) % 7 == 0)
			style = ' class="dtWeekend" ';
		
		// is this the date
		if (printed == DateTimePicker.cur_date)
			style = ' class="dtSelected" ';
		
		// echo line
		
		code+= '<td' + style + ' onclick="DateTimePicker.changeDate(' + printed + ')" >' + (printed) + '</td>';
		
		// close row if required
		if ((printed + firstDay) % 7 == 0)
		{
			code+= '</tr>';
			opened = false;
		}		
		
		printed++;
	}
	
	// needs closing
	if (opened == true)
	{
		code+= '</tr>';
	}
	
	// footer of table
	code+= '</table>';
	
	// time hour element
	code+= '<select id="_dtPickerHour" style="width: 50%; text-align: right" onchange="DateTimePicker.updateHours(this);">';
	code+= '<option value="0" ' + (i == DateTimePicker.cur_hour ? 'selected="selected"' : '') + '>12 am</option>';
	for (i = 1; i < 12; i++) code+= '<option value="' + i + '" ' + (i == DateTimePicker.cur_hour ? 'selected="selected"' : '') + '>' + i + ' am</option>';
	
	code+= '<option value="12" ' + (i == DateTimePicker.cur_hour ? 'selected="selected"' : '') + '>12 pm</option>';
	for (i = 13; i < 24; i++)	code+= '<option value="' + i + '" ' + (i == DateTimePicker.cur_hour ? 'selected="selected"' : '') + '>' + (i - 12) + ' pm</option>';
	code+= '</select>';
	
	// time minute element
	code+= '<select id="_dtPickerSeconds" style="width: 50%" onchange="DateTimePicker.updateMinutes(this);">';
	for (i = 0; i < 60; i+= 5) code+= '<option value="' + i + '" ' + (i == DateTimePicker.cur_minute ? 'selected="selected"' : '') + '>' + i + ' min</option>';
	code+= '</select>';
	
	// confirm or cancel buttons
	code+= '<div style="padding-top: 5px; text-align: right;">';
	code+= '<input type="button" value="  Cancel  " onclick="DateTimePicker.cancel()" />';
	code+= '<input type="button" value="    OK    " onclick="DateTimePicker.submit()" />';
	code+= '</div>';
	code+= '</form>';
	
	getElement('dtpicker').innerHTML = code;
}

DateTimePicker.use = function(parent, clicked)
{
	// lookup the required components
	cont = getElement(parent);
	box  = getElement('dtpicker');

	// show the box
	box.style.display = '';
	box.style.top 	= getTopFromObject(clicked) + "px";
	box.style.left	= getLeftFromObject(clicked) + "px";
	
	// access current time
	d = null;
	if (cont.value.length != 0)
		d = new Date(cont.value);
	else
		d = new Date();
		
	// storage
	DateTimePicker.control		= parent;
	DateTimePicker.cur_year 	= d.getFullYear();
	DateTimePicker.cur_month	= d.getMonth();
	DateTimePicker.cur_date		= d.getDate();
	DateTimePicker.cur_hour		= d.getHours();
	DateTimePicker.cur_minute	= d.getMinutes() - d.getMinutes() % 5;
	
	// display current time
	DateTimePicker.update();
}