$('form').each(function() {
    if (!$(this).is('[action]')) {
        $(this).attr('action', '/');
    }
});

function setCookie(key, value, expiry) {
    var expires = new Date();
    if (expiry == null){
        expires.setTime(expires.getTime() + (expiry + 24 * 60 * 60 * 1000));
    } else {
        expires = expiry;
    }
    document.cookie = key + '=' + value + ';SameSite=Strict; path=/; expires=' + expires.toUTCString();
}

function getCookie(cname) {
  var name = cname + "=";
  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);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return null;
}

function eraseCookie(key) {
    var keyValue = getCookie(key);
    setCookie(key, keyValue, '-1');
}

$(document).ready(function(){

    
   $('.consent_button').click(function(e) { //Submits consent and hides the forms
        var url_c = $(this).attr('href'); 
        var consent_cookie = $(this).attr('data-consent-name'); 
        var consent_value = $(this).attr('data-consent-value'); 
        var consent_expiry = new Date();
        consent_expiry.setDate(consent_expiry.getDate() + 30);
       $.ajax({
            url: url_c,
            dataType :'json',
            data : '{}',
            success :  function(data){
                // alert('success');
                
            },
            error :  function () {
                // alert('error');
                // $(this).parents('.consent_popup').hide();
            }
        });

        setCookie(consent_cookie,consent_value, consent_expiry);
        $(this).parents('.consent_popup').children().hide(300);
        $(this).parents('.consent_popup').hide(400);
        e.preventDefault();
        // alert(consent_cookie + ' = ' + getCookie(consent_cookie));
        return false;
    });
  

    $( "#register_member_form" ).submit(function( event ) { //fudge fix so that users don't have to enter a username, instead it's populated by the email input

            $("#username").val($("#email").val());
            return true;

    });

    $( "#contact_form" ).submit(function( event ) { //makes a more helpful subject line 

            $("#subject").val($('#subject').val() + $("#Name").val());
            
            return true;

    });

    $("#email-form").submit(function(e){
        e.preventDefault(); 
        
        var $form = $(this),
        name = $form.find('input[name="name"]').val(),
        email = $form.find('input[name="email"]').val(),
        url = $form.attr('action');
        
        $.post(url, {name:name, email:email},
          function(data) {
              if(data)
              {
                if(data=="Some fields are missing.")
                {
                    $("#m-form-fail").text("Please fill in your name and email.");
                    $("#m-form-fail").show(500);
                }
                else if(data=="Invalid email address.")
                {
                    $("#m-form-fail").show(500);
                    $("#m-form-fail").text("Your email address is invalid.");
                }
                else if(data=="Already subscribed.")
                {
                    $("#m-form-done").text("You were already subscribed! ;)");
                    $("#m-form-done").show(500);
                    setCookie('exp_subscribed','true');
                }
                else
                {
                    $("#m-form-done").text("Success. You're subscribed!");
                    $("#m-form-done").show(500);

                    $("#email-form").hide();
                    setCookie('exp_subscribed','true');

                }
              }
              else
              {$("#m-form-fail").show();}
          }
        );
    });
    $("#email-form").keypress(function(e) {
            if(e.keyCode == 13) {
                e.preventDefault(); 
                $(this).submit();
            }
        });
    $("#submit").click(function(e){
        e.preventDefault(); 
        $("#email-form").submit();
    });

    const observer = lozad('.lozad', {
    rootMargin: '1000px 0px'
    }); // lazy loads elements with default selector as '.lozad'
    observer.observe(); 
});