﻿if(!navigator.cookieEnabled)        //判断浏览器是否支持cookie
{
	alert("你的浏览器不支持cookie，不能实现购物功能，请开启");                 //提示浏览器不支持cookie 
}


//writeCookie("cookie名称","产品ID","产品数量")
function writeCookie(name, ID, num,others,t_show)
{
	msg_1="产品已经添加到购物车!"
	msg_2="产品已存购物车中！请不要重复添加!"
 	var cookieString=document.cookie;
    if (cookieString.length>=4000)
    {
        alert("您的购物车已满！");
    }
    else if(isNaN(num)||num<1||num.indexOf('.')!=-1)
    {
        alert("数量输入错误！");
    }
	else
	{
		var values="," + escape(ID) + "#" + escape(num)+"#"+escape(others);
		var cookieValue = readCookie(name);
		var Then = new Date();
		Then.setTime(Then.getTime()+24*60*60*1000);
		if(cookieValue=="")
		{
			document.cookie=name+"="+values+";expires=" + Then.toGMTString()+";path=/"
			//alert("已经加入您的购物车！");
			if(t_show!="" && t_show!=undefined){
				if(t_show=="2"){
					cartAlert(msg_1);
				}
			}else{
				cartAlert(msg_1);
			}
		}
		else
		{
			var test=false;
			var sss=cookieValue.split(",");
		
			if(sss.length>1){
			for (i=1;i<sss.length;i++){
			
			
			
				var s=sss[i].split("#");
			
					if(s[0]==ID)
					{
						cookieValue=cookieValue.replace(","+sss[i],"");
						nums=s[1];
						test=true;
						break;
					}
				
			}
			}
			
			if (test)
			{
					
		document.cookie = name+"="+cookieValue+ "," + escape(ID) + "#" + escape(parseInt(nums))+ "#" + escape(others)+";expires=" + Then.toGMTString()+";path=/";
				cartAlert(msg_2);
			}
			else
			{
			
				document.cookie = name+"="+cookieValue+values+";expires=" + Then.toGMTString()+";path=/"
				//alert("已经加入您的购物车！");
				if(t_show!="" && t_show!=undefined){
				if(t_show=="2"){
					cartAlert(msg_1);
				}
				}else{
				cartAlert(msg_1);
				}
			}
		}
	//	alert(document.cookie);
	}
}

function readCookie(name)
{
	var cookieString=document.cookie;
    if (cookieString=="")
    {
        return false;
    }
	else
    {
        var firstChar,lastChar;
        firstChar=cookieString.indexOf(name);
        if(firstChar!=-1)
        {
            firstChar+=name.length+1;
            lastChar = cookieString.indexOf(';', firstChar);
            if(lastChar == -1) lastChar=cookieString.length;
            return cookieString.substring(firstChar,lastChar);
        }
        else
        {
            return "";
        }
    }
}

function stringToInteger (inputString)
{
  return parseInt(inputString);
}//stringToInteger(inputString)   返回由字符串(inputString)转换所得到的整数

function isNotaNumber (inputString)
{
  return isNaN(inputString);
}//isNotaNumber(inputString)   返回一个 Boolean 值，指明inputString是否是保留值NaN(不是数字)

function UpdateCookie(name,Ids)		//删除单种商品
{

	var Then = new Date();
	Then.setTime(Then.getTime()+24*60*60*1000);
	
	var buy_num=document.getElementById(Ids);
	buy_num_value=buy_num.value;

	buy_num_value=buy_num_value+"#"
	buy_num_s=buy_num_value.split("#");

	value=escape(buy_num_s[0])+"#"+escape(buy_num_s[1])+"#"+escape(buy_num_s[2]);

	var vvv="";
	vvv=readCookie(name);//返回所有购买的内容

	vvv=vvv.replace(','+value,"");//把要删除的内容和其前面的","一起置空

	if (trim(vvv)=="" || trim(vvv)==",") {
	
		clears(name);
	}
	else{
	document.cookie = name + "=" + vvv+";expires=" + Then.toGMTString()+";path=/";//再把新的内容写入cookie里面
	}
	
}//UpdateCookie(name, value)   删除value这个内容

function ModifyCookie(name, valuex, ID, num)		//数量修改
{
	var Then = new Date();
	Then.setTime(Then.getTime()+24*60*60*1000);
	var vvv="";
	vvv=readCookie(name);//返回购买的所有内容,包括  物品ID,物品数量
	s=valuex.split("#");
	valuex=escape(s[0])+"#"+escape(s[1])+"#"+escape(s[2]);
	vvv=vvv.replace(','+valuex,"");//把要修改的内容去掉
	document.cookie = name + "=" + vvv + "," + escape(ID) + "#" + escape(num) + "#" + escape(others)+";expires=" + Then.toGMTString()+";path=/";//加上修改后得到的内容
	//location.reload();
	location.href='?';
}//ModifyCookie(name, valuex, ID, num)   修改内容,valuex为修改前的内容

function clears(name)			//删除所有购物车信息
{
	var exp = new Date();    
    exp.setTime (exp.getTime() - 1);    
	document.cookie=name+"=;expires=" + exp.toGMTString()+";path=/";
	//window.location.href='cookie_show.asp';
}//清除COOKIE

function AllPrice(num)
{
	var All=document.getElementById("all");
	All.innerHTML=num;
}//显示价格

function trim(str){  //删除左右两端的空格
 return str.replace(/(^\s*)|(\s*$)/g, "");
}
                                                                                                                   
