#define SHOW_MAIN		0
#define SHOW_BUY		1
#define SHOW_SELL		2
#define SHOW_DESCRIBE	3

int	nCurScrollNum = -1;
string curItemGroup = ""; // KK
int showType = -1;
string oldCurNode = "";

ref refMyCh;
ref refTrader;

int nTradeQuantity = 0;
float buyratio = 1.0; // NK 05-04-08
bool useskill = true; // NK 05-04-08

int idLngFile = -1;
int buyPrice = 0;
int sellPrice = 0;
//bool anytrades = false; // TIH not needed anymore Aug24'06 - NK 05-04-07 to keep track of how often you use trader.
object gTradetracker; // TIH used to track trades to level up merchant Aug24'06

bool bEnableDown = true;

void InitInterface_RR(string iniName,ref pChar,ref pTrader)
{
	refTrader = pTrader;
	refMyCh = pChar;
	refTrader.dayssincelasttrade = 0;
	string tid = refTrader.id;
	if(CheckAttribute(ShipLookupTable, "itemtraders."+tid)) { ShipLookupTable.itemtraders.(tid) = true; }
	//remove all skill modifiers
	ref pch = GetMainCharacter();
	pch.skiprecalcskill = true;
	DeleteAttribute(&pch,"skill.Leadership.mod"); // NK/GZ items mod
	DeleteAttribute(&pch,"skill.Fencing.mod"); // NK/GZ items mod
	DeleteAttribute(&pch,"skill.Sailing.mod"); // NK/GZ items mod
	DeleteAttribute(&pch,"skill.Accuracy.mod"); // NK/GZ items mod
	DeleteAttribute(&pch,"skill.Cannons.mod"); // NK/GZ items mod
	DeleteAttribute(&pch,"skill.Grappling.mod"); // NK/GZ items mod
	DeleteAttribute(&pch,"skill.Repair.mod"); // NK/GZ items mod
	DeleteAttribute(&pch,"skill.Defence.mod"); // NK/GZ items mod
	DeleteAttribute(&pch,"skill.Commerce.mod"); // NK/GZ items mod
	DeleteAttribute(&pch,"skill.Sneak.mod"); // NK/GZ items mod
	buyratio = 1.0;
	useskill = true;
	if(CheckAttribute(refTrader, "itemtrade.pricebuyratio")) buyratio = stf(refTrader.itemtrade.pricebuyratio); // NK 05-04-08
	if(CheckAttribute(refTrader, "itemtrade.pricenoskill")) useskill = !sti(refTrader.itemtrade.pricenoskill); // NK 05-04-08

// KK -->
	if (!LAi_IsCapturedLocation) {
		GameInterface.title = "titleItemsTrade";
		if(HasSubStr(pChar.location,"store")) { GameInterface.title = "titleStore"; }//MAXIMUS
	}
	else { GameInterface.title = "titleRansack"; }

	curItemGroup = GUN_ITEM_TYPE;
	FillSelectedScroll();
// <-- KK

	//FillScroll();
	SendMessage(&GameInterface,"ls",MSG_INTERFACE_INIT,iniName);

// KK -->
	if (LAi_IsCapturedLocation) {
		SendMessage(&GameInterface, "lsls", MSG_INTERFACE_MSG_TO_NODE, "BUY_BUTTON", 0, XI_ConvertString("Take"));
		SendMessage(&GameInterface, "lsls", MSG_INTERFACE_MSG_TO_NODE, "SELL_BUTTON", 0, XI_ConvertString("Leave"));
	}
// <-- KK
	CreateExitString();//MAXIMUS: standard exit-string for exit-button

	SetCurrentNode("ITEMSLIST");

	CreateString(true,"Money","",FONT_NORMAL,COLOR_MONEY,173,393,SCRIPT_ALIGN_LEFT,0.9); // NK itemtrade 05-04-02 was 320,389,center,1.0
	CreateString(true,"SMoney","",FONT_NORMAL,COLOR_MONEY,467,393,SCRIPT_ALIGN_RIGHT,0.9); // NK itemtrade 05-04-02
	if(CheckAttribute(refMyCh,"firstname")==true) CreateString(true,"ChrName",refMyCh.name + " " + refMyCh.firstname + " " + refMyCh.lastname,FONT_NORMAL,COLOR_NORMAL,320,203,SCRIPT_ALIGN_CENTER,1.0);
	else CreateString(true,"ChrName",refMyCh.name + " " + refMyCh.lastname,FONT_NORMAL,COLOR_NORMAL,320,203,SCRIPT_ALIGN_CENTER,1.0);
	CreateString(true,"ItemsName","",FONT_NORMAL,COLOR_NORMAL,320,245,SCRIPT_ALIGN_CENTER,0.8);
	CreateString(true,"BuyPrice","",FONT_BOLD_NUMBERS,COLOR_NORMAL,260,307,SCRIPT_ALIGN_RIGHT,0.75);
	CreateString(true,"SellPrice","",FONT_BOLD_NUMBERS,COLOR_NORMAL,380,307,SCRIPT_ALIGN_LEFT,0.75);
	CreateString(true,"OnMyQuantity","",FONT_BOLD_NUMBERS,COLOR_NORMAL,79,413,SCRIPT_ALIGN_CENTER,1.0);
	CreateString(true,"OnTraderQuantity","",FONT_BOLD_NUMBERS,COLOR_NORMAL,561,413,SCRIPT_ALIGN_CENTER,1.0);

	CreateString(false,"PriceSum","",FONT_NORMAL,COLOR_NORMAL,324,245,SCRIPT_ALIGN_LEFT,1.0);
	CreateString(false,"TradeQuantity","",FONT_NORMAL,COLOR_NORMAL,320,292,SCRIPT_ALIGN_CENTER,1.0);

	CreateString(TRUE,"itemname","",FONT_NORMAL,COLOR_NORMAL,320,475,SCRIPT_ALIGN_CENTER,0.8); // NK

	SetNodeUsing("BUYSELL_IMAGES",false);
	SetNodeUsing("OK_BUTTON",false);
	SetNodeUsing("CANCEL_BUTTON",false);
	SetNodeUsing("TRADEQUANTITY",false);
	SetNodeUsing("TEXTWINDOW",false);

	SetSelectable("EXIT_BUTTON",true);
	//idLngFile = LanguageOpenFile(GetStorylineDir(FindCurrentStoryline()) + "ItemsDescribe.txt"); // KK

	SetEventHandler("InterfaceBreak","ProcessCancelExit",0);
	SetEventHandler("frame","ProcessFrame",1);
	SetEventHandler("exitCancel","ProcessCancelExit",0);
	SetEventHandler("ievnt_command","ProcessCommandExecute",0);

// KK -->
	//SetSelectable("I_WEAPON", false);
	if (!LAi_IsCapturedLocation) {
		SetNodeUsing("SELL_LOOT_BUTTON",true);
		SetSelectable("SELL_LOOT_BUTTON",true);
	} else {
		SetNodeUsing("SELL_LOOT_BUTTON",false);
	}
// <-- KK
// LDH Fix
//	SetCurrentNode("SELL_LOOT_BUTTON");
}

void ProcessFrame()
{
	if(oldCurNode!=GetCurrentNode())
	{
		oldCurNode = GetCurrentNode();
		if(oldCurNode=="ITEMSLIST")
		{
			nCurScrollNum = -1;
		}
	}

	if(sti(GameInterface.itemslist.current)!=nCurScrollNum)
	{
		nCurScrollNum = sti(GameInterface.itemslist.current);
		SetShowMode(SHOW_MAIN);
		RefreshAllStrings();
		return;
	}

// KK -->
	if (showType == SHOW_MAIN) FreezeScreen(false);
// <-- KK
}

void RefreshAllStrings()
{
	//int tmpLangFileID = LanguageOpenFile("interface_strings.txt");
	int priceSum;
	string itmName = GetItemsID(nCurScrollNum);
	aref refitm;
	int itmIdx = Items_FindItem(itmName,&refitm);
	ref mchref = GetMainCharacter();

	if(itmIdx>=0)
	{
		GameInterface.strings.Money = MakeMoneyShow(sti(mchref.money),MONEY_SIGN,MONEY_DELIVER);
// KK -->
		SetNodeUsing("MAIN_IMAGES", showType == SHOW_MAIN);
		SetSelectable("INFO_BUTTON", true);
		int traderMoney = 0;
		if (!LAi_IsCapturedLocation) traderMoney = GetCharacterMoney(&refTrader);
		if(CheckAttribute(refTrader,"itemtrade.quality")) GameInterface.strings.SMoney = TranslateString("","Qual") + " " + refTrader.itemtrade.quality + ",   " + MakeMoneyShow(traderMoney,MONEY_SIGN,MONEY_DELIVER); // NK itemtrade 05-04-02
		if(CheckAttribute(refitm,"name"))
		{
			// scheffnow - weaponsmod -->
			string sIName = "";
			if (CheckAttribute(refitm, "QualityName"))
			{
				sIName = TranslateString("", refitm.QualityName) + " ";
			}
			GameInterface.strings.ItemsName = sIName + GetAssembledString(TranslateString("", refitm.name), refitm);
			// scheffnow - weaponsmod <--
		} else {
			GameInterface.strings.ItemsName = "";
		}
// <-- KK
		buyPrice = GetTradeItemPrice(itmIdx,PRICE_TYPE_BUY);
		sellPrice = GetTradeItemPrice(itmIdx,PRICE_TYPE_SELL);
		GameInterface.strings.BuyPrice = buyPrice;
		GameInterface.strings.SellPrice = sellPrice;
		GameInterface.strings.OnMyQuantity = GetCharacterItem(refMyCh,itmName);
		GameInterface.strings.OnTraderQuantity = GetCharacterItem(refTrader,itmName);

		GameInterface.strings.TradeQuantity = nTradeQuantity;
		if(showType==SHOW_BUY)
		{	priceSum = nTradeQuantity * GetTradeItemPrice(itmIdx,PRICE_TYPE_BUY);
		}
		else
		{	priceSum = nTradeQuantity * GetTradeItemPrice(itmIdx,PRICE_TYPE_SELL);
		}
		GameInterface.strings.priceSum = priceSum;

		if(showType==SHOW_DESCRIBE)
		{
			SetItemDescribe(refitm);
		}

		SetSelectable("BUY_BUTTON",true);
		SetSelectable("SELL_BUTTON",true);
		if (LAi_IsCapturedLocation == false && buyPrice <= 0) // KK
		{
			SetSelectable("BUY_BUTTON",false);
			SetSelectable("SELL_BUTTON",false);
			ShowHelpString("chelp_itemstrade#10");
			return;
		}

		// TIH --> allow the sell of equipped items, except equipped spyglass, its special Jul15'06
		//if( GetCharacterItem(refMyCh,itmName)==1 && ItemUsedByCharacter(itmIdx) )// old method
		/*if( GetCharacterItem(refMyCh,itmName)==1 && ItemUsedByCharacter(itmIdx) && Items[GetItemIndex(itmName)].groupID == SPYGLASS_ITEM_TYPE )
		{
			SetSelectable("SELL_BUTTON",false);
			ShowHelpString("chelp_itemstrade#11");
			return;
		}*/
		// TIH <--

		SetSelectable("BUY_BUTTON",GetCharacterItem(refTrader,itmName) > 0);
		SetSelectable("SELL_BUTTON", false);
		int quantity = GetCharacterItem(refMyCh, itmName);
		int isEquiped = IsEquipCharacterByItem(refMyCh, itmName);
		if (isEquiped == true && quantity > 0) SetSelectable("SELL_BUTTON", true);
		if (isEquiped == true && Items[GetItemIndex(itmName)].groupID == SPYGLASS_ITEM_TYPE && quantity == 1) SetSelectable("SELL_BUTTON", false);
		if (isEquiped == false && quantity > 0) SetSelectable("SELL_BUTTON", true);

		SendMessage(&GameInterface,"lsl",MSG_INTERFACE_MSG_TO_NODE,"CONTEXTHELP", 0);
	}
	else
	{
		SetSelectable("BUY_BUTTON",false);
		SetSelectable("SELL_BUTTON",false);
// KK -->
		GameInterface.strings.BuyPrice = "";
		GameInterface.strings.SellPrice = "";
		GameInterface.strings.ItemsName = "";
		SetSelectable("INFO_BUTTON", false);
		SetNodeUsing("MAIN_IMAGES", false);
// <-- KK
	}
}

bool ItemUsedByCharacter(int itmIdx)
{
	if(itmIdx<0 || itmIdx>=ITEMS_QUANTITY) return false;
	return IsEquipCharacterByItem(GetMainCharacter(),Items[itmIdx].id);
}

int GetTradeItemPrice(int itmIdx, int tradeType)
{
	int itmprice = 0;
	if(itmIdx<0 || itmIdx>ITEMS_QUANTITY) return 0;
	if (LAi_IsCapturedLocation) return 0; // KK

	if(CheckAttribute(&Items[itmIdx],"price"))
	{
		itmprice = sti(Items[itmIdx].price);
	}

	int skillDelta = GetSummonSkillFromName(refMyCh,SKILL_COMMERCE);
	float skillModify = 1.0; // NK 05-04-09
	if(useskill) skillModify = GetCharPriceMod(&refMyCh, tradeType, true); // NK now use central func 05-04-19
	if(tradeType==PRICE_TYPE_SELL)
	{
/*		// NK 05-04-09 -->
		
		{
			skillModify *= 1.4 - skillDelta*0.019;
			if(CheckOfficersPerk(refMyCh,"AdvancedCommerce"))	{ skillModify -= 0.2; }
			else
			{
				if(CheckOfficersPerk(refMyCh,"BasicCommerce"))	{ skillModify -= 0.1; }
			}
		}
	}
	else
	{
		if(useskill)
		{
			skillModify *= 0.75 + skillDelta*0.019;
			if(CheckOfficersPerk(refMyCh,"AdvancedCommerce"))	skillModify += 0.05;
		}*/
		skillModify *= buyratio;
		// NK <--
	}

	return makeint(makefloat(GetItemPriceTrader(&refTrader, Items[itmIdx].id))*skillModify); // NK itemtrade 05-04-02 return makeint(makefloat(itmprice)*skillModify);
}

// KK -->
void ChangeSelectedScroll()
{
	DeleteAttribute(&GameInterface,"itemslist");
	FillSelectedScroll();
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"ITEMSLIST",-1);
	SetCurrentNode("ITEMSLIST");
	SetSelectable("I_WEAPON", curItemGroup == GUN_ITEM_TYPE);
	SetSelectable("I_POTION", curItemGroup == POTION_ITEM_TYPE);
	SetSelectable("I_ITEMS", curItemGroup == OBJECT_ITEM_TYPE);
	SetSelectable("I_MAP", curItemGroup == MAP_ITEM_TYPE);
}
// <-- KK

void FillSelectedScroll()
{
	int idx,i,j;
	string attributeName;
	string itemName;

	aref arImgGrp;
	makearef(arImgGrp,GameInterface.itemslist.ImagesGroup);
	arImgGrp.t0 = "ICONS";
	arImgGrp.t1 = "usedflag";
	// KK -->
	if (!LAi_IsCapturedLocation) {
		switch (LanguageGetLanguage()) {
			case "Polish":
				arImgGrp.t2 = "TRADE_STATE_POL";
			break;
			// default:
			arImgGrp.t2 = "TRADE_STATE";
		}
	}
	// <-- KK
	FillImagesGroupForItems(arImgGrp);
	GameInterface.itemslist.NotUsed = 5;
	GameInterface.itemslist.BadTex1 = GetItemPictureTexture("itemslist.ImagesGroup","");
	GameInterface.itemslist.BadPic1 = GetItemPictureName("");

	idx = 0;
	aref curItem,arItem;
	aref rootItems;
	object tempitemnames; // NK 05-04-05

	string curItemID = OBJECT_ITEM_TYPE;
	makearef(rootItems,refMyCh.Items);
	for(i=0; i<GetAttributesNum(rootItems); i++)
	{
		attributeName = "pic" + (idx+1);
		curItem = GetAttributeN(rootItems,i);
		itemName = GetAttributeName(curItem);
		if(!TraderUseItem(&refTrader, itemName)) continue; // NK itemtrade 05-04-02 skip qty <=0 items.
		if (!IsTradeItem(itemname)) continue; // KK
		if(Items_FindItem(GetAttributeName(curItem),&arItem)>=0)
		{
			if(CheckAttribute(arItem,"groupID"))
			{
				curItemID = OBJECT_ITEM_TYPE;
				if(arItem.groupID==GUN_ITEM_TYPE || arItem.groupID==BLADE_ITEM_TYPE || arItem.groupID==ARMOR_ITEM_TYPE || arItem.groupID==AMMUNITION_ITEM_TYPE) { curItemID = GUN_ITEM_TYPE; }
				if(arItem.groupID==POTION_ITEM_TYPE) curItemID = POTION_ITEM_TYPE;
				if (arItem.groupID == MAP_ITEM_TYPE) curItemID = MAP_ITEM_TYPE; // KK
				if(curItemID==curItemGroup)
				{
					attributeName = "pic" + (idx+1);
					GameInterface.itemslist.(attributeName).img1 = GetItemPictureName(arItem.id);
					GameInterface.itemslist.(attributeName).tex1 = GetItemPictureTexture("itemslist.ImagesGroup",arItem.id);
					if(j>1)	GameInterface.itemslist.(attributeName).str1 = "#"+j;
					GameInterface.itemslist.(attributeName).itemID = arItem.id;
					if( IsEquipCharacterByItem(refMyCh,itemName) )
					{
						GameInterface.itemslist.(attributeName).img2 = "usedflag";
						GameInterface.itemslist.(attributeName).tex2 = 1;
					}
					GameInterface.itemslist.(attributeName).img3 = GetItemStatus(itemName, true);
					GameInterface.itemslist.(attributeName).tex3 = 2;
					GameInterface.itemslist.(attributeName).img4 = GetItemStatus(itemName, false);
					GameInterface.itemslist.(attributeName).tex4 = 2;
					idx++;
				}
				else
				{
					attributeName = "pic" + (idx+1);
					GameInterface.itemslist.(attributeName).img1 = GameInterface.itemslist.BadPic1;
					GameInterface.itemslist.(attributeName).tex1 = GameInterface.itemslist.BadTex1;
				}
			}
			else
			{
				if(curItemID==curItemGroup)
				{
					attributeName = "pic" + (idx+1);
					GameInterface.itemslist.(attributeName).img1 = GetItemPictureName(arItem.id);
					GameInterface.itemslist.(attributeName).tex1 = GetItemPictureTexture("itemslist.ImagesGroup",arItem.id);
					if(j>1)	GameInterface.itemslist.(attributeName).str1 = "#"+j;
					GameInterface.itemslist.(attributeName).itemID = arItem.id;
					GameInterface.itemslist.(attributeName).img3 = GetItemStatus(itemName, true);
					GameInterface.itemslist.(attributeName).tex3 = 2;
					GameInterface.itemslist.(attributeName).img4 = GetItemStatus(itemName, false);
					GameInterface.itemslist.(attributeName).tex4 = 2;
					idx++;
				}
				else
				{
					attributeName = "pic" + (idx+1);
					GameInterface.itemslist.(attributeName).img1 = GameInterface.itemslist.BadPic1;
					GameInterface.itemslist.(attributeName).tex1 = GameInterface.itemslist.BadTex1;
				}
			}
		}
		else
		{
			attributeName = "pic" + (idx+1);
			GameInterface.itemslist.(attributeName).img1 = GameInterface.itemslist.BadPic1;
			GameInterface.itemslist.(attributeName).tex1 = GameInterface.itemslist.BadTex1;
		}
		tempitemnames.(itemName) = true;
	}

	makearef(rootItems,refTrader.Items);
	for(i=0; i<GetAttributesNum(rootItems); i++)
	{
		attributeName = "pic" + (idx+1);
		curItem = GetAttributeN(rootItems,i);
		itemName = GetAttributeName(curItem);
		if(!TraderSellItem(&refTrader, itemName)) continue; // NK itemtrade 05-04-02 skip qty <=0 items.
		if( CheckAttribute(tempitemnames,itemName) ) continue; // NK modded 05-04-05
		j = GetCharacterItem(refTrader,itemName);
		if(Items_FindItem(GetAttributeName(curItem),&arItem)>=0)
		{
			if(CheckAttribute(arItem,"groupID"))
			{
				curItemID = OBJECT_ITEM_TYPE;
				if(arItem.groupID==GUN_ITEM_TYPE || arItem.groupID==BLADE_ITEM_TYPE || arItem.groupID==ARMOR_ITEM_TYPE || arItem.groupID==AMMUNITION_ITEM_TYPE) { curItemID = GUN_ITEM_TYPE; }
				if(arItem.groupID==POTION_ITEM_TYPE) curItemID = POTION_ITEM_TYPE;
				if (arItem.groupID == MAP_ITEM_TYPE) curItemID = MAP_ITEM_TYPE; // KK
				if(curItemID==curItemGroup)
				{
					if(curItemID==GUN_ITEM_TYPE && arItem.id=="bladeX4") continue;//MAXIMUS
					attributeName = "pic" + (idx+1);
					GameInterface.itemslist.(attributeName).img1 = GetItemPictureName(arItem.id);
					GameInterface.itemslist.(attributeName).tex1 = GetItemPictureTexture("itemslist.ImagesGroup",arItem.id);
					if(j>1)	GameInterface.itemslist.(attributeName).str1 = "#"+j;
					GameInterface.itemslist.(attributeName).itemID = arItem.id;
					GameInterface.itemslist.(attributeName).img3 = GetItemStatus(itemName, true);
					GameInterface.itemslist.(attributeName).tex3 = 2;
					GameInterface.itemslist.(attributeName).img4 = GetItemStatus(itemName, false);
					GameInterface.itemslist.(attributeName).tex4 = 2;
					idx++;
				}
				else
				{
					attributeName = "pic" + (idx+1);
					GameInterface.itemslist.(attributeName).img1 = GameInterface.itemslist.BadPic1;
					GameInterface.itemslist.(attributeName).tex1 = GameInterface.itemslist.BadTex1;
				}
			}
			else
			{
				if(curItemID==curItemGroup)
				{
					attributeName = "pic" + (idx+1);
					GameInterface.itemslist.(attributeName).img1 = GetItemPictureName(arItem.id);
					GameInterface.itemslist.(attributeName).tex1 = GetItemPictureTexture("itemslist.ImagesGroup",arItem.id);
					if(j>1)	GameInterface.itemslist.(attributeName).str1 = "#"+j;
					GameInterface.itemslist.(attributeName).itemID = arItem.id;
					if( IsEquipCharacterByItem(refMyCh,itemName) )
					{
						GameInterface.itemslist.(attributeName).img2 = "usedflag";
						GameInterface.itemslist.(attributeName).tex2 = 1;
					}
					GameInterface.itemslist.(attributeName).img3 = GetItemStatus(itemName, true);
					GameInterface.itemslist.(attributeName).tex3 = 2;
					GameInterface.itemslist.(attributeName).img4 = GetItemStatus(itemName, false);
					GameInterface.itemslist.(attributeName).tex4 = 2;
					idx++;
				}
				else
				{
					attributeName = "pic" + (idx+1);
					GameInterface.itemslist.(attributeName).img1 = GameInterface.itemslist.BadPic1;
					GameInterface.itemslist.(attributeName).tex1 = GameInterface.itemslist.BadTex1;
				}
			}
		}
		else
		{
			attributeName = "pic" + (idx+1);
			GameInterface.itemslist.(attributeName).img1 = GameInterface.itemslist.BadPic1;
			GameInterface.itemslist.(attributeName).tex1 = GameInterface.itemslist.BadTex1;
		}
	}

	GameInterface.itemslist.ListSize = idx-1;
	GameInterface.itemslist.current = 0;
	nCurScrollNum = -1;
}

void ProcessCancelExit()
{
	for(int s = 0; s < 10; s++) GetCharacterSkill(GetMainCharacter(), GetSkillName(s)); // NK/GZ items mod to force refresh of modifiers.
	DeleteAttribute(GetMainCharacter(), "skiprecalcskill");
	// TIH --> figure out how many different types of items were traded Aug24'06
	int numTrades = 0;
	if (CheckAttribute(gTradetracker, "tradeitems")) {
		aref tmp; makearef(tmp,gTradetracker.tradeitems); numTrades = GetAttributesNum(tmp);
	}
	if (numTrades <= 0) { numTrades = 1; }
	if (CheckAttribute(refTrader, "itemtrade.numtimes")) { refTrader.itemtrade.numtimes = sti(refTrader.itemtrade.numtimes) + numTrades; }
	else { refTrader.itemtrade.numtimes = numTrades; }
	trace("IT traded with trader "+ refTrader.itemtrade.numtimes +" times");
	DeleteAttribute(&gTradetracker, "tradeitems"); // TIH clear this global
	//if(CheckAttribute(refTrader, "itemtrade.numtimes")) { refTrader.itemtrade.numtimes = sti(refTrader.itemtrade.numtimes) + anytrades; }
	//else { refTrader.itemtrade.numtimes = anytrades; } // NK so we know we traded.
	// TIH <--
	DelEventHandler("InterfaceBreak","ProcessCancelExit");
    DelEventHandler("frame","ProcessFrame");
    DelEventHandler("exitCancel","ProcessCancelExit");
	DelEventHandler("ievnt_command","ProcessCommandExecute");

	interfaceResultCommand = RC_INTERFACE_ITEMSTRADE_EXIT;
	//LanguageCloseFile(idLngFile);
    EndCancelInterface(true);
}

void SetShowMode(int st)
{
	if(st==showType) return;

	switch(showType)
	{
		case SHOW_MAIN:
			DisableString("ItemsName");
			DisableString("BuyPrice");
			DisableString("SellPrice");
			SetNodeUsing("BUY_BUTTON",false);
			SetNodeUsing("INFO_BUTTON",false);
			SetNodeUsing("SELL_BUTTON",false);
			SetNodeUsing("MAIN_IMAGES",false);
			FreezeScreen(false);
		break;
		case SHOW_BUY:
			DisableString("PriceSum");
			DisableString("TradeQuantity");
			SetNodeUsing("BUYSELL_IMAGES",false);
			SetNodeUsing("OK_BUTTON",false);
			SetNodeUsing("CANCEL_BUTTON",false);
			SetNodeUsing("TRADEQUANTITY",false);
			CreateImage("TradeType","","",0,0,0,0);
		break;
		case SHOW_SELL:
			DisableString("PriceSum");
			DisableString("TradeQuantity");
			SetNodeUsing("BUYSELL_IMAGES",false);
			SetNodeUsing("OK_BUTTON",false);
			SetNodeUsing("CANCEL_BUTTON",false);
			SetNodeUsing("TRADEQUANTITY",false);
			CreateImage("TradeType","","",0,0,0,0);
		break;
		case SHOW_DESCRIBE:
			SetNodeUsing("TEXTWINDOW",false);
			SendMessage(&GameInterface, "lsls", MSG_INTERFACE_MSG_TO_NODE, "INFO_BUTTON", 0, XI_ConvertString("INFO")); // KK
		break;
	}

	showType = st;

	switch(showType)
	{
		case SHOW_MAIN:
			EnableString("ItemsName");
			EnableString("BuyPrice");
			EnableString("SellPrice");
			SetNodeUsing("BUY_BUTTON",true);
			SetNodeUsing("INFO_BUTTON",true);
			SetNodeUsing("SELL_BUTTON",true);
			SetNodeUsing("MAIN_IMAGES",true);
		break;
		case SHOW_BUY:
			EnableString("PriceSum");
			EnableString("TradeQuantity");
			SetNodeUsing("BUYSELL_IMAGES",!LAi_IsCapturedLocation); // KK
			SetNodeUsing("OK_BUTTON",true);
			SetNodeUsing("CANCEL_BUTTON",true);
			SetNodeUsing("TRADEQUANTITY",true);
// KK -->
			if (!LAi_IsCapturedLocation) {
				switch (LanguageGetLanguage()) {
					case "Polish":
						CreateImage("TradeType","BUY_SELL_POL","buy",126,242,190,306);
					break;
					// default:
					CreateImage("TradeType","BUY_SELL","buy",126,242,190,306);
				}
			}
			FreezeScreen(true);
// <-- KK
		break;
		case SHOW_SELL:
			EnableString("PriceSum");
			EnableString("TradeQuantity");
			SetNodeUsing("BUYSELL_IMAGES",!LAi_IsCapturedLocation);
			SetNodeUsing("OK_BUTTON",true);
			SetNodeUsing("CANCEL_BUTTON",true);
			SetNodeUsing("TRADEQUANTITY",true);
// KK -->
			if (!LAi_IsCapturedLocation) {
				switch (LanguageGetLanguage()) {
					case "Polish":
						CreateImage("TradeType","BUY_SELL_POL","sell",450,242,514,306);
					break;
					// default:
					CreateImage("TradeType","BUY_SELL","sell",450,242,514,306);
				}
			}
			FreezeScreen(true);
// <-- KK
		break;
		case SHOW_DESCRIBE:
			SetNodeUsing("TEXTWINDOW",true);
// KK -->
			SendMessage(&GameInterface, "lsls", MSG_INTERFACE_MSG_TO_NODE, "INFO_BUTTON", 0, XI_ConvertString("OK"));
			SetNodeUsing("INFO_BUTTON", true);
			SetCurrentNode("INFO_BUTTON");
			FreezeScreen(true);
// <-- KK
		break;
	}

}

string GetItemsID(int scrollIdx)
{
	if(scrollIdx<0) return "";
	string attributeName = "pic" + (scrollIdx+1);
	if( CheckAttribute(&GameInterface,"itemslist."+attributeName+".itemID") )
	{
		return GameInterface.itemslist.(attributeName).itemID;
	}
	return "";
}

void ProcessCommandExecute()
{
	string comName = GetEventData();
	string nodName = GetEventData();

	switch(nodName)
	{
		case "ITEMSLIST":
			if(comName=="deactivate")
			{
				PostEvent("exitCancel",1);
			}
			if(comName=="downstep" || comName=="speeddown")
			{
				if(GetSelectable("BUY_BUTTON"))
				{
					SetCurrentNode("BUY_BUTTON");
					return;
				}
				if(GetSelectable("SELL_BUTTON"))
				{
					SetCurrentNode("SELL_BUTTON");
					return;
				}
			}
			if(comName=="upstep")
			{
				SetCurrentNode("SELL_LOOT_BUTTON");
			}
		break;

		case "INFO_BUTTON":
			if(comName=="activate" || comName=="click")
			{
				if (showType == SHOW_DESCRIBE) {
					SetShowMode(SHOW_MAIN);
				} else {
					SetShowMode(SHOW_DESCRIBE);
					SetCurrentNode("TEXTWINDOW");
				}
				RefreshAllStrings();
				return;
			}
			if(comName=="leftstep")
			{
				if( GetSelectable("BUY_BUTTON") )
				{
					SetCurrentNode("BUY_BUTTON");
				}
				return;
			}
			if(comName=="rightstep")
			{
				if( GetSelectable("SELL_BUTTON") )
				{
					SetCurrentNode("SELL_BUTTON");
				}
				return;
			}
		break;

		case "BUY_BUTTON":
			if(comName=="activate" || comName=="click")
			{
				SetShowMode(SHOW_BUY);
				SetStartTradeQuantity();
				RefreshAllStrings();
				SetCurrentNode("TRADEQUANTITY");
			}
		break;

		case "SELL_BUTTON":
			if(comName=="activate" || comName=="click")
			{
				SetShowMode(SHOW_SELL);
				SetStartTradeQuantity();
				RefreshAllStrings();
				SetCurrentNode("TRADEQUANTITY");
			}
		break;

		case "TRADEQUANTITY":
			if(comName=="activate")
			{
				DoTradeOperation();
				SetShowMode(SHOW_MAIN);
				SetCurrentNode("ITEMSLIST");
				RefreshAllStrings();
			}
			switch(comName)
			{
			case "rightstep":	AddTradeQuantity(1); break;
			case "speedright":	AddTradeQuantity(10); break;
			case "leftstep":	AddTradeQuantity(-1); break;
			case "speedleft":	AddTradeQuantity(-10); break;
			}
		break;

		case "OK_BUTTON":
			if(comName=="activate" || comName=="click")
			{
				DoTradeOperation();
				SetShowMode(SHOW_MAIN);
				SetCurrentNode("ITEMSLIST");
				RefreshAllStrings();
			}
		break;

		case "CANCEL_BUTTON":
			if(comName=="activate" || comName=="click")
			{
				SetShowMode(SHOW_MAIN);
				SetCurrentNode("ITEMSLIST");
			}
		break;

		case "TEXTWINDOW":
			if(comName=="downstep")
			{
				if(!bEnableDown)
				{
					SendMessage(&GameInterface,"lslll",MSG_INTERFACE_MSG_TO_NODE,"TEXTWINDOW",-1, 0,ACTION_UPSTEP);
				} else {
					bEnableDown = SendMessage(&GameInterface,"lsll",MSG_INTERFACE_MSG_TO_NODE,"TEXTWINDOW", 6, 0);
				}
			}
			if(comName=="upstep")
			{
				bEnableDown = SendMessage(&GameInterface,"lsll",MSG_INTERFACE_MSG_TO_NODE,"TEXTWINDOW", 6, 0);
			}
		break;

		case "SELL_LOOT_BUTTON":
			if(comName=="activate" || comName=="click")
			{
				SellAllLoot();
				SetShowMode(SHOW_MAIN);
				SetCurrentNode("ITEMSLIST");
				RefreshAllStrings();
			}
			if(comName=="downstep")
			{
				SetCurrentNode("ITEMSLIST");
			}
			if(comName=="upstep")
			{
				SetCurrentNode("INFO_BUTTON");
			}
		break;

// KK -->
		case "I_WEAPON":
			if (comName == "activate" || comName == "click") {
				curItemGroup = GUN_ITEM_TYPE;
				ChangeSelectedScroll();
				RefreshAllStrings();
			}
		break;

		case "I_POTION":
			if (comName == "activate" || comName == "click") {
				curItemGroup = POTION_ITEM_TYPE;
				ChangeSelectedScroll();
				RefreshAllStrings();
			}
		break;

		case "I_ITEMS":
			if (comName == "activate" || comName == "click") {
				curItemGroup = OBJECT_ITEM_TYPE;
				ChangeSelectedScroll();
				RefreshAllStrings();
			}
		break;

		case "I_MAP":
			if (comName == "activate" || comName == "click") {
				curItemGroup = MAP_ITEM_TYPE;
				ChangeSelectedScroll();
				RefreshAllStrings();
			}
		break;
// <-- KK
	}
}

void DoTradeOperation()
{
	if(nTradeQuantity<=0) return;
	string itmName = GetItemsID(nCurScrollNum);
	aref itemref; makearef(itemref,Items[GetItemIndex(itmName)]);// TIH added for ease of use Jul15'06
	ref mchref = GetMainCharacter();

	int TQ;		//JRH
	TQ = nTradeQuantity;
	int gp1;
	int pb;
	int pb1;
	int pg;
	int pg1;
	int pg2;
	int mb;
	int mb1;
	int am;
	int am1;
	int p;
	int s;

	if(showType==SHOW_SELL)
	{
		// TIH --> unequip if selling it off Jul15'06
		bool soldEquip = false;
		// first check if they are selling ALL of the item
		if ( nTradeQuantity >= GetCharacterItem(refMyCh,itmName) && CheckAttribute(itemref,"groupID") ) {
			if ( IsEquipCharacterByItem(refMyCh,itmName) ) {
				RemoveCharacterEquip(refMyCh, itemref.groupID);
				soldEquip = true;
			}
		}
		// TIH <-- allow selling equipped items
		TakeNItems(refTrader,itmName,nTradeQuantity);
		TakeNItems(refMyCh,itmName,-nTradeQuantity);
		AddMoneyToCharacter(mchref,nTradeQuantity*sellPrice);
		if(AUTO_SKILL_SYSTEM)
		{
			AddPartyExpChar(mchref, "Commerce", makeint(nTradeQuantity*sellPrice/10));
			AddCharacterExpNSChar(&refTrader, "Commerce", makeint(nTradeQuantity*sellPrice/10));
		}
		AddMoneyToCharacter(&refTrader,-nTradeQuantity*sellPrice); // NK itemtrade 05-04-02
		// TIH --> auto-requip if sold off equipped items Jul15'06
		// do after TakeNItems, because of the "Find" function needs the prior one removed
		if ( soldEquip && CheckAttribute(itemref,"groupID") ) {
			// if sold off their blade, equip last best item or their fists
			if ( itemref.groupID == BLADE_ITEM_TYPE ) {
				EquipCharacterByItem(refMyCh, FindCharacterItemByGroup(refMyCh, BLADE_ITEM_TYPE) );
				//EquipCharacterByItem(refMyCh, "bladeX4");
			}
			// if sold off their gun, equip last best gun allowed in inventory
			if ( itemref.groupID == GUN_ITEM_TYPE ) {
				EquipCharacterByItem(refMyCh, FindCharacterItemByGroup(refMyCh, GUN_ITEM_TYPE) );
			}
			soldEquip = false;// reset just in case
		}
		// TIH <-- allow selling equipped items
	}

	if(showType==SHOW_BUY)
	{
		// TIH --> bracket fixing Jul15'06
		//JRH -->
		if(itmName=="gunpowder" || itmName=="pistolbullets" || itmName=="pistolgrapes" || itmName=="musketbullets")
		{
			if(itmName=="gunpowder")
			{
				if(CheckAttribute(mchref,"equip."+FLASK_ITEM_TYPE) )
				{
					p = 12;
				}
				else
				{
					p = 6;
				}

				int gp;
				gp = GetCharacterItem(mchref, "gunpowder");
				TQ = nTradeQuantity;
	
				if(gp >= p)
				{
					TakeNItems(mchref,"gunpowder", -100);
					TakeNItems(mchref,"gunpowder", p);
					return;	//just in case
				}
				else
				{
					if(TQ + gp >= p) gp1 = p - gp;
					if(TQ + gp < p) gp1 = TQ;
					TakeNItems(refTrader, "gunpowder", -gp1);
					TakeNItems(refMyCh, "gunpowder", gp1);
					AddMoneyToCharacter(mchref,-gp1*buyPrice);
					if(AUTO_SKILL_SYSTEM)
					{
						AddPartyExpChar(mchref, "Commerce", makeint(gp1*buyPrice/10));
						AddCharacterExpNSChar(&refTrader, "Commerce", makeint(gp1*buyPrice/10));
					}
					AddMoneyToCharacter(&refTrader,gp1*buyPrice);	
				}
			}

			if(itmName=="pistolbullets" || itmName=="pistolgrapes" || itmName=="musketbullets") 
			{
				if(CheckAttribute(mchref,"equip."+POUCH_ITEM_TYPE) )
				{
					s = 12;
				}
				else
				{
					s = 6;
				}

				// JRH fix oct 06 -->
				pb = GetCharacterItem(mchref, "pistolbullets");
				pg = GetCharacterItem(mchref, "pistolgrapes");
				mb = GetCharacterItem(mchref, "musketbullets");
				am = pb + pg + 2*mb;

				if(am < s)
				{
					if(itmName=="pistolbullets" || itmName=="pistolgrapes")
					{
						if(TQ + am > s) am1 = s - am;
						if(TQ + am <= s) am1 = TQ;
						TakeNItems(refTrader,itmName,-am1);
						TakeNItems(refMyCh,itmName,am1);
						AddMoneyToCharacter(mchref,-am1*buyPrice);
						if(AUTO_SKILL_SYSTEM)
						{
							AddPartyExpChar(mchref, "Commerce", makeint(am1*buyPrice/10));
							AddCharacterExpNSChar(&refTrader, "Commerce", makeint(am1*buyPrice/10));
						}
						AddMoneyToCharacter(&refTrader,am1*buyPrice);
					}
					if(itmName=="musketbullets") 
					{
						if(TQ > (s - am)/2) am1 = (s - am)/2;
						if(TQ <= (s - am)/2) am1 = TQ;
						TakeNItems(refTrader,itmName,-am1);
						TakeNItems(refMyCh,itmName,am1);
						AddMoneyToCharacter(mchref,-am1*buyPrice);
						if(AUTO_SKILL_SYSTEM)
						{
							AddPartyExpChar(mchref, "Commerce", makeint(am1*buyPrice/10));
							AddCharacterExpNSChar(&refTrader, "Commerce", makeint(am1*buyPrice/10));
						}
						AddMoneyToCharacter(&refTrader,am1*buyPrice);
					}
				}
				// <-- JRH
			}
		}
		else
		{
			TakeNItems(refTrader,itmName,-nTradeQuantity);
			TakeNItems(refMyCh,itmName,nTradeQuantity);
			AddMoneyToCharacter(mchref,-nTradeQuantity*buyPrice);
			if(AUTO_SKILL_SYSTEM)
			{
				AddPartyExpChar(mchref, "Commerce", makeint(nTradeQuantity*buyPrice/10));
				AddCharacterExpNSChar(&refTrader, "Commerce", makeint(nTradeQuantity*buyPrice/10));
			}
			AddMoneyToCharacter(&refTrader,nTradeQuantity*buyPrice); // NK itemtrade 05-04-02
			// TIH --> auto-equip better spyglass if bought another Jul15'06
			if ( nTradeQuantity > 0 && CheckAttribute(itemref,"groupID") ) {
				if ( itemref.groupID == SPYGLASS_ITEM_TYPE ) {
					if( sti(itemref.quality) > sti(Items[GetItemIndex(GetCharacterEquipByGroup(refMyCh,SPYGLASS_ITEM_TYPE))].quality) ) {
						RemoveCharacterEquip(refMyCh, SPYGLASS_ITEM_TYPE);// remove equip of old one
						EquipCharacterByItem(refMyCh, itmName);// equip the new better one
					}
				}
			}
			// TIH <-- auto-equip better spyglass
		}
		// TIH <-- bracket fixing
	}

	// TIH --> count types of items traded to better level a merchant up Aug24'06
	// (only per type, not every single sold quantity)
	if (!CheckAttribute(gTradetracker,"tradeitems")) { gTradetracker.tradeitems = ""; }
	gTradetracker.tradeitems.(itmName) = nTradeQuantity;
	//anytrades = true; // NK 05-04-07
	// TIH <--

	nTradeQuantity = 0;
	if(sti(mchref.money)<0)
	{
		mchref.money = 0;
	}

	UpdateScroll();	//TIH update all items, as the equip flag make have changed
}

void AddTradeQuantity(int incrVal)
{
	ref mchref = GetMainCharacter();
	nTradeQuantity = nTradeQuantity + incrVal;
	if(nTradeQuantity<0)
	{
		nTradeQuantity = 0;
		RefreshAllStrings();
		return;
	}

	if(LAi_IsCapturedLocation == false && buyPrice == 0) // KK
	{
		nTradeQuantity = 0;
	}

	string itmName = GetItemsID(nCurScrollNum);
	if(showType==SHOW_SELL)
	{
		int realQ = GetCharacterItem(refMyCh,itmName);
		if( realQ > 0 && IsEquipCharacterByItem(refMyCh,itmName) ){
			// TIH --> allow the selling of equipped items Jul15'06
			// EXCEPT the current equipped spyglass... it is a required special
			if ( Items[GetItemIndex(itmName)].groupID == SPYGLASS_ITEM_TYPE ) {
				realQ--;
			}
			// TIH <-- allow sell equips
		}
		if(realQ<nTradeQuantity)
		{
			nTradeQuantity = realQ;
		}
		// NK itemtrade 05-04-02 -->
		if(sellprice*nTradeQuantity > GetCharacterMoney(&refTrader))
		{
			nTradeQuantity = GetCharacterMoney(&refTrader) / sellPrice;
		}
		// NK <--
	}

	if(showType==SHOW_BUY)
	{
		if(GetCharacterItem(refTrader,itmName)<nTradeQuantity)
		{
			nTradeQuantity = GetCharacterItem(refTrader,itmName);
		}

		if(buyPrice*nTradeQuantity > sti(mchref.money))
		{
			nTradeQuantity = sti(mchref.money) / buyPrice;
		}
	}

	RefreshAllStrings();
}

void SetItemDescribe(aref arItm)
{
	string describeStr = "";
	if( CheckAttribute(arItm,"groupID") )
	{
		if(arItm.groupID==GUN_ITEM_TYPE)
		{
			describeStr += GetAssembledString(TranslateString("","weapon gun parameters"), arItm) + TranslateString("","new_string");
		}
		if(arItm.groupID==BLADE_ITEM_TYPE)
		{
			describeStr += GetAssembledString(
				TranslateString("","weapon blade parameters"), arItm) + TranslateString("","new_string");
		}
		// GreatZen -->
		if(arItm.groupID==ARMOR_ITEM_TYPE)
		{
			describeStr = GetAssembledString(TranslateString("","weapon armor parameters"),arItm)+ TranslateString("","new_string");
		}
		// GreatZen <--
	}
	if( CheckAttribute(arItm,"describe") )
	{
		describeStr += GetAssembledString(TranslateString("",arItm.describe), arItm) + GetItemBonuses(arItm.id); // NK // KK
	}
	SetFormatedText("TEXTWINDOW",describeStr);

	bEnableDown = SendMessage(&GameInterface,"lsll",MSG_INTERFACE_MSG_TO_NODE,"TEXTWINDOW", 6, 0);
}

void SetStartTradeQuantity()
{
	if( showType == SHOW_SELL )
	{
		nTradeQuantity = GetCharacterItem(refMyCh,GetItemsID(nCurScrollNum));
		if( nTradeQuantity>0 && IsEquipCharacterByItem(refMyCh, GetItemsID(nCurScrollNum)) ) {
			// TIH --> allow the selling of equipped items Jul15'06
			// EXCEPT the current equipped spyglass... it is a required special
			if ( Items[ GetItemIndex( GetItemsID(nCurScrollNum) ) ].groupID == SPYGLASS_ITEM_TYPE ) {
				nTradeQuantity--;
			}
			// TIH <-- allow sell equips
		}
		if(sellprice*nTradeQuantity > GetCharacterMoney(&refTrader))
		{
			nTradeQuantity = GetCharacterMoney(&refTrader) / sellPrice;
		}
		return;
	}

	nTradeQuantity = 0;
	AddTradeQuantity(1);
}

string GetItemStatus(string itemName, bool bIsForMy)
{
	if (bIsForMy) {
// KK -->
		int quantity = GetCharacterItem(refMyCh, itemName);
		int isEquiped = IsEquipCharacterByItem(refMyCh, itemName);
		if (isEquiped == true && Items[GetItemIndex(itemName)].groupID == SPYGLASS_ITEM_TYPE && quantity == 1) return "";
		if (isEquiped == true && quantity > 0) return "sell";
		if (isEquiped == false && quantity > 0) return "sell";
// <-- KK
		return "";
	} else {
		if (GetCharacterItem(refTrader, itemName) > 0) return "buy";
		return "";
	}
}

// START MOD Code by Stone-D : 29/07/2003
// LDH - June 27, 2006 - Sell all jewelry, indian trinkets and minerals that do not alter skills.  Won't sell quest items.
// LDH - Can sell lower quality blades as well.
// LDH - Also fixed a bug that missed some items each pass causing player to have to click Sell All Loot more than once.
void SellAllLoot()
{
    int i,j,s,t,q;
	int itmIdx;
	string itemName;
	aref curItem,arItem;
	aref refitm;
	aref rootItems;
	makearef(rootItems,refMyCh.Items);
	ref mchref = GetMainCharacter();
	// LDH -->
	int CharLevel = sti(mchref.rank);
	int JunkBladeMaxDamage;
	string itemShortName;
	// LDH <--
	bool SellMe;
	bool Done;

	j=0;
	q=0;
	t=0;
	Done=false;

	while (!Done)
	{
	Done = true;	// LDH moved from below
	for(i=0; i<GetAttributesNum(rootItems); i++)
    {
		curItem = GetAttributeN(rootItems,i);
		itemName = GetAttributeName(curItem);
		SellMe = false;
//		Done = true;  // LDH moved above, wasn't getting all items in one pass
		if(!TraderUseItem(&refTrader, itemName)) continue; // NK itemtrade 05-04-02 skip qty <=0 items.

// LDH -->
		itemShortName = strleft(itemName,5);	// LDH - shorten name

		if (itemShortName == "jewel")
		{
			SellMe = true;
		}
		if (itemShortName == "india")
		{
			SellMe = true;
		}
		if(itemShortName == "miner")
		{
			SellMe = true;
		}

		itmIdx = Items_FindItem(itemName,&refitm);	// Moved from below.  If function not called here the following code is broken.

		if (itmIdx >= 0)	// this test is probably unnecessary, but Items_FindItem must be called or rest of code doesn't work.
		{
			if (SELL_JUNK_BLADES && itemShortName == "blade")	// Sell low quality blades.
			{
				JunkBladeMaxDamage = 12;	// below level 5 sell daggers only
				if (CharLevel >= JUNK_BLADE_LEVEL_1) JunkBladeMaxDamage = 20;	// at level 5, start selling blades with max_dmg < 20
				if (CharLevel >= JUNK_BLADE_LEVEL_2) JunkBladeMaxDamage = 25;	// at level 9, start selling blades with max_dmg < 25
				if (CharLevel >= JUNK_BLADE_LEVEL_3) JunkBladeMaxDamage = 30;	// at level 13, start selling blades with max_dmg < 30

				if (sti(refitm.dmg_max) < JunkBladeMaxDamage) SellMe = true;
				if (sti(refitm.piercing) > 40 || sti(refitm.block) > 40) SellMe = false;	// don't sell if pierce/block over 40
			}
			if (CheckAttribute(refitm,"skill"))	// Don't sell items that alter skills (includes blades if blades ever have skill mods added)
			{
				SellMe = false;
			}
		}
// LDH <--

		if(!SellMe) { continue; }
		itmIdx = Items_FindItem(itemName,&refitm);	// LDH moved above, left here just in case
		j = GetCharacterItem(refMyCh,itemName);
		if (ItemUsedByCharacter(itmIdx)) j = j - 1; // LDH - Don't sell equipped item as loot!
		if (j < 1) continue; // LDH
		s = GetTradeItemPrice(itmIdx,PRICE_TYPE_SELL);
		if(s<=0) { continue; } // Checks for quest items
		Done = false;
		q = q+j;
		t = t+(s*j);	// LDH fix, wasn't adding full amount for multiple items
		gTradetracker.tradeitems.(itemName) = q;	// LDH 15Oct06 - add trade numbers for merchant levelup for items sold by selling loot
		Log_SetStringToLog(TranslateString("",refitm.name) + ": " + j + " * " + s + " = " + (j*s));
		TakeNItems(refTrader,itemName,j);
		TakeNItems(refMyCh,itemName,-j);
		AddMoneyToCharacter(mchref,j*s);
		if(AUTO_SKILL_SYSTEM)
		{
			AddPartyExpChar(mchref, "Commerce", makeint(j*s/10));
			AddCharacterExpNSChar(&refTrader, "Commerce", makeint(j*s/10));
		}
		AddMoneyToCharacter(&refTrader,-j*s); // NK itemtrade 05-04-02
    }
	}
	if ( q > 0 ) Log_SetStringToLog(XI_ConvertString("Sold") + " " + q + " " + XI_ConvertString("items for") + " " + t);// TIH display fix, dont show if nothing sold
	UpdateScroll();
}

void UpdateScroll()
{
	int numItems=GameInterface.itemslist.ListSize;
	
	for (int i = 0; i <= numItems; i++) // KK
	{
		string attributeName = "pic"+(i+1);
		string itemName = GetItemsID(i);
		GameInterface.itemslist.(attributeName).str1 = "#"+GetCharacterItem(refTrader,itemName);
		// TIH --> change equip status Jul15'06
		if ( IsEquipCharacterByItem(refMyCh,itemName) ) {
			GameInterface.itemslist.(attributeName).img2 = "usedflag";
			GameInterface.itemslist.(attributeName).tex2 = 1;
		} else {
			DeleteAttribute(&GameInterface,"itemslist."+ attributeName +".img2");
			DeleteAttribute(&GameInterface,"itemslist."+ attributeName +".tex2");
		}
		// TIH <--
		GameInterface.itemslist.(attributeName).img3 = GetItemStatus(itemName, true);
		GameInterface.itemslist.(attributeName).img4 = GetItemStatus(itemName, false);
		SendMessage(&GameInterface,"lsl", MSG_INTERFACE_SCROLL_CHANGE, "ITEMSLIST", i);// TIH done below
	}
	//SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"ITEMSLIST",-1);// TIH refresh the list once, instead on each item
}

// END MOD Code by Stone-D : 29/07/2003

// KK -->
void FreezeScreen(bool bSet)
{
	bSet = !bSet;
	SetSelectable("ITEMSLIST", bSet);
	SetNodeUsing("LEFTSCROLLBUTTON", bSet);
	SetNodeUsing("RIGHTSCROLLBUTTON", bSet);
	SetSelectable("I_WEAPON", bset == true && curItemGroup != GUN_ITEM_TYPE);
	SetSelectable("I_POTION", bset == true && curItemGroup != POTION_ITEM_TYPE);
	SetSelectable("I_ITEMS", bset == true && curItemGroup != OBJECT_ITEM_TYPE);
	SetSelectable("I_MAP", bset == true && curItemGroup != MAP_ITEM_TYPE);
}
// <-- KK
