Subversion Repositories SmartDukaan

Rev

Rev 7529 | Blame | Compare with Previous | Last modification | View Log | RSS feed

$(function() {
        
        $("#query").autocomplete({ autoFocus: true, minLength: 3,
                source: "/storewebsite/auto-suggest",
                select: function( event, ui ) {
                this.value = ui.item.value;
                $('#formSearch').submit();
                return false;
                }
        }); 
        
        $("#formSearch").submit(function() {
                if($("#query").val() == "" || $("#query").val() == "Search for more items...") {
                        return false;
                }
        });
        
        $('#pincodeForm').submit(function() {
                
                if($('#pincodeSubmit:hidden').length == 1) {
                        return false;
                }
                
                if ($('#pincodeText').val().length != 6) {
                        alert('Please enter 6 digit pin code');
                        return false;
                }
                
                var result = $('#pincodeText').val().match(/^\d*$/);
                if(result == null || result.length == 0) {
                        alert('Please enter a valid pin code');
                        return false;
                }
                
                $('#priceDiv').empty();
                showLoading();
                $.ajax({
                        type: "POST",
                        url: "/storewebsite/estimate",
                        dataType:"json",
                        data:$('#pincodeForm').serialize(),
//                      timeout: 10000,
                        success:function(msg) {
                                hideLoading();
                                if(msg.length != 0) {
                                        if(msg.selling_price == 0.0 || msg.delivery_estimate == '01 January 1970') {
                                                $('#loading').hide();
                                                $('#shippingAddress').hide();
                                                $('#priceDiv').html("<div class='error'>There was some error. Please try again.</div>");
                                                $('#pincodeSubmit').show();
                                        } else {
                                                $('#priceDiv').html(
                                                "<div class='date'><span class='infoText'>Get this product by </span><span class='infoValue'>" + msg.delivery_estimate + "</span></div>" + 
                                                "<div class='price'><span class='infoText'>Total Price</span><span class='infoValue'>&nbsp;Rs.&nbsp;" + $('#shippingAddress input[name=price]').val() + "</span></div>" +
                                                "<div class='advance'><span class='infoText'>Advance to be paid</span><span id='advPrice' class='infoValue'>&nbsp;Rs.&nbsp;" + msg.min_advance_amount + "</span></div>");
                                                if(msg.min_advance_amount > parseFloat($('#shippingAddress input[name=price]').val())) {
                                                        $('#shippingAddress input[name=advanceAmount]').val($('#shippingAddress input[name=price]').val());
                                                        $('#shippingAddress input[name=minAdvanceAmount]').val($('#shippingAddress input[name=price]').val());
                                                        $('#editPriceBox').val($('#shippingAddress input[name=price]').val());
                                                } else {
                                                        $('#shippingAddress input[name=advanceAmount]').val(msg.min_advance_amount);
                                                        $('#shippingAddress input[name=minAdvanceAmount]').val(msg.min_advance_amount);
                                                        $('#editPriceBox').val(msg.min_advance_amount);
                                                }
                                                $('#shippingAddress input[name=deliveryDate]').val(msg.delivery_estimate);
                                                $('#shippingAddress input[name=minPrice]').val(msg.min_selling_price);
                                                $('#shippingAddress input[name=maxPrice]').val(msg.max_selling_price);
                                                $('#shippingAddress input[name=bestDealText]').val(msg.best_deal_text);
                                                $('#bestDealText').text(msg.best_deal_text);
                                                $('#bestDealContainer').show();
                                                $('#pincodeInput').val($('#pincodeText').val());
                                                $('#editPriceButton').show();
                                                
                                        }
                                        
                                } else {
                                        $('#priceDiv').html("<div class='error'>No info found for this pincode. Please try again.</div>");
                                }
                        },
                        error : function(msg) {
                                $('#loading').hide();
                                $('#shippingAddress').hide();
                                $('#priceDiv').html("<div class='error'>There was some error. Please try again.</div>");
                                $('#pincodeSubmit').show();
                },
                complete : function() {
                        $('#pinSpan').html($('#pincodeText').val());
                        
                }
                });
                return false;
        });
        
        function showLoading() {
                $('#pincodeSubmit').hide();
                $('#shippingAddress').hide();
                $('#loading').show();
        }
        
        function hideLoading() {
                $('#loading').hide();
                $('#shippingAddress').show();
        }
        
        $('#changePincode').click(function() {
                window.location.reload();
        });
        
        $('#reloadSuccessPage').click(function() {
                window.location.reload();
        });
        
        $('#pincodeEdit').click(function(event){
                window.location.href = '/storewebsite/order-details!create?product_id=' + $('#createOrderForm input[name=product_id]').val() + '&price=' + $('#createOrderForm input[name=price]').val();
        });
        
        $('#addressEdit').click(function() {
                $('#createOrder, #showPinDiv, #showAddDiv').hide();
                $('#editAddBlock').show();
        });
        
        $('#saveEditedAdd').click(function() {
                var editedFields = $('#editAddBlock input');
                var msg = "";
                var displayAlert = false;
                editedFields.each(function(i, inputField) {
                        if($(inputField).val() == "") {
                                if($(inputField).attr('name') != 'line2') {
                                        msg = "Please enter correct " + $(inputField).attr('name');
                                        displayAlert = true;
                                        return;
                                }
                        }
                        
                        if ($(inputField).attr('name') == "phone") {
                                if ($(inputField).val().length != 10) {
                                        msg = 'Please enter a 10 digit mobile number';
                                        displayAlert = true;
                                        return;
                                }
                                
                                var result = $(inputField).val().match(/^\d*$/);
                                if(result == null || result.length == 0) {
                                        msg = 'Please enter a valid mobile number';
                                        displayAlert = true;
                                        return;
                                }
                        }
                        
                        var nameToSearch = $(inputField).attr("name");
                        $('#showAddDiv [name="' + nameToSearch + '"]').html($(inputField).val());
                        $('#createOrderForm input[name="' + nameToSearch + '"]').val($(inputField).val());
                });
                
                if(displayAlert == true) {
                        alert(msg);
                } else {
                        $('#editAddBlock').hide();
                        $('#createOrder, #showPinDiv, #showAddDiv').show();
                }
        });

        $('#editPriceButton').click(function() {
                $('#editPriceButton').hide();
                $('#editPriceSpan').show();
        });
        
        $('#priceFixButton').click(function() {
                var result = $('#editPriceBox').val().match(/^\d*$/);
                if(result == null || result.length == 0) {
                        alert('Please enter a valid amount');
                        return false;
                }
                
                if(parseFloat($('#editPriceBox').val()) > parseFloat($('#shippingAddress input[name=price]').val())) {
                        alert('Advance cannot be more than Rs.' + $('#shippingAddress input[name=price]').val());
                        return false;
                }
                
                if(parseFloat($('#editPriceBox').val()) < parseFloat($('#shippingAddress input[name=minAdvanceAmount]').val())) {
                        alert('Advance cannot be below Rs. ' + $('#shippingAddress input[name=minAdvanceAmount]').val());
                        return false;
                }
                
                $('#editPriceButton').show();
                $('#editPriceSpan').hide();
                $('#advPrice').empty();
                $('#advPrice').text(' Rs. ' + parseFloat($('#editPriceBox').val()).toFixed(1));
                $('#shippingAddress input[name=advanceAmount]').val($('#editPriceBox').val());
        });
        
        $('#editSellingPriceButton').click(function() {
                $('#editSellingPriceButton').hide();
                $('#pricePopupBox').show();
        });
        
        $('#cancelPriceEdit').click(function() {
                $('#pricePopupBox').hide();
                $('#editSellingPriceButton').show();
        });
        
        $('#savePriceEdit').click(function() {
                var result = ($('#sellingPriceBox').val().split('.')[0]).match(/^\d*$/);
                if(result == null || result.length == 0) {
                        alert('Please enter a valid amount');
                        return false;
                }
                
                if(parseFloat($('#sellingPriceBox').val()) > parseFloat($('#maxPrice').text())) {
                        alert('You cannot sell this product at more than Rs.' + $('#maxPrice').text());
                        return false;
                }
                
                if(parseFloat($('#sellingPriceBox').val()) < parseFloat($('#absMin').val())) {
                        alert('You cannot sell below Rs.' + $('#absMin').val());
                        return false;
                }
                
                if(parseFloat($('#sellingPriceBox').val()) < parseFloat($('#minPrice').text())) {
                        alert('You will need permission of the zonal head to sell at this price');
                }
                
                $('#editSellingPriceButton').show();
                $('#pricePopupBox').hide();
                
                $('#mrp').html(parseFloat($('#sellingPriceBox').val()).toFixed(2));
        });
        
        $("#dateselector").change(function() {
                if($(this).val()=="3"){
                        $("#daterange").show();
                }else {
                        $("#daterange").hide();
                }
        });
        
        $("#dateselector1").change(function() {
                if($(this).val()=="3"){
                        $("#daterange1").show();
                }else {
                        $("#daterange1").hide();
                }
        });
        
        $(".returnButton").click(function() {
                $("#returnPopupOrder").val($(this).attr("order"));
                $("#returnPopupBox").show();
        });
        
        $("#cancelReturnRequest").click(function() {
                $("#returnPopupOrder").val("");
                $("#returnPopupBox").hide();
        });
        
        $(".refundButton").click(function() {
                $("#refundPopupOrder").val($(this).attr("order"));
                $("#refundPopupBox").show();
        });
        
        $("#cancelRefundRequest").click(function() {
                $("#refundPopupOrder").val("");
                $("#refundPopupBox").hide();
        });
        
        $('#createOrderForm input[name=cardAmount]').focusin(function() {
                $('#bankSelector').show();
        });
        
        $('#createOrderForm input[name=cardAmount]').focusout(function() {
                tmp = parseFloat($('#createOrderForm input[name=cardAmount]').val());
                if(isNaN(tmp) || tmp < 1) {
                        $('#bankSelector').hide();
                }
                else {
                        $('#bankSelector').show();
                }
        });
});

function validateAddress() {
        if ($('input[name=name]').val() == "") {
                alert("Please enter the customer's name");
                return false;
        }
        
        if ($('input[name=phone]').val().length != 10) {
                alert('Please enter a 10 digit mobile number');
                return false;
        }
        
        var result = $('input[name=phone]').val().match(/^\d*$/);
        if(result == null || result.length == 0) {
                alert('Please enter a valid mobile number');
                return false;
        }
        
        if ($('input[name=line1]').val() == "") {
                alert('Please enter a shipping address');
                return false;
        }
        
        if ($('input[name=city]').val() == "") {
                alert('Please enter the name of the city');
                return false;
        }
        if ($('#shipState').val() == "") {
                alert('Please enter the name of the state');
                return false;
        }
        
        if ($('input[name=email]').val() == "") {
                alert("Please enter the customer's name");
                return false;
        }
        
        return true;
}

function validatePaymentDetails() {
        if($('#createOrderForm input[name=advanceAmount]').val() == ""){
                total = 0;
        } else {
                total = parseFloat($('#createOrderForm input[name=advanceAmount]').val());
        }
        
        if($('#createOrderForm input[name=cashAmount]').val() == ""){
                cashAmount = 0;
        } else {
                cashAmount = parseFloat($('#createOrderForm input[name=cashAmount]').val());
        }
        
        if($('#createOrderForm input[name=cardAmount]').val() == ""){
                cardAmount = 0;
        } else {
                cardAmount = parseFloat($('#createOrderForm input[name=cardAmount]').val());
        }
                
        if(isNaN(total) || isNaN(cashAmount) || isNaN(cardAmount)) {
                alert("Please enter valid amounts");
                return false;
        }
        
        if(total != cashAmount + cardAmount) {
                alert("Sum of cash and card is not equal to advance amount");
                return false;
        }
        
        if(cardAmount > 0) {
                if($('#edcSelector').val() == "") {
                        alert('Please select the name of EDC bank');
                        return false;
                }
                
                if($('#createOrderForm input[name=approvalCode]').val() == "") {
                        alert("Please enter approval code");
                        return false;
                }
        }
        
        $('#createOrder').hide();
        $('#waitSpan').show();
        $('#addressEdit').hide();
        $('#pincodeEdit').hide();
}

function validateAmount() {
        var result = $('#refundAmountText').val().match(/^\d*$/);
        if(result == null || result.length == 0) {
                alert('Please enter a valid amount');
                return false;
        }
}