Subversion Repositories SmartDukaan

Rev

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

var totalAmount = 0.00;

$(document).ready(function(){

        checkIfUserHasAddress();
        if(window.location.pathname === "/cart") {
                changeEstimate();
        }
        $('#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(){
                if(($('#store-addresses').find('.default-address').attr('zone') !=  $('#zone-selector').val()) && ($("#tabSelector").val() == "HotSpot")) {
                        alert("Please select one store address");
                        return false;
                }
                var canProceedToPay = parseInt($('#canProceedToPay').val());
                
                if (canProceedToPay == 1)       {
                        
                        trackProceedToPay();
                        $('#formProceedToPay').submit();
                } else  {
                        var reasonActionDisability = $('#reasonActionDisability').val();
                        
                        if(reasonActionDisability.indexOf('Location not serviceable') >= 0)     {
                                trackEventWithGA('Cart', 'Location not serviceable', $('#selectedPincode').val());
                        }
                        else if (($("#tabSelector").val() == "HotSpot")) {
                                reasonActionDisability = 'Please enter an address which will be printed on the bill';
                        } else {
                                reasonActionDisability = 'Please specify a delivery address';
                        }
                        alert(reasonActionDisability);
                }
        });
        
        $('#viewOrders').click(function(){
                window.location.href = "/myaccount";
        });

        $('#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();
        });
                
        function checkIfUserHasAddress(){
                var addressEmpty = parseInt($('#addressEmpty').val());
                
                if (addressEmpty == 1)  {
                        showAddAddressForm();
                }
        }
        
        function showAddAddressForm(){
                //$('#main-right-container').hide();
                $('#shipping-address-div').hide();
                $('#billing-address-div').hide();
                $('#frmShippingAddress').show();
        }
        
        function showAddressList()      {
                $('#frmShippingAddress').hide();
                //$('#main-right-container').show();
                if($("#tabSelector").val() == "HotSpot") {
                        $('#billing-address-div').show();
                } else {
                        $('#shipping-address-div').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(){
                                $("#img" + "_" + item_id).html("<img src='/images/loader_l.gif'>").show();
                                $("#block" + "_" + item_id).hide();
                                $("#serv_" + item_id).hide();
                        },
                        success: function(data){
                                $("#img" + "_" + item_id).html("<img src='/images/loader_l.gif'>").hide();
                                var response = eval('(' + data + ')');
                                var deliveryEstimate = parseInt(response['delivery_estimate']);
                                var otg= response['on_time_guarantee'];
                                
                                if(deliveryEstimate == -1)      {
                                        $("#serv_" + item_id).show();
                                        return;
                                        
                                } else if(deliveryEstimate == 1)        {
                                        $("#days_" + item_id).html('1 Business Day');
                                }
                                else    {
                                        $("#days_" + item_id).html(deliveryEstimate + ' Business Days');
                                }
                                if(otg == "true")       {
                                        $("#otg_" + item_id).show();
                                } else {
                                        $("#otg_" + item_id).hide();
                                }
                                $("#block" + "_" + item_id).show();
                        }
                });
        }
}