Subversion Repositories SmartDukaan

Rev

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

function loadCartDetails(){
$('.itemquantity').attr('disabled', 'true');
var cart_details = localStorage.getItem('cart_details');
if(cart_details==undefined){
        $(".pmfooter span.carttotalval").text('0');
        $(".pmfooter span.carttotalitemsval").text('0');
        $(".pmfooter span.badge").text('0');
        $(".pmfooter").addClass('hidden');
}else{
        var cartDetailsObj = JSON.parse(cart_details);
        if(cartDetailsObj.totalCartQuantity > 0){
                $(".pmfooter span.badge").text(cartDetailsObj.totalCartQuantity.toString());
                $(".pmfooter span.carttotalitemsval").text(cartDetailsObj.totalSkus.toString());
                var totalCartValue = cartDetailsObj.totalCartValue;
                var priceChangeMap = {};
                $.each(cartDetailsObj.cartItems, function(key,val) {
                        var newUnitPrice ;
                        var bulkpricing ;
                        
                        if($(".number-spinner button[data-id='"+key+"']").length == 0 && $(".button-checkbox button[data-id='"+key+"']").length != 0)
                                {
                                        newUnitPrice = $(".button-checkbox button[data-id='"+key+"']").data('price');
                                        bulkpricing = $(".button-checkbox button[data-id='"+key+"']").data('bulkpricing');
                                }
                        else{
                                        newUnitPrice = $(".number-spinner button[data-id='"+key+"']").data('price');
                                        bulkpricing = $(".number-spinner button[data-id='"+key+"']").data('bulkpricing');
                        }

                        
                        if (bulkpricing != undefined && bulkpricing.length != 0){
//                              bulkpricing = bulkpricing.sort(function(a, b){
//                                      return a.quantity - b.quantity;
//                  });
                                

                        for(var i=0;i<bulkpricing.length;i++)
                                {
                                        if(val.quantity<bulkpricing[i]['quantity']){
//                                              $(".number-spinner button[data-id='"+key+"']").parent().parent().parent().parent().find('.select'+key+'a'+bulkpricing[i-1]['quantity']).addClass('setback');
                                                break;
                                        }
                                        else{
                                                newUnitPrice = bulkpricing[i]['price'];
                                                
                                                $(".number-spinner button[data-id='"+key+"']").parent().parent().parent().parent().find('.setback').removeClass('setback');
                                                $(".number-spinner button[data-id='"+key+"']").parent().parent().parent().parent().find('.select'+key+'a'+bulkpricing[i]['quantity']).addClass('setback');

                                        }
                                }
                    }
                        if(newUnitPrice!=undefined){
                                $(".number-spinner button[data-id='"+key+"']").closest('.number-spinner').find('input').val(val.quantity);
                                if(parseInt(newUnitPrice)!=parseInt(val.unitprice)){
                                        totalCartValue = totalCartValue - (parseInt(val.unitprice)*parseInt(val.quantity)) + (parseInt(newUnitPrice)*parseInt(val.quantity));
                                        priceChangeMap[key] = newUnitPrice;
                                }
                        }
                });
                $(".pmfooter span.carttotalval").text(numberWithCommas(totalCartValue));
                if(totalCartValue!=undefined){
                        cartDetailsObj.totalCartValue = totalCartValue;
                }
                var cartItems = cartDetailsObj.cartItems;
                $.each(priceChangeMap, function(key,val) {
                        cartItems[key].unitprice = val;
                });
                cartDetailsObj.cartItems = cartItems;
                localStorage.setItem('cart_details',JSON.stringify(cartDetailsObj));
                $(".pmfooter").removeClass('hidden');
        }else{
                $.each(cartDetailsObj.cartItems, function(key,val) {
                        var newUnitPrice = $(".number-spinner button[data-id='"+key+"']").data('price');
                        if(newUnitPrice!=undefined){
                                $(".number-spinner button[data-id='"+key+"']").closest('.number-spinner').find('input').val(val.quantity);
                                if(parseInt(newUnitPrice)!=parseInt(val.unitprice)){
                                        totalCartValue = totalCartValue - (parseInt(val.unitprice)*parseInt(val.quantity)) + (parseInt(newUnitPrice)*parseInt(val.quantity));
                                        priceChangeMap[key] = newUnitPrice;
                                }
                        }
                });
                $(".pmfooter span.carttotalval").text('0');
                $(".pmfooter span.badge").text('0');
                $(".pmfooter span.carttotalitemsval").text('0');
                $(".pmfooter").addClass('hidden');
        }
        }
        if(!$('div.pmfooter').hasClass('hidden')) { 
                $('div.content').attr('style', 'margin-bottom: 42px');
        }else{
                $('div.content').attr('style', 'margin-bottom: 0px');
        }
}
$(function(){
        loadCartDetails();
});
$(document).on('click','.number-spinner button',function(){
    btn = $(this);
    input = btn.closest('.number-spinner').find('input');
    oldValue = btn.closest('.number-spinner').find('input').val().trim();
        newVal = 0;
    btn.closest('.number-spinner').find('button').prop("disabled", false);
    var totalCartQuantity = 0;
    var totalCartValue = 0;
    var totalSkus = 0;
    var cartDetailsObj; 
    var jsonObjToBeStored = {};
    var cartItems = {};
    var cart_details = localStorage.getItem('cart_details');
    if(cart_details!=undefined){
        cartDetailsObj = JSON.parse(cart_details);
        totalCartQuantity = cartDetailsObj.totalCartQuantity;
        totalCartValue = cartDetailsObj.totalCartValue;
        cartItems = cartDetailsObj.cartItems;
    }
    var inc = 0;
    var dec = 0;
//    var sku = btn.data('id');
    var sku = parseInt(btn.attr('data-id'));
    var unitPrice = parseInt(btn.attr('data-price'));
    var prodname = btn.data('name');
    var brandname = btn.data('brand');
    var catalogItemId = btn.data('identifier');
    var minBuyQuantity = parseInt(btn.attr('data-minquantity'));
    if(minBuyQuantity == "" || minBuyQuantity == undefined){
        minBuyQuantity =1;
    }
    var quantityStep = parseInt(btn.attr('data-quantitystep'));
    if(quantityStep == "" || quantityStep == undefined){
        quantityStep =1;
    }
    var bulkpricing = JSON.parse(btn.attr('data-bulkpricing'));
    var maxquantflag = false;
    
    if (btn.attr('data-dir') == 'up') {         
                if ( input.attr('max') == undefined || parseInt(input.val()) < parseInt(input.attr('max')) ) {
                        if((parseInt(input.val())== 0 || parseInt(input.val())< minBuyQuantity) && (parseInt(input.attr('max')) >= minBuyQuantity)){    
                                inc = minBuyQuantity - parseInt(input.val());
                                newVal = parseInt(oldValue) + inc;
                        }
                        else{
                                inc = quantityStep;
                                if(parseInt(oldValue)< minBuyQuantity+(quantityStep*4)){

                                        newVal = parseInt(oldValue) + inc;
                                }
                                else if(parseInt(oldValue)< minBuyQuantity+(quantityStep*28)){
                                        var minqty = minBuyQuantity;
                                        
                                        if((parseInt(oldValue)+quantityStep) % quantityStep == 0 ){minqty = 0;}
                                        if(parseInt(oldValue) % (quantityStep*5) == 0){
                                                inc = (5*quantityStep)+minqty;
                                                newVal = parseInt(oldValue)+ (5*quantityStep)+minqty;
                                        }
                                        else{
                                                inc = minqty+ (5*quantityStep) - (parseInt(oldValue) % (5*quantityStep));
                                                newVal =minqty+ (5*quantityStep)+ parseInt(oldValue) - (parseInt(oldValue) % (5*quantityStep));
                                        }
                                }
                                else{
                                        var minqty = minBuyQuantity;
                                        if((parseInt(oldValue)+quantityStep) % quantityStep == 0){minqty = 0;}
                                        if(parseInt(oldValue) % (quantityStep*10) == 0){
                                                inc = (10*quantityStep)+minqty;
                                                newVal = parseInt(oldValue)+ (10*quantityStep)+minqty;
                                        }
                                        else{
                                                inc = (10*quantityStep)  - (parseInt(oldValue) % (10*quantityStep)) +minqty;
                                                newVal =10*quantityStep + parseInt(oldValue) - (parseInt(oldValue) % (10*quantityStep)) +minqty;
                                        }
                                }
                                if(newVal>parseInt(input.attr('max'))){
                                        var minqty = minBuyQuantity;
                                        if((parseInt(oldValue)+quantityStep) % quantityStep == 0){minqty = 0;}
                                        
                                        newVal = parseInt(input.attr('max')) - (newVal - parseInt(input.attr('max'))) % (minqty +quantityStep);
                                        inc = newVal - parseInt(oldValue);
                                        maxquantflag = true;
                                }
                        }
                }else{
                        newVal = parseInt(oldValue);
                        btn.prop("disabled", true);
                }
                if(newVal==parseInt(input.attr('max')) || maxquantflag == true){
                        input.attr('style', 'border: 2px solid #ff0000');
                }else{
                        input.attr('style', 'border: 1px solid #CCCCCC');
                }
                totalCartQuantity = totalCartQuantity + inc;
        } else {
                if ( input.attr('min') == undefined || parseInt(input.val()) > parseInt(input.attr('min')) ) {
//                  dec = 1;
                        dec = quantityStep;
                        newVal = parseInt(oldValue) - dec;
                        var minBuyQtyDiff = parseInt(oldValue) - minBuyQuantity;
                        if(minBuyQtyDiff ==0){
                                dec = minBuyQuantity;
                                newVal = parseInt(oldValue) - dec;
                        }else if(dec>minBuyQtyDiff){
                                dec = minBuyQtyDiff;
                                newVal = parseInt(oldValue) - dec;
                        }
                }else{
                        btn.prop("disabled", true);
                }
                if(newVal==parseInt(input.attr('min'))){
                        input.attr('style', 'border: 2px solid #5bc0de');
                }else{
                        input.attr('style', 'border: 1px solid #CCCCCC');
                }
                totalCartQuantity = totalCartQuantity - dec;
        }
    
        btn.closest('.number-spinner').find('input').val(newVal);
        
        if (bulkpricing != undefined && bulkpricing.length != 0){
                if(newVal == 0){
                        $(this).parent().parent().parent().parent().find('.setback').removeClass('setback');
                }
        for(var i=0;i<bulkpricing.length;i++)
                {
                        if(newVal<bulkpricing[i]['quantity']){
                                break;
                        }
                        else{
                                $(this).parent().parent().parent().parent().find('.setback').removeClass('setback');
                                $(this).parent().parent().parent().parent().find('.select'+sku+'a'+bulkpricing[i]['quantity']).addClass('setback');
                                unitPrice = bulkpricing[i]['price'];
                        }
                }
    }
        
        if(cartDetailsObj==undefined){
                jsonObjToBeStored['totalCartQuantity'] = totalCartQuantity;
                totalCartValue = totalCartValue + (unitPrice * newVal);
                jsonObjToBeStored['totalCartValue'] = totalCartValue;
                var itemDetail = {};
                itemDetail['quantity']=newVal;
                itemDetail['unitprice']=unitPrice;
                itemDetail['productname']=prodname;
                itemDetail['brand']=brandname;
                itemDetail['catalogItemId'] = catalogItemId.toString();
                cartItems[sku.toString()]=itemDetail;
                $.each( cartItems, function(itemId,value){
                        if(value['quantity']>0){
                                totalSkus = totalSkus +1;
                        }
                });
                jsonObjToBeStored['cartItems']=cartItems;
                jsonObjToBeStored['totalSkus']=totalSkus;
                localStorage.setItem('cart_details',JSON.stringify(jsonObjToBeStored));
        }else{
                cartDetailsObj.totalCartQuantity = totalCartQuantity;
                var itemDetail = cartItems[sku.toString()];
                if(itemDetail==undefined){
                        var itemDetail = {};
                        itemDetail['quantity']=newVal;
                        itemDetail['unitprice']=unitPrice;
                        itemDetail['productname']=prodname;
                        itemDetail['brand']=brandname;
                        itemDetail['catalogItemId'] = catalogItemId.toString();
                        totalCartValue = totalCartValue + (unitPrice * newVal);
                        cartItems[sku.toString()]=itemDetail;
                }else{
                        totalCartValue = totalCartValue - (cartItems[sku.toString()].quantity * cartItems[sku.toString()].unitprice) + (unitPrice * newVal);
                        if(newVal==0){
                                cartItems[sku.toString()].quantity = 0;
                                cartItems[sku.toString()].unitprice = unitPrice;
                                if(!cartItems[sku.toString()].hasOwnProperty('catalogItemId')){
                                        cartItems[sku.toString()]['catalogItemId'] = catalogItemId.toString();
                                }
                        }else{
                                cartItems[sku.toString()].quantity = newVal;
                                cartItems[sku.toString()].unitprice = unitPrice;
                                if(!cartItems[sku.toString()].hasOwnProperty('catalogItemId')){
                                        cartItems[sku.toString()]['catalogItemId'] = catalogItemId.toString();
                                }
                        }
                        
                }
                $.each( cartItems, function(itemId,value){
                        if(value['quantity']>0){
                                totalSkus = totalSkus +1;
                        }
                });
                cartDetailsObj.totalCartValue = totalCartValue;
                cartDetailsObj.totalSkus = totalSkus;
                cartDetailsObj.cartItems = cartItems;
                localStorage.setItem('cart_details',JSON.stringify(cartDetailsObj));
        }
        if(totalCartQuantity > 0){
                $(".pmfooter span.carttotalval").text(numberWithCommas(totalCartValue));
                $(".pmfooter span.badge").text(totalCartQuantity);
                $(".pmfooter span.carttotalitemsval").text(totalSkus);
                $(".pmfooter").removeClass('hidden');
        }else{
                $(".pmfooter span.carttotalval").text('0');
                $(".pmfooter span.badge").text('0');
                $(".pmfooter span.carttotalitemsval").text('0');
                $(".pmfooter").addClass('hidden');
        }
});

$(document).on('click','.see_more_colors',function(){
        var id = $(".number-spinner button").data('id');
        if($('.morecoloroptions_'+$(this).data('id')+' .button-checkbox button').hasClass('active'))
        {}
        else{
        $(".morecoloroptions_"+$(this).data('id')+" button:first-child" ).addClass("active");
        }
        $('.morecoloroptions_'+$(this).data('id')).toggleClass('hidden');
});

$(document).on('click','.button-checkbox > .btn',function(){
    $(this).addClass('active').siblings().removeClass('active');
    $(this).parent().parent().parent().find('.priceshow').addClass('hidden');
    $("."+$(this).attr('data-setsetect')).removeClass('hidden');
    var bulkpricing = $(this).data("bulkpricing");
    var cart_details = localStorage.getItem('cart_details');
    $(".number-spinner button[data-did='"+$(this).data('did')+"']").prop("disabled", false);
    if(cart_details!=undefined){
        var cartDetailsObj = JSON.parse(cart_details);
        var item = cartDetailsObj.cartItems[$(this).data('id')];
        if(item!=undefined){
                $(".number-spinner input[data-did='"+$(this).data('did')+"']").attr('style', 'border: 0');
                $(".number-spinner button[data-did='"+$(this).data('did')+"']").attr('data-id',$(this).data('id'));
                $(".number-spinner button[data-did='"+$(this).data('did')+"']").attr('data-price',parseInt($(this).data('price')));
                $(".number-spinner input[data-did='"+$(this).data('did')+"']").attr('max',parseInt($(this).data('max')));
                $(".number-spinner input[data-did='"+$(this).data('did')+"']").val(item.quantity);
                $(".accsproduct span[data-did='"+$(this).data('did')+"']").text(parseInt($(this).attr('data-price')));
                
                $(".number-spinner button[data-did='"+$(this).data('did')+"']").attr('data-minquantity',$(this).data('minquantity'));
                $(".number-spinner button[data-did='"+$(this).data('did')+"']").attr('data-bulkpricing',$(this).attr('data-bulkpricing'));
                $(".number-spinner button[data-did='"+$(this).data('did')+"']").attr('data-quantitystep',$(this).data('quantitystep'));
                
                if (bulkpricing != undefined && bulkpricing.length != 0){
                        var bulk_quant = cartDetailsObj.cartItems[$(this).data('id')].quantity;
                        if(bulk_quant == 0){
                                $(this).parent().parent().parent().parent().find('.setback').removeClass('setback');
                        }
                        
                for(var i=0;i<bulkpricing.length;i++)
                        {
                                if(bulk_quant<bulkpricing[i]['quantity']){
                                        break;
                                }
                                else{
                                        $(this).parent().parent().parent().parent().find('.setback').removeClass('setback');
                                        $(this).parent().parent().parent().parent().find('.select'+$(this).data("id")+'a'+bulkpricing[i]['quantity']).addClass('setback');
                                }
                        }
            }
                
//              cartDetailsObj.cartItems[$(this).data('id')].unitprice = parseInt($(this).attr('data-price'));
                localStorage.setItem('cart_details',JSON.stringify(cartDetailsObj));
        }else{
                $(".number-spinner input[data-did='"+$(this).data('did')+"']").attr('style', 'border: 0');
                $(".number-spinner button[data-did='"+$(this).data('did')+"']").attr('data-id',$(this).data('id'));
                $(".number-spinner button[data-did='"+$(this).data('did')+"']").attr('data-price',parseInt($(this).data('price')));
                $(".number-spinner input[data-did='"+$(this).data('did')+"']").attr('max',parseInt($(this).data('max')));
                $(".number-spinner input[data-did='"+$(this).data('did')+"']").val('0');
                
                $(".number-spinner button[data-did='"+$(this).data('did')+"']").attr('data-minquantity',$(this).data('minquantity'));
                $(".number-spinner button[data-did='"+$(this).data('did')+"']").attr('data-bulkpricing',$(this).attr('data-bulkpricing'));
                $(".number-spinner button[data-did='"+$(this).data('did')+"']").attr('data-quantitystep',$(this).data('quantitystep'));
                
                $(".accsproduct span[data-did='"+$(this).data('did')+"']").text(parseInt($(this).attr('data-price')));
        }
    }else{
        $(".number-spinner input[data-did='"+$(this).data('did')+"']").attr('style', 'border: 0');
        $(".number-spinner button[data-did='"+$(this).data('did')+"']").attr('data-id',$(this).data('id'));
                $(".number-spinner button[data-did='"+$(this).data('did')+"']").attr('data-price',parseInt($(this).data('price')));
                $(".number-spinner input[data-did='"+$(this).data('did')+"']").attr('max',parseInt($(this).data('max')));
                $(".number-spinner input[data-did='"+$(this).data('did')+"']").val('0');
                
                $(".number-spinner button[data-did='"+$(this).data('did')+"']").attr('data-minquantity',$(this).data('minquantity'));
                $(".number-spinner button[data-did='"+$(this).data('did')+"']").attr('data-bulkpricing',$(this).attr('data-bulkpricing'));
                $(".number-spinner button[data-did='"+$(this).data('did')+"']").attr('data-quantitystep',$(this).data('quantitystep'));
                
                $(".accsproduct span[data-did='"+$(this).data('did')+"']").text(parseInt($(this).attr('data-price')));
    }
    
});

$(document).on('click','#bottomNav',function(){
        $('#loadingModal').modal('show');
        var url = apihost + 'cartdetails/?user_id='+me;
    var newForm = $('<form>', {
        'action': url,
        'method':'post'
    }).append($('<input>', {
        'name': 'cart_details',
        'value': localStorage.getItem('cart_details'),
        'type': 'hidden'
    }));
    newForm.appendTo($('body'));
    newForm.submit();
});

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

$(document).on('click','.radGroup2',function(){
    var pin = $(this).data('pin');
     var add_id = $(this).data('id');
     
    $('#loadingModal').modal('show');
        var formData = {'cart_details': localStorage.getItem('cart_details')};
        console.log(localStorage.getItem('cart_details'));
    $.ajax({
            method: "POST",
            url: apihost+"shippings/isServicable/"+pin,
            data : formData
        }).done(function( msg ) {
        $('#loadingModal').modal('hide');
        if(msg== 'ok'){
            $('.myaddress').removeClass('addressselect');
            $('#address'+add_id).addClass('addressselect');
                
            var pin_val = $('.addressselect').data('pinval');
                        $.ajax({
                    method: "GET",
                    url:  apihost +"shippings/getstate/"+pin_val,
                },'json')
                .done(function(msg){ 
                        msg = eval('('+msg+')');
                        if(msg['state'].trim() == "Delhi" || msg['state'].trim() == "Haryana")                 
                        {
                                $(".taxbanner").removeClass('hidden');          
                        }
                        else{
                                $(".taxbanner").addClass('hidden');
                        }
                });
            
        } else if(msg== 'non_service'){
            $('#message').empty();
            $('#message').append("Location is not serviceable...");
            $('#message').removeClass('hidden');
            $("input:radio").removeAttr("checked");
            $('.myaddress').removeClass('addressselect');
            setTimeout(function() {
                $('#message').addClass('hidden');
            }, 3500);
        } else {
                url = apihost + "cartdetails/?user_id=" + me;
                var newForm = $('<form>', {
                        'action': url,
                        'method':'post'
                    }).append($('<input>', {
                        'name': 'cart_details',
                        'value': localStorage.getItem('cart_details'),
                        'type': 'hidden'
                    }));
                    newForm.appendTo($('body'));
                    newForm.submit();
        }
    });
}); 

$(document).on('click','#showmore',function(){
    $("#showmoreaddressdropdown").toggle();
    if($('#showmore').html() == 'Show More')
    {
        $("#showmore").html('Show Less');
    }
    else if($("#showmore").html() == 'Show Less')
    {
        // $("#showmore").empty();
        $("#showmore").html('Show More');    
    }
});

$(document).on("click", "form input[type=submit]", function() {
    $('#message').empty();
    var name = document.getElementById("name").value.trim();
    var line1 = document.getElementById("line1").value.trim();
    var city = document.getElementById("city").value.trim();
    var phone = document.getElementById("phone").value.trim();
    var state = document.getElementById("state").value.trim();
    var pin = document.getElementById("pin").value.trim();
    var message ="";
    var value = "true";
    if(name == '')
    {
       value = "false";
       message = "Please fill name field!";
    }
    else if(line1 == '')
    {
        value = "false";
        message = "Please fill address field!";
    }
    else if(city == '')
    {
        value = "false";
        message = "Please fill city field!";
    }
    
    else if(phone == '')
    {
        value = "false";
        message = "Please fill contact number field!";    
        
    }

    else if(phone.match(/^\d{10}$/) == null)
    {
        value = "false";
        message = "Invalid contact number";    
    }

    else if(state == '')
    {
        value = "false";
        message = "Please fill state field!";
    }
    else if(pin == '')
    {

        value = "false";
        message = "Please fill pin field!";
    }
    else if(pin.match(/^\d{6}$/) == null)
    {
        value = "false";
        message = "Invalid pin number";    
    }

    if(value == "false")
    {
        $('#message').append(message);
        $('#message').removeClass('hidden');
        setTimeout(function() {
                    $('#message').addClass('hidden');
                }, 2000);  
        return false;
    }
    else{

        $("input[type=submit]", $(this).parents("form")).removeAttr("clicked");
        $(this).attr("clicked", "true");
    }
        
});

$(document).on('submit', '#form2', function(e){
        var address_id = $('p#confirmOrderText').attr('addressId');
        $('input.submit_check').prop( "disabled", true );
        $('#loadingModal').modal('show');
        var formData = {
                'name'              : $('#name').val().trim(),
                'line1'             : $('#line1').val().trim(),
                'city'              : $('#city').val().trim(),
                'phone'             : $('#phone').val().trim(),
                'state'             : $('#state').val().trim(),
                'pin'               : $('#pin').val().trim(),   
                'cart_details'          : localStorage.getItem('cart_details')
            };
            
            $.ajax(
            {
                method: "POST",
                url:  apihost + "shippings/submitcheck",
                data: formData,
            },'json')
            .done(function(msg){
                var url = apihost + 'shippings/';
                if(msg=='cart_redirect'){    
                    url = apihost + "cartdetails/?user_id=" + me;
                }
                var newForm = $('<form>', {
                'action': url,
                'method':'post'
            }).append($('<input>', {
                'name': 'cart_details',
                'value': localStorage.getItem('cart_details'),
                'type': 'hidden'
            }));
            newForm.appendTo($('body'));
            newForm.submit();
            });
            return false;
});

function confirmPrepaid(element){
        $('#loadingModal').modal('show');
        addressid=$("input.css-checkbox:checked").val();
        if (addressid==undefined || typeof addressid=="undefined"){
                $('#loadingModal').modal('hide');
                $('#message').empty();
        $('#message').append("Please select an address...");
        $('#message').removeClass('hidden');
        setTimeout(function() {
            $('#message').addClass('hidden');
        }, 2000);
        element.addClass('confirmprepaid').removeClass('confirmprepaidDisabled');
        return;
        }
        element.addClass('confirmprepaid').removeClass('confirmprepaidDisabled');
        
        var url = apihost + 'checkout/?user_id='+me+'&cod=0';
    var newForm = $('<form>', {
        'action': url,
        'method':'post'
    }).append($('<input>', {
        'name': 'addressid',
        'value': addressid,
        'type': 'hidden'
    })).append($('<input>', {
        'name': 'paymethod',
        'value': 'oth',
        'type': 'hidden'
    })).append($('<input>', {
        'name': 'walletUsed',
        'value': walletUsed,
        'type': 'hidden'
    })).append($('<input>', {
        'name': 'cart_details',
        'value': localStorage.getItem('cart_details'),
        'type': 'hidden'
    }));
    newForm.appendTo($('body'));
    newForm.submit();
}

function confirmCheckout(addressid){
        var url = apihost + 'checkout/?user_id='+me+'&cod=1';
        if (typeof addressid=="undefined"){
                addressid=$("input.css-checkbox:checked").val();
        }
        if (addressid==undefined || typeof addressid=="undefined"){
                $('#loadingModal').modal('hide');
                $('#message').empty();
        $('#message').append("Please select an address...");
        $('#message').removeClass('hidden');
        setTimeout(function() {
            $('#message').addClass('hidden');
        }, 2000);
        $('button.confirmcheckout').prop( "disabled", false );
        return;
        }
        
    var newForm = $('<form>', {
        'action': url,
        'method':'post'
    }).append($('<input>', {
        'name': 'addressid',
        'value': addressid,
        'type': 'hidden'
    })).append($('<input>', {
        'name': 'walletUsed',
        'value': walletUsed,
        'type': 'hidden'
    }));
    newForm.appendTo($('body'));
    newForm.submit();
}

/*function confirmWalletCheckout(addressid){
        if (typeof addressid=="undefined"){
                addressid=$("input.css-checkbox:checked").val();
        }
        if (addressid==undefined || typeof addressid=="undefined"){
                $('#loadingModal').modal('hide');
                $('#message').empty();
        $('#message').append("Please select an address...");
        $('#message').removeClass('hidden');
        setTimeout(function() {
            $('#message').addClass('hidden');
        }, 2000);
        $('button.confirmwallet').prop( "disabled", false );
        return;
        }
        
        var url = apihost + 'checkout/?user_id='+me+'&cod=0';
    var newForm = $('<form>', {
        'action': url,
        'method':'post'
    }).append($('<input>', {
        'name': 'addressid',
        'value': addressid,
        'type': 'hidden'
    })).append($('<input>', {
        'name': 'walletUsed',
        'value': totalPayable,
        'type': 'hidden'
    })).append($('<input>', {
        'name': 'cart_details',
        'value': localStorage.getItem('cart_details'),
        'type': 'hidden'
    }));
    newForm.submit();
}*/

function handleConfirmOrder(checkoutType, addressId){
        if (typeof addressId=="undefined"){
                addressId=$("input.css-checkbox:checked").val();
        }
        if (addressId==undefined || typeof addressId=="undefined"){
                $('#loadingModal').modal('hide');
                $('#message').empty();
        $('#message').append("Please select an address...");
        $('#message').removeClass('hidden');
        setTimeout(function() {
            $('#message').addClass('hidden');
        }, 2000);
        $('button.confirmcheckout').prop( "disabled", false );
        $('button.confirmwallet').prop( "disabled", false );
        $('button.confirmcod').prop( "disabled", false );
        return;
        }
        $('button.confirmwallet').prop( "disabled", false );
        $('button.confirmcod').prop( "disabled", false );
        window.history.pushState('forward', null, './');
        if(checkoutType=='codoption'){
                $('p#confirmOrderText').html("<span>Confirm Order with <span style='color:#ff0000;'>COD</span>."
                                +"</span><hr style='margin-top: 5px;margin-bottom: 5px;'>"
                                +"<span><span style='color:#ff0000'>सीओडी (COD)</span> के साथ आर्डर  कन्फर्म करें। </span>");
        }else{
                $('p#confirmOrderText').html("<span>Confirm Order with <span style='color:#ff0000;'>WALLET</span>."
                                +"</span><hr style='margin-top: 5px;margin-bottom: 5px;'>"
                                +"<span><span style='color:#ff0000'>वॉलेट(WALLET)</span> के साथ आर्डर  कन्फर्म करें। </span>");
        }
        $('p#confirmOrderText').attr('addressId',addressId);
        $('p#confirmOrderText').attr('checkoutType',checkoutType);
        $('#myModal').modal('show');
        $("#myModal").on('hide.bs.modal', function(){
                window.history.back();
        })
        $('#loadingModal').modal('hide');
}

$(document).on('click','#changeOption',function(){
        $('#myModal').modal('hide');
});

$(document).on('click','#orderNow',function(){
        $("#cartloader").removeClass('hidden');
        //setTimeout(function () {history.back();}, 5000);
        var checkout_type = $('p#confirmOrderText').attr('checkoutType');
        if(checkout_type=='codoption'){
                var url = apihost + 'checkout/?user_id='+me+'&cod=1';
                var newForm = $('<form>', {
                'action': url,
                'method':'post'
            }).append($('<input>', {
                'name': 'addressid',
                'value': $('p#confirmOrderText').attr('addressId'),
                'type': 'hidden'
            }));
                newForm.appendTo($('body'));
                newForm.submit();
        }else{
                var url = apihost + 'checkout/?user_id='+me+'&cod=0';
            var newForm = $('<form>', {
                'action': url,
                'method':'post'
            }).append($('<input>', {
                'name': 'addressid',
                'value': $('p#confirmOrderText').attr('addressId'),
                'type': 'hidden'
            })).append($('<input>', {
                'name': 'walletUsed',
                'value': totalPayable,
                'type': 'hidden'
            })).append($('<input>', {
                'name': 'cart_details',
                'value': localStorage.getItem('cart_details'),
                'type': 'hidden'
            }));
            newForm.appendTo($('body'));
            newForm.submit();
        }
});

$(document).on('click','.confirmprepaid', function(){
        $(this).addClass('confirmprepaidDisabled').removeClass('confirmprepaid');
        confirmPrepaid($(this));
});
$(document).on('click','button.confirmcheckout', function(){
        $('button.confirmcheckout').prop( "disabled", true );
        $('#loadingModal').modal('show');
        confirmCheckout();
});
$(document).on('click','button.confirmwallet', function(){
        $('button.confirmwallet').prop( "disabled", true );
        $('#loadingModal').modal('show');
        handleConfirmOrder('walletoption');
});
$(document).on('click','button.confirmcod', function(){
        $('button.confirmcod').prop( "disabled", true );
        $('#loadingModal').modal('show');
        handleConfirmOrder('codoption');
});
$(document).on('click','a.usewallet', function(){
        var text = $(this).html().trim();
        var that = this; 
        if(text==="Use Wallet") {
                walletUsed = walletPayable;
                $('#walletAmount').animate({"padding-left":"43px",color:"#ff0000"}, {complete:function(){
                        $(that).html("Don't use wallet");
                        curText=$(this).html();
                        $(this).html("-" + curText.substr(1,curText.indexOf(")")-1));
                        $("#netpayable").css("visibility","visible");
                }});
        } else {
                walletUsed = 0;
                $('#walletAmount').animate({"padding-left":"0px", "color":"#555555"}, {complete:function(){
                        $(that).html("Use Wallet");
                        curText=$(this).html();
                        $(this).html("(" + curText.substr(1,curText.length-1) + ")");
                        $("#netpayable").css("visibility","hidden");
                }});
        }
});