#define ITEM_MODE_SWAP		0
#define ITEM_MODE_TAKE		1
#define ITEM_MODE_NONE		2
//#define ITEM_MODE_INFO		3

aref g_refItems;

int nCurScroll1;
int nCurScroll2;

string oldNodeName="";
int oldItemMode = -1;

bool g_bIBoxUsed;

string oldCurScrollName = "ONE_ITEMSLIST";

string boxName
string boxType;
string curLoc;
bool isItemsBox = false;

int idLngFile;

bool bEnableDown = false;
ref lootedChar;
bool noConfirm = false; // added by MAXIMUS [confirmation dialog will be disabled, if character hasn't any items]

void InitInterface_RS(string iniName,ref itemsRef,string faceID)
{
	ref pch = GetMainCharacter();
	if(bDeadExchange) // KK
	{
		locCameraSleep(true);
		SetTimeScale(0.0);

		EngineLayersOffOn(true);
	}

    // JRH fix oct 06: remove the ghost gp --> 
	if(ENABLE_AMMOMOD == 1) 
	{
		int gp = GetCharacterItem(pch, "gunpowder");
		if(gp == 0)
		{
			TakenItems(pch, "gunpowder", 1); TakenItems(pch, "gunpowder", -1);
		}
	}
    // <-- JRH

	lootedChar = itemsRef;
	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

// changed by MAXIMUS -->
   //JRH -->
	string weaponID = GetCharacterEquipByGroup(pch,GUN_ITEM_TYPE);
	aref weapon;
	Items_FindItem(weaponID, &weapon);

	int s; //shots
	int ShCl = makeint(GetCharacterShipClass(GetMainCharacter()));
    //JRH <--

	// TIH lets just set this here, so its not pasted every other line below 7-7-06
	// this is never changed to true anyway, so I really don't understand the need for it
	isGunpowder = false;

	// TIH --> total house cleaning 7-7-06
	if(faceID=="")
	{
		boxName = pch.boxname;
		boxType = strcut(boxName, 0, strlen(boxName)-3);
		curLoc = Locations[FindLocation(pch.location)].filespath.models;
		int powderCount;
		string whichInterface = "";
		
		// TIH --> treasure quest update Aug31'06
		// if they finally open the box, the location is removed
		if (CheckAttribute(pch,"trlocation") && CheckAttribute(pch,"trlocator") && CheckAttribute(pch,"trlocatorfound"))
		{
			if (pch.location == pch.trlocation && pch.trlocator == pch.trlocatorfound)
			{
//				CloseQuestHeader(("treasure_" + pch.trquestnum)); // close the entry
				CloseQuestHeader("treasure"); // close the entry
				pch.trquestnum = "";		// no more!
				pch.trlocatorfound = "";	// deletes the treasureinfo...
				pch.trlocation = "";		// so that you can find it only once...
				pch.trlocator = "";			// dont close the box without looting!
			}
		}
		// TIH <--
		//changed by MAXIMUS: box-labels weren't right -->
			if(boxName=="WeaponsLocker") { whichInterface = "Weaponslocker"; }
			else
			{
				if(boxType == "gunpowder")
				{
					if(boxName == "gunpowder_1")
					{
						if(LAi_IsBoardingProcess())
						{
							if (HasSubStr(pch.location, "BOARDING_")) // KK
								whichInterface = "GunpowderStorageEnemy";
							else
								whichInterface = "GunpowderBarrelEnemy";
						}
						else
						{
							if(HasSubStr(curLoc,"locations\decks\udec")) { whichInterface = "GunpowderBarrel"; }
							else { whichInterface = "GunpowderStorage"; }
						}
					}
					else
					{
						if(LAi_IsBoardingProcess()) { whichInterface = "GunpowderBarrelEnemy"; }
						else { whichInterface = "GunpowderBarrel"; }
					}
				}
				else
				{
					if(HasSubStr(boxName, "box") && HasSubStr(curLoc, "locations\decks")) { whichInterface = "SeaBox"; }
					else { whichInterface = "ItemsBox"; }
				}
			}
		//changed by MAXIMUS: box-labels weren't right -->

		// do specific actions based on the whichInterface HERE
		switch (whichInterface)
		{
			case "Weaponslocker":
				GameInterface.title = "titleWeaponslocker";
				PlaySound("AMBIENT\JAIL\door_003.wav");// was door_002 which is corrupted in a way
				isGunpowder = false;
				isItemsBox = true;
				break;
			
			case "SeaBox":
				GameInterface.title = "titleSeaBox";
				PlaySound("AMBIENT\JAIL\door_001.wav");
				isGunpowder = false;
				isItemsBox = true;
				break;
			
			case "ItemsBox":
				GameInterface.title = "titleItemsBox";
				PlaySound("AMBIENT\JAIL\door_003.wav");// was door_004 which is corrupted in a way
				isGunpowder = false;
				isItemsBox = true;
				break;
			
			case "GunpowderStorage":
				GameInterface.title = "titleGunpowderStorage";
				isGunpowder = true;
				isItemsBox = false;
				PlaySound("AMBIENT\JAIL\door_003.wav");// was door_002 which is corrupted in a way
				if(ENABLE_AMMOMOD==1) {// TIH --> mod toggle 7-7-06

		/*	// JRH fix oct 06:
			// 	pistol gp is not based on cannon gp amount. Itīs bad enough to forget to get cannon gp.
			// 	And you could say there is always enough gp for some pistols.
			// 	the outcommented code failed sometimes, only returning gp not bullets.
			// 	yes itīs a new amount every time you open it - not a big problem for me.
			// 	Maximus code unchanged for reference

			// MAXIMUS 21.07.2006: random bullets moved to itemLogic.c, if we have some amount of bullets, why should we change it by each open-box procedure?
			// MAXIMUS 21.07.2006: section rewritten, because the WHOLE IDEA for gunpowder was - possibility to use ANY gunpowder
			// MAXIMUS 21.07.2006: and not VIRTUAL (randomly generated) gunpowder, but our REAL gunpowder amount.
					locations[FindLocation(pch.location)].(boxName).items.gunpowder = GetCargoGoods(GetMainCharacter(), GOOD_GUNPOWDER);
					locations[FindLocation(pch.location)].(boxName).open = true;

					if(CheckAttribute(weapon,"shottype") ) 
					{
						string ammoName = "";
						bool bAmmoTaken = false;
						bool bPwdrTaken = false;

						if(CheckCharacterItem(pch,"ammopouch")) s = 12;
						else s = 6;

						int grapesAmount, bulletsAmount, mbulletsAmount;
						grapesAmount = s;
						bulletsAmount = s;
						mbulletsAmount = s/2;
						
						if(weapon.shottype=="pg2" || weapon.shottype=="pg")
						{
							locations[FindLocation(pch.location)].(boxName).items.pistolgrapes = sti(locations[FindLocation(pch.location)].(boxName).items.pistolgrapes) + sti(GetCharacterItem(pch, "pistolgrapes"));
							grapesAmount = locations[FindLocation(pch.location)].(boxName).items.pistolgrapes;
							TakeNItems(pch,"pistolgrapes", - sti(GetCharacterItem(pch, "pistolgrapes")));
						}
						if(weapon.shottype=="pb")
						{
							locations[FindLocation(pch.location)].(boxName).items.pistolbullets = sti(locations[FindLocation(pch.location)].(boxName).items.pistolbullets) + sti(GetCharacterItem(pch, "pistolbullets"));
							bulletsAmount = locations[FindLocation(pch.location)].(boxName).items.pistolbullets;
							TakeNItems(pch,"pistolbullets", - sti(GetCharacterItem(pch, "pistolbullets")));
						}
						if(weapon.shottype=="mb")
						{
							locations[FindLocation(pch.location)].(boxName).items.musketbullets = sti(locations[FindLocation(pch.location)].(boxName).items.musketbullets) + sti(GetCharacterItem(pch, "musketbullets"));
							mbulletsAmount = locations[FindLocation(pch.location)].(boxName).items.musketbullets;
							TakeNItems(pch,"musketbullets", - sti(GetCharacterItem(pch, "musketbullets")));
						}
						SetCharacterGoods(pch, GOOD_GUNPOWDER, sti(GetCargoGoods(pch, GOOD_GUNPOWDER)) + sti(GetCharacterItem(pch, "gunpowder")));
						TakeNItems(pch,"gunpowder", - sti(GetCharacterItem(pch, "gunpowder")));

						if(weapon.shottype == "pg2" || weapon.shottype == "pg")
						{
							ammoName = "pistolgrapes";
							bAmmoTaken = true;
							if(sti(locations[FindLocation(pch.location)].(boxName).items.pistolgrapes)>=s)
							{
								locations[FindLocation(pch.location)].(boxName).items.pistolgrapes = grapesAmount - s;
								TakeNItems(pch,"pistolgrapes", s);
							}
							else
							{
								TakeNItems(pch,"pistolgrapes", sti(locations[FindLocation(pch.location)].(boxName).items.pistolgrapes));
								locations[FindLocation(pch.location)].(boxName).items.pistolgrapes = 0;
							}
						}
						if(weapon.shottype == "pb")
						{
							ammoName = "pistolbullets";
							bAmmoTaken = true;
							if(sti(locations[FindLocation(pch.location)].(boxName).items.pistolbullets)>=s)
							{
								locations[FindLocation(pch.location)].(boxName).items.pistolbullets = bulletsAmount - s;
								TakeNItems(pch,"pistolbullets", s);
							}
							else
							{
								TakeNItems(pch,"pistolbullets", sti(locations[FindLocation(pch.location)].(boxName).items.pistolbullets));
								locations[FindLocation(pch.location)].(boxName).items.pistolbullets = 0;
							}
						}
						if(weapon.shottype == "mb")
						{
							ammoName = "musketbullets";
							bAmmoTaken = true;
							if(sti(locations[FindLocation(pch.location)].(boxName).items.musketbullets)>=s/2)
							{
								locations[FindLocation(pch.location)].(boxName).items.musketbullets = mbulletsAmount - s/2;
								TakeNItems(pch,"musketbullets", s/2);
							}
							else
							{
								TakeNItems(pch,"musketbullets", sti(locations[FindLocation(pch.location)].(boxName).items.musketbullets));
								locations[FindLocation(pch.location)].(boxName).items.musketbullets = 0;
							}
						}
						if(CheckCharacterItem(pch,"powderflask"))
						{
							bPwdrTaken = true;
							if(sti(GetCargoGoods(pch, GOOD_GUNPOWDER))>=12)
							{
								SetCharacterGoods(pch, GOOD_GUNPOWDER, makeint(sti(GetCargoGoods(pch, GOOD_GUNPOWDER))-12));
								TakeNItems(pch,"gunpowder", 12);
							}
							else
							{
								TakeNItems(pch,"gunpowder", sti(GetCargoGoods(pch, GOOD_GUNPOWDER)));
								SetCharacterGoods(pch, GOOD_GUNPOWDER, 0);
							}
						}
						else
						{
							bPwdrTaken = true;
							if(sti(GetCargoGoods(pch, GOOD_GUNPOWDER))>=6)
							{
								SetCharacterGoods(pch, GOOD_GUNPOWDER, makeint(sti(GetCargoGoods(pch, GOOD_GUNPOWDER))-6));
								TakeNItems(pch,"gunpowder", 6);
							}
							else
							{
								TakeNItems(pch,"gunpowder", sti(GetCargoGoods(pch, GOOD_GUNPOWDER)));
								SetCharacterGoods(pch, GOOD_GUNPOWDER, 0);
							}
						}

						int tmpLangFileID = LanguageOpenFile("interface_strings.txt");
						if(bAmmoTaken) Log_SetStringToLog(LanguageConvertString(tmpLangFileID,"You have taken") + " " + XI_ConvertString("n"+ammoName) + "!");
						if(bPwdrTaken) Log_SetStringToLog(LanguageConvertString(tmpLangFileID,"You have taken") + " " + XI_ConvertString("nGunpowder") + "!");
						LanguageCloseFile(tmpLangFileID);
						if(bAmmoTaken || bPwdrTaken) PlaySound("INTERFACE\important_item.wav");
					}
			*/

				// JRH fix oct 06: old code but functional -->
					bool bAmmoTaken = false;
					int pb = GetCharacterItem(pch, "pistolbullets");
					int pg = GetCharacterItem(pch, "pistolgrapes");
					int mb = GetCharacterItem(pch, "musketbullets");
					int p;

					locations[FindLocation(pch.location)].(boxName).items.gunpowder = 300*(8 - ShCl) + rand(100);
					locations[FindLocation(pch.location)].(boxName).items.pistolbullets = 300*(8 - ShCl) + rand(100);
					locations[FindLocation(pch.location)].(boxName).items.pistolgrapes = 75*(8 - ShCl) + rand(75);
					locations[FindLocation(pch.location)].(boxName).items.musketbullets = 15*(8 - ShCl) + rand(15);

					if(CheckAttribute(weapon, "shottype") ) 
					{
						if(weapon.shottype == "pg2" || weapon.shottype == "pg" || weapon.shottype == "mb" || weapon.shottype == "pb")
						{
							if(CheckAttribute(pch,"equip."+FLASK_ITEM_TYPE) ) { p = 12; } 
							else { p = 6; }

							if(CheckAttribute(pch,"equip."+POUCH_ITEM_TYPE) ) { s = 12;}
							else { s = 6; }

							if(gp < p) bAmmoTaken = true;

							if(weapon.shottype == "pg2" || weapon.shottype == "pg") 
							{
								if(pg < s) bAmmoTaken = true; 
							}
							if(weapon.shottype == "mb")
							{
								if(mb < s/2) bAmmoTaken = true;
							}
							if(weapon.shottype == "pb")
							{
								if(pb < s) bAmmoTaken = true;
							}

							TakeNItems(pch,"gunpowder", -100); TakeNItems(pch,"pistolbullets", -100);
							TakeNItems(pch,"pistolgrapes", -100); TakeNItems(pch,"musketbullets", -100);

							if(weapon.shottype == "pg2" || weapon.shottype == "pg") TakeNItems(pch,"pistolgrapes", s);
							if(weapon.shottype == "mb") TakeNItems(pch,"musketbullets", s/2);
							if(weapon.shottype == "pb") TakeNItems(pch,"pistolbullets", s);
							TakeNItems(pch,"gunpowder", p);

							if(bAmmoTaken)
							{
								Log_SetStringToLog("I have taken Pistol Ammunition!");
								PlaySound("INTERFACE\important_item.wav");
							}
						}
					}
				//<-- old code JRH
				}// TIH <-- mod toggle
				break;
			
			case "GunpowderBarrel":
				GameInterface.title = "titleGunpowderBarrel";
				PlaySound("AMBIENT\JAIL\door_003.wav");
				if (CANNONPOWDER_MOD) {// TIH --> mod toggle 7-7-06
					//JRH in here
					powderCount = GetCargoGoods(pch, GOOD_GUNPOWDER);
					locations[FindLocation(pch.location)].(boxName).open = true;
					locations[FindLocation(pch.location)].(boxName).items.gunpowder = powderCount;
				}// TIH <-- mod toggle
				isGunpowder = true;
				isItemsBox = false;
				break;
			
			case "GunpowderStorageEnemy":
				GameInterface.title = "titleGunpowderStorage";
				PlaySound("AMBIENT\JAIL\door_003.wav");// was door_002 which is corrupted in a way
				if (CANNONPOWDER_MOD) {// TIH --> mod toggle 7-7-06
					if(bAbordageStarted && !bCabinStarted && !bDeckStarted && !bCrewStarted)
					{
						powderCount = GetCargoGoods(boarding_enemy, GOOD_GUNPOWDER);
						locations[FindLocation(pch.location)].(boxName).open = true;
						locations[FindLocation(pch.location)].(boxName).items.gunpowder = powderCount;
					}
				}// TIH <-- mod toggle
				isGunpowder = true;
				isItemsBox = false;
				break;
			
			case "GunpowderBarrelEnemy":
				GameInterface.title = "titleGunpowderBarrel";
				PlaySound("AMBIENT\JAIL\door_003.wav");
				if (CANNONPOWDER_MOD) {// TIH --> mod toggle 7-7-06
					if(bAbordageStarted && !bCabinStarted && !bDeckStarted && !bCrewStarted)
					{
						powderCount = GetCargoGoods(boarding_enemy, GOOD_GUNPOWDER);
						locations[FindLocation(pch.location)].(boxName).open = true;
						locations[FindLocation(pch.location)].(boxName).items.gunpowder = powderCount;
					}
				}// TIH <-- mod toggle
				isGunpowder = true;
				isItemsBox = false;
				break;
			
		}// end switch whichInterface
		
	}
	else 
	{
		// changed by MAXIMUS -->
		if(bDeadExchange==true) { GameInterface.title = "titleExchangeWithDead"; isGunpowder = false; }
		if(bEnemyExchange==true) { GameInterface.title = "titleExchangeWithEnemy"; isGunpowder = false; }
		if(bStealExchange==true) { GameInterface.title = "titleExchangeByStealing"; isGunpowder = false; }
		for(int i = 1; i < 4; i++)
		{
		    ref Char = GetCharacter(GetOfficersIndex(GetMainCharacter(), i));
			if(CheckAttribute(Char,"faceID"))
			{
			  if(faceID==Char.faceID) GameInterface.title = "titleExchangeItems";
			  isGunpowder = false;
			}
		}
		// changed by MAXIMUS <--

		// Viper - Kindly ask that officer to unequip the stuff
		RemoveCharacterEquip(itemsRef, GUN_ITEM_TYPE); 
		RemoveCharacterEquip(itemsRef, BLADE_ITEM_TYPE);
		RemoveCharacterEquip(itemsRef, ARMOR_ITEM_TYPE); // NK
		RemoveCharacterEquip(itemsRef, FLASK_ITEM_TYPE); //JRH
		RemoveCharacterEquip(itemsRef, POUCH_ITEM_TYPE); //JRH
		itemsRef.skiprecalcskill = true;
		//remove all skill modifiers
		DeleteAttribute(&itemsRef,"skill.Leadership.mod"); // NK/GZ items mod
		DeleteAttribute(&itemsRef,"skill.Fencing.mod"); // NK/GZ items mod
		DeleteAttribute(&itemsRef,"skill.Sailing.mod"); // NK/GZ items mod
		DeleteAttribute(&itemsRef,"skill.Accuracy.mod"); // NK/GZ items mod
		DeleteAttribute(&itemsRef,"skill.Cannons.mod"); // NK/GZ items mod
		DeleteAttribute(&itemsRef,"skill.Grappling.mod"); // NK/GZ items mod
		DeleteAttribute(&itemsRef,"skill.Repair.mod"); // NK/GZ items mod
		DeleteAttribute(&itemsRef,"skill.Defence.mod"); // NK/GZ items mod
		DeleteAttribute(&itemsRef,"skill.Commerce.mod"); // NK/GZ items mod
		DeleteAttribute(&itemsRef,"skill.Sneak.mod"); // NK/GZ items mod
	}
	// TIH <-- total house cleaning
	g_refItems = itemsRef;

	bool bUse = GameInterface.title!="titleExchangeItems" && bDeadExchange==false && bEnemyExchange==false && bStealExchange==false;
	if(!bUse) bUse = CheckAttribute(pch, "ExchangeItemsFromCharacterScreen")==true;
	if(!bUse)
	{
		locCameraSleep(true);
		SetTimeScale(0.0);

		Log_SetActiveAction("Nothing");
		BLI_RefreshCommandMenu();
		BLI_DisableShow();
		SendMessage(&ILogAndActions,"ll",LI_SET_VISIBLE,false);

		LayerFreeze("realize",bSeaActive && !ownDeckStarted() && !LAi_IsBoardingProcess());
		LayerFreeze("execute",true);
		bool bShow = ownDeckStarted();
		if(!bShow) bShow = LAi_IsBoardingProcess();
		LayerFreeze("sea_realize",!bSeaActive && bShow);
		LayerFreeze("sea_execute",true);
	}

	//idLngFile = LanguageOpenFile(GetStorylineDir(FindCurrentStoryline()) + "ItemsDescribe.txt"); // KK

	FillScroll();
	SendMessage(&GameInterface,"ls",MSG_INTERFACE_INIT,iniName);
	CreateExitString();//MAXIMUS: standard exit-string for exit-button
	if(bDeadExchange) // KK
	{
		SetNodeUsing("VIDEOBASE",false);
		SetNodeUsing("BACKSLIDE",false);
		SetNodeUsing("SCROLLRECTANGLE",false);
		SetNodeUsing("DOWNSCROLLRECT",false);
		SetNodeUsing("DOWNRECTSMOOTH",false);
		SetNodeUsing("TITLE",false);
		SetNodeUsing("CENTERRECTANGLE",false);
		SetNodeUsing("CENTERBOUND",false);
		SetNodeUsing("BACKGROUND",false);
		SetNodeUsing("INFO",false);
	}

	CreateImage("MyPic",GetFaceGroupName(GetMainCharacterIndex()),GetFacePictureName(GetMainCharacterIndex()),5,39,133,167);

	GameInterface.pictures.OtherPic.tex = "";
	GameInterface.pictures.OtherPic.pic = "";
	SetNodeUsing( "FACEPICT", faceID!="" );
	if(faceID=="")
	{
		g_bIBoxUsed = true;
		if(boxType == "gunpowder") CreateImage("OtherPic","GOODS","Gunpowder",-5,292,143,450);// TIH no need for mod toggle on this 7-7-06
		else 
		{
			curLoc = Locations[FindLocation(pch.location)].filespath.models;
// KK -->
			if (boxName == "WeaponsLocker")
				CreateImage("OtherPic","RELATIONS","Enemy",-5,292,143,450);
			else
				CreateImage("OtherPic","BOX_IMAGE","BoxImage",5,302,133,430);
// <-- KK
		}
	}
	else
	{
		g_bIBoxUsed = false;
// changed by MAXIMUS -->
		if (CheckAttribute(GameInterface, "title") && GameInterface.title == "titleExchangeWithDead")	// LDH fix for missing attrib
		{
		    SetNewPicture("EN_PIC", "interfaces\portraits\128\face_skull.tga");
		    SetNewPicture("FACEPICT", "interfaces\portraits\128\face_skull.tga");
		}
		else
		{
		    SetNewPicture("EN_PIC", "interfaces\portraits\128\face_" + faceID + ".tga");
		    SetNewPicture("FACEPICT", "interfaces\portraits\128\face_" + faceID + ".tga");
		}
// changed by MAXIMUS <--
	}

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

// added by MAXIMUS-->
	SetSelectable("EXIT_BUTTON",true);
	SetNodeUsing("CONFIRM_RECTANGLE",false);
	SetNodeUsing("TEXTWINDOW",false);
	SetNodeUsing("CONFIRM_YES_BUTTON",false);
	SetNodeUsing("CONFIRM_NO_BUTTON",false);
	SetNodeUsing("REL_PIC",false);
	SetNodeUsing("EN_PIC",false);
// added by MAXIMUS <--

	SetEventHandler("InterfaceBreak","ProcessCancelExit",0);
	SetEventHandler("exitCancel","ProcessCancelExit",0);
	SetEventHandler("frame","FrameProcess",1);

	SetEventHandler("ievnt_command","ProcInterfaceCommand",0);
// added by MAXIMUS-->
	SetEventHandler("ExitPress","ProcessExit",0);
	SetEventHandler("ExitNo","ProcessExit_no",0);
	SetEventHandler("ExitYes","ProcessExit_yes",0);
// added by MAXIMUS <--

	SetItemMode(ITEM_MODE_SWAP);

	IGotoNewNode("TWO_ITEMSLIST");
	IGotoNewNode("ONE_ITEMSLIST");

	SetUpDownUsed();
	SetScrollsUsing();
}

// added by MAXIMUS -->
void ProcessExit()
{
	string faceID;
	SetNodeUsing("CONFIRM_RECTANGLE",true);
	SetNodeUsing("TEXTWINDOW",true);
	SetNodeUsing("CONFIRM_YES_BUTTON",true);
	SetNodeUsing("CONFIRM_NO_BUTTON",true);
	SetNodeUsing("REL_PIC",true);
	SetNodeUsing("EN_PIC",true);
	SetCurrentNode("CONFIRM_NO_BUTTON");
	SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 1, "CONFIRM_NO_BUTTON");
	SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 2, "CONFIRM_YES_BUTTON");

	//int tmpLangFileID = LanguageOpenFile("interface_strings.txt");
	SetFormatedText("TEXTWINDOW", TranslateString("","Exit from exchange confirm"));
	//LanguageCloseFile(tmpLangFileID);
}

void ProcessExit_no()
{
	SendMessage(&GameInterface,"ll", MSG_INTERFACE_LOCK_NODE, 0);

	SetNodeUsing("CONFIRM_RECTANGLE",false);
	SetNodeUsing("TEXTWINDOW",false);
	SetNodeUsing("CONFIRM_YES_BUTTON",false);
	SetNodeUsing("CONFIRM_NO_BUTTON",false);
	SetNodeUsing("REL_PIC",false);
	SetNodeUsing("EN_PIC",false);

	SetCurrentNode("INFO_BUTTON");
}

void ProcessExit_yes()
{
//	if(oldItemMode==ITEM_MODE_INFO) SetItemMode(ITEM_MODE_SWAP);
//	else IDoExit(RC_INTERFACE_ITEMSBOX_EXIT);
//	else
	IDoExit(RC_INTERFACE_DO_RESUME_GAME);
}
// added by MAXIMUS <--

void FrameProcess()
{
	IGotoNewNode(GetCurrentNode());

	if( nCurScroll1 != sti(GameInterface.one_itemslist.current) ||
		nCurScroll2 != sti(GameInterface.two_itemslist.current) )
	{
		nCurScroll1 = sti(GameInterface.one_itemslist.current);
		nCurScroll2 = sti(GameInterface.two_itemslist.current);
		if(oldItemMode!=ITEM_MODE_SWAP)	{SetItemMode(ITEM_MODE_SWAP);}
		SetUpDownUsed();
	}
}

void FillScroll()
{
    int i,j,q;
    string attributeName;
	aref curItem,arItem,rootItems;
	aref arImgGrp;
	ref mchref = GetMainCharacter();

	if(CheckAttribute(&GameInterface,"one_itemslist.current"))
		nCurScroll1 = sti(GameInterface.one_itemslist.current);
	else	nCurScroll1 = 0;
	if(CheckAttribute(&GameInterface,"two_itemslist.current"))
		nCurScroll2 = sti(GameInterface.two_itemslist.current);
	else	nCurScroll2 = 0;

	DeleteAttribute(&GameInterface,"one_itemslist");
	DeleteAttribute(&GameInterface,"two_itemslist");

	//============================================================================
	// PLAYER ITEMS
	//============================================================================
	GameInterface.one_itemslist.NotUsed = 5;
	GameInterface.one_itemslist.current = nCurScroll1;
	GameInterface.one_itemslist.ImagesGroup.t0 = "usedflag";// TIH
	GameInterface.one_itemslist.ImagesGroup.t1 = "ICONS";// TIH
	makearef(arImgGrp,GameInterface.one_itemslist.ImagesGroup);
	FillImagesGroupForItems(arImgGrp);

	i=0;
	// figure out the total gold the player has and add it to the first scroll item
	if( sti(mchref.Money)>0 )
	{
		GameInterface.one_itemslist.pic1.img1 = GetItemPictureName("Gold");
		GameInterface.one_itemslist.pic1.tex1 = GetItemPictureTexture("one_itemslist.ImagesGroup","Gold");
		GameInterface.one_itemslist.pic1.itemID = "Money";
		GameInterface.one_itemslist.pic1.str1 = "#"+sti(mchref.Money);		// LDH 15Oct06 fix for decimal point money
		i++;
	}
	// loop through all items except maps of the player and add to scroller
	makearef(rootItems,mchref.Items);
    for(j=0; j<GetAttributesNum(rootItems); j++)
    {
		attributeName = "pic" + (i+1);
		curItem = GetAttributeN(rootItems,j);
		if (GetAttributeName(curItem) == "bladeX4") continue;// TIH simply never show fists in looting screens 7-7-06

		if( Items_FindItem(GetAttributeName(curItem),&arItem)>=0 )
		{
			if (CheckAttribute(arItem, "groupID") == true && arItem.groupID == MAP_ITEM_TYPE) continue; // KK
			GameInterface.one_itemslist.(attributeName).img1 = GetItemPictureName(arItem.id);
			GameInterface.one_itemslist.(attributeName).tex1 = GetItemPictureTexture("one_itemslist.ImagesGroup",arItem.id);
			GameInterface.one_itemslist.(attributeName).itemID = arItem.id;
			//if(CheckConsumedItem(arItem.id)) {// TIH doesnt work right?
			if ( sti(GetAttributeValue(curItem)) > 1 ) {// TIH absolute check
				GameInterface.one_itemslist.(attributeName).str1 = "#"+sti(GetAttributeValue(curItem));
			}
			// TIH --> add equipped checkmarks
			if( IsEquipCharacterByItem(mchref,arItem.id) )
			{
				GameInterface.one_itemslist.(attributeName).img2 = "usedflag";
				GameInterface.one_itemslist.(attributeName).tex2 = 0;
			}
			// TIH <--
		}
		else
		{
			trace("WARNING! Can`t find item ID = "+GetAttributeName(curItem));
			GameInterface.one_itemslist.(attributeName).img1 = "itm16";
			GameInterface.one_itemslist.(attributeName).tex1 = 0;
		}
		i++;
    }
	GameInterface.one_itemslist.ListSize = i;

	//============================================================================
	// TRANSFER FROM ITEMS
	//============================================================================
	GameInterface.two_itemslist.NotUsed = 5;
	GameInterface.two_itemslist.current = nCurScroll2;
	makearef(arImgGrp,GameInterface.two_itemslist.ImagesGroup);
	FillImagesGroupForItems(arImgGrp);

	i=0;
	// if the location/character has no gold, create the attribute and set to zero
	if(!CheckAttribute(g_refItems,"Money"))	g_refItems.Money = 0;

	makearef(rootItems,g_refItems.Items);
	q = GetAttributesNum(rootItems);

	// loop through items to find any gold to add to the total
	for(j=0; j<q; j++)
	{
		curItem = GetAttributeN(rootItems,j);
		if(sti(GetAttributeValue(curItem))<=0)	continue;
		if( Items_FindItem(GetAttributeName(curItem),&arItem)>=0 )
		{
			if( CheckAttribute(arItem,"Gold") )
			{
				g_refItems.Money = sti(g_refItems.Money) + sti(arItem.Gold)*GetCharacterItem(g_refItems,arItem.Gold);
				DeleteAttribute(rootItems,arItem.id);
				j--; q--;
			}
		}
	}
	if( sti(g_refItems.Money)>0 )
	{
		GameInterface.two_itemslist.pic1.img1 = GetItemPictureName("Gold");
		GameInterface.two_itemslist.pic1.tex1 = GetItemPictureTexture("two_itemslist.ImagesGroup","Gold");
		GameInterface.two_itemslist.pic1.itemID = "Money";
		GameInterface.two_itemslist.pic1.str1 = "#"+sti(g_refItems.Money);		// LDH 15Oct06 fix for decimal point money
		i++;
	}

	// loop through items of the location/character and add to second scroller
	for(j=0; j<q; j++)
	{
		attributeName = "pic" + (i+1);
		curItem = GetAttributeN(rootItems,j);

		if(sti(GetAttributeValue(curItem))<=0)	continue;
		if (GetAttributeName(curItem) == "bladeX4") continue;// TIH simply never show fists in looting screens 7-7-06

		if( Items_FindItem(GetAttributeName(curItem),&arItem)>=0 )
		{
			if(bDeadExchange)
			{
				if(IsCharacterEquipByGroup(g_refItems,BLADE_ITEM_TYPE)) { RemoveCharacterEquip(g_refItems, BLADE_ITEM_TYPE); }
				if(IsCharacterEquipByGroup(g_refItems,GUN_ITEM_TYPE)) { RemoveCharacterEquip(g_refItems, GUN_ITEM_TYPE); }
				if(IsCharacterEquipByGroup(g_refItems,ARMOR_ITEM_TYPE)) { RemoveCharacterEquip(g_refItems, ARMOR_ITEM_TYPE); }
			}
			GameInterface.two_itemslist.(attributeName).img1 = GetItemPictureName(arItem.id);
			GameInterface.two_itemslist.(attributeName).tex1 = GetItemPictureTexture("two_itemslist.ImagesGroup",arItem.id);
			GameInterface.two_itemslist.(attributeName).itemID = arItem.id;
			//if(CheckConsumedItem(arItem.id)) {// TIH doesnt work right?
			if ( sti(GetAttributeValue(curItem)) > 1 ) {// TIH absolute check
				GameInterface.two_itemslist.(attributeName).str1 = "#"+sti(GetAttributeValue(curItem));
			}
		}
		else
		{
			trace("WARNING! Can`t find item ID = "+GetAttributeName(curItem));
			GameInterface.two_itemslist.(attributeName).img1 = "itm16";
			GameInterface.two_itemslist.(attributeName).tex1 = 0;
		}
		i++;
	}
	GameInterface.two_itemslist.ListSize = i;
	if(i==0) noConfirm = true;

	nCurScroll1 = sti(GameInterface.one_itemslist.current);
	nCurScroll2 = sti(GameInterface.two_itemslist.current);
}

void ProcessCancelExit()
{
// changed by MAXIMUS -->
	if(!isItemsBox)
	{
		if(bDeadExchange==true || bEnemyExchange==true || bStealExchange==true)
		{
			if(noConfirm==true) ProcessExit_yes();// TIH procedure fix 7-7-06
			else ProcessExit();
		}
		else
		{
//	        if(oldItemMode==ITEM_MODE_INFO) SetItemMode(ITEM_MODE_SWAP);
//	        else IDoExit(RC_INTERFACE_ITEMSBOX_EXIT);
//	        else
			IDoExit(RC_INTERFACE_DO_RESUME_GAME);
		}
	}
	else
	{
//	    if(oldItemMode==ITEM_MODE_INFO) SetItemMode(ITEM_MODE_SWAP);
//	    else IDoExit(RC_INTERFACE_ITEMSBOX_EXIT);
//	    else
		IDoExit(RC_INTERFACE_DO_RESUME_GAME);
	}
// changed by MAXIMUS <--
}

void IDoExit(int exitCode)
{
	int s; // NK for below for() statements
	//LanguageCloseFile(idLngFile);
	if(!g_bIBoxUsed)
	{
		for(s = 0; s < 10; s++) GetCharacterSkill(g_refItems, GetSkillName(s)); // NK/GZ items mod to force refresh of modifiers.
		DeleteAttribute(&g_refItems, "skiprecalcskill");
		if( GetCharacterSkill(g_refItems,"fencing") > 0 )
		{
			EquipCharacterByItem(g_refItems, FindCharacterItemByGroup(g_refItems,BLADE_ITEM_TYPE));
			EquipCharacterByItem(g_refItems, FindCharacterItemByGroup(g_refItems,GUN_ITEM_TYPE));
			EquipCharacterByItem(g_refItems, FindCharacterItemByGroup(g_refItems,ARMOR_ITEM_TYPE)); // NK
			EquipCharacterByItem(g_refItems, FindCharacterItemByGroup(g_refItems,FLASK_ITEM_TYPE)); //JRH
			EquipCharacterByItem(g_refItems, FindCharacterItemByGroup(g_refItems,POUCH_ITEM_TYPE)); //JRH
		}
	}
	for(s = 0; s < 10; s++) GetCharacterSkill(GetMainCharacter(), GetSkillName(s)); // NK/GZ items mod to force refresh of modifiers.
	DeleteAttribute(GetMainCharacter(), "skiprecalcskill");
	DelEventHandler("InterfaceBreak","ProcessCancelExit");
	DelEventHandler("exitCancel","ProcessCancelExit");
	DelEventHandler("frame","FrameProcess");
	DelEventHandler("ievnt_command","ProcInterfaceCommand");
// added by MAXIMUS -->
	DelEventHandler("ExitPress","ProcessExit");
	DelEventHandler("ExitNo","ProcessExit_no");
	DelEventHandler("ExitYes","ProcessExit_yes");
// added by MAXIMUS <--

    interfaceResultCommand = exitCode;

// LDH - go back to Character screen if exchange called from there.
	if (CheckAttribute(GetMainCharacter(), "ExchangeItemsFromCharacterScreen"))
	{
		PostEvent("LaunchIAfterFrame",1,"sl", "I_CHARACTER", 1);
		DeleteAttribute(GetMainCharacter(), "ExchangeItemsFromCharacterScreen");
	    EndCancelInterface(false);
		return;
	} 
	else 
	{
		EndCancelInterface(true);	// original code
	}

	if(bDeadExchange || bEnemyExchange || bStealExchange || GameInterface.title=="titleExchangeItems")
	{
		SendMessage(&ILogAndActions,"ll",LI_SET_VISIBLE,true);
		SetTimeScale(1.0);
		locCameraSleep(false);

		EngineLayersOffOn(false);
		SendMessage(&ILogAndActions,"ll",LI_SET_VISIBLE,true);
		BLI_EnableShow();
		BLI_RefreshCommandMenu();
		CheckCharacterAction();
		if(interfaceResultCommand == RC_INTERFACE_DO_RESUME_GAME) { DeleteEntitiesByType("scrshoter"); }
		bDeadExchange = false;
		bEnemyExchange = false;
		bStealExchange = false;
	}
}

void IGotoNewNode(string newNode)
{
	if(oldNodeName==newNode) return;
	oldNodeName=newNode;
	if(newNode=="ONE_ITEMSLIST")	SetScroll(true);
	if(newNode=="TWO_ITEMSLIST")	SetScroll(false);

	if(oldItemMode!=ITEM_MODE_SWAP)
	{
		if(newNode!="DESCRIBEWINDOW")
		{
			SetItemMode(ITEM_MODE_SWAP);
			SetUpDownUsed();
		}
	}
}

void SetScroll(bool bIsOne)
{
	if(bIsOne)
	{
		if(oldCurScrollName=="TWO_ITEMSLIST")
		{
			SetNodeUsing("TWO_LEFTSCROLLBUTTON",false);
			SetNodeUsing("TWO_RIGHTSCROLLBUTTON",false);
		}
		SetNodeUsing("ONE_LEFTSCROLLBUTTON",true);
		SetNodeUsing("ONE_RIGHTSCROLLBUTTON",true);
		oldCurScrollName = "ONE_ITEMSLIST";
	}
	else
	{
		if(oldCurScrollName=="ONE_ITEMSLIST")
		{
			SetNodeUsing("ONE_LEFTSCROLLBUTTON",false);
			SetNodeUsing("ONE_RIGHTSCROLLBUTTON",false);
		}
		SetNodeUsing("TWO_LEFTSCROLLBUTTON",true);
		SetNodeUsing("TWO_RIGHTSCROLLBUTTON",true);
		oldCurScrollName = "TWO_ITEMSLIST";
	}
}

void SetItemMode(int mode)
{
	switch(oldItemMode)
	{
	case ITEM_MODE_SWAP:
		SetNodeUsing("INFO_BUTTON",false);
		SetNodeUsing("SWAP_BUTTON",false);
		SetNodeUsing("ONEUP_BUTTON",false);
		SetNodeUsing("ONEDOWN_BUTTON",false);
		SetNodeUsing("ALLUP_BUTTON",false);
		SetNodeUsing("ALLDOWN_BUTTON",false);
	break;
	//case ITEM_MODE_INFO:	SetNodeUsing("DESCRIBEWINDOW",false);	break;
	}

	oldItemMode = mode;

	switch(mode)
	{
	case ITEM_MODE_SWAP:
		SetNodeUsing("INFO_BUTTON",true);
		SetNodeUsing("SWAP_BUTTON",true);
		SetNodeUsing("ONEUP_BUTTON",true);
		SetNodeUsing("ONEDOWN_BUTTON",true);
		SetNodeUsing("ALLUP_BUTTON",true);
		SetNodeUsing("ALLDOWN_BUTTON",true);
	break;
	//case ITEM_MODE_INFO:	SetNodeUsing("DESCRIBEWINDOW",true);	break;
	}
}

void ProcInterfaceCommand()
{
	string comName = GetEventData();
	string nodeName = GetEventData();

	switch(nodeName)
	{
	case "INFO_BUTTON":
		if(comName=="upstep") {GoToTopScroll();}
		if(comName=="downstep") {GoToBottomScroll();}
		if(comName=="rightstep") {SelectButtonRight("ONEDOWN_BUTTON");}
		if(comName=="deactivate")	{SetCurrentNode(oldCurScrollName);}
// KK -->
		if(comName=="activate" || comName=="click")
		{
			/*SetItemMode(ITEM_MODE_INFO);
			SetDescribeText();
			SetCurrentNode("DESCRIBEWINDOW");*/
			ShowDescription(true);
		}
// <-- KK
	break;

// KK -->
	case "SHOW_DESCRIPTION_OK_BUTTON":
		if(comName == "activate" || comName == "click" || comName == "deactivate")
		{
			ShowDescription(false);
		}
	break;
// <-- KK

	case "ONEDOWN_BUTTON":
		if(comName=="upstep") {GoToTopScroll();}
		if(comName=="downstep") {GoToBottomScroll();}
		if(comName=="leftstep") {SelectButtonLeft("INFO_BUTTON");}
		if(comName=="rightstep") {SelectButtonRight("ONEUP_BUTTON");}
		if(comName=="activate" || comName=="click")	{GiveOneItem();}
		if(comName=="deactivate")	{SetCurrentNode(oldCurScrollName);}
	break;

	case "ONEUP_BUTTON":
		if(comName=="upstep") {GoToTopScroll();}
		if(comName=="downstep") {GoToBottomScroll();}
		if(comName=="leftstep") {SelectButtonLeft("ONEDOWN_BUTTON");}
		if(comName=="rightstep") {SelectButtonRight("SWAP_BUTTON");}
		if(comName=="activate" || comName=="click")	{TakeOneItem();}
		if(comName=="deactivate")	{SetCurrentNode(oldCurScrollName);}
	break;

	case "SWAP_BUTTON":
		if(comName=="upstep") {GoToTopScroll();}
		if(comName=="downstep") {GoToBottomScroll();}
		if(comName=="leftstep") {SelectButtonLeft("ONEUP_BUTTON");}
		if(comName=="rightstep") {SelectButtonRight("ALLUP_BUTTON");}
		if(comName=="activate" || comName=="click")	{ChangeItemsSlot();}
		if(comName=="deactivate")	{SetCurrentNode(oldCurScrollName);}
	break;

	case "ALLUP_BUTTON":
		if(comName=="upstep") {GoToTopScroll();}
		if(comName=="downstep") {GoToBottomScroll();}
		if(comName=="leftstep") {SelectButtonLeft("SWAP_BUTTON");}
		if(comName=="rightstep") {SelectButtonRight("ALLDOWN_BUTTON");}
		if(comName=="activate" || comName=="click")	{TakeItemsCollection();}
		if(comName=="deactivate")	{SetCurrentNode(oldCurScrollName);}
	break;

	case "ALLDOWN_BUTTON":
		if(comName=="upstep") {GoToTopScroll();}
		if(comName=="downstep") {GoToBottomScroll();}
		if(comName=="leftstep") {SelectButtonLeft("ALLUP_BUTTON");}
		if(comName=="activate" || comName=="click")	{GiveItemsCollection();}
		if(comName=="deactivate")	{SetCurrentNode(oldCurScrollName);}
	break;

	case "ONE_ITEMSLIST":
		if(comName=="downstep")	{SelectButtonLeft("SWAP_BUTTON");}
	break;

	case "TWO_ITEMSLIST":
		if(comName=="upstep")	{SelectButtonLeft("SWAP_BUTTON");}
	break;

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

void ChangeItemsSlot()
{
	string itm1 = GetItemIDByOrder(true,nCurScroll1);
	string itm2 = GetItemIDByOrder(false,nCurScroll2);
	if(itm1=="" || itm1!=itm2)
	{
		if( !CanCharacterTakeItem(true,itm2) ) return;
	}

	int iq1 = GetItemQntByOrder(true,nCurScroll1);
	int iq2 = GetItemQntByOrder(false,nCurScroll2);

	aref mchref; makearef(mchref,Characters[GetMainCharacterIndex()]);

	if(iq1>0)
	{
		if(itm1=="Money")
		{
			mchref.Money = 0;
			g_refItems.Money = iq1+sti(g_refItems.Money);
		}
		else
		{
			TakeNItems(GetMainCharacter(),itm1,-iq1);
			if( CheckAttribute(g_refItems,"Items."+itm1) )
			{
				iq1 = iq1 + sti(g_refItems.Items.(itm1));
			}
			g_refItems.Items.(itm1) = iq1;
		}
	}
	if(iq2>0)
	{
		if(itm2=="Money")
		{
			if(iq2 < 0)
			{
				g_refItems.Money = sti(g_refItems.Money)-iq2;
				AddWealthToCharacter(mchref, iq2);
			}
		}
		else
		{
			TakeNItems(GetMainCharacter(),itm2,iq2);
			g_refItems.Items.(itm2) = sti(g_refItems.Items.(itm2))-iq2;
			if( sti(g_refItems.Items.(itm2))<=0 ) DeleteAttribute(g_refItems,"Items."+itm2);
		}
	}

	FillScroll();
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"ONE_ITEMSLIST",-1);
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"TWO_ITEMSLIST",-1);
	SetUpDownUsed();
	SetScrollsUsing();
}

void TakeOneItem()
{
	string itm2 = GetItemIDByOrder(false,nCurScroll2);
	int iq2 = GetItemQntByOrder(false,nCurScroll2);
	if(iq2<=0) return;

	int gp;	//JRH
	int pb;
	int pg;
	int pg1;
	int mb;
	int mb1;

	int p;	//powder
	int s;	//shot

	aref mchref; makearef(mchref,Characters[GetMainCharacterIndex()]);

	// TIH --> bracket fixing 7-7-06
	if (itm2=="Money" || itm2=="gunpowder" || itm2=="pistolbullets" || itm2=="pistolgrapes" || itm2=="musketbullets") 
	{
		if(itm2=="Money")
		{
			if(iq2 >= 100)
			{
				iq2 = iq2 - 100;
				mchref.Money = sti(mchref.Money)+100;
				g_refItems.Money = iq2;
			}
		}

		//JRH -->
		if(itm2=="gunpowder")
		{
			if(CheckAttribute(mchref,"equip."+FLASK_ITEM_TYPE) )
			{
				p = 12;
			}
			else
			{
				p = 6;
			}

			int newQty;
			gp = GetCharacterItem(mchref, "gunpowder");
			if( gp >= p ) 
			{
				TakeNItems(mchref,"gunpowder", -100);
				TakeNItems(mchref,"gunpowder", p);
			}
			else
			{
				iq2--;
				TakeNItems(GetMainCharacter(),itm2,1);
				// added by MAXIMUS [for gunpowder barrels] -->
				if(CANNONPOWDER_MOD && isGunpowder) {// TIH --> mod toggle 7-7-06
					if(bAbordageStarted && !bCabinStarted && !bDeckStarted && !bCrewStarted)
					{
						newQty = sti(GetCargoGoods(boarding_enemy, GOOD_GUNPOWDER))-1;
						SetCharacterGoods(boarding_enemy, GOOD_GUNPOWDER, newQty);
					}
					else
					{
						newQty = sti(GetCargoGoods(GetMainCharacter(), GOOD_GUNPOWDER))-1;
						if(bDeckStarted) SetCharacterGoods(GetMainCharacter(), GOOD_GUNPOWDER, newQty);
					}
				}// TIH <-- mod toggle
				// added by MAXIMUS [for gunpowder barrels] <--
				g_refItems.Items.(itm2) = iq2;
				if( iq2<=0 ) DeleteAttribute(g_refItems,"Items."+itm2);
			}
		}

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

			pb = GetCharacterItem(mchref, "pistolbullets");
			if(pb > s)
			{
				TakeNItems(mchref,"pistolbullets", -100);
				TakeNItems(mchref,"pistolbullets", s);	
			}
			if(pb <= s)
			{
				pb = GetCharacterItem(mchref, "pistolbullets");
				pg = GetCharacterItem(mchref, "pistolgrapes");
				if(pb + pg >= s)
				{
					pg1 = s - pb;
					TakeNItems(mchref,"pistolgrapes", -pg);
					TakeNItems(mchref,"pistolgrapes", pg1);
				}
				if(pb + pg1 < s) 
				{
					pb = GetCharacterItem(mchref, "pistolbullets");
					pg1 = GetCharacterItem(mchref, "pistolgrapes");
					mb = GetCharacterItem(mchref, "musketbullets");
					if(pb + pg1 + 2*mb >= s)
					{
						mb1 = (s - pb - pg1)/2;
						TakeNItems(mchref,"musketbullets", -mb);
						TakeNItems(mchref,"musketbullets", mb1);
					}
					if(pb + pg1 + 2*mb == s - 1)
					{
						if(itm2=="pistolbullets" || itm2=="pistolgrapes")
						{
							iq2--;
							TakeNItems(GetMainCharacter(),itm2,1);
							g_refItems.Items.(itm2) = iq2;
							if( iq2<=0 ) DeleteAttribute(g_refItems,"Items."+itm2);	
						}
					}
					if(pb + pg1 + 2*mb < s - 1)
					{
						if(itm2=="pistolbullets" || itm2=="pistolgrapes" || itm2=="musketbullets")
						{
							iq2--;
							TakeNItems(GetMainCharacter(),itm2,1);
							g_refItems.Items.(itm2) = iq2;
							if( iq2<=0 ) DeleteAttribute(g_refItems,"Items."+itm2);	
						}
					}
				}	
			}
		}
		//JRH <--

	}
	else
	{
		iq2--;
		TakeNItems(GetMainCharacter(),itm2,1);
		g_refItems.Items.(itm2) = iq2;
		if( iq2<=0 ) DeleteAttribute(g_refItems,"Items."+itm2);
	}
	// TIH <-- bracket fixing


	FillScroll();
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"ONE_ITEMSLIST",-1);
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"TWO_ITEMSLIST",-1);
	SetUpDownUsed();
	SetScrollsUsing();
}

void GiveOneItem()
{
	string itm1 = GetItemIDByOrder(true,nCurScroll1);
	int iq1 = GetItemQntByOrder(true,nCurScroll1);
	if(iq1<=0) return;

	int gp;	//JRH not necessary to re-equip etc
	int pb;
	int pg;
	int pg1;
	int mb;
	int mb1;
	int p;	
	int s;

	aref mchref; makearef(mchref,Characters[GetMainCharacterIndex()]);

	// TIH --> bracket fixing 7-7-06
	if (itm1=="Money" || itm1=="gunpowder" || itm1=="pistolbullets" || itm1=="pistolgrapes" || itm1=="musketbullets") 
	{
		if(itm1=="Money")
		{
			if(iq1 > 99)
			{
				iq1 = iq1-100;
				mchref.Money = sti(mchref.Money)-100;
				g_refItems.Money = sti(g_refItems.Money)+100;
			}
			else
			{
				iq1 = 0;
				g_refItems.Money = sti(g_refItems.Money)+sti(mchref.Money);
				mchref.Money = 0;
			}
		}

		//JRH -->
		if(itm1=="gunpowder")
		{
			if ( IsOfficer(g_refitems) ) // JRH fix oct 06
			{
				if(CheckCharacterItem(g_refitems,"powderflask") > 0 )
				{
					p = 12;
				}
				else
				{
					p = 6;
				}
			}
			else p = 1000000;// changed from 1000 by MAXIMUS [for gunpowder barrels]

			gp = GetCharacterItem(g_refitems, "gunpowder");
			if( gp >= p) 
			{
				TakeNItems(g_refitems,"gunpowder", -100);
				TakeNItems(g_refitems,"gunpowder", p);
			}
			else
			{
				iq1--;
				TakeNItems(GetMainCharacter(),itm1,-1);
				if(CheckAttribute(g_refItems,"Items."+itm1)) g_refItems.Items.(itm1) = 1 + sti(g_refItems.Items.(itm1));
				else g_refItems.Items.(itm1) = 1;
				// added by MAXIMUS [for gunpowder barrels] -->
				if(CANNONPOWDER_MOD && isGunpowder) {// TIH --> mod toggle 7-7-06
				  int newQty;
				  if(bAbordageStarted && !bDeckEnter) // KK
				  {
					  newQty = sti(GetCargoGoods(boarding_enemy, GOOD_GUNPOWDER))+1;
					  SetCharacterGoods(boarding_enemy, GOOD_GUNPOWDER, newQty);
				  }
				  else
				  {
					  newQty = sti(GetCargoGoods(GetMainCharacter(), GOOD_GUNPOWDER))+1;
					  if(bDeckStarted) SetCharacterGoods(GetMainCharacter(), GOOD_GUNPOWDER, newQty);
				  }
				}// TIH <-- mod toggle
				// added by MAXIMUS [for gunpowder barrels] <--
			}
		}

		if(itm1=="pistolbullets" || itm1=="pistolgrapes" || itm1=="musketbullets") 
		{
			if ( IsOfficer(g_refitems) ) // JRH fix oct 06
			{
				if(CheckCharacterItem(g_refitems,"ammopouch") > 0 )
				{
					s = 12;
				}
				else
				{
					s = 6;
				}
			}
			else s = 1000;

			pb = GetCharacterItem(g_refitems, "pistolbullets");
			// MAXIMUS 21.07.2006 [for correct bullets transfer] -->
			if(isGunpowder)
			{
				if(itm1=="pistolbullets")
				{
					if(GetCharacterItem(GetMainCharacter(), "pistolbullets")<=s && GetCharacterItem(GetMainCharacter(), "pistolbullets")>0)
					{
						TakeNItems(GetMainCharacter(),"pistolbullets", -1);
						TakeNItems(g_refitems,"pistolbullets", 1);
					}
				}
				if(itm1=="pistolgrapes")
				{
					if(GetCharacterItem(GetMainCharacter(), "pistolgrapes")<=s && GetCharacterItem(GetMainCharacter(), "pistolgrapes")>0)
					{
						TakeNItems(GetMainCharacter(),"pistolgrapes", -1);
						TakeNItems(g_refitems,"pistolgrapes", 1);
					}
				}
				if(itm1=="musketbullets")
				{
					if(GetCharacterItem(GetMainCharacter(), "musketbullets")<=s && GetCharacterItem(GetMainCharacter(), "musketbullets")>0)
					{
						TakeNItems(GetMainCharacter(),"musketbullets", -1);
						TakeNItems(g_refitems,"musketbullets", 1);
					}
				}
			}
			else
			{// MAXIMUS 21.07.2006 [for correct bullets transfer] <--
			if(pb > s)
			{
				TakeNItems(g_refitems,"pistolbullets", -100);
				TakeNItems(g_refitems,"pistolbullets", s);
			}
			if(pb <= s)
			{
				pb = GetCharacterItem(g_refitems, "pistolbullets");
				pg = GetCharacterItem(g_refitems, "pistolgrapes");
				if(pb + pg >= s)
				{
					pg1 = s - pb;
					TakeNItems(g_refitems,"pistolgrapes", -pg);
					TakeNItems(g_refitems,"pistolgrapes", pg1);
				}
				if(pb + pg1 < s) 
				{
					pb = GetCharacterItem(g_refitems, "pistolbullets");
					pg1 = GetCharacterItem(g_refitems, "pistolgrapes");
					mb = GetCharacterItem(g_refitems, "musketbullets");
					if(pb + pg1 + 2*mb >= s)
					{
						mb1 = (s - pb - pg1)/2;
						TakeNItems(g_refitems,"musketbullets", -mb);
						TakeNItems(g_refitems,"musketbullets", mb1);
					}
					if(pb + pg1 + 2*mb == s - 1)
					{
						if(itm1=="pistolbullets" || itm1=="pistolgrapes")
						{
							iq1--;
							TakeNItems(GetMainCharacter(),itm1,-1);
							if( CheckAttribute(g_refItems,"Items."+itm1) ) g_refItems.Items.(itm1) = 1 + sti(g_refItems.Items.(itm1));
							else	g_refItems.Items.(itm1) = 1;
						}
					}
					if(pb + pg1 + 2*mb < s - 1)
					{
						if(itm1=="pistolbullets" || itm1=="pistolgrapes" || itm1=="musketbullets")
						{
							iq1--;
							TakeNItems(GetMainCharacter(),itm1,-1);
							if( CheckAttribute(g_refItems,"Items."+itm1) ) g_refItems.Items.(itm1) = 1 + sti(g_refItems.Items.(itm1));
							else	g_refItems.Items.(itm1) = 1;
						}
					}
				}	
			}
			}
		}
		//JRH <--

	}
	else
	{
		iq1--;
		TakeNItems(GetMainCharacter(),itm1,-1);
		if( CheckAttribute(g_refItems,"Items."+itm1) )
			g_refItems.Items.(itm1) = 1 + sti(g_refItems.Items.(itm1));
		else	g_refItems.Items.(itm1) = 1;
	}
	// TIH <-- bracket fixing

	FillScroll();
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"ONE_ITEMSLIST",-1);
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"TWO_ITEMSLIST",-1);
	SetUpDownUsed();
	SetScrollsUsing();
}
void TakeItemsCollection()
{
	string itm2 = GetItemIDByOrder(false,nCurScroll2);
	int iq2 = GetItemQntByOrder(false,nCurScroll2);
	if(iq2<=0) return;

	int gp;	//JRH
	int gp1;
	int pb;
	int pg;
	int pg1;
	int mb;
	int mb1;
	int am1;
	int mb2;
	int p;	
	int s;

	aref mchref; makearef(mchref,Characters[GetMainCharacterIndex()]);
	
	// TIH --> bracket fixing 7-7-06
	if (itm2=="Money" || itm2=="gunpowder" || itm2=="pistolbullets" || itm2=="pistolgrapes" || itm2=="musketbullets") 
	{
		if(itm2=="Money")
		{
			mchref.Money = sti(mchref.Money)+iq2;
			g_refItems.Money = 0;
		}

		//JRH -->
		if(itm2=="gunpowder")
		{
			if(CheckAttribute(mchref,"equip."+FLASK_ITEM_TYPE) )
			{
				p = 12;
			}
			else
			{
				p = 6;
			}

			gp = GetCharacterItem(mchref, "gunpowder");
			if( gp >= p) 
			{
				TakeNItems(mchref,"gunpowder", -100);
				TakeNItems(mchref,"gunpowder", p);
			}
			else
			{
				gp = GetCharacterItem(mchref, "gunpowder");
				if(iq2 >= p - gp) gp1 = p - gp;
				if(iq2 < p - gp) gp1 = iq2; 				
				TakeNItems(GetMainCharacter(),itm2, gp1);
				// added by MAXIMUS [for gunpowder barrels] -->
				if(CANNONPOWDER_MOD && isGunpowder) {// TIH --> mod toggle 7-7-06
					int newQty;
					if(bAbordageStarted && !bCabinStarted && !bDeckStarted && !bCrewStarted)
					{
						newQty = sti(GetCargoGoods(boarding_enemy, GOOD_GUNPOWDER))-gp1;
						SetCharacterGoods(boarding_enemy, GOOD_GUNPOWDER, newQty);
					}
					else
					{
						newQty = sti(GetCargoGoods(GetMainCharacter(), GOOD_GUNPOWDER))-gp1;
						if(bDeckStarted) SetCharacterGoods(GetMainCharacter(), GOOD_GUNPOWDER, newQty);
					}
				}// TIH <-- mod toggle
				// added by MAXIMUS [for gunpowder barrels] <--
				g_refItems.Items.(itm2) = iq2 - gp1;
				if( iq2<=0 ) DeleteAttribute(g_refItems,"Items."+itm2);
			}
		}
		if(itm2=="pistolbullets" || itm2=="pistolgrapes" || itm2=="musketbullets") 
		{
			if(CheckAttribute(mchref,"equip."+POUCH_ITEM_TYPE) )
			{
				s = 12;
			}
			else
			{
				s = 6;
			}

			pb = GetCharacterItem(mchref, "pistolbullets");
			if(pb > s)
			{
				TakeNItems(mchref,"pistolbullets", -100);
				TakeNItems(mchref,"pistolbullets", s);	
			}
			if(pb <= s)
			{
				pb = GetCharacterItem(mchref, "pistolbullets");
				pg = GetCharacterItem(mchref, "pistolgrapes");
				if(pb + pg >= s)
				{
					pg1 = s - pb;
					TakeNItems(mchref,"pistolgrapes", -pg);
					TakeNItems(mchref,"pistolgrapes", pg1);
				}
				if(pb + pg1 < s) 
				{
					pb = GetCharacterItem(mchref, "pistolbullets");
					pg1 = GetCharacterItem(mchref, "pistolgrapes");
					mb = GetCharacterItem(mchref, "musketbullets");
					if(pb + pg1 + 2*mb >= s)
					{
						mb1 = (s - pb - pg1)/2;
						TakeNItems(mchref,"musketbullets", -mb);
						TakeNItems(mchref,"musketbullets", mb1);
					}
					if(pb + pg1 + 2*mb == s - 1)
					{
						if(itm2=="pistolbullets" || itm2=="pistolgrapes")
						{
							TakeNItems(GetMainCharacter(),itm2, 1);// 1 was iq2
							g_refItems.Items.(itm2) = iq2 - 1;
							if( iq2<=0 ) DeleteAttribute(g_refItems,"Items."+itm2);
						}
					}
					if(pb + pg1 + 2*mb < s - 1)
					{
						if(itm2=="pistolbullets" || itm2=="pistolgrapes" )
						{
							if(iq2 >= s - pb - pg1 - 2*mb) am1 = s - pb - pg1 - 2*mb;
							if(iq2 < s - pb - pg1 - 2*mb) am1 = iq2; 				
							TakeNItems(GetMainCharacter(),itm2, am1);
							g_refItems.Items.(itm2) = iq2 - am1;
							if( iq2<=0 ) DeleteAttribute(g_refItems,"Items."+itm2);
						}
						if(itm2=="musketbullets")
						{
							
							if(iq2 >= (s - pb - pg1 - 2*mb)/2) mb2 = (s - pb - pg1 - 2*mb)/2;
							if(iq2 < (s - pb - pg1 - 2*mb)/2) mb2 = iq2; 				
							TakeNItems(GetMainCharacter(),itm2, mb2);
							g_refItems.Items.(itm2) = iq2 - mb2;
							if( iq2<=0 ) DeleteAttribute(g_refItems,"Items."+itm2);
						}
					}
				}	
			}
		}
		//JRH <--

	}
	else
	{
		TakeNItems(GetMainCharacter(),itm2,iq2);
		DeleteAttribute(g_refItems,"Items."+itm2);
	}
	// TIH <-- bracket fixing

	FillScroll();
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"ONE_ITEMSLIST",-1);
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"TWO_ITEMSLIST",-1);
	SetUpDownUsed();
	SetScrollsUsing();
}
void GiveItemsCollection()
{
	string itm1 = GetItemIDByOrder(true,nCurScroll1);
	int iq1 = GetItemQntByOrder(true,nCurScroll1);
	if(iq1<=0) return;

	int gp;	//JRH not necessary to re-equip etc
	int gp1;
	int pb;
	int pg;
	int pg1;
	int mb;
	int mb1;
	int am1;
	int mb2;
	int p;	
	int s;

	aref mchref; makearef(mchref,Characters[GetMainCharacterIndex()]);

	// TIH --> bracket fixing 7-7-06
	if (itm1=="Money" || itm1=="gunpowder" || itm1=="pistolbullets" || itm1=="pistolgrapes" || itm1=="musketbullets") 
	{
	
		if(itm1=="Money")
		{
			mchref.Money = 0;
			g_refItems.Money = sti(g_refItems.Money)+iq1;
		}

		//JRH -->
		if(itm1=="gunpowder")
		{
			if ( IsOfficer(g_refitems) ) // JRH fix oct 06
			{
				if(CheckCharacterItem(g_refitems,"powderflask") > 0 )
				{
					p = 12;
				}
				else
				{
					p = 6;
				}
			}
			else p = 1000000;// changed from 1000 by MAXIMUS [for gunpowder barrels]

			gp = GetCharacterItem(g_refitems, "gunpowder");
			if( gp >= p) 
			{
				TakeNItems(g_refitems,"gunpowder", -100);
				TakeNItems(g_refitems,"gunpowder", p);
			}
			else
			{
				gp = GetCharacterItem(g_refitems, "gunpowder");
				if(iq1 >= p - gp) gp1 = p - gp;
				if(iq1 < p - gp) gp1 = iq1; 
				TakeNItems(GetMainCharacter(),itm1, -gp1);
				// added by MAXIMUS [for gunpowder barrels] -->
				if(CANNONPOWDER_MOD && isGunpowder) {// TIH --> mod toggle 7-7-06
					int newQty;
					if(bAbordageStarted && !bCabinStarted && !bDeckStarted && !bCrewStarted)
					{
						newQty = sti(GetCargoGoods(boarding_enemy, GOOD_GUNPOWDER))+gp1;
						SetCharacterGoods(boarding_enemy, GOOD_GUNPOWDER, newQty);
					}
					else
					{
						newQty = sti(GetCargoGoods(GetMainCharacter(), GOOD_GUNPOWDER))+gp1;
						if(bDeckStarted) SetCharacterGoods(GetMainCharacter(), GOOD_GUNPOWDER, newQty);
					}
				}// TIH <-- mod toggle
				// added by MAXIMUS [for gunpowder barrels] <--
				if( iq1<=0 ) DeleteAttribute(mchref,"Items."+itm1);
				if( CheckAttribute(g_refItems,"Items."+itm1) ) g_refItems.Items.(itm1) = gp1 + sti(g_refItems.Items.(itm1));
				else g_refItems.Items.(itm1) = gp1;
			}
		}

		if(itm1=="pistolbullets" || itm1=="pistolgrapes" || itm1=="musketbullets") 
		{
			if ( IsOfficer(g_refitems) ) // JRH fix oct 06
			{
				if(CheckCharacterItem(g_refitems,"ammopouch") > 0 )
				{
					s = 12;
				}
				else
				{
					s = 6;
				}
			}
			else s = 1000;

			pb = GetCharacterItem(g_refitems, "pistolbullets");
			// MAXIMUS 21.07.2006 [for correct bullets transfer] -->
			if(isGunpowder)
			{
				if(itm1=="pistolbullets")
				{
					if(GetCharacterItem(GetMainCharacter(), "pistolbullets")>=s)
					{
						TakeNItems(GetMainCharacter(),"pistolbullets", -s);
						TakeNItems(g_refitems,"pistolbullets", s);
					}
					else
					{
						TakeNItems(g_refitems,"pistolbullets", GetCharacterItem(GetMainCharacter(), "pistolbullets"));
						TakeNItems(GetMainCharacter(),"pistolbullets", -GetCharacterItem(GetMainCharacter(), "pistolbullets"));
					}
				}
				if(itm1=="pistolgrapes")
				{
					if(GetCharacterItem(GetMainCharacter(), "pistolgrapes")>=s)
					{
						TakeNItems(GetMainCharacter(),"pistolgrapes", -s);
						TakeNItems(g_refitems,"pistolgrapes", s);
					}
					else
					{
						TakeNItems(g_refitems,"pistolgrapes", GetCharacterItem(GetMainCharacter(), "pistolgrapes"));
						TakeNItems(GetMainCharacter(),"pistolgrapes", -GetCharacterItem(GetMainCharacter(), "pistolgrapes"));
					}
				}
				if(itm1=="musketbullets")
				{
					if(GetCharacterItem(GetMainCharacter(), "musketbullets")>=makeint(s/2))
					{
						TakeNItems(GetMainCharacter(),"musketbullets", -makeint(s/2));
						TakeNItems(g_refitems,"musketbullets", makeint(s/2));
					}
					else
					{
						TakeNItems(g_refitems,"musketbullets", GetCharacterItem(GetMainCharacter(), "musketbullets"));
						TakeNItems(GetMainCharacter(),"musketbullets", -GetCharacterItem(GetMainCharacter(), "musketbullets"));
					}
				}
			}
			else
			{// MAXIMUS 21.07.2006 [for correct bullets transfer] <--
			if(pb > s)
			{
				TakeNItems(g_refitems,"pistolbullets", -100);
				TakeNItems(g_refitems,"pistolbullets", s);	
			}
			if(pb <= s)
			{
				pb = GetCharacterItem(g_refitems, "pistolbullets");
				pg = GetCharacterItem(g_refitems, "pistolgrapes");
				if(pb + pg >= s)
				{
					pg1 = s - pb;
					TakeNItems(g_refitems,"pistolgrapes", -pg);
					TakeNItems(g_refitems,"pistolgrapes", pg1);
				}
				if(pb + pg1 < s) 
				{
					pb = GetCharacterItem(g_refitems, "pistolbullets");
					pg1 = GetCharacterItem(g_refitems, "pistolgrapes");
					mb = GetCharacterItem(g_refitems, "musketbullets");
					if(pb + pg1 + 2*mb >= s)
					{
						mb1 = (s - pb - pg1)/2;
						TakeNItems(g_refitems,"musketbullets", -mb);
						TakeNItems(g_refitems,"musketbullets", mb1);
					}
					if(pb + pg1 + 2*mb == s - 1)
					{
						if(itm1=="pistolbullets" || itm1=="pistolgrapes")
						{

							TakeNItems(GetMainCharacter(),itm1, -1);
							if( iq1<=0 ) DeleteAttribute(mchref,"Items."+itm1);
							if( CheckAttribute(g_refItems,"Items."+itm1) ) g_refItems.Items.(itm1) = 1 + sti(g_refItems.Items.(itm1));	
						}
					}
					if(pb + pg1 + 2*mb < s - 1)
					{
						if(itm1=="pistolbullets" || itm1=="pistolgrapes")
						{
							if(iq1 >= s - pb - pg1 - 2*mb) am1 = s - pb - pg1 - 2*mb;
							if(iq1 < s - pb - pg1 - 2*mb) am1 = iq1; 
							TakeNItems(GetMainCharacter(),itm1, -am1);
							if( iq1<=0 ) DeleteAttribute(mchref,"Items."+itm1);
							if( CheckAttribute(g_refItems,"Items."+itm1) ) g_refItems.Items.(itm1) = am1 + sti(g_refItems.Items.(itm1));
							else	g_refItems.Items.(itm1) = am1;
						}
						if(itm1=="musketbullets")
						{
							if(iq1 >= (s - pb - pg1 - 2*mb)/2) mb2 = (s - pb - pg1 - 2*mb)/2;
							if(iq1 < (s - pb - pg1 - 2*mb)/2) mb2 = iq1; 
							TakeNItems(GetMainCharacter(),itm1, -mb2);
							if( iq1<=0 ) DeleteAttribute(mchref,"Items."+itm1);
							if( CheckAttribute(g_refItems,"Items."+itm1) ) g_refItems.Items.(itm1) = mb2 + sti(g_refItems.Items.(itm1));
							else	g_refItems.Items.(itm1) = mb2;	
						}
					}
				}	
			}
			}
		}
		//JRH <--

	}
	else
	{
		DeleteAttribute(mchref,"Items."+itm1);
		if( CheckAttribute(g_refItems,"Items."+itm1) ) g_refItems.Items.(itm1) = iq1 + sti(g_refItems.Items.(itm1));
		else	g_refItems.Items.(itm1) = iq1;
	}
	// TIH <-- bracket fixing


	FillScroll();
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"ONE_ITEMSLIST",-1);
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"TWO_ITEMSLIST",-1);
	SetUpDownUsed();
	SetScrollsUsing();
}

void GetDescribeData(ref nameStr, ref descrStr)
{
	//int tmpLangFileID = LanguageOpenFile("interface_strings.txt");
	aref arItem;
	//string descrStr = "";
	string Quality = ""; // NK
	string newLineStr = TranslateString("","new_string"); // NK
	bool bYesDescribe = false;

// KK -->
	nameStr = "";
	descrStr = "";
// <-- KK

	if(oldCurScrollName=="ONE_ITEMSLIST") {
		bYesDescribe = GetItemRefByOrder(true,nCurScroll1,&arItem);
	}
	if(oldCurScrollName=="TWO_ITEMSLIST") {
		bYesDescribe = GetItemRefByOrder(false,nCurScroll2,&arItem);
	}

	if( !bYesDescribe ) {
		// KK SetFormatedText("DESCRIBEWINDOW","");
		bEnableDown = false;
		//LanguageCloseFile(tmpLangFileID); // KK
		return;
	}
	// NK -->
	if( CheckAttribute(arItem,"name") ) // NK, was checking attr "describe"
	{
		nameStr = TranslateString("",arItem.name);
	}
	// NK <--
	if( CheckAttribute(arItem,"groupID") )
	{
		// NK -->
		if (CheckAttribute(arItem,"QualityName")) { Quality = arItem.QualityName; nameStr = TranslateString("",Quality) + " " + nameStr; }
		//GameInterface.strings.itemname = descrStr;
		//descrStr += newLineStr;
		// NK <--
		if(arItem.groupID==GUN_ITEM_TYPE)
		{
			descrStr = GetAssembledString(TranslateString("","weapon gun parameters"), arItem);
		}
		if(arItem.groupID==BLADE_ITEM_TYPE)
		{
			descrStr = GetAssembledString(TranslateString("","weapon blade parameters"), arItem);
		}
		// GreatZen -->
		if(arItem.groupID==ARMOR_ITEM_TYPE)
		{
			descrStr = GetAssembledString(TranslateString("","weapon armor parameters"),arItem);
		}
		// GreatZen <--
	}
	if( CheckAttribute(arItem,"describe") )
	{
		if(descrStr!="") { descrStr += TranslateString("","new_string"); }
		descrStr += TranslateString("",arItem.describe) + GetItemBonuses(arItem.id); // NK
	}

	// KK SetFormatedText("DESCRIBEWINDOW",descrStr);
	//LanguageCloseFile(tmpLangFileID); // KK
	bEnableDown = SendMessage(&GameInterface,"lsll",MSG_INTERFACE_MSG_TO_NODE,"DESCRIBEWINDOW", 6, 0);
	return;
}

//==============================
// items utilites
//==============================
string GetItemIDByOrder(bool my,int num)
{
	string attrName = "pic"+(num+1);
	if(my)
	{
		if( CheckAttribute(&GameInterface,"one_itemslist."+attrName+".itemID") )
			return GameInterface.one_itemslist.(attrName).itemID;
	}
	else
	{
		if( CheckAttribute(&GameInterface,"two_itemslist."+attrName+".itemID") )
			return GameInterface.two_itemslist.(attrName).itemID;
	}
	return "";
}

bool GetItemRefByOrder(bool my,int num, ref refItem)
{
	aref ar;
	string itemID = GetItemIDByOrder(my,num);
	if(itemID=="Money") itemID=GOLD;
	if( Items_FindItem(itemID,&ar)<0 ) return false;
	refItem = ar;
	return true;
}

int GetItemQntByOrder(bool my,int num)
{
	string itmName = GetItemIDByOrder(my,num);
	if(my)
	{
		if(itmName=="Money")
		{
			return sti(Characters[GetMainCharacterIndex()].Money);
		}
		return GetCharacterItem(GetMainCharacter(),itmName);
	}
	if(itmName=="Money") return sti(g_refItems.Money);
	if( CheckAttribute(g_refItems,"items."+itmName) )
		return sti(g_refItems.items.(itmName));
	return 0;
}

bool CheckConsumedItem(string itemID)
{
	aref arone;
	makearef(arone,Characters[GetMainCharacterIndex()].items);
	aref artwo;
	makearef(artwo,g_refItems.items);

	int n=0;
	if( CheckAttribute(arone,itemID) ) n = sti(arone.(itemID));
	if( CheckAttribute(artwo,itemID) ) n = n + sti(artwo.(itemID));

	if(n>1) return true;
	return false;
}

bool CanCharacterTakeItem(bool myCh,string itemName)
{
	if(!myCh) return true;
	aref chref = GetMainCharacter();
//	makearef(chref,Characters[GetMainCharacterIndex()]);
	if( CheckAttribute(chref,"items."+itemName) ) return true;
	if( GetChrItemQuantity(GetMainCharacter())<MAX_ITEM_TAKE ) return true;
	return false;
}

void SetUpDownUsed()
{
	aref iref; makearef(iref,Characters[GetMainCharacterIndex()].items);

	string itmName1 = GetItemIDByOrder(true,nCurScroll1);
	int iq1 = GetItemQntByOrder(true,nCurScroll1);
	string itmName2 = GetItemIDByOrder(false,nCurScroll2);
	int iq2 = GetItemQntByOrder(false,nCurScroll2);

	if(iq1<=0)
	{
		SetSelectable("ONEDOWN_BUTTON",false);
		SetSelectable("ALLDOWN_BUTTON",false);
	}
	else
	{
		SetSelectable("ONEDOWN_BUTTON",true);
		SetSelectable("ALLDOWN_BUTTON",true);
	}

	if(iq2<=0)
	{
		SetSelectable("ONEUP_BUTTON",false);
		SetSelectable("ALLUP_BUTTON",false);
	}
	else
	{
		if( CheckAttribute(iref,itmName2) || GetAttributesNum(iref)<MAX_ITEM_TAKE )
		{
			SetSelectable("ONEUP_BUTTON",true);
			SetSelectable("ALLUP_BUTTON",true);
		}
		else
		{
			SetSelectable("ONEUP_BUTTON",false);
			SetSelectable("ALLUP_BUTTON",false);
		}
	}

	if(itmName2=="" || itmName2=="Money" || GetAttributesNum(iref)<MAX_ITEM_TAKE)
	{
		SetSelectable("SWAP_BUTTON",true);
	}
	else
	{
		if( CheckAttribute(iref,itmName2) )	SetSelectable("SWAP_BUTTON",true);
		else
		{
			if(itmName1!="" && itmName1!="Money") SetSelectable("SWAP_BUTTON",true);
			else SetSelectable("SWAP_BUTTON",false);
		}
	}

	if( IsQuestUsedItem(itmName1) )
	{
		SetSelectable("ONEDOWN_BUTTON",false);
		SetSelectable("ALLDOWN_BUTTON",false);
	}

	if (ENABLE_AMMOMOD) {// TIH --> mod toggle 7-7-06
		// added by MAXIMUS [for gunpowder barrels] -->
		if(isGunpowder)
		{// MAXIMUS 21.07.2006: checking for bullets
			if(GetItemIDByOrder(true,nCurScroll1) != "gunpowder" && GetItemIDByOrder(true,nCurScroll1) != "pistolgrapes" && GetItemIDByOrder(true,nCurScroll1) != "pistolbullets" && GetItemIDByOrder(true,nCurScroll1) != "musketbullets")
			{
				SetSelectable("ONEDOWN_BUTTON",false);
				SetSelectable("ALLDOWN_BUTTON",false);
			}
			SetSelectable("SWAP_BUTTON",false);
		}
		// added by MAXIMUS [for gunpowder barrels] <--
	}// TIH <-- mod toggle

	ThatOneAndEquiped(itmName1,itmName2);

	if(!GetSelectable(GetCurrentNode()))	SetCurrentNode(oldCurScrollName);
}

void SelectButtonLeft(string startBtnNode)
{
	if(GetSelectable(startBtnNode))	SetCurrentNode(startBtnNode);
	else
	{
		switch(startBtnNode)
		{
		case "ONEDOWN_BUTTON":	SelectButtonLeft("INFO_BUTTON"); break;
		case "ONEUP_BUTTON":	SelectButtonLeft("ONEDOWN_BUTTON"); break;
		case "SWAP_BUTTON":		SelectButtonLeft("ONEUP_BUTTON"); break;
		case "ALLUP_BUTTON":	SelectButtonLeft("SWAP_BUTTON"); break;
		case "ALLDOWN_BUTTON":	SelectButtonLeft("ALLUP_BUTTON"); break;
		}
	}
}

void SelectButtonRight(string startBtnNode)
{
	if(GetSelectable(startBtnNode))	SetCurrentNode(startBtnNode);
	else
	{
		switch(startBtnNode)
		{
		case "INFO_BUTTON":		SelectButtonRight("ONEDOWN_BUTTON"); break;
		case "ONEDOWN_BUTTON":	SelectButtonRight("ONEUP_BUTTON"); break;
		case "ONEUP_BUTTON":	SelectButtonRight("SWAP_BUTTON"); break;
		case "SWAP_BUTTON":		SelectButtonRight("ALLUP_BUTTON"); break;
		case "ALLUP_BUTTON":	SelectButtonRight("ALLDOWN_BUTTON"); break;
		}
	}
}

void ThatOneAndEquiped(string itmName1, string itmName2)
{
	if( !GetRemovable(g_refItems) )
	{
		SetSelectable("ONEDOWN_BUTTON",false);
		SetSelectable("ALLDOWN_BUTTON",false);
		SetSelectable("SWAP_BUTTON",false);
		SetSelectable("ONEUP_BUTTON",false);
		SetSelectable("ALLUP_BUTTON",false);
		return;
	}

	if( IsEquipCharacterByItem(GetMainCharacter(),itmName1) )
	{
		if( GetCharacterItem(GetMainCharacter(),itmName1)<=1 )
		{
			SetSelectable("ONEDOWN_BUTTON",false);
		}
		SetSelectable("ALLDOWN_BUTTON",false);
		SetSelectable("SWAP_BUTTON",false);
	}

	if( IsEquipCharacterByItem(g_refItems,itmName2) )
	{
		if( GetCharacterItem(g_refItems,itmName2)<=1 )
		{
			SetSelectable("ONEUP_BUTTON",false);
		}
		SetSelectable("ALLUP_BUTTON",false);
		SetSelectable("SWAP_BUTTON",false);
	}
}

void SetScrollsUsing()
{
	if( CheckAttribute(&GameInterface,"two_itemslist.pic1") ) {
		SendMessage(&GameInterface,"lsll", MSG_INTERFACE_MSG_TO_NODE,"TWO_ITEMSLIST",0, true);
		SetSelectable("TWO_ITEMSLIST",true);
		SetSelectable("TWO_LEFTSCROLLBUTTON",true);
		SetSelectable("TWO_RIGHTSCROLLBUTTON",true);
	} else {
		SendMessage(&GameInterface,"lsll", MSG_INTERFACE_MSG_TO_NODE,"TWO_ITEMSLIST",0, false);
		SetSelectable("TWO_ITEMSLIST",false);
		SetSelectable("TWO_LEFTSCROLLBUTTON",false);
		SetSelectable("TWO_RIGHTSCROLLBUTTON",false);
		if( GetCurrentNode()=="TWO_ITEMSLIST" ) {
			SetCurrentNode("ONE_ITEMSLIST");
		}
	}

	if( CheckAttribute(&GameInterface,"one_itemslist.pic1") ) {
		SendMessage(&GameInterface,"lsll", MSG_INTERFACE_MSG_TO_NODE,"ONE_ITEMSLIST",0, true);
		SetSelectable("ONE_ITEMSLIST",true);
		SetSelectable("ONE_LEFTSCROLLBUTTON",true);
		SetSelectable("ONE_RIGHTSCROLLBUTTON",true);
	} else {
		SendMessage(&GameInterface,"lsll", MSG_INTERFACE_MSG_TO_NODE,"ONE_ITEMSLIST",0, false);
		SetSelectable("ONE_ITEMSLIST",false);
		SetSelectable("ONE_LEFTSCROLLBUTTON",false);
		SetSelectable("ONE_RIGHTSCROLLBUTTON",false);
		if( GetCurrentNode()=="ONE_ITEMSLIST" ) {
			if( GetSelectable("TWO_ITEMSLIST") ) {SetCurrentNode("TWO_ITEMSLIST");}
		}
	}
}

void GoToTopScroll()
{
	if( GetSelectable("ONE_ITEMSLIST") ) SetCurrentNode("ONE_ITEMSLIST");
}

void GoToBottomScroll()
{
	if( GetSelectable("TWO_ITEMSLIST") ) SetCurrentNode("TWO_ITEMSLIST");
}

// KK -->
void ShowDescription(bool bShow)
{
	if (bShow) {
			string itmname, itmdescr;
			GetDescribeData(&itmname, &itmdescr);
			if (itmname == "" && itmdescr == "") return;
			SetNodeUsing("SHOW_DESCRIPTION_RECTANGLE",true);
			SetNodeUsing("ITEMNAME_TEXT", true);
			SetNodeUsing("DESCRIPTION_TEXT",true);
			SetNodeUsing("SHOW_DESCRIPTION_OK_BUTTON",true);
			SetCurrentNode("SHOW_DESCRIPTION_OK_BUTTON");
			SetFormatedText("ITEMNAME_TEXT", itmname);
			SetFormatedText("DESCRIPTION_TEXT", itmdescr);
			SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 1, "SHOW_DESCRIPTION_OK_BUTTON");
	} else {
			SendMessage(&GameInterface,"ll", MSG_INTERFACE_LOCK_NODE, 0);
			SetNodeUsing("SHOW_DESCRIPTION_RECTANGLE", false);
			SetNodeUsing("ITEMNAME_TEXT", false);
			SetNodeUsing("DESCRIPTION_TEXT", false);
			SetNodeUsing("SHOW_DESCRIPTION_OK_BUTTON", false);
			SetCurrentNode("INFO_BUTTON");
	}
}
// <-- KK
