﻿var cartlist = new Array();
var proplist = new Array();

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 (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 openCart(show){
    var lang = getCookie('language');
    var carturl = 'cart_list.aspx';
    if (lang && lang!='') carturl = 'cart_list_' + lang + '.aspx';
var mainurl = '';
var jsCart = document.getElementById('jsCart');
if (jsCart != null) mainurl = jsCart.src.replace('script/cart.js', '');
else{
	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', '');
	}
}
    if (show) window.open(mainurl+carturl+'?seed='+(new Date()).getTime(), '_self');
}

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]==escape(memo)){
            if(pid>0){
                cartlist[i][1] = Math.floor(cartlist[i][1]) + Math.floor(pcount);
                cartlist[i][2] = price;
                //cartlist[i][3] = memo;
            }
            flag = true;
            break;
        }
    }
    if(cartlist.length<=0 && pid<0) flag=true;
    if(!flag) cartlist.push(new Array(pid, pcount, price, escape(memo)));
    setProductList();
    openCart(show);
}

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] = escape(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] = escape(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';
    if (ep && ep.Matrix){
        if (ep.Matrix){
            var show = false;
            for (var i=0; i<ep.Property.length; i++){
                if (ep.Property[i][1] == 3){
                    var mxs = document.getElementsByName(ep.Property[i][0]);
                    for (var ii=0; ii<mxs.length; ii++){
                        if (mxs[ii].value!=''){
                            var mxsv = parseInt(mxs[ii].value, 10);
                            if (!isNaN(mxsv)){
                                var tmpmemo = ep.Property[i][2] + ':' + mxs[ii].title;
                                tmpmemo = memo == '' ? tmpmemo : memo + '/' + tmpmemo;
                                addCart(id, mxsv, price, false, tmpmemo);
                                show = true;
                            }
                        }
                    }
                }
            }
            if (show) openCart(true);
            return;
        }
    }
    addCart(id, cnt, price, true, memo);
}

function goContinue(){
    history.go(-1);
}

/*
    Class for extend property products
    2009.03.30
*/
function ExtendProperty(_id){
    this.ID = _id;
    this.Matrix = false;
    // DCOMid、DCOMtype、标题、必填、提示
    this.Property = new Array();
    this.isValid = function(){
        for (var i=0; i<this.Property.length; i++){
            if (this.Property[i][3]==1){
                if (this.Property[i][1] == 3){
                    // matrix
                    var mxs = document.getElementsByName(this.Property[i][0]);
                    var flag = false;
                    for (var ii=0; ii<mxs.length; ii++){
                        if (mxs[ii].value!=''){
                            flag = true;
                            break;
                        }
                    }
                    if (!flag){
                        alert(this.Property[i][4]);
                        return false;
                    }
                }
                else if (this.Property[i][1] == 2){
                    // checkbox
                    var cbs = document.getElementsByName(this.Property[i][0]);
                    var flag = false;
                    for (var ii=0; ii<cbs.length; ii++){
                        if (cbs[ii].checked){
                            flag = true;
                            break;
                        }
                    }
                    if (!flag){
                        alert(this.Property[i][4]);
                        return false;
                    }
                }
                else{
                    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++){
            if (this.Property[i][1] == 3){
                // matrix
            }
            else if (this.Property[i][1] == 2){
                // checkbox
                var cbs = document.getElementsByName(this.Property[i][0]);
                var cbv = new Array();
                for (var ii=0; ii<cbs.length; ii++){
                    if (cbs[ii].checked){
                        cbv.push(cbs[ii].value);
                    }
                }
                if (cbv.length > 0) memo.push(this.Property[i][2] + ':' + cbv.join(','));
            }
            else{
                var obj = $(this.Property[i][0]);
                if (obj && obj.value!='') memo.push(this.Property[i][2] + ':' + obj.value);
            }
        }
        return memo.join('/');
    }
}