function getTempCartRaw(){
    return _srv_tc_;
}

function loadCartItem(cookieBaseName /*, defaultSku*/ ){
    if (typeof cookieBaseName == "undefined") {
        cookieBaseName = "tc_";
    }
    var php = new PHP_Serializer();
    
    var i = 0;
    var buffer = "";
    var c = "";
//    while(c = getCookieRaw(cookieBaseName + i)){
      c = getTempCartRaw();
        buffer += c;
        i++;
//    }
    if(buffer > ""){
        return php.unserialize(stripslashes(unescape(buffer)));
        //return php.unserialize(buffer);
    }
    return new CartItem();    
}

function CartItem(sku, qty, numSubs, cookieName, pic){
    if (typeof cookieName == "undefined") {
        cookieName = "tc_";
    }
    if (typeof numSubs == "undefined") {
        numSubs = 0;
    }
    
    this.s = sku;
    this.q = qty;
    this.m = numSubs;
    this.i = new Array();
    this.c = cookieName;
    this.n = 0;
    this.p = pic;

    this.equals = function(ci, cmpQty){
        if(typeof cmpQty == "undefined"){
            if(this.getNumSubs() > 0){
                cmpQty = true;
            }else{
                cmpQty = false;
            }
        }
        
        var skuMatch = this.getSkus().toString() == ci.getSkus().toString();
        var qtyMatch = this.getQtys().toString() == ci.getQtys().toString();

        if(skuMatch == true && (qtyMatch == false && cmpQty == false || qtyMatch == true)){
            return true;
        }
        return false;
    }

    this.initIterators = function(){
        this.n = 0;
        for(x in this.i){
            this.i[x].initIterators();
        }
    }

    this.getPic = function(){
        return this.p;
    }
    
    this.setPic = function(pic){
        this.p = pic;
    }

    this.next = function(){
        if(this.getNumSubs() == 0 && this.n >= 0 || this.n >= this.getNumSubs()+1){
            return false;
        }else if(this.getNumSubs()+1 > this.n){
            this.n++;
            return true;
        }//else if(this.getNumSubs() == 0 && this.n == 0){
        //    return false;
        //}
    }

    this.getNextPic = function(){
        if(this.n == 0){
            return this.getPic();
        }else if(this.n < this.getNumSubs()+1){
            return this.i[this.n-1].getPic();
        }else{
            return false;
        }
    }

    this.getNextSku = function(){
        if(this.n == 0){
            return this.getSku();
        }else if(this.n < this.getNumSubs()+1){
            return this.i[this.n-1].getSku();
        }else{
            return false;
        }
    }

    this.getNextQty = function(){
        if(this.n == 0){
            return Number(this.getQty());
        }else if(this.n < this.getNumSubs()+1){
            return Number(this.i[this.n-1].getQty());
        }else{
            return false;
        }
    }

    this.getSkus = function(){
        var ret = new Array();
        ret.push(this.getSku());
        for(var x = 0; x < this.i.length; x++){
            ret.push(this.i[x].getSku());
        }
        return ret;
    }

    this.getQtys = function(){
        var ret = new Array();
        ret.push(this.getQty());
        for(var x = 0; x < this.i.length; x++){
            ret.push(this.i[x].getQty());
        }
        return ret;
    }

    this.getCookieName = function(){
        return this.c;
    }

    this.setCookieName = function(cookieName){
        this.c = cookieName;
    }

    this.getSku = function(){
        return this.s;
    }

    this.setSku = function(sku){
        this.s = sku;
    }

    this.getQty = function(){
        return Number(this.q);
    }

    this.setQty = function(qty){
        this.q = qty;
    }

    this.getMaxSubs = function(){
        return Number(this.m);
    }

    this.setMaxSubs = function(num){
        this.m = num;
    }

    this.getNumSubs = function(){
        return this.i.length;
    }

    this.getNumItems = function(){
        var ret = 0;
        for(x =0; x < this.i.length; x++){
            ret += this.i[x].getQty();
        }
        return Number(ret);
    }

    this.addItem = function(sub){
        var x = 0;
        for(x =0; x < this.i.length; x++){
            if(this.i[x].getSku() == sub.getSku()){
                this.i[x].setQty(this.i[x].getQty() + sub.getQty());
                break;   
            }
        }
        if(x == this.i.length){
            this.i.push(sub);
        }
        this.save();
    }

    this.delItem = function(sub){
        var x = 0;
        for(x in this.i){
            if(this.i[x].getSku() == sub.getSku()){
                break;
            }
        }
        if(this.i[x].getQty() == 1){
            this.i.splice(x,1);
        }else{
            this.i[x].setQty(this.i[x].getQty()-1);
        }
        this.save();
    }

    this.clearSubs = function(){
        this.i = new Array();
        this.n = 0; //maybe?
    }

    this.reset = function(){
        this.s = "";
        this.q = 0;
        this.m = 0;
        this.i = new array();
        this.n = 0;
    }

    this.serialize = function(){
        //var php = new PHP_Serializer();        
        //return php.serialize(this);
        return "";
    }

    this.upgradeToNextBigestContainer = function(){/*[jax, not ajax]*/}

    this.save = function(){
    }
}


