Subversion Repositories SmartDukaan

Rev

Rev 4453 | Rev 5040 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

var totalAmount = 0.00;

$(document).ready(function(){

        checkIfUserHasAddress();
        
        $('#checkout').click(function(){
                window.location.href = "/shipping";
        });
        
        $('#poplogin').click(function(){
                $.colorbox({
                width: "860px",
                height: "340px",
                iframe: false,
                href: "/login-mini",
                
                onComplete: function(){
                        trackEventWithGA('Cart', 'Login/Register Popup', '');
                }
        });
        });
        
        $('#proceedToPay').click(function(){
                var canProceedToPay = parseInt($('#canProceedToPay').val());
                
                if (canProceedToPay == 1)       {
                        
                        trackProceedToPay();
                        $('#formProceedToPay').submit();
                } else  {
                        
                        alert($('#reasonActionDisability').val());
                }
        });

        $('#cart .remove-quantitybttn').click(function(){
                var itemId = $(this).attr('id').split('_')[1];
                window.location.href = "/cart/"  + itemId + "?_method=delete" + "&productid=" + itemId;
        });
        
        $('#computeEstimate').click(function(){
                changeEstimate();
                $('#cartDetail').find('thead .dev-pincode').html($('#zipcode').val());
        });
        
        $('#applyCoupon').click(function(){
                $('#couponAction').val('applycoupon');
                $('#frmCouponCode').submit();
        });
        
        $('#removeCoupon').click(function(){
                $('#couponAction').val('removecoupon');
                $('#frmCouponCode').submit();
        });
        
        $('.cart-item-quantity').change(function(){
                var itemId = $(this).attr("id").split('_')[1];
                var quantity = parseInt($(this).val());
                
                if (quantity > 5)       {
                        alert("You can not order more than 5 pieces of same product.");
                        $(obj).focus();
                }
                else    {
                        jQuery.ajax({
                                type: "POST",
                                url: "/cart/" + itemId + "?_method=put&productid=" + itemId + "&quantity=" + quantity,
                                data: "productid=" + itemId + "&quantity=" + quantity,
                                success: function(msg){
                                        window.location.reload();
                                }
                        });
                }
        });
        
        $('#submitAddress').click(function(){
                $('#frmShippingAddress').submit();
        });
        
        $('#addAddress').live('click', function(){
                showAddAddressForm();
        });

        $('#closeAddAddressForm').live('click', function() {
                showAddressList();
        });
        
        $('#addresses .button-address-select').click(function(){
                var addressId = $(this).attr('id').split('_')[1];
                $('#formChangeAddressTo_' + addressId).submit();
        });
        
        $('#addresses .delete-address').click(function(){
                var addressId = $(this).attr('id').split('_')[1];
                
                jQuery.ajax({
                        type: "POST",
                        url: "/address",
                        data: "action=delete&addressid=" + addressId,
                        success: function(msg){
                                window.location.reload();
                        }
                });
        });
        
        function checkIfUserHasAddress()        {
                var addressEmpty = parseInt($('#addressEmpty').val());
                
                if (addressEmpty == 1)  {
                        showAddAddressForm();
                }
        }
        
        function showAddAddressForm(){
                $('#addresses').hide();
                $('#frmShippingAddress').show();
        }
        
        function showAddressList()      {
                $('#frmShippingAddress').hide();
                $('#addresses').show();
        }
});

function sumOfColumns(tableID, columnIndex, hasHeader)  {
        var tot = 0;
        var inc = 1;
        var tableElement = $("#" + tableID + " tr" + (hasHeader ? ":gt(0)" : ""));
        var tableChildren = tableElement.children("td:nth-child(" + columnIndex + ")");

        tableChildren.each(function() {
                var currentVal = document.getElementById('totalPrice' + inc).innerHTML;
                inc ++;
                var splitVal = currentVal.split("Rs.");
                var num = parseFloat(splitVal[1].replace(/[^\d\.-]/g,''));
                tot += parseFloat(num);
        });
        totalAmount = tot;
        var formated_value = $().number_format(tot, {
                numberOfDecimals:2,
                decimalSeparator: '.',
                thousandSeparator: ',',
                symbol: 'Rs.'
        });
        return formated_value;
}

function subtractionOfColumns(tableID, columnIndex, hasHeader) {
        var tot = 0;

        $("#" + tableID + " tr" + (hasHeader ? ":gt(0)" : "")).children("td:nth-child(" + columnIndex + ")").each(function(){
                var currentVal=$(this).html();
                var splitVal=currentVal.split("Rs.");
                var num = parseFloat(splitVal[1].replace(/[^\d\.-]/g,''));
                tot += parseInt(num);
        });

        var formated_value = $().number_format(tot, {
                numberOfDecimals: 2,
                decimalSeparator: '.',
                thousandSeparator: ',',
                symbol: 'Rs.'
        });
        return formated_value;
}

function changeEstimate(item_id)        {
        if(item_id == null )    {
                $("#cartDetail tbody tr").each(function(index, item){
                        var itemId = $(item).attr("id");
                        changeEstimate(itemId);
                });
                
        } else  {
                jQuery.ajax({
                        type: "GET",
                        url: "/estimate/" + $("#zipcode").val() + "_" + item_id,
                        beforeSend: function(){
                                $("#days" + "_" + item_id).html("<img src='/images/loader_l.gif'>");
                                $("#shipping_time_" + item_id).html('<img src="/images/loader_l.gif">');
                        },
                        success: function(data){
                                var response = eval('(' + data + ')');
                                var deliveryEstimate = parseInt(response['delivery_estimate']);
                                
                                if(deliveryEstimate == -1)      {
                                        $("#shipping_time_" + item_id).html('Location is not serviceable');
                                        
                                } else if(deliveryEstimate == 1)        {
                                        $("#shipping_time" + "_" + item_id).html('FREE DELIVERY in 1 Business Day');
                                }
                                else    {
                                        $("#shipping_time" + "_" + item_id).html('FREE DELIVERY in ' + deliveryEstimate + ' Business Days');
                                }
                        }
                });
        }
}