jQuery(document).ready(function() {

    jQuery("a.add_coupon").click(function(){
       var a = '';
        jQuery('#product_list input:checkbox:checked').each(function(i){
            a += jQuery(this).val() + ',';
        } );
        a = a.slice(0, a.length-1 );
        jQuery('#product_id').val(a);
    });

    jQuery('#product_list input:checkbox').click(function(){
        jQuery('.coupon_code_hidden').html('');
        jQuery('#all_coupon').html('');
        jQuery('#coupon_disc').html('');
    });

    jQuery("#empty_cart").click(function(){
        jQuery('#product_list input:checkbox:checked').each(function(i){
            jQuery(this).removeAttr('disabled').removeAttr('checked');
        } );
        total_price = 0;
        jQuery('#total_amount b').html('$0');
        jQuery('.coupon_code_hidden').html('');
        jQuery('#all_coupon').html('');
        jQuery('#coupon_disc').html('');
    });

    jQuery("a.add_coupon").fancybox({
        'scrolling'	: 'no',
        'titleShow': false
    });

    jQuery("#couponForm").bind("submit", function() {

        if (jQuery("#coupon_code").val().length < 1 ) {
            jQuery("#error").show();
            jQuery.fancybox.resize();
            return false;
        }

        jQuery.fancybox.showActivity();

        jQuery.ajax({
            type: "POST",
            url: wpsa_coupon_var.admin_ajax_php,
            data: jQuery(this).serializeArray(),
            success: function(msg){
                if(msg==0){
                    jQuery.fancybox('<p>Sorry, The coupon code entered is not valid.</p>');
                }
                else {
                    //0 = product_id
                    //1 = actual price
                    //2 = discount price
                    //3 = msg
                    //4 = coupon code

                    msg = jQuery.trim(msg);
                    t = msg.split('|');

                    //if coupon is all and purchaches limit is less
                    if(t.length == 1) {
                        jQuery.fancybox('This coupon is applicable only for purchase limit equal or above '+t[0]+' USD');
                    }
                    else{
                        //ctotal_price is global var defined in wpsa
                        ctotal_price = 0;
                        //var item_price;

                        jQuery('#coupon_disc').html(t[5]); 

                        if(t[0] > 0) {
                            jQuery('#item_'+t[0]+' span.coupon_code_hidden').html(t[4]);
                            jQuery('span#all_coupon').html('');
                            
                            //item_price = jQuery('#item_'+t[0]+' span.item_price').html();
                            ctotal_price = parseFloat(t[2]);
                        }
                        else{
                            jQuery('span#all_coupon').html(t[4]);
                            ctotal_price = parseFloat(t[2]);
                            jQuery('.coupon_code_hidden').html('');
                        }

                        var total = Math.round(ctotal_price*100)/100;
                        jQuery('#total_amount b').html('$'+total);

                        jQuery('#product_list input:checkbox:checked').each(function(i){
                            jQuery(this).attr('disabled', 'disabled');
                        });

                        
                        jQuery.fancybox(t[3]);
                    }
                }
                        
            }
        });

        return false;
    });

});