// JavaScript Document
<!-- Begin
function isValidDate(dateStr) {
	// from: http://fluppe.wordpress.com/2006/04/13/javascript-check-on-valid-date/
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) {
		alert('Please enter the vaccination date as dd/mm/yyyy');
		return false;
	}
	
	day = matchArray[1]; // p@rse date into variables
	month = matchArray[3];
	year = matchArray[5];
	
	if (month < 1 || month > 12) { // check month range
		alert('Month must be between 1 and 12');
		return false;
	}
	
	if (day < 1 || day > 31) {
		alert('Day must be between 1 and 31');
		return false;
	}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert('Month ' +month+' doesn\'t have 31 days!');
		return false;
	}
	
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
		alert('February ' + year + ' doesn\'t have ' + day + ' days!');
		return false;
		}
	}
	return true; // date is valid
}
function reformDate(dateString){
	// from: http://www.dynamicdrive.com/forums/archive/index.php/t-9198.html
	var dateAr = dateString.split('/')
	return dateAr[1]+'/'+dateAr[0]+'/'+dateAr[2]
}	

function dateDiff() {
	/* compilation from many sources! */
	if(isValidDate(document.getElementById('vaccinationdate').value)) {
		try {
			//var vaccDate = new Date();
			var todayDate = new Date();
			var dayCount = 0;
			var dayText = ' days ago';
			var one_day=1000*60*60*24; // milliseconds
			var vaccDate = reformDate(document.getElementById('vaccinationdate').value)

			var vaccDateUS = new Date(vaccDate);
			//alert('vacc date = '+vaccDateUS.toLocaleString() +' -- todays date = '+todayDate.toLocaleString());
			dayCount = Math.floor(todayDate / one_day) - Math.floor(vaccDateUS / one_day);
			if(dayCount<0){
				alert('The vaccination date is in the future!');
			} else {
				if(dayCount==1){dayText = ' day ago';}
				document.getElementById('difference').innerText = 'Vaccination given '+dayCount+dayText;
				document.getElementById('difference').innerHTML = 'Vaccination given '+dayCount+dayText; // FF
				alert('*** The vaccination was given '+dayCount+dayText+' ***'+getJC());
			}
		} 
		catch(e) {alert('sorry, there has been a problem! Please phone 01460 66099.');}
	}
	
	return false; // form should never submit, returns false
}  // end function

function getJC() {
	var jcMessage = "";
	jcMessage = jcMessage + '\n____________________________________________________________\n\n';
	jcMessage = jcMessage + 'Note that the Jockey Club influenza vaccination schedule is:\n\n';
	jcMessage = jcMessage + '  a. Starter injection\n\n';
	jcMessage = jcMessage + '  b. Second injection: 21-92 days after starter\n\n';
	jcMessage = jcMessage + '  c. Third vaccination: Within 150-215 days of 2nd injection\n\n';
	jcMessage = jcMessage + '  d. Following boosters: annually (within 365 days of\n';
	jcMessage = jcMessage + '  preceding booster) - should be given more frequently\n';
	jcMessage = jcMessage + '  during an outbreak.\n\n';
	jcMessage = jcMessage + 'Phone us now on 01460 66099 if you need to book a vaccination appointment.';
	
	return jcMessage;
}

function resetForm() {
	document.getElementById('difference').innerText = '&nbsp;';
	document.getElementById('difference').innerHTML = '&nbsp;'; // FF
}
//  End -->