/*!
 * 浏览器兼容测试.IE6, IE7, FireFox
 *
 *
 * @category:   Simple
 * @package:    js
 * @copyright:  Copyright (c) 2009
 * @version:    1.0
 * @author:     黄浪 (langlang_30@163.com)
 * @modified by:	黄浪 (langlang_30@163.com)
 * @last modified date: 2009-09-08
 * @使用的救命对象: product = {'id':,'name':,'price':''};
 */
var simpleCart = {
    cartName     : "TradeMicCart",
    cart         : [],
    pathDs       : './',
    ns           : ((navigator.appName.indexOf("Netscape") != -1) || window.opera),
    iecompattest : function (){
        return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
    },
    get          : function( id ){ return document.getElementById( id ); },
	json2string  : function( o ) {
        var arr = [];
        var fmt = function( s ) {
            if (typeof s == 'object' && s != null) return simpleCart.json2string(s);
                return /^(string|number)$/.test(typeof s) ? "'" + s + "'" : s;
        }
        for (var i in o){
			if (o.constructor==Array)
			    arr.push(fmt(o[i]));
			else
			    arr.push("'" + i + "':" + fmt(o[i]));
		}
		var str = '{' + arr.join(',') + '}';
		if( o.constructor==Array )str = '[' + arr.join(',') + ']';
        return str;
    },
	/*array2string : function( o ){
		var str = "";
		for( i = 0; i < o.length; i++ ){
			str += ((str == '')?'':',') + this.json2string( o[i] );
		}
			//var last = str.length-1;
		return "["+str+"]";
	},*/
	string2json  : function( str ){
		if( str.toString() == '') return [];
		//alert(str);
	    eval('var obj ='+str+';');
		return obj;
	},
	getCartCookie: function (){
        var cartValue = document.cookie.match(new RegExp("(^| )"+this.cartName+"=([^;]*)(;|$)"));
	    cartValue = (cartValue == null) ? false : unescape(cartValue[2]);
	//	alert(cartValue);
	    var cartArr = this.string2json(cartValue);
  
	    if( typeof(cartArr) != 'object' )  cartArr = [];
	    this.cart = cartArr;
        //this.setCartCookie();
        return this.cart;
    },
    setCartCookie: function(){
        var temp = new Array();
        for( i = 0; i < this.cart.length; i++ )
            if( this.cart[i] != null  ) temp.push(this.cart[i]);
        this.cart = temp;
	    var cartObj  = this.json2string(this.cart);
	    var Days = 1, expire  = new Date();    //new Date("December 31, 9998");
	    expire.setTime(expire.getTime() + Days*24*60*60*1000);
	    document.cookie = this.cartName + "="+escape(cartObj)+ ";expires=" + expire.toGMTString() + ";path=/";
	},
	delCartCookie: function (){
		var expire = new Date();
		expire.setTime(expire.getTime()-1);
		var value=getCookie(this.cartName);
		document.cookie = this.cartName + "=" +value + "; expires ="+expire.toGMTString();
	},
	emptyCart :function(){
	    this.cart = [];
	    this.setCartCookie();
	}
}
simpleCart.productExist = function(proId){
	this.getCartCookie();
	for( i = 0; i < this.cart.length; i++ )
		if( this.cart[i].id == proId )
				    return i;
	return -1;
}
//添加商品至购物车
simpleCart.addProduct = function( obj ){
	this.getCartCookie();
	var product = "";
	product = this.ns?obj.attributes['product'].nodeValue:obj.product;
	var proObj = this.string2json(product);
	var index = this.productExist(proObj.id);
	var qty = 1;
	if(  typeof(proObj.qty) != 'undefined'  ) qty = Number(proObj.qty);
	if( index == -1 )this.cart.unshift({"id":proObj.id,"qty":qty,"pro":proObj,"remark":""});
	else this.cart[index].qty += Number(qty);
	this.setCartCookie();
}
simpleCart.getProduct = function(proId){
	this.getCartCookie();
	var index = this.productExist(Number(proId));
	return this.cart[index];
	
}
//从购物车中删除产品
simpleCart.delProduct = function( proId ){
	this.getCartCookie();
	var index = this.productExist(Number(proId));
	delete this.cart[index];
	this.setCartCookie();
}
//修改购物车中产品数量
simpleCart.changeProductQty = function( proId, num ){
	this.getProduct(proId).qty = Number(num);
	this.setCartCookie();
}
simpleCart.changePrice = function( proId, currPrice ){
	this.getProduct(proId).pro.currPrice = Number(currPrice);
	this.setCartCookie();
}
//修改购物车中产品备注
simpleCart.changeProductRemark = function( proId, remark ){
	this.getProduct(proId).remark = escape(remark);
	this.setCartCookie();
}
simpleCart.getProductRemark = function( proId, remark ){
	return unescape(this.getProduct(proId).remark);
}

simpleCart.getCartLength = function(){
	this.getCartCookie();
    return this.cart.length;
}
