﻿var CONTACT_BUTTON = '#sendButton';
var CONTACT_FORM = '#contactForm';
        
function throwError(MSG, msg) {
    $(MSG).html('<div class="error">Sorry, we could not process this request. ' + msg + '</div>');
    $(CONTACT_FORM).validate();
}

var globalValidate;

Contact = {
    init: function() {
        
        var validator = globalValidate = $(CONTACT_FORM).validate();
        
        $(CONTACT_BUTTON).click(function() {
            if ($(CONTACT_FORM).valid()) {
                $(CONTACT_BUTTON).text('Sending...');
                $.post("/about-us/contact-us", {
                    FullName: $('#FullName').val(),
                    Email: $('#Email').val(),
                    UserType: $('#UserType').val(),
                    Topic: $('#Topic').val(),
                    Message: $('#Message').val()
                },
                    function(data) {
                        $(CONTACT_BUTTON).text('Send');
                        if (data.success) {
                            $('#contactFields').slideUp();
                            validator.resetForm();
                            $('#contactMsg').html('<span class="success">Your message has been sent.  Thank you for contacting us.</span>');
                        } else {
                            throwError('#contactMsg', data.msg);
                        }
                    }, 'json');
            }
        });
    }
};

$().ready(function() {
    Contact.init();
});