var MIN_AGE_DATE = getMinAgeDateStr (13);
var MIN_AGE_URL = "/kids/";
var gIsCanada = false;

/**
 * Returns a String formatted Date for the minimum age required
 * @param	yearsOld	Number: The minimum age in years a subscriber can be
 * @returns	String: Date formatted as "MM/DD/YYYY"
 */
function getMinAgeDateStr (yearsOld) {
	var minDate = new Date ();
	minDate.setFullYear (minDate.getFullYear () - yearsOld);
	return dateToString (minDate);
}
/**
 * Returns a String formatted Date
 * @param    theDate  Date: The date object to convert
 * @returns           String: Date formatted as "MM/DD/YYYY"
 */
function dateToString (theDate) {
	return (theDate.getMonth () +1) +"/"+ theDate.getDate () +"/"+ theDate.getFullYear ();
}
/**
 * Populates a select box with data
 * @param   id      The ID of the Select to update
 * @param   type    The type of data to insert, 'm', 'd' or 'y'
 * @param   def     The default value to select
 */
function populateSelect (id, type, def) {
	var el = document.getElementById (id);
	if (el) {
		var cnt = el.options.length;
		switch (type) {
			case "m":
				var months = new Array ("Month","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
				// Add months to this select box
				for (i=0; i<months.length; i++) {
					el.options[i]=new Option(months[i], padNum(i));
					el.options[i].selected=(i==def);
				}
				// Delete any remaining options
				for (i=31; i<=cnt; i++) {
					el.options[i]=null;
				}
				break;
			case "d":
				el.options[0]=new Option("Day", '00');
				// Add days to this select box
				for (i=1; i<=31; i++) {
					el.options[i]=new Option(i, padNum(i));
					el.options[i].selected=(i==def);
				}
				// Delete any remaining options
				for (i=31; i<=cnt; i++) {
					el.options[i]=null;
				}
				break;
			case "y":
				var today = new Date();
				var startYear = today.getFullYear();
				var endYear = 1900;
				var index = 1;
				el.options[0]=new Option("Year", '00');
				// Add years to this select box
				for (i=startYear; i>=endYear; i--) {
					el.options[index]=new Option(i, i);
					el.options[index].selected=(i==def);
					index++;
				}
				// Delete any remaining options
				for (i=endYear; i<=cnt; i++) {
					el.options[i]=null;
				}
				break;
		}
	}
}
/**
 * Populates the hidden date field when individual selects are changed
 * @param   id          The ID of the Select to update
 *                      Each of the matching Select boxes should have suffixes
 *                      of M, D and Y respectfully.
 * @param   validator   Reference to the Spry validator associated with the
 *                      hidden date textbox.
 */
function date_onChange (id, validator) {
	var dob = document.getElementById (id);
	var dobm = document.getElementById (id+'M');
	var dobd = document.getElementById (id+'D');
	var doby = document.getElementById (id+'Y');
	if (dob && dobm && dobd && doby) {
		var m = dobm.options[dobm.options.selectedIndex].value;
		var d = dobd.options[dobd.options.selectedIndex].value;
		var y = doby.options[doby.options.selectedIndex].value;
		// Make sure a complete date is entered before validating it.
		if (m=='00' || d=='00' || y=='00') {
			dob.value = '';
			validator.reset ();
			return;
		}
		// We have a complete date
		var birthday = new Date (y, m, d);
		dob.value = m + "/" + d + "/" + y;
		if (!validator.validate ()) {
			var minDateAry = MIN_AGE_DATE.split("/");
			var minDate = new Date (minDateAry[2], minDateAry[0]-1, minDateAry[1]);
			if (minDate < birthday) {
				SetCookie("noAccess", "true", null, "/");
				document.location.href = MIN_AGE_URL;
			}
		}
	}
}
/**
 * Used by the email and password fields to clear out their confirm field
 * counterparts and reset their validation
 * @param   id           The id to the confirm field
 * @param   validatior   The reference to the validator object for the confirm
 */
function clearField (id, validator) {
	var el = document.getElementById (id);
	if (id) {
		//setTimeout (function (){id.value = "";}, 10);
		el.value = "";
		validator.reset ();
	}
}