/*
请保留此信息，不会影响程序的执行。试用于ff2.0和ie6，其他浏览器没测试。
            --2008-4-16,ShowBo
*/
//扩展string原型
String.prototype.trim=function(reg){  if(reg) return this.replace(reg,""); else return this.replace(/^\s*|\s*$/g,"");}
function Product(CookieName)
{
  this.Product=new Array();  
  this.Init=function()
  {
    //生成数组
    var match=document.cookie.match(new RegExp(CookieName+"=([^;]+);?","i"));
    var cookieStr=match?match[1]:"";
    if(cookieStr.trim()!="")
    {
      var ps=cookieStr.split("|");
      var p;
      for(var i=0;i<ps.length;i++)
      {
         p=ps[i].split(",");
         this.Product.push(this.createObj(p[0],unescape(p[1])));
      }
    }
  }
  //重写toString方法
  this.toString=function()
  {
     var Str="",o;
     for(var i=0;i<this.Product.length;i++)
     {
       o=this.Product[i];
       Str+=o.id+","+escape(o.name)+","+o.price+","+o.count+"|";
     }
     return Str.trim(/\|$/);
  }
  //产品对象
  this.createObj=function(id,name)
  {
     var o=new Object();
     o.id=id;
     o.name=name;
     return o;
  }
}
function Car(CookieName,txtTotalMoney)
{
   this.expires=30;//存活期限30天
   this.Product=new Product(CookieName);//产品对象
   this.Product.Init();//初始化
   this.RefreshCar=function()
   {
     this.Product=new Product(CookieName);
     this.Product.Init();
   }
   this.Add=function(id,name)
   {    
      if(!id||!name)
      {
        alert('参数不正确！');
        return;
        }
      var o=this.FindProductObj(id.trim());
      if(o)//存在这个id物品，则在原来基础上加1
      {
         
      }
      else
      {
        this.Product.Product.push(this.Product.createObj(id,name));//添加新产品
      }
      this.Save();//保存cookie
   }
   
   
   this.Save=function()
   {      
      var d=new Date();
      d.setDate(d.getDate()+this.expires);
      document.cookie=CookieName+"="+this.Product.toString()+";expires="+d.toGMTString();
   }
   this.FindProductObj=function(id,NeedIndex)
   {
      var o;
      for(var i=0;i<this.Product.Product.length;i++)
      {
         o=this.Product.Product[i];
         if(o.id==id)
         {
           if(NeedIndex)//需要索引位置
              return i;
           return o;
         }
      }
      return null;
   }

   this.Read=function()
   {
      var Str="";
      if(this.Product.Product.length==0)
      {
         Str="";
      }
      else
      {
		var p = 1;
        var o,tm=0;  
		var i = this.Product.Product.length - 1;
		Str += "<table width=\"183\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td height=\"7\" colspan=\"3\"></td>";
		Str += "</tr><tr><tr><td height=\"27\" colspan=\"3\" background=\"/images/hsh6.gif\" class=\"hsh6\" align=\"left\">您最近查看过的酒店</td></tr>";
        Str += "<tr><td width=\"1\" bgcolor=\"#7bbdff\"></td><td width=\"181\" bgcolor=\"#ffffff\" align=\"left\">";
		for(;i>=0;i--)
        {
           o=this.Product.Product[i];
           Str+="<li id=\"hsh7\"><a href=\"" + o.id + "\" target=\"_blank\" title=\"" + o.name + "\">";
			if(o.name.length > 12)
			{
				
				Str += o.name.substr(0,10) + "...";
			}
			else
			{
				Str += o.name;
			}
		   Str += "</a></li>";
		   if(p >= 7)
			{
			break;
		   }
		   p++;
        } 
		Str += "</td><td width=\"1\" bgcolor=\"#7bbdff\"></td></tr><tr><td colspan=\"3\" height=\"4\"><img src=\"/images/hsh7.gif\"/></td></tr></table>";
      }
      return Str;
   }
    
}


//参数名称为cookie的名称和接收总价钱的控件的id 
var CarObj=new Car('hotelshy766','TotalMoney');