﻿function validateAndSend(name, email, telephone, message, type) {
    //do some checks
    var nameGood = true;
    var emailGood = true;
    var teleGood = true;
    var messGood = true;
    //check the name
    if (name.length == 0) {
        nameGood = false;
    }
    //check the email
    if (email.length == 0) {
        emailGood = false;
    }
    //check the telephone
    if (telephone.length == 0) {
        teleGood = false;
    }
    //check the message
    if (message.length == 0) {
        messGood = false;
    }
    var errMessage = "";
    if (!nameGood) errMessage += "Please enter your name.";
    if (!emailGood) errMessage += "\nPlease check your email address.";
    if (!teleGood) errMessage += "\nPlease check your telephone number.";
    if (!messGood) errMessage += "\nPlease enter a message for our advisors.";
    if (errMessage.length != 0) {
        alert(errMessage);
    }
    else {
        $.ajax({
            type: "POST",
            url: "/AJAXSendContactForm.aspx",
            data: "name=" + name + "&email=" + email + "&telephone=" + telephone + "&message=" + message + "&type=" + type,
            success: function(data, textStatus) {
                if (data == "success") {
                    alert("Thankyou, your request has been successfully received and you will hear from one of our representatives shortly.");
                }
                else {
                    alert("Sorry, there was an error with your request, please try again later");
                }
                reset();
            },
            error: function(ajaxObj, textStatus, errorThrown) {
                alert("Sorry, there was an error with your request, please try again later");
            }
        });
    }
}
function reset() {
    $("#select-contact-method").attr("selectedIndex", "0");
    $(".enquiry").hide();
    $("#input-fields input[type=text], #input-fields textarea").val("");
    $("#input-fields").hide();
}
$(document).ready(function() {
    $(".contact-wizard").show();
    $("#select-contact-method").change(function() {
        var selectedIndex = $(this).attr("selectedIndex");
        $(".enquiry").hide();
        switch (selectedIndex) {
            case 1:
                $(".sales-enquiry").show();
                break;
            case 2:
                $(".delivery-enquiry").show();
                break;
            case 3:
                $(".product-support-enquiry").show();
                break;
            case 4: 
                $(".trade-enquiry").show();
                break;
        }
        if (selectedIndex != 0) {
            $("#input-fields").show();
        }
        else {
            $("#input-fields").hide();
        }
    });
    $("#send-message").click(function() {
        var name = $("#CustomerName").val();
        var email = $("#CustomerEmail").val();
        var telephone = $("#CustomerTelephone").val();
        var message = $("#CustomerMessage").val();
        
        var options = $("#select-contact-method").attr("options");
        var selectedIndex = $("#select-contact-method").attr("selectedIndex");
        var type = options[selectedIndex].text;
        
        validateAndSend(name, email, telephone, message, type);
    });
});
