/**
 * @author cashbit
 */

function OnMxKartShowKart(htmltext){
	Ext.get('mxkartmenuvoicecontents').update(htmltext);
	MxKartSessionData.itemlist.each(function(item,i){ 
		var r = Ext.get('MxKartItem_' + item.pinfo.RECID) ;
		if (item.active == 1){
			var strike = 'none' ;
			var rp = mxkartremoveproduct ;
		} else {
			var strike = 'line-through' ;
			var rp = mxkartreplaceproduct ;
		}
		r.setStyle('text-decoration',strike) ;
		Ext.get('MxKartRemoveProduct_'+item.pinfo.RECID).update(rp) ;
	})
}

function MxKartEmptyKartRender(){
	MxKartSessionData.itemlist.each(function(item,i){ 
		var r = Ext.get('MxKartItem_' + item.pinfo.RECID) ;
		if (r) {
			r.remove();
		}
	})
	try {
		Ext.get('MxKartAmount').update(0) ;
		Ext.get('MxKartAmountVat').update(0) ;		
	} catch (err){
	}
	
}

function MxKartCalcAmount(){
	var t = 0 ;
	var tv = 0 ;
	
	MxKartSessionData.itemlist.each(function(item,i){ 
		if (typeof(item.active) == 'undefined'){
			item.active = 1 ;
		}
		t = t + (item.pinfo.PREZZO * item.qty * item.active) ;
		tv = tv + (item.pinfo.PREZZOIVATO * item.qty * item.active) ;
		tv= Math.round(tv*100)/100;
		t= Math.round(t*100)/100;
		
	})
	
	return {amount: t, amountvat: tv} ;	
}



function MxKartUpdateProduct(ProductRecId){
	var i = FindItem(ProductRecId) ;
	if (i > -1){
		var q = Ext.get('MxKartQty_'+ProductRecId).getValue() ;
		MxKartSessionData.itemlist[i].qty = q * 1;
		var p = MxKartSessionData.itemlist[i].pinfo['PREZZO'] ;
		var pv = MxKartSessionData.itemlist[i].pinfo['PREZZOIVATO'] ;
		var ra = Math.round( p * q *100)/100 ;
		var rav = Math.round( pv * q *100)/100 ;
		if (showpricevat)
			Ext.get('MxKartAmount_' + ProductRecId).update(rav) ;
		else
			Ext.get('MxKartAmount_' + ProductRecId).update(ra) ;
		
		OnMxUpdToKart(MxKartSessionData.itemlist[i]) ;
	} else {
		Ext.Msg.alert('Product not found') ;
	}

	var t = MxKartCalcAmount() ;
	
	Ext.get('MxKartAmount').update(t.amount) ;
	Ext.get('MxKartAmountVat').update(t.amountvat) ;
	
	MxBasketTotal() ;
	
}

function MxKartRemoveProduct(ProductRecId){
	var i = FindItem(ProductRecId) ;
	if (i > -1) {
		if (MxKartSessionData.itemlist[i].active == 0){
			MxKartSessionData.itemlist[i].active = 1;
			var strike = 'none' ;
			var rp = mxkartremoveproduct ;
		} else {
			MxKartSessionData.itemlist[i].active = 0;
			var strike = 'line-through' ;
			var rp = mxkartreplaceproduct ;
		}
		OnMxUpdToKart(MxKartSessionData.itemlist[i]) ;
		MxBasketTotal() ;
	}
	
	var r = Ext.get('MxKartItem_' + ProductRecId) ;
	r.setStyle('text-decoration',strike) ;
	
	var t = MxKartCalcAmount() ;
	
	Ext.get('MxKartAmount').update(t.amount) ;
	Ext.get('MxKartAmountVat').update(t.amountvat) ;
	
	Ext.get('MxKartRemoveProduct_'+ProductRecId).update(rp) ;
	
}
