jQuery(document).ready(function() {
    jQuery("#datepicker").datepicker(  {dateFormat: 'yy-mm-dd'} );

    var validator = jQuery("#coupon_form").validate({
            rules: {
                coupon_code: "required",
                discount: {
                    required: true,
                    number: true
                },
                purchase_limit: {
                    number: true
                },
                datepicker: "required"
            },

            // the errorPlacement has to take the table layout into account
            errorPlacement: function(error, element) {
                if ( element.is(":radio") )
                    error.appendTo( element.parent().next().next() );
                else if ( element.is(":checkbox") )
                    error.appendTo ( element.next() );
                else
                    error.appendTo( element.parent().next() );
            },
            // set this class to error-labels to indicate valid fields
            success: function(label) {
                // set &nbsp; as text for IE
                label.html("&nbsp;").removeClass("error");
                //label.html("&nbsp;").addClass("checked");
            }
        });


    jQuery('#product_list', '#coupon_form').change(function(){
        if(jQuery(this).val() == 0){
            jQuery('#purchlimit').show();
            jQuery("#purchase_limit").rules("add","required");
        }
        else{
            jQuery('#purchase_limit').val('');
            jQuery('#purchlimit').hide();
            jQuery("#purchase_limit").rules("remove","required");
        }
    });
  });