<?xml version="1.0" encoding="utf-8"?><waypoints>
<onLoad>
	testItemId = 240181;
	testItem = GetIdName(testItemId);
	
	testNpc = 110752;  -- Marlis Sister (Varanas West)

	-- preconditions: 
	-- char inventory must not contain the item being tested
	-- char inventory must have at least six free slots 
	-- bank must contain a single stack of the item being tested.
	-- char must be standing near to Marlis Sister (Varanas West)

function printResult(testName, result, status)
	if result then
		cprintf(cli.yellow,"Testing %s - result: %s\r\n",testName, status);
	else
		cprintf(cli.red,"Testing %s - result: %s\r\n",testName, status);
	end
end

	
function findEmptySlot(location)  -- returns the first empty slot
    location:update();
	for i, item in pairs(location.BagSlot) do
	   if item.Id == 0 and item.SlotNumber > 60 then
			return item.SlotNumber
		end
	end
	return -1
end

function buildItemTableFor(itemId, location)
   local result = {};
   local count = 1;
   if not location then
      location = inventory
   end
   for i, item in pairs(location.BagSlot) do
	   if item.Id == itemId then
			result[count]=item;
            count = count + 1;
	   end
   end
   return result;
end

function checkTestBankPreconditions(testItemId,npcId)
    local item, items;
    local occupiedSlots, availableSlots, totalSlots = RoMScript("GetBagCount()");
	
	-- test that we have at least 6 empty bag slots
	if 6 > availableSlots then
		printf("Please make sure that you have at least six empty bag slots\r\n");
        return false, "wrong setup"		
	end
		
	-- test that we don't have items in our bag
    item = inventory:findItem(testItemId);
	if item then
		printf("Please remove all "..GetIdName(testItemId).." from your bag, before running this test\r\n");
        return false, "wrong setup"		
	end
	
	-- test that the bank does contain a single of our test items
	items = buildItemTableFor(testItemId, bank);
	if table.getn(items) ~= 1 then
		return false, "bank does not contain the expected item stack"
	end
	
	-- test if we are near to the bank npc
	if player:findNearestNameOrId(npcId) == nil then
		printf("Please get next to "..GetIdName(npcId).." before running this test\r\n");
		return false, "wrong setup"
	end
	return true, "setup passed"
end
	
	-- test move item to bag
function testBankAccess1(testItemId, npcId)
    local item, bankItem

    -- close all windows 
	RoMScript("CloseAllWindows()");
	
    -- open bank
	player:target_NPC(npcId); -- Marlis Sister (Varanas West)
	sendMacro("ChoiceOption(5);");   
	yrest(1000);               -- wait for client to update bank 

	-- take item from bank
	bankItem = bank:findItem(testItemId);
	if bankItem then
	  print("moving "..GetIdName(bankItem.Id).." to inventory");   
	  bankItem:moveTo("bags");
	end
	
	yrest(1000);
	inventory:update();
	bank:update();
	-- check that bank does not contain that item any more
	bankItem = bank:findItem(testItemId);
	if bankItem then
		return false, "bank should be no longer contain item"
	end
	-- check if item can be found in inventory
	item = inventory:findItem(testItemId)
	if item == nil then
		return false, "move from bank to bags failed"
	end
	return true, "testBankAccess1 passed"
	
end

    -- test moving items to bank
function testBankAccess2(testItemId, npcId)
    local item, bankItem, items;

    -- close all windows 
	RoMScript("CloseAllWindows()");
	RoMScript("BagFrame:Show()");
	inventory:update();
	
	-- split the local stack
	item = inventory:findItem(testItemId);
	if item == nil then
		return false, "item not found"
	end
	for i=1,5 do
		emptySlot = findEmptySlot(inventory);
		if emptySlot == -1 then
		   return false, "no empty slot found"
		end
		RoMScript("SplitBagItem("..tostring(item.SlotNumber)..",1)");
		yrest(250);
		inventory.BagSlot[emptySlot]:pickup();
		yrest(250);
	end

	yrest(1000);
	inventory:update();
	items = buildItemTableFor(testItemId);
	if table.getn(items) ~= 6 then
	    printf("table size %i\r\n", table.getn(items));
		return false, "splitting failed"
	end
	
	-- now move the stacks back to bank
	player:target_NPC(npcId); 
	sendMacro("ChoiceOption(5);");   
	yrest(1000);
	
	bank:update();
	for j, element in pairs(items) do
		element:moveTo("bank");
		yrest(250);
    end
	
	-- check that item is no longer found in inventory
	item = inventory:findItem(testItemId)
	if item then
		return false, "move from bag to bank failed"
	end
	
	yrest(1000)
	bank:update();
	-- check that item can be found in bank
	items = buildItemTableFor(testItemId, bank);
	if table.getn(items) ~= 1 then
		return false, "bank does not contain the expected item stack"
	end
	return true, "testBankAccess2 passed"
end

testResult, status = checkTestBankPreconditions(240181, 110752)
printf("Testing preconditions returned: %s\r\n",status);
if testResult then 
	testResult, status = testBankAccess1(240181, 110752);
	printResult("bank access (1)", testResult, status);
	testResult, status = testBankAccess2(240181, 110752);
	printResult("bank access (2)", testResult, status);
end

</onLoad>
</waypoints>