﻿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 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 + '; ';
}

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][1] = Math.floor(cartlist[i][1]) + Math.floor(pcount);
			cartlist[i][2] = price;
			cartlist[i][3] = memo;
			flag = true;
			break;
		}
	}
	if(!flag) cartlist.push(new Array(pid, pcount, price, memo));
	setProductList();
	if (show) window.open('cart_list.aspx', '_blank');
}

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++){
        if(cartlist[i][1]!=$('cnt_'+cartlist[i][0]).value)
            cartlist[i][1] = $('cnt_'+cartlist[i][0]).value;
        if(cartlist[i][3]!=$('m_'+cartlist[i][0]).value)
            cartlist[i][3] = $('m_'+cartlist[i][0]).value;
    }
	setProductList();
	location.reload();
}

function delCart(pid){
	getProductList();
	for(var i=0; i<cartlist.length; i++){
		if(cartlist[i][0]==pid){
			cartlist.splice(i, 1);
			break;
		}
	}
	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 = 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='';
    addCart(id, cnt, price, true, memo);
}
