﻿// JScript File
// Create by: Kimi Yang (Yang Xi)
// Create Date: 2007-11-16
// Description: Compare Product

//Clear Cookie;
//SetCookie("CompareProducts","",24);
//================================================================================
// Read Cookie
function GetCookie(name)
{
	var strArg=name+"=";
	var nArgLen=strArg.length;
	var nCookieLen=document.cookie.length;
	var nEnd;
	var i=0;
	var j;

	while (i<nCookieLen)
	{
		j=i+nArgLen;
		if (document.cookie.substring(i,j)==strArg)
		{
			nEnd=document.cookie.indexOf (";",j);
			if (nEnd==-1) nEnd=document.cookie.length;
			return DecodeCookie(unescape(document.cookie.substring(j,nEnd)));
		}
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
} 

//================================================================================
// Write Cookie
function SetCookie(name,value,expires)
{
	var exp = new Date();
	exp.setTime(exp.getTime()+expires*60*60*1000);
	document.cookie=name+"="+escape(EncodeCookie(value))+";path=/;expires="+exp.toGMTString();
}


//================================================================================
// unicode Encoding

function EncodeCookie(InputString)
{
//	var strRtn="";
//	for (var i=InputString.length-1;i>=0;i--)
//	{
//		strRtn+=InputString.charCodeAt(i);
//		if (i) strRtn+="|"; //Using "|" to split
//	}
//	return strRtn;
return InputString;
}

// unicode Decoding
function DecodeCookie(InputString)
{
//	var strArr;
//	var strRtn="";

//	strArr=InputString.split("|");

//	for (var i=strArr.length-1;i>=0;i--)
//		strRtn+=String.fromCharCode(eval(strArr[i]));

//	return strRtn;
return InputString;
}


//================================================================================
//Add the Product to Compare
function Compare_AddorDelProduct(ProductID,chkId,ProductCategory)
{
 	Compare_CheckCategoryChange(ProductCategory);
	var CompareProducts = GetCookie("CompareProducts");
 
   if(chkId.checked)
   {
	    if ((CompareProducts != "") && (CompareProducts != null))
	    {
		    var arrCookies = CompareProducts.split(",");
		    if (arrCookies.length < 4)
		    {
			    if (CompareProducts.indexOf(ProductID) == -1)
			    {
    			
				    Compare_CheckSetCookieValue("CompareProducts",ProductID,1);
			    }
		    }
		    else
		    {
			    alert("Sorry! You can only compare 4 products in one time.");
			    chkId.checked=false;
		    }
	    }
	    else
	    {
		    Compare_CheckSetCookieValue("CompareProducts",ProductID ,1);
	    }
	}
	else
	{
	    Compare_DelProduct(ProductID)
	}
//	Compare_ReRender();
//	Compare_ShowLayer();
}
//================================================
// Check the product whether is the same category of previous product

function Compare_CheckCategoryChange(SubCategory)
{
	var CategoryID = GetCookie("ProductCategory");
	if ((SubCategory == null) || (SubCategory == ""))
	{
		SetCookie("ProductCategory",SubCategory,1);
	}
	else
	{
		if (SubCategory != CategoryID)
		{
			SetCookie("ProductCategory",SubCategory,1);
			SetCookie("CompareProducts","",1);
		}
	}
}
//===============================================================================
function Compare_DelProduct(ProductID)
{
	var nameCookieValue = GetCookie("CompareProducts");
	if ((nameCookieValue != null) && (nameCookieValue != ""))
	{
		if (nameCookieValue.indexOf(ProductID) > -1)
		{
			var arrCookies = nameCookieValue.split(",");
			var ValueNum = arrCookies.length;
			nameCookieValue = "";
			for (i=0; i < ValueNum; i++)
			{
				if (arrCookies[i].indexOf(ProductID) == -1)
				{
					nameCookieValue += arrCookies[i] + ",";
				}
			}
			nameCookieValue = nameCookieValue.substring(0,nameCookieValue.length - 1);
			SetCookie("CompareProducts",nameCookieValue,1);
		}
//		Compare_ReRender();
//		redraw();
	}
}

//===============================================================================
// Check Cookie whether exists and write product Cookie
function Compare_CheckSetCookieValue(name,value,expires)
{
	var nameCookieValue = GetCookie(name);
	if ((nameCookieValue == "") || (nameCookieValue == null))
	{
		var exp = new Date();
		exp.setTime(exp.getTime()+expires*60*60*1000);
		document.cookie=name+"="+escape(EncodeCookie(value))+";path=/;expires="+exp.toGMTString();
	}
	else
	{
		if (nameCookieValue.indexOf(value) == -1)
		{
			var arrCookies = nameCookieValue.split(",");
			var ValueNum = arrCookies.length;
			if (ValueNum > 4)
			{
				nameCookieValue = "";
				for (loop=0; loop < 4; loop++)
				{
					nameCookieValue += arrCookies[loop] + ",";
				}
				nameCookieValue = nameCookieValue.substring(0,nameCookieValue.length - 1);
			}

			var exp = new Date();
			exp.setTime(exp.getTime()+expires*60*60*1000);
			document.cookie=name+"="+escape(EncodeCookie(value+","+nameCookieValue))+";path=/;expires="+exp.toGMTString();
		}
	}
}

function Compare_CheckSetShopCartCookieValue(name,value,expires)
{
	var nameCookieValue = GetCookie(name);
	if ((nameCookieValue == "") || (nameCookieValue == null))
	{
		var exp = new Date();
		exp.setTime(exp.getTime()+expires*60*60*1000);
		document.cookie=name+"="+escape(EncodeCookie(value))+";path=/;expires="+exp.toGMTString();
	}
	else
	{
		 	var arrCookies = nameCookieValue.split(",");
			var ValueNum = arrCookies.length;
	 	
			nameCookieValue = "";
			for (loop=0; loop < ValueNum; loop++)
			{
				nameCookieValue +=  arrCookies[loop] + ",";
		    }
			var exp = new Date();
			exp.setTime(exp.getTime()+expires*60*60*1000);
			document.cookie=name+"="+escape(EncodeCookie(value+","+nameCookieValue))+";path=/;expires="+exp.toGMTString();
		 
	}
}


//=====================================================================================
function Compare_ClearAll()
{
	SetCookie("ProductCategory","",1);
	SetCookie("CompareProducts","",1);
	Compare_ReRender();
//	redraw();
}


//======================================================================================

function Compare_ReRender()
{
	//FixPosition();
	var CompareProducts = GetCookie("CompareProducts");
	if (CompareProducts == null)
	{
		CompareProducts = "";
	}
	var arrCookies = CompareProducts.split(",");
	var CompareTable = "<table width='100%' border='0' cellpadding='1' cellspacing='1' style='background-color:#92BCDC' style='font-size:12px'>"
		+ "  <tr>"
		+ "    <td height='22' align='center' style='background-color:#E8F5FD'><b style='font-size:12px; color:red'>Compare List</b></td>"
		+ "  </tr>"
		+ "  <tr>"
		+ "    <td align='center' style='background-color:#E8F5FD'>";

	for (i = 0; i < arrCookies.length; i++)
	{
		if ((arrCookies[i] != "") && (arrCookies[i] != null))
		{
			if (arrCookies[i].indexOf("|") >= 0)
			{
				var ProductInfo = arrCookies[i].split("|");
				if (ProductInfo.length = 4)
				{
					var ProductID = ProductInfo[0];
					var ProductName = ProductInfo[1];
					var ProductLink = ProductInfo[2];
					var ProductImg = ProductInfo[3];
					if (ProductID == null)
					{
						ProductID = "";
					}
					if (ProductName == null)
					{
						ProductName = "";
					}
					if (ProductLink == null)
					{
						ProductLink = "";
					}
					if (ProductImg == null)
					{
						ProductImg = "";
					}
					if ((ProductName != "") && (ProductID != "") && (ProductLink != "") && (ProductImg != ""))
					{
						CompareTable = CompareTable //+ "<span style='color:#FF0000'>|</span>"
							+ "      <table border='0' align='center' cellpadding='0' cellspacing='1' style='background-color:#000000'>"
							+ "        <tr>"
							+ "          <td style='background-color:#FFFFFF'><a href='" + ProductLink + "' target='_blank'><img src='" + ProductImg + "' width='80' height='60' border='0' alt='" + ProductName + "' /></a></td>"
							+ "        </tr>"
							+ "      </table>"
							+ "      <table width='80' align='center' border='0' cellpadding='0' cellspacing='0'>"
							+ "        <tr>"
							+ "          <td height='2'></td>"
							+ "        </tr>"
							+ "      </table>"
							+ "      <input type='button' name='Product" + i + "' value='" + ProductName + "' style='border:1px solid; border-color:#92BCDC; background-color:white; height:22px; width:111px; cursor:hand; color:black; font-size:12px' onClick=Compare_DelProduct('" + ProductID + "'); />"
							+ "      <br />";
					}
				}
			}
		}
	}

	CompareTable = CompareTable + "      <table width='100%' border='0' cellpadding='0' cellspacing='0' style='font-size:12px'>"
		+ "      <tr>"
		+ "        <td height='25' align='center' valign='bottom'><a onClick='Compare_ClearAll()' style='color:blue;cursor: hand'>Clear</a>&nbsp;&nbsp;&nbsp;<a onClick='Compare_HideLayer()' style='color:blue;cursor: hand'>Hide</a></td>"
		+ "      </tr>"
		+ "      </table>"
		+ "    </td>"
		+ "  </tr>"
		+ "  <tr>"
		+ "    <td align='center' style='background-color:#E8F5FD'><input type='button' value='Compare' name='CompareBtn' style='cursor:hand; width:80px; font-size:12px' onClick='Compare_Check()' /></td>"
		+ "  </tr>"
		+ "</table>";
	document.getElementById('CompareBasket').innerHTML = CompareTable;
}


//==========================================================================================================
function Compare_Check()
{
    //Add by Ruby Gao on 2009-10-16
	var isOther = GetCookie("IsOther");
	if (isOther == null)
		isOther = '0';

	var URL = "../ProductGallery/ProductComparison.aspx";
	var CompareProductsValue = GetCookie("CompareProducts");
	if ((CompareProductsValue != "") && (CompareProductsValue != null))
	{
		var arrValues = CompareProductsValue.split(",");
		var ValuesNum = arrValues.length;
		if (ValuesNum < 2)
		{
//			alert ('Please select more than 2 products to compare!');
            alert ('至少需要兩個產品才能進行比較！');

		}
		else
		{
			var CategoryID = GetCookie("ProductCategory");
			if ((CategoryID != "") && (CategoryID != null))
			{
//				if (IsNumber(CategoryID))
//				{
					var QueryString = "Category=" + CategoryID;
					for (i = 0; i < ValuesNum; i++)
					{
						var ProductInfo = arrValues[i].split("|");
//						if (IsNumber(ProductInfo[0]))
//						{
							QueryString += "&Product" + (i+1) + "=" + ProductInfo[0];
//						}
					}
//					alert(URL + "?" + QueryString);
//					window.open(URL + "?" + QueryString);

					QueryString += "&Other=" + isOther; //Add by Ruby Gao on 2009-10-16

					window.location.href = URL + "?" + QueryString;
//					window.open(URL+"?" + QueryString, '_blank', 'location=1,toolbar=1,menubar=1,resizable=1,scrollbars=1');
					
					SetCookie("CompareProducts","",1);
					ClearCompareCheck();
//				}
			}
		}
	}
	else
	{
	    alert('您還未選擇任何需要比較的產品！');
	}

}

function Compare_Check_ENG()
{
    //Add by Ruby Gao on 2009-10-16
	var isOther = GetCookie("IsOther");
	if (isOther == null)
		isOther = '0';
		
	var URL = "../ProductGallery/ProductComparison.aspx";
	var CompareProductsValue = GetCookie("CompareProducts");
	if ((CompareProductsValue != "") && (CompareProductsValue != null))
	{
		var arrValues = CompareProductsValue.split(",");
		var ValuesNum = arrValues.length;
		if (ValuesNum < 2)
		{
//			alert ('Please select more than 2 products to compare!');
            alert ('At least two products to compare!');

		}
		else
		{
			var CategoryID = GetCookie("ProductCategory");
			if ((CategoryID != "") && (CategoryID != null))
			{
//				if (IsNumber(CategoryID))
//				{
					var QueryString = "Category=" + CategoryID;
					for (i = 0; i < ValuesNum; i++)
					{
						var ProductInfo = arrValues[i].split("|");
//						if (IsNumber(ProductInfo[0]))
//						{
							QueryString += "&Product" + (i+1) + "=" + ProductInfo[0];
//						}
					}
//					alert(URL + "?" + QueryString);
//					window.open(URL + "?" + QueryString);

					QueryString += "&Other=" + isOther; //Add by Ruby Gao on 2009-10-16
					
					window.location.href = URL + "?" + QueryString;
//					window.open(URL+"?" + QueryString, '_blank', 'location=1,toolbar=1,menubar=1,resizable=1,scrollbars=1');
					
					SetCookie("CompareProducts","",1);
					ClearCompareCheck();
//				}
			}
		}
	}
	else
	{
	    alert('You do not choose the products to compare!');
	}

}

  function ClearCompareCheck()
    {
        var oCookies = GetCookie("CompareProducts");
        if (oCookies == null || oCookies.length == 0)
        {
            var oProd = $get("tdProductDisplay");
            //alert(oProd);
            var oChks = oProd.getElementsByTagName("input");
//            alert(oChks.length);
            for ( var i = 0; i < oChks.length; i ++)
            {
                //alert(oChks);
                if (oChks[i].type == "checkbox")
                {
                    //alert(oChks[i].type);
                    oChks[i].checked = false;
                }
            }
        }
    }

//=============================================================================================

//function Compare_Hide()
//{
////  obj=document.getElementById("CompareBasket");
////  obj.style.display='none';
//}

//function Compare_Show()
//{
////  obj=document.getElementById("CompareBasket");
////  obj.style.display='block';
//}

//function Compare_HideLayer()
//{
//	SetCookie("HiddenCompare","Hide",24);
//	Compare_Hide();
//}

//function Compare_ShowLayer()
//{
//	SetCookie("HiddenCompare","Show",24);
//	Compare_Show();
//}

//-------------------------------------------------------------------------------------------------------------------------------------------------
//Compare_ReRender(); --del by linxu
//if ((GetCookie("HiddenCompare") == "Hide") || (GetCookie("HiddenCompare") == "") || (GetCookie("HiddenCompare") == null))
//{
//	Compare_HideLayer();
//}
//else
//{
//	Compare_ShowLayer();
//}
//-------------------------------------------------------------------------------------------------------------------------------------------------
//============================================================================================

//recordPositionY = 0;
//function Compare_FixPosition()
//{
//	var recordScrollTop = 0;
//	if(document.documentElement && document.documentElement.scrollTop)
//	{
//		recordScrollTop = document.documentElement.scrollTop;
//	}
//	else
//	{
//		if(document.body)
//		{
//			recordScrollTop = document.body.scrollTop;
//		}
//	}
//	var movePixY = 0.2 * (recordScrollTop - recordPositionY);
//	if(movePixY > 0)
//	{
//		movePixY = Math.ceil(movePixY);
//	}
//	else
//	{
//		movePixY = Math.floor(movePixY);
//	}
//	obj=document.getElementById("CompareBasket");
//	var basketTop = obj.style.pixelTop;
//	obj.style.pixelTop = basketTop + movePixY;
//	recordPositionY = recordPositionY + movePixY;
//}
//window.setInterval("Compare_FixPosition()",2);

