var fnAddSelectedToCart = function(model_id, left, product_id){
				var qty = Ext.get('qty_model_'+model_id).getValue();
	
	           	if (model_id){
	            	if (left==0){
	            		Ext.Msg.show({
						   title: general_warning,
						   msg: messages_product_soldout,
						   buttons: Ext.Msg.OK,
						   icon: Ext.MessageBox.WARNING
						});
	            	}else if (left<qty){
	            		Ext.Msg.show({
							   title: general_warning,
							   msg: 'Insufficient stock.',
							   buttons: Ext.Msg.OK,
							   icon: Ext.MessageBox.WARNING
							});
	            	}else{
	            		Ext.Updater.defaults.showLoadIndicator = false;
						Ext.get('div_cartsummary').load({
					        url: addcart_url,
					        scripts: true,
					        params: {model_id:  model_id, qty: qty},
					        callback: function(){
					        	Ext.get('modelstable_'+product_id).load({
					                url: models_url,
					                scripts: true,
					                params: {task: 'modelstable', product_id:  product_id, campcategory_id: campcategory_id},
					                callback: function(){
					                	if (details==true){
					                		showCartOnClick();
					                	}
					                }
					        	});	
					        }			                
						});						
	            	}
	            }else{
	            	Ext.Msg.show({
						   title: general_warning,
						   msg: messages_noselection,
						   buttons: Ext.Msg.OK,
						   icon: Ext.MessageBox.QUESTION
						});
	            }
	    	};
	    	
var fnBatchAddToCart = function(product_id){
	
   	if (product_id){
   		
	var frmBatch = new Ext.form.BasicForm();
   		
   	 var textBoxes = Ext.DomQuery.select("input[class~=qtyprod_"+product_id+"]");  
   	 var qString=null;

     
     // An array to keep track of all ExtJs Text Fields  
     //var ExtTextFields = Array();  
   
     // Now loop through each element and make it Ext TextField  
     Ext.each(textBoxes,function(item,index,allItems){
    	 
    	 if (item.value){
	    	 if (qString==null){
	    		 qString = item.name+"="+item.value;
	    	 }else{
	    		 qString += "&"+item.name+"="+item.value;
	    	 }
    	 }
    	 
       	 //alert(item.name);
        // ExtTextFields[index] = new Ext.form.TextField({
        /*var aField = new Ext.form.Hidden({
        		name : item.name ,
        		name : item.value 
              allowBlank : false,  
              name: item.name,
              value: item.value
              applyTo : item  
         });  
        
        //alert(aField.getName()+":"+aField.getValue());
        
        frmBatch.add(aField);*/
     });
     
     /*frmBatch.submit({success: function(form, action){
    	 alert('done');    	 
     }});*/
     
    // alert(qString);
   		
		Ext.Updater.defaults.showLoadIndicator = false;
		
		Ext.get('div_cartsummary').load({
	        url: 'Shop_addtocartcataloguebatch.do',
	        scripts: true,
	        params: qString,
	        callback: function(){
	        	Ext.get('modelstable_'+product_id).load({
	                url: models_url,
	                scripts: true,
	                params: {task: 'modelstable', product_id:  product_id, campcategory_id: campcategory_id},
	        		callback : function(){
	                	activateEnter.createDelegate(this, ["modelstable_"+product_id+" input[class~=boxqty]"]) 
	                	if (details==true){
	                		showCartOnClick();
	                	}
	                }
	        	});	
	        }			                
		});				
    }else{
    	Ext.Msg.show({
			   title: general_warning,
			   msg: messages_noselection,
			   buttons: Ext.Msg.OK,
			   icon: Ext.MessageBox.QUESTION
			});
    }
};
	    	
function reloadModels(prod_id){
	if (prod_id!=null){
		Ext.get('modelstable_'+prod_id).load({
	        url: models_url,
	        scripts: true,
	        params: {task: 'modelstable', product_id:  prod_id, campcategory_id: campcategory_id}    
		});	
	}else{
		Ext.get('modelstable').load({
	        url: models_url,
	        scripts: true,
	        params: {task: 'modelstable', product_id:  product_id, campcategory_id: campcategory_id}    
		});	
	}
}	    

function reloadModelsPerProd(prod_id){
	Ext.get('modelstable_'+prod_id).load({
        url: models_url,
        scripts: true,
        params: {task: 'modelstable', product_id:  prod_id, campcategory_id: campcategory_id}    
	});	
}

function reloadModels(){
	var elmts = Ext.DomQuery.select('.modelsdiv');
	for (var x = 0 ; x < elmts.length; x ++){
		var prod_id = Ext.Element.fly(elmts[x]).getAttributeNS('','rel');
		Ext.get('modelstable_'+prod_id).load({
	        url: models_url,
	        scripts: true,
	        params: {task: 'modelstable', product_id:  prod_id, campcategory_id: campcategory_id}    
		});	
	}
}	    	


function activateEnter(selector){
	var textBoxes = Ext.DomQuery.select(selector);
	Ext.each(textBoxes,function(item,index,allItems){
		Ext.fly(item).addKeyListener(13, function(e, o){
			fnBatchAddToCart(Ext.fly(o.getTarget()).getAttributeNS("ext", "prodid"));
			/*alert(item.id.split("_")[2]);*/		
		});
	});
}

//activateEnter.createDelegate(this, ["input[class~=boxqty]"])

Ext.onReady(activateEnter.createDelegate(this, ["input[class~=boxqty]"]));

/*
Ext.onReady(function(){
	var textBoxes = Ext.DomQuery.select("input[class~=boxqty]");
	Ext.each(textBoxes,function(item,index,allItems){
		Ext.fly(item).addKeyListener(13, function(e, o){
			fnBatchAddToCart(Ext.fly(o.getTarget()).getAttributeNS("ext", "prodid"));	
		});
	});
});
*/
