function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie (name) {
	createCookie (name, "", -1);
}

function checkUserAge (theCookie, ifYes, noRedirect) {
	theCookie = readCookie (theCookie);
	if ((theCookie != null) && (theCookie != "null") && (theCookie != "")) {
		if (theCookie == "no") {
			// USER IS TOO YOUNG
			window.location = "tooyoung.php";
		} else if (theCookie == "yes") {
			// USER IS OLD ENOUGH
			if (ifYes != "") {
				// SEND THEM TO THE SPECIFIED PAGE (FROM AGE SELECT PAGE)
				window.location = ifYes;
			} else {
				// THEY JUST STAY ON THE PAGE AND NOTHING HAPPENS
			}
		}
	} else {
		// NO COOKIE, SEND THEM THERE (IF THEY AREN'T THERE ALREADY)
		if (!noRedirect) {
			window.location = "index.php";
		}
	}
}

function showMonths () {
	document.write ('<select name="month" size="1" id="month">');
	document.write ('<option selected value="NULL">month</option>');
	document.write ('<option value="1">January</option>');
	document.write ('<option value="2">February</option>');
	document.write ('<option value="3">March</option>');
	document.write ('<option value="4">April</option>');
	document.write ('<option value="5">May</option>');
	document.write ('<option value="6">June</option>');
	document.write ('<option value="7">July</option>');
	document.write ('<option value="8">August</option>');
	document.write ('<option value="9">September</option>');
	document.write ('<option value="10">October</option>');
	document.write ('<option value="11">November</option>');
	document.write ('<option value="12">December</option>');
}

function makeList (whichList, listTitle, theStart, theEnd) {
	document.write ('<select name="' + whichList + '" size="1" id="' + whichList + '">');
	document.write ('<option selected value="NULL">' + listTitle + '</option>');
	if (whichList == "year") {
		for (x = theEnd; x >= theStart; x--) {
			document.write ('<option value="' + x + '">' + x + '</option>');
		}
	} else {
		for (x = theStart; x <= theEnd; x++) {
			document.write ('<option value="' + x + '">' + x + '</option>');
		}
	}
	document.write ('</select>');
}

function verifyAge (minAge) {
	var now = new Date ();
	var today = new Date (now.getYear (), now.getMonth (), now.getDate ());

	var yearNow = now.getFullYear ();
	var monthNow = now.getMonth () + 1;
	var dateNow = now.getDate ();

	yearDob = document.getElementById ('year').value;
	monthDob = document.getElementById ('month').value;
	dateDob = document.getElementById ('day').value;

	if ((yearDob !== "NULL") && (monthDob !== "NULL") && (dateDob !== "NULL")) {
		yearAge = yearNow - yearDob;

		if (monthNow >= monthDob) {
			var monthAge = monthNow - monthDob;
		} else {
			yearAge--;
			var monthAge = 12 + monthNow -monthDob;
		}

		if (dateNow >= dateDob){
			var dateAge = dateNow - dateDob;
		} else {
			monthAge--;
			var dateAge = 31 + dateNow - dateDob;
			if (monthAge < 0) {
				monthAge = 11;
				yearAge--;
			}
		}

		if (yearAge >= minAge) {
			createCookie ("microsite", "yes", 7);
			window.location = "microsite.php";
		} else {
			createCookie ("microsite", "no", 7);
			window.location = "tooyoung.php";
		}
	} else {
		window.alert ("Please enter your date of birth");
	}
}

