﻿

function SendEmailRequest() {

    var feedbackDetails = {};

    feedbackDetails.name = $("[id$='_txtName']").val();
    feedbackDetails.emailAddress = $("[id$='_txtEmail']").val();
    feedbackDetails.telephone = $("[id$='_txtTelephone']").val();
    feedbackDetails.registrationNoOrPostcode = $("[id$='txtregistrationNoOrPostcode']").val();
    feedbackDetails.comments = $("[id$='_txtMessage']").val();
    feedbackDetails.dpa = $("[id$='_chkDPA']").attr('checked');
    
    // Create a data transfer object (DTO) with the proper structure.
    var DTO = { 'feedbackDetails': feedbackDetails };

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/services/ContactUs.asmx/SendFeedback",
        data: JSON.stringify(DTO),
        dataType: "json",
        success: EmailRequestSucceeded,
        error: AjaxSendError
    });
}

// Successful AJAX response handling
function EmailRequestSucceeded(results) {
    if (results.d != 'true' && results.d != true) {
        AjaxSendError();
        return;
    }
    swapDialogClass('stateSuccess');
    $('form').clearForm();
    $('.dialog').dialog('open');
    
    // GOOGLE TAGGING START
    _gaq.push(
		['_trackPageview', 'VIRTUAL/ContactFormSuccess'],
		['cross._trackPageview', 'VIRTUAL/ContactFormSuccess']
	);
	// GOOGLE TAGGING END
}

// Generic AJAX Error Handler
function AjaxSendError() {
    swapDialogClass('stateFailure');
    $('.dialog').dialog('open');
}

// Reset CSS classes to initial state
function resetDialogClass() {
    $('.ui-dialog').removeClass('stateSuccess');
    $('.ui-dialog').removeClass('stateFailure');
    $('.ui-dialog').removeClass('stateQuoteFailure');
}

// Set CSS classes to a new state
function swapDialogClass(newClass) {
    resetDialogClass();
    $('.ui-dialog').addClass(newClass);
}

// Implements the jQuery UI dialog control
function emailOverlays() {
    //	Set dialog options and behaviour
    var dialogOptions = {
        'autoOpen': false,
        'draggable': false,
        'modal': true,
        'resizable': false,
        'width': 640
    };

    //	Setup form as dialog in overlay
    $('.dialog').dialog(dialogOptions);
    $('#summaryDialog').removeClass('hide');

}

function hideFallbackControls() {
    $('.noscript').hide();
    $('#buttonSubmit').removeClass('scriptonly');
    $('#buttonSubmit').attr('style', '"display: block !important"');    
}

$(document).ready(function() {

    hideFallbackControls();
    emailOverlays();

    $("#btnSubmit").click(function() {
        $("form").submit();
    });
    $("form").validate({
        errorPlacement: function(error, element) {
            element.before(error);
        },
        errorClass: "enquiryError",
        showErrors: function(errorMap, errorList) {
            $("#messageBox").html("Your form contains "
                                       + this.numberOfInvalids()
                                       + " error(s), see details below.");
            this.defaultShowErrors();
        },
        messages: {
            txtEmail: {
                required: "Email address",
                email: "Your email address must be in the format of name@domain.com"
            }
        },
        submitHandler: SendEmailRequest
    });
});


