var sell_started = false;

function send_items(itemName, update_panel, select_name){
if($('send_to').value==""){
alert('You must type the name of the destination player');
return false;
}
if(sell_started){
	alert("Registering in progress, please wait...");
	return;
}	
if($(select_name).options.length == 0){
alert("You haven't selected anything!\n(use the arrows to move the items)");
return
}

if(!confirm('Are you sure you want to send the selected ' + itemName + '?'))	
	return false ;	
sell_started = true;		
new Ajax.Updater(update_panel, '/message/send_item?rend=' + update_panel + '&send_to=' + escape($('send_to').value) + '&'  + select_name + '=' + escape(select_items_serialize(select_name)), {asynchronous:true, evalScripts:true,onComplete:function(request){sell_started = false;}});return false 
}



function sell_items(itemName, update_panel, select_name){
if(sell_started){
	alert("Sale in progress, please wait...");
	return;
}	
if($(select_name).options.length == 0){
alert("You haven't selected anything for sale\n(use the arrows to move the items)");
return
}

if(!confirm('Are you sure you want to sell the selected ' + itemName + '?'))	
	return false ;	
sell_started = true;		
new Ajax.Updater(update_panel, '/market/sell?rend=' + update_panel + '&' + select_name + '=' + escape(select_items_serialize(select_name)), {asynchronous:true, evalScripts:true,onComplete:function(request){sell_started = false;}});return false 
}

function show_item_info(item,item_type){
	var item_price = eval("item_type+'_price'");
	$(item_price).innerHTML = get_price(item.value,item_type)
	return
	new Ajax.Updater('item_info', 
				'/item/get_info/'+item.value +'?partial=true', 
				{asynchronous:true, evalScripts:true});
}

function get_price(id,item_type){
var ids = eval(item_type +'_ids');
var prices = eval(item_type + '_prices')
for(var i = 0;i<ids.length;i++){
	if(id == ids[i])
		return prices[i]
		}
return 0		
}

function move_select_items(source,dest,how_many,add_money,item_type){
	var src_select = $(source)
	var dest_select = $(dest)
	var si = src_select.selectedIndex;
	var sell_money=	eval("'sell_' + item_type + '_money'")	
	var money = parseInt($(sell_money).innerHTML)
	if(how_many == 1){//One item
	  if(si == -1)
			return
		  money+=get_price(src_select[si].value,item_type)*add_money;	
	  	  dest_select.appendChild(src_select[si]);
	}
	else {//All items
		while(src_select.length>0){
			money+=get_price(src_select[0].value,item_type)*add_money;
			dest_select.appendChild(src_select[0]);
		}
	}

	$(sell_money).innerHTML = money		
}
function select_items_serialize(sel){
so = $(sel);
vec = new Array()

for(var i=0;i<so.length;i++)
vec.push(so[i].value);
return vec.join(',');
}
