var monthNames = [ "January", "February", "March", "April", "May", "June",
		"July", "August", "September", "October", "November", "December" ];
var monthDayCounts = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
var minimumAge = 18;

$(function() {
	showFeedback("");
	$("#submitButton").click(function() {
		var isValid = validate();
		if (!isValid) {
			return;
		}
		var remember = $("#rememberMe").attr("checked") != "";
		showFeedback("Please wait...");
		var dataString = "verified=true&remember=" + (remember ? "true" : "false");
		$.ajax( {
			type : "POST",
			url : "ajaxverifyage.php",
			data : dataString,
			success : onAjaxSuccess,
			error : onAjaxError
		});
		return false;
	});
});

function onAjaxSuccess(data, textStatus, xhRequest) {
	// alert("SUCCESS: data=" + data + "\ntextStatus=" + textStatus +
	// "\nxhRequest=" + xhRequest);
	if (data != "success") {
		showFeedback(data, null, 0);
	} else {
//		alert("redirecting to " + $("#from").val());
		showFeedback("Welcome! Redirecting...", null, 1);
		var redirectTo = $("#from").val();
		if(redirectTo != "") {
			window.location = redirectTo;
		}else{
			window.location = ".";
		}
	}
}

function onAjaxError(xhRequest, textStatus, errorThrown) {
	// alert("ERROR: textStatus=" + textStatus + "\nerrorThrown=" + errorThrown
	// + "\nxhRequest=" + xhRequest);
	showFeedback("AJAX error: " + textStatus, null, 0);
}

function validate() {
	var dd = $("#dd");
	var mm = $("#mm");
	var yyyy = $("#yyyy");
	var ddVal = $.trim(dd.val());
	var mmVal = $.trim(mm.val());
	var yyyyVal = $.trim(yyyy.val());

	// dd not a number?
	if (ddVal == "" || isNaN(ddVal)) {
		showFeedback("Please enter a number for your birth day", dd, 0);
		return false;
	}
	var ddNVal = Number(ddVal);
	// dd in the correct range?
	if (ddNVal < 1 || ddNVal > 31) {
		showFeedback(
				"Please enter a number for your birth day in the range 1 to 31",
				dd, 0);
		return false;
	}

	// mm not a number?
	if (isNaN(mmVal)) {
		showFeedback("Please enter a number for your birth month", mm, 0);
		return false;
	}
	// mm in the correct range?
	var mmNVal = Number(mmVal);
	if (mmNVal < 1 || mmNVal > 12) {
		showFeedback(
				"Please enter a number for your birth month in the range 1 to 12",
				mm, 0);
		return false;
	}

	// yyyy not a number?
	if (isNaN(yyyyVal)) {
		showFeedback("Please enter a number for your birth year", yyyy, 0);
		return false;
	}
	var yyyyNVal = Number(yyyyVal);
	if (yyyyNVal < 1900 || yyyyNVal > 2010) {
		showFeedback(
				"Please enter a number for your birth year in the range 1900 to 2010",
				yyyy, 0);
		return false;
	}
	var leap = isLeapYear(yyyyNVal);
	var daysInMonth = monthDayCounts[mmNVal - 1]
			+ (leap && mmNVal == 2 ? 1 : 0);
	if (daysInMonth < ddNVal) {
		showFeedback("There are only " + daysInMonth + " days in "
				+ monthNames[mmNVal - 1] + " of " + yyyyVal, mm, 0);
		return false;
	}
	if(!isMinimumAge(ddNVal, mmNVal, yyyyNVal, minimumAge)){
		showFeedback("You are not old enough to enter the site", 0);
		return false;
	}
	return true;
}

function showFeedback(s, focusElement, status) { // status -1 (default) = normal, 0 = error, 1 = success
	var fb = s ? s : "&nbsp;";
	switch(status) {
		case 0:
			fb = "<span class=\"feedbackNegative\">" + fb + "</span>";
			break;
		case 1:
			fb = "<span class=\"feedbackPositive\">" + fb + "</span>";
			break;
	}
	$("#feedback").html(fb);
	if(focusElement){
		focusElement.focus();
	}
}

function isLeapYear(year) {
	if (year % 400 == 0) {
		return true;
	}
	if (year % 100 == 0) {
		return false;
	}
	if (year % 4 == 0) {
		return true;
	}
	return false;
}

function isMinimumAge(day, month, year, age) {
	return getYearDiff(day, month, year) >= age;
}

// inputs must be numbers
function getYearDiff(day, month, year) {
	var date = new Date();
	var dayCurrent = Number(date.getDate());
	var monthCurrent = Number(date.getMonth()) + 1;
	var yearCurrent = Number(date.getFullYear());
	var subtractYear = (month > monthCurrent) || (month == monthCurrent && day > dayCurrent);
	yearDiff = yearCurrent - year - (subtractYear ? 1 : 0);
	return yearDiff;
}
