var Vers=1.4;
var Items={};
var Army={};
var ItemsBox={};
var ItemsBoxLog={};
var path='http://www.fantasyland.ru/images/items/';
var pathA='http://www.fantasyland.ru/images/armies/';
var pathE='http://www.fantasyland.ru/images/effects/';
var pathM='http://www.fantasyland.ru/images/medals/';

/*path='../img/';*/
var Types={'1':'Амулеты','2':'Броня','3':'Оружие','4':'Щиты','5':'Кольца','6':'Перчатки','8':'Обувь','9':'Пояса','10':'Браслеты','11':'Двуручное оружие','13':'Боевые свитки','22':'Все Ресурсы','23':'\u0428\u043b\u0435\u043c','24':'Ключи','25':'Зелья','26':'Свитки','27':'Квестовые','55':'Раритет'};
var Guilds={"12":"Ювелир","15":"Портной","11":"Кузнец","28":"Алхимик","6":"Мудрец","4":"Шахтеры","8":"Охотники","18":"Собиратели","70":"Купцы","100":"Фальшивомонетчик","0":"Без гильдии","101":"Марго из Илдиора","7":"Бойцы"};
var Rangs = {"0":"Новичок","1":"Ученик","2":"Адепт","3":"Мастер","4":"Грандмастер","5":"Элита"};
var speed=0;
var p=0;
var iLG='http://www.fantasyland.ru/images/';
var money='<img src="'+iLG+'miscellaneous/money.gif" align="top" alt="монет" />';
var unmoney='<img src="'+iLG+'miscellaneous/quest.gif" align="top" alt="ум" />';
function myEval(code){return eval(code);}
function DoRequest(url,request,callback,key){
	$('#status').html('Получение данных...<img src=\"../../images/load.gif\" align=\"absbottom\">');
	$.ajax({
		type: "GET",
		url: url,
		data: request,
		dataType: "json",
		processData: true,
		cache: true,
		success: function(q){
			$('#status').html('Обработка данных...<img src=\"../../images/load.gif\" align=\"absbottom\">');
			callback(q,key);
			}
	});
}
function createDiv(parent,text,id,classname,visible,style){
	if ((visible)&&(text)){
		var tree = document.createElement('div');
		tree.setAttribute('id', '_'+id);
		var div = document.createElement('div');
		div.setAttribute('id', id);
		if (ge('tree').checked==true) visible='block';
		else visible='none';
		div.style.display=visible;
		if (style) for(var key in style) div.style[key]=style[key]+'px';
		//if ((classname)&&($.browser.msie)) div.setAttribute('className', classname);
		if (classname) div.setAttribute('class', classname);
		if (parent) parent.appendChild(tree);
		tree.innerHTML=tree.innerHTML+text;	
		if (visible=='none') createIMG(tree,'../img/e_plus.gif','Открыть',{margin:'0px 5px 0px 5px',height:11,width:11,align:"absmiddle"},"if (ge('"+id+"').style.display == 'none') {$('#"+id+"').show(speed);this.src='../img/e_minus.gif';} else {$('#"+id+"').hide(speed);this.src='../img/e_plus.gif'}")
		else createIMG(tree,'../img/e_minus.gif','Открыть',{margin:'0px 5px 0px 5px',height:11,width:11,align:"absmiddle"},"if (ge('"+id+"').style.display == 'block') {$('#"+id+"').hide(speed);this.src='../img/e_plus.gif';} else {$('#"+id+"').show(speed);this.src='../img/e_minus.gif'}")
		tree.appendChild(div);
		return div;
	}
	else{
		var div = document.createElement('div');
		if (id) div.setAttribute('id', id);
		//if ((classname)&&($.browser.msie)) div.setAttribute('className', classname);
		if (classname) div.setAttribute('class', classname);
		if (text) div.innerHTML=text;
		if (style) for(var key in style) div.style[key]=style[key]+'px';
		if (parent) parent.appendChild(div);
		return div;
	}
}
function createP(parent,text){
	var p = document.createElement('p');
	if (text) p.innerHTML=text;
	if (parent) parent.appendChild(p);
	return p;
}
function createIMG(parent,image,text,style,onclick){
	if (onclick){
		var img = '<input type="image" src="'+image+'" onclick="'+onclick+'"';
		if (text) img+=' value="'+text+'"';
		if (style){
		if (style.align) img+=' align="'+style.align+'"';
		}
		parent.innerHTML=parent.innerHTML+img+' />';
	}
	else{
		var img = document.createElement('img');
		if (text) {img.setAttribute('title',text);img.setAttribute('alt',text);}
		img.src=image;
		if (style){
			if (style.height) img.style.height=style.height+'%';
			if (style.width) img.style.width=style.width+'%';
			if (style.margin) img.style.margin=style.margin;
			if (style.align) img.align=style.align;
		}
		if (onclick){
			img.style.cursor='pointer';
			img.onclick = function() { myEval(onclick); };
		}
		parent.appendChild(img);
	}
	return img;
}
function ClearBox(){
	if(p==1) $('#list').html('');
	$('#box').html('');
	$('#box_log').html('');
	$('#Sum span').text('0'); 
	$('#OurSum span').text('0');
	$('#OstSum span').text('0');
	$('#Ost').html('');
	$('#all').html('');
	ItemsBox={};
	ItemsBoxLog={};
	p=0;
}
function OpenBox(){
	Items={};
	Armys={};
	$('#pages').hide(speed);
	$('#list').show();
	p=1;
	createDOMItems(ItemsBox);
}
function AddItemToBoxLog(ID){
 if (ItemsBoxLog[ID]){
	 ItemsBoxLog[ID].Count=ItemsBoxLog[ID].Count*1+ItemsBox[ID].Count*1;
	 $('#box_log_count'+ID).text(ItemsBoxLog[ID].Count);
 }
 else {
	 ItemsBoxLog[ID]={};
	 $.extend(ItemsBoxLog[ID],ItemsBox[ID]);
	 var box=createDiv(null,null,'box_log'+ID,'left');
	 var img =createDiv(box,null,null,'b32n');
	 createIMG(img,path+ItemsBoxLog[ID].Image,ItemsBoxLog[ID].Name,{height:70,width:70,align:'absbottom'});
	 createDiv(box,ItemsBoxLog[ID].Count,'box_log_count'+ID,'bcountlog');
	 $('#box_log').append(box);
 }
}
function AddItemToBox(ID){
	if (ItemsBox[ID]){
		ItemsBox[ID].Count=ItemsBox[ID].Count*1+1;
		$('#box_count'+ID).text(ItemsBox[ID].Count);
	}
	else {
		ItemsBox[ID]={};
		if (Items[ID]) $.extend(ItemsBox[ID],Items[ID]);
		if (Army[ID]) $.extend(ItemsBox[ID],Army[ID]);
		if (!ItemsBox[ID].OurCost) ItemsBox[ID].OurCost=ItemsBox[ID].Cost;
		var box=createDiv(null,null,'box'+ID,'b48');
		var b_l=createDiv(box,null,null,'b32');
		if (Army[ID]) createIMG(b_l,pathA+ItemsBox[ID].Image,ItemsBox[ID].Name,{height:100,width:100});
		if (Items[ID]) createIMG(b_l,path+ItemsBox[ID].Image,ItemsBox[ID].Name,{height:70,width:70});
		var b_r=createDiv(box,null,'box'+'edit'+ID,'b16');
		createIMG(b_r,'../img/b_edit.png','Изменить цену',null,'createOurCost('+ID+')');
		createIMG(b_r,'../img/b_drop.png','Удалить из корзины',null,'deleteIDItems('+ID+')');
		var count =createDiv(box,ItemsBox[ID].Count,'box_count'+ID,'bcount');
		count.onclick=function(){createCount(ID);}
		count.style.cursor='pointer';
		$('#box').append(box);
	}
	ItogoCost();
}
function CreateDOMPage(count,q){
	var i=1;
	var s='';
	while((count-i)>=12){
		s+='<div id="'+(i*1-1)+'" class="page">'+(i)+'-'+(i*1+11)+'</div>';
		i=i+12;
	}
	s+='<div id="'+(i*1-1)+'" class="page">'+(i)+'-'+count+'</div>';
	s+='<div class="clear"></div>';
	$('#pages').html(s);
	$('#pages').show(speed);
	$('#pages #'+q.L1).attr('class','page active');
	s='';for(var key in q) if (key!='L1') s+=key+'='+q[key]+'&';
	$('#pages>div').click(function(){
		DoRequest('fitems.php',s+Q_all()+'&L1='+this.id,cItems);
	});
}
function cItems(ItemTMP){
	if (ItemTMP.Count>12){
		CreateDOMPage(ItemTMP.Count,ItemTMP.Q);
	}
	else $('#pages').html('');
	createItems(ItemTMP.Items);
}
function ShowOne(ItemTMP,a){
	for (var ID in ItemTMP.Items) createDOMItem(ItemTMP.Items[ID],ID,a);
}
function createItems(Obj){
	Items={};
	$.extend(Items,Obj);
	p=0;
	$('#list').show();
	createDOMItems(Obj);
}
function createDOMItems(Obj){
	$('#list').html('');
	for (var ID in Obj){
		if (Obj[ID].Damage) {Army={};$.extend(Army,Obj);createDOMArmy(Obj[ID],ID);}
		else createDOMItem(Obj[ID],ID);
	}
	$('#status').html('Список загружен');
}
function createDOMItem(Obj,ID,a){
	if (!a){
		$('#'+ID).hide(speed, function () {
			$(this).remove();
		});
		if (Obj.Type>100) var d_id=createDiv(null,null,ID,'items 34');
		else var d_id=createDiv(null,null,ID,'items');
	}
	else var d_id=createDiv(null,null,ID,'one');
	var d_title=createDiv(d_id,null,'t'+ID,'title');
	createDiv(d_title,null,'check','right');
	if (Items[ID]) if (Items[ID].PayCount>1) AddItemToBox(ID);
	if (!a){
		if (ItemsBox[ID]){
			if (ItemsBox[ID].Count==ItemsBox[ID].PayCount) createIMG(d_title,'../img/tick_16.png','',{align:"right"},'');
			createIMG(d_title,'../img/b_drop.png','Удалить из корзины',{align:"right"},'deleteIDItems('+ID+')');
		}
		else{
			createIMG(d_title,'../img/buy_16.png','Добавить в корзину',{align:"right"},'AddItemToBox('+ID+')');
		}
	}
	var img=createDiv(d_title,null,'img'+ID,'b50');
	var img1=createDiv(img,null,null,'center50');
	if (Obj.Type>100) img1.style.backgroundImage="url('"+pathM+Obj.Image+"')";
	else img1.style.backgroundImage="url('"+path+Obj.Image+"')";
	s='';
	if (Obj.Level!=0) s+='L-<span>'+Obj.Level+'</span>';
	s+='<span style="color:#663300">'+Obj.Name+'</span>';
	if (Obj.Type<100) s+='<a href="http://www.fantasyland.ru/cgi/item_desc.php?id='+Obj.LG+'" target=_blank ><img src="../img/info_R.gif" height="11px" width="11px" title="инф" hspace="5" /></a>';
	if (Obj.Type<100) s+='<a href="http://electa-lg.com/servise/enc/stat.php?ID='+Obj.LG+'" target=_blank ><img src="../img/statistics.png" height="11px" width="11px" title="статистика цен" hspace="5" /></a>';
	
	createP(d_title,s);
	if (Obj.Cost!=0){
		if (Obj.UN*1==0) var cost = createP(d_title,'Цена: <span>'+Obj.Cost+'</span>'+money);
		else var cost = createP(d_title,'Цена: <span>'+Obj.Cost+'</span>'+unmoney);
		if (!Obj.OurCost) Obj.OurCost=Obj.Cost;
		if (ItemsBox[ID]){
			cost.innerHTML+='(<span>'+Obj.OurCost+'</span>'+money;
			createIMG(cost,'../img/b_edit.png','Изменить цену',null,'createOurCost('+ID+')');
			cost.innerHTML+=')';
		}
		else createP(d_title,'');
	}
	if (!Obj.PayCount) Obj.PayCount=0;
	if (ItemsBox[ID]){
		var count = createP(d_title,'Кол-во: <span>'+Obj.Count+'</span>');
		createIMG(count,'../img/b_edit.png','Изменить кол-во',null,'createCount('+ID+')');
		count.innerHTML += '(Купл.: <span>'+Obj.PayCount+'</span>';
		createIMG(count,'../img/b_edit.png','Изменить кол-во',null,'createPayCount('+ID+')');
		count.innerHTML += ')';
	}
	
	if (Obj.Dobs){
		for (key in Obj.Dobs) createP(d_id,'<span>Добывается: </span>'+Guilds[key]+' - '+Rangs[Obj.Dobs[key]]);
		//d=createDiv(d_id,'<span>Добывается: </span>'+Guilds[key]+' - '+Rangs[Obj.Dobs[key]]);
	}
	if (Obj.Type!=13){
		if (Obj.Stats){
			var d=createDiv(d_id,'<span>Статы:&nbsp;</span>','Stats'+ID,'left');
			var s='';
			for (key in Obj.Stats){
				if (StatsImage[key]!='') s='<img src="http://www.fantasyland.ru/images/miscellaneous/'+StatsImage[key]+'" align="left" height="18px" width="18px" alt="'+Stats[key]+'" title="'+Stats[key]+'"  />&nbsp;&nbsp;';
				else s=Stats[key]+':';
				if (Obj.Stats[key]>0) s+='<span>+'+Obj.Stats[key]+'</span>'; 
				else s+='<span>'+Obj.Stats[key]+'</span>'; 
				createDiv(d,s,null,'clear');
			}
		}
	}
	else{
		var s='';
		for (key in Obj.Stats){
			for (key2 in Obj.Stats[key]){
				if (key2=='Value'){
					if(StatsImage[key].indexOf('.gif') + 1) {
						s='<img src="../effect/'+StatsImage[key]+'" align="absmiddle" height="20px" width="20px" alt="" title=""  />&nbsp;'+Effects[key]+':&nbsp;';
					}
					else{
						if (Math.abs(Obj.Stats[key]['Value'])<10){
							if (StatsImage[key]!='') s='<img src="../effect/'+StatsImage[key]+'_0'+Math.abs(Obj.Stats[key]['Value'])+'.gif" align="absmiddle" height="20px" width="20px" alt="" title=""  />&nbsp'+Effects[key]+':&nbsp;';
							else s=Effects[key]+':';
						}
						else {
							if (StatsImage[key]!='') s='<img src="../effect/'+StatsImage[key]+'_'+Math.abs(Obj.Stats[key]['Value'])+'.gif" align="absmiddle" height="20px" width="20px" alt="" title=""  />&nbsp;'+Effects[key]+':&nbsp;';
							else s=Effects[key]+':';
						}
					}
					if (Obj.Stats[key]['Value']>0) s+='<span>+'+Obj.Stats[key]['Value']+'</span>'; 
					else if (Obj.Stats[key]['Value']<0) s+='<span>'+Obj.Stats[key]['Value']+'</span>';
				}
				else{
					s+='<p>Подробнее:</p>'
					for (var key3 in Obj.Stats[key]['Effect']){
						switch (Obj.Stats[key]['Effect'][key3]*1){
							case 0: s+='<p style="padding-left:20px;"><img src="http://www.fantasyland.ru/images/miscellaneous/'+StatsImage[key3]+'" align="absmiddle" height="20px" width="20px" alt="" />&nbsp;<span>+'+Obj.Stats[key]['Value']+'</span>&nbsp;'+Stats[key3]+'</p>';break;
							case 1: s+='<p style="padding-left:20px;"><img src="http://www.fantasyland.ru/images/miscellaneous/'+StatsImage[key3]+'" align="absmiddle" height="20px" width="20px" alt="" />&nbsp;<span>'+Obj.Stats[key]['Value']+'</span>&nbsp;'+Stats[key3]+' сопернику</p>';break;
							case 2: s+='<p style="padding-left:20px;"><img src="http://www.fantasyland.ru/images/miscellaneous/'+StatsImage[key3]+'" align="absmiddle" height="20px" width="20px" alt="" />&nbsp;<span>+'+Obj.Stats[key]['Value']+'</span>&nbsp;'+Stats[key3]+' всем в группе</p>';break;
							case 3: s+='<p style="padding-left:20px;"><img src="http://www.fantasyland.ru/images/miscellaneous/'+StatsImage[key3]+'" align="absmiddle" height="20px" width="20px" alt="" />&nbsp;<span>'+Obj.Stats[key]['Value']+'</span>&nbsp;'+Stats[key3]+' у всех соперников</p>';break;
							default:break;
						}
					}
				}
			}
			createDiv(d_id,s,null,'left');
		}
	}
	if (Obj.Reqs){
		d=createDiv(d_id,'<span>Требования:&nbsp;</span>','Reqs'+ID,'right');
		for (key in Obj.Reqs) if (key==119) createDiv(d,'<span>'+Sex[Obj.Reqs[key]]+'</span>',null,'clear'); else if (StatsImage[key]!='') createDiv(d,'<img src="http://www.fantasyland.ru/images/miscellaneous/'+StatsImage[key]+'" align="left" height="18px" width="18px" alt="'+Reqs[key]+'" title="'+Reqs[key]+'" />&nbsp;&nbsp;<span>+'+Obj.Reqs[key]+'</span>',null,'clear'); else createDiv(d,Reqs[key]+': <span>+'+Obj.Reqs[key]+'</span>',null,'clear');
	}
	createDiv(d_id,null,null,'clear');
	if (Obj.Quests){
		d=createDiv(d_id,'<span>Квесты:&nbsp;</span>','Quests'+ID,null,1);
		for (key in Obj.Quests) createP(d,key);
	}
	if ((ge('Shops').checked)&&(Obj.Shops)){
		d=createDiv(d_id,'<span>Магазины:</span>','Shops'+ID);
		for (key in Obj.Shops) createP(d,key+': '+Obj.Shops[key]);
	}
	if ((ge('Mobs').checked)&&(Obj.Labs)){
		d=createDiv(d_id,'<span>Лабиринты</span>','Labs'+ID,null,1);
		for (key in Obj.Labs) createP(d,Obj.Labs[key]);
	}
	if ((ge('Mobs').checked)&&(Obj.Mobs)){
		d=createDiv(d_id,'<span>Мобы</span>','Mobs'+ID,null,1);
		for (key in Obj.Mobs) createP(d,'<span>L'+Obj.Mobs[key].Level+'</span> '+Obj.Mobs[key].Name+'<a href="http://www.fantasyland.ru/cgi/mob_info.php?type='+key+'" target=_blank><img src="img/info_R.gif" height=11 width=11 alt="инфо" /></a>: <span>'+Obj.Mobs[key].Value+' ('+Obj.Mobs[key].Procent+')<\/span>');
	}
	if ((ge('Uses').checked)&&(Obj.Uses)){
		d=createDiv(d_id,'<span>Используется в рецептах</span>','Uses'+ID,'uses',1);
		for (key in Obj.Uses){
		var pp=createP(d,'<p><input type="image" src="img/info_R.gif" value="показать" onclick="DoRequest(\'fitems.php\',\'show&ID1='+key+'\'+Q_all(),ShowOne,'+key+');$(\'#u_'+key+' p\').remove();" />  <span>L-'+Obj.Uses[key].Level+'</span> '+Obj.Uses[key].Name+' (исп. <span>'+Obj.Uses[key].Count+'</span> шт.)</p>');
			pp.setAttribute('id', 'u_'+key);
			}
	}
	if ((ge('Sales').checked)&&(Obj.Sales)){
		d=createDiv(d_id,'<span>Продажи ('+Obj.Sales.info+')</span>','Sales'+ID,'sales',1);
		for (key in Obj.Sales)
			if(key!='info') createP(d,'[<span>'+Obj.Sales[key].Count+'</span>] по <span>'+Obj.Sales[key].Price+'</span> в '+Obj.Sales[key].Name+'<a href="http://www.fantasyland.ru/cgi/v_trade_load_shop.php?id='+Obj.Sales[key].ID+'" target=_blank><img src="img/info_R.gif" height=11 width=11 alt="инфо" /></a>');
		
	}
	if ((ge('Sales').checked)&&(Obj.Pays)){
		d=createDiv(d_id,'<span>Скупка ('+Obj.Pays.info+')</span>','Pays'+ID,'pays',1);
		for (key in Obj.Pays)
			if(key!='info') createP(d,' За <span>'+Obj.Pays[key].Price+'</span> в '+Obj.Pays[key].Name+'<a href="http://www.fantasyland.ru/cgi/v_trade_show_goods_for_sale.php?id='+Obj.Pays[key].ID+'" target=_blank><img src="img/info_R.gif" height=11 width=11 alt="инфо" /></a>');
		
	}
	if ((a)||((ge('Recepts').checked)&&(Obj.Recepts))){
		var recept=createDiv(d_id,'<span>Рецепты</span>','Recepts'+ID,null,1);
		var count=1;
		if (Obj.Recepts){
			for (var id_recept in Obj.Recepts){
				if (!a)	var use=' <span style="color:red;font-weight:bold;cursor:pointer;" onclick="urlUseRecept('+ID+','+id_recept+');">Разложить по рецепту</span>';
				else var use='';
				var s='<span>'+Guilds[Obj.Recepts[id_recept].Guild]+'</span>';
				s+=' (';
				if (Obj.Recepts[id_recept].Rang) s+=Rangs[Obj.Recepts[id_recept].Rang];
				if (Obj.Recepts[id_recept].Level) s+=' L-<span>'+Obj.Recepts[id_recept].Level+'</span>';
				if (Obj.Recepts[id_recept].Reqs) for (key in Obj.Recepts[id_recept].Reqs) s+=', '+Reqs[key]+': <span>'+Obj.Recepts[id_recept].Reqs[key]+'</span>';
				s+=' )';
				s+='. Сделает: <span>'+Obj.Recepts[id_recept].Count+' шт.</span>';
				if (count>1) d=createDiv(recept,'Рецепт №'+(count++)+use+'<p>'+s+'</p>',ID+'-'+id_recept,'recepts');
				else d=createDiv(recept,'Рецепт №'+(count++)+use+'<p>'+s+'</p>',ID+'-'+id_recept);
				if(Obj.Recepts[id_recept].Items){
					var i=createDiv(d,'<span>Ингридиенты</span>');
					for (id_Ritems in Obj.Recepts[id_recept].Items){
						var im=createDiv(i,null,null,'left');
						var img=createDiv(im,null,null,'b40');
						createIMG(img,path+Obj.Recepts[id_recept].Items[id_Ritems].Image,Obj.Recepts[id_recept].Items[id_Ritems].Name,{height:70,width:70,align:'absbottom'});
						createP(im,Obj.Recepts[id_recept].Items[id_Ritems].Count+' шт.');
						createP(im,'Цена: '+Obj.Recepts[id_recept].Items[id_Ritems].Cost);
					}
					createDiv(i,null,null,'clear');
				}
			}
		}
	}
	if (!a) $('#list').append(d_id);
	else $('#u_'+a).append(d_id);
}
function urlUseRecept(id,id_recept){
	if (!ItemsBox[id]){
		var Count = Items[id].Recepts[id_recept].Count*1;
		var value=prompt("Использовать рецепт на все количество?",1);
	}
	else{
		var Count = ItemsBox[id].Recepts[id_recept].Count*1;
		var value=prompt("Использовать рецепт на все количество?",ItemsBox[id].Count);
	}
	value=value.replace(" ", "");
	if (!parseInt(value)) alert('Вы ввели некорректные данные!')
	else{
		var RCount=1
		var s='';
		if (!ItemsBox[id]) AddItemToBox(id);
		value=parseInt(value);
		var Ostatok=ItemsBox[id].Count*1-1*value;
		ItemsBox[id].Count=1*value;
		
		var PCount=1*ItemsBox[id].Count;
		if (ItemsBox[id].Count%Count!=0){
			ItemsBox[id].Count=Math.floor(ItemsBox[id].Count/Count)*Count+Count;
			if ($('#Ost').text()=='') $('#Ost').append('<p>Лишнее</p>');
			$('#Ost').append('<p>'+ItemsBox[id].Name+' ['+(ItemsBox[id].Count*1-PCount)+'шт.] по цене: '+ItemsBox[id].OurCost+'('+(ItemsBox[id].OurCost*(ItemsBox[id].Count*1-PCount))+')</p>');
			$('#OstSum span').text($('#OstSum span').text()*1+ItemsBox[id].OurCost*(ItemsBox[id].Count-PCount));
		}
		var PCount=1*ItemsBox[id].Count;
		for(var ID in ItemsBox[id].Recepts[id_recept].Items){
			if (ItemsBox[ID]){
					ItemsBox[ID].Count=ItemsBox[ID].Count*1+Math.ceil(ItemsBox[id].Count*ItemsBox[id].Recepts[id_recept].Items[ID].Count/Count);
					$('#box_count'+ID).text(ItemsBox[ID].Count);
					if (p==1) createDOMItem(ItemsBox[ID],ID);
			}
			else s+=ID+',';
		}
		s+='0';
		if (s=='0') UseRecept({Count:0},[id,id_recept,PCount,Ostatok]);
		else DoRequest('fitems.php','show&use_recept='+s+Q_all(),UseRecept,[id,id_recept,PCount,Ostatok]);
	}
}
function UseRecept(q,id){
	var Count = ItemsBox[id[0]].Recepts[id[1]].Count*1;
	var PCount=id[2];
	var Ostatok=id[3];
	if(q.Count!='0') {
		for(var ID in q.Items){
			Items[ID]={};
			$.extend(Items[ID],q.Items[ID]);
			Items[ID].Count=Math.ceil(ItemsBox[id[0]].Count*ItemsBox[id[0]].Recepts[id[1]].Items[ID].Count/Count);
			AddItemToBox(ID);Items[ID]={};
			if (p==1) createDOMItem(ItemsBox[ID],ID);
		}
	}
	AddItemToBoxLog(id[0]);
	if ((PCount>=ItemsBox[id[0]].Count)&&(Ostatok<=0)){
		delete ItemsBox[id[0]];
		$('#box'+id[0]).remove();
		if(p==1) $('#'+id[0]).remove();
	}
	else{
		ItemsBox[id[0]].Count=Ostatok*1;
		ItemsBox[id[0]].Count=Ostatok*1;
		$('#list #'+id[0]+' #t'+id[0]+' p:eq(2) span:eq(0)').text(ItemsBox[id[0]].Count);
		$('#box_log_count'+id[0]).text(ItemsBoxLog[id[0]].Count);
		$('#box_count'+id[0]).text(ItemsBox[id[0]].Count);
	}
	ItogoCost();
	$('#status').html('Ok');
}
function createOurCost(ID){
	var value=prompt("Введите цену за единицу",ItemsBox[ID].Cost);
	value=value.replace("ю", ".");
	value=value.replace(",", ".");
	if (value*1>0){
		ItemsBox[ID].OurCost=value;
		ItogoCost();
	}	
	if (ge(ID)){
		$('#list #'+ID+' #t'+ID+' p:eq(1) span:eq(1)').text(ItemsBox[ID].OurCost);
	}
}
function createPayCount(ID){
	var value=prompt("Введите кол-во",ItemsBox[ID].PayCount);
	value=value.replace(",", "");
	if (value*1>0) {
		ItemsBox[ID].PayCount=value;
		ItogoCost();
		if (ge(ID)){
			$('#list #'+ID+' #t'+ID+' p:eq(2) span:eq(1)').text(ItemsBox[ID].PayCount);
		}
		if (ItemsBox[ID].PayCount==ItemsBox[ID].Count) $('#list #'+ID+' #t'+ID+' #check').html('<img src="img/tick_16.png" />');
		else $('#list #'+ID+' #t'+ID+' #check').html('');
	}
}
function createCount(ID){
	var value=prompt("Введите кол-во",ItemsBox[ID].Count);
	if (value*1 > 0) {
		ItemsBox[ID].Count=value;
		$('#box_count'+ID).text(ItemsBox[ID].Count);
		ItogoCost();
		if (ge(ID)){
			$('#list #'+ID+' #t'+ID+' p:eq(2) span:eq(0)').text(ItemsBox[ID].Count);
		}
		if (ItemsBox[ID].PayCount==ItemsBox[ID].Count) $('#list #'+ID+' #t'+ID+' #check').html('<img src="img/tick_16.png" />');
		else $('#list #'+ID+' #t'+ID+' #check').html('');
	}
}
function deleteIDItems(ID){
	$('#box'+ID).remove();
	delete ItemsBox[ID];
	if(p==1) $('#'+ID).remove();
	ItogoCost();
}
function ItogoCost(){
	var s=0;
	for(var key in ItemsBox) s+=ItemsBox[key].Cost*ItemsBox[key].Count;
	$('#Sum span').text(s); 
	s=0;
	for(key in ItemsBox){
		if (ItemsBox[key].OurCost) s+=ItemsBox[key].OurCost*ItemsBox[key].Count;
		else s+=ItemsBox[key].Cost*ItemsBox[key].Count;
	}
	$('#OurSum span').text(Math.ceil(s));
	s=0;
	for(key in ItemsBox){
		if (ItemsBox[key].OurCost) s+=ItemsBox[key].OurCost*ItemsBox[key].PayCount;
		else s+=ItemsBox[key].Cost*ItemsBox[key].PayCount;
	}
	$('#PaySum span').text(Math.ceil(s));
	$('#OstPaySum span').text(Math.ceil($('#OurSum span').text())-s*1);
}
function Load(q,a){
	for(var ID in q){
		if (Items[ID]){
			for(var key in q[ID]){
				if (!Items[ID][key]){
					if ((key=='ID')||(key=='Name')||(key=='LG')||(key=='Count')||(key=='Level')||(key=='Cost')||(key=='Image')) Items[ID][key]=q[ID][key];
					else{
						Items[ID][key]={};
						$.extend(Items[ID][key],q[ID][key]);
					}
				}
			}
			$('#'+ID).remove();
			if ((a)&&(a==1)) AddItemToBox(ID);
			createDOMItem(Items[ID],ID);
		}
		if (ItemsBox[ID]){
			for(var key in q[ID]){
				if (!ItemsBox[ID][key]){
					if ((key=='ID')||(key=='Name')||(key=='LG')||(key=='Count')||(key=='Level')||(key=='Cost')||(key=='Image')) ItemsBox[ID][key]=q[ID][key];
						else {
							ItemsBox[ID][key]={};
							$.extend(ItemsBox[ID][key],q[ID][key]);
						}
				}
			}
			$('#'+ID).remove();
			createDOMItem(ItemsBox[ID],ID);
		} 
	}
	$('#status').html('Ok');
}
function CreateDOMAPage(count,q){
	var i=1;
	var s='';
	if (!q.L1) q.L1=0;
	while((count-i)>=12){
		s+='<div id="'+(i*1-1)+'" class="page">'+(i)+'-'+(i*1+11)+'</div>';
		i=i+12;
	}
	s+='<div id="'+(i*1-1)+'" class="page">'+(i)+'-'+count+'</div>';
	s+='<div class="clear"></div>';
	$('#pages').html(s);
	$('#pages').show(speed);
	$('#pages #'+q.L1).attr('class','page active');
	s='';for(var key in q) if (key!='L1') s+=key+'='+q[key]+'&';
	$('#pages>div').click(function(){
		DoRequest('farmy.php',s+Q_all()+'&L1='+this.id,aItems);
	});
}
function aItems(ItemTMP){
	if (ItemTMP.Count>12) CreateDOMAPage(ItemTMP.Count,ItemTMP.Q);
	else $('#pages').html('');
	createArmy(ItemTMP.Army);
}
function createArmy(Obj){
	Army={};
	$.extend(Army,Obj);
	$('#list').show();
	createDOMArmys(Army);
}
function createDOMArmys(Obj){
	$('#list').html('');
	for (var ID in Obj) createDOMArmy(Obj[ID],ID);
	$('#status').html('Список загружен');
}
function ArmyReplaceR(ID,IDx){
DoRequest('farmy.php','show&ID='+IDx,ArmyReplaceDOM,[ID,IDx]);
}
function ArmyReplaceDOM(Obj,a){
$('#status').html('Список загружен');
var IDp=a[0];
var IDx=a[1];
Army[IDx]={};
$.extend(Army,Obj.Army);
delete Army[IDp];
$('#'+IDp).html('');
$('#'+IDp).attr('id',IDx);
for (var ID in Obj.Army) createDOMArmy(Obj.Army[ID],IDx,1);

}

function createDOMArmy(Obj,ID,a){
	var d_id=createDiv(null,null,ID,'army');
	var d_title=createDiv(d_id,null,'t'+ID,'title');
	createDiv(d_title,null,'check','right');
	if (ItemsBox[ID]){
		if (ItemsBox[ID].Count==ItemsBox[ID].PayCount) createIMG(d_title,'../img/tick_16.png','',{align:"right"},'');
		createIMG(d_title,'../img/b_drop.png','Удалить из корзины',{align:"right"},'deleteIDItems('+ID+')');
	}
	else{
		createIMG(d_title,'../img/buy_16.png','Добавить в корзину',{align:"right"},'AddItemToBox('+ID+')');
	}
	createP(d_title,'L-<span>'+Obj.Level+'</span> <span style="color:#663300">'+Obj.Name+'</span><a href="http://www.fantasyland.ru/cgi/army_desc.php?id='+Obj.LG+'" target=_blank ><img src="../img/info_R.gif" height="11px" width="11px" title="Исходный" /></a>');
	var img=createDiv(d_title,null,'img'+ID,'b72');
	var img1=createDiv(img,null,null,'center70');
	img1.style.backgroundImage="url('"+pathA+Obj.Image+"')";
	dd=createDiv(d_title,null,null,'PN');
	if (Obj.Prior) dd.innerHTML+='<div class="bac" onclick="ArmyReplaceR('+ID+','+Obj.Prior.ID+')"><img src="'+pathA+Obj.Prior.Image+'" width="35" height="35" align="top" alt="L-'+Obj.Prior.Level+' '+Obj.Prior.Name+'" title="L-'+Obj.Prior.Level+' '+Obj.Prior.Name+'" /></div>';
	if (Obj.Next) {
		if (Obj.Prior) i=0;else i=37;
		dd.innerHTML+='<div class="bac" style="margin-top:'+i+'px;" onclick="ArmyReplaceR('+ID+','+Obj.Next.ID+')"><img src="'+pathA+Obj.Next.Image+'" width="35" height="35" align="top" alt="L-'+Obj.Next.Level+' '+Obj.Next.Name+'" title="L-'+Obj.Next.Level+' '+Obj.Next.Name+'" /></div>';
	}
	if (Obj.UN*1==0) var cost = createP(d_title,'Цена: <span>'+Obj.Cost+'</span>'+money);
	else var cost = createP(d_title,'Цена: <span>'+Obj.Cost+'</span>'+unmoney);
	if (ItemsBox[ID]){
		cost.innerHTML+='(<span>'+Obj.OurCost+'</span>'+money;
		createIMG(cost,'../img/b_edit.png','Изменить цену',null,'createOurCost('+ID+')');
		cost.innerHTML+=')';
	}
	else createP(d_title,'');
	if (!Obj.PayCount) Obj.PayCount=0;
	if (ItemsBox[ID]){
		var count = createP(d_title,'Кол-во: <span>'+Obj.Count+'</span>');
		createIMG(count,'../img/b_edit.png','Изменить кол-во',null,'createCount('+ID+')');
	}
	if (Obj.Reqs){
		d=createDiv(d_title,'<span>Требования:&nbsp;</span>','Reqs'+ID);
		for (key in Obj.Reqs)
			if (key==3) createDiv(d,'Гильдия: <span>'+Guilds[Obj.Reqs[key]]+'</span>');
			else
				if (StatsImage[key]!='') createDiv(d,'<img src="http://www.fantasyland.ru/images/miscellaneous/'+StatsImage[key]+'" align="absmiddle" height="20px" width="20px" title=""  alt=""  />&nbsp;'+Reqs[key]+':&nbsp;<span>+'+Obj.Reqs[key]+'</span>',null);
				else createDiv(d,Reqs[key]+': <span>+'+Obj.Reqs[key]+'</span>');
	}
	var d=createDiv(d_id,'<span>Статы:&nbsp;</span>','Stats'+ID);
	createP(d,'<img src="http://www.fantasyland.ru/images/miscellaneous/attack_k.gif" alt="Атака" title="Атака" align="absmiddle" width="20px" height="20px" />&nbsp;&nbsp;<span>+'+Obj.Damage+'</span>');
	if (Obj.Defence) createP(d,'<img src="http://www.fantasyland.ru/images/miscellaneous/defence_all.gif" alt="Защита" title="Защита" align="absmiddle" width="20px" height="20px" />&nbsp;&nbsp;<span>+'+Obj.Defence+'</span>');
	if (Obj.Stats){
		var s='';
		for (key in Obj.Stats){
			for (key2 in Obj.Stats[key]){
				if (key2=='Value'){
					if(StatsImage[key].indexOf('.gif') + 1) {
						s='<img src="../effect/'+StatsImage[key]+'" align="absmiddle" height="20px" width="20px" alt="" title=""  />&nbsp;'+Effects[key]+':&nbsp;';
					}
					else{
						if (Math.abs(Obj.Stats[key]['Value'])<10){
							if (StatsImage[key]!='') s='<img src="../effect/'+StatsImage[key]+'_0'+Math.abs(Obj.Stats[key]['Value'])+'.gif" align="absmiddle" height="20px" width="20px" alt="" title=""  />&nbsp'+Effects[key]+':&nbsp;';
							else s=Effects[key]+':';
						}
						else {
							if (StatsImage[key]!='') s='<img src="../effect/'+StatsImage[key]+'_'+Math.abs(Obj.Stats[key]['Value'])+'.gif" align="absmiddle" height="20px" width="20px" alt="" title=""  />&nbsp;'+Effects[key]+':&nbsp;';
							else s=Effects[key]+':';
						}
					}
					if (Obj.Stats[key]['Value']>0) s+='<span>+'+Obj.Stats[key]['Value']+'</span>'; 
					else if (Obj.Stats[key]['Value']<0) s+='<span>'+Obj.Stats[key]['Value']+'</span>';
				}
				else{
					s+='<p>Подробнее:</p>'
					for (var key3 in Obj.Stats[key]['Effect']){
						switch (Obj.Stats[key]['Effect'][key3]*1){
							case 0: s+='<p style="padding-left:20px;"><img src="http://www.fantasyland.ru/images/miscellaneous/'+StatsImage[key3]+'" align="absmiddle" height="20px" width="20px" alt="" />&nbsp;<span>+'+Obj.Stats[key]['Value']+'</span>&nbsp;'+Stats[key3]+'</p>';break;
							case 1: s+='<p style="padding-left:20px;"><img src="http://www.fantasyland.ru/images/miscellaneous/'+StatsImage[key3]+'" align="absmiddle" height="20px" width="20px" alt="" />&nbsp;<span>'+Obj.Stats[key]['Value']+'</span>&nbsp;'+Stats[key3]+' сопернику</p>';break;
							case 2: s+='<p style="padding-left:20px;"><img src="http://www.fantasyland.ru/images/miscellaneous/'+StatsImage[key3]+'" align="absmiddle" height="20px" width="20px" alt="" />&nbsp;<span>+'+Obj.Stats[key]['Value']+'</span>&nbsp;'+Stats[key3]+' всем в группе</p>';break;
							case 3: s+='<p style="padding-left:20px;"><img src="http://www.fantasyland.ru/images/miscellaneous/'+StatsImage[key3]+'" align="absmiddle" height="20px" width="20px" alt="" />&nbsp;<span>'+Obj.Stats[key]['Value']+'</span>&nbsp;'+Stats[key3]+' у всех соперников</p>';break;
							default:break;
						}
					}
				}
			}
			createDiv(d,s,null,'clear');
		}
	}
	createDiv(d_id,null,null,'clear');
	if ((ge('Shops').checked)&&(Obj.Shops)){
		d=createDiv(d_id,'<span>Магазины:</span>','Shops'+ID);
		for (key in Obj.Shops) createP(d,key+': '+Obj.Shops[key]);
	}
	if ((ge('Sales').checked)&&(Obj.Sales)){
		d=createDiv(d_id,'<span>Продажи ('+Obj.Sales.info+')</span>','Sales'+ID,'sales',1);
		for (key in Obj.Sales)
			if(key!='info') createP(d,'[<span>'+Obj.Sales[key].Count+'</span>] по <span>'+Obj.Sales[key].Price+'</span> в '+Obj.Sales[key].Name+'<a href="http://www.fantasyland.ru/cgi/v_trade_load_shop.php?id='+key+'" target=_blank><img src="img/info_R.gif" height=11 width=11 alt="инфо" /></a>');
		
	}
	if ((ge('Sales').checked)&&(Obj.Pays)){
		d=createDiv(d_id,'<span>Скупка ('+Obj.Pays.info+')</span>','Pays'+ID,'pays',1);
		for (key in Obj.Pays)
			if(key!='info')	createP(d,' За <span>'+Obj.Pays[key].Price+'</span> в '+Obj.Pays[key].Name+'<a href="http://www.fantasyland.ru/cgi/v_trade_show_goods_for_sale.php?id='+key+'" target=_blank><img src="img/info_R.gif" height=11 width=11 alt="инфо" /></a>');
		
	}
	if (!a) $('#list').append(d_id);
	else $('#'+ID).html(d_id.innerHTML);
}
function SaveBox(name){
	var s='{';
	for( var ID in ItemsBox){
		if (!ItemsBox[ID].PayCount) ItemsBox[ID].PayCount=0;
		s+=ID+':{Count:"'+ItemsBox[ID].Count+'",PayCount:"'+ItemsBox[ID].PayCount+'",OurCost:"'+ItemsBox[ID].OurCost+'",Image:"'+ItemsBox[ID].Image+'"},';
	}
	s+=",}";s=s.replace(",,", "");
	if (name==0){
		var name=prompt("Введите название комплекта","");
		if (name=="") alert("Вы не задали название");
		else{
			if ($.cookie('complect'+ decodeURIComponent(name))) alert("Такое название уже существует!");
			else SaveCookie('complect'+ decodeURIComponent(name), s);
		}
	}
	else SaveCookie('complect'+name, s);
	$("#box_name").html(f_complect());
}
function SaveCookie(name,value){
if (document.cookie && document.cookie != ''){
	$.cookie(name, value, {
		expires: 1000,
		path: "/servise/enc",
		domain: "electa-lg.com",
		secure: false
	});
	alert('Комплект сохранен!');
}else alert('Проблема сохранения через куки!');
}
function OpenBox1(name){
	if (name!=0){
		Items={};ItemsBox={};
		ClearBox();$('#list').html('');$('#pages').html('');
		Items=myEval('('+$.cookie('complect'+name)+')');
		fID('Sales');
		for(var key in Items) AddItemToBox(key);
	}
}
function DeleteBox(name){
if (document.cookie && document.cookie != ''){
	$.cookie('complect'+name, null, {
		expires: 1,
		path: "/servise/enc",
		domain: "electa-lg.com",
		secure: false
	});
	alert('Комплект удален!');
	$("#box_name").html(f_complect());
}
}
function ShowLog(){
	var s='<h2>Полный лог</h2>';
	s+='<ul style="list-style-type:none;">';
	var allcost=0;
	for (var ID in ItemsBox) allcost+=ItemsBox[ID].Count*ItemsBox[ID].OurCost;
	for (var ID in ItemsBox){
	if (ItemsBox[ID].PayCount!=0)
	s+='<li>'+ItemsBox[ID].Name+' ['+ItemsBox[ID].Count+'/'+ItemsBox[ID].PayCount+' шт.] по '+ItemsBox[ID].OurCost+' всего на ['+(Math.ceil(ItemsBox[ID].Count*ItemsBox[ID].OurCost)*1)+'/'+(Math.ceil(ItemsBox[ID].PayCount*ItemsBox[ID].OurCost)*1)+'] - '+Math.ceil(ItemsBox[ID].Count*ItemsBox[ID].OurCost*100/allcost)+'%</li>'
	else
	s+='<li>'+ItemsBox[ID].Name+' ['+ItemsBox[ID].Count+' шт.] по '+ItemsBox[ID].OurCost+' всего на ['+(Math.ceil(ItemsBox[ID].Count*ItemsBox[ID].OurCost)*1)+'] - '+Math.ceil(ItemsBox[ID].Count*ItemsBox[ID].Cost*100/allcost)+'%</li>'
	}
	s+='</ul>';

	s+='<h2>Куплено</h2>';
	s+='<ul style="list-style-type:none;">';
	for (var ID in ItemsBox)
		if (ItemsBox[ID].PayCount!=0) s+='<li>'+ItemsBox[ID].Name+' ['+ItemsBox[ID].PayCount+' шт.] по '+ItemsBox[ID].OurCost+' всего на ['+(Math.ceil(ItemsBox[ID].PayCount*ItemsBox[ID].OurCost)*1)+']</li>';
	s+='</ul>';
	s+='<h2>Осталось закупить</h2>';
	s+='<ul style="list-style-type:none;">';
	for (var ID in ItemsBox)
		if (ItemsBox[ID].Count-ItemsBox[ID].PayCount>0) s+='<li>'+ItemsBox[ID].Name+' ['+(ItemsBox[ID].Count-ItemsBox[ID].PayCount*1)+' шт.] по '+ItemsBox[ID].OurCost+' всего на ['+(Math.ceil((ItemsBox[ID].Count-ItemsBox[ID].PayCount)*ItemsBox[ID].OurCost)*1)+']</li>';
	s+='</ul>';
	
	s+='<h2 style="margin-top:20px;">Закупка (количество)</h2>';
	s+='<ul style="list-style-type:none;">';
	for (var ID in ItemsBox){
	if (ItemsBox[ID].PayCount!=0)
	s+='<li>'+ItemsBox[ID].Name+' ['+ItemsBox[ID].Count+'/'+ItemsBox[ID].PayCount+' шт.]</li>';
	else
	s+='<li>'+ItemsBox[ID].Name+' ['+ItemsBox[ID].Count+' шт.]</li>';
	}
	s+='</ul>';
	s+='<h2>Куплено (количество)</h2>';
	s+='<ul style="list-style-type:none;">';
	for (var ID in ItemsBox)
		if (ItemsBox[ID].PayCount!=0) s+='<li>'+ItemsBox[ID].Name+' ['+ItemsBox[ID].PayCount+' шт.]</li>';
	s+='</ul>';
	s+='<h2>Осталось закупить(количество)</h2>';
	s+='<ul style="list-style-type:none;">';
	for (var ID in ItemsBox)
		if (ItemsBox[ID].Count-ItemsBox[ID].PayCount>0) s+='<li>'+ItemsBox[ID].Name+' ['+(ItemsBox[ID].Count-ItemsBox[ID].PayCount*1)+' шт.]</li>';
	s+='</ul>';

	$('#list').html(s);
}
