﻿function BindBlurs(){
    $('#contact-form input.required').blur(function () {
        if ($(this).val()) {
            $(this).siblings('.error-message').html('').removeAttr("style");
        }
    });
}

function SubmitForm() {
    if (ValidateForm()) {
        $('#submit-result').html('<p class="in-progress">Submitting...</p>');
        $.ajax({
            type: 'POST',
            url: '/contact-ajax.aspx',
            data: {
                'firstName': $('#txtFirstName').val(),
                'lastName': $('#txtLastName').val(),
                'companyName': $('#txtCompany').val(),
                'email': $('#txtEmail').val(),
                'telephone': $('#txtPhone').val(),
                'facility': $('#txtFacility').val(),
                'services': BuildServicesString(),
                'question': $('#txtQuestion').val()
            },
            success: function (elm) {
                $('#submit-result').html('<p class="thank-you">Thank you for contacting MicroTek. We&#8217;ll be in touch shortly to discuss your event or meeting needs.</p>');
				
				var script = document.createElement("script");
                script.type = "text/javascript";
                script.text = 'var _gaq = _gaq || []; ' +
                                '_gaq.push([\'_trackPageview\', \'/facility/thanks.aspx\']);';
                document.body.appendChild(script);
				
                setTimeout("$.fancybox.close();", 2500);
            },
            error: function (elm, status, message) {
                $('#submit-result').html('<p class="error">An error occured.  Please try again.  If the error persists, contact support.</p>');
                setTimeout("$.fancybox.close();", 2500);
            }

        }); 
    }
    else {
        BindBlurs();
    }
}

function SubmitFormFull() {
    if (ValidateForm()) {
        $('#submit-result').html('<p class="in-progress">Submitting...</p>');
        $.ajax({
            type: 'POST',
            url: '/contact-ajax.aspx',
            data: {
                'firstName': $('#txtFirstName').val(),
                'lastName': $('#txtLastName').val(),
                'companyName': $('#txtCompany').val(),
                'email': $('#txtEmail').val(),
                'telephone': $('#txtPhone').val(),
                'facility': $('#txtFacility').val(),
                'services': BuildServicesString(),
                'question': $('#txtQuestion').val()
            },
            success: function (elm) {
                window.location.href = "/thank-you.aspx";
            },
            error: function (elm, status, message) {
                $('#submit-result').html('<p class="error">An error occured.  Please try again.  If the error persists, contact support.</p>');
            }
        });
    }
    else {
        BindBlurs();
    }
}

function BuildServicesString() {
    var array = new Array();

    if ($('#chkTrainingFacilities').is(':checked')) {
        array.push("Training Facilities");
    }
    if ($('#chkMeetingFacilities').is(':checked')) {
        array.push("Meeting Accommodations");
    }
    if ($('#chkAccommodations').is(':checked')) {
        array.push("Student Accommodations");
    }
    if ($('#chkCourseware').is(':checked')) {
        array.push("Courseware Printing & Delivery");
    }
    if ($('#chkInstructors').is(':checked')) {
        array.push("Instructors & Moderators");
    }
    if ($('#chkTesting').is(':checked')) {
        array.push("Testing & Learning Analytics");
    }
    if ($('#chkRegistration').is(':checked')) {
        array.push("Event Registration");
    }
    if ($('#chkTraining').is(':checked')) {
        array.push("Virtual Training");
    }
    if ($('#chkOther').is(':checked')) {
        array.push("Other");
    }

    return array.join(",");
}

function ValidateForm() {
    var retVal = true;

    if ($('#txtFirstName').val() == '') {
        $('#first-name-error').html('First name is required').attr("style", "display: block");
        retVal = false;
    }
    else {
        $('#first-name-error').html('').removeAttr("style");
    }

    if ($('#txtLastName').val() == '') {
        $('#last-name-error').html('Last name is required').attr("style", "display: block");
        retVal = false;
    }
    else {
        $('#last-name-error').html('').removeAttr("style");
    }

    if ($('#txtCompany').val() == '') {
        $('#company-error').html('Company is required').attr("style", "display: block");
        retVal = false;
    }
    else {
        $('#company-error').html('').removeAttr("style");
    }

    if ($('#txtEmail').val() == '') {
        $('#email-error').html('Email is required').attr("style", "display: block");
        retVal = false;
    }
    else {
        var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
        if (!$('#txtEmail').val().match(re)) {
            $('#email-error').html('A valid e-mail address is required').attr("style", "display: block");
            retVal = false;
        }
        else {
            $('#email-error').html('').removeAttr("style");
        }
    }

    if ($('#txtPhone').val() == '') {
        $('#phone-error').html('Phone is required').attr("style", "display: block");
        retVal = false;
    }
    else {
        $('#phone-error').html('').removeAttr("style");
    }

    return retVal;
}

function ClearErrors() {    
    $('#first-name-error').html('').removeAttr("style");    
    $('#last-name-error').html('').removeAttr("style");    
    $('#company-error').html('').removeAttr("style");   
    $('#email-error').html('').removeAttr("style");   
    $('#phone-error').html('').removeAttr("style");
}

function ResetForm() {
    $('#submit-result').html('')
    $('#chkTrainingFacilities').removeAttr('checked');
    $('#chkMeetingFacilities').removeAttr('checked');
    $('#chkAccommodations').removeAttr('checked');
    $('#chkCourseware').removeAttr('checked');
    $('#chkInstructors').removeAttr('checked');
    $('#chkTesting').removeAttr('checked');
    $('#chkRegistration').removeAttr('checked');
    $('#chkTraining').removeAttr('checked');
    $('#chkOther').removeAttr('checked');
    $('#txtFirstName').val('');
    $('#txtLastName').val('');
    $('#txtCompany').val('');
    $('#txtEmail').val('');
    $('#txtPhone').val('');
    $('#txtFacility').val('');
    $('#txtQuestion').val('');
}

