
// COPPA STUFF
// Global Variables
var underage = false;
var expdate = new Date();
expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * 3)); 

function  IsUnderage() {
  date(); 
  var birth_year=jQuery("#birthYear").val();
  var birth_month=jQuery("#birthMonth").val();
  var birth_day=jQuery("#birthDay").val();
	// Checks Year for less than 13
	if(((thisyear - 13) < birth_year) && birth_year != "0"){
	  //alert ("under 13");
	  underage = true;
	  SetCookie("AgeVerification",underage,expdate);
	  return true;
	} else if(eval(birth_year) == eval(thisyear - 13)){
	// Checks for the month on the 13the year 
			if((birth_month != "0") && (eval(thismonth) < eval(birth_month))) {
				underage = true;	
				SetCookie("AgeVerification",underage,expdate);
				//alert ("a few months short of 13");
				 return true;
			// checks for the day on the month of the 13th year    
			} else if (eval(birth_month) == eval(thismonth)) {
				if((birth_day != "0") && (eval(thisday) < eval(birth_day))) {       
					underage = true;
					SetCookie("AgeVerification",underage,expdate);
					//alert ("a few days short of 13");
					 return true;
				}
			//if date checks out check for cookie
			} else  {
				if(checkForCookie()) {
					return true;
				} 
				else {
					return false;
				}  
			}  			
	} else {

				if(checkForCookie()) {
					return true;
				} 
				else {
					return false;
				} 	
	}				
}


// If the data is fine - this checks the cookie to make sure that the person hasnt already been in coppa violation
function checkForCookie(){
	if (GetCookie("AgeVerification")== null ){
	//alert ("no cookie");
		return false;
		
	} else {
		if (GetCookie("AgeVerification")!=false) {
			//alert ("cookie set to underage");
			return true;
		} 
	}
}		
		
		
// Functions for Cookies
function date()
{
	var browser = navigator.appName;
	temp_date = new Date();
	thisday = temp_date.getDate();
	thismonth = (temp_date.getMonth()+1);
	thisyear = temp_date.getYear();

	if(browser == "Microsoft Internet Explorer")
	{
		thisyear = thisyear;
	}
	else if(browser == "Netscape")
	{
		thisyear = thisyear + 1900;
	}
}

function SetCookie(name,value,expires) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "");
}
function GetCookie(name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

// An adaptation of Dorcht's function for deleting a cookie.
function delCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
