var curY = 0;
var offY = 0;
var abY = 0;
var length = 1;
var num = 5;
var name = new Array(num);
var photo = new Array(num);
var userId = new Array(num);
var carId = new Array(num);


function FixPosition()
{
var obj = document.getElementById("CompareBasket");
curY = Math.ceil(obj.style.top.replace(/px/,""));
abY  = Math.ceil(document.documentElement.scrollTop);
offY = 0.1 * (abY - curY);
curY += offY;
obj.style.top = String(curY) + "px";
}
srlIndex = window.setInterval("FixPosition()",2);

/*function initDiv(){
  window.clearInterval(srlIndex);  //清除定时
  document.getElementById("compareBox").style.top = initTop;  //设置为初始化位置
  document.getElementById("compareBox").style.left = initLeft;
}*/
// unicode 编码程序
function BasketEncodeCookie(InputString)
{
	var strRtn="";
	for (var i=InputString.length-1;i>=0;i--)
	{
		strRtn+=InputString.charCodeAt(i);
		if (i) strRtn+="a"; // 用 a 作分隔符
	} 
	return strRtn;
}

//unicode 解码程序
function BasketDecodeCookie(InputString)
{
	var strArr;
	var strRtn="";

	strArr=InputString.split("a");

	for (var i=strArr.length-1;i>=0;i--)
		strRtn+=String.fromCharCode(eval(strArr[i]));

	return strRtn;
}

// 读 Cookie
function BasketGetCookie(name)
{
	var strArg=name+"=";
	var nArgLen=strArg.length;
	var nCookieLen=document.cookie.length;
	var nEnd;
	var i=0;
	var j;
	//alert(strArg);
	while (i<nCookieLen)
	{
		j=i+nArgLen;
		//alert("i="+i+"---------j="+j);
		//alert("1-"+document.cookie.substring(i,j));
		//alert("2-"+strArg);
		if (document.cookie.substring(i,j)==strArg)
		{
		//alert(1);
			nEnd=document.cookie.indexOf (";",j);
			if (nEnd==-1) nEnd=document.cookie.length;
			//var q = BasketDecodeCookie(unescape(document.cookie.substring(j,nEnd)));
			//alert(q);
			return unescape(document.cookie.substring(j,nEnd));
			
		}
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
}

// 写 CookieBasketSetCookie("CAR_HiddenCompare","Hide",24);
function BasketSetCookie(name,value,expires)
{
	var exp = new Date();
	exp.setTime(exp.getTime()+expires*60*60*1000);
	document.cookie=name+"="+escape(value)+";expires="+exp.toGMTString();
	//alert(document.cookie);
}

// 判断 Cookie 是否存在并写产品对比 Cookie   ("CAR_CompareProducts",ProductSN,24);
function BasketCheckSetCookieValue(name,value,expires)
{

	var nameCookieValue = BasketGetCookie(name);
	
	if ((nameCookieValue == "") || (nameCookieValue == null))
	{
		var exp = new Date();
		exp.setTime(exp.getTime()+expires*60*60*1000);
		document.cookie=name+"="+escape(value)+";expires="+exp.toGMTString();
	}
	else
	{
		if (nameCookieValue.indexOf(value) == -1)
		{
			var arrCookies = nameCookieValue.split("；");
			var ValueNum = arrCookies.length;
			if (ValueNum > 100)
			{
				nameCookieValue = "";
				for (loop=0; loop < 100; 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(value+"；"+nameCookieValue)+";expires="+exp.toGMTString();
		}
	}
}



function hide()
{
  
  obj=document.getElementById("CompareBasket");
  obj.style.display='none';
}

function show()
{
  obj=document.getElementById("CompareBasket");
  obj.style.display='block';
}

function Empty()
{
    length = 1;
    name = new Array(num);
    photo = new Array(num);
    userId = new Array(num);
    carId = new Array(num);

    setCmpName();
    var CompareProducts = BasketGetCookie("CAR_CompareProducts");
	if (CompareProducts == null)
	{
		CompareProducts = "";
	}
	
	var arrCookies = CompareProducts.split("；");
	//alert(arrCookies);
	for (i = 0; i < arrCookies.length; i++)
	{
	
		if ((arrCookies[i] != "") && (arrCookies[i] != null))
		{
			
			//alert(arrCookies[i]);
					var ProductSN =arrCookies[i]; 
					//alert(ProductSN);
					if (ProductSN == null)
					{
						ProductSN = "";
					}
					
					Ajax_CompareSetElement(carCmpName,false,'id='+ProductSN);
					
		}
	}
	
	BasketSetCookie("CAR_SubCategory","",24);
	BasketSetCookie("CAR_CompareProducts","",24);
	
	
	//alert(carCmpName);
	//AllCheckboxStatus(document.carform,document.carform.modefykey1,document.carform.modefykey10);
	//Ajax_CompareSetElement(carCmpName,false,'id='+carid);
	compareCarsSelectAll(document.carform,carCmpName,false);
	redraw();
}

// 判断选择的产品和以前选择的产品是否同类同类产品
function CheckCategoryChange(SubCategory)
{

	var SubCategorySN = BasketGetCookie("CAR_SubCategory");
	//alert(SubCategorySN);
	if ((SubCategory == null) || (SubCategory == ""))
	{
		BasketSetCookie("CAR_SubCategory",SubCategory,24);
	}
	else
	{
		if (SubCategory != SubCategorySN)
		{
			BasketSetCookie("CAR_SubCategory",SubCategory,24);
			BasketSetCookie("CAR_CompareProducts","",24);
		}
	}
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function Ajax_compare(url,id,cartype) 
{
	http_request = false;
	var carid = id;
	var type = cartype;
	
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType("text/xml");
		}
	} else {
		if (window.ActiveXObject) { // IE
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
				}
			}
		}
	}
	if (!http_request) {
		//alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = findInfo;
	var str_url = url + "?id=" + carid+"&cartype="+type;//+"&temp="+Math.random();
	//alert(str_url);
	http_request.open("GET", str_url, true);
	
	http_request.send(null);
	
}

function findInfo()
{
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var res = http_request.responseText;
			var paraArray = res.split("|");
			/*
			var CompareProducts = BasketGetCookie("CAR_CompareProducts");
			var arrCookies=false;
			if ((CompareProducts != "") && (CompareProducts != null)){
			    arrCookies = CompareProducts.split("；");
				}
			var length = arrCookies.length;
			*/
			//alert(arrCookies);
			if (paraArray.length == 4) {
			if(length <num+1){
			    userId[length]= paraArray[0];
			    name[length]  = paraArray[1];
			    photo[length] = paraArray[2];
			    carId[length] = paraArray[3].replace("\r\n","");
			    //alert(1+"----------------------------------"+name[1]);
			    length+=1;
			    
			    }
			    
			}
		}
	}
	//redraw();
	//ShowLayer();
}


function Ajax_compare2(url,id,cartype) 
{
	http_request = false;
	var carid = id;
	var type = cartype;
	
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType("text/xml");
		}
	} else {
		if (window.ActiveXObject) { // IE
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
				}
			}
		}
	}
	if (!http_request) {
		//alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = findInfo2;
	var str_url = url + "?id=" + carid+"&cartype="+type;//+"&temp="+Math.random();
	//alert(str_url);
	http_request.open("GET", str_url, true);
	
	http_request.send(null);
	
}

function findInfo2()
{
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var res = http_request.responseText;
			var paraArray = res.split("|");
			/*
			var CompareProducts = BasketGetCookie("CAR_CompareProducts");
			var arrCookies=false;
			if ((CompareProducts != "") && (CompareProducts != null)){
			    arrCookies = CompareProducts.split("；");
				}
			var length = arrCookies.length;
			*/
			//alert(arrCookies);
			if (paraArray.length == 4) {
			if(length <num+1){
			    userId[length]= paraArray[0];
			    name[length]  = paraArray[1];
			    photo[length] = paraArray[2];
			    carId[length] = paraArray[3].replace("\r\n","");
			    //alert(1+"----------------------------------"+name[1]);
			    length+=1;
			    
			    }
			    
			}
			redraw();
			ShowLayer();
		}
	}
	
}
function Ajax_compare3(url,id,cartype) 
{
	http_request = false;
	var carid = id;
	var type = cartype;
	
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType("text/xml");
		}
	} else {
		if (window.ActiveXObject) { // IE
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
				}
			}
		}
	}
	if (!http_request) {
		//alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = findInfo3;
	var str_url = url + "?id=" + carid+"&cartype="+type;//+"&temp="+Math.random();
	//alert(str_url);
	http_request.open("GET", str_url, true);
	
	http_request.send(null);
	
}

function findInfo3()
{
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var res = http_request.responseText;
			var paraArray = res.split("|");
			/*
			var CompareProducts = BasketGetCookie("CAR_CompareProducts");
			var arrCookies=false;
			if ((CompareProducts != "") && (CompareProducts != null)){
			    arrCookies = CompareProducts.split("；");
				}
			var length = arrCookies.length;
			*/
			//alert(arrCookies);
			if (paraArray.length == 4) {
			if(length <num+1){
			    userId[length]= paraArray[0];
			    name[length]  = paraArray[1];
			    photo[length] = paraArray[2];
			    carId[length] = paraArray[3].replace("\r\n","");
			    //alert(1+"----------------------------------"+name[1]);
			    length+=1;
			    
			    }
			    
			}
			redraw();
			ShowLayer();
			doAjaxCircle();
		}
	}
	
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////



function AddCompareProduct(ProductSN,SubCategory)
{
	//CheckCategoryChange(SubCategory);
	//alert(length);
	var CompareProducts = BasketGetCookie("CAR_CompareProducts");
    //alert(CompareProducts);
	if ((CompareProducts != "") && (CompareProducts != null))
	{
		var arrCookies = CompareProducts.split("；");
		//alert(arrCookies.length);
		if (arrCookies.length <num)
		{
		    //alert(arrCookies);
		    
			if (CompareProducts.indexOf(ProductSN) != -1)
			{
			
				alert("（" + ProductSN + "）已经被选择了！");
			}
			
			else
			{
				BasketCheckSetCookieValue("CAR_CompareProducts",ProductSN,24);
				Ajax_compare2('AjaxCompareAction.do',ProductSN,1);
			}
		}
		else
		{
			alert("对不起！您只能选择五款同类产品进行比较。");
			redraw();
			ShowLayer();
		}
		
	}
	else
	{
		BasketCheckSetCookieValue("CAR_CompareProducts",ProductSN,24);
		//alert(1);
		Ajax_compare2('AjaxCompareAction.do',ProductSN,1);
	}
	//redraw();
	//ShowLayer();
}


function DelProduct(ProductSN)
{
    //alert(ProductSN);
    
    
    var Products = BasketGetCookie("CAR_CompareProducts");
    var Cookies = "";
	//alert(Products);
	if (Products == null)
	{
		Products = "";
	}
	if((Products != "") && (Products != null)){
	    
	     Cookies = Products.split("；");
	}
    //alert("1--------"+carId);
    
    for(i=1;i<=Cookies.length;i++){
    	if(carId[i]==ProductSN){
    	length--;
    		//alert(ProductSN+"--------"+carId[i]);
       		for(j=i;j<=Cookies.length;j++){
       		    //alert(carId[j]);
          		carId[j]=carId[j+1];
          		userId[j]=userId[j+1];
          		name[j]=name[j+1];
          		photo[j]=photo[j+1];
       		}
       		carId[Cookies.length]="";
       		userId[Cookies.length]="";
       		name[Cookies.length]="";
       		photo[Cookies.length]="";
       	}
    }
    //alert("2----------"+carId);
    
	setCmpName();
	var nameCookieValue = BasketGetCookie("CAR_CompareProducts");
	//alert(nameCookieValue);
	if ((nameCookieValue != null) && (nameCookieValue != ""))
	{
		if (nameCookieValue.indexOf(ProductSN) > -1)
		{
			var arrCookies = nameCookieValue.split("；");
			//alert(arrCookies);
			var ValueNum = arrCookies.length;
		    
			nameCookieValue = "";

			for (i=0; i < ValueNum; i++)
			{
				if (arrCookies[i].indexOf(ProductSN) == -1)
				{
					nameCookieValue += arrCookies[i] + "；";
					//alert(nameCookieValue);
				}
				//AllCheckboxStatus(document.carform,document.carform.modefykey1,document.carform.modefykey10);
				
				Ajax_CompareSetElement(carCmpName,false,'id='+ProductSN);
			}
			nameCookieValue = nameCookieValue.substring(0,nameCookieValue.length - 1);
			BasketSetCookie("CAR_CompareProducts",nameCookieValue,24);
		}
		redraw();
	}
}



function redraw()
{  //alert(3+"----------------------------------"+name[1]);
	//FixPosition();
	//document.write("12");
	
	var CompareProducts = BasketGetCookie("CAR_CompareProducts");
	//alert(CompareProducts);
	if (CompareProducts == null)
	{
		CompareProducts = "";
	}
	
	var arrCookies = CompareProducts.split("；");
	//alert(arrCookies);
	var CompareTable = "<table width='100%'>"
		+ "<tr>"
		+ "<th>车辆对比</th>"
		+ "</tr>"
		+ "<tr>"
		+ "<td>";
	for (i = 0; i < arrCookies.length; i++)
	//for (i = arrCookies.length+1; i>0; i--)
	{
		if ((arrCookies[i] != "") && (arrCookies[i] != null))
		{
				var ProductInfo = arrCookies[i];
				//alert("ProductInfo="+ProductInfo);
				if (ProductInfo.length == 32)
				{  
				//alert("ProductInfo="+ProductInfo.length);
				
					var ProductSN = ProductInfo;
					var ProductLink = "car-detail-"+carId[i+1]+"-1.htm";
					//var ProductImg = "http://www5.1bib.com/upload/"+userId[i+1]+"/s/"+photo[i+1];
					
			        
				    var ProductImg = "http://image.1bib.com/upload/"+userId[i+1]+"/s/"+photo[i+1];
				    
				    
				    if(photo[i+1]==null||photo[i+1]=="null"||photo[i+1]=="")
				       ProductImg ="http://image.1bib.com/upload/default/m/nopic.gif";
				    
				    /*
				    if(photo[i+1]==null || photo[i+1]==""){
				       ProductImg = "http://www5.1bib.com/upload/default/m/nopic.gif";
				       }
				    else{
				       ProductImg = "http://www5.1bib.com/upload/"+userId[i+1]+"/s/"+photo[i+1];
				       
				       }
				       
				    */
					var ProductName = name[i+1];
					
					var sn = carId[i+1];
					//alert("sn="+sn);
					//alert("sn="+ProductLink.replace("\r\n",""));
					//alert("sn="+sn);
					//alert("i="+carId[i+1]);
					
					//alert(carId[i+1]);
					//alert("1---"+name);
					
					
					if (ProductSN == null)
					{
						ProductSN = "";
					}
					if (ProductSN != "")
					{
						
						CompareTable = CompareTable +"<span style='color:#FFF5E6'>|</span>"
							+ "<table style='margin:0 auto;'>"
							+ "<tr>"
							+ "<td bgcolor='white'><a href='"+ProductLink+"' target='_blank'><img style='border:1px solid #e1e1e1;' src='" + ProductImg + "' width='72' height='54' alt='" + ProductName + "'></a></td>"
							+ "</tr>"
							+ "</table>"
							+ "<table>"
							+ "<tr>"
							+ "<td height='5'></td>"
							+ "</tr>"
							+ "</table>"
							+ "<input type='button' title='点击删除该产品对比' name='Product" + i + "' value='" + ProductName + "' style='border:1px solid; border-color:#FF9900;width:90px; background-color:white; height:22px;  cursor:pointer; color:#FF9900; font-size:12px' onClick=DelProduct('" + sn + "');>"
							+ "<br>";
					}
				}
		}
	}

	CompareTable = CompareTable + "<table width='100%' border='0' cellpadding='0' cellspacing='0' style='font-size:12px;margin-top:5px'>"
		+ "<tr>"
		+ "<td style='padding:5px 0;border-top:1px dashed #ccc' align='center' valign='bottom'><a onClick=Empty() class='cBlue' href='javascript:void(0)' title='清空对比框'>清空</a>&nbsp;&nbsp;&nbsp;<a onClick='HideLayer()' href='javascript:void(0)'  class='cBlue' title='隐藏对比框'>隐藏</a></td>"
		+ "</tr>"
		+ "</table>"
		+ "</td>"
		+ "</tr>"
		+ "<tr>"
		//+ "<td align='center' bgcolor='#FFF3E1' style='padding:5px 0;'><input type='button' value='对比产品' class='button1' name='CompareBtn' style='cursor:pointer; width:80px; font-size:12px' onClick=compareCarVehicles()></td>"
		+"<td align='center' bgcolor='#FFF3E1' style='padding:5px 0;'><a href='car-compare' target='_blank' class='buttonLink'>对比产品</a></td>"
		+ "</tr>"
		+ "</table>";
		//alert();
	document.getElementById('CompareBasket').innerHTML = CompareTable;
	
}


/*
function IsNumber(inputVal)
{
	var inputStr = inputVal.toString();
	var i = 0;
	for (i =0; i<inputStr.length; i++)
	{
		var oneChar = inputStr.charAt(i)
		if (oneChar < "0" || oneChar> "9")
		{
			return false;
		}
	}
	return true;
}


function CompareCheck()
{
	var URL = "http://product.pcpop.com/ProductComp/ProductCompare.aspx";
	var CompareProductsValue = BasketGetCookie("CAR_CompareProducts");
	if ((CompareProductsValue != "") && (CompareProductsValue != null))
	{
		var arrValues = CompareProductsValue.split("；");
		var ValuesNum = arrValues.length;
		if (ValuesNum < 2)
		{
			alert ('请至少选择两款同类别产品进行对比！');
		}
		else
		{
			var SubCategorySN = BasketGetCookie("CAR_SubCategory");
			if ((SubCategorySN != "") && (SubCategorySN != null))
			{
				if (IsNumber(SubCategorySN))
				{
					var QueryString = "Category=" + SubCategorySN;
					for (i = 0; i < ValuesNum; i++)
					{
						var ProductInfo = arrValues[i].split("|");
						if (IsNumber(ProductInfo[0]))
						{
							QueryString += "&Product" + i + "=" + ProductInfo[0];
						}
					}
					window.open(URL + "?" + QueryString);
				}
			}
		}
	}
}
*/

var Products = BasketGetCookie("CAR_CompareProducts");
	//alert(Products);
	if (Products == null)
	{
		Products = "";
	}

var cookiestemp = "";
if((Products != "") && (Products != null)){
	var Cookies = Products.split("；");
	//length = Cookies.length+1;
	//alert(Cookies.length);
	for(j=1;j<Cookies.length;j++){
		if(j<Cookies.length-1)cookiestemp = cookiestemp + Cookies[j] + ",";
		else cookiestemp = cookiestemp + Cookies[j];
	}
	if ((Cookies[0] != "") && (Cookies[0] != null))
	{
		  //alert(Cookies[i]);
		  Ajax_compare3('AjaxCompareAction.do',Cookies[0],1);
		  //alert(name);
	}
//	for (i = 0; i < Cookies.length; i++)
//	{ 
//		if ((Cookies[i] != "") && (Cookies[i] != null))
//		{
		  //alert(Cookies[i]);
//		  Ajax_compare2('AjaxCompareAction.do',Cookies[i],1);
		  //alert(name);
//		}
//	}
	//redraw();
}
function doAjaxCircle(){
	var Cookies = cookiestemp.split(",");
	//alert(Cookies.length);
	cookiestemp = "";
	if((Cookies != null) && (Cookies.length != 0)){
		for(j=1;j<Cookies.length;j++){
			if(j<Cookies.length-1)cookiestemp = cookiestemp + Cookies[j] + ",";
			else cookiestemp = cookiestemp + Cookies[j];
		}
		if((Cookies[0] != "") && (Cookies[0] != null)){
			Ajax_compare3('AjaxCompareAction.do',Cookies[0],1);
		}
	}
}		  



function HideLayer()
{
	BasketSetCookie("CAR_HiddenCompare","Hide",24);
	hide();
}

function ShowLayer()
{
	BasketSetCookie("CAR_HiddenCompare","Show",24);
	show();
}

if ((BasketGetCookie("CAR_HiddenCompare") == "Hide") || (BasketGetCookie("CAR_CompareProducts") == "") || (BasketGetCookie("CAR_CompareProducts") == null))
{
	HideLayer();
}
else
{
	ShowLayer();
}


function ProductFavorite(Product)
{
alert("添加");
}
