$.ajaxSetup({ 
     beforeSend: function(xhr, settings) {
         function getCookie(name) {
             var cookieValue = null;
             if (document.cookie && document.cookie != '') {
                 var cookies = document.cookie.split(';');
                 for (var i = 0; i < cookies.length; i++) {
                     var cookie = jQuery.trim(cookies[i]);
                     // Does this cookie string begin with the name we want?
                 if (cookie.substring(0, name.length + 1) == (name + '=')) {
                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                     break;
                 }   
             }   
         }   
         return cookieValue;
         }   
         if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
             // Only send the token to relative URLs i.e. locally.
             xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
         }   
     }   
});

function update_ui(data, textStatus) {
    if (data.agent == true) {
        // Show which fields are required.
        $('.agent-only-field').show();
        return true;
    }
    $('.agent-only-field').hide();
    return false;
}

$(document).ready(function() {
    $('#tripFinder').hide();
    $('#slideShow').hide();
    $('#id_agent_code').blur(function() {
        jQuery.post('/web/verify-agent/', "code="+this.value, update_ui, 'json');
    });
    if ($('#id_agent_code').attr('value') != '') {
        $('.agent-only-field').show();
    }

    // Add some more verbiage to opt-in label.
    $('label[for=id_pre_departure_opt_in]').append('<br/>&nbsp;<br/>In our experience, OUT Adventures clients like to be in touch with each other prior to their official departure date. Please check the box if you wish to be included in this group communication.<br/>&nbsp;');

});

