//document.cookie = "jpcmagcart=;path=/;";
//document.cookie = "jpcfav=;path=/;";
// Download
function download(format,bookid,bookname,booktype,havepic)
{
	if(format=='PNG')
	{
		addtocart(bookid,bookname,booktype,havepic);
	}
	else
	{
		addtofav(bookid,bookname,booktype,havepic);
		
		//alert(RCk("jpcfav", ""));
	}
	return false;
}


// 函数名  RCk()
// 功能         将二维数组的值重新连接成格式字符串并写到Cookie中去
// 参数    name 要读取的Cookie的名字
// 参数    defu 如果Cookie不存在时返回的默认值
// 返回值  读到的Cookie字符串或者默认值
function RCk (name, defu){
     var arg=name+"="
     var ck=document.cookie
     if (ck.indexOf(arg)!=-1){
           var vstr=ck.indexOf(arg)+arg.length
           if (ck.indexOf(";",vstr)==-1) var vend=ck.length
           else var vend=ck.indexOf(";",vstr)
           return ck.substring(vstr,vend);
           //return unescape(ck.substring(vstr,vend))
     }else return defu
}

// 函数名  writeCookie()
// 功能         将字符串写到Cookie中去
// 参数    strCookie 要写的Cookie的值
// 返回值  无
function writeCookie (strCookie){
     //alert(strCookie);
     //alert(escape(strCookie));
     document.cookie = "jpcmagcart=" + strCookie + ";path=/;";
     //document.cookie = "Goods=" + escape(strCookie) + ";path=/;";
}

// 函数名  addGoods()
// 功能         添加商品到购物车
// 返回值  无
// 参数
// gId       gName       gPrice        gSpecs
// 商品ID    商品名称    单价   数量   规格
// 生成的字符串如下面格式，商品和商品之间用"##"分隔
// 10004225::摄像头系列::348.00::1::规格:xxxxywww

// 以下变量的顺序因该和上面的顺序相对应，这些变量类似宏的作用
var GID=0,   GNAME=1,   GPRICE=2, GQT=3, GSPECS=4

// 函数名  removeCart()
// 功能         删除购物车中的所有信息
// 参数    tag
// 返回值  无
function removeCart (tag){
     writeCookie ("");
}

// 函数名  indexOfGoods()
// 功能         从Cookie中取一个商品的纪录
// 参数    gId 商品ID
// 返回值  一维数组 存有一个商品的所有信息
function indexOfGoods (gId){
     var i, j, strGoods, strOneGoods;
     var strCookie, strAllGoods;
	 strGoods = new Array();
	 
     strCookie = RCk("jpcmagcart", "");
     strAllGoods = strCookie.split("##");
	
     for (j=0; j<strAllGoods.length; j++){
           strGoods = strAllGoods[j].split("::");
			 for (i=0; i<strGoods.length; i++){
				   if (strGoods[GID] == gId){
						 strOneGoods = strGoods;
						 break;
				   }
			 }
	 }
     return strOneGoods;



}

// 函数名  removeGoods()
// 功能         删除购物车中的某项物品
// 参数    gId 商品ID
// 返回值  无
function removeGoods (gId){
     var i, j, strAllGoods, strCookie, strCompart;
     strCookie = RCk("jpcmagcart", "");
	 if (strCookie == "") return;
     strAllGoods = strCookie.split("##");
     strCookie = "";
     for (i=0; i<strAllGoods.length; i++){
           if (strAllGoods[i].indexOf(gId) == -1){
                 strCompart = (strCookie == "") ? "" : "##";
                 strCookie += strCompart + strAllGoods[i];
		   }
     }
     writeCookie (strCookie);
	 	 if(strAllGoods.length==1)
	 {
		 writeCookie("");
	 }
	 else
	 {	 
	 }
}

/////////////////////////////////////////////////////////////////
// 函数名  viewCart()
// 功能         浏览购物车中的所有商品
// 参数    无
// 返回值  无
function viewCart(){
     //cartWin = newWin("", "viewCart", 600, 400);
     //printGoods(cartWin);
	 alert(RCk("jpcmagcart", ""));
}

////////////////////////////////////////////////////////////////////

// 函数名  getCart()
// 功能         从Cookie中读出购物车
// 参数    无
// 返回值  二维数组
function getCart (){
     var strGoods = new Array();
     var strCookie, strAllGoods, i, j;

     strCookie = RCk("jpcmagcart", "");
     strAllGoods = strCookie.split("##");
	
     for (i=0; i<strAllGoods.length; i++){
           strGoods = strAllGoods[i].split("::");
     }
		   //alert(strAllGoods);
     return strGoods;
}
function addtocart(gId, gName, gPrice, gSpecs){
     var strCookie, strGoods, strCompart;
     if(indexOfGoods(gId)){
		 		 removeGoods (gId);
		 	var div = document.getElementById("downloadloader-PNG");
			div.innerHTML = gName + "<p>已從您的購物車中移除</p>";
			var shottext=document.getElementById("cart"+gId);
			shottext.src="../stylecss/interface/cart.png";
			$('#downloadloader-PNG').fadeIn('fast');
			setTimeout("$('#downloadloader-PNG').fadeOut('fast');",6000 );

     }
	 else{
		   strCookie = RCk("jpcmagcart", "");
           strCompart = (strCookie == "") ? "" : "##";
           strGoods = strCompart + gId + "::" + escape(gName) + "::" + gPrice + "::" + gSpecs;
           strCookie += strGoods;
		   writeCookie (strCookie);
		   strCookie = RCk("jpcmagcart", "");
		   
		 	var div = document.getElementById("downloadloader-PNG");
			div.innerHTML = gName + "<p>已添加到您的購物</p>";
			var shottext=document.getElementById("cart"+gId);
			shottext.src="../stylecss/interface/carted.png";
			$('#downloadloader-PNG').fadeIn('fast');
			setTimeout("$('#downloadloader-PNG').fadeOut('fast');",6000 );
		   
	 }
}



//removeGoods("868");
//removeCart();
//addtocart("818","中國生物工程雜誌","中科院文獻情報中心","nopic");

//alert(RCk("jpcmagcart", ""));


//设置两个cookie
//document.cookie="jpcmagcart=828::中國金融::中國人民銀行總行::nopic##919::中國金2融::中國人民銀行3總行::nosic; path=/";
//获取cookie字符串
//var strCookie=document.cookie;
//将多cookie切割为多个名/值对
//var arrCookie=strCookie.split("; ");
//var userId;
//遍历cookie数组，处理每个cookie对
//for(var i=0;i<arrCookie.length;i++){
//        var arr=arrCookie[i].split("=");
//       //找到名称为userId的cookie，并返回它的值
 //       if("userId"==arr[0]){
   //            userId=arr[1];
     //          break;
       // }
//}

//alert(document.cookie);



// 收藏夾

function writefavCookie (strfavCookie){
     document.cookie = "jpcfav=" + strfavCookie + ";path=/;";
}

var FID=0,   FNAME=1,   FPRICE=2, FQT=3, FSPECS=4



function indexOffav (fId){
     var i, j, strfav, strOnefav;
	 
     var strfavCookie, strAllfav;
	 strfav = new Array();
     
	 strfavCookie = RCk("jpcfav", "");
     strAllfav = strfavCookie.split("##");
	 
     for (j=0; j<strAllfav.length; j++){
           strfav = strAllfav[j].split("::");
		        for (i=0; i<strfav.length; i++){
				   if (strfav[FID] == fId){
						 strOnefav = strfav;
						 break;
				   }
     			}

     }
     return strOnefav;
	 
	 
}

function removefav (fId){
     var i, j, strAllfav, strfavCookie, strfavCompart;
	 var div = document.getElementById("ddd");
     strfavCookie = RCk("jpcfav", "");
	 if (strfavCookie == "") return;
     strAllfav = strfavCookie.split("##");
     strfavCookie = "";
     for (i=0; i<strAllfav.length; i++){
           if (strAllfav[i].indexOf(fId) == -1){
                 strfavCompart = (strfavCookie == "") ? "" : "##";
                 strfavCookie += strfavCompart + strAllfav[i];
		   }
		   
     }

     writefavCookie (strfavCookie);

	 if(strAllfav.length==1)
	 {
		 writefavCookie("");
	 }
	 else
	 {	 
	 }
	 
	 }

function viewfav(){
	 alert(RCk("jpcfav", ""));
}

function getfav (){
     var strfav = new Array();
     var strfavCookie, strAllfav, i, j;

     strfavCookie = RCk("jpcfav", "");
     strAllfav = strfavCookie.split("##");
	 alert(strAllfav);
     for (i=0; i<strAllfav.length; i++){
           strfav = strAllfav[i].split("::");
     }
     alert(strfav);
	 return strfav;
}
function addtofav(fId, fName, fPrice, fSpecs){
     var strfavCookie, strfav, strfavCompart;
     if(indexOffav(fId)){
         removefav(fId);
         PageMethods.memberDelfav(fId);
		 	var div = document.getElementById("downloadloader-ICO");
			div.innerHTML = fName + "<p>已從您的收藏夾中移除</p>";
			var shottext=document.getElementById("star"+fId);
			if (shottext != null) {
			shottext.src = "../stylecss/interface/icon_bookmark.png";
			}
			shottext=document.getElementById("starb"+fId);
			if (shottext != null) {
			shottext.src = "../stylecss/interface/icon_bookmark.png";

			}
			shottext = document.getElementById("starc" + fId);
			if (shottext != null) {
				shottext.src = "../stylecss/interface/btn_bookmark.png";
			}

			$('#downloadloader-ICO').fadeIn('fast');
			setTimeout("$('#downloadloader-ICO').fadeOut('fast');",6000 );

     }
	 else{
	     PageMethods.memberAddfav(fId, fName, fPrice, fSpecs, showMemFav);
		   strfavCookie = RCk("jpcfav", "");
           strfavCompart = (strfavCookie == "") ? "" : "##";
           strfav = strfavCompart + fId + "::" + escape(fName) + "::" + fPrice + "::" + fSpecs;
           strfavCookie += strfav;
		   writefavCookie (strfavCookie);
		   strfavCookie = RCk("jpcfav", "");
			var div = document.getElementById("downloadloader-ICO");
			div.innerHTML = fName + "<p>已加到您的收藏夾</p>";
	 		var shottext=document.getElementById("star"+fId);
			if (shottext != null) {
	 		shottext.src = "../stylecss/interface/icon_bookmarked.png";
			}
			shottext=document.getElementById("starb"+fId);
			if (shottext != null) {
	 		shottext.src = "../stylecss/interface/icon_bookmarked.png";
			}
			shottext = document.getElementById("starc" + fId);
			if (shottext != null) {
				shottext.src = "../stylecss/interface/btn_bookmarked.png";
			}
			$('#downloadloader-ICO').fadeIn('fast');
			setTimeout("$('#downloadloader-ICO').fadeOut('fast');", 6000);

			

	 }
}

function showMemFav(res) {
    //alert(res);
}

//removefav("2131");
//removeCart();
//removefav();
//alert(RCk("jpcmagcart", ""));
//removefav (tag);
//alert(RCk("jpcfav", ""));


