Array.prototype.toTRACKERJSONString=function()
{
var jsonStr="[";
for(var i=0;i<this.length;i++)
 {
    if(this[i] instanceof Parameter)
    {
    if(this[i].value instanceof Array)
      {
      jsonStr+="{"+this[i].key+"="+this[i].value.toTRACKERJSONString()+"}"+",";
      }
    else  
    jsonStr+=this[i].toTRACKERJSONString()+",";
    }
 }
if(jsonStr.indexOf(",")>0)
jsonStr=jsonStr.substring(0,jsonStr.length-1);
return jsonStr+"]";
 
}


///////////////////定义封装对象//////////////////////////
function Parameter(key,value)
{
this.key=key;
this.value=value;

this.toTRACKERJSONString=function()
{
return "{"+this.key+"="+this.value+"}";
};

}

///////////////////定义封装需跟踪库存对象//////////////////////////

function TrackerContainer(url)
{
this.url=url;
this.parameterArray=new Array();
this.stockArray=new Array();
this.commonAttached=new Array();//普通的附加对象（key->value）
this.addParameter=function(parameter)
  {
  this.parameterArray.push(parameter);
  };
 this.addStock=function(productId,stockInfo) //增加库存
  {
   this.stockArray.push(new Parameter(productId,stockInfo));
  };
  this.addCommonAttached=function(key,value) //增加普通的附加信息
  {
  this.commonAttached.push(new Parameter(key,value));
  };

//构建附加信息
this.buildAttached=function()
{
 if(this.stockArray.length>0)
 this.commonAttached.push(new Parameter("1",this.stockArray));
 if(this.commonAttached.length>0)
 this.addParameter(new Parameter("attachedInfo", this.commonAttached.toTRACKERJSONString()));//增加库存信息
}

 
this.toUrl=function()
  {
   this.buildAttached();
   for(var i=0;i<this.parameterArray.length;i++)
      {
      var key=this.parameterArray[i].key;
      var value=this.parameterArray[i].value;
      this.url+="&"+key+"="+value;
      }
    return this.url;  
  };  
}

var trackerContainer=new  TrackerContainer("http://tracker.yihaodian.com/tracker/info.do?1=1");
