var $j = jQuery.noConflict();

jQuery.validator.messages.required = "";

$j(document).ready(function () {
    $j("#accountForm").validate({
        invalidHandler: function(e, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {
                var message = errors == 1
                    ? '1 field is incomplete. It has been highlighted below.'
                    : errors + ' fields have not been completed. They have been highlighted below.';
                $j("div.error span").html(message);
                $j("div.error").show();
            } else {
                $j("div.error").hide();
            }
        },
        rules: {
            password: "required",
            confirmPassword: {
                equalTo: "#password"
            }
        }
    });
    $j("#bookingForm").validate({
        invalidHandler: function(e, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {
                var message = errors == 1
                    ? '1 field is incomplete. It has been highlighted below.'
                    : errors + ' fields have not been completed. They have been highlighted below.';
                $j("div.error span").html(message);
                $j("div.error").show();
            } else {
                $j("div.error").hide();
            }
        },
        rules: {
            companyName: "required",
            address1: "required",
            postcode: "required",
            userTitle: "required",
            userFirstName: "required",
            userSurname: "required",
            telephoneNumber: "required",
            emailAddress: "required",
            bookingCompanyName: {
                required: "#agencyBooking:checked"
            },
            bookingAddress1: {
                required: "#agencyBooking:checked"
            },
            bookingTown: {
                required: "#agencyBooking:checked"
            },
            bookingPostcode: {
                required: "#agencyBooking:checked"
            },
            bookingContactName: {
                required: "#agencyBooking:checked"
            },
            bookingTelephone: {
                required: "#agencyBooking:checked"
            },
            bookingEmail: {
                required: "#agencyBooking:checked"
            },
            termsAcceptance: "required"
        }
    });
    $j("#agencyBookingDisclosedAgent").click(function () {
        if ($j("#agencyBookingDisclosedAgent").attr("checked")) {
            $j("#agencyBookingOwnAccount").attr("checked", "");
            //$j("#termsFieldset").fadeOut("normal");
           // $j("#otherInformation").fadeOut("normal", function () {
                $j("#bookingOnBehalf").fadeIn("normal");
                //$j("#otherInformation").fadeIn("normal");
                //$j("#termsFieldset").fadeIn("normal");
            //});
        } else {
            //$j("#otherInformation").fadeOut("normal");
            //$j("#termsFieldset").fadeOut("normal");
            $j("#bookingOnBehalf").fadeOut("normal", function () {
                //$j("#otherInformation").fadeIn("normal");
               // $j("#termsFieldset").fadeIn("normal");
            });
        }
    });
    $j("#agencyBookingOwnAccount").click(function () {
        if ($j("#agencyBookingOwnAccount").attr("checked")) {
            $j("#agencyBookingDisclosedAgent").attr("checked", "");
            //$j("#termsFieldset").fadeOut("normal");
            //$j("#otherInformation").fadeOut("normal", function () {
                $j("#bookingOnBehalf").fadeIn("normal");
               // $j("#otherInformation").fadeIn("normal");
                //$j("#termsFieldset").fadeIn("normal");
            //});
        } else {
            //$j("#otherInformation").fadeOut("normal");
            //$j("#termsFieldset").fadeOut("normal");
            $j("#bookingOnBehalf").fadeOut("normal", function () {
                //$j("#otherInformation").fadeIn("normal");
               // $j("#termsFieldset").fadeIn("normal");
            });
        }
    });
});
