function isValidDate(dateStr) {

// requires 4 digit year
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

// is the format ok?
var matchArray = dateStr.match(datePat); 
if (matchArray == null) {
alert(dateStr + " Date is not in a valid format.")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
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 (year < 1900) {
alert("Year "+year+" is too low!")
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;
}

function isValidTime(timeStr) {

var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

var matchArray = timeStr.match(timePat);
if (matchArray == null) {
alert("Time is not in a valid format.");
return false;
}
hour = matchArray[1];
minute = matchArray[2];
second = matchArray[4];
ampm = matchArray[6];

if (second=="") { second = null; }
if (ampm=="") { ampm = null }
return true;
}

function dateDiff(field) {
date1 = new Date();
date2 = new Date();
diff  = new Date();


dte_to_check = field.value;

if (isValidDate(dte_to_check)) { // Validates first date 
// get birthday
date1temp = new Date(dte_to_check);
date1.setTime(date1temp.getTime());
}
else return false; // otherwise exits

// get today's date
date2temp = new Date();
date2.setTime(date2temp.getTime());

// sets the system time
//alert (date2.getTime());

// sets difference date to difference of first date and today's date

diff.setTime(Math.abs(date1.getTime() - date2.getTime()));

timediff = diff.getTime();

//calculate the number of days you have from the date of birth 

days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
timediff -= days * (1000 * 60 * 60 * 24);

//alert (days)
if (days < 6570)
{
alert ("You are currently not eligible to register for Geoffrey's Birthday Club.  Enter parent/guardian date of birth.");
// if the age is less than required it redirects to other pages
// location.href = "/bdayrus/gbc/brus_ad_gbc_faqs.cfm";
    
    //document.validate.action = "/bdayrus/gbc/brus_ad_gbc_faqs.cfm";
 
return false; // form should never submit, returns false
}
else
{
// if the age is mores than required it redirects to form
// location.href = "/bdayrus/gbc/brus_ad_gbc_enroll.cfm";

    //document.validate.action = "/bdayrus/gbc/dsp_enrollment_form.cfm";
}

//dateform.difference.value = days + " days ";

return true; // form should never submit, returns false
}
