﻿var MAX_DUMP_DEPTH = 10; function dumpObj(obj, name, indent, depth) { if (depth > MAX_DUMP_DEPTH) { return indent + name + ": \x3CMaximum Depth Reached\x3E\x0A"; }; if (typeof obj == "object") { var child = null; var output = indent + name + "\x0A"; indent += "	"; for (var item in obj) { try { child = obj[item]; } catch (e) { child = "\x3CUnable to Evaluate\x3E"; }; if (typeof child == "object") { output += dumpObj(child, item, indent, depth + 1); } else { output += indent + item + ": " + child + "\x0A"; }; }; return output; } else { return obj; }; };

function CertifyBBB() {
    popupWin = window.open("http://www.la.bbb.org/Business-Report/RazorGator-Inc-13160628", 'Participant', 'location,scrollbars,width=450,height=300')
    window.top.name = 'opener';
    popupWin && popupWin.focus();
}
function CertifyVerisign() {
    return false;
    popupWin = window.open("https://seal.verisign.com/splash?form_file=fdf/splash.fdf&type=GOLD&sealid=1&dn=www.tickco.com&lang=en", 'Participant', 'location,scrollbars,width=450,height=300')
    window.top.name = 'opener';
    popupWin && popupWin.focus();
}

$(document).ready(function() {

  
    // bind to the submit button
    $('#submit-order').click(function(e) {
        e.preventDefault();
        cart_page.begin_submit();
    });

    $('input[title!=""]').hint();

    // for logged in users, get the client details to show
    // todo

    $('#same-as-billing').click(function(e) {
        cart_page.copy_address();
        $('#shipping-address').slideUp(500);
        $('#shipping-right-show').fadeOut(500);
    });
    $('#different-address').click(function(e) {
        $('#shipping-right-show').fadeIn(500);
        $('#shipping-left').fadeOut(500, function(e) {
            $('#shipping-right').fadeIn(500);
        });
    });
    $('#shipping-right-back').click(function(e) {
        e.preventDefault();
        $('#shipping-right').fadeOut(500, function(e) {
            $('#shipping-left').fadeIn(500);
        cart_page.is_empty_different_address();
        });
    });
    $('#shipping-right-show').click(function(e) {
        e.preventDefault();
        $('#shipping-left').fadeOut(500, function(e) {
            $('#shipping-right').fadeIn(500);
        });
    });
    
    
    //
   $('#digitaloption-digital').click(function(e) {
        $('#shipping-only-physical').fadeOut(500);
        document.getElementById('comments').rows=3;
        cart_page.set_shipping_digital(cart_page.get_cart_totals);
    });
    $('#digitaloption-physical').click(function(e) {
       cart_page.change_to_physical();
    });


    // load the cart totals
    cart_page.get_cart_totals();
    

    
    
    // get the countries and states
    cart_page.get_states(); 
    cart_page.get_countries();

    // add the years (this year + 8) to the expiration combo
    var year_options = '<option value="">Year</option>';
    for (var i = 0; i < 8; i++) {
        year_options +=
            '<option value="' +
            (parseInt(new Date().getFullYear()) + i) +
            '">' +
            (parseInt(new Date().getFullYear()) + i) +
            '</option>';
    };
    $('#card_expirationYear').html(year_options);

    // load the shipping methods
    cart_page.init_shipping();

    $('#shipping-method').change(function(e) {
        cart_page.hide_totals();
        cart_page.save_shipping(cart_page.get_cart_totals);
        //cart_page.show_totals();
    });

    $('#remove-coupon').click(function(e) {
        cart_page.remove_discount();
    });

    $('#apply-discount').click(function(e) {
        cart_page.apply_discount();
        //cart_page.get_cart_totals();
    });
    
    //On Change of Country set State
    $('#billTo_country').change(function(e) {
      if ($('#billTo_country').val() != "US" && $('#billTo_country').val() != 'CA')
      {
         $('#billTo_state').val('Non-US');
      }
      //Change Shipping Options 
      cart_page.ship_international();
        
    });
    
    //On Change of State set Country(Billing)
    $('#billTo_state').change(function(e) {
      if ($('#billTo_state').val() != "Non-US")
      {
        $('#billTo_country').val('US');
        //Change Shipping Options 
        cart_page.ship_international();
      }
    });
    
    //On Change of Country set State (Shipping)
    $('#shipTo_country').change(function(e) {
      if ($('#shipTo_country').val() != "US"  && $('#shipTo_country').val() != 'CA')
      {
         $('#shipTo_state').val('Non-US');
      }
    });
    
    //On Change of State set Country
    $('#shipTo_state').change(function(e) {
      if ($('#shipTo_state').val() != "Non-US")
      {
         $('#shipTo_country').val('US');
      }
    });


    //cart_page.show_discount();
});

var cart_page = {

    _loadingForQty: -1,
        
    ///the ajax service the cart uses, return a proxy
    get_ajax_proxy: function() {
        var serviceUrl = "/services/cart_ajax_service.asmx/";
        return new ServiceProxy(serviceUrl);
    },

    ///get the last errors and show them
    show_order_errors: function() {
        cart_page.get_ajax_proxy().invoke(
            'GetOrderErrors',
            {},
            function(errors) {
                var error_message = 'Please fix the following error(s) and try again:<br /><ul>';
                $.each(errors, function(key, value) {
                    error_message += '<li>' + value + '</li>';
                });
                error_message += '</ul>';
                cart_page.show_error(error_message);
            },
            function(error) {
                cart_page.show_error(error.error_message);
            }
        );
    },
    
    ///sets shipping methos for non US
    ship_international: function() {
       cart_page.get_ajax_proxy().invoke(
            'SetInternationalShipping',
            {Country: $('#billTo_country').val()},
            function(return_value) {
              cart_page.get_cart_totals();
            },
            function(error) {
                cart_page.show_error(error.error_message);
            }
    );
    },

    ///show a single error message
    show_error: function(error_message) {
        if (error_message == '0') 
            return;
            
        $('#cart-error').html(error_message).fadeIn(500);
    },

    ///erase any error
    clear_error: function() {
        $('#cart-error').html('').fadeOut(500);
    },

    ///start the process to submit the order
    begin_submit: function() {
        $('#cart-loading-modal').modal({
            close: false,
            overlayId: 'cart-loading-overlay'
        });

        cart_page.clear_error();
        var error = cart_page.validate_local();
        if (error && error.length) {
            cart_page.show_error(error);
            $.modal.close();
            return;
        }

        cart_page.save_cart(function() {
            cart_page.validate_remote(function(action_url, payment_signature, amount) {
                $('#checkoutForm').action = action_url;
                // set the signature fields
                $('#orderPage_timestamp').val(payment_signature.Timestamp);
                $('#merchantID').val(payment_signature.MerchantID);
                $('#orderPage_transactionType').val(payment_signature.TransactionType);
                $('#orderPage_signaturePublic').val(payment_signature.SignaturePublic);
                $('#orderPage_version').val(payment_signature.Version);
                $('#orderPage_serialNumber').val(payment_signature.SerialNumber);
                $('#amount').val(amount);
                $('#recurringSubscriptionInfo_amount').val(payment_signature.SubscriptionAmount);
                $('#recurringSubscriptionInfo_numberOfPayments').val(payment_signature.RecurringNumberOfPayments);
                $('#recurringSubscriptionInfo_frequency').val(payment_signature.SubscriptionFrequency);
                $('#recurringSubscriptionInfo_automaticRenew').val(payment_signature.SubscriptionAutoRenew);
                $('#recurringSubscriptionInfo_startDate').val(payment_signature.SubscriptionStartDate);
                $('#recurringSubscriptionInfo_signaturePublic').val(payment_signature.SubscriptionSignaturePublic);

                
                // submit the form
                $('#checkoutForm').submit();
            },
            function() {
                $.modal.close();
            });
        });
    },

    save_billing_address: function(success_callback) {
        cart_page.get_ajax_proxy().invoke(
            'SetCartAddress',
            { addressType: 'billing',
                firstName: $('#billTo_firstName').val(),
                lastName: $('#billTo_lastName').val(),
                company: $('#billTo_company').val(),
                street: $('#billTo_street1').val(),
                city: $('#billTo_city').val(),
                state: $('#billTo_state').val(),
                postal: $('#billTo_postalCode').val(),
                country: $('#billTo_country').val()
            },
            function(result) {
                if (result)
                    success_callback();
                else
                    cart_page.show_error('Error saving billing address.');
            },
            function(error) {
                cart_page.show_error(error.message);
            }
        );
    },

    copy_address: function() {
        $('#shipTo_firstName').val($('#billTo_firstName').val());
        $('#shipTo_lastName').val($('#billTo_lastName').val());
        $('#shipTo_company').val($('#billTo_company').val());
        $('#shipTo_street1').val($('#billTo_street1').val());
        $('#shipTo_city').val($('#billTo_city').val());
        $('#shipTo_state').val($('#billTo_state').val());
        $('#shipTo_postalCode').val($('#billTo_postalCode').val());
        $('#shipTo_country').val($('#billTo_country').val());
    },
    
    save_shipping_address: function(success_callback) {
        if ($('#same-as-billing').attr('checked') == true) {
            cart_page.copy_address();
            cart_page.get_ajax_proxy().invoke(
            'SetCartAddress',
            { addressType: 'shipping',
                firstName: $('#billTo_firstName').val(),
                lastName: $('#billTo_lastName').val(),
                company: $('#billTo_company').val(),
                street: $('#billTo_street1').val(),
                city: $('#billTo_city').val(),
                state: $('#billTo_state').val(),
                postal: $('#billTo_postalCode').val(),
                country: $('#billTo_country').val()
            },
            function(result) {
                if (result)
                    success_callback();
                else
                    cart_page.show_error('Error saving shipping address.');
            },
            function(error) {
                cart_page.show_error(error.message);
            }
            );
        }
        else
            cart_page.get_ajax_proxy().invoke(
            'SetCartAddress',
            { addressType: 'shipping',
                firstName: $('#shipTo_firstName').val(),
                lastName: $('#shipTo_lastName').val(),
                company: $('#shipTo_company').val(),
                street: $('#shipTo_street1').val(),
                city: $('#shipTo_city').val(),
                state: $('#shipTo_state').val(),
                postal: $('#shipTo_postalCode').val(),
                country: $('#shipTo_country').val()
            },
            function(result) {
                if (result)
                    success_callback();
                else
                    cart_page.show_error('Error saving shipping address.');
            },
            function(error) {
                cart_page.show_error(error.message);
            }
        );
    },

    save_shipping: function(success_callback) {
        cart_page.get_ajax_proxy().invoke(
            'SetCartShipping', {
                shippingMethod: $('#shipping-method :selected').val(),
                notes: $('#comments').val()
            },
        function(result) {
            if (result)
            {
                success_callback();
            }
            else
                cart_page.show_error('Error saving shipping information.');
        },
            function(error) {
                cart_page.show_error(error.message);
            }
        );
    },
    
    change_to_physical: function() {
        $('#shipping-only-physical').fadeIn(0);
        document.getElementById('comments').rows=1;
        cart_page.hide_totals();
        cart_page.get_ajax_proxy().invoke(
            'SetCartPhysicalSelected', 
            { },
        function(result) {
            cart_page.show_shippingmethods(result);
            cart_page.save_shipping(cart_page.get_cart_totals);
            if ($('#billTo_country').val()!= "US")
                cart_page.ship_international();
        },
        function(error) {
            cart_page.show_error(error.message);
        });

    },
    
    set_shipping_digital: function(success_callback) {
        cart_page.get_ajax_proxy().invoke(
            'SetCartShipping', {
                shippingMethod: '38',
                notes: $('#comments').val()
            },
        function(result) {
            if (result)
                success_callback();
            else
                cart_page.show_error('Error saving shipping information.');
        },
            function(error) {
                cart_page.show_error(error.message);
            }
        );
    },

    save_contact: function(success_callback) {
        cart_page.get_ajax_proxy().invoke(
        'SetCartContact', {
            dayPhone: $('#day-phone').val(),
            eveningPhone: $('#evening-phone').val(),
            fax: $('#fax').val(),
            email: $('#email').val(),
            receiveNewsletter: $('#newsletter').attr('checked'),
            upgradeMe: $('#upgrade-me').attr('checked')
        },
        function(result) {
            if (result)
                success_callback();
            else
                cart_page.show_error('Error saving contact information.');
        },
            function(error) {
                cart_page.show_error(error.message);
            }
        );
    },
    
    update_quantities: function(success_callback) {
        cart_page.get_ajax_proxy().invoke(
        'UpdateItemQuantities', {},
        function(result) {
            if (result)
                success_callback();
            else
                cart_page.show_error('Error updating item quantities.');
        },
            function(error) {
                cart_page.show_error(error.message);
            }
        );
    },

    ///make the ajax calls to save this cart in the database
    save_cart: function(success_callback) {
        cart_page.save_billing_address(function() {
            cart_page.save_shipping_address(function() {
                cart_page.save_shipping(function() {
                    cart_page.save_contact(function() {
                        cart_page.update_quantities(function() {
                            success_callback();
                         });
                    });
                });
            });
        });
    },

    append_error: function(destination, error_message) {
        destination = destination + '<li>' + error_message + '</li>';
        return destination;
    },

    ///validate locally, just ensure valid-looking data is present
    validate_local: function() {
        var validation_error = '';
        if ($('#billTo_firstName').val().length == 0)
            validation_error = cart_page.append_error(validation_error, 'First Name is Required');
        if ($('#billTo_lastName').val().length == 0)
            validation_error = cart_page.append_error(validation_error, 'Last Name is Required');
        if ($('#billTo_street1').val().length == 0)
            validation_error = cart_page.append_error(validation_error, 'Address is Required');
        if ($('#billTo_city').val().length == 0)
            validation_error = cart_page.append_error(validation_error, 'City is Required');
        if ($('#billTo_state').val() == '')
            validation_error = cart_page.append_error(validation_error, 'State is Required');
        if ($('#billTo_postalCode').val().length == 0)
            validation_error = cart_page.append_error(validation_error, 'ZIP is Required');
        if ($('#day-phone').val().length == 0)
            validation_error = cart_page.append_error(validation_error, 'Day Phone is Required');
        if ($('#email').val().length == 0)
            validation_error = cart_page.append_error(validation_error, 'Email is Required');
        else
        {
            if ($('#email').val().indexOf('@') == -1)
                validation_error = cart_page.append_error(validation_error, 'Valid Email is Required');
            else if ($('#email').val().indexOf('.') == -1)
                validation_error = cart_page.append_error(validation_error, 'Valid Email is Required');
        }
        
        if ($('#card_accountNumber').val().length == 0)
            validation_error = cart_page.append_error(validation_error, 'Credit Card # is Required');
        if ($('#card_expirationMonth').val() == '')
            validation_error = cart_page.append_error(validation_error, 'Expiration Month is Required');
        if ($('#card_expirationYear').val() == '')
            validation_error = cart_page.append_error(validation_error, 'Expiration Year is Required');
        if ($('#card_cardType').val() == '')
            validation_error = cart_page.append_error(validation_error, 'Credit Card Type is Required');
        if (!$('#agree-t-and-c').attr('checked'))
            validation_error = cart_page.append_error(validation_error, 'You must agree to the TickCo Terms and Conditions');

        var billCountry = $('#billTo_country').val();
        var shipCountry = $('#shipTo_country').val();
        if (billCountry == 'US' || billCountry == 'CA') {
            var billState = $('#billTo_state').val();
            if (billCountry == 'US' && billState == 'Non-US') {
                validation_error = cart_page.append_error(validation_error, 'Valid State (Billing) is Required');
            } else if (billCountry == 'CA' && billState == 'Non-US') {
                validation_error = cart_page.append_error(validation_error, 'Valid Province (Billing) is Required');
            }   
        }
        if ($('#different-address').attr('checked')) {
            if (shipCountry == 'US' || shipCountry == 'CA') {
                var shipState = $('#shipTo_state').val();
                if (shipCountry == 'US' && shipState == 'Non-US') {
                    validation_error = cart_page.append_error(validation_error, 'Valid State (Shipping) is Required');
                } else if (shipCountry == 'CA' && shipState == 'Non-US') {
                    validation_error = cart_page.append_error(validation_error, 'Valid Province (Shipping) is Required');
                }       
            }
        }
        
        if (validation_error.length > 0)
            validation_error = 'One or more of the fields needs to be updated before we can accept your submission:<ul>' + validation_error + '</ul>';

        return validation_error;
    },

    ///validate at the service layer, this will invoke an ajax call and callback when done
    validate_remote: function(success_callback, error_callback) {
        cart_page.get_ajax_proxy().invoke(
            'ValidateCart',
            {},
            function(validate_result) {
                if (validate_result.IsValid) {
                    success_callback(
                        validate_result.RedirectURL,
                        validate_result.TransactionSignature,
                        validate_result.Amount);
                    return;
                }

                error_callback();
                cart_page.show_error(validate_result.ValidationMessage);
            },
            function(error) {
                error_callback();
                cart_page.show_error(error.message);
            }
        );
    },

    get_states: function() {
        cart_page.get_ajax_proxy().invoke(
            'GetStates',
            {},
            function(states) { 
                var state_options = '';
                $.each(states, function(key, value) {
                    state_options += '<option value="' +
                        value.substring(0, value.indexOf('|')) +
                        '">' +
                        value.substring(value.indexOf('|') + 1) +
                        '</option>';
                });
                $('#billTo_state').html(state_options);
                $('#shipTo_state').html(state_options);
            },
            function(error) {
                cart_page.show_error(error.message);
            }
        );
    },

    get_countries: function() {
        cart_page.get_ajax_proxy().invoke(
            'GetCountries',
            {},
            function(countries) {
                var country_options = '';
                $.each(countries, function(key, value) {
                    country_options += '<option value="' + value.substring(0, value.indexOf('|')) + '"';
                    if (value.substring(0, value.indexOf('|')) == 'US')
                        country_options += ' selected';
                    country_options += '>' + value.substring(value.indexOf('|') + 1) + '</option>';
                });
                $('#billTo_country').html(country_options);
                $('#shipTo_country').html(country_options);
            },
            function(error) {
                cart_page.show_error(error.message);
            }
        );
    },

    get_cart_totals: function() {
     var isDigital=false;
     if(document.getElementById('digitaloption-digital')!=null)
        isDigital=document.getElementById('digitaloption-digital').checked;
        cart_page.get_ajax_proxy().invoke(
            'GetCartTotals',
            {
            IsDigital:isDigital
            },
            function(cart_totals) {
                if (cart_totals.LineItemCount != 0) {
                    $('#divCartWithItems').show();
                    $('#cart-table').show();
                    $('#divCartNoItems').hide();
              
                    $('#loading-totals').fadeOut(500, function() {
                        $('#cart-totals').fadeIn(0);
                        $('#cart-total').fadeIn(0);
                        $('#subtotal').html(cart_totals.Subtotal).fadeIn(500);
                        $('#grand-total').fadeIn(500).html('<strong>' + cart_totals.GrandTotal + '</strong>');
                        $('#processing-fee').html(cart_totals.ProcessingFee).fadeIn(500);
                        $('#shipping-cost').html(cart_totals.ShippingMethod.Fee).fadeIn(500);
                        $('#shipping-name').html(cart_totals.ShippingMethod.Description.toUpperCase()).fadeIn(500);
                        $('#amount').val(cart_totals.GrandTotalNumeric);

                        // if theres a promotion then show it
                        if (cart_totals.Promotion != null)
                            cart_page.show_discount(cart_totals.Promotion);
                 

                        //Reload Shipping
                        cart_page.reload_shipping();      
                    });
                }
                else {
                    $('#divCartNoItems').show();
                    $('#divCartWithItems').hide();
                    
                }
              
            },
            function(error) {
                cart_page.show_error('Error getting the totals, the error was: ' + error.message);
            }
        );
    },
    
    
    is_empty_different_address: function(){
    
        if ($('#shipTo_firstName').val().length == 0 && $('#shipTo_lastName').val().length == 0 && $('#shipTo_street1').val().length == 0 && $('#shipTo_city').val().length == 0 && $('#shipTo_state').val() == '' && $('#shipTo_postalCode').val().length == 0  )
            $('#same-as-billing').click();    
    
    },
    
    ///init shipping, check if it's mixed inventory, etc
    reload_shipping: function() {
        cart_page.get_ajax_proxy().invoke(
            'GetCartShippingDetails',
            { },
            function(shipping_details) {
                cart_page.show_shippingmethods(shipping_details.AvailableShippingMethods);
            },
            function(error) {
                cart_page.show_error(error.message);
            }
        );
    },

    ///init shipping, check if it's mixed inventory, etc
    init_shipping: function() {
        cart_page.get_ajax_proxy().invoke(
            'GetCartShippingDetails',
            { },
            function(shipping_details) {
                cart_page.show_shippingmethods(shipping_details.AvailableShippingMethods);
                if (!shipping_details.ContainsMixed) {
                    if (shipping_details.ContainsOnlyDigital) {
                        $('#shipping-only-digital').fadeIn(0);
                        $('#shipping-only-physical').fadeOut(500);
                    } else {
                        $('#shipping-only-physical').fadeIn(0);
                        $('#shipping-instructions').fadeIn(0);
                        $('#shipping-method').fadeIn(0);
                        
                    }
                } else {
                    // show the digital physical page
                    //window.location.href = '/cart/addtocart.aspx';
                }
            },
            function(error) {
                cart_page.show_error(error.message);
            }
        );
    },
    
    ///the shipping methods may have changed for this cart
    get_shippingmethods: function() {
        cart_page.get_ajax_proxy().invoke(
            'GetShippingMethods',
            {},
            function(shipping_methods) {
                cart_page.show_shippingmethods(shipping_methods);
            },
            function(error) {
                cart_page.show_error(error.message);
            }
        );
    },
    
    ///given an array of shipping methods, make them in the UI
    show_shippingmethods: function(shipping_methods) {
        var shipping_options = '';
        $.each(shipping_methods, function(key, value) {
            shipping_options += '<option value="' + value.ShippingMethod.ShippingMethodId + '"';
            if (value.Selected == true)
                shipping_options += ' selected';
            shipping_options += '>';
            shipping_options += value.OverriddenDisplayName;
            shipping_options += '</option>';
        });
        $('#shipping-method').html(shipping_options).removeAttr('disabled');    
    },
    
    ///show the totals in the ui
    show_totals: function(cart_totals) {
        var fee = '';
        if (cart_totals.ShippingMethod == null)
            fee = 0;
        else
            fee = cart_totals.ShippingMethod.Fee;
        $('#loading-totals').fadeOut(500, function() {
            $('#cart-total').fadeIn(500);
            $('#cart-totals').fadeIn(500);
            $('#subtotal').html(cart_totals.Subtotal);
            $('#grand-total').html('<strong>' + cart_totals.GrandTotal + '</strong>');
            $('#processing-fee').html(cart_totals.ProcessingFee);
            $('#shipping-cost').html(fee);
            $('#amount').val(cart_totals.GrandTotalNumeric);
        });
    },

    ///show the loading instead of the cart totals while we recalculate
    hide_totals: function() {
        $('#cart-total').fadeOut(500);
        $('#cart-totals').fadeOut(500, function() {
            $('#loading-totals').fadeIn(500);
        });

        $('#amount').val('0');
    },

    ///remove an item from the cart
    remove_item: function(item_id,removedCartProduct) {
        //cart_page.hide_totals();
        cart_page.get_ajax_proxy().invoke(
            'RemoveCartItem',
            { cartItemId: item_id },
            function(result) {
                // remove this item from the table now
                $('#cartitem_' + item_id).fadeOut(500, function() { $('#cartitem_' + item_id).remove() });
                 cart_page.SetOmnitureValues(removedCartProduct);
                if (result.LineItemCount == 0) {
                    $('#divCartNoItems').show();
                    $('#divCartWithItems').hide();
                    //cart_page.hide_totals();
                    cart_page.remove_discount();
                }
                else {
                    cart_page.show_totals(result);
                }
            },
            function(error) {
                cart_page.show_error(error.message);
            }
        );
    },

    ///change an item's quantity (look at the DOM to get the new qty)
    change_quantity: function(item_id) {
        $('#extendedprice_' + item_id).html('');
        //cart_page.hide_totals();
        var new_quantity = $('#quantity_' + item_id).val();
        $('#quantity_' + item_id).attr('disabled', 'disabled');
        cart_page.get_ajax_proxy().invoke(
            'ChangeItemQuantity',
            { cartItemId: item_id,
                newQuantity: new_quantity
            },
            function(result) {
                $('#extendedprice_' + item_id).html(result.ItemTotal);
                $('#quantity_' + item_id).removeAttr('disabled');
                cart_page.show_totals(result.CartTotals);
            },
            function(error) {
                cart_page.show_error(error.message);
            }
        );
    },

    // apply the discount to the cart
    apply_discount: function() {
        cart_page.get_ajax_proxy().invoke(
            'ApplyDiscount',
            { promotionCode: $('#discount-code').val() },
            function(discount) {
                //Clear Errors
                cart_page.clear_error();
                if (discount != null) 
                {
                    cart_page.show_discount(discount);
                    cart_page.save_discount(discount.Code);
                }
                else
                    cart_page.show_error('<span style="font-weight:bolder;font-size:13px;">Coupon not applied:</span><br/>Sorry, we cannot accept this coupon at this time.');
            },

            function(error) {
                cart_page.show_error(error.message);
            }

        );
    },
    
    remove_discount: function() {
        cart_page.get_ajax_proxy().invoke(
            'RemoveDiscount',
            {},
            function(totals) {
                if (totals.Promotion == null) {
                    $('#coupon-row').slideToggle(500);
                    $('#coupon-info').fadeOut(500);
                    cart_page.show_totals(totals);
                }
            });
    },

    show_discount: function(discount) {
        $('#coupon-row').fadeIn(500);
        $('#promotion-title').html(discount.Title).fadeIn(500);
        $('#promotion-amount').html('-' + discount.Amount).fadeIn(500);
        $('#coupon-info').fadeIn(500);
        $('#coupon').html('-' + discount.Amount).fadeIn(500);

    },

    save_discount: function(code) {
        cart_page.get_ajax_proxy().invoke(
            'SetCartPromotionCode',
            { promotionCode: code },
            function(totals) {
                cart_page.show_totals(totals);
            });
    },
    SetOmnitureValues:function(strProduct)
	{
         try
        {
            var s = s_gi('razorgtickcoprod'); 
            var evetntName='scRemove';
            s.linkTrackVars='events';
            s.linkTrackEvents="scRemove";
            s.prop3=evetntName;
            s.events="scRemove";
            s.products= ";"+strProduct;
            s.tl(this,'o',"CartItem Removed -"+strProduct); 
         }
        catch(e)
        {
            alert('Error : '+ e.description);
        }
}    
}

