﻿
function validate_form() {
	var name = document.getElementById("frmName").value;

	var day = document.getElementById("frmDay").value;
	var month = document.getElementById("frmMonth").value;
	var year = document.getElementById("frmYear").value;
	var age = document.getElementById("frmAge");
	var dob = document.getElementById("frmDOB");

	var email = document.getElementById("frmEmail").value;
	var address = document.getElementById("frmAddress").value;
	var townCity = document.getElementById("frmTownCity").value;
	var postcode = document.getElementById("frmPostCode").value;
	var country = document.getElementById("frmCountry").value;
	var phone = document.getElementById("frmPhone").value;
	var errors = "";
	var termsAccepted = document.getElementById("terms").checked;
	var consentObtained = document.getElementById("consent").checked;

	var redirect = document.getElementById("redirectTo");
	if (redirect.value.indexOf("?comp=") == -1) {
		redirect.value = redirect.value + "?comp=" + document.getElementById("competition").value;
	}

	if (name == "") {
		errors += "- Name is required.\n";
	}

	if (!IsValidDate(day, month, year)) {
		errors += "- Please enter a valid date of birth.\n";
	}
	else {
		if (age && dob) {
			age.value = calculateAge(day, month, year);
			/*dob.value = day + "/" + month + "/" + year;*/
            dob.value = month + "/" + day + "/" + year;
		} else {
			alert("ERROR ON PAGE - ONE OR MORE REQUIRED HIDDEN FIELDS (frmAge and/or frmDOB) ARE MISSING\n\nDate of Birth data will be mising from the competition entry.");
			return false;
		}
	}
	if (email == "") {
		errors += "- Email Address is required.\n";
	}
	if (address == "") {
		errors += "- Address is required.\n";
	}
	if (townCity == "") {
		errors += "- Town/\City is required.\n";
	}
	if (postcode == "") {
		errors += "- Postcode is required.\n";
	}
	if (country == "") {
		errors += "- Country is required.\n";
	}
	if (phone == "") {
		errors += "- Telephone is required.\n";
	}
	if (!termsAccepted) {
		errors += "- You must accept the Terms and Conditions.\n";
	}
	if (!consentObtained) {
		errors += "- You must obtained your parent's/guardian's consent.";
	}
	if (errors == "") {
		return true;
	} else {
		errors = "Please correct the following errors:\n\n" + errors;
		alert(errors);
		return false;
	}
}

var one_day = 1000 * 60 * 60 * 24
var one_month = 1000 * 60 * 60 * 24 * 30
var one_year = 1000 * 60 * 60 * 24 * 30 * 12

function calculateAge(day, month, year) {
	var dobYear = year;
	var dobMonth = month;
	var dobday = day;
	var retVal = -1;
	if (IsValidDate(dobday, dobMonth, dobYear)) {

		if (!isNaN(dobYear) && !isNaN(dobMonth) && !isNaN(dobday)) {
			var age = displayage(dobYear, dobMonth, dobday, "years", 0, "rounddown");
			if (Number(age) < 1) {
				age = displayage(dobYear, dobMonth, dobday, "months", 0, "rounddown");
				retVal = age < 0 ? -1 : age + " months";
			} else {
				retVal = age;
			}
		} else {
			retVal = -1;
		}
	}
	return retVal;
}

function IsValidDate(Day, Mn, Yr) {
	var DateVal = Mn + "/" + Day + "/" + Yr;
	var dt = new Date(DateVal);
	if (dt.getDate() != Day) {
		return (false);
	}
	else if (dt.getMonth() != Mn - 1) {
		return (false);
	}
	else if (dt.getFullYear() != Yr) {
		return (false);
	}
	return (true);
}

function displayage(yr, mon, day, unit, decimal, round) {
	today = new Date()
	var pastdate = new Date(yr, mon - 1, day)

	var countunit = unit
	var decimals = decimal
	var rounding = round

	finalunit = (countunit == "days") ? one_day : (countunit == "months") ? one_month : one_year
	decimals = (decimals <= 0) ? 1 : decimals * 10

	if (unit != "years") {
		if (rounding == "rounddown")
			return (Math.floor((today.getTime() - pastdate.getTime()) / (finalunit) * decimals) / decimals)
		else
			return (Math.ceil((today.getTime() - pastdate.getTime()) / (finalunit) * decimals) / decimals)
	} else {
		yearspast = today.getFullYear() - yr - 1
		tail = (today.getMonth() > mon - 1 || today.getMonth() == mon - 1 && today.getDate() >= day) ? 1 : 0
		pastdate.setFullYear(today.getFullYear())
		pastdate2 = new Date(today.getFullYear() - 1, mon - 1, day)
		tail = (tail == 1) ? tail + Math.floor((today.getTime() - pastdate.getTime()) / (finalunit) * decimals) / decimals : Math.floor((today.getTime() - pastdate2.getTime()) / (finalunit) * decimals) / decimals
		return (yearspast + tail)
	}
}	