
var KEEP_AGE_DAYS   = 31;
var AGE_LIMIT_YEARS = 21;
var DAY_IN_MILLISECONDS = 1000 * 60 * 60 * 24;
var REDIRECT_TIMEOUT_SECONDS = 5;
var REDIRECT_URL_APPROVED = "http://sparklingnuvo.com/index2.html";
var REDIRECT_URL_DECLINED = "http://www.centurycouncil.org/landing";

var ageDay   = 0;
var ageMonth = 0;
var ageYear  = 0;

$(function() {
     ageDay   = initAgePart('ageDay', 'day');                		
     ageMonth = initAgePart('ageMonth', 'month');
     ageYear  = initAgePart('ageYear', 'year');
});


function initAgePart(cookieName, elementID) {
    var result = readCookie(cookieName);
    if (!result) {
        result = 0;
    } else {
        result = parseInt(result);
        $("#" + elementID).val(result);
        $("#remember_age_yes").attr("checked", "checked");
    }
    return result;
}

function markError(selector) {
    $(selector).fadeTo('fast', 0.5).fadeTo('fast', 1);
}

function verifyAge() {
    var result = true;
    
    ageDay   = parseInt($("#day").val());
    ageMonth = parseInt($("#month").val());
    ageYear  = parseInt($("#year").val());
    
    if (!ageDay) {
        result = false;
        markError("#day");
    }
    if (!ageMonth) {
        result = false;
        markError("#month");
    }
    if (!ageYear) {
        result = false;
        markError("#year");
    }
    if (!result) {
        $("#dob_message").text("Please enter your date of birth to view our website");
    }

    if (!$("#remember_age_yes").attr("checked") && !$("#remember_age_no").attr("checked")) {
        result = false;
        markError("#remember_age_yes,#remember_age_no");
    }
    
    if (!result) {
        return;
    }
    
    var dobDate = new Date(ageYear, ageMonth - 1, ageDay, 1, 1, 1);
    var now   = new Date();
    var today = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 1, 1, 1);
    var diffAge = (today.getTime() - dobDate.getTime())/DAY_IN_MILLISECONDS/365.25;
    
    if ($("#remember_age_yes").attr("checked")) {
        createCookie('ageDay',   ageDay,   KEEP_AGE_DAYS);
        createCookie('ageMonth', ageMonth, KEEP_AGE_DAYS);
        createCookie('ageYear',  ageYear,  KEEP_AGE_DAYS);
    } else {
        eraseCookie('ageDay');
        eraseCookie('ageMonth');
        eraseCookie('ageYear');
    }
    
    if (diffAge < AGE_LIMIT_YEARS) {
        $("#dob_message").text("We require all visitors to our website be at least 21 years of age");
        markError("#year,#day,#month");
        
        $.timer(REDIRECT_TIMEOUT_SECONDS * 1000, function (timer) {
            timer.stop();
            window.location.href = REDIRECT_URL_DECLINED;
        });
    } else {
        window.location.href = REDIRECT_URL_APPROVED;
    }
}


function createCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') {
            c = c.substring(1,c.length);
        }
        if (c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length,c.length);
        }
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

//timer support
jQuery.timer = function (interval, callback) {
    var interval = interval || 100;
    if (!callback) return false;

    _timer = function (interval, callback) {
        this.stop = function () {
            clearInterval(self.id);
        };

        this.internalCallback = function () {
            callback(self);
        };

        this.reset = function (val) {
            if (self.id) clearInterval(self.id);
            var val = val || 100;
            this.id = setInterval(this.internalCallback, val);
        };

        this.interval = interval;
        this.id = setInterval(this.internalCallback, this.interval);

        var self = this;
    };

    return new _timer(interval, callback);
};
