﻿$(function() {
    var num = parseInt(ShopCartSum());
    if (num > 0) {
        if (num > 1) {
            $("#CartItem").html("Shipping cart(" + num + ")");
        }
        else {
            $("#CartItem").html("Shipping cart(" + num + ")");
        }
    }
})
function ShopCartItem() { 
    var shopcart = GetShopCart();
    var nowCur = getCurrcy().split(",");
    var items = "<div style='text-align:center'>Your Shopping Cart Is Empty.</div>";
    if (shopcart.ProductId != "") {
        items = "";
        var idarr = shopcart.ProductId.split("|");
        var namearr = shopcart.ProductName.split("|");
        var photo = shopcart.Smallphoto.split("|");
        var price = shopcart.Price.split("|");
        var num = shopcart.ProductNum.split("|");
        var size = shopcart.Size.split("|");       
        var reg = /[\s]{1,}/gi;
        var link = "";
        for (var i = 0; i < idarr.length - 1; i++) {  
            items += "<ul class=\"cart_content\" id='" + idarr[i] + "'><li class=\"c1\">";
            items += "<img src=\"ProductImg/" + photo[i].replace("1danyinhao1", "'") + "\" /></li>"
                link = namearr[i].replace(reg, "-").replace(",", "-").replace("1danyinhao1", "'").replace("(", "-").replace(")", "-");
                items += "<li class=\"c2\"><h4 class=\"title\"><a href=\"p-" + link + "-" + idarr[i] + ".html\">" + namearr[i].replace("1danyinhao1", "'") + "</a></h4>";
            items += "<p>Size: " + size[i] + "</p></li><li class=\"c3\"><input class=\"input-w2\" type=\"text\" proid='" + idarr[i] + "' value='" + num[i] + "' />";
            items += "<input type=\"button\" id='updatecart' class='btn_cart' value=\"Update\" onclick=\"UpdateNum('" + idarr[i] + "','" + num[i] + "','1','"+size[i]+"')\"  /></li>";
            items += "<li class=\"c4\"><font name='price' class=\"price\" id='p" + idarr[i] + "'> " + nowCur[0] + " " +(parseFloat(nowCur[1])*parseFloat( price[i])*num[i]).toFixed(2) + "</font></li>";
            items += "<li class=\"c5\"><input type=\"button\"  class='btn_cart'  value=\"Delete\" onclick=\"DelProduct('" + idarr[i] + "')\"  /></li><div class='clear'></div></ul>";
            
        }
      
    }
    $("div.Shopping-body").empty();
    $("div.Shopping-body").append(items);   
    $("#total").html(nowCur[0] + (parseFloat(shopcart.TotalPrice) * parseFloat(nowCur[1])).toFixed(2));
}

function ShopCartItemPay() { 
    var shopcart = GetShopCart();
    var nowCur = getCurrcy().split(",");
    var items = "<div style='text-align:center'>Your Shopping Cart Is Empty.</div>"; 
    if (shopcart.ProductId != "") {
        items = "";
        var idarr = shopcart.ProductId.split("|");
        var namearr = shopcart.ProductName.split("|");
        var photo = shopcart.Smallphoto.split("|");
        var price = shopcart.Price.split("|");
        var num = shopcart.ProductNum.split("|");
        var size = shopcart.Size.split("|");       
        var reg = /[\s]{1,}/gi;
        var link = "";
        for (var i = 0; i < idarr.length - 1; i++) {
            items += "<ul class=\"cart_cpay\" id='" + idarr[i] + "'>"
             link = namearr[i].replace(reg, "-").replace(",", "-").replace("1danyinhao1", "'").replace("(", "-").replace(")", "-");
            items += "<li class=\"w1\"><h4 class=\"title\"><a href=\"p-" + link + "-" + idarr[i] + ".html\"  style='font-size:11px;color:#450000' >" +namearr[i].replace("1danyinhao1", "'")  + "</a></h4>";
            items += "<p>Size: " + size[i] + "</p></li><li class=\"w2\">" + price[i] + "</li><li class=\"w3\">" + num[i] + "";
            items += "</li>";
            items += "<li class=\"w4\"><font name='price' class=\"price\" id='p" + idarr[i] + "'> " + nowCur[0] + " " +(parseFloat(nowCur[1])*parseFloat( price[i])*num[i]).toFixed(2) + "</font></li>";
            items += "<div class='clear'></div></ul>";
            
        }
      
    }
    $("div.Shopping-body").empty();
    $("div.Shopping-body").append(items);   
    $("#total").html(nowCur[0] + (parseFloat(shopcart.TotalPrice) * parseFloat(nowCur[1])).toFixed(2));
    $("#price_total").html(nowCur[0] + (parseFloat(shopcart.TotalPrice) * parseFloat(nowCur[1])).toFixed(2));
}


function ShopCartView(coupprice,sendtype,sendprice) {
 var shopcart = GetShopCart();
 if (shopcart.ProductId != "") {
     var nowCur = getCurrcy().split(",");
     var Symbol = nowCur[0];
     var rate = parseFloat(nowCur[1]);
     var items = "";
     var idarr = shopcart.ProductId.split("|");
     var namearr = shopcart.ProductName.split("|");
     var photo = shopcart.Smallphoto.split("|");
     var price = shopcart.Price.split("|");
     var num = shopcart.ProductNum.split("|");
     var size = shopcart.Size.split("|");
     var reg = /[\s]{1,}/gi;
     var totalprice = (parseFloat(shopcart.TotalPrice) * parseFloat(rate)).toFixed(2);
     sendprice = (rate * parseFloat(sendprice)).toFixed(2);
     coupprice = (rate * parseFloat(coupprice)).toFixed(2);
     var realprice = (parseFloat(totalprice) + parseFloat(sendprice) - parseFloat(coupprice)).toFixed(2);
     for (var i = 0; i < idarr.length - 1; i++) {
         items += "<ul class=\"Shopping-header\">";
         items += "<li class=\"w1\"><h4 class=\"title\">" + namearr[i] + "</h4>";
         items += "<p>Size: " + size[i] + "</p></li>";
         items += "<li class=\"w2\"><font name='price' class=\"price\" > " + Symbol + " " + (rate * parseFloat(price[i])).toFixed(2) + "</font></li>";
         items += "<li class=\"w3\">" + num[i] + "";

         items += "<li class=\"w4\"><font name='price' class='price'>" + Symbol + " " + (rate * parseFloat(price[i]) * parseFloat(num[i])).toFixed(2) + "</font></li></ul>";
     }
     items += "<ul class=\"Shopping-header\"><div class=\"Shopping-footer02\">";
     items += "<p> Subtotal&nbsp;&nbsp;&nbsp; <font class='price' name='price'>" + Symbol + " " + totalprice + "</font></p>";
     items += "<p>Shipping &amp; Handling (" + sendtype + ") &nbsp;&nbsp;&nbsp;<font name='price' class='price'>" + Symbol + " " + sendprice + "</font></p>";
     items += "<p>Discount Amount &nbsp;&nbsp;&nbsp;<font name='price' class='price'>$ " + coupprice + "</font></p>";
     items += "<p><strong> Total 	<font name='price' class='price'>" + Symbol + " " + realprice + "</font></strong> </p></div></ul>";
     $("div.Shopping-body").empty();
     $("div.Shopping-body").append(items);
 }
 else {
     alert("Your Shopping Cart Is Empty");location.href = "Index.html";
 }
}

function ShopCartObj() {
    var shopcart = { ProductId: '', ProductName: '', Smallphoto: '', Price: '', ProductNum: '', TotalPrice: '', Size: '' };
    return shopcart;
}

function GetShopCart() {
    var shopcart = ShopCartObj();
    var cookie = GetCookie("shopcart"); 
    if (cookie != null) {
        var value = cookie.toString().substr(cookie.toString().indexOf("=") + 1);
        var values = value.split("&");
        var total = 0;
        var numarr = [];
        var pricearr = [];
        var separate = "||";
        shopcart.ProductId = values[0].split(separate)[1];
        shopcart.ProductName = values[1].split(separate)[1];
        shopcart.Smallphoto = values[2].split(separate)[1];
        shopcart.Price = values[3].split(separate)[1];
        shopcart.ProductNum = values[4].split(separate)[1];
        shopcart.TotalPrice = values[5].split(separate)[1];
        shopcart.Size = values[6].split(separate)[1];
        numarr = shopcart.ProductNum.split("|");
        pricearr = shopcart.Price.split("|");
        for (var i = 0; i < numarr.length - 1; i++) {
            total += parseInt(numarr[i]) * parseFloat(pricearr[i]);
        }
        shopcart.TotalPrice = total;
    }
    SaveToCookie(shopcart); 
    return shopcart;
}

function AddToShopCart(proid, name, photo, price) { 
    var num = 0, size = "";
    num = $("#txtqty").val();
    size = $("#ddlsize").val();
    var reg = /^[0-9]*$/;
    if (!reg.test(num)) {
        alert("required number"); return;
    }
   // price = (parseFloat(price) * parseFloat(getCurrcy().split(',')[1])).toFixed(2);alert(price);
    var shopcart = GetShopCart();
    if ((shopcart.ProductId.indexOf(proid) > -1)&&(shopcart.Size.indexOf(size) > -1)) { 
        UpdateNum(proid, num, "2",size);
    }
    else {
        shopcart.ProductId += proid + "|";
        shopcart.ProductName += name + "|";
        shopcart.Smallphoto += photo + "|";
        shopcart.Price += price + "|";
        shopcart.ProductNum += num + "|";
        shopcart.Size += size + "|";
        shopcart.TotalPrice = parseFloat(shopcart.TotalPrice) + parseFloat(price) * parseInt(num);
        SaveToCookie(shopcart);
    }
    location.href = "cart.html";
}

function UpdateNum(proid, num, mode,size) {
    var reg = /^[0-9]*$/;
    if (!reg.test(num.toString())) {
        alert("required number"); return;
    }
    if (parseInt(num) <= 0) {
        alert("required number that lager than 0"); return;
    }
    var shopcart = GetShopCart();
    var idarr = shopcart.ProductId.split("|");
    var numarr = shopcart.ProductNum.split("|");
    var pricearr = shopcart.Price.split("|");
    var sizerr = shopcart.Size.split("|");
    var addnum = 0; 
    var addprice = 0; 
    idarr.pop();
    numarr.pop();
    var nowCur = getCurrcy().split(',');
    for (var i = 0; i < idarr.length; i++) {
        if (idarr[i] == proid&&sizerr[i]==size) {
            if (mode == "1") {
                num = $("input[proid=" + proid + "]").val();
                if (!reg.test(num)) {
                    alert("required number"); return;
                }
                addnum = parseInt(num) - parseInt(numarr[i]);
                numarr[i] = num;
                var price = parseInt(num) * (parseFloat(nowCur[1]) * parseFloat(pricearr[i]));                
                price = price.toFixed(2);                
                $("font[id=p" + proid + "]").html(nowCur[0] + " " + price);

            }
            else {
                addnum = num;
                numarr[i] = parseInt(numarr[i]) + parseInt(num);
            }
            addprice = parseInt(addnum) * parseFloat(pricearr[i]);
            break;
        }
    }
    shopcart.ProductNum = numarr.join("|") + "|";
    shopcart.TotalPrice = parseFloat(shopcart.TotalPrice) + parseFloat(addprice);    
    $("#total").html(nowCur[0]+" "+(parseFloat(nowCur[1])*parseFloat( shopcart.TotalPrice)).toFixed(2));
    SaveToCookie(shopcart);
}

function SaveToCookie(obj) {   
    var exp = new Date();
    exp.setTime(exp.getTime() + 24 * 60 * 60 * 1000);
    var value = "shopcart=";
    value += "ProductId||" + obj.ProductId;
    value += "&ProductName||" + obj.ProductName;
    value += "&Smallphoto||" + obj.Smallphoto;
    value += "&Price||" + obj.Price;
    value += "&ProductNum||" + obj.ProductNum;
    value += "&TotalPrice||" + obj.TotalPrice;
    value += "&Size||" + obj.Size;
    document.cookie = value + ";expires=" + exp.toGMTString()+";path=/";
}

function ShopCartSum() {
    var shopcart = GetShopCart();
    var totalnum = 0;
    var numArr = shopcart.ProductNum.split("|");
    for (var i = 0; i < numArr.length - 1; i++) {
        totalnum += parseInt(numArr[i]);
    }
    return totalnum;
}

function CartChangeCur() {
    var shopcart = GetShopCart();
}

function DelProduct(proid) {
    var separate = "|";
    var shopcart = GetShopCart();
    var arr = [];
    var index = 0;
    var num = 0;
    var price = 0;
    arr = shopcart.ProductId.split(separate);
    arr.pop();
    for (index = 0; index < arr.length; index++) {
        if (arr[index] == proid) {
            if (arr.length == 1) {
                shopcart = ShopCartObj();
                break;
            }
            arr.splice(index, 1);
            shopcart.ProductId = arr.join(separate) + separate;
            arr = shopcart.ProductName.split(separate);
            arr.pop();
            arr.splice(index, 1);
            shopcart.ProductName = arr.join(separate) + separate;
            arr = shopcart.Smallphoto.split(separate);
            arr.pop();
            arr.splice(index, 1);
            shopcart.Smallphoto = arr.join(separate) + separate;
            arr = shopcart.Price.split(separate);
            arr.pop();
            price = arr[index];
            arr.splice(index, 1);
            shopcart.Price = arr.join(separate) + separate;
            arr = shopcart.ProductNum.split(separate);
            arr.pop();
            num = arr[index];
            arr.splice(index, 1);
            shopcart.ProductNum = arr.join(separate) + separate;
            shopcart.TotalPrice = parseFloat(shopcart.TotalPrice) - parseInt(num) * parseFloat(price);
            arr = shopcart.Size.split(separate);
            arr.pop();
            arr.splice(index, 1);
            shopcart.Size = arr.join(separate) + separate;
            break;
        }
    }
    SaveToCookie(shopcart);
    $("ul[id=" + proid + "]").remove();
    ShopCartItem();
}


function GetCookie(name) {
    var cookies = document.cookie.split(";");
    var cookie = "";
    for (var i = 0; i < cookies.length; i++) {
        cookie = cookies[i].split("=");
        if ($.trim(cookie[0]) == name) {
            return cookies[i];
        }
    }
    return null;
}
