﻿//得到cookie中的值
function CookieValue(name)
{
    var new_arg=name+"=";
    var clen=document.cookie.length;
    if (document.cookie.indexOf(new_arg)!=-1)
    {
        vstr=document.cookie.indexOf(new_arg)+new_arg.length;
        if (document.cookie.indexOf(";",vstr)==-1)
            vend=document.cookie.length;
        else
            vend=document.cookie.indexOf(";",vstr);
        return document.cookie.substring(vstr,vend);
    }
    return "0";
}

//隐藏漂浮对比篮
function hide(){
    obj=document.getElementById("float");
    obj.style.display='none';
}
//通过点击button删除产品
function DelCookieValById(id,checkbox)
{
    eval("var cBox=document.compForm."+checkbox);
    var exp=new Date();
    exp.setTime(exp.getTime()-1000);

    var vstart = -1;
    var vend = -1;
    var vlength = 0;
    var Cookie=CookieValue("CompNew");
    vlength = Cookie.length;
    if(id)
    {
        var new_arg = "&&&";
        if((vstart = Cookie.indexOf(id)) != -1)
        {
            if((vend = Cookie.indexOf(new_arg,vstart)) != -1)
            {
                vend += new_arg.length;
                document.cookie = "CompNew="+Cookie.substring(0,vstart) + Cookie.substring(vend, vlength);
                redraw();
                document.cookie = "productid_" + id + "=0;expires=" + exp.toGMTString();
            }
        }
    }
    if(cBox!=null&&cBox.checked==true){
       cBox.checked=false;
    }
}

//通过取消复选框删除产品
function DelCookieVal(pid,name,checkbox)
{
    var Cookie=CookieValue("CompNew");
    var names=pid+"|"+name+"|"+checkbox;
    if(names)
    {
        var new_arg=names+"&&&";
        if (Cookie.indexOf(new_arg)!=-1)
        {
            vstr=Cookie.indexOf(new_arg);

            if (Cookie.indexOf(";",vstr)==-1)
                vend=Cookie.length;
            else
                vend=Cookie.indexOf(";",vstr);
            document.cookie="CompNew="+Cookie.substring(0,vstr)+Cookie.substring(vstr+new_arg.length,vend);
                redraw();
            return 1;
        }
    }
    return "0";
}

//显示提示信息
var oldtext="加速变量";
function document.onmousemove(){
    try{
        if(event.srcElement.getAttribute('caution'))
        {
            if(showdiv.style.display=='none')
            {
                showdiv.style.left=event.x+6;
                showdiv.style.top=event.y+document.body.scrollTop;
            }
            if(event.srcElement.caution!=oldtext)
            {
                oldtext=event.srcElement.caution;
                showdiv.innerText=oldtext;
                showdiv.style.backgroundColor="#F5FFEB"
            };
            if(showdiv.style.display=='none') showdiv.style.display=''
        }
        else
        {
            if(showdiv.style.display=='') showdiv.style.display='none';
        }
    }catch(e){}
}
//用指定的字符串div切开str字符串后返回一个数组
function stringToArray(str,div){
  objArr=str.split(div);
  return objArr;
}


//点击checkbox时触发的事件，向cookie中添加或删除值
function updCompCom(Com,check,obj)
{
    //alert(document.cookie);
    //alert(obj);
    var Cookie=CookieValue("CompNew");
      //检查数组的个数（即选择产品的个数）
      tmp_arr=stringToArray(document.cookie,"&&&");
      namearray=Com.split("|");
      var new_arg=Com+"&&&";
      //alert(namearray[0]);
    if (Cookie.indexOf(new_arg)==-1)
    //if (document.cookie.indexOf(new_arg)==-1)
    {
        if(check)
        {
            if (CookieValue("CompNew")!=0)
            {
                if(tmp_arr.length<5)
                {
                document.cookie="CompNew="+CookieValue("CompNew")+new_arg;
                }
                else
                {
                 var cookiee=CookieValue("CompNew")+new_arg;
                 
                 var c = cookiee.split("&&&");
                 cookiee = c[1] + "&&&" + c[2] + "&&&" + c[3] + "&&&";
                 document.cookie="CompNew=" +cookiee + new_arg;
                 //alert(document.cookie);
                }
            }
            else
            {
                document.cookie="CompNew="+new_arg;
            }
        }
        //alert(document.cookie);
    }
    else
    {
        if (Cookie.indexOf(new_arg)==-1&&check==true)
        {
            obj.checked=false;
            alert("对不起，您只能选择4个简章进行比较！！！");

        }

        else if(!check)
            return DelCookieVal(namearray[0],namearray[1],namearray[2]);

    }
        redraw();
    return 1;
}


if (document.cookie.indexOf("CompNew")==-1)
{
    document.cookie="CompNew=0";
    if (document.cookie.indexOf("CompNew")==-1)
        alert("简章对比功能要用到您的浏览器的Cookie属性，\n请在您浏览器的internet属性中打开您的");
}

function ocTR(theTR,theMethod){
    document.all(theTR).style.display=theMethod;
}
function StartComp()
{
    var exp=new Date();
    exp.setTime(exp.getTime() + (3600 * 1000));

    var temp=CookieValue("CompNew");
    //document.cookie = "CompNew=0;expires=" + exp.toGMTString() + ";path=/;domain=;domain=.allfang.com";
    var namearray = temp.split("&&&");

    for(i = 0; i < namearray.length; i++)
    {
        temparray = namearray[i].split("|");
        if(temparray[0] != '') {
            document.cookie="productid_" + temparray[0] + "=0;expires=" + exp.toGMTString();
        }
    }
    return 1;

}

function SubmitComp()
{
    StartComp();var ids = "";
    var Cookie=CookieValue("CompNew");

    //alert("cookie==" + Cookie);
    var temp = Cookie.split("&&&");
    for(i = 0; i< temp.length; i++){
        temparray = temp[i].split("|");
        if(temparray[0]!='' && temparray[0] != '0'){
            ids += temparray[0] + ",";
        }
    }
    //alert("ids===" + ids);
    if(ids==""){
        alert("请您选择简章，最多为4个");
        return;
    }
    if(ids.indexOf(",")==ids.length-1){
        alert("请您最少选择2种简章");
        return;
    }
    document.compForm.action="/rules/contrast.aspx?pid=" + ids;
    document.compForm.target="_blank";
    document.compForm.submit();
    redraw();

    //document.location.reload();
    return 1;
}
//清空cookie
function empty()
{
    var exp=new Date();
    exp.setTime(exp.getTime()-1000);

    var temp=CookieValue("CompNew");
    var namearray = temp.split("&&&");

    for(i = 0; i < namearray.length; i++)
    {
        temparray = namearray[i].split("|");
        if(temparray[0] != '') {
            document.cookie="productid_"+temparray[0]+"=0;expires="+exp.toGMTString();
        }
    }
    document.cookie="CompNew=0;expires="+exp.toGMTString();
    //alert(document.cookie);
    document.location.reload();
}
lastScrollY = 0;
function heartBeat()
{
    diffY    = document.body.scrollTop;
    //alert(diffY);
    percent =.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 redraw()
{
//    if(document.getElementById("float")==null)
//    {
//        alert("您的浏览器可能屏蔽了浮动对比框，\n请您关掉浏览器的屏蔽功能，\n然后重新打开此页，进行简章对比");
//         return ;
//    }
    //alert(CookieValue("CompNew"));
    obj=document.getElementById("float");
    obj.style.display='';
   //var pid;
    //var names;

    heartBeat();
    tmp=CookieValue("CompNew");
    //alert(tmp);
    item_arr=tmp.split("&&&");

    floatstr = "<table height=100% width=100% cellpadding=0 cellspacing=0><tr><td>"
             +"<table height=100% width=100%  cellpadding=0 cellspacing=0>"
             +
               //"<form action=comp.php method=get name=formComp >"+
        "<tr><td align=center height=29 background=/img/dbbj.gif><a href='javascript:void(0)' onclick='empty()'><font color='blue' style=font-size:12px;>清空</font></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript: ;' onclick='hide()'><font color='blue'style=font-size:12px;>隐藏</font></a></td></tr>";
    for(key in item_arr) {
      if (key){
        //元素形如：2132|我的产品测试
        tmp_info_arr=item_arr[key].split("|");
        pid  = tmp_info_arr[0];
        names= tmp_info_arr[1];
        checkbox= tmp_info_arr[2];
        if (pid && names){
            ft = "<tr><td height=30 align=center background=/img/dbbjdb.gif><a title='"+names+"'><input type=button name=names value='"+names+"' style='border:0px solid;border-color:#90ABC1 ;background:url(/img/dbbjc.gif) no-repeat;height:24;width:125;padding-left:12px; cursor:hand;color:'black';'></a> <input value='×' type=button onClick=DelCookieValById('"+pid+"','"+checkbox+"') caution='删除' style='cursor:hand; border:0px;height:24px; width:10px; background:#FFEFD5; color:#FF0000;font-weight:bold;'></td></tr>";
            //ft = names+"<br>";
            //<input type=button name=name value='"+names+"' onclick=\"DelCookieVal('"+pid+"','"+names+"')\" style='border:1px solid;border-color:red ;background-color:white;height:24;width:150;cursor:hand;color:'black';'><br>";
          floatstr = floatstr+ft;
        }
      }
    }
    //floatstr =floatstr + "<input type='hidden' name=CompNewCode value=''>";
    floatstr = floatstr + "<tr><td align=center height=33 background=/img/dbbjd.gif><input type='hidden' name='CompNewCode' value='"+"'>"
    +"<input type='button' value='简章对比' name='submits'"+
        "onclick='SubmitComp()' style='cursor:hand;background-color:#F3AA1E;'></td></tr><tr><td height=5></td></tr>";
    floatstr = floatstr +"</td></tr></table></td></tr></table>";
    //alert(floatstr);
    document.all.float.innerHTML = floatstr;
    
}
    redraw();

