$(document).ready(function(){
  
  if(swfobject.getFlashPlayerVersion().major > 6){
    
    swfobject.embedSWF("/flash/age-verify.swf", "swf-replace", "100%", "100%", "7.0.0", null, {}, {wmode:"transparent"}, {id:"age-verify-swf", name:"age-verify-swf"});
    
  } else {
    
    $("#day, #month, #year").bind('focus', onSetFocus).bind("blur", onLoseFocus);
    
    $("#submit").click(ageCheck);
    
  }
  
  
});


function onSetFocus(){
  var t = this.value;
	if (t == "dd" || t == "mm" || t == "yyyy"){
	  $(this).data("oldvalue", this.value);
		this.value = "";
	}
}

function onLoseFocus(){
	if (this.value == ""){
		this.value = $(this).data("oldvalue");
	}
}

function ageCheck(){
  
  if(overAge()){
    location.href = "/home";
  } else {
    ageCheckFailed();
  }
  
}

function overAge(){
  
  var legal_age = 18;
  var now = new Date();
  var min_year = now.getFullYear() - legal_age;
	var min_month = now.getMonth()+1;
	var min_day = now.getDate();
	
  var day = parseInt($("#day").attr("value"));
  var month = parseInt($("#month").attr("value"));
  var year = parseInt($("#year").attr("value"));
  
  if(!day || !month || !year) return false;
  
  if(day < 1 || day > 31) return false;
  if(month < 1 || month > 12) return false;
  if(year < 1880 || year > now.getFullYear()) return false;
  
  if (year < min_year){
		return true;
	} else if (year == min_year){
		if (month < min_month){
			return true;
		} else if (month == min_month) {
			if (day <= min_day){
				return true;
			}
		}
	}
	
	return false;
  
}

function ageCheckFailed(){
  $("#no-id").css({display:"block"});
  $("#please-enter").css({display:"none"});
}