﻿var ageGate = {
    init:function(){
         $(".styled").change(function(){
                country = $(this).val();
                
                $.ajax({
                    url: baseUrl + "askage.aspx?country="+country,
                    type:"get",
                    dataType:"json",
                    success: function(data, textStatus){
                        if(data.minimumAge == "0" || data.minimumAge =="1000"){
                            $(".ageText").addClass("disabled");
                            $(".ageText").attr("disabled","disabled");
                        }
                        else
                        {
                            $(".ageText").removeClass("disabled");
                            $(".ageText").removeAttr("disabled");
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown){
                            $('.error').removeClass('hide');
                    }
                });
            });
            
            $(".enterHere").click(function(){
                var month = $('.month').val();
                var day = $('.day').val();
                var year = $('.year').val();
                var rem = $('#remember').val();
                var country =$(".styled").val();
                var option = {path: '/', expires:10};
                var validate;
                
                if($('.ageText').attr('disabled')){
                    month =01;
                    day =01;
                    year = 1795;   
                }
                
                data = "cuervoCountry="+country+"&txtMonth="+month+"&txtDay="+day + "&txtYear="+year+"&remember="+rem;
                $.ajax({
                    url: baseUrl + "validate.aspx",
                    type:"GET",
                    dataType:"json",
                    data:data,
                    success: function(data, textStatus){
                        if(rem == "yes")
                        {
                            $.cookie("month",month,option);
                            $.cookie("day",day,option);
                            $.cookie('year',year,option);
                            $.cookie('country',country,option);
                        }
                    
                        if(data.goto == "home"){
                            window.location =  baseUrl + "home";
                        }else
                        {
                            window.location = data.goto;
                        }
                    },
                    error:function(XMLHttpRequest, textStatus, errorThrown)
                    { 
                        $('.error').removeClass('hide');
                        setTimeout(window.location.replace('http://www.centurycouncil.org/'), 5000);
                    }
                    
                });
            });
    }
}

