Subversion Repositories SmartDukaan

Rev

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

var grnhistory= grnhistory || {};
grnhistory.searchText = "";
grnhistory.searchType = "";
grnhistory.startTime = "0";
grnhistory.endTime = "0";

var saleHistory=saleHistory || {};
saleHistory.searchText="";
saleHistory.searchType="";
saleHistory.startTime="";
saleHistory.endTime="";

var walletHistory = walletHistory || {};
walletHistory.startTime="";
walletHistory.endTime="";

$(function() {
        $(".new_grn").live('click', function() {
                loadNewGrn("main-content");
        });
        
        $("#retailerDocumentForm").submit(function(event) {
                event.preventDefault();
                var emailIdOrMobileNumber = $('#retailer-details-search-text').val();
                var file = $("#retailerDocument").val();
                uploadRetailerDocument("retailer-details-container", file, emailIdOrMobileNumber);
        });
        
        
        $(".good_inventory").live('click', function() {
                loadGoodInventory("main-content","");
        });
        
        $(".catalog").live('click', function() {
                loadCatalog("main-content","");
        });
        
        $(".item_aging").live('click', function() {
                getInventoryItemAgingByInterval("main-content", "");
        });
        
        $("#item-aging-paginated .next").live('click', function() {
                var end = $( "#item-aging-paginated .end" ).text();
                getItemAgingNextPreviousItems(end, "");
                $("#item-aging-paginated .next").blur();
    });
        
        $("#item-aging-search-button").live('click', function() {
                searchContent = $("#item-aging-search-text").val();
                if (typeof (searchContent) == "undefined" || !searchContent){
                        searchContent = "";
                }
                getInventoryItemAgingByInterval("main-content", searchContent);
    });
        
        $("#retailer-details-search-button").live('click', function() {
                searchContent = $("#retailer-details-search-text").val();
                if (typeof (searchContent) == "undefined" || !searchContent){
                        searchContent = "";
                }
                getRetailerDetailsByEmailIdOrMobileNumber("retailer-details-container", searchContent);
    });
        
        $("#item-aging-search-text").live("keyup", function(e) {
                var keyCode = e.keyCode || e.which;
        if(keyCode == 13){
                $("#item-aging-search-button").click();
        }
        });
        
        $("#item-aging-paginated .previous").live('click', function() {
                var start = $( "#item-aging-paginated .start" ).text();
                getItemAgingNextPreviousItems(start - 11, "");
                $("#item-aging-paginated .previous").blur();
    });
        
        $(".download_aging_report").live('click', function() {
                downloadAgingReport();
        });
        
        $(".item-ledger-report-download-page").live('click', function() {
                loadItemLedgerReportDownloadPage("main-content");
        });
        
        /*$(".item-ledger-report-download").live('click', function() {
                var startEndDateTime = $('input[name="startEndDateTime"]').val().split("-");
                var startDateTime = $.trim(startEndDateTime[0]);
                var endDateTime = $.trim(startEndDateTime[1]);
                downloadItemLedgerReport(startDateTime, endDateTime);
        });*/
        
        $(".bad_inventory").live('click', function() {
                loadBadInventory("main-content","");
        });

        $(".grn_history").live('click', function() {
                loadGrnHistory("main-content","","","0","0");
        });
        
        $(".pending_grn").live('click', function() {
                loadPendingGrn("main-content", "", "", "0", "0");
        });
        
        $(".cart_icon_header").live('click', function() {
                loadCart("main-content");
        });
        
        $(".wallet_icon_header").live('click', function() {
                loadWallet("main-content", "", "");
        });
        
        $(".cart-checkout").live('click', function() {
                checkout("main-content");
        });
        
        $(".create-order").live('click', function() {
                checkout("main-content");
        });
        
        $(".sale-history").live('click', function() {
                console.log("Sale History Button Clicked...")
                saleHistory("main-content","","","0","0");
        });
        
        $(".schemes").live('click', function() {
                console.log("Active Schemes Button Clicked...")
                schemes("main-content");
        });
        
        $(".schemes-download-page").live('click', function() {
                console.log("Schemes Download Clicked...")
                loadSchemesDownloadPage("main-content");
        });
        
        
        $(".contact-us").live('click', function() {
                console.log("Contact Us Button Clicked...")
                contactUs("main-content");
        });
        
        $(".create-scheme").live('click', function() {
                loadScheme("main-content");
        });
        
        $(".retailer-info").live('click', function() {
                retailerInfo("main-content");
        });
        
        $('#brands li a').live('click', function(){
                $('#brand-value').text($(this).text());
                loadItemsDescriptionByBrand("items-description-container", $(this).text());
            console.log($(this).text());
        });
        

        $("td.startGrnSerialized").live('click', function() {
                var invoiceNumber = $(this).attr('invoiceNumber');
                var quantity = $(this).attr('quantity');
                var displayName = $(this).attr('displayName');
                var itemId = $(this).attr('itemId');
                console.log(quantity);
                $(".modal-body .grnInvoiceNumber>span").text(invoiceNumber);
                $(".modal-body .grnProductInfo>span").text(displayName);
                $("#scanModel .invoiceNumber").val(invoiceNumber);
                $("#scanModel .itemId").val(itemId);
                $( "#grnImeiInformation" ).empty();
                var qty = parseInt(quantity);
                var divCode = '<div class="row">CONTENT</div><p></p>';
                var innerDiv = '<div class="col-sm-3">'+
                '<div class="input-group">'+
                '<input type="text" class="form-control">'+
                '</div>'+
                '</div>';
                while(qty!=0){
                        var divText  = "";
                        if (qty < 4){
                                var cp = qty;
                                for (var i=0;i<cp;i++ ){
                                        divText = divText + innerDiv;
                                        qty = qty - 1;
                                }
                        }
                        else{
                                for(var i=0;i<4;i++){
                                        divText = divText + innerDiv;
                                        qty = qty - 1;
                                }
                        }
                        divText = divCode.replace("CONTENT",divText); 
                        $("#grnImeiInformation")
                        .append(divText)
                }
        });

        $("td.startGrnNonSerialized").live('click', function() {
                var invoiceNumber = $(this).attr('invoiceNumber');
                var quantity = $(this).attr('quantity');
                var displayName = $(this).attr('displayName');
                var itemId = $(this).attr('itemId');
                console.log(quantity);
                $("#scanNonSerializedModel .modal-body .grnInvoiceNumber>span").text(invoiceNumber);
                $("#scanNonSerializedModel .modal-body .grnProductInfo>span").text(displayName);
                $("#scanNonSerializedModel .invoiceNumber").val(invoiceNumber);
                $("#scanNonSerializedModel .itemId").val(itemId);
                $( "#grnImeiInformation" ).empty();
                var qty = parseInt(quantity);
                var divCode = '<div class="row">CONTENT</div><p></p>';
                var innerDiv = '<div class="col-sm-3">'+
                '<div class="input-group">'+
                '<input type="text" class="form-control">'+
                '</div>'+
                '</div>';
                while(qty!=0){
                        var divText  = "";
                        if (qty < 4){
                                var cp = qty;
                                for (var i=0;i<cp;i++ ){
                                        divText = divText + innerDiv;
                                        qty = qty - 1;
                                }
                        }
                        else{
                                for(var i=0;i<4;i++){
                                        divText = divText + innerDiv;
                                        qty = qty - 1;
                                }
                        }
                        divText = divCode.replace("CONTENT",divText); 
                        $("#grnImeiInformation")
                        .append(divText)
                }
        });
        
        $("#grnSubmitSerialized").live('click', function(){
                var imeis = [];
                var error = false;
                var errorText = "";
                $("#grnImeiInformation :input").each(function(){
                        var input = $(this).val().trim();
                        if (imeis.indexOf(input) !=-1 || !input){
                                error = true;
                                if (!input){
                                        $(this).addClass("border-highlight");
                                }
                        }
                        imeis.push(input);
                        
                });
                if (error){
                        return false;
                }
                var invoiceNumber = $("#scanModel .invoiceNumber").val();
                var itemId = $("#scanModel .itemId").val();
                var postData = JSON.stringify({"itemId":parseInt(itemId),"invoiceNumber":invoiceNumber,"serialNumbers":imeis});
                jQuery.ajax({
                        url: context+"/scanSerialized",
                        type: 'POST',
                        data: postData,
                        contentType:'application/json',
                        async: false,
                        success: function (data) {
                                alert("Purchase booked successfully");
                                $('#scanModel').modal('hide');
                                $("#scanModel").on("hidden.bs.modal", function () {
                                        $("#purchase-reference-submit-button").click();
                                });
                        },
                        cache: false,
                        processData: false
                });
                return false;
        });
        
        $("#grnNonSerializedSubmit").live('click', function(){
                var error = false;
                var errorText = "";
                if (error){
                        return false;
                }
                var invoiceNumber = $("#scanNonSerializedModel .invoiceNumber").val();
                var itemId = $("#scanNonSerializedModel .itemId").val();
                var quantity = $("#scanNonSerializedModel .quantity").val();
                var postData = JSON.stringify({"itemId":parseInt(itemId),"invoiceNumber":invoiceNumber,"quantity":parseInt(quantity)});
                jQuery.ajax({
                        url: context+"/scanNonSerialized",
                        type: 'POST',
                        data: postData,
                        contentType:'application/json',
                        async: false,
                        success: function (data) {
                                alert("Purchase booked successfully");
                                $('#scanNonSerializedModel').modal('hide');
                                $("#scanNonSerializedModel").on("hidden.bs.modal", function () {
                                        $("#purchase-reference-submit-button").click();
                                });
                        },
                        cache: false,
                        processData: false
                });
                return false;
        });

        $("#grnImeiInformation :input").live('change paste keyup mouseup', function() {
                if ($(this).val().trim() != "") {
                        console.log('The text box really changed this time');
                        $('#grnImeiInformation :input').removeClass("border-highlight");
                        if(findDuplicateSerialNumbers( $(this).val().trim()) > 0){
                                var inputs = $("#grnImeiInformation :input[value="+$(this).val().trim()+"]");
                                if(inputs.length > 1){
                                        inputs.each(function(){$(this).addClass("border-highlight")});
                                }
                        }
                }
        });
        
        
        $("#grnImeiInformation :input").live('paste', function() {
                if ($(this).val().trim() != "") {
                        console.log('paste event');
                        $(this).next("input .form-control").focus();
                }
        });
        
        $("#good-inventory-paginated .next").live('click', function() {
                var start = $( "#good-inventory-paginated .start" ).text();
                var end = $( "#good-inventory-paginated .end" ).text();
                var searchText = $("#good-inventory-search-text").val();
                if (typeof (searchText) == "undefined" || !searchText){
                        searchText = "";
                }
                getNextItems(start,end, searchText);
                $("#good-inventory-paginated .next").blur();
                
    });
        
        $("#good-inventory-paginated .previous").live('click', function() {
                var start = $( "#good-inventory-paginated .start" ).text();
                var end =  $( "#good-inventory-paginated .end" ).text();
                var searchText = $("#good-inventory-search-text").val();
                if (typeof (searchText) == "undefined" || !searchText){
                        searchText = "";
                }
                var pre = end - 20;
                getPreviousItems(start, end, pre, searchText);
                $("#good-inventory-paginated .previous").blur();
    });
        
        $("#good-inventory-search-button").live('click', function() {
                var searchText = $("#good-inventory-search-text").val();
                if (typeof (searchText) == "undefined" || !searchText){
                        searchText = "";
                }
                loadGoodInventorySearchInfo(searchText);
    });
        
        $("#good-inventory-search-text").live("keyup", function(e) {
                var keyCode = e.keyCode || e.which;
        if(keyCode == 13){
                $("#good-inventory-search-button").click();
        }
        });
        
        $("#airwayBillOrInvoiceNumberText").live("keyup", function(e) {
                var keyCode = e.keyCode || e.which;
        if(keyCode == 13){
                $("#purchase-reference-submit-button").click();
        }
        });
        
        $("#catalog-paginated .next").live('click', function() {
                var start = $( "#catalog-paginated .start" ).text();
                var end = $( "#catalog-paginated .end" ).text();
                var searchText = $("#catalog-search-text").val();
                if (typeof (searchText) == "undefined" || !searchText){
                        searchText = "";
                }
                getNextCatalogItems(start,end,searchText);
                $("#catalog-paginated .next").blur();
    });
        
        $("#catalog-paginated .previous").live('click', function() {
                var start = $( "#catalog-paginated .start" ).text();
                var end =  $( "#catalog-paginated .end" ).text();
                var searchText = $("#catalog-search-text").val();
                if (typeof (searchText) == "undefined" || !searchText){
                        searchText = "";
                }
                var pre = end - 20;
                getPreviousCatalogItems(start,end,pre,searchText);
                $("#catalog-paginated .previous").blur();
    });
        
        $("#catalog-search-button").live('click', function() {
                var searchText = $("#catalog-search-text").val();
                if (typeof (searchText) == "undefined" || !searchText){
                        searchText = "";
                }
                loadCatalogSearchInfo(searchText);
    });
        
        $("#catalog-search-text").live("keyup", function(e) {
                var keyCode = e.keyCode || e.which;
        if(keyCode == 13){
                $("#catalog-search-button").click();
        }
        });
        
        $("#grn-history-search-button").live('click', function() {
                grnhistory.searchText = $("#grn-history-search-text").val();
                grnhistory.searchType = "purchaseReference";
                grnhistory.startTime = "0";
                grnhistory.endTime = "0";
                if (typeof (grnhistory.searchText) == "undefined" || !grnhistory.searchText){
                        grnhistory.searchText = "";
                }
                loadGrnHistorySearchInfo(grnhistory.searchText,grnhistory.searchType,grnhistory.startTime,grnhistory.endTime);
    });
        
        $("#grn-history-search-text").live("keyup", function(e) {
                var keyCode = e.keyCode || e.which;
        if(keyCode == 13){
                $("#grn-history-search-button").click();
        }
        });
        
        $("#retailer-details-search-text").live("keyup", function(e) {
                var keyCode = e.keyCode || e.which;
        if(keyCode == 13){
                $("#retailer-details-search-button").click();
        }
        });
        
        
        $('#searchType').live('change', function() { 
            var searchType = $(this).find('option:selected').text();
            console.log("selected searchType = " + searchType);
            $("#sale-history-search-text").attr('placeholder', 'Search By '+searchType);
            $("#sale-history-search-text").val('');
        });
        
        $('#retailerAddressState').live('change', function() { 
            var stateName = $(this).find('option:selected').text();
            loadDistrictNames(stateName);
        });
        
        
        $("#sale-history-search-button").live('click', function() {
                console.log("sale-history-search-button clicked")
                var searchType = $("#searchType option:selected").val();
                console.log("searchType = " + searchType);
                $("#searchType").removeClass("border-highlight");
                if(searchType == ""){
                        alert("Please choose Search Type");
                        $("#searchType").addClass("border-highlight");
                }else{
                        var startDateTime = $('input[name="startDateTime"]').val();
                        console.log("startDateTime : "+startDateTime);
                        var endDateTime = $('input[name="endDateTime"]').val();
                        console.log("endDateTime : "+endDateTime);
                        
                        saleHistory.searchText = $("#sale-history-search-text").val();
                        saleHistory.searchType = searchType;
                        saleHistory.startTime = startDateTime;
                        saleHistory.endTime = endDateTime;
                        if (typeof (saleHistory.searchText) == "undefined" || !saleHistory.searchText){
                                saleHistory.searchText = "";
                        }
                        saleHistorySearchInfo(saleHistory.searchType, saleHistory.searchText, saleHistory.startTime, saleHistory.endTime);
                        $("#sale-history-search-text").val(saleHistory.searchText);
                        $("#searchType").val(searchType);
                        $('input[name="startDateTime"]').val(saleHistory.startTime);
                        $('input[name="endDateTime"]').val(saleHistory.endTime);
                }
    });
        
        /*$("#sale-history-search-text").live("keyup", function(e) {
                var keyCode = e.keyCode || e.which;
        if(keyCode == 13){
                $("#sale-history-search-button").click();
        }
        });*/
        
        $("#grn-history-paginated .next").live('click', function() {
                var start = $( "#grn-history-paginated .start" ).text();
                var end = $( "#grn-history-paginated .end" ).text();
                getGrnHistoryNextItems(start,end,grnhistory.searchText,grnhistory.searchType,grnhistory.startTime,grnhistory.endTime);
                $("#grn-history-paginated .next").blur();
                
    });
        
        $("#grn-history-paginated .previous").live('click', function() {
                var start = $( "#grn-history-paginated .start" ).text();
                var end =  $( "#grn-history-paginated .end" ).text();
                var pre = end - 20;
                getGrnHistoryPreviousItems(start,end,pre,grnhistory.searchText,grnhistory.searchType,grnhistory.startTime,grnhistory.endTime);
                $("#grn-history-paginated .previous").blur();
    });
        
        $("#grn-history-date-button").live('click', function() {
                var dateRange = $('#reportrange span').text();
                grnhistory.startTime = new Date(dateRange.split("-")[0]).getTime();
                grnhistory.endTime = new Date(dateRange.split("-")[1]).getTime();
                grnhistory.searchType = "dateFilter";
                grnhistory.searchText = "";
                loadGrnHistorySearchInfo(grnhistory.searchText,grnhistory.searchType,grnhistory.startTime,grnhistory.endTime);
    });
        
        
        $("#sale-history-paginated .next").live('click', function() {
                var start = $( "#sale-history-paginated .start" ).text();
                var end = $( "#sale-history-paginated .end" ).text();
                getSaleHistoryNextItems(start, end, saleHistory.searchType, saleHistory.searchText, saleHistory.startTime, saleHistory.endTime);
                $("#sale-history-paginated .next").blur();
    });
        
        $("#sale-history-paginated .previous").live('click', function() {
                var start = $( "#sale-history-paginated .start" ).text();
                var end =  $( "#sale-history-paginated .end" ).text();
                var pre = end - 20;
                getSaleHistoryPreviousItems(start, end, pre, saleHistory.searchType, saleHistory.searchText, saleHistory.startTime, saleHistory.endTime);
                $("#sale-history-paginated .previous").blur();
    });
        
        $("#schemes-paginated .next").live('click', function() {
                var start = $( "#schemes-paginated .start" ).text();
                var end = $( "#schemes-paginated .end" ).text();
                getSchemesNextItems(start, end);
                $("#schemes-paginated .next").blur();
    });
        
        $("#schemes-paginated .previous").live('click', function() {
                var start = $( "#schemes-paginated .start" ).text();
                var end =  $( "#schemes-paginated .end" ).text();
                var pre = end - 20;
                getSchemesPreviousItems(start, end, pre);
                $("#schemes-paginated .previous").blur();
    });
        
        $("#wallet-history-paginated .next").live('click', function() {
                var start = $( "#wallet-history-paginated .start" ).text();
                var end = $( "#wallet-history-paginated .end" ).text();
                getWalletHistoryNextItems(start, end, walletHistory.startTime, walletHistory.endTime);
                $("#wallet-history-paginated .next").blur();
    });
        
        $("#wallet-history-paginated .previous").live('click', function() {
                var start = $( "#wallet-history-paginated .start" ).text();
                var end =  $( "#wallet-history-paginated .end" ).text();
                var pre = end - 20;
                getWalletHistoryPreviousItems(start, end, pre, walletHistory.startTime, walletHistory.endTime);
                $("#wallet-history-paginated .previous").blur();
    });
        
        /*$("#sale-history-date-button").live('click', function() {
                var dateRange = $('#reportrange span').text();
                saleHistory.startTime = new Date(dateRange.split("-")[0]).getTime();
                saleHistory.endTime = new Date(dateRange.split("-")[1]).getTime();
                saleHistory.searchType = "";
                saleHistory.searchText = "";
                saleHistorySearchInfo(saleHistory.searchType, saleHistory.searchText, saleHistory.startTime, saleHistory.endTime);
    });*/
        
        
        //grn-details
        
        $(".grn-details").live('click',function(){
                var purchaseId = $(this).attr('data');
                console.log(purchaseId);
                loadGrnDetails(purchaseId,"grn-details-container");
        });
        
        $(".pending-grn-details").live('click',function(){
                console.log("pending-grn-details clicked");
                var orderId = $(this).attr('data');
                console.log("orderId : "+orderId);
                loadPendingGrnDetails(orderId, "pending-grn-details-container");
        });
        
        $(".sale-details").live('click',function(){
                var orderId = $(this).attr('data');
                console.log("orderId = "+orderId);
                loadSaleDetails(orderId,"sale-details-container");
        });
        
        $(".scheme-details").live('click',function(){
                var schemeId = $(this).attr('data');
                console.log("schemeId = "+schemeId);
                loadSchemeDetails(schemeId, "scheme-details-container");
        });
        
        $(".pending-grn").live('click',function(){
                var purchaseReference = $(this).attr('data');
                console.log(purchaseReference);
                loadPendingGrnDetails(purchaseReference,"main-content");
        });
        
        $(".active-scheme").live('click',function(){
                var schemeId = $(this).attr('data');
                console.log("active-scheme id : "+schemeId);
                var start = $( "#schemes-paginated .start" ).text();
                //var end = $( "#schemes-paginated .end" ).text();
                var offset = start - 1;
                if(confirm("Are you sure you want to Active Scheme!") == true){
                        activeScheme(schemeId, offset, "schemes-table");
                }
        });
        
        $(".expire-scheme").live('click',function(){
                var schemeId = $(this).attr('data');
                console.log("expire-scheme id : "+schemeId);
                var start = $( "#schemes-paginated .start" ).text();
                //var end = $( "#schemes-paginated .end" ).text();
                var offset = start - 1;
                if(confirm("Are you sure you want to Expire Scheme!") == true){
                        expireScheme(schemeId, offset, "schemes-table");
                }
        });
        
        //add-to-bag
        
        $(".add-to-bag").live('click', function() {
                var itemId = $(this).attr('data');
                $("#bagModel .itemId").val(itemId);
                $("#bagModel .quantity").val(0);
                jQuery.ajax({
                        url: context+"/checkItemAvailability/?itemId="+itemId,
                        type: 'GET',
                        contentType:'application/json',
                        async: false,
                        success: function (data) {
                                var obj = JSON.parse(data);
                                console.log(obj);
                                console.log(obj.displayName);
                                $(".modal-body h4.bagModalProductInfo.modelHeaderCustom > span").text(obj.displayName);
                                $(".modal-body h4.bagModalAvailability.modelHeaderCustom > span").text(obj.availability);
                                $("#bagModel .availability").val(obj.availability);
                        },
                        cache: false,
                        processData: false
                });
        });
        
        //addToBagButton
        
        $("#addToBagButton").live('click', function() {
                var itemId = $(this).attr('data');
                var itemId = $("#bagModel .itemId").val();
                var tempAvailability = $("#bagModel .availability").val();
                var quantity = $("#bagModel .quantity").val();
                
                if (!quantity || parseInt(quantity) <= 0 || isNaN(parseInt(quantity)) || quantity.indexOf('.') != -1){
                        alert("Please enter valid quantity");
                        return;
                }
                
                jQuery.ajax({
                        url: context+"/checkItemAvailability/?itemId="+itemId,
                        type: 'GET',
                        contentType:'application/json',
                        async: false,
                        success: function (data) {
                                var obj = JSON.parse(data);
                                console.log(obj);
                                if (obj.availability < parseInt(quantity) || obj.availability == 0){
                                        alert("You don't have enough inventory of this item");
                                        return;
                                }
                                var obj = {'itemId':obj.itemId,'displayName':obj.displayName,'quantity':parseInt(quantity),'iconUrl':obj.iconUrl};
                                console.log(obj);
                                addItemInLocalStorage(obj);
                                alert("Item added to bag successfully");
                                $('#bagModel').modal('hide');
                        },
                        cache: false,
                        processData: false
                });
        });
        
        $(".cart-trash").live('click',function(){
                var itemId = $(this).attr('data');
                removeItemFromLocalStorage(itemId);
                $('div[itemId="' + itemId + '"]').remove();
                if ($('#cart-details').html().trim() == ""){
                        $('#cart-details-empty').show();
                        $('#cart-checkout').hide();
                }
        });
        
        $(".cart-qty").live('change paste keyup mouseup', function() {
                var itemId = $(this).attr('data');
                var maxQty = $(this).attr('max');
                var qty = $(this).val();
                if (!qty){
                        qty = 0;
                }
                if(parseInt(qty) < 0){
                        alert("Invalid quantity");
                        $(this).val(0);
                        qty = 0;
                }
                if (parseInt(qty) > parseInt(maxQty)){
                        alert("You don't have availability of "+qty);
                        $(this).val(maxQty);
                        qty = maxQty;
                }
                changeQuantityInLocalStorage(itemId, qty);
        });
        
        $("form#cd input.discountAmount").live('change paste keyup mouseup', function() {
                var discountAmount = parseFloat($(this).val());
                if (isNaN(discountAmount)){
                        discountAmount = 0;
                }
                if(discountAmount < 0){
                        alert("Invalid insurance Value");
                        $(this).val(0);
                }
                calculateTotalAmount();
        });
        
        $("form#cd input.unitPrice").live('change paste keyup mouseup', function() {
                
                var unitPrice = parseFloat($(this).val());
                if (isNaN(unitPrice)){
                        unitPrice = 0;
                }
                if(unitPrice < 0){
                        alert("Invalid unit price");
                        $(this).val(0);
                }
                calculateTotalAmount();
        });
        
        
        $("form#cd input.insuranceAmount").live('change paste keyup mouseup', function() {
                calculateTotalAmount();
        });

});

function calculateTotalAmount() {
        var netPayableAmount = 0;
        $("#cd").find('tr:gt(0)').each(function(index, el){
                var rowTotal = 0;
                $tr = $(el);

                var insuranceAmount = $tr.find('.insuranceAmount').val();
                if (!insuranceAmount){
                        insuranceAmount = 0;
                }else{
                        insuranceAmount = parseFloat(insuranceAmount);
                }
                
                var qty = parseFloat($(el).find('.unitPrice').attr('quantity'));
                var unitPrice = parseFloat($(el).find('.unitPrice').val());
                
                var discountAmount =  parseFloat($(el).find('.discountAmount').val());
                if(isNaN(discountAmount)){
                        discountAmount = 0;
                }

                rowTotal = unitPrice*qty + insuranceAmount - discountAmount; 
                $tr.find('.totalPrice').val(rowTotal);
                netPayableAmount += rowTotal;
        });
        $('#cd').find('input.netPayableAmount').val(netPayableAmount);
}

function updateRetailerDocument(){
        //$("#updateRetailerShopDocument0").click( function() {
        console.log("Update Retailer Document Clicked");
        //console.log(ownerId);
        var emailIdOrMobileNumber = $('#retailer-details-search-text').val();
        console.log("emailIdOrMobileNumber: "+ emailIdOrMobileNumber);
        var file = $('#retailerDocument')[0].files[0];
        console.log("file : "+file.name);
        uploadRetailerDocument("retailer-details-container", file, emailIdOrMobileNumber);
}

function updateRetailerShopDocument(ownerId){
        //$("#updateRetailerShopDocument0").click( function() {
        console.log("Update Retailer Shop Document Clicked");
        console.log(ownerId);
        var shopSize = parseInt($("#shopDetailsSize").attr('size'));
        console.log("size : "+shopSize);
        for(var index = 0; index < shopSize; index++){
                if("updateRetailerShopDocument"+index == ownerId){
                        var emailIdOrMobileNumber = $('#retailer-details-search-text').val();
                        console.log("emailIdOrMobileNumber: "+ emailIdOrMobileNumber);
                        var shopId = $('#retailerShopDocument'+index).attr("shopId");
                        console.log("shopId : "+shopId);
                        var file = $('#retailerShopDocument'+index)[0].files[0];
                        console.log("file : "+file.name);
                        uploadRetailerShopDocument("retailer-details-container", file, emailIdOrMobileNumber, shopId);
                }
        }
}

function updateDetails(){
        
}