function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1){
         IsNumber = false;
      }
   }
   return IsNumber;
 }


$(function()
{
	$('#corId').qtip({ content: { text: $('#corId').attr("rel"), prerender: true }, show: { effect: { type: 'none', length: 0 }, delay:0 }, hide: 'mouseout'});
	$('#grBook').qtip({ content: { text: $('#grBook').attr("rel"), prerender: true }, show: { effect: { type: 'none', length: 0 }, delay:0 }, hide: 'mouseout' });
	startD = new Date();
	// initialise the "Select date" link
	$('#date-pick')
		.datePicker(
			// associate the link with a date picker
			{
				createButton:false,
				endDate:  startD.getDate() + '/' + startD.getMonth()+1 + '/' + (startD.getFullYear()+1)
			}
		).bind(
			// when the link is clicked display the date picker
			'click',
			function()
			{

				$(this).dpDisplay();
				return false;
			}
		).bind(
			// when a date is selected update the SELECTs
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				updateSelects(selectedDate);
			}
		).bind(
			'dpClosed',
			function(e, selected)
			{
				updateSelects(selected[0]);
			}
		);

	var updateSelects = function (selectedDate)
	{
		var selectedDate = new Date(selectedDate);
		if(IsNumeric(selectedDate.getDate()) == true) {
			$('#date option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
			$('#month option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
			$('#year option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
		}
	}
	// listen for when the selects are changed and update the picker
	$('#date, #month, #year')
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#year').val(),
							$('#month').val()-1,
							$('#date').val()
						);
				$('#date-pick').dpSetSelected(d.asString());
			}
		);

	// default the position of the selects to today
	var today = new Date();
	updateSelects(today.getTime());

	// and update the datePicker to reflect it...
	$('#date').trigger('change');
});

