$(document).ready(function() {

      var webServiceUrl = './SalesEmailService/Emailer.asmx';

    
    //////////////////////////////////////////Contact Me///////////////////////////////////////////

    $('#cusSubmit').click(function(e) {
        toggleFormElements('#contactMeForm', true)
        e.preventDefault();
        var isValid = validateForm('#contactMeForm');

        if (isValid) {
            $.ajax({
                type: 'POST',
                data: '{"name": "' + $("#cus_txtContactName").val() + '", "company": "' + $("#cus_txtCompanyName").val()
                        + '", "phoneNumber": "' + $("#cus_txtPhoneNo").val() + '", "emailAddress": "' + $("#cus_txtEmail").val()
                        + '", "comments": "' + $("#cus_txtMessage").val() + '"}',
                url: webServiceUrl + '/ContactMe',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success:
                          function(data, textStatus, XMLHttpRequest) {
                              if (data.d == true) {
                                  $("#formMessage").html('Thank you for your request. <br/><br/>A Console representative will be in touch with you shortly.');
                              }
                              else {
                                  $("#formMessage").html('An error has occurred. Please try again later.');
                              }
                          },
                error:
                          function(XMLHttpRequest, textStatus, errorThrown) {
                              $("#formMessage").html('An error has occurred. Please try again later.');
                          }
            });

            $('#cus-formContainer').fadeOut('slow', function() {
                $("#formMessage").fadeIn('fast').delay(2500).fadeOut('slow', function() {
                    fadeOutContainer();
                    clearForm('#contactMeForm');
                });
            });
        }

        toggleFormElements('#contactMeForm', false)
    });


    //////////////////////////////////////////Place an Order///////////////////////////////////////////

    $('#paoSubmit').click(function(e) {
        toggleFormElements('#placeAnOrderForm', true)

        e.preventDefault();
        var isValid = validateForm('#placeAnOrderForm');

        if (isValid) {

            var hasUsedGateway = $('#rbGWUsedYes').is(':checked') ? "Yes" : "No";
            var experienceLevel = $('#rbGWUsedNo').is(':checked') ? "None" : $("#selLevelExp option:selected").text();

            $.ajax({
                type: 'POST',
                data: '{"name": "' + $("#pao_txtContactName").val() + '", "company": "' + $("#pao_txtCompanyName").val() + '", "phoneNumber": "'
                        + $("#pao_txtPhoneNo").val() + '", "emailAddress": "' + $("#pao_txtEmail").val() + '", "currentSoftwarePackage": "'
                        + $("#pao_txtSoftwarePackage").val() + '", "numberOfUsers": "' + $("#pao_txtNumberOfUsers").val() + '", "hasUsedGateway": "'
                        + hasUsedGateway + '", "experienceLevel": "' + experienceLevel + '"}',
                url: webServiceUrl + '/RequestADemo',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success:
                          function(data, textStatus, XMLHttpRequest) {                              
                              if (data.d == true) {
                                  $("#formMessage").html('Thank you for your order. <br/><br/>A Console representative will be in touch shortly to arrange a product demonstration.');
                              }
                              else {
                                  $("#formMessage").html('An error has occurred. Please try again later.');
                              }                              
                          },
                error:
                          function(XMLHttpRequest, textStatus, errorThrown) {
                                $("#formMessage").html('An error has occurred. Please try again later.');
                          }
            });

            $('#pao-formContainer').fadeOut('slow', function() {
                $("#formMessage").fadeIn('fast').delay(2500).fadeOut('slow', function() {
                    fadeOutContainer();
                    clearForm('#placeAnOrderForm');
                    $('#trLevelExp').hide();
                });
            });
        }

        toggleFormElements('#placeAnOrderForm', false);
    });

});
