var buy_horses_amount=0
var buy_items_amount=0
var buy_eqs_amount=0
var buy_tools_amount=0
var seller_id=-1
var buy_eq_started=false;var update_eq_started=false;function sync_buy_other_amount(check,obj,by,buy_amount){buy_amount+=check.checked?by:-by
$(obj).style.color=buy_amount<=player_money?"black":"red";$(obj).innerHTML=buy_amount
return buy_amount}
function sync_buy_horses_amount(check,obj,by){buy_horses_amount=sync_buy_other_amount(check,obj,by,buy_horses_amount)
return buy_horses_amount}
function sync_buy_tools_amount(check,obj,by){buy_tools_amount=sync_buy_other_amount(check,obj,by,buy_tools_amount)
return buy_tools_amount}
function sync_buy_items_amount(check,obj,by){buy_items_amount=sync_buy_other_amount(check,obj,by,buy_items_amount)
return buy_items_amount}
function sync_buy_eqs_amount(check,obj,by){buy_eqs_amount=sync_buy_other_amount(check,obj,by,buy_eqs_amount)
return buy_eqs_amount}
function buy_other_items(form,buy_amount,itemName,controller,action,updatePanel){if(buy_eq_started){alert("Transaction already in progress, please wait...");return;}
if(buy_amount==0){alert("You have not selected any "+itemName+"!");return false;}
if(buy_amount>player_money){alert("You do not have enough coins! (You need "+(buy_amount-player_money)+" more coins)");return false;}
if(!confirm('Are you sure you want to buy the selected '+itemName+'?'))
return;set_result_message("Buying the selected "+itemName+", please wait...");buy_eq_started=true
new Ajax.Updater(updatePanel,'/'+controller+'/'+action+'/'+seller_id+'?partial=true',{asynchronous:true,evalScripts:true,onComplete:function(request){buy_eq_started=false},parameters:Form.serialize(form)});return true;}
function buy_other_horses(form,controller,update){if(!update)
var update='soldhorsesContent';if(buy_other_items(form,buy_horses_amount,'horses',controller,'buy_horses',update))
buy_horses_amount=0;return false;}
function buy_other_eqs(form,controller){if(buy_other_items(form,buy_eqs_amount,'items',controller,'buy_eqs','used_eq_content'))
buy_eqs_amount=0
return false;}
function buy_items(form,controller){if(buy_other_items(form,buy_items_amount,'items',controller,'buy_items','item_content'))
buy_eqs_amount=0
return false;}
function buy_tools(form,controller){if(buy_other_items(form,buy_tools_amount,'tools',controller,'buy_tools','tools_output'))
buy_tools_amount=0
return false;}
function enable_price(check,inp){obj=$(inp)
obj.readOnly=!check.checked
if(!obj.readOnly){obj.select();obj.style.backgroundColor='white';}
else{obj.style.backgroundColor='transparent';}}
function update_items_for_sale_list(form,updatePanel,action){if(update_eq_started){alert("Already updating the list, please wait...");return;}
update_eq_started=true;new Ajax.Updater(updatePanel,'/shop/'+action+'?partial=true',{asynchronous:true,evalScripts:true,onComplete:function(request){update_eq_started=false;},parameters:Form.serialize(form)});}
var items_cnt=new Array();var items_ids=new Array();var total_cost=0;var $total_cost;var buying_limit=999
var buy_standard_eq_started=false
function set_cart(position,value,cost){if(value==items_cnt[position])
return
if(isNaN(value))
value=0
if(value>buying_limit){value=buying_limit
$('item_'+position).value=value}
if(!items_cnt[position])
items_cnt[position]=0
total_cost+=(value-items_cnt[position])*cost
items_cnt[position]=value
items_ids[position]=$('item_'+position)
set_cost(total_cost)}
function change_cart(id,cost,sign,idn){if(items_cnt[idn]>=buying_limit&&sign==1){alert("Come on, you cannot carry that much!")
return false;}
if(items_ids[idn]==null)
items_ids[idn]=$(id);$id=items_ids[idn];if(sign==-1&&(total_cost==0||items_cnt[idn]==null||items_cnt[idn]==0)){return}
total_cost+=cost*sign
set_cost(total_cost)
items_cnt[idn]=items_cnt[idn]==null?sign:items_cnt[idn]+sign
$id.value=items_cnt[idn]}
function set_cost(total_cost){var color=total_cost<=player_money?"black":"red";$total_cost.innerHTML="<font color="+color+">"+total_cost+"</font>";$('total_cost').innerHTML=$total_cost.innerHTML}
function empty_cart(init)
{for(var i=0;i<items_ids.length;i++){items_cnt[i]=0;if(items_ids[i]!=null)
items_ids[i].value=0;}
total_cost=0;set_cost(0)}
function clear_cart(){items_cnt=new Array();items_ids=new Array();total_cost=0;$total_cost=$('total_cost');}
function buy_eq(){if(buy_standard_eq_started){alert("Transaction already in progress, please wait...");return;}
if(total_cost==0){alert("You have not selected any items!");return false;}
if(total_cost>player_money){alert("You do not have enough coins! (You need "+(total_cost-player_money)+" more coins)");return false;}
if(!confirm('Are you sure you want to buy the selected items?'))
return false;ret=""
for(var i=0;i<items_cnt.length;i++){if(items_cnt[i]!=null&&items_cnt[0]!=0)
ret+="eq["+i+"]="+items_cnt[i]+"&"}
set_result_message("Buying the selected items, please wait...");buy_standard_eq_started=true;var t=new Date().getTime();new Ajax.Updater('control_buy','/market/buy_items?'+ret+"when="+t+"&",{asynchronous:true,evalScripts:true,onComplete:function(request){clear_cart();buy_standard_eq_started=false;},parameters:ret,method:'GET'});empty_cart()
return false;}
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){if(si==-1)
return
money+=get_price(src_select[si].value,item_type)*add_money;dest_select.appendChild(src_select[si]);}
else{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(',');}
var started_creating=false;var selected_form_options;var create_form_options;var repair_messages=["You carefully start repairing the tack...","This is going to take a while, but you are enjoying it","While skillfully handling the tools, you think about life and universe...","Wow, you're almost done repairing and you are not tired at all!","There you go. You have repaired the tack!"];var create_messages=["You start working and hope to be happy by the results","Here, in your workshop, you feel truly happy","Working with tools and materials is what you always wanted to do","and now you have your own workshop...","You feel like an artist working anxiously waiting to finish your work","Will this be your masterpiece?","There you go, looking at what you have accomplished, you smile happily."];function add_item(name,shape,id){sel=$("create_selected_items");for(var i=0;i<sel.options.length;i++)
if(sel.options[i].value==id){alert("That item is already added!");return false;}
op=new Option(name+" ["+shape+"%]",id);if(navigator.userAgent.indexOf('Safari')==-1)
sel.options.add(op,1);else{sel.options[sel.options.length]=op;}}
function check_item(which){if(started_creating==true){alert("You are already creating an item!");return false;}
sel=$("create_"+(which==0?"item":"tack"));if(sel.selectedIndex==-1){alert("Please select what you want to manufacture!");return-1;}
started_creating=true;selected_form_options=(which==0?0:"1&sel_ids="+escape(select_items_serialize('create_selected_items')));form=$('item_form');create_form_options=Form.serialize(form);new Ajax.Updater("result",'/workshop/can_create_item?id='+
selected_form_options,{asynchronous:true,evalScripts:true,onComplete:function(request){create_item(request.responseText,which);set_result_message();},parameters:create_form_options});set_result_message("You are getting the needed tools and materials...");}
function clear_items(){sel=$("create_selected_items");len=sel.options.length;for(var i=0;i<len;i++)
sel.remove(0);}
function repair_tack(id,repair){if(started_creating==true){alert("You are already working on something!");return false;}
if(repair==true){started_creating==true
new Ajax.Updater("list_all",'/workshop/repair_tack?id='+id,{asynchronous:true,evalScripts:true,onComplete:function(request){started_creating==false;}});}
else{new Ajax.Updater("result",'/workshop/can_repair_tack?id='+id,{asynchronous:true,evalScripts:true,onComplete:function(request){set_result_message();if(request.responseText==" "){var rep_prg=new fs_ProgressBar(repair_messages,repair_time,function(ind){if(ind==repair_messages.length-1)
repair_tack(id,true);})}}});set_result_message("You start looking for the required tools and materials...");}}
function create_item(item,which){if(item!=" "){started_creating=false;return false;}
var cre_prg=new fs_ProgressBar(create_messages,create_time,function(ind){if(ind==create_messages.length-1)
new Ajax.Updater("",'/workshop/create_item?id='+selected_form_options,{asynchronous:true,evalScripts:true,onComplete:function(request){set_result_message();clear_items();started_creating=false;},parameters:create_form_options});})
return false;}