﻿// JScript 文件

function AddFavorite()
{
	sURL="http://"+window.location.host;
	sTitle=document.title;
    try
    {
        window.external.addFavorite(sURL, sTitle);
    }
    catch (e)
    {
        try
        {
            window.sidebar.addPanel(sTitle, sURL, "");
        }
        catch (e)
        {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}
function SetHome(obj){
	vrl="http://"+window.location.host;
        try{
                obj.style.behavior='url(#default#homepage)';obj.setHomePage(vrl);
        }
        catch(e){
                if(window.netscape) {
                        try {
                                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
                        } 
                        catch (e)  {
                                alert("此操作被浏览器拒绝！"); 
                        }
                        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
                        prefs.setCharPref('browser.startup.homepage',vrl);
                 }
        }
}
function setCookie(name,value) 
{
   var today = new Date();
   var expires = new Date();
   expires.setTime(today.getTime() + 1000*60*60*24*365);
   document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString()+";path=/";
}

function getCookie(Name) 
{
   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;
         return unescape(document.cookie.substring(offset, end));
      }
      else return('');
   }
   else return('');
}
//加入历史记录
function addHistoryProduct(productid,productname,price,pic,maxLength)
{
var productHistory=getCookie("producthistory");

var array= productHistory.split("|");
for(i=0;i<array.length;i+=4)
{
 if(productid== array[i])
{return;
}
}

if(array.length<maxLength*4)
{  
productHistory=productid+"|"+productname+"|"+price+"|"+pic+"|"+productHistory;
}else
{  
productHistory=productid+"|"+productname+"|"+price+"|"+pic;
for(i=0;i<array.length-4;i+=4)
{
 productHistory=productHistory+"|"+array[i]+"|"+array[i+1]+"|"+array[i+2]+"|"+array[i+3];
}
}

setCookie("producthistory",productHistory);
}


//显示历史记录
function listHistoryProduct(historyid)
{

var productHistory=getCookie("producthistory");
var array= productHistory.split("|");

var historyDiv=document.getElementById(historyid);
if(historyDiv!=null)
{
historyDiv=historyDiv.innerHTML
}
 
var str="";
for(i=0;i<array.length;i+=4)
{
if(historyDiv!=null)
{
if(i+3<array.length)
{
var content=historyDiv.replace("@productid",array[i]);
content=content.replace("@productname",array[i+1]);
content=content.replace("@price",array[i+2]);
content=content.replace("@pic",array[i+3]);
str+=content;
}
}
}
 document.getElementById(historyid).innerHTML=str;
}

