
var pickedDay = "";
var pickedMonth = "";
var pickedYear = "";


// initialize
$(document).ready(function(){


	
	// generate days
	for (var i = 1; i < 32; i++)
	{	
		$("#ageCheck .row.days").append('<div class="fld day" id="day_' + i + '">' + i + '</div>');
	}

	$("#ageCheck .fld.day").hover(
	
		function () {
			$(this).addClass("hover");
		},
		function () {
			$(this).removeClass("hover");
		}
	);
	
	$("#ageCheck .fld.day").click(function(){
		$("#ageCheck .fld.day").removeClass("active");
		$(this).addClass("active");
		checkAge();
	});
	
	// generate months
	var monthList = new Array();
	if (typeof monthString != "undefined")
	{
		monthList = monthString.split(",");
	}
	
	var monthVal = "";
	for (var i = 1; i < 13; i++)
	{	
		if (monthList[i - 1] == undefined)
		{
			monthVal = i + "";	
		} else {
			monthVal = monthList[i - 1];
		}
		$("#ageCheck .row.months").append('<div class="fld month" id="month_' + (i - 1) + '">' + monthVal + '</div>');
	}

	$("#ageCheck .fld.month").hover(
		function () {
			$(this).addClass("hover");
		},
		function () {
			$(this).removeClass("hover");
		}
	);
	
	$("#ageCheck .fld.month").click(function(){
		$("#ageCheck .fld.month").removeClass("active");
		$(this).addClass("active");
		checkAge();
	});
	
	// generate years
	for (var i = 0; i < 11; i++)
	{	
		$("#ageCheck .row.years").append('<div class="fld year"><span>' + (1900 + (i * 10)) + '</span></div>');
	}
	
	$("#ageCheck .row.hover").mouseover(function(e) {
		$(".year-tooltip").show();
	});
	
	$("#ageCheck .row.hover").mouseout(function(e) {
		$(".year-tooltip").hide();
	});
	
	$("#ageCheck .row.hover").mousemove(function(e) {
		positionTooltip(e, "#year-tooltip");
	});
	
	$("#ageCheck .row.hover").click(function(e) {
		positionTooltip(e, "#year-active");
		$("#year-active").show();
		checkAge();
	});
	
})

function positionTooltip(e, tooltip)
{
	var xpos = e.pageX - $("#ageCheck .row.years").position().left;
	$(tooltip).css("marginLeft", xpos);	
	perc = (xpos / (902- 82)) * 100;
	$(tooltip).html(Math.round(1900 + perc));
}

function checkAge()
{
	var day = -1;
	var month = -1;
	var year = -1;
	
	var dayObject = $("#ageCheck .fld.day.active");
	var monthObject = $("#ageCheck .fld.month.active");
	var yearObject = $("#ageCheck #year-active");
	
	if(dayObject.length == 1)
	{
		day = parseInt(dayObject.attr("id").replace("day_", ""));
	}
	
	if(monthObject.length == 1)
	{
		month = parseInt(monthObject.attr("id").replace("month_", ""));
	}
	
	if (yearObject.css("display") == "block")
	{
		year = parseInt(yearObject.html());
	}

	if (day >= 0 && month >= 0 && year >= 0)	
	{
	
		var userDate = new Date();
		userDate.setFullYear(year,month,day);
		var userTime = userDate.getTime();

		
		var ageValid = false;
		
		if (userTime < 0)
		{
			ageValid = true;
		} else {
			var timeDiff = new Date(currentTime - userTime);
			yearsDiff = timeDiff.getFullYear() - 1970;
			if (yearsDiff >= minAge)
			{
				ageValid = true;
			}
		}
		
		
		if (ageValid)
		{
			$("#ageInvalid").hide();
			$("#ageform").submit();
		} else {
			$("#ageInvalid").show();
		}
	}
	
}





