var credentialsCookieValidFor = 120;
var lastVerifiedValidFor = 120;


function getQueryVariable(query, variable){
  var vars = query.split("&");
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
}

function showSite(){

  createCookie("age_verified", "true", credentialsCookieValidFor);

  if (document.location.search) {
    document.location.href = document.location.href.substring(0, document.location.href.indexOf('?')) +
    document.location.hash;
    return false;
  }
  
  document.getElementById('ageCheck').style.visibility = "hidden";
  document.body.style.backgroundColor = "#ffffff";
  loadFlashShell();
}


function submitAgeCheck(formName, monthInput, dayInput, yearInput){
	var selectedCountry = document.getElementById(formName).country.options[document.getElementById(formName).country.selectedIndex].value;
	
	if (validDOB(document.getElementById(formName)[monthInput].value, document.getElementById(formName)[dayInput].value, document.getElementById(formName)[yearInput].value)) {
		if (isOfAgeInCountry(getAge(document.getElementById(formName)[monthInput].value, document.getElementById(formName)[dayInput].value, document.getElementById(formName)[yearInput].value), selectedCountry)) {
	     	window.location = "homepage";
			//alert("Showing site.");

		}
	}
	return false;
}

// minified get age from http://forr.st/~5XW
function getAge(m,d,y){var o=new Date();var l=o.getDate();var k=o.getMonth()+1;var j=o.getFullYear();return(d<=l)?(m<=k)?j-y:j-y-1:(m>=k)?j-y-1:j-y};

function validDOB(month, date, year) {
	var now = new Date();
	
	if (month > 12 || month < 1) {
		alert("Please enter a valid month. (mm)");
		return false;
	}
	
	if (date > 31 || date < 1) {
		alert("Please enter a valid date. (dd)");
		return false;
	}
	
	if (year > now.getFullYear() || year < 1900) {
		alert("Please enter a valid year. (yyyy)");
		return false;
	}
	
	return true;
}

function isOfAgeInCountry(age, country){

	var countryAgeLimit;
	
	//get country's age limit
	switch (country) {
		case "India":
		case "Sweden":
			countryAgeLimit = 25;
			break;
		case "Iceland":
		case "Japan":
		case "Paraguay":
			countryAgeLimit = 20;
			break;
		case "Canada":
		case "Nicaragua":
		case "South Korea":
		 	countryAgeLimit = 19;
			break;
		case "Algeria":
		case "Argentina":
		case "Australia":
		case "Bahamas":
		case "Barbados":
		case "Belarus":
		case "Belize":
		case "Bermuda":
		case "Bolivia":
		case "Botswana":
		case "Brazil":
		case "British Virgin Islands":
		case "Bulgaria":
		case "Cameroon":
		case "Cape Verde":
		case "Central African Republic":
		case "Chile":
		case "China":
		case "Colombia":
		case "Congo, Republic of":
		case "Costa Rica":
		case "Croatia":
		case "Czech Republic":
		case "Denmark":
		case "Dominican Republic":
		case "Ecuador":
		case "Egypt":
		case "El Salvador":
		case "Eritrea":
		case "Estonia":
		case "Ethiopia":
		case "Finland":
		case "France":
		case "Guatemala":
		case "Guyana":
		case "Hungary":
		case "Indonesia":
		case "Ireland":
		case "Israel":
		case "Jamaica":
		case "Kazakhstan":
		case "Kenya":
		case "Latvia":
		case "Lesoto":
		case "Lithuania":
		case "Malawi":
		case "Mauritius":
		case "Mexico":
		case "Moldova":
		case "Mongolia":
		case "Mozambique":
		case "Namibia":
		case "New Zealand":
		case "Niger":
		case "Nigeria":
		case "Panama":
		case "Papua New Guinea":
		case "Peru":
		case "Philippines":
		case "Russia":
		case "Samoa":
		case "Seychelles":
		case "Singapore":
		case "Slovak Republic":
		case "South Africa":
		case "Spain":
		case "St. Maarten":
		case "Thailand":
		case "Trinidad and Tobago":
		case "Turkey":
		case "Turkmenistan":
		case "Uganda":
		case "Ukraine":
		case "United Kingdom":
		case "Uruguay":
		case "Vanuatu":
		case "Venezuela":
		case "Zambia":
		case "Zimbabwe":
		 	countryAgeLimit = 18;
			break;
		case "Cyprus":
			countryAgeLimit = 17;
			break;
		case "Antigua":
		case "Belgium":
		case "Georgia":
		case "Germany":
		case "Greece":
		case "Luxembourg":
		case "Malta":
		case "Norway":
		case "Poland":
		case "Portugal":
			countryAgeLimit = 16;
			break;
		case "Albania":
		case "Armenia":
		case "Azerbaijan":
		case "Comoros":
		case "Equatorial Guinea":
		case "Fiji":
		case "Gabon":
		case "Ghana":
		case "Guinea-Bissau":
		case "Kyrgyuzstan":
		case "Morocco":
		case "Solomon Islands":
		case "Swaziland":
		case "Togo":
		case "Tonga":
		case "Viet Nam":
			countryAgeLimit = 0;
			break;
		default:
			countryAgeLimit = 21;
	}
	
	if (age < countryAgeLimit) {
		window.location = "http://www.google.com";
		//alert("BOUNCE.");
		return false;
	}

	return true;
}

function is21ThisYear(yearOfBirth){

  var now = new Date();
  
  if (!yearOfBirth.match(/\d{4}/) || yearOfBirth == "00") {
    alert('Please enter a valid Year of Birth [YYYY]');
    return false;
  }
  
  if ((now.getFullYear() - yearOfBirth) < 21) {
    window.location = "http://www.google.com";
    return false;
  }
  
  return true;
}

function checkForBypass(){

  var verifiedCookie = readCookie("age_verified");
  if (verifiedCookie == "true") {
    showSite();
    return false;
  }
  
  var uri = document.location;
  if (uri.search) {
  
    var lv = getQueryVariable(uri.search.substring(1), "lv");
    if (lv && lv.match(/^[\d]+$/)) {
    
      var now = Math.round(new Date().getTime() / 1000);
      var two_hours_ago = now - (lastVerifiedValidFor * 60);
      if (lv > two_hours_ago && lv < now) {
        showSite();
        return false;
      }
    }
  }
  
  document.body.style.backgroundColor = "#000000"
  document.getElementById("ageCheck").style.visibility = 'visible';
  
}


