﻿$(function() {//validate audi enquiry form within audi centre sections

    var title = $("#title"),
	firstName = $("#first_name"),
	surname = $("#surname"),
	building = $("#name_number"),
	address = $("#address"),
	county = $("#county"),
    postCode = $("#post_code"),
    telephone = $("#phone"),
    telephone2 = $("#phone2"),
    emailAddress = $("#email"),
	allFields = $([]).add(title).add(firstName).add(surname).add(address).add(county).add(postCode).add(telephone).add(telephone2).add(emailAddress),
	tips = $("#validateTips");

    function updateTips(t) {
        tips.text(t).effect("highlight", {}, 1500);
    }

    function checkLength(o, n, min, max, fieldName) {

        if (o.val().length < min) {
            o.addClass('ui-state-error');
            updateTips("Please enter your " + fieldName + ".");
            o.focus();
            return false;
        } else {
            return true;
        }
    }

    function checkRegexp(o, regexp, n) {
        if (!(regexp.test(o.val()))) {
            o.addClass('ui-state-error');
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }

    $('#send-enquiry').click(function() {
        var bValid = true;
        allFields.removeClass('ui-state-error');
        bValid = bValid && checkLength(title, "title", 1, 255, "title");
        bValid = bValid && checkLength(firstName, "first_name", 1, 255, "first name");
        bValid = bValid && checkLength(surname, "surname", 1, 255, "surname");
        bValid = bValid && checkLength(building, "name_number", 1, 255, "building name/number");
        bValid = bValid && checkLength(address, "address", 1, 255, "address");
        bValid = bValid && checkLength(county, "county", 1, 255, "county");
        bValid = bValid && checkLength(postCode, "post_code", 1, 255, "post code");
        bValid = bValid && checkLength(telephone, "phone", 1, 255, "telephone number");
        bValid = bValid && checkLength(telephone2, "phone2", 1, 255, "alternative telephone number");
        bValid = bValid && checkLength(emailAddress, "email", 1, 255, "email address");

        bValid = bValid && checkRegexp(emailAddress, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "Please supply a valid email address.");
        if (bValid) {
            var enqVehicle = '{' + '"centreID":"' + $("#hCentreId").val() + '"' + ',';
            enqVehicle += '"title":"' + $("#title").val() + '"' + ',';
            enqVehicle += '"firstName":"' + $("#first_name").val() + '"' + ',';
            enqVehicle += '"surname":"' + $("#surname").val() + '"' + ',';
            enqVehicle += '"company":"' + $("#company").val() + '"' + ',';
            enqVehicle += '"building":"' + $("#name_number").val() + '"' + ',';
            enqVehicle += '"address":"' + $("#address").val() + '"' + ',';
            enqVehicle += '"county":"' + $("#county").val() + '"' + ',';
            enqVehicle += '"postcode":"' + $("#post_code").val() + '"' + ',';
            enqVehicle += '"telephone":"' + $("#phone").val() + '"' + ',';
            enqVehicle += '"telephone2":"' + $("#phone2").val() + '"' + ',';
            enqVehicle += '"emailAddress":"' + $("#email").val() + '"' + ',';
            enqVehicle += '"subscribe":"' + $("#subscribe").attr("checked") + '"' + ',';
            enqVehicle += '"enquiry":"' + $("#message").val() + '"}';
            $.ajax({
                type: "POST",
                url: "/helpers/EmailHelper.aspx/EmailAudiEnquiry",
                contentType: "application/json; charset=utf-8",
                data: enqVehicle,
                dataType: "json",
                success: function(msg) {
                    updateTips("Thank you. Your enquiry has been sent successfully!");
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    updateTips("We are sorry but there was an error sending your enquiry, please call the dealership on the number shown. Thank you.");
                }
            })
        }
    })
});
	