function initDateTimeSelect(prefix)
{
	initDateSelect(prefix);
	initTimeSelect(prefix);
}

function initTimeSelect(prefix) {
  var hour;
  var min;
  var times = [ ];
  for (thour = 0; (thour < 24); thour++)
  {
    // Starting with 8am makes more sense for typical clients
    var hour = (thour + 8) % 24;
    for (min = 0; (min < 60); min += 30)
    {
      times.push(prettyTime(hour, min));
    }
  }
	
	
	jQuery(document).ready(function() {		
		$('#'+ prefix +'-ui').autocomplete(times, { minChars: 0, selectFirst: false, max: 100 });

		// Double click on focus pops up autocomplete immediately
		$('#'+ prefix +'-ui').focus(function() { $(this).click(); $(this).click() } );	
		$('#'+ prefix +'-ui').blur(function() {
			var val = $(this).val();
			var components = val.match(/(\d\d?)(:\d\d)?\s*(am|pm)?/i);
			if (components)
			{
				var hour = components[1];
				var min = components[2];
				if (min)
				{
					min = min.substr(1);
				}
				if (!min)
				{
					min = '00';
				}
				if (min < 10)
				{
					min = '0' + Math.floor(min);
				}
				var ampm = components[3] ? components[3].toUpperCase() : false;
				if (!ampm)
				{
					if (hour >= 8)
					{
						ampm = 'AM';
					}
					else
					{
						ampm = 'PM';
					}
				}
				var formal = hour + ':' + min + ' ' + ampm;
				$(this).val(formal);
				if ((ampm === 'AM') && (hour == 12))
				{
					hour = 0;
				}
				if (ampm === 'PM')
				{
					// Careful: force numeric
					hour = Math.floor(hour) + 12;
				}
				$('#'+ prefix +'_hour').val(hour);
				$('#'+ prefix +'_minute').val(min);
			}
			else
			{
				if (val.length)
				{
					alert("The time must be in hh:mm format, followed by AM or PM. Hint: click on the typeahead suggestions.");
					$('#'+ prefix +'-ui').focus();
				}
			}
		});
	});
}


function initDateSelect(prefix) {
	var code = 'function wfd_'+ prefix +'_read_linked()	{';
	code +=	'jQuery("#'+ prefix +'_jquery_control").val(jQuery("#'+ prefix +'_year").val() + "-" + jQuery("#'+ prefix +'_month").val() + "-" + jQuery("#'+ prefix +'_day").val());	return {}; }';
	code += 'function wfd_'+ prefix +'_update_linked(date) { jQuery("#'+ prefix +'_year").val(date.substring(0, 4)); jQuery("#'+ prefix +'_month").val(date.substring(5, 7)); jQuery("#'+ prefix +'_day").val(date.substring(8)); }';
	code += 'function wfd_'+ prefix +'_check_linked_days() { var daysInMonth = 32 - new Date(jQuery("#'+ prefix +'_year").val(), jQuery("#'+ prefix +'_month").val() - 1, 32).getDate(); jQuery("#'+ prefix +'_day option").attr("disabled", ""); jQuery("#'+ prefix +'_day option:gt(" + (daysInMonth) +")").attr("disabled", "disabled"); 		if (jQuery("#'+ prefix +'_day").val() > daysInMonth) { jQuery("#'+ prefix +'_day").val(daysInMonth); } }';
	code += ' jQuery(document).ready(function() { jQuery("#'+ prefix +'_jquery_control").datepicker(jQuery.extend({}, {	minDate:    new Date(2005, 1 - 1, 1),	maxDate:    new Date(2015, 12 - 1, 31),	beforeShow: wfd_'+ prefix +'_read_linked,	onSelect:   wfd_'+ prefix +'_update_linked,	showOn:     "button", buttonImage: "/images/icons/calendar_view_month.png", buttonImageOnly: true}, jQuery.datepicker.regional[""], {}, {dateFormat: "yy-mm-dd"})); }); ';
	code += 'jQuery("#'+ prefix +'_day, #'+ prefix +'_month, #'+ prefix +'_year").change(wfd_'+ prefix +'_check_linked_days);';
	eval(code);
}



function prettyTime(hour, min)
{
	var ampm = 'AM';
	phour = hour;
	if (hour >= 12)
	{
		ampm = 'PM';
	}
	if (hour >= 13)
	{
		phour -= 12;
	}
	if (phour == 0)
	{
		phour = 12;
	}
	pmin = min;
	if (min < 10)
	{
		pmin = '0' + Math.floor(min);
	}
	return phour + ':' + pmin + ' ' +  ampm;
}
