$(document).ready(function(){

	if($.cookie('stella')){
		window.location = "recyclage-de-luxe.html";
	}
        countries = { "United States": "21",
        "United Kingdom": "18",
        "Canada En": "19",
        "Canada Fr": "19",
        "Argentina": "18",
        "Russia": "16",
        "Ukraine": "18",
        "Czech Rep": "18",
        "Australia": "18",
        "New Zealand": "18",
        "Albania": "16",
        "Algeria": "18",
        "Argentina": "18",
        "Armenia": "16",
        "Australia": "18",
        "Austria": "16",
        "Azerbaijan": "18",
        "Belarus Rus": "18",
        "Belarus Ukr": "18",
        "Belgium En": "18",
        "Belgium Nl": "18",
        "Belgium Fr": "18",
        "Bolivia": "18",
        "Bosnia Herzegovina": "16",
        "Brazil": "18",
        "Bulgaria": "18",
        "Cambodia": "16",
        "Cameroon En": "18",
        "Cameroon Fr": "18",
        "Canada En": "19",
        "Canada Fr": "19",
        "Chile": "18",
        "China": "18",
        "Colombia": "18",
        "Costa Rica": "18",
        "Croatia": "18",
        "Czech Rep": "18",
        "Denmark": "16",
        "Ecuador": "18",
        "Egypt": "18",
        "El Salvador": "18",
        "Eritrea": "18",
        "Estonia": "18",
        "Ethiopia": "18",
        "Finland": "18",
        "France": "16",
        "Georgia": "16",
        "Germany": "18",
        "Greece": "18",
        "Guatemala": "18",
        "Guyana": "18",
        "Honduras": "21",
        "Hong Kong": "18",
        "Hungary": "18",
        "Iceland": "20",
        "India": "25",
        "Indonesia": "21",
        "Ireland": "18",
        "Israel": "18",
        "Italy": "16",
        "Japan": "20",
        "Kazakhstan": "18",
        "Kenya": "18",
        "Kyrgyzstan": "16",
        "Latvia": "18",
        "Liechtenstein": "17",
        "Lithuania": "18",
        "Macedonia": "18",
        "Malaysia": "18",
        "Malta": "16",
        "Mexico": "18",
        "Moldova": "16",
        "Mongolia": "21",
        "Namibia": "18",
        "Netherlands": "18",
        "New Zealand": "18",
        "Nicaragua": "19",
        "Niger": "18",
        "Nigeria": "18",
        "Norway": "18",
        "Palau": "18",
        "Panama": "18",
        "Papua New Guinea": "18",
        "Paraguay": "18",
        "Peru": "18",
        "Philippines": "18",
        "Poland": "18",
        "Portugal": "18",
        "Romania": "18",
        "Russia": "18",
        "Samoa": "21",
        "Seychelles En": "18",
        "Seychelles Fr": "18",
        "Serbia": "16",
        "Singapore": "18",
        "Slovak Rep": "18",
        "Slovenia": "18",
        "Solomon Isl": "21",
        "South Africa": "18",
        "South Korea": "19",
        "Spain": "18",
        "Sri Lanka": "18",
        "Suriname": "16",
        "Sweden": "18",
        "Switzerland": "18",
        "Thailand": "18",
        "Turkey": "18",
        "Turkmenistan": "18",
        "Uganda": "18",
        "Ukraine": "18",
        "United Kingdom": "18",
        "United States": "21",
        "Uruguay": "18",
        "Uzbekistan": "18",
        "Vanuatu En": "18",
        "Vanuatu Fr": "18",
        "Venezuela": "18",
        "Zambia": "18",
        "Zimbabwe": "18" }
        
        
		jQuery.each(countries, function(i, val) {
			$("<option>").attr("value", i).text(i).appendTo("#country");
		});
        
		for(i=1910;i<2010;i++){
			$("<option>").attr("value", i).text(i).appendTo("#year");
		}

		$("#year").attr('selectedIndex', 99);
	
});

function checkAge(){
	
	var redirectPath = $(document).getUrlParam("path");
	if(!redirectPath){
		redirectPath = 'recyclage-de-luxe.html';
	}
   
 error_msg = 'Sorry, it looks like you are not old enough';

    c = $('#country').val();
    d = $('#day').val();
    age_minimum = countries[c];
    y = $('#year').val();
    m = $('#month').val();
    remember_me = $('#rememberme').is(':checked');
    
    var today = new Date();
	// console.log(today.getYear());

	var curr_year = today.getFullYear();
	var curr_month = today.getMonth() + 1;
	var curr_day = today.getDate();

	/* get the current value of the input field */
	year_value = y;
	month_value = m;
	day_value = d;

	if (m > 12) {
		$('age_month').val(12);
	}
	
	year_diff = curr_year - year_value;
	month_diff = curr_month - month_value;
	day_diff = curr_day - day_value;

    USER_YEAR_POSSIBLY_UNDERAGE = year_diff <= age_minimum;
    USER_YEAR_UNDERAGE = year_diff < age_minimum;
    USER_YEAR_EXACTLY_UNDERAGE_YEAR = year_diff == age_minimum;
    USER_MONTH_EXACTLY_THIS_MONTH = month_diff == 0;
    USER_BORN_THIS_MONTH_UNDERAGE_YEARS_AGO = USER_MONTH_EXACTLY_THIS_MONTH && USER_YEAR_EXACTLY_UNDERAGE_YEAR;
    
    if(USER_YEAR_UNDERAGE){
        // console.log("Underage");
        $('#error').html(error_msg);
    }
    else if(USER_YEAR_POSSIBLY_UNDERAGE){
        NEGATIVE_MONTH_DIFF = month_diff < 0;
        NEGATIVE_DAY_DIFF = day_diff <= 0;
        if(NEGATIVE_MONTH_DIFF && USER_YEAR_EXACTLY_UNDERAGE_YEAR){
            // console.log("Denying entry because although you're born in right year, your birthday hasn't come yet.")
            $('#error').html(error_msg);
        }
        else if(!NEGATIVE_MONTH_DIFF && USER_YEAR_EXACTLY_UNDERAGE_YEAR && !NEGATIVE_DAY_DIFF){
            //console.log("Just made it! Born this year under-age years ago");
            allowEntry();
        }
        else if(USER_BORN_THIS_MONTH_UNDERAGE_YEARS_AGO && NEGATIVE_DAY_DIFF){
            // console.log("Denying entry - a few more days to go");
            $('#error').html(error_msg);
        }
        else if(USER_BORN_THIS_MONTH_UNDERAGE_YEARS_AGO && !NEGATIVE_DAY_DIFF){
            allowEntry();
        }
        else {
            // console.log("error");
        }
    }
    else {
        allowEntry();
    }
    
    function allowEntry(){
        if(remember_me == true){
            $.cookie('stella', 'age_check_complete', { expires: 7, path: '/'});
            window.location = redirectPath;
        } else {
            $.cookie('stella', 'age_check_complete');
            window.location = redirectPath;
        }
    }

}
