var rear_item;

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function Show(arr, result)
{
	var Items = document.getElementById('Items');
	var ItemsContainer = document.getElementById('ItemsList');
	ItemsContainer.style.visibility = "hidden";
	Items.innerHTML = '';
	var sForm = document.getElementById('search_form');
	var hint = '';

	if (result == ""){
	
	if (arr=="Helmets"){
	   var IList=Helmets;
	   var idIList="Helmets";
	}else if (arr=="Amulets"){
	   var IList=Amulets;
	   var idIList="Amulets";
	}else if (arr=="Armor"){
	   var IList=Armor;
	   var idIList="Armor";
	}else if (arr=="Naruchi"){
	   var IList=Naruchi;
	   var idIList="Naruchi";
                   }else if (arr=="Gloves"){
	   var IList=Gloves;
	   var idIList="Gloves";
            	}else if (arr=="Boots"){
	   var IList=Boots;
	   var idIList="Boots";
	}else if (arr=="Distant"){
	   var IList=Distant;
	   var idIList="Distant";
	}else if (arr=="Weapon"){
	   var IList=Weapon;
	   var idIList="Weapon";
	}else if (arr=="Rings"){
	   var IList=Rings;
	   var idIList="Rings";
	}else if (arr=="Belts"){
	   var IList=Belts;
	   var idIList="Belts";
	}else if (arr=="Shields"){
	   var IList=Shields;
	   var idIList="Shields";
	}

	hint += '<div style="width:247px;height:455px;background:#FFF; border:0; overflow:auto; font-size:12px; color:#00000;">';


	for (i=0;i<IList.length;i++) 
	{
	  if (IList[i][0] == "Уровень 1"){
	  	hint += '<span style="color:#000000;font-weight:bold;background:#CCAF53;width:150px;">-------' + IList[i][1] + '-------</span><br>';
	  }
	  else if (IList[i][0] == "Уровень 2"){
	  	hint += '<span style="color:#000000;font-weight:bold;background:#CCAF53;width:150px;">-------' + IList[i][1] + '-------</span><br>';
	  }
	  else if (IList[i][0] == "Уровень 3"){
	  	hint += '<span style="color:#000000;font-weight:bold;background:#CCAF53;width:150px;">-------' + IList[i][1] + '-------</span><br>';
	  }
	  else if (IList[i][0] == "Уровень 4"){
	  	hint += '<span style="color:#000000;font-weight:bold;background:#CCAF53;width:150px;">-------' + IList[i][1] + '-------</span><br>';
	  }
	  else if (IList[i][0] == "Уровень 5"){
	  	hint += '<span style="color:#000000;font-weight:bold;background:#CCAF53;width:150px;">-------' + IList[i][1] + '-------</span><br>';
	  }
	  else if (IList[i][0] == "Уровень 6"){
	  	hint += '<span style="color:#000000;font-weight:bold;background:#CCAF53;width:150px;">-------' + IList[i][1] + '-------</span><br>';
	  }
	  else if (IList[i][0] == "Уровень 7"){
	  	hint += '<span style="color:#000000;font-weight:bold;background:#CCAF53;width:150px;">-------' + IList[i][1] + '-------</span><br>';
	  }
	  else if (IList[i][0] == "Уровень 8"){
	  	hint += '<span style="color:#000000;font-weight:bold;background:#CCAF53;width:150px;">-------' + IList[i][1] + '-------</span><br>';
	  }
	  else if (IList[i][0] == "Уровень 9"){
	  	hint += '<span style="color:#000000;font-weight:bold;background:#CCAF53;width:150px;">-------' + IList[i][1] + '-------</span><br>';
	  }
	  else if (IList[i][0] == "Уровень 10"){
	  	hint += '<span style="color:#000000;font-weight:bold;background:#CCAF53;width:150px;">-------' + IList[i][1] + '-------</span><br>';
	  }
	  else if (IList[i][0] == "Уровень 11"){
	  	hint += '<span style="color:#000000;font-weight:bold;background:#CCAF53;width:150px;">-------' + IList[i][1] + '-------</span><br>';
	  }
	  else if (IList[i][0] == "Уровень 12"){
	  	hint += '<span style="color:#000000;font-weight:bold;background:#CCAF53;width:150px;">-------' + IList[i][1] + '-------</span><br>';
	  }
	  else if (IList[i][0] == "Уровень 13"){
	  	hint += '<span style="color:#000000;font-weight:bold;background:#CCAF53;width:150px;">-------' + IList[i][1] + '-------</span><br>';
	  }
	  else if (IList[i][0] == "Уровень 14"){
	  	hint += '<span style="color:#000000;font-weight:bold;background:#CCAF53;width:150px;">-------' + IList[i][1] + '-------</span><br>';
	  }
	  else
	  	hint += '<a href="#" style="color:#000;" onClick = "SelectItem('+"'"+idIList+"',"+i+')">' + IList[i][0] + '</a><br>';
	}

	
	hint += '</div>';
	ItemsContainer.style.visibility = "visible";
	
	Items.innerHTML = hint;

	sForm.innerHTML = '<b>Введите часть названия предмета:</b>&nbsp;<input type="text" id="sString" name="sItem" style="width:250px;" value="" onKeyUp=Search("' + idIList + '")>';
	}
	else {
  	   ItemsContainer.style.visibility = "visible";
	   Items.innerHTML = result;	   
	}

}

function SelectItem(idArray, numArray)
{
	SetNullData();

	var IList;
	var dItems = document.getElementById('prod_info');
	var rearItem = document.getElementById('rear_item');
	//rearItem.innerHTML = '<img src="images/calc/new_calc_08_clear.jpg" width="100" height="179" alt="">';
	var hint = "";
	var names;
	var endn;
	var percent = '';
	var symbol = '+';
	var amm = new Array();

	if (idArray=="Helmets") {IList=Helmets[numArray];}
	else if (idArray=="Amulets") {IList=Amulets[numArray];}
	else if (idArray=="Armor") {IList=Armor[numArray];}
	else if (idArray=="Boots") {IList=Boots[numArray];}
	else if (idArray=="Distant") {IList=Distant[numArray];}
	else if (idArray=="Naruchi") {IList=Naruchi[numArray];}
                 else if (idArray=="Gloves") {IList=Gloves[numArray];}
	else if (idArray=="Weapon") {IList=Weapon[numArray];}
	else if (idArray=="Rings") {IList=Rings[numArray];}
	else if (idArray=="Belts") {IList=Belts[numArray];}
	else if (idArray=="Shields") {IList=Shields[numArray];}
	else if (idArray=="All") {IList=All[numArray];}
	
	//show ammunition for distant weapon
	if(idArray=="Distant"){
	  amm = IList[33].split(" ");
	  document.getElementById('amm_total').value = amm[1];
	  document.getElementById('amm_left').value = amm[1];
	  document.getElementById("show_amm").style.visibility = "visible";
	}
	else document.getElementById("show_amm").style.visibility = "hidden";

	hint += '<table border="0" cellpadding="0" cellspacing="0" style="font-size:8pt;font-family:Arial;color:#000;padding-left:3px;" align="left" width="252">';
	hint += '<tr><td align="center" style="padding-bottom:15px;"><img src= "/i/c_items/'+IList[1]+'" onClick="ClearItem()"></td></tr>'+
		'<tr><td style="padding-left:3px;font-size:10pt;"><b>'+IList[0]+'</b><br>';
	for (i=2;i<5;i++)
	{
		if (IList[i] != '0'){
			if (i==4){
				names = "<b>"+stats[i];
				endn ="</b>";
			}else{
				names = stats[i];
				endn ="";
			}
			hint += names+' '+IList[i]+endn+'<br>';
		}
	}
	hint +='</td></tr><tr><td width="139px" valign="top"><hr style="width:250px;" no-shadow align="left">';
	
	if(IList[31] > 0 && IList[32] > 0)
	{
	   hint += '<span align="left" style = "font-size:9pt;color:#000000"><b>Урон ' + IList[31] + ' - ' + IList[32] + '</b><br>';
	}

	for (i=5;i<stats.length-1;i++)
	{
		//if (IList[i] != 0 && IList[i] != 31 && IList[i] != 32){
		if (IList[i] != 0 && i != 31 && i != 32){
			if (i>10&&i<27){
				percent = '%';
				if ((i > 14 && i < 19) || (i > 22 && i < 27)) {
					symbol = '-';
				} else {
					symbol = '+';
				}
			}			
			else{
				percent = '';
				symbol = '+';
			}

			if (parseInt(IList[i]) < 0){
				symbol = '';
			}

			if(stats[i] != "" && stats[i] != "+")
			{			
			   var names = stats[i];
			   hint += '<span align="left" style = "font-size:9pt;color:#000">' + names + ' ' + symbol + IList[i] + percent + '<br>';
			}
			else if (stats[i] == "+")
			{
			   symbol = '';
			   hint += '<span align="left" style = "font-size:9pt;color:#000"><b>' + symbol + IList[i] + '<br>';
			}
		}
	}
	hint += '</td></tr></table>';
	dItems.innerHTML = hint;

	document.getElementById('gov_price').value = ""+IList[3];
	document.getElementById('durability_left').value = ""+IList[2];
	document.getElementById('durability_total').value = ""+IList[2];

	if (IList[IList.length-1] != '1'){
		this.rear_item = 1;
		//rearItem.innerHTML = '<img src="images/calc/new_calc_08.jpg" width="100" height="79" alt="">';
	}
	else {	
		this.rear_item = 0;
		//rearItem.innerHTML = '<img src="images/calc/new_calc_08_clear.jpg" width="100" height="179" alt="">';
	}

	Recalculate();
}

function Recalculate(own_price){
	var tmp, tmp2;
	var buy = document.getElementById('buy').value;
	var tax = document.getElementById('tax').value;
	var sell = document.getElementById('sell').value;
	var buy_price = document.getElementById('buy_price').value;
	var sell_price = parseFloat(document.getElementById('sell_price').value);
	var gov_price = parseFloat(document.getElementById('gov_price').value);
	var revenue = document.getElementById('revenue').value;
	var money_needed = document.getElementById('money_needed').value;
	var sell_rm = document.getElementById('sell_rm').value;
	var rTotal = document.getElementById('rTotal');
	var dur_left = parseInt(document.getElementById('durability_left').value);
	var dur_total = parseInt(document.getElementById('durability_total').value);
	var amm_left = parseInt(document.getElementById('amm_left').value);
	var amm_total = parseInt(document.getElementById('amm_total').value);

	var rearItem = document.getElementById('rear_item');

        document.getElementById('repair_cost').value = "0";
        document.getElementById('amm_cost').value = "0";
	

	//begin calculations
	  if (sell_rm == "") sell_rm = 0;

 	  if (own_price == 1){
	     buy = Math.round(parseFloat(buy_price / gov_price) * 10000) / 100; 
	     document.getElementById('buy').value = buy;
	  }

          if (gov_price < sell_price) {
		sell_price = gov_price;
		document.getElementById('sell_price').value = sell_price;
 	  }


	  if (own_price == 2){
	     tmp = parseFloat(gov_price * tax) / 100;
	     sell_price = parseFloat(sell_price - tmp);
             sell = parseFloat(sell_price / gov_price * 100);
             sell = parseFloat(sell) + parseFloat(tax);
	     document.getElementById('sell').value = sell;
	  }

	  sell_price = parseFloat(gov_price) * sell / 100;

	  buy = parseFloat(gov_price * buy / 100);

	  tmp = buy * tax / 100;
	  money_needed = tmp + buy;	  
	  tmp = sell_price - sell_price * tax / 100;
	  revenue = tmp - money_needed;
	  sell_rm = parseFloat(sell_rm) - sell_rm * 0.1;

   	  
	    if (dur_left > dur_total){
	      dur_left = dur_total;
	      document.getElementById('durability_left').value = dur_left;
	    }
	    if (dur_left < dur_total){
	      tmp = Math.round(parseFloat(gov_price) / parseFloat(dur_total) * 10) / 100;

	      if (tmp < 0.01) {
	        tmp = 0.01;
  	      }
	      tmp2 = Math.round(parseFloat(dur_total - dur_left) * tmp * 100) /100; 
	      buy = buy - tmp2;

	      document.getElementById('repair_cost').value = tmp2;
	    }

	    if (amm_left > amm_total){
	      amm_left = amm_total;
	      document.getElementById('amm_left').value = amm_left;
	    }
	    if (amm_left < amm_total){
	      tmp = Math.round(parseFloat(gov_price) / parseFloat(amm_total) / 2 * 100) / 100;

	      if (tmp < 0.01) {
	        tmp = 0.01;
  	      }
	      tmp2 = Math.round(parseFloat(amm_total - amm_left) * tmp * 100) / 100; 
	      buy = buy - tmp2;

	      document.getElementById('amm_cost').value = tmp2;
	    }

	  
	//end calculations

	document.getElementById('revenue').value = roundNumber(revenue, 2);
	document.getElementById('sell_price').value = roundNumber(sell_price, 2);
	document.getElementById('buy_price').value = roundNumber(buy, 2);
	document.getElementById('money_needed').value = roundNumber(money_needed, 2);
	document.getElementById('revenue_rm').value = roundNumber(sell_rm, 2);

	rTotal.innerHTML = '<b style="font-size:12px;color:#4E0100;">Общая прибыль </b><br><b>' + Math.round(parseFloat(revenue + sell_rm) * 100) / 100 + '</b>';

	if (revenue < 0){
	  //if(this.rear_item == 0)
		 //rearItem.innerHTML = '<img src="images/calc/new_calc_08.jpg" width="100" height="79" alt=""><br><img src="images/calc/new_calc_08_3.gif" width="100" height="100" alt="">';
	  //else rearItem.innerHTML = '<img src="images/calc/new_calc_08_3.gif" width="100" height="100" alt="">';
	}
	else {
	  //f(this.rear_item == 0)
		//rearItem.innerHTML = '<img src="images/calc/new_calc_08.jpg" width="100" height="79" alt="">';
	  //else
		//rearItem.innerHTML = '<img src="images/calc/new_calc_08_clear.jpg" width="100" height="179" alt="">';
	}


}

function Search(arr){
	var hint = '';
	var v = document.getElementById('sString').value;
	var sForm = document.getElementById('search_form');
	var found = 0;
	var re;
	var doS;
	
	if (arr=="Helmets"){
	   var IList=Helmets;
	   var idIList="Helmets";
	}else if (arr=="Amulets"){
	   var IList=Amulets;
	   var idIList="Amulets";
	}else if (arr=="Armor"){
	   var IList=Armor;
	   var idIList="Armor";
	}else if (arr=="Naruchi"){
	   var IList=Naruchi;
	   var idIList="Naruchi";
}else if (arr=="Gloves"){
	   var IList=Gloves;
	   var idIList="Gloves";
	}else if (arr=="Boots"){
	   var IList=Boots;
	   var idIList="Boots";
	}else if (arr=="Distant"){
	   var IList=Distant;
	   var idIList="Distant";
	}else if (arr=="Weapon"){
	   var IList=Weapon;
	   var idIList="Weapon";
	}else if (arr=="Rings"){
	   var IList=Rings;
	   var idIList="Rings";
	}else if (arr=="Belts"){
	   var IList=Belts;
	   var idIList="Belts";
	}else if (arr=="Shields"){
	   var IList=Shields;
	   var idIList="Shields";
	}	

	if (v != ""){
	   hint += '<div style="width:247px;height:455px;background:#FFF; border:0; overflow:auto; font-size:12px;">';
	   hint += '<b>Результат поиска:</b><br>';

 	   re = new RegExp(v, "gi");

	   for (i=0;i<IList.length;i++){
	     if (re.test(IList[i][0])) {	        
		hint += '<a href="#" style="color:#000;" onClick = "SelectItem('+"'"+idIList+"',"+i+')">' + IList[i][0] + '</a><br>';
		found = 1;
	     }
	   }

	if (found == 0){	
	   hint += 'Не найдено...';
	}

        hint += '</div>';

	Show(arr, hint);

	sForm.innerHTML = '<b>Введите часть названия предмета:</b>&nbsp;<input type="text" id="sString" name="sItem" style="width:250px;" value="' + document.getElementById('sString').value + '" onKeyUp=Search("' + idIList + '")>';
	document.getElementById('sString').focus();
	document.getElementById('sString').value = v;
	}
	document.getElementById('sString').focus();
	document.getElementById('sString').value = v;
}


function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}


function SetNullData(){
	//document.getElementById('buy').value = "70";
	//document.getElementById('sell').value = "99";
	document.getElementById('buy_price').value = "0";
	document.getElementById('sell_price').value = "0";
	document.getElementById('gov_price').value = "0";
	document.getElementById('revenue').value = "0";
	document.getElementById('money_needed').value = "0";
	document.getElementById('sell_rm').value = "0";
	document.getElementById('durability_left').value = "0";
	document.getElementById('durability_total').value = "0";
	document.getElementById('amm_left').value = "0";
	document.getElementById('amm_total').value = "0";
	document.getElementById('amm_cost').value = "0";
	document.getElementById('repair_cost').value = "0";
}

