
function    jsCart_item(code, value, count, rtype, rval) {
    if(null==count) {
        // pregunto por cantidad (nuevo item/modifica cantidad)
        count = (this.items[ code ] ?this.items[ code ][0] : 1);
        var t = (this.no_prompt?1:self.prompt('Cantidad', count));
        if(t) count=t;
        
    }

    if(top.DEBUG) top.DEBUG(''+this.name+' [item '+code+','+Number(value)+','+Number(count)+']');

    if( (0+count) <=0) {
        // si la cantidad era menor a cero o no se ingres� cantidad, borro el item
        this.items[ code ]=[0,0];
        //// var b=''; for(var i in this.items) b += '('+i+': '+this.items[i]+') '; alert(b);
        delete this.items[ code ];
    }else{
        if( !isNaN(Number(count)) ) {
            // si est� todo bien, agrego el item.
            this.items[ code ] = [ Number(count), Number(value)];
        }
    }

    
    if(top.DEBUG) {
        var b=''; for(var i in this.items) b += '('+i+': '+this.items[i]+') ';
        top.DEBUG(''+this.name+' ['+b+']');
    }

    // llamo al redibujado ( 1: link 2: form 3: win )
    if(rtype==1) {
        this.redraw(true);
        return this.serverCall(rval);
    }else if(rtype==2) {
        this.redraw(true);
        return this.serverCall(null,rval);
    }else if(rtype==3) {
        this.redraw(true);
        return this.serverCall(null,null,rval);
    }else{
        this.redraw();
    }
    return false;
}

function    jsCart_clearAll() {
    this.items = [];
    this.redraw();
}

function    jsCart_redraw(no_reload) {
    // recalculo los valores y busco los tags donde escribir los nuevos datos.
    this.subtotal=0, counter=0, itemcount=0;
    for(var i in this.items) {
        this.items[i][0] = Number(this.items[i][0]);
        this.subtotal += this.items[i][0]*this.items[i][1];
        counter++;
        itemcount += this.items[i][0];
    }
    
	var sub1 = 0;
	var sub2 = 0;
    this.total_1  = this.subtotal*(1-this.discount/100);
	
    sub1 = this.subtotal-this.total_1;
	
    this.total    = this.subtotal*(1-this.discount_c/100);
	
	sub2 = this.subtotal-this.total;
	
	this.total= this.subtotal-sub1-sub2;
	
    var elt=null;
	
	if(this.resumenwin && this.resumenwin.document && this.resumenwin.document.getElementById) {
        if( (elt=this.resumenwin.document.getElementById('jscartSubTotal')) )
            elt.innerHTML = this.format(this.subtotal);
        if( (elt=this.resumenwin.document.getElementById('jscartDiscount')) )
            elt.innerHTML = this.format(this.discount);
        if( (elt=this.resumenwin.document.getElementById('jscartTotal')) )
            elt.innerHTML = this.format(this.total);
        if( (elt=this.resumenwin.document.getElementById('jscartCount')) )
            elt.innerHTML = counter;
        if( (elt=this.resumenwin.document.getElementById('jscartItemCount')) )
            elt.innerHTML = itemcount;
        if( (elt=this.resumenwin.document.getElementById('jscartTotalSubTotalDiff')) )
            elt.innerHTML = this.format(sub1);
         if( (elt=this.resumenwin.document.getElementById('jscartTotalSubTotalDiffCli')) )
            elt.innerHTML = this.format(sub2);
    }
    // Si est� el flag seteado, hago todo via server.
    if(!no_reload && this.server_side) {
        var url = this.setURL( this.server_side, 'jsCart_'+escape(this.name)+'=');
        top.tmpimage1=new Image();
        top.tmpimage1.onerror   = function() { if(top.DEBUG) top.DEBUG('Error'); };
        top.tmpimage1.onabort   = function() { if(top.DEBUG) top.DEBUG('Abort'); };
        top.tmpimage1.onload    = function() { if(top.DEBUG) top.DEBUG('Ok'); top.tmpimage1=null; };
        top.tmpimage1.src=url;
        if(top.DEBUG) top.DEBUG(''+this.name+' 0url='+url);
    }
}

function    jsCart_setURL(url, key, items) {
    if(!items) { 
        items='';
        for(var i in this.items)
            items += escape(i+'`'+this.items[i][0])+' ';
    }
    url = url.split(/[&?]/);
    var new_url=[];
    for(var i in url)
        if(url[i].substr(0,key.length)!=key) new_url[new_url.length]=url[i];
    new_url[new_url.length] = key + escape(items);
    url='';
    for(var i in new_url) url += new_url[i]+((i<1)?'?':'&');
    return url.substr(0,url.length-1);
}

function    jsCart_serverCall(link, form, win) {
    // Si est� el flag seteado, hago todo via server.
    var items = '';
    for(var i in this.items)
        items += escape(i+'`'+this.items[i][0])+' ';
    var key = 'jsCart_'+escape(this.name)+'=';
    if(form && form.elements && form.elements[key.susbstr(0,key.length-1)]) {
        form.elements[key.susbstr(0,key.length-1)].value = items;
        if(form.elements['jsCart_pre_post'])
            form.elements['jsCart_pre_post'].value=1;
        this.redraw(true);
        form.submit();
    }else if(link) {
        link.href = this.setURL(link.href,key,items);
        if(top.DEBUG) top.DEBUG(''+this.name+' 1url='+link.href);
        this.redraw(true);
    }else if(win) {
        this.redraw(true);
        win.location = this.setURL(win.location.href,key,items);
        if(top.DEBUG) top.DEBUG(''+this.name+' 2url='+win.location);
    }else{
        if(top.DEBUG) top.DEBUG(''+this.name+' 3...');
        this.redraw();
    }
    return true;
}

function    jsCart_format(n) {
    n = Math.round(n*100);
    if(0==n) return '0.00';
    return ((n<100)?'0':'')+String(n).replace(/([0-9]{2})$/,'.$1');
}

function    jsCart(name, resumewin, resultswin, discount, discount_c, no_prompt, server_side,items) {
    this.name       = name;
    this.items      = [];
    this.discount   = (discount?discount:0);
    this.discount_c = (discount_c?discount_c:0);
    this.no_prompt    = (no_prompt?true:false);
    this.subtotal   = 0;
    this.total      = 0;
    this.server_side = server_side;
	this.total_1 = 0;
    this.resumenwin = resumewin;
    this.resultswin = resultswin;

    this.format     = jsCart_format;
    this.item       = jsCart_item;
    this.redraw     = jsCart_redraw;
    this.serverCall = jsCart_serverCall;
    this.setURL     = jsCart_setURL;
    this.clearAll   = jsCart_clearAll;

    if(items) {
        this.items = items;
        this.redraw(true);
    }
    return this;
}

function    AssignOptionsToSelect(elt,options,value) {
    if(!elt) return;
    elt.options.length = 1;
    elt.disabled=true;
    var idx=0;
    for(var i in options) {
        elt.options[elt.options.length] = new Option(options[i],i );
        elt.disabled=false;
        idx++;
        if(i==value) { elt.selectedIndex = idx; }
    }

}

