﻿/// <reference path="jquery.min-vsdoc.js" />

$.ajaxSetup({
    dataFilter: function(data) {
        var msg;

        // Checks if we can use the browsers native JSON parser for speed
        if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function')
            msg = JSON.parse(data);
        else
            msg = eval('(' + data + ')');

        return msg;
    }
});

function CheckTextBoxes(value) {
    if (value == null)
        return false;
    if (value == "" || value == " ")
        return false;
    return true;
}

$(document).ready(function() {

    $('#SendForm').click(function() {
        // Abort sending form if there are validation errors
        var isError = false;
        if (!CheckTextBoxes($('#ctl00_cphBody_ReplyBackBox').val())) {
            $('#emailErr').css("display", "inline").text('A method of contact must be entered');
            isError = true;
        }
        else $('#emailErr').css("display", "none");
        if (!CheckTextBoxes($('#ctl00_cphBody_MessageTextBox').val())) {
            $('#messageErr').css("display", "inline").text("A message must be entered");
            isError = true;
        }
        else $('#messageErr').css("display", "none");
        if (isError) return false;


        $(this).attr("disabled", "true");
        var contactCat = $('#ctl00_cphBody_ContactSpecifyDropDown').val();
        var replyType = $('#ctl00_cphBody_ReplyBackBox').val();
        var message = $('#ctl00_cphBody_MessageTextBox').val();
        if (contactCat != null || contactCat != "")
            if (replyType != null || replyType != "")
            if (message != null || message != "")
            $.ajax({
                type: "POST",
                url: "contact.aspx/ContactFromSubmit",
                data: "{'contactCat': '" + contactCat + "', 'replyType': '" + replyType + "', 'message': '" + message + "'}",
                contentType: "application/json; charset=utf-8",
                success: function(msg) {
                    $('div#supportFormElements').css("display", "none");
                    $('span#SuccessfulSendLabel').text(msg.d.toString());
                    $('span#reloadFormLink').html("<br /><br />To send another request or to try again please click <a href=\"contact.aspx\">here.</a>");
                }
            });
        return false;
    });
});

$(document).ajaxError(function() {
    if (window.console && window.console.error) {
        console.error(arguments);
    }
    $('div#supportFormElements').css("display", "none");
    $('span#SuccessfulSendLabel').text("Sorry, there seems to have been a problem sending your request. This error has been logged. If your request is urgent please use our alternative contact details below. We apologise for this inconvenience");
    $('span#reloadFormLink').html("<br /><br />To try sending your request again please click <a href=\"Contact.aspx\">here.</a>");
});