﻿
// Example:
// alert( readCookie("myCookie") );
var promo;
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  {
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    {
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  //alert(cookieValue);
  return cookieValue;
}
// Example:
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours)
{
  var expire = "";
  //if(hours != null)
  //{
    expire = new Date((new Date()).getTime() + 10 * 3600000);
    expire = "; expires=" + expire.toGMTString();
  //}
  document.cookie = name + "=" + escape(value) + expire + "; path=/;";
}


var lastScrollY = 0 ;
var scrollNum = 0 ;
/*
function heartBeat()
{
	diffY = document.body.scrollTop + document.body.clientHeight - document.getElementById("float").scrollHeight - 2;

	if(scrollNum != 1){
		document.all.float.style.display = "none" ;
	}

	if( scrollNum == 0 && document.all.float.style.pixelTop == (document.body.scrollTop + document.body.clientHeight - document.getElementById("float").scrollHeight - 2) ){
		scrollNum = 1;
		//alert("0")
		document.all.float.style.pixelTop = 0;
		lastScrollY = 0 ;
		document.all.float.style.display = "" ;
	}

		percent =0.1*(diffY-lastScrollY);
		if( percent > 0 ) {
			percent = Math.ceil(percent);
		}else{
			percent = Math.floor(percent);
		}
		document.all.float.style.pixelTop += percent;
		lastScrollY = lastScrollY + percent;

}
*/
//window.setInterval("heartBeat()",1);


function hide(){
  obj=document.getElementById("float");
  obj.style.display='none';
}

function show(){

  obj=document.getElementById("float");
  //position:absolute;right:1px;top:100;filter:alpha(opacity=50);z-index:99;width:140;
  obj.style.position='absolute';
  obj.style.right='1px';
  obj.style.top='100px';
  obj.style.filter='alpha(opacity=50)';
  //obj.style.z-index='99';
  obj.style.width='180';
  obj.style.display='';
}
var staticdisplay
function redraw(promo)
{
	document.getElementById("float").style.display = "" ;
	
	//alert("00");
    //heartBeat();
    //show();
    tmp=readCookie("pro_str");
	//alert(tmp);
	if(tmp=="")
	{
		staticdisplay = 1
	}else{
	   staticdisplay = 0
	}
	item_arr=tmp.split("###");
//	floatstr = "<div style='margin:auto;float:left;'><form method='post' action='"+compare_path+"/compare.jsp?P="+promo+"' name='compare' target='_blank' style='margin:0px;'>"+"<table height=100%  bgcolor=#ededed border=1>"+
	floatstr = "<div style='margin:auto;float:left;'><table height=100%  bgcolor=#ededed border=1>"+
	           "<tr><td align='center'><input type='button' value='开始对比' name='submits'  "+
			   "onclick='comparecheck();' style='cursor:pointer;'><br>";
			   //&nbsp;&nbsp;&nbsp;<a href='#' onclick='hide()'>隐藏</a>
	for(key in item_arr) {
	  if (key){
	    //元素形如：2132|我的产品测试
		//alert(item_arr[key]);
		tmp_info_arr=item_arr[key].split("|");
		pid  = tmp_info_arr[0];
		names= tmp_info_arr[1];
        if (pid && names){
          ft = "<font color=red size=1>|</font><br><img src='"+compare_path+"/images/del.gif' onclick='del(\""+pid+"\",\""+names+"\",\"0\");' style='cursor:hand;cursor:pointer;margin-right:2px;' title='从对比列表中删除'><input type=button name=name value='"+names+"' style='border:1px solid;border-color:#D9D9D9 ;background-color:white;height:24;color:black;margin-left:2px;margin-right:2px;' title='"+names+"'><input type='hidden' name='pro_id[]' value='"+pid+"'><br>";
		  floatstr = floatstr+ft;
		}
	  }
	}
	floatstr = floatstr + "<center><input type='hidden' name='subcatid' value='"+readCookie("subcat")+"'><br><a href=\"javascript:empty()\" class=\"gome_keyword\">全部清空</a></center></td></tr></table>";
	floatstr = floatstr + "<IFRAME id='iframe' name='iframe'  height='100%' frameborder='1' src='' style='Z-INDEX:-1;BORDER-TOP-STYLE:none;BORDER-RIGHT-STYLE:none;BORDER-LEFT-STYLE:none;BORDER-BOTTOM-STYLE:none;POSITION:absolute;TOP:0px;LEFT:1px;'></IFRAME></div>";
	document.getElementById("float").innerHTML = floatstr;
}
var expandState = 0;
var oldPosition = 0;
var basketOrgTop=200;
var ie = document.all ? 1 : 0;
var ns = document.layers ? 1 : 0;
var firstStep = 20;
function heartBeat1()
{
	var scrollPos;
	var cHeight;
	//2008.3.17修改
	//修改支持w3c标准
	//主要判断是以typeof作条件判断
	//滚动条顶部到网页顶部的这段距离window.pageYOffset是NS专用属性，它的含义和IE下的document.body.scrollTop一样
	//document.compatMode 文档模式
	//document.documentElement.clientHeight 可见区域高度在FireFox
	//document.body.clientHeight指的是IE中显示页面部分窗口的高度,
	if(staticdisplay==1)
	{
		hide();
	}
	if (typeof window.pageYOffset != 'undefined') {//ns专有属性
		scrollPos = window.pageYOffset;
		cHeight=document.documentElement.clientHeight;

	}
	else if (typeof document.compatMode != 'undefined' &&
		document.compatMode != 'BackCompat') {
		scrollPos = document.documentElement.scrollTop;
		cHeight=document.documentElement.clientHeight;
	}
	else if (typeof document.body != 'undefined') {
		scrollPos = document.body.scrollTop;//ie
		cHeight=document.body.clientHeight;
	}
	var a = scrollPos;
	if(a!=oldPosition){
		document.getElementById("float").style.top = (a+basketOrgTop)+'px';
		oldPosition = a;
	}
}

window.setInterval("heartBeat1()",1);

function empty(){
    var f1 = document.aspnetForm;
	for( var i=0;i<f1.elements.length;i++)
	{
		if(f1.elements[i].type=="checkbox") {
			f1.elements[i].checked=false;
		}
    }
   writeCookie("subcat","");
   writeCookie("pro_str","");
   redraw();

   //window.location="search_good.jsp";
}
function check(chkradio,subcatid,pid,names,promo){
    var f1 = document.aspnetForm;
    var chkID =document.getElementById("compare"+pid);
    var i=chkradio;
	if(f1.compare.length>1 || i>0)
	{
		if(f1.compare[i].checked)
		{
		add(subcatid,pid,names,promo);
		}
		else
		{
		del(pid,names,promo);
		}
	}
	else
    {
		if(f1.compare.checked)
		{
		add(subcatid,pid,names,promo);
		}
		else
		{
		del(pid,names,promo);
		}
		//alert("对不起，该页只有一个产品时不能比较！");
        //chkID.checked =false;
	}
}
function comparecheck(){
    obj=document.aspnetForm;
	mark=0;
	for(k=0;k<obj.elements.length;k++)
        {
	  if (obj.elements[k].name=="pro_id[]"    &&  obj.elements[k].value!="")
          {
	    //alert(obj.elements[k].name+"===第"+k+"个元素==="+obj.elements[k].value);
            if (obj.elements[k].value.indexOf("undefined")!=(-1))
            {
		   obj.elements[k].value=obj.elements[k].value.replace("undefined","");
            }
	    mark++;
	  }
	}
	if(mark<2) {
		//alert ('请至少选择两款产品进行对比!');
		alert ('系统提示：请至少选择两款产品进行对比！！！\r\n选择方法：点击对应产品的“加到对比篮”即可添加该产品到产品对比篮。 ');
	}
        else
        {
           window.open('/product/Compare.aspx');
	}
}

//pro_str:产品字符串
//subcat :子类字符串
function add(subcatid,pid,names,promo){
  var chkID =document.getElementById("compare"+pid);
  //检查是否为相同的子类
  subcat_id_change(subcatid,pid,names)
  //读出产品串后用函数返回一个数组
  tmp=readCookie("pro_str");
  //检查数组的个数（即选择产品的个数）
  tmp_arr=stringToArray(tmp,"###");
  if (tmp_arr.length<6)
  {
     //看是否有相同的产品已被选择
	 //alert(tmp+"=="+pid+"==="+tmp.indexOf(pid));
	 if (tmp.indexOf(pid+"|")!=-1)
        {
	    alert("（"+names+"）已经选择了！！！");
            chkID.checked =false;
	}
        else
        {
            writeCookie("pro_str",tmp+pid+"|"+names+"###");
        }
  }
  else
  {
    alert("对不起，您只能选择五款产品比较！！！");
    chkID.checked =false;
  }

   redraw(promo);
}
//该函数功能是
function subcat_id_change(subcat,id,name){
  //alert("函数参数subcatid="+subcat);
  //change=false;

  tmp_subcat=readCookie("subcat");
  if (tmp_subcat==null || tmp_subcat==""){
    //还没有选择产品呢！！！
	writeCookie("subcat",subcat);
	//alert(readCookie("zol_pro_cmp_subcat"));
  } else {
	writeCookie("subcat",subcat);
  }
  /*else if (subcat != tmp_subcat){
    //选择另类产品
	if (confirm("您选择的产品和以前选择的产品不是同类产品，无法比较,确实要这样做吗？（单击确定，将清掉以前选择的产品）")){
        var f1 = document.aspnetForm;
        //var chkID =document.getElementById("compare"+pid);
	for( var i=0;i<f1.elements.length;i++)
	{
            if(f1.compare[i].value!=id){
                f1.compare[i].checked=false;
            }
        }
	  writeCookie("subcat",subcat);
	  writeCookie("pro_str","");
	  //change=true;
	}
	//end of if
  }*/
  //return change;
}
//用指定的字符串div切开str字符串后返回一个数组
function stringToArray(str,div){
  objArr=str.split(div);
  return objArr;
}
function del(pid,name,promo){
  tmp=readCookie("pro_str");
  //alert(tmp);
  //alert(pid+"|"+name+"###");
  //alert(tmp.replace(pid+"|"+names+"###"));
  writeCookie("pro_str",tmp.replace(pid+"|"+name+"###",""));
   var f1 = document.aspnetForm;
	for( var i=0;i<f1.elements.length;i++)
	{
		if(f1.elements[i].type=="checkbox"&&f1.elements[i]==document.getElementById("compare"+pid)) {
			f1.elements[i].checked=false;
		}
    }
  tmp=readCookie("pro_str");
  //如果没有产品了，自动清空subcat
  if (tmp=="" || tmp==null){
    writeCookie("subcat","");
  }
  redraw(promo);
}
redraw(promo);
