﻿$(function () {
    $("#topnav_contact").addClass("selected"); // Set the highlighted top nav item

    // Set the contact type if one is specified in the querystring
    var type = getParameterByName("type");
    if (type != "")
        $('#ddlContactType').val(type);

    // Handles the contact submit buttion click
    $('#btnEmailSubmit').click(function () {
        SendContactEmail();
    });
});

function SendContactEmail() {
    if (Validate()) {
        // Get the values from the contact form
        var contactType = $('#ddlContactType').val();
        var name = escape($('#txtbxName').val());
        var email = $('#txtbxEmailAddress').val();
        var organization = escape($('#txtbxOrganization').val());
        var message = escape($('#txtbxMessage').val());
        // Call the controller to send the email
        $('#lblContactEmailResponse').hide();

        $.ajax({
            type: "POST",
            url: "/Contact/SendContactEmail",
            data: "contactType=" + contactType + "&name=" + name + "&email=" + email + "&organization=" + organization + "&message=" + message,
            dataType: "json",
            error: function (xhr, status, error) {
                // Show the error
                $('#lblContactEmailResponse').attr('class', 'errormsg');
                $('#lblContactEmailResponse').text(xhr.responseText);
                $('#lblContactEmailResponse').attr('style', 'display:block;');
            },
            success: function (data, textSuccess) {
                // show the success message
                $('#lblContactEmailResponse').attr('class', 'successmsg');
                $('#lblContactEmailResponse').text("Contact email sent successfully, we will respond back shortly.");
                $('#lblContactEmailResponse').show();
                window.scrollTo(0, 0);
            }
        });
    }
}

function Validate() {
    $.validity.start();

    $("#txtbxName").require();
    $("#txtbxEmailAddress").require().match("email");
    $("#txtbxMessage").require();

    var result = $.validity.end();
    return result.valid;
}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}
