var minYear=1900;
var today = new Date();
var maxYear=today.getYear()+1900;

function daysInFeb (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

/* Creates an array of the number of days per month */
function daysArray() {
	for (var i = 1; i <= 12; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
		if (i==2) {this[i] = 29;}
   }
   return this;
}
// does not test for is empty, because you might allow empty dates - empty date will pass the test
function isDate(d, m, y){
    return isDate(d,m,y,false);
}

// tests whether or not a date is an empty date
function isEmptyDate(d,m,y) {
    ld = leadZeroTrim(d);
    lm = leadZeroTrim(m);
    ly = leadZeroTrim(y);
    var daysInMonth = daysArray();

    var empties = 0;

    if (isEmpty(ld)) {
        empties++;
    }

    if (isEmpty(lm)) {
        empties++;
    }

    if (isEmpty(ly)) {
        empties++;
    }

    if (empties > 0 && empties < 3) {
        return false;
    } else if (empties == 3) {
        return true;
    }
}

// isDate which ignores whether or not the date is in the future
// Allows empty dates
function isDate(d,m,y,ignoreFuture) {
    ld = leadZeroTrim(d);
    lm = leadZeroTrim(m);
    ly = leadZeroTrim(y);
    var daysInMonth = daysArray();

    if (isEmptyDate(ld,lm,ly)) {
        return true;
    }

    // check day number is valid for month
    if (isNaN(ld) || (ld < 1 || ((lm == 2 && ld > daysInFeb(ly)) || ld > daysInMonth[lm]))) {
        return false;
    }

    // check year number is valid
    if (isNaN(lm) || (lm <1 || lm > 12)) {
        return false;
    }

    if (ignoreFuture) {
        maxYear = 9999;
    }
    //Finally test the date is not in the future
    if (!isEmpty(ld) && !isEmpty(lm) && !isEmpty(ly)) {
        var fDate = new Date(ly,lm-1,ld); // need to subtract because js months start at 0
        if (!ignoreFuture && fDate > today) {
            return false;
        }
    }
    return true;
}

function isEmpty(testVar) {
	if (testVar == null || testVar.length == 0) {
		return true;
	} else {
		return false;
	}
}

function leadZeroTrim(testVar) {
	if (testVar != null) {
		if (testVar.toString().indexOf('0')==0 && testVar.length==2) {
			return parseInt(testVar.toString().substr(1,1));
		} else {
			return testVar;
		}
	} return testVar;
}

function isRadioChecked(testRad) {
    for (i = 0; i < testRad.length; i++) {
        if (testRad[i].checked) {
            return true;
        }
    }
    return false;
}

function formatDate(day, month, year) {
    if (!isEmpty(day) && !isEmpty(month) && !isEmpty(year)) {
        return day + '/' + month + '/' + year;
    } else {
        return '';
    }
}

function isNum(testVar) {
    if (!isEmpty(testVar)) {
        return !isNaN(testVar);
    }
    return true;
}

