//页面每次加载时，对对比栏信息的刷新，得到之前存放在对比栏内商品的cookie在页面显示
function GetList()
{
	var cSpace_CodeList;
	var cSpace_NameList;
	var cPicList;
	var cType_CodeList;
	var Cookies;
	var cStr1;
	var CookCodeAry;
	var CookNameAry;
	var CookPicAry;
	var CookTypeAry;
	Cookies = document.cookie;
	cSpace_CodeList = GetCookie(Properties.getCookie().cSpace_CodeList);
	cSpace_NameList = unescape(GetCookie(Properties.getCookie().cSpace_NameList));
	cPicList = GetCookie(Properties.getCookie().cPicList);
	cType_CodeList = GetCookie(Properties.getCookie().cType_CodeList);
	if(cSpace_CodeList!=null)
	{
		CookCodeAry = cSpace_CodeList.split(",") ;
		CookNameAry = cSpace_NameList.split(",") ;
		CookPicAry = cPicList.split(",") ;
		CookTypeAry = cType_CodeList.split(",") ;
		L = CookCodeAry.length;
		cStr1="";
		var picPath = "";
		for(k=0;k<L;k++)
		{
			var strh = CutString(CookNameAry[k],5);
			if(CookPicAry[k] != null && CookPicAry[k] != '/ProductPictures/'){
				picPath = CookPicAry[k];
			}else{
				picPath = "/images/noPic_style3.jpg";
			}
			cStr1 = cStr1 + ""
			  +"	<table width='100%' border='0' cellspacing='0' cellpadding='0'>"
			  +"    	<tr>"
			  +"			<td align='center' valign='top' width='60'>"
			  +"				<a href='item-"+CookCodeAry[k]+".html' target='_blank' style='cursor:pointer'>"
			  +"					<img src='"+picPath+"' onload=\"javascript:resizeImage(this,60,60)\" border='0'/><br/><br/>"
			  +"				</a>"
			  +"				<a href='#g' onclick='EmptyComparePoolByProductId(this);' class='"+CookCodeAry[k]+"'>"
			  +"					<img width='17' height='17' alt='Delete' src='/images/iconDelete.gif'/>"
			  +"				</a>"
			  +"			</td>"
			  +"			<td align='left' valign='middle'>"
			  +"				<a href='item-"+CookCodeAry[k]+".html' target='_blank' style='cursor:pointer'>"
			  +						CookNameAry[k]
			  +"				</a>"
			  +"			</td>"
			  +"		</tr>"
			  +"	</table>"
			  +"";
		}
		cStr1 = cStr1+'<div class="w196"></div>';
		document.getElementById("SpaceList").innerHTML = cStr1;
		cClear_Tag = '1';
		if(document.getElementById('rw001').style.display=='none')
		{
			document.getElementById('rw001').style.display='';
		}
	}else{
		document.getElementById("SpaceList").innerHTML = '<div class="w196"></div>';
		document.getElementById('rw001').style.display='none';
	}
}

//清空所有对比栏内商品的cookie
function ClearTool()
{
	var SpaceList = document.getElementById('SpaceList');
	if(GetCookie(Properties.getCookie().cSpace_CodeList) == null){
		alert(Properties.getMessage().Limit0);
		return false;
	}else{
		if(confirm(Properties.getMessage().confirmClean))
		{

			DelCookie(Properties.getCookie().cSpace_CodeList);
			DelCookie(Properties.getCookie().cSpace_NameList);
			DelCookie(Properties.getCookie().cPicList);
			DelCookie(Properties.getCookie().cType_CodeList);
			SpaceList.innerHTML = '<div class="w196"></div>';
			document.getElementById('rw001').style.display='none';
			cClear_Tag = '0';
		}
		else
		{
			return false;
		}
	}
}

// 删除产品对比篮
function EmptyComparePool(){
    var cSpace_CodeList;
	var cSpace_NameList;
	var cPicList;
	var cType_CodeList;
    var productSize = 0;
    var CookCodeAry;
	var CookNameAry;
	var CookPicAry;
	var CookTypeAry;
	var checkedIds = "";
    if(GetCookie(Properties.getCookie().cSpace_CodeList) != null){
        productSize = GetCookie(Properties.getCookie().cSpace_CodeList).split(",").length;
        Cookies = document.cookie;
		cSpace_CodeList = GetCookie(Properties.getCookie().cSpace_CodeList);
		cSpace_NameList = unescape(GetCookie(Properties.getCookie().cSpace_NameList));
		cPicList = GetCookie(Properties.getCookie().cPicList);
		cType_CodeList = GetCookie(Properties.getCookie().cType_CodeList);
		CookCodeAry = cSpace_CodeList.split(",");
		CookNameAry = cSpace_NameList.split(",") ;
		CookPicAry = cPicList.split(",") ;
		CookTypeAry = cType_CodeList.split(",") ;
    }

    if(productSize <= 0){ // 对比篮中没有产品，提示用户。
        alert(Properties.getMessage().Limit0);
        return ;
    }
  	var ischecked = IsChecked();
    if( ischecked == false){
        alert(Properties.getMessage().deleteProduct);return;
    }else{
        var obj = document.forms["frm"].compareProduct;
        var sizeNum = GetCookie(Properties.getCookie().cSpace_CodeList).split(",").length;
      	for(m = 0 ; m < obj.length ; m++){
			var checkedId = obj[m].value;
			if(obj[m].checked){
				checkedIds = checkedIds+checkedId+",";
			}
		}
		var SpaceList = document.getElementById('SpaceList');
		if(confirm(Properties.getMessage().confirmDelete)){
			DelCookie(Properties.getCookie().cSpace_CodeList);
			DelCookie(Properties.getCookie().cSpace_NameList);
			DelCookie(Properties.getCookie().cPicList);
			DelCookie(Properties.getCookie().cType_CodeList);
			SpaceList.innerHTML = '<div class="w196"></div>';
			document.getElementById('rw001').style.display='none';
		    cClear_Tag = '0';
		    if(obj.length != null){
				for(i = (sizeNum - 1); i >= 0; i--){
					var productId = CookCodeAry[i];
					if(checkedIds.indexOf(productId) == -1){
						AddProductToComparePool(CookTypeAry[i] , productId , CookNameAry[i] , CookPicAry[i]);
					}
				}
			}
		}else{
			return false;
		}
    }
}


/*
	Terry.B.Li 2008-8-21 添加，点击X删除对比栏中产品
	@param productId 产品编号
*/
function EmptyComparePoolByProductId(obj){
    var cSpace_CodeList;
	var cSpace_NameList;
	var cPicList;
	var cType_CodeList;
    var productSize = 0;
    var CookCodeAry;
	var CookNameAry;
	var CookPicAry;
	var CookTypeAry;
	var checkedIds = "";
    if(GetCookie(Properties.getCookie().cSpace_CodeList) != null){
        productSize = GetCookie(Properties.getCookie().cSpace_CodeList).split(",").length;
        Cookies = document.cookie;
		cSpace_CodeList = GetCookie(Properties.getCookie().cSpace_CodeList);
		cSpace_NameList = unescape(GetCookie(Properties.getCookie().cSpace_NameList));
		cPicList = GetCookie(Properties.getCookie().cPicList);
		cType_CodeList = GetCookie(Properties.getCookie().cType_CodeList);
		CookCodeAry = cSpace_CodeList.split(",");
		CookNameAry = cSpace_NameList.split(",") ;
		CookPicAry = cPicList.split(",") ;
		CookTypeAry = cType_CodeList.split(",") ;
    }

    if(productSize <= 0){ // 对比篮中没有产品，提示用户。
        alert(Properties.getMessage().Limit0);
        return ;
    }
//  	var ischecked = IsChecked();
//    if( ischecked == false){
//        alert(Properties.getMessage().deleteProduct);return;
//    }else{
//        var obj = document.forms["frm"].compareProduct;
        var sizeNum = GetCookie(Properties.getCookie().cSpace_CodeList).split(",").length;
//      	for(m = 0 ; m < obj.length ; m++){
//			var checkedId = obj[m].value;
			var checkedId = $(obj).attr("class");
//			if(obj[m].checked){
				checkedIds = checkedIds+checkedId+",";
//			}
//		}
		var SpaceList = document.getElementById('SpaceList');
//		if(confirm(Properties.getMessage().confirmDelete)){
			DelCookie(Properties.getCookie().cSpace_CodeList);
			DelCookie(Properties.getCookie().cSpace_NameList);
			DelCookie(Properties.getCookie().cPicList);
			DelCookie(Properties.getCookie().cType_CodeList);
			SpaceList.innerHTML = '<div class="w196"></div>';
			document.getElementById('rw001').style.display='none';
		    cClear_Tag = '0';
//		    if(obj.length != null){
				for(i = (sizeNum - 1); i >= 0; i--){
					var productId = CookCodeAry[i];
					if(checkedIds.indexOf(productId) == -1){
						AddProductToComparePool(CookTypeAry[i] , productId , CookNameAry[i] , CookPicAry[i]);
					}
				}
//			}
//		}else{
//			return false;
//		}
//    }
}

function CutString(str,len)
{
	var le = len;
	for(var i=0;i<len;i++)
	{
		if(str.charCodeAt(i)>255)
		{
			le--;
		}
	}
	return str.substring(0,le);
}

//页面显示产品加入对比栏，属性分别是当前产品的类别编号，此产品的编号，产品名称，产品的url连接
function AddProductToComparePool(cType_code , cSpace_code , cSpace_name , cPic)
{
	GetList();
	var cType_codeStr = cType_code;
	var cSpace_CodeStr = cSpace_code;
	var cSpace_NameStr = cSpace_name;
	var cPicStr = trim(cPic);
	var cSpace_CodeList;
	var cSpace_NameList;
	var cPicList;
	var cType_CodeList;
	var Cookies;
	var Ary;
	var CookCodeAry;
	var CookNameAry;
	var CookPicAry;
	var SpaceList = document.getElementById('SpaceList');
	//alert("**************************"+SpaceList.value);
	if(cPicStr == null || cPicStr == '/ProductPictures/'){
		cPicStr = "/images/noPic_style3.jpg";
	}
	var strh = CutString(cSpace_name,9);
	var Str = ""
			  +"	<table width='100%' border='0' cellspacing='0' cellpadding='0'>"
			  +"    	<tr>"
			  +"			<td align='center' valign='top' class='havelink' width='60'>"
			  +"				<a href='item-"+cSpace_CodeStr+".html' target='_blank' style='cursor:pointer'>"
			  +"					<img src='"+cPicStr+"' onload=\"javascript:resizeImage(this,60,60)\" border='0'/><br/><br/>"
			  +"				</a>"
			  +"				<a href='#g' onclick='EmptyComparePoolByProductId(this);' class='"+cSpace_CodeStr+"'>"
			  +"					<img width='17' height='17' alt='Delete' src='/images/iconDelete.gif'/>"
			  +"				</a>"
			  +"			</td>"
			  +"			<td align='left' valign='top' class='havelink'>"
			  +"				<a href='item-"+cSpace_CodeStr+".html' target='_blank' style='cursor:pointer'>"
			  +						cSpace_name
			  +"				</a>"
			  +"			</td>"
			  +"		</tr>"
			  +"	</table>"
			  +"";

	Cookies = document.cookie;

	cSpace_CodeList = GetCookie(Properties.getCookie().cSpace_CodeList);
	//alert("1111:"+cSpace_CodeList);
	cSpace_NameList = GetCookie(Properties.getCookie().cSpace_NameList);
	cPicList = GetCookie(Properties.getCookie().cPicList);
	cType_CodeList = GetCookie(Properties.getCookie().cType_CodeList);

	var num=0;
	var CookAry;

	if(cSpace_CodeList != "" && cSpace_CodeList != null){
		CookAry = cSpace_CodeList.split(",");
		num = CookAry.length;
	}
	//alert(num + "----" + cSpace_CodeList);
	if(num<5){
		if(cSpace_CodeList != null){
			if(cSpace_CodeList.indexOf(cSpace_code,0)==-1){
				cSpace_CodeList = cSpace_CodeStr+","+cSpace_CodeList;
				cSpace_NameList = cSpace_NameStr+","+cSpace_NameList;
				cType_CodeList = cType_codeStr+","+cType_CodeList;
				cPicList = cPicStr+","+cPicList;
				SpaceList.innerHTML = Str+SpaceList.innerHTML;
			}else{
				alert(Properties.getMessage().alreadyCompare);
				return false;
			}
		}else{
			cSpace_CodeList = cSpace_CodeStr;
			cSpace_NameList = cSpace_NameStr;
			cType_CodeList = cType_codeStr;
			cPicList = cPicStr;
			SpaceList.innerHTML = Str+SpaceList.innerHTML;
		}
		//alert(cSpace_CodeList);
		cClear_Tag = '1';
		CookieSet(Properties.getCookie().cSpace_CodeList, cSpace_CodeList);
		//alert(cSpace_CodeList);
		CookieSet(Properties.getCookie().cSpace_NameList, cSpace_NameList);
		CookieSet(Properties.getCookie().cPicList, cPicList);
		CookieSet(Properties.getCookie().cType_CodeList, cType_CodeList);
	}else{
		alert(Properties.getMessage().cartFull);
	}
	if(document.getElementById('rw001').style.display=='none'){
		document.getElementById('rw001').style.display='';
	}
}

function CookieSet(name,value)//两个参数，一个是cookie的名子，一个是值
{
	//alert("set cookie:" + name + "=" + value);
	var Days = 1; //此 cookie 将被保存 1 天
	var exp  = new Date();    //new Date("December 31, 9998");
	exp.setTime(exp.getTime() + Days*24*60*60*1000);
	document.cookie = name + "="+ escape (value) + "; expires=" + exp.toGMTString()+"; path="+"/";
	//alert("after set, cookie=" + document.cookie);
}

//删除cookie
function DelCookie(name)
{
	var expires = new Date(2000,1,1);
    expires.setTime(expires.getTime() - 1);
    var cval=GetCookie(name);
	if(cval!=null) document.cookie= name + "="+cval+";expires="+expires.toGMTString()+";path="+"/";
}

function getCookieVal (offset)
{
    var endstr = document.cookie.indexOf (";",offset);

    if (endstr==-1) endstr=document.cookie.length;

    return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name)
{
  var reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
  var ValueList = document.cookie.match(reg);
  if(ValueList!=null) return unescape(ValueList[2]);
  else return null;
}


// 对比篮中的产品进行对比
function CompareProduct()
{
    var productSize = 0;
    if(GetCookie(Properties.getCookie().cSpace_CodeList) != null){
        productSize = GetCookie(Properties.getCookie().cSpace_CodeList).split(",").length;
    }

    if(productSize > 0){
    	var compareItemIdListInCookie = GetCookie(Properties.getCookie().cSpace_CodeList);
    	//alert(compareItemIdListInCookie);
    	var url="/ProductInfo_compareProduct.action?compareItemIdListInCookie="+compareItemIdListInCookie;
    	//alert(url);
        window.open(url,'','scrollbars=yes,toolbar=no,menubar=no,location=no,resizable=yes');
    }
    else{ // 对比篮中没有产品，提示用户。
        alert(Properties.getMessage().Limit0);
    }
}


function deleteOneProductFromCookie(itemId,catalogId)
{
    var cSpace_CodeList;
	var cSpace_NameList;
	var cPicList;
	var cType_CodeList;

    var CookCodeAry;
	var CookNameAry;
	var CookPicAry;
	var CookTypeAry;
	var checkedIds = itemId;
	//alert("--------------------------------------");
    if(GetCookie(Properties.getCookie().cSpace_CodeList) != null){
     	//alert("**************************************");
        Cookies = document.cookie;
		cSpace_CodeList = GetCookie(Properties.getCookie().cSpace_CodeList);
		cSpace_NameList = unescape(GetCookie(Properties.getCookie().cSpace_NameList));
		cPicList = GetCookie(Properties.getCookie().cPicList);
		cType_CodeList = GetCookie(Properties.getCookie().cType_CodeList);
		CookCodeAry = cSpace_CodeList.split(",");
		CookNameAry = cSpace_NameList.split(",") ;
		CookPicAry = cPicList.split(",") ;
		CookTypeAry = cType_CodeList.split(",") ;
    }

    var selectIndex = 0;

    if(CookCodeAry.length == 1){
    	alert(Properties.getMessage().onlyProduct);
    	return false;
    }
    if(confirm(Properties.getMessage().confirmDelete)){
    	if(CookCodeAry.length > 1){
		DelCookie(Properties.getCookie().cSpace_CodeList);
		DelCookie(Properties.getCookie().cSpace_NameList);
		DelCookie(Properties.getCookie().cPicList);
		DelCookie(Properties.getCookie().cType_CodeList);

		var newItemIDList = "";
		var sizeNum = CookCodeAry.length;
		//alert(sizeNum);
		for(i=0 ; i<  sizeNum; i++){
			var productId = CookCodeAry[i];
			if(checkedIds.indexOf(productId) == -1){
				newItemIDList = newItemIDList + productId;
				newItemIDList = newItemIDList + ",";

			}
		}

		var lastChar = newItemIDList.charAt(newItemIDList.length-1);
		if(lastChar == ',' ){
			newItemIDList = newItemIDList.substring(0, newItemIDList.length-1);
		}
		//alert("itemId: "+itemId+" <br>newItemIDList:"+newItemIDList);
		for(i=0 ; i<  sizeNum; i++){
			var productId = CookCodeAry[i];
			if(checkedIds.indexOf(productId) > -1){
				break;
			}
			selectIndex++;
		}

		var newPicList = getNewCookieList(selectIndex,CookPicAry);
		var newNameList = getNewCookieList(selectIndex,CookNameAry);
		var newTypeList = getNewCookieList(selectIndex,CookTypeAry);

		CookieSet(Properties.getCookie().cSpace_CodeList, newItemIDList);
		CookieSet(Properties.getCookie().cSpace_NameList, newNameList);
		CookieSet(Properties.getCookie().cPicList, newPicList);
		CookieSet(Properties.getCookie().cType_CodeList, newTypeList);

    	var url="/ProductInfo_compareProduct.action?compareItemIdListInCookie="+newItemIDList;

		document.deleteForm.action=url;
    	document.deleteForm.submit(); }
    }
}

function getNewCookieList(selectIndex,arrayList){
	var newList="";
	for(i=0 ; i<  arrayList.length; i++){
		if(i != selectIndex){
			var itemp = arrayList[i];
			newList = newList + itemp;
			if(i !=  arrayList.length-1){
				newList = newList + ",";
			}
		}
	}
	return deleteLastComma(newList);
}

function deleteLastComma(str){
	var lastChar = str.charAt(str.length-1);
	if(lastChar == ',' ){
		str = str.substring(0, str.length-1);
	}
	return str;
}

function trim(w) {
  while (w.length>0 && (w.substr(0,1)==' ' || w.substr(0,1)=='?')) w=w.substr(1);
  while (w.length>0 && (w.substr(w.length-1)==' ' || w.substr(w.length-1)=='?')) w=w.substr(0,w.length-1);
  return(w);
}