

  /*
    购物车
  */
  function Cart (){
    var itemMap = {};
    var totalPrice=0;
    var areaId = '';
    var me = this;
    this.setAreaId = function(areaID){
    	areaId = areaID;
    }
    this.getAreaId = function(){
    	return areaId;
    }
    this.getTotalPrice = function(){
    	return totalPrice;
    }
    //获取商品
    this.getItemById = function(id){
    	if(typeof id =='undefined')return false;
    	return itemMap[id];
     }
	//增加商品
	this.addItem = function(item){
		 var uniId = item.getUniId();
		if(typeof itemMap[uniId]=='undefined'){
			 itemMap[uniId] = item;
		}
		 this.setTotalPrice();//更新合计
	}
	//移除商品
	this.removeItemById = function(uniId){
		 if(typeof uniId =='undefined'){return false;}
		 for(var p in itemMap){
		     if(uniId == p){
			   delete itemMap[p];
			 }
		 }
	}
	//清空购物车
	this.clearCart = function(){
	    itemMap = {};
	    totalPrice = 0;
	    areaId='';
	    this.saveCartTocookie(true);
	}
	//
	//获取购物车中的所有商品
	this.getAllCartItems = function(){
		return itemMap;
	}
	this.getItemNum = function(){
		 var count=0;
		 for(var p in itemMap){
			 count++;
		 }
		 return count;
	}
	this.isEmpty = function(){
		if(this.getItemNum() == 0)
		{
			return true;
		}else{
			return false;
		}
	
	}
	this.updateCartItem = function(cartItem){
		if(typeof cartItem =='undefined'){return false;}
		
		var uniId = cartItem.getUniId();
		if(typeof itemMap[uniId]!='undefined'){
			itemMap[uniId] = cartItem;
		}
		
	}
	this.getCartItemsByType=function(type){
		var cartItems  = new Array();
		for(var p in itemMap){
		       var itemType = itemMap[p].getItemType();
		       if(itemType==type){
		    	   cartItems.push(itemMap[p]);
		       }
		 }
		return cartItems;
	}

	this.addItems = function(cartItems){
		 if(typeof cartItems == 'undefined')return false;
		 for(var p in cartItems){
			 this.addItem(cartItems[p])
		}
	}
	
	//调试方法
	this.getAllCartItems_String = function(){
        var tmp="";
		for(var p in itemMap){
		  tmp += '  ' +p +' = ' + itemMap[p];
		}
	   return tmp;
	}
	//购物车总计
	this.setTotalPrice = function(){
		var tmp_price = new Number();
		for(var p in itemMap){
		       var subPrice = itemMap[p].getSubPrice();
		       if(typeof subPrice !='undefined'){
		    	   var subPrice = new Number(subPrice);
		    	   tmp_price = tmp_price + subPrice;
		       }
		 }
		 totalPrice = tmp_price.toFixed(2);
	}
	//保存至cookie
	this.saveCartTocookie = function (clear){
	   this.setTotalPrice();
	   //清空购物车
	   if(clear){
		   SetCookie('ecwmp_shop_cart',this.toString());
		   return false;
	   }
	   //叠加
	   //先读取cookie
	    var cookie = new Cart();
	    cookie.readFromCookie();
	    if(typeof cookie == "undefined"){
	    	  SetCookie('ecwmp_shop_cart',this.toString());
	    }else{
	    	//已存在
	    		this.addItems(cookie.getAllCartItems());
	    		SetCookie('ecwmp_shop_cart',this.toString());
	    }
	   
	   
	
	}
	//从cookie中读取cart
	this.readFromCookie = function (){
	  var cart = getCookie('ecwmp_shop_cart');
	   if(typeof cart =='undefined'||cart == null){
		   return false;
	   }
	   this.buildCartFromJson(JSON.decode(cart));
	}
	this.buildCartFromJson = function(cart){
	if(typeof cart =="undefined"||typeof cart['items']=='undefined')return false;
		var itemList = cart['items'];
		itemMap = {};
		for(var i=0;i<itemList.length;i++){
		  var cartItem  = new CartItem();
		  var item = itemList[i];
		  itemMap[item['uniId']]  = cartItem.readFromJson(item);
		}
	    totalPrice = cart['totalPrice'];
	    areaId = cart['areaId'];
	}
	this.buildCartJson = function(){
		 var map = {};
		 var itemList = [];
		 for(var p in itemMap){
		    itemList.push(itemMap[p].getJson());
		 }
		 map['items'] = itemList;
		 map['totalPrice'] = totalPrice;
		 map['areaId'] = areaId;
	    return map;
	}
	this.toString = function(){
		return  JSON.encode(this.buildCartJson());
	}
	this.getCartTitle = function(){
		var title =  new Array();
		if(this.isEmpty()){
			return '<table><tbody><th>购物车为空!!!</th></tbody></table>';
		}
		//title.push('<div class="tooltip_top"></div>');
		title.push('<table><tbody><tr><th>商品名称</th><th>价格</th></tr></tbody><tbody>');
		for(var p in itemMap){
			title.push('<tr><td>');
			title.push(itemMap[p].getItemName());
			title.push('</td><td>');
			title.push('&nbsp;￥&nbsp;'+itemMap[p].getSubPrice());
			title.push('</td></tr>');
		}
		title.push('</tbody></table>');
		//title.push('<div class="tooltip_bottom"></div>');
		return title.join('');
	}

  
  
  }




    /*
	 * 购物车 物品
	*/
	function CartItem(params){
	var uniId="";
    var itemId=""; 
	var itemName="";
	var itemQuantity="";
	var itemPrice="";
	var feeInfo="";
	var extraStartCost='';
	var subPrice="";
	var in_netType="";
	var is_part_contract="";
	var itemType="";
	var mobile="";
	var enternet="";
	var customerInfo={};
	var contractId;
	var suitId="";
	var gjType ="";
	var mobileId = "";
	var phoneMissPrice="";
	var contract_price="";
	var mobilePrice="";
	var terminalPrice="";
	var remark="";
	var suitFee="";
	{
	 if(typeof params == "undefined")
	   params={};
	 uniId= params['itemId']+'_'+(new Date()).getTime();
	 itemId = params['itemId'];
	 itemName  = params['itemName'];
	 itemQuantity = params['itemQuantity'];
	 itemPrice = params['itemPrice'];
	 feeInfo = params['feeInfo'];
	 extraStartCost = params['extraStartCost'];
	 subPrice = params['subPrice'];
	 in_netType = params['in_netType'];
	 is_part_contract = params['is_part_contract'];
	 itemType = params['itemType'];
	 mobile = params['mobile'];
	 enternet = params['enternet'];
	 customerInfo = params['customerInfo']||{};
	 contractId = params['contractId'];
	 suitId = params['suitId'];
	 contract_price = params['contract_price'];
	 gjType = params['gjType'];
	 mobileId = params['mobileId'];
	 phoneMissPrice = params['phoneMissPrice'];
	 mobilePrice = params['mobilePrice'];
	 terminalPrice = params['terminalPrice'];
	 remark = params['remark'];
	 suitFee = params['suitFee'];
	}
	this.getItemType = function(){
		return itemType;
	}
	this.setRemark = function(remark_){
		remark = remark_;
	}
	this.setMobile = function(mobile_){
		mobile = mobile_;
	}
	this.getUniId=function(){
		return  uniId;
	}
	this.setSort = function(sort_){
		sort = sort_;
	}
	//set get methods
	this.getItemId = function(){
	  return  itemId;
	}
	this.setItemId = function(itemId_){
	     itemId = itemId_;
	}
	this.getItemName =function(){
	  return itemName;
	}
	this.setItemName = function(itemName_){
	     itemName = itemName_;
	}

	this.getItemQuantity =function(){
	  return itemQuantity;
	}
	this.setItemQuantity = function(itemQuantity_){
	     itemQuantitye = itemQuantity_;
	}

	this.getItemPrice =function(){
	  return itemPrice;
	}
	this.setItemPrice = function(itemPrice_){
	     itemPrice = itemPrice_;
	}

	this.getFeeInfo =function(){
	  return feeInfo;
	}
	this.setFeeInfo = function(feeInfo_){
	     feeInfo = feeInfo_;
	}

	this.getExtraStartCost =function(){
	  return extraStartCost;
	}
	this.setExtraStartCost = function(extraStartCost_){
	     extraStartCost = extraStartCost_;
	}

     this.getSubPrice =function(){
	  return subPrice;
	}
	this.setSubPrice = function(subPrice_){
	     subPrice = subPrice_;
	}
	this.getCusInfo = function(){
		return customerInfo;
	},
	this.setCusInfo = function(cusInfo){
		customerInfo = cusInfo;
	},
	this.addStartCost = function(startcost){
		if(typeof startcost =='undefined'){return false;}
		 var sc = new Number(startcost);
		 if(typeof extraStartCost == 'undefined'){
			 extraStartCost = sc;//预存
			 subPrice = new Number( new Number(subPrice) + new Number(sc)).toFixed(2) ;
		 }else{
			 subPrice =  new Number(new Number(subPrice)  - new Number(extraStartCost) + new Number(sc)).toFixed(2) ;
			 extraStartCost = sc;//预存
		 }
		  
	},
    // end of set get methods
    this.toString = function(){
    	return JSON.encode(this.getJson());
	}
	this.readFromJson = function(map){
	  if (typeof map=='undefined')return false;
	  	 uniId = map['uniId'];
		 itemId =  map['itemId'] ;
		 itemName = map['itemName'];
		 itemQuantity = map['itemQuantity'];
		 itemPrice = map['itemPrice'];
		 feeInfo = map['feeInfo'];
		 extraStartCost = map['extraStartCost'];
		 subPrice = map['subPrice'];
		 itemType = map['itemType'];
		 mobile = map['mobile'];
		 is_part_contract = map['is_part_contract'];
		 enternet = map['enternet'];
		 customerInfo = map['customerInfo']||{};
		 contractId = map['contractId'];
		 suitId = map['suitId'];
		 contract_price = map['contract_price'];
		  gjType = map['gjType'];
		 mobileId = map['mobileId'];
		 phoneMissPrice = map['phoneMissPrice'];
		 mobilePrice = map['mobilePrice'];
		 terminalPrice = map['terminalPrice'];
		 remark = map['remark'];
		 suitFee = map['suitFee'];
		 return this;
	}
	this.getJson = function(){
	  var map = {};
		map['uniId'] = uniId;
		map['itemId'] = itemId;
		map['itemName']  = itemName;
		map['itemQuantity'] = itemQuantity;
		map['itemPrice'] = itemPrice;
		map['feeInfo'] = feeInfo;
		map['extraStartCost'] = extraStartCost;
		map['subPrice'] = subPrice;
		map['itemType'] = itemType;
		map['mobile'] = mobile;
		map['is_part_contract'] = is_part_contract;
		map['enternet'] = enternet;
		map['customerInfo'] = customerInfo;
		map['contractId'] = contractId;
		map['suitId'] = suitId;
		map['contract_price'] = contract_price;
		map['gjType'] = gjType;
		map['mobileId'] = mobileId;
		map['phoneMissPrice'] = phoneMissPrice;
		map['mobilePrice']=mobilePrice;
		map['terminalPrice']=terminalPrice;
		map['remark']=remark;
		map['suitFee']=suitFee;
	  return map;
	}
	this.incrementQuantity =function(){
		 itemQuantity++;
	}
	}
	
	
