﻿jQuery(document).ready(function() {
    /* FORM FUNCTIONALITY */

    // Set langauge dependant variables.
    // Check for multilanguage override, if not the set default en values.
    if (typeof (fatalErrLangDep) == 'undefined') {
        fatalErrLangDep = 'An error occurred in the form submission. Please try again.';
    }
    if (typeof (reqFieldLangDep) == 'undefined') {
        reqFieldLangDep = 'required field';
    }
    if (typeof (displayLanguageLangDep) == 'undefined') {
        displayLanguageLangDep = 'en';
    }

    // Add updating to the contact field form
    jQuery('fieldset.ContactUsFields input')
            .focus(function() {
                if (this.value == reqFieldLangDep || jQuery(this).css('color') == 'red') {
                    jQuery(this).css({ 'color': 'black' });
                    this.value = '';
                }
            }).blur(function() {
                if (this.value == '') {
                    this.value = reqFieldLangDep;
                    jQuery(this).css({ 'color': 'red' });
                }
            });

    // Add callbacks for the JSON submission service
    jQuery('#SubmitContactButton').click(function() {
        jQuery(this).attr('disable', 'true');
        PostContact();
    });
});

/// The function that posts the data back to the server and unpacks any response
function PostContact() {
    var formSelectors = { 'contactid': '#input_contactid', 'name': '#input_name', 'phone': '#input_name', 'email': '#input_email', 'phone': '#input_phone', 'event': '#input_event', 'comments': '#input_comments' };

    // Respond to the callback from the server
    var processServerResponse = function(response) {
        var container = jQuery('#ContactFormContainer');

        if (response.Error.IsFatal) {
            // A fatal error occurred, and we have a response from the server
            container.html(response.MessageHtml);
            return;
        }

        var params = response.Error.Parameters;
        if (params != 'undefined' && params != null && params.length > 0) {
            // We encountered errors for parameters, display them
            for (var i = 0; i < params.length; i++) {
                // Add the logic to show errors;
                var p = params[i];
                var fieldID = formSelectors[p.Name];
                jQuery(fieldID).css({ 'color': 'red' }).val(p.Description);
            }
            return;
        }

        // It worked!
        container.html(response.MessageHtml);
    };


    // Gather the form parameters to pass to the service in the form of a JSON string
    var gatherJsonParameters = function() {
        var toJoin = new Array('{');
        for (var key in formSelectors) {
            toJoin.push(key);
            toJoin.push(':"');
            jQuery(formSelectors[key]).each(function() {
                toJoin.push(this.value.replace('"', "'"));
            });
            toJoin.push('",');
        }
        // 5938 - Make Contact Us submission support language independance
        toJoin.push('language:"');
        toJoin.push(displayLanguageLangDep);
        toJoin.push('",error_message:"');
        toJoin.push(fatalErrLangDep);
        toJoin.push('",required_field_text:"');
        toJoin.push(reqFieldLangDep);
        toJoin.push('"}');

        return toJoin.join('');
    };

    var params = gatherJsonParameters();

    jQuery.ajax({
        type: 'POST',
        url: '/Services/ContactUs.asmx/SubmitContact',
        data: params,
        beforeSend: function(xhr) {
            xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
            xhr.setRequestHeader('Content-length', params.length);
        },
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(msgData, status) {
            processServerResponse(eval(msgData.d)); // This is safe, because we trust the source
        },
        error: function(xhr, msg, e) {
            alert(fatalErrLangDep);
        }
    });
} 