// function to create an array 
function makeArray() {
   this[0] = makeArray.arguments.length;
   for (i = 0; i<makeArray.arguments.length; i++)
       this[i+1] = makeArray.arguments[i];
}
	
var submitcount=0;
var daysofmonth   = new makeArray( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var daysofmonthLY = new makeArray( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var months = new makeArray('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
var monthsShort = new makeArray('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

// function to check if a selected year is a leap year
function LeapYear(year) {
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}

// function to convert a year like "53" to "1953"
function y2k(number)    { return (number < 1000) ? number*1 + 1900 : number; }
	
function isDate (day,month,year) {
// checks if date passed is valid
// will accept dates in following format:
// isDate(dd,mm,ccyy), or
// isDate(dd,mm) - which defaults to the current year, or
// isDate(dd) - which defaults to the current month and year.
// Note, if passed the month must be between 1 and 12, and the
// year in ccyy format.

    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    if (!day) return false
    var test = new Date(year,month,day);
    if ( (y2k(test.getYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()) )
        return true;
    else
        return false;
	}
		
		
// checks that the date is not more than today
function checkDate(selectedday, selectedmonth, selectedyear, inclusive, message) {
	// get the date right now
	var today = new Date();
		
	// create a date with the expiry date details from the form.  
	// the card will expire on the last day of the month at 23:59;59
	var selecteddate = new Date(selectedyear, eval(selectedmonth*1 - 1), selectedday, 23, 59, 59);
		
	// check the date
	if (inclusive == 0)
	{
		if ( !(selecteddate.getTime() > today.getTime()))
	    {
			alert(message);
			return false;
		}
	}
	else
	{
		if ( !(selecteddate.getTime() >= today.getTime()))
	    {
	   	    alert(message);
			return false;
		}
	}
		
	return true;
}

function calcAge(ctr, fName) {
	var dFld = document.getElementById('fldDOB' + ctr + '_D');
	var mFld = document.getElementById('fldDOB' + ctr + '_M');
	var yFld = document.getElementById('fldDOB' + ctr + '_Y');
	var hFld = document.getElementById(fName);
	
	var dVal = dFld.selectedIndex * 1;
	var mVal = mFld[mFld.selectedIndex].value * 1;
	var yVal = yFld[yFld.selectedIndex].value * 1;
	
	var now = new Date();
	var	tday = now.getDate();
	var	tmo = now.getMonth() + 1;
	var	tyr = now.getFullYear();
	var strDOB = "";
	var age = 0;

	if(dVal > 0 && mVal > 0 && yVal > 0) {
		if(isDate(dVal,mVal,yVal)) {
		
			if((tmo > mVal)||(tmo==mVal & tday>=dVal))
				age = tyr - yVal;
			else
				age = tyr - yVal - 1;	
			
			strDOB = dVal + ' ' + monthsShort[mVal] + ' ' + yVal;
			
			if(confirm('Please confirm that you are ' + age + ' years old and were born on ' + dVal + ' ' + months[mVal] + ' ' + yVal + '. Press cancel to go back and change your date of birth')) {
				hFld.value = strDOB;
			}
			else {
				yFld.selectedIndex = 0;
				hFld.value = "";
			}							
			
		}
		else {
			alert('Sorry, that is not a valid date');
			dFld.selectedIndex = 0;
			hFld.value = "";
		}						
	}
	else {
		hFld.value = "";
	}
	
	
}

