$(document).ready(function() {
    $(":input:visible:enabled:first").focus();

    var otherCheckBox = $("#paid_by_other");
    var otherField    = $("#paid_by_other_field");

    function visibility(state, el) {
        switch (state) {
            case "show":
                el.removeAttr("disabled");
                el.css("visibility", "visible");
                break;
                
            case "hide":
                el.attr("disabled", "disabled");
                el.css("visibility", "hidden");
                break;
        }
    }
    
    visibility(otherCheckBox.attr("checked") ? "show" : "hide", otherField);

    otherCheckBox.click(function() {
        if (this.checked) {
            visibility("show", otherField);
            otherField.focus();
        } else {
            visibility("hide", otherField);
        }
    });
    
    jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
        phone_number = phone_number.replace(/\s+/g, ""); 
        return this.optional(element) || phone_number.match(/^\([2-9]\d{2}\)\s?[2-9]\d{2}-?\d{4}$/);
    }, "Please specify a valid phone number");
    
    jQuery.validator.addMethod("state", function(state, element) {
        return this.optional(element) || state.match(/^[a-z]{2}$/i);
    }, "Please specify a valid state");

    jQuery.validator.addMethod("dob", function(dob, element) {
        return this.optional(element) || dob.match(/^\d{2}\/\d{2}\/\d{4}$/);
    }, "Please specify a valid state");

    jQuery.validator.addMethod("ssn", function(ssn, element) {
        return this.optional(element) || ssn.match(/^\d{3}-\d{2}-\d{4}$/);
    }, "Please specify a valid state");

    $("#rentapp").validate({ 
        rules: { 
            property_address: "required",
            first_name: "required",
            last_name: "required",
            address: "required",
            city: "required",
            state: {
                required: true,
                state: true
            },
            zip: {
                required: true,
                digits: true,
                min: 5
            },
            mobile_phone: {
                required: true,
                phoneUS: true
            },
            email: {
                required: true,
                email: true
            },
            dob: {
                required: true,
                dob: true
            },
            ssn: {
                required: true,
                ssn: true
            }
        },
        errorPlacement: function(error, element) {}
    });

    $("#mobile_phone").mask("(999) 999-9999");
    $("#home_phone").mask("(999) 999-9999");
    $("#state").mask("aa");
    $("#zip").mask("99999");
    $("#dob").mask("99/99/9999");
    $("#ssn").mask("999-99-9999");
    $("#drivers_license_state").mask("aa");
    $("#current_landlord_phone").mask("(999) 999-9999");
    $("#employer_phone").mask("(999) 999-9999");
    $("#parents_state").mask("aa");
    $("#parents_zip").mask("99999");
    $("#parents_phone").mask("(999) 999-9999");
});
