$(document).ready(function(){
    

//View Catalogues..
    getCatalogue();
    function getCatalogue(){
        $.ajax({
            url: "https://d101sale.com/functions/php/functions.php",
            method: "POST",
            data: {viewCatalogue:1},
            success: function(data){
                $(".view_catalogues").html(data);
            }
        })
    } 

//View Products..
    getProduct();
    function getProduct(){
        $.ajax({
            url: "https://d101sale.com/functions/php/functions.php",
            method: "POST",
            data: {viewProduct:1},
            success: function(data){
                $(".view_products").html(data);
            }
        })
    } 
    

//View Products By Catalogue..
    $("body").delegate("#products_by_catalogue","click",function(event){
        var cid = $(this).attr("cid");
        event.preventDefault();
        $.ajax({
            url : "https://d101sale.com/functions/php/functions.php",
            method : "POST",
            data : {viewProductByCatalogue:1,catId:cid},
            success : function(data){
                $(".view_products_by_catalogue").html(data);
            }
        })
    })
    

//Single product detail..
    $("body").delegate("#product_detail","click",function(event){
        var pid = $(this).attr("pid");
        event.preventDefault();
        $.ajax({
            url : "https://d101sale.com/functions/php/functions.php",
            method : "POST",
            data : {productDetail:1,proId:pid},
            success : function(data){
                $('.product_detail').html(data);
            }
        })
    })
    

    
//Add Product into Cart
    $("body").delegate("#add_to_cart","click",function(event){
        var pid = $(this).attr("pid");
        event.preventDefault();
        $.ajax({
            url : "https://d101sale.com/functions/php/functions.php",
            method : "POST",
            data : {addToCart:1,proId:pid},
            success : function(data){
                count_item();
                $('#promsg').html(data);
            }
        })
    })
  
    $("body").delegate("#add_detail_cart","click",function(event){
        var pid = $(this).attr("pid");
        event.preventDefault();
        $.ajax({
            url : "https://d101sale.com/functions/php/functions.php",
            method : "POST",
            data : {addDetailCart:1,proId:pid},
            success : function(data){
                count_item();
                $('#detmsg').html(data);
            }
        })
    })
    
    
    
    
    
//Place order..
    $("body").delegate("#order_cart","click",function(event){
        var pid = $(this).attr("pid");
        event.preventDefault();
        $.ajax({
            url : "https://d101sale.com/functions/php/functions.php",
            method : "POST",
            data : {orderCart:1,userId:pid},
            success : function(data){
                count_item();
                $('#msg').html(data);
            }
        })
    })
    
    

//Total Deposit Amount
    tot_deposit();
    function tot_deposit(){
        $.ajax({
            url : "https://d101sale.com/functions/php/functions.php",
            method : "POST",
            data : {deposit:1},
            success : function(data){
                $(".deposit").html(data);
            }
        })
    }
//Latest Deposit Amount
    latestDeposit();
    function latestDeposit(){
        $.ajax({
            url : "https://d101sale.com/functions/php/functions.php",
            method : "POST",
            data : {latest_deposit:1},
            success : function(data){
                $(".latest_deposit").html(data);
            }
        })
    }
  
  

//Count user cart items funtion
    count_item();
    function count_item(){
        $.ajax({
            url : "https://d101sale.com/functions/php/functions.php",
            method : "POST",
            data : {count_item:1},
            success : function(data){
                $(".chosen").html(data);
            }
        })
    }
    count_processing();
    function count_processing(){
        $.ajax({
            url : "https://d101sale.com/functions/php/functions.php",
            method : "POST",
            data : {count_processing:1},
            success : function(data){
                $(".processing").html(data);
            }
        })
    }
    count_processed();
    function count_processed(){
        $.ajax({
            url : "https://d101sale.com/functions/php/functions.php",
            method : "POST",
            data : {count_processed:1},
            success : function(data){
                $(".processed").html(data);
            }
        })
    }



    
//Fetch Cart item from Database
    getCart();
    function getCart(){
        $.ajax({
            url : "https://d101sale.com/functions/php/functions.php",
            method : "POST",
            data : {Common:1,getCartItem:1},
            success : function(data){
                $(".get_cart").html(data);
                count_item();
            }
        })
    }
    
    getOrderCart();
    function getOrderCart(){
        $.ajax({
            url : "https://d101sale.com/functions/php/functions.php",
            method : "POST",
            data : {Common:1,getOrderItem:1},
            success : function(data){
                $(".get_order_cart").html(data);
                count_processing();
                getOrderCart();
                count_item();
            }
        })
    }
  
    getOrderedCart();
    function getOrderedCart(){
        $.ajax({
            url : "https://d101sale.com/functions/php/functions.php",
            method : "POST",
            data : {Common:1,getOrderedItem:1},
            success : function(data){
                $(".get_ordered_cart").html(data);
                count_processing();
                count_processed();
                getOrderedCart();
                count_item();
            }
        })
    }
    
    
    
    
    

//Update Cart Product.. 
    $("body").delegate(".update","click",function(event){
        var update = $(this).parent().parent().parent();
        var update_id = update.find(".update").attr("update_id");
        var qty = update.find(".qty").val();
        var mail = update.find(".mail").val();
        var coupon = update.find(".coupon").val();
        $.ajax({
            url : "https://d101sale.com/functions/php/functions.php",
            method : "POST",
            data : {updateCartItem:1,update_id:update_id,qty:qty,mail:mail,coupon:coupon},
            success : function(data){
                $(".cart_msg").html(data);
                checkOutDetail();
            }
        })
    })
    
    
    
    
    

//Remove product from cart..    
    $("body").delegate(".remove","click",function(event){
        var remove = $(this).parent().parent().parent();
        var remove_id = remove.find(".remove").attr("remove_id");
        $.ajax({
            url :   "https://d101sale.com/functions/php/functions.php",
            method  :   "POST",
            data    :   {removeItemFromCart:1,rid:remove_id},
            success :   function(data){
                $(".cart_msg").html(data);
                $(".msg").html(data);
                checkOutDetail();
                count_item();
                count_processing();
                count_processed();
                getOrderCart();
            }
        })
    })
    
    





    checkOutDetail();
    net_total();
    function checkOutDetail(){
        $.ajax({
            url : "https://d101sale.com/functions/php/functions.php",
            method : "POST",
            data : {Common:1,checkOutDetails:1},
            success : function(data){
                $(".cart_checkout").html(data);
                net_total();
            }
        })
    }
    
    
    
    

//Keyup function..
    $("body").delegate(".qty","keyup",function(event){
        event.preventDefault();
        var row = $(this).parent().parent();
        var price = row.find('.price').val();
        var ship = row.find('.ship').val();
        var qty = row.find('.qty').val();
        if (isNaN(qty)) {qty = 1;};
        if (qty < 1) {qty = 1;};
        var total = price * qty;
        row.find('.total').val(total);
        var net_total=0;
        $('.total').each(function(){
            net_total += ($(this).val()-0);
        })
        $('.net_total').html("Total : $" +net_total);
    })
    
    function net_total(){
        var net_total = 0;
        $('.qty').each(function(){
            var row = $(this).parent().parent();
            var price  = row.find('.price').val();
            var ship  = row.find('.ship').val();
            var total = price * $(this).val()-0;
            var ttotal = ship + $(this).val()-0;
            var ntotal = total+ttotal;
            row.find('.total').val(ntotal);
        });
        $('.total').each(function(){
            net_total += ($(this).val()-0);
        });
        $('.net_total').html("Total : $" +net_total);
    }



    






//Web User Register..
    $("#user_register_form").on("submit",function(event){
        event.preventDefault();
        $.ajax({
            url : "https://d101sale.com/functions/php/register.php",
            method : "POST",
            data : $("#user_register_form").serialize(),
            success : function(data){
                if (data == "Registered successfully!") {
                    window.location.href = "https://d101sale.com/login";
                }else if (data == "Email already exists!"){
                    $("#msg").html("<div class='alert alert-warning'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a><b> "+data+" </b></div>");
                }else{
                    $("#msg").html(data);
                }
            }
        })
    })
    

    

    
//Login..
    $("#login_form").on("submit",function(event){
        event.preventDefault();
        $.ajax({
            url : "https://d101sale.com/functions/php/login.php",
            method : "POST",
            data : $("#login_form").serialize(),
            success : function(data){
                if(data == "login_success"){
                    window.location.href = 'https://d101sale.com';
                }else if(data == "cart_login"){
                    window.location.href = 'https://d101sale.com/cart';
                }else{
                    $("#msg").html(data);
                }
            }
        })
    })





})