var cartlist = new Array();
var proplist = new Array();
var mainurl = '';
for(var i=0; i<document.scripts.length; i++){
    if(document.scripts[i].src.indexOf('cart.js')>0)
        mainurl = document.scripts[i].src.replace('script/cart.js', '');
}

function getCookie(sName){
    var aCookie = document.cookie.split("; ");
    for (var i=0; i < aCookie.length; i++){
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0] && aCrumb.length > 1) return unescape(aCrumb[1]);
    }
    return null;
}

function getProductList(){
    var sl = getCookie('mycart');
    if(sl!=null){
        cartlist = sl.split('|');
        for(var i=0; i<cartlist.length; i++) cartlist[i] = cartlist[i].split(',');
    }
}

function setProductList(){
    var s = '';
    if(cartlist!=null){
        for(var i=0; i<cartlist.length; i++){
            if(cartlist[i][1]==0) continue;
            var t = cartlist[i].toString();
            if(s=='') s += t;
            else s += '|' + t;
        }
    }
    if(s=='') s='|';
    document.cookie = 'mycart=' + s + '; path=/; ';
}

function addCart(pid, pcount, price, show, memo){
    if (!price) price = 0;
    if (!memo) memo='';
    getProductList();
    var flag = false;
    for(var i=0; i<cartlist.length; i++){
        if(cartlist[i][0]==pid && cartlist[i][3]==memo){
            if(pid>0){
                cartlist[i][1] = Math.floor(cartlist[i][1]) + Math.floor(pcount);
                cartlist[i][2] = price;
                cartlist[i][3] = encodeURI(memo);
            }
            flag = true;
            break;
        }
    }
    if(cartlist.length<=0 && pid<0) flag=true;
    if(!flag) cartlist.push(new Array(pid, pcount, price, memo));
    setProductList();
    var lang = getCookie('language');
    var carturl = 'cart_list.aspx';
    if (lang && lang!='') carturl = 'cart_list_' + lang + '.aspx';
    if (show) window.open(mainurl+carturl+'?seed='+(new Date()).getTime(), '_self');
}

function modCart(pid, pcount, psize){
    if(Math.floor(pcount)<=0){
        alert('请输入一个正的数字！');
        return;
    }
    getProductList();
    for(var i=0; i<cartlist.length; i++){
        if(cartlist[i][0]==pid){
            cartlist[i][1] = pcount;
            cartlist[i][3] = psize;
            break;
        }
    }
    setProductList();
}

function updateCart(){
    getProductList();
    for(var i=0; i<cartlist.length; i++){
        var obj = $('cnt_'+i);
        if(obj && cartlist[i][1]!=obj.value)
            cartlist[i][1] = obj.value;
        obj = $('m_'+i);
        if(obj && cartlist[i][3]!=obj.value)
            cartlist[i][3] = obj.value;
    }
    setProductList();
    location.reload();
}

function delCart(pid){
    getProductList();
    if(pid<cartlist.length)
        cartlist.splice(pid, 1);
    setProductList();
    location.reload();
}

function clearCart(){
    getProductList();
    cartlist.splice(0, cartlist.length);
    setProductList();
}

function getCartCount(){
    getProductList();
    var res = 0;
    for(var i=0; i<cartlist.length; i++){
        res = res + Math.floor(cartlist[i][1]);
    }
    return res;
}

function getCartPrice(){
    getProductList();
    var res = 0;
    for(var i=0; i<cartlist.length; i++){
        res = res + Math.floor(cartlist[i][2])*Math.floor(cartlist[i][1]);
    }
    return res;
}

function addToCart(id, price){
    var obj = $('cnt_'+id);
    var cnt = obj ? parseInt(obj.value, 10) : 1;
    var memo;
    if(id>0){
        if(proplist.length>0){
            memo = new Array();
            for(var i=0; i<proplist.length; i++){
                obj = $(proplist[i][0]+id);
                var one = obj ? proplist[i][1]+obj.value : '';
                memo.push(one);
            }
            if(memo.length>0)memo=memo.join('/');
            else memo='';
        }
        try{
            var ep = eval('EP_'+id);
            if(!ep) return;
            if(!ep.isValid()) return;
            memo = ep.getMemo();
        }
        catch (e){
            memo = '';
        }
    }
    else memo='gift';
    addCart(id, cnt, price, true, memo);
}

/*
    Class for extend property products
    2009.03.30
*/
function ExtendProperty(_id){
    this.ID = _id;
    // DCOMid、DCOMtype、标题、必填、提示
    this.Property = new Array();
    this.isValid = function(){
        for (var i=0; i<this.Property.length; i++){
            if (this.Property[i][3]==1){
                var obj = $(this.Property[i][0]);
                if (obj && obj.value==''){
                    alert(this.Property[i][4]);
                    obj.focus();
                    return false;
                }
            }
        }
        return true;
    }
    this.getMemo = function(){
        var memo = new Array();
        for (var i=0; i<this.Property.length; i++){
            var obj = $(this.Property[i][0]);
            if (obj && obj.value!='') memo.push(this.Property[i][2] + ':' + obj.value);
        }
        return memo.join('/');
    }
}