﻿$(document).ready(function () {

    $('#checker').hide();

    $.blockUI({
        message: $('#checker'),
        // checking window sizes and calculating centre 
        css: {
            top: ($(window).height() - 580) / 2 + 'px',
            left: ($(window).width() - 911) / 2 + 'px',
            width: '911px'
        }
    });

    //this checks for an existing cookie and redirects if it's set
    if ($.cookie('is_legal') == 'yes') {
        //window.location.replace("http://www.snowleopardvodka.co.uk"); 
        $('#checker').hide();
        $.unblockUI();
    }

    $('.AgeBtn').click(function () {

        /* the minumum age you want to allow in */
        var min_age = 18;

        var year = parseInt($('.age-year').val()) + min_age;
        var month = $('.age-month').val();
        var day = $('.age-day').val();
        var invalidDate = isNaN(month) || isNaN(year) || isNaN(day);
        var theirDate = new Date();
        theirDate.setFullYear((year), (month - 1), day);
        var today = new Date;

        if (invalidDate) {
            alert("Please enter a valid date of birth");
        }
        else {


                if ($('.checkTerms').is(':checked')) {


                        if ((today.getTime() - theirDate.getTime()) < 0) {
                            alert("You are too young to enter this site!");
                            $.cookie('is_legal', 'no', { expires: 1, path: '/' });
                            //$('#checker').show();
                            $.blockUI({
                                message: $('#checker'),
                                css: {
                                    top: ($(window).height() - 580) / 2 + 'px',
                                    left: ($(window).width() - 911) / 2 + 'px',
                                    width: '911px'
                                }
                            });
                            return false;
                        }
                        else {
                            $.cookie('is_legal', 'yes', { expires: 1, path: '/' });
                            $('#checker').hide();
                            //window.location.replace("http://www.snowleopardvodka.co.uk");
                            $.unblockUI();

                            return false;

                        }
                        //            ev.preventDefault();

                }
                else {

                    alert('Please agree to our terms and conditions');

                }

        }

    });

    //alert($.cookie('is_legal'));

});

