
// When the DOM is ready...
$(function() {

    // Hide stuff with the JavaScript. If JS is disabled, the form will still be useable.
    // NOTE:
    // Sometimes using the .hide(); function isn't as ideal as it uses display: none; 
    // which has problems with some screen readers. Applying a CSS class to kick it off the
    // screen is usually prefered, but since we will be UNhiding these as well, this works.

    $("#address_wrap").hide();
    $("#reqs_wrap").hide();
    // Reset form elements back to default values
    $("#submit").hide();
    $("#step_2 input[type=radio]").each(function() {
        this.checked = false;
    });
    $("#step_3 input[type=radio]").each(function() {
        this.checked = false;
    });


    // Fade out steps 2 and 3 until ready
    $("#step_2").css({ opacity: 0.3 });
    $("#step_3").css({ opacity: 0.3 });

    $.stepTwo = false;
    $.stepThree = false;

    $(".input1").blur(function() {

        var all_complete = true;

        $(".input1").each(function() {
            if ($(this).val() == '') {
                all_complete = false;
            };
        });

        if (all_complete) {
            $("#step_1")
			.animate({
			    paddingBottom: "120px"
			})
			.css({
			    "background-image": "url(/images/check.png)",
			    "background-position": "bottom center",
			    "background-repeat": "no-repeat",
			    "background-color": "#72DC72",
			    "border-top": "5px solid #222222"
			});
            $("#step_2").css({
                opacity: 1.0
            });
            $("#step_2 legend").css({
                opacity: 1.0 // For dumb Internet Explorer
            });
        };
    });

    $(".input2").blur(function() {
        stepTwoTest();
    });

    $("#step_2 input[name=address_group]").click(function() {   
        $.stepTwo = true;
        if ($("#address_toggle_on:checked").val() == 'on') {
            $("#address_wrap").slideDown();

        } else {
            $("#address_wrap").slideUp();
        };
        stepTwoTest();
    });

    function stepTwoTest() {
        if ($.stepTwo) {
            var all_complete = true;

            $(".input2").each(function() {
                if ($(this).val() == '') {
                    all_complete = false;
                };
            });

            if (all_complete) {
                $("#step_2")
			.animate({
			    paddingBottom: "120px"
			})
			.css({
			    "background-image": "url(/images/check.png)",
			    "background-position": "bottom center",
			    "background-repeat": "no-repeat",
			    "background-color": "#72DC72",
			    "border-top": "5px solid #222222"
			});
                $("#step_3").css({
                    opacity: 1.0
                });
                $("#step_3 legend").css({
                    opacity: 1.0 // For dumb Internet Explorer
                });
            };
        }
    };

    $("#step_3 input[name=reqs_group]").click(function() {
        $.stepThree = true;
        if ($("#radReqsYes:checked").val() == 'on') {
            $("#reqs_wrap").slideDown();
        } else {
            $("#reqs_wrap").slideUp();
        };
        stepThreeTest();
    });

    function stepThreeTest() {
        if ($.stepThree) {

            $("#step_3")
			.animate({
			    paddingBottom: "120px"
			})
			.css({
			    "background-image": "url(/images/check.png)",
			    "background-position": "bottom center",
			    "background-repeat": "no-repeat",
			    "background-color": "#72DC72",
			    "border-top": "5px solid #222222"
			});
            $("#step_3").css({
                opacity: 1.0
            });
            $("#step_3 legend").css({
                opacity: 1.0 // For dumb Internet Explorer
            });

            $("#submit").fadeIn("slow");
        }
    };

});