function getCookie(name)
{
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg))
	{
		return unescape(arr[2]);	
	}
	else
	{
		return null;	
	}
}

function accMul(arg1,arg2) 
{ 
var m=0,s1=arg1.toString(),s2=arg2.toString(); 
try{m+=s1.split(".")[1].length}catch(e){} 
try{m+=s2.split(".")[1].length}catch(e){} 
return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m) 
} 

function onAddBuyToCookies(cookieValue)
{
	var Days = 30;
	var exp = new Date(); 
	exp.setTime(exp.getTime() + Days*24*60*60*1000);
	
	if(document.cookie==null || document.cookie=="")
	{
		document.cookie="shopCartCookie="+escape(cookieValue+"zhangshihua")+";expires=" + exp.toGMTString();
		document.location.href="ShopCart.asp";
	}
	else
	{
		var shopCartCookieStr=getCookie("shopCartCookie");
		
		if(cookieValue!=null && cookieValue!="")
		{
			if(shopCartCookieStr!=null && shopCartCookieStr!="")
			{
				var cookieValueArray=cookieValue.split("*");
				var pos=shopCartCookieStr.indexOf(cookieValueArray[0]);
				if(parseInt(pos)==-1)
				{
					shopCartCookieStr=shopCartCookieStr.concat(cookieValue,"zhangshihua");
				}
				else
				{
					var endPos=shopCartCookieStr.indexOf("zhangshihua",pos);
					var currentStr=shopCartCookieStr.substring(pos,parseInt(endPos)-1);
					var currentStrArray=currentStr.split("*");
					currentStrArray[2]=parseInt(currentStrArray[2])+1;
					currentStrArray[3]=parseFloat(currentStrArray[3])+0.0000000001;
					currentStrArray[4]=accMul(parseFloat(currentStrArray[2]),parseFloat(currentStrArray[3]));
					var newCurrentStr=currentStrArray.join("*");
					shopCartCookieStr=shopCartCookieStr.replace(currentStr,newCurrentStr);
				}
			    document.cookie="shopCartCookie="+escape(shopCartCookieStr)+";expires=" + exp.toGMTString();
				document.location.href="ShopCart.asp";
			}
			else
			{
				document.cookie="shopCartCookie="+escape(cookieValue+"zhangshihua")+";expires=" + exp.toGMTString();
				document.location.href="ShopCart.asp";
			}
		}
	}
}

function onDeleteBuyFromCookie(product_ID)
{
	var Days = 30;
	var exp = new Date(); 
	exp.setTime(exp.getTime() + Days*24*60*60*1000);
	
	var shopCartCookieStr=getCookie("shopCartCookie");
	var pos=shopCartCookieStr.indexOf(product_ID);
	if(parseInt(pos)!=-1)
	{
		var endPos=shopCartCookieStr.indexOf("zhangshihua",pos);
		var replaceEndPos=parseInt(endPos)+11;
		var currentStr=shopCartCookieStr.substring(pos,replaceEndPos);
		shopCartCookieStr=shopCartCookieStr.replace(currentStr,"");
		document.cookie="shopCartCookie="+escape(shopCartCookieStr)+";expires=" + exp.toGMTString();
		document.execCommand('Refresh');
	}
}

function onDisplayCookies()
{	
	if(document.cookie!=null && document.cookie!="")
	{
		var shopCartCookieStr=getCookie("shopCartCookie");
		var shopCartCookieArray=shopCartCookieStr.split("zhangshihua");
		var returnStr="";
		for(i=0;i<=shopCartCookieArray.length-1;i++)
		{
			if(shopCartCookieArray[i]!=null && shopCartCookieArray[i]!="")
			{
				var singleProductArray=shopCartCookieArray[i].split("*");
				var obj=document.getElementById("shopCartDiv");
				returnStr=returnStr+"<tr style='font-family:\"ËÎÌå\"; font-size:12px; text-align:center; padding-top:5px;'><td>"+singleProductArray[1]+"</td><td><input type='text' value='"+singleProductArray[2]+"' style='width:70px;'/><input type='button' value='Modify' onclick='onModifyBuyFromCookie(\""+singleProductArray[0]+"\",this)'/></td><td>"+parseFloat(singleProductArray[3]).toFixed(2)+"</td><td>"+parseFloat(singleProductArray[4]).toFixed(2)+"</td><td><span onclick='onDeleteBuyFromCookie(\""+singleProductArray[0]+"\")' style='cursor:pointer;'>Remove</span></td></tr>"
			}
		}
		obj.innerHTML="<table width='100%' border='0' cellspacing='0' cellpadding='0' style='margin-top:20px;border-top-color:#999999; border-top-style:dashed; border-top-width:1px; border-bottom-color:#999999; border-bottom-style:dashed; border-bottom-width:1px;padding-top:10px; padding-bottom:10px;'><tr style='font-family:\"ËÎÌå\"; font-size:12px; font-weight:600; text-align:center;'><td>Name</td><td>Count</td><td>Price</td><td>Total</td><td>Option</td></tr>"+returnStr;
	}
	onCountAllMoney();
}

function onModifyBuyFromCookie(product_ID,obj)
{
	var Days = 30;
	var exp = new Date(); 
	exp.setTime(exp.getTime() + Days*24*60*60*1000);
	var modifyValue=obj.parentNode.childNodes[0].value;
	
	var shopCartCookieStr=getCookie("shopCartCookie");
	if(shopCartCookieStr!=null && shopCartCookieStr!="")
	{
		var pos=shopCartCookieStr.indexOf(product_ID);
		if(parseInt(pos)!=-1)
		{
			var endPos=shopCartCookieStr.indexOf("zhangshihua",pos);
			var currentStr=shopCartCookieStr.substring(pos,parseInt(endPos)-1);
			var currentStrArray=currentStr.split("*");
			if(modifyValue=="" || modifyValue==null || modifyValue=="0")
			{
				currentStrArray[2]=0;
			}
			else
			{
				currentStrArray[2]=modifyValue;	
			}
			currentStrArray[3]=parseFloat(currentStrArray[3])+0.0000000001;
			currentStrArray[4]=accMul(parseFloat(currentStrArray[2]),parseFloat(currentStrArray[3]));
			var newCurrentStr=currentStrArray.join("*");
			shopCartCookieStr=shopCartCookieStr.replace(currentStr,newCurrentStr);
			document.cookie="shopCartCookie="+escape(shopCartCookieStr)+";expires=" + exp.toGMTString();
			document.execCommand('Refresh');
		}
	}
}

function onCountAllMoney()
{	
	var returnAllMoney=0;
	var shopCartCookieStr=getCookie("shopCartCookie");
	if(shopCartCookieStr!=null && shopCartCookieStr!="")
	{
		    var shopCartCookieStrArray=shopCartCookieStr.split("zhangshihua");
			for(i=0;i<=shopCartCookieStrArray.length-1;i++)
			{
				if(shopCartCookieStrArray[i]!="" && shopCartCookieStrArray[i]!=null)
				{
					var currentStrArray=shopCartCookieStrArray[i].split("*");
					if(currentStrArray[4]!="" && currentStrArray[4]!=null)
					{
						returnAllMoney=parseFloat(returnAllMoney)+parseFloat(currentStrArray[4]);
					}
				}	
			}
	}
	document.getElementById("Count_Money").innerText=parseFloat(returnAllMoney).toFixed(2);
}

function onDisplayBook()
{	
	if(document.cookie!=null && document.cookie!="")
	{
		var shopCartCookieStr=getCookie("shopCartCookie");
		var shopCartCookieArray=shopCartCookieStr.split("zhangshihua");
		var returnStr="";
		for(i=0;i<=shopCartCookieArray.length-1;i++)
		{
			if(shopCartCookieArray[i]!=null && shopCartCookieArray[i]!="")
			{
				var singleProductArray=shopCartCookieArray[i].split("*");
				var obj=document.getElementById("shopCartDiv");
				returnStr=returnStr+"<tr style='font-family:\"ËÎÌå\"; font-size:12px; text-align:center; padding-top:5px;'><td>"+singleProductArray[1]+"</td><td>"+singleProductArray[2]+"</td><td>"+parseFloat(singleProductArray[3]).toFixed(2)+"</td><td>"+parseFloat(singleProductArray[4]).toFixed(2)+"</td></tr>"
			}
		}
		obj.innerHTML="<table width='100%' border='0' cellspacing='0' cellpadding='0' style='margin-top:20px;border-top-color:#999999; border-top-style:dashed; border-top-width:1px; border-bottom-color:#999999; border-bottom-style:dashed; border-bottom-width:1px;padding-top:10px; padding-bottom:10px;'><tr style='font-family:\"ËÎÌå\"; font-size:12px; font-weight:600; text-align:center;'><td>Name</td><td>Count</td><td>Price</td><td>Total</td></tr>"+returnStr;
		document.getElementById("bookInfo").value=shopCartCookieStr;
		onCountAllMoney();
	}
}

