
var VAR_CURRENT_USER = "CURRENT_USER";
var VAR_NOT_LOGIN_USER = "NOT_LOGIN_USER";
var VAR_SEPARATOR_GOODS = "~";
var VAR_SEPARATOR_PARAM = "|";
  	
  	//add to cart
function buy(id, quantity) {
	if(quantity==null || quantity==""){
		alert("Error quantity!");
		return;
	}
	var re = /^[1-9]\d*$/;
	if (!re.test(quantity)) {
		alert("Error quantity!");
		return;
	}
	var userid = getCookie(VAR_CURRENT_USER);
	userid = getCurrentUser(userid);
	if(userid != VAR_NOT_LOGIN_USER){
		clearOtherUserCookie(userid);
	}
	bool = false;
	userid_cart_goods = getCookie(userid);
	if (document.cookie != null) {
		if (document.cookie.length > 3700) {
			alert("too long!");
			return false;
		}
	}
	if (userid_cart_goods == null) {
		setQuantityToCookie(userid, id, quantity);
	} else {
		goods_all = userid_cart_goods.split(VAR_SEPARATOR_GOODS);
		str = "";
		for (var i = 0; i < goods_all.length; i++) {
			goods = goods_all[i].split(VAR_SEPARATOR_PARAM);
			if (id == goods[0]) {
				qty = quantity * 1 + goods[1] * 1;
				str = str + goods[0] + VAR_SEPARATOR_PARAM + qty + VAR_SEPARATOR_GOODS;
				bool = true;
			} else {
				str = str + goods_all[i] + VAR_SEPARATOR_GOODS;
			}
		}
		if (!bool) {
			str = str + id + VAR_SEPARATOR_PARAM + quantity + VAR_SEPARATOR_GOODS;
		}
		setCookie(userid, str.substr(0, str.length - 1));
	}
	flushCart();
}
  	
  	//delete goods in cart
function delCartGoods(id,step) {
	var userid = getCookie(VAR_CURRENT_USER);
	userid = getCurrentUser(userid);
	userid_cart_goods = getCookie(userid);
	goods_all = userid_cart_goods.split(VAR_SEPARATOR_GOODS);
	str = "";
	for (var i = 0; i < goods_all.length; i++) {
		goods = goods_all[i].split(VAR_SEPARATOR_PARAM);
		if (id != goods[0]) {
			str = str + goods_all[i] + VAR_SEPARATOR_GOODS;
		}
	}
	if (str == null || str == "") {
		deleteCartCookie();
	} else {
		setCookie(userid, str.substr(0, str.length - 1));
	}
	if(step==1){
		flushCart();
	}else if(step==2){
		flushSettle();
	}
}
  	
  	//update goods quantity
function updateGoodsQuantity(id, obj,step) {
	quantityError(obj);
	var userid = getCookie(VAR_CURRENT_USER);
	userid = getCurrentUser(userid);
	userid_cart_goods = getCookie(userid);
	goods_all = userid_cart_goods.split(VAR_SEPARATOR_GOODS);
	str = "";
	for (var i = 0; i < goods_all.length; i++) {
		goods = goods_all[i].split(VAR_SEPARATOR_PARAM);
		if (id == goods[0]) {
			str = str + goods[0] + VAR_SEPARATOR_PARAM + obj.value + VAR_SEPARATOR_GOODS;
		} else {
			str = str + goods_all[i] + VAR_SEPARATOR_GOODS;
		}
	}
	setCookie(userid, str.substr(0, str.length - 1));
	if(step==1){
		flushCart();
	}else if(step==2){
		flushSettle();
	}
}
  	
  	//empty cart
function emptyCart() {
	deleteCartCookie();
	flushCart();
}

function flushCart(){
	$('#cart_goods').load('cart.do?action=view&step=1&r='+Math.random());
}

function flushSettle(){
	$('#settle_goods').load('cart.do?action=view&step=2&r='+Math.random());
}	

function mergeCartCookie() {
	var userid = getCookie(VAR_CURRENT_USER);
	userid = getCurrentUser(userid);
	//merge goods in cart.
	mergeCookie(userid);
}

  	//merge cookie
function mergeCookie(userid) {
	if (userid != VAR_NOT_LOGIN_USER) {
  			//not login user cart goods
		not_login_user_cart_goods = getCookie(VAR_NOT_LOGIN_USER);
  			//login user cart goods
		login_user_cart_goods = getCookie(userid);
		str = "";
		if (not_login_user_cart_goods != null) {
			if (login_user_cart_goods == null) {
				setCookie(userid, not_login_user_cart_goods);
			} else {
				goods_all = not_login_user_cart_goods.split(VAR_SEPARATOR_GOODS);
				for (var i = 0; i < goods_all.length; i++) {
					goods = goods_all[i].split(VAR_SEPARATOR_PARAM);
					if (login_user_cart_goods.indexOf(goods[0]) == -1) {
						str = str + goods_all[i] + VAR_SEPARATOR_GOODS;
					}
				}
				if (str != "") {
					setCookie(userid, login_user_cart_goods + VAR_SEPARATOR_GOODS + str.substr(0, str.length - 1));
				}
			}
			deleteCartCookieByName(VAR_NOT_LOGIN_USER);
		}
	}
}
  	
  	//add goods in shopping list to cart
function addAllGoodsToCart(goodsIdList) {
	var goodslist = goodsIdList.split(",");
	for (var i = 0; i < goodslist.length; i++) {
		buy(goodslist[i], 1);
	}
}

function getCartCookie() {
	var userid = getCookie(VAR_CURRENT_USER);
	if (userid == null || userid == "") {
		userid = VAR_NOT_LOGIN_USER;
	}
	return getCookie(userid);
}
function getCurrentUser(userid) {
	if (userid == null || userid == "") {
		return VAR_NOT_LOGIN_USER;
	}
	return userid;
}
function getCookie(name) {
	var arrStr = document.cookie.split("; ");
	for (var i = 0; i < arrStr.length; i++) {
		var str = arrStr[i];
		var temp = str.split("=");
		if (temp[0] == name) {
			return temp[1];
		}
	}
}
function setCookie(userid, str) {
	var Days = 2;
	var exp = new Date();
	exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
	document.cookie = userid + "=" + str + ";expires=" + exp.toGMTString();
}
function setUserCookie(userid) {
	if (userid == null || userid == "") {
		userid = VAR_NOT_LOGIN_USER;
	}
	var Days = 30;
	var exp = new Date();
	exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
	document.cookie = VAR_CURRENT_USER + "=" + userid + ";expires=" + exp.toGMTString();
}
function setQuantityToCookie(userid, id, quantity) {
	var Days = 2;
	var exp = new Date();
	exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
	document.cookie = userid + "=" + id + VAR_SEPARATOR_PARAM + quantity + ";expires=" + exp.toGMTString();
}
function deleteCartCookie() {
	var userid = getCookie(VAR_CURRENT_USER);
	userid = getCurrentUser(userid);
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = getCookie(userid);
	document.cookie = userid + "=" + cval + "; expires=" + exp.toGMTString();
}
function deleteCartCookieByName(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = getCookie(name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
function clearOtherUserCookie(userid) {
	var arrStr = document.cookie.split("; ");
	for (var i = 0; i < arrStr.length; i++) {
		var str = arrStr[i];
		var temp = str.split("=");
		if (temp[0] != userid && temp[0].length == 32) {
			deleteCartCookieByName(temp[0]);
		}
	}
}
function quantityError(obj) {
	var re = /^[1-9]\d*$/;
	if (!re.test(obj.value)) {
		alert("Error quantity!");
		window.setTimeout(function () {
			obj.focus();
		}, 0);
		obj.select();
		return false;
	}
	return true;
}

