/*global $, jQuery, fb */

$(document).ready(function () {
    
    "use strict";
    
    var showSuccess, showError, showArrows, hideArrows;
    
    // Display the success "thank you" message in a lightbox.
    showSuccess = function (id) {
        
        var url;
        
        url = "/iframes/lead-form-thankyou.php";
        url += "?name=" + $("#LeadFormName").val();
        url += "&id=" + id;
        
        $.fn.colorbox({
            iframe: true,
            href: url,
            title: "Thank You!",
            width: "400px",
            height: "300px"
        });
        $("#LeadForm form").get(0).reset();

    };
    
    // Display an error message in a lightbox.
    showError = function () {
        
        var url;
        
        url = "/iframes/lead-form-thankyou.php?error=1";
        
        $.fn.colorbox({
            iframe: true,
            href: url,
            title: "An error occured",
            width: "400px",
            height: "300px"
        });
        $("#LeadForm form").get(0).reset();

    };

    // Show the arrows and disable the submit button.
    showArrows = function () {
        $("#LeadFormArrows").show();
        $("#LeadFormSubmit").attr("disabled", "true");
    };
    
    // Hide the arrows and re-enable the submit button.
    hideArrows = function () {
        $("#LeadFormArrows").hide();
        $("#LeadFormSubmit").removeAttr("disabled");
    };
    
    // Form validation
    $("#LeadForm form").validate({
        rules: {
            LeadFormName: "required",
            LeadFormEmail: {
                required: true,
                email: true
            },
            LeadFormDistrict: "required",
            LeadFormPosition: "required",
            LeadFormZip: "required"
        },
        messages: {
            LeadFormName: "Please include your name.",
            LeadFormEmail: "Please provide a valid e-mail address.",
            LeadFormDistrict: "Please provide your school district.",
            LeadFormPosition: "Please provide your job title or position.",
            LeadFormZip: "Please provide your postal code."
        },
        submitHandler: function (form) {
            
            var fields;
            
            fields = {};
            
            fields.campaignID = $("#LeadFormCampaignID").val(); 
            fields.name = $("#LeadFormName").val();
            fields.email = $("#LeadFormEmail").val();
            fields.title = $("#LeadFormPosition").val();
            fields.district = $("#LeadFormDistrict").val();
            fields.school = $("#LeadFormSchool").val();
            fields.zip = $("#LeadFormZip").val();
            
            $.ajax({
                beforeSend: function () {
                    showArrows();
                },
                cache: false,
                data: fields,
                dataType: "json",
                error: function () {
                    hideArrows();
                    showError();
                },
                success: function (data) {
                    
                    // Data stored successfully.
                    if (data.success) {
                        hideArrows();
                        showSuccess(data.insert_id);
                    }
                    
                    // Unable to store.
                    else {
                        hideArrows();
                        showError();
                    }

                },
                url: "/scripts/camp/store-contact.php"
            });

        }
    });           
  
});
