$(document).ready(function() {
    $("#orderform").validate({
        invalidHandler: function(form, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {
                var message = errors == 1
                ? 'You missed 1 field. It has been highlighted'
                : 'You missed ' + errors + ' fields. They have been highlighted';
                alert(message);
            }
        }
    });
    $("#submit").attr('disabled', 'disabled');
    $("#step2, #step3, #step4").children().find('input, textarea, radio').attr('disabled', 'disabled');
    $("#step2, #step3, #step4").fadeTo(10, 0.5);
    $(".numboxes").change(function() {
        var numbox = $(this).children("option:selected").val();
        if(numbox <=5) {
            if(numbox > 1) var boxs = ' boxes.';
            else var boxs = ' box.';
            $('#statusbox').html('We recommend you have '+numbox+boxs);
        } else $('#statusbox').html($(".numboxes:selected").text());
    });
    $('.statusbox').hide();
    $(".numboxes, .whcbox, .delamount").click(function() {
        if($("#orderform").validate().element(".delamount") && $("#orderform").validate().element(".whcbox") && $("#orderform").validate().element(".numboxes") && $(".numboxes").children("option:selected").val() <= 5) {
            $("#step2").children().find('input, textarea, radio').removeAttr('disabled');
            $("#step2").fadeTo('slow', 1);
            $.post('../placeorder.php?rec=1', {
                'ajax': 'true',
                'whichbox': $("input[name=whichbox]:checked").val(),
                'numboxes': $(".numboxes").children("option:selected").val(),
                'delamount': $(".delamount:checked").val()
            }, function(data) {
                $('#statusbox').html(data);
                $('#statusbox').slideDown();
            }, 'html');
        }
    });
    $(".delbtn").click(function() {
        
        if($("#box_once").attr("checked")){
          max = 1;
        } else if($("#box_twice").attr("checked")) {
            max = 2;
        }

        if($(".delbtn:checked").size() > max) {
            return false;
        }
        if($(".delbtn:checked").size() == max) {
            gotoStepThree();
        }
    });
    function gotoStepThree() {
        $("#step3").children().find('input, textarea, radio').removeAttr('disabled');
        $("#step3").fadeTo('slow', 1);
        $("#submit").removeAttr('disabled');
    }
});