//Script Name: inv_system_ou
//////////////////////////////////////////
//Created By: Genisys (Guile)
//Created On: 8/01/08
//Updated On: 8/20/08
//////////////////////////////////////////////////////////////////
/*
This script is must be in the Onused event of the placeable object
in the Inventory Room.  NOTE: The user MUST be in that room, or
the system may error due to lagg! (This is paramount!)

*/
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
//PROTOTYPES DECLARED
int GetNum(object oTarget);
void ClearBag(object oTarget);
void CheckBox(object oTarget);
void Seperate(object oTarget);
void Disperse(object oTarget);
void ReturnUndroppables(object oTarget);
void ClearAll();
void SortBoxes();
void StartSorting(object oTarget);
void SortHoldingTank(object oTarget);
void SortContainer(object oTarget);
void SortBox(object oTarget);
void GiveAll(object oTarget);
void StartUp();
//////////////////////////////////////////////////////////

//Main Script
void main()
{

//Declare All Major Variables
 object oPC = GetLastUsedBy();
 object oNPC;  //The Holding Box
 object oSelf = OBJECT_SELF;
 object oItem;

if(GetIsDM(oPC)||GetIsDMPossessed(oPC))
{
SendMessageToAllDMs("DMs Cannot Use The Inventory Organizer, Sorry!");
return;
}

//If anyone is using this system..
if(GetLocalInt(GetModule(), "ORGANIZING")==1)
{
 FloatingTextStringOnCreature("This system is busy, try again later", oPC, FALSE);
//Stop the script here!
return;
}

//Since it's not in use..
else
{

//If this system is in use, tell them to try later.. (A Second Check)
if(GetLocalInt(oSelf, "INUSENOW")==1)
{
 FloatingTextStringOnCreature("System in use, try again later.", oPC, FALSE);
//Stop here!
return;
}

 //Otherwise allow them to use it and set that it's in use
 else
 {
  SetLocalInt(oSelf, "INUSENOW", 1);
 }

//Else statement end..
}

//Continue with the script...

//Otherwise set that it is in use
SetLocalInt(GetModule(), "ORGANIZING", 1);

//This is an important fix for Module Events OnAcquire / OnUnAcquire
//A lot of items are beinging taken / given, so it would fire
//these Module Event Scripts repeatedly, which is REAL bad!
SetLocalInt(oPC, "ORGANIZING", 1);


/////////////////////////////////////////////////////////////////////////////
////////////////////////////MAIN FUNCTIONS///////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

 oNPC = GetObjectByTag("ihold4u");

 //This must be present or we will not be able to continue!
 ClearAll();

 SendMessageToAllDMs("ATTENTION: Inventory System is in use!");

 //First take all of the items from the PC, cause this takes
 //the longest it must be delayed the most!
 oItem = GetFirstItemInInventory(oPC);
 while(GetIsObjectValid(oItem))
 {

  AssignCommand(oNPC, ActionTakeItem(oItem, oPC));

 oItem = GetNextItemInInventory(oPC);
 //While Loop End
 }

 //Next, Start sorting the boxes out one by one..
 //This is the minimum delay! (Do not set it lower than 16.1 )
 DelayCommand(16.1, StartUp());

 //Finally set all of the variables back to 0

 //All of these delays MUST be no less than 46.5 Seconds!

 //Allow the Control to be used again afte some time..
 DelayCommand(69.5, SetLocalInt(oSelf, "INUSENOW", 0));

 //Allow the system to be useable again after some time..
 DelayCommand(69.7, SetLocalInt(GetModule(), "ORGANIZING", 0));

 //Take the variable off of the player so the events fire on them again.
 DelayCommand(69.9, SetLocalInt(oPC, "ORGANIZING", 0));

 //Tell the PC the system is finished, they can now use it again.
 DelayCommand(70.7, FloatingTextStringOnCreature(
 "Organizing Complete.", oPC, TRUE));

//Main Script End
}

///////////////////////////////////////////////////////////////////////////
////////////////////DEFINE ALL PROTOTYPES/////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//DON'T TOUCH ANYTHING BELOW UNLESS YOUR AN ADVANCED SCRIPTER!!!////////
///////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////
//PROTOTYPE DEFINED
// Return the number of items oTarget possesses in thier inventory
int GetNum(object oTarget)
{
    int nNum = 0;
    object oItem = GetFirstItemInInventory(oTarget);

    while (GetIsObjectValid(oItem))
    {
        //Add +1 for every item..
        nNum = nNum +1;

        oItem = GetNextItemInInventory(oTarget);
    }

   if(nNum>=1)
   {
   return TRUE;
   }
   else
   {
   return FALSE;
   }

//END PROTOTYPE
}

//////////////////////////////////////////////////////////////
//PROTOTYPE DEFINED

void ClearBag(object oTarget)
{

//NOTE: oTarget = oPC
//AssignCommand(oTarget, ClearAllActions());

object oNPC = GetObjectByTag("ihold4u");
object oItem;

//First, take all items from inside of bags first!
 oItem = GetFirstItemInInventory(oTarget);
 while(GetIsObjectValid(oItem))
 {
  //Have the NPC (Box) take all of the items out of the bag
  AssignCommand(oNPC, ActionTakeItem(oItem, oTarget));

  oItem = GetNextItemInInventory(oTarget);
 }

//PROTOTYPE END
}

////////////////////////////////////////////////////////////////////////
//PROTOTYPE DEFINED

void CheckBox(object oTarget)
{

object oBox = oTarget;

 //We only continue if the box has items!
if(GetNum(oBox)>=TRUE && GetNum(oBox)!=FALSE)
{
 DelayCommand(0.1, SortBox(oTarget));
}

//PROTOTYPE END
}

////////////////////////////////////////////////////////////////////////
//PROTOTYPE DEFINED
void Seperate(object oTarget)
{

//We are going to take items in bags with this function.

//NOTE: oTarget = The bag which we identified in Clear Bags

 //Declare major variables
 object oBox; object oBox1; object oBox2; object oBox3; object oBox4;
 object oBox5; object oBox6;object oBox7; object oItem;

//Now lets deal with the items in the bags
oItem = GetFirstItemInInventory(oTarget);
while(GetIsObjectValid(oItem))
{

 //If in fact the item in Undroppable, then give it to the ibox..
 if(GetItemCursedFlag(oItem))
 {
  oBox1 = GetObjectByTag("plotbox");
  AssignCommand(oBox1, ActionTakeItem(oItem, oTarget));
 }

  //Serpate ALL Potions, Scrolls, & Healer's Kits into the potbox
  else if(GetBaseItemType(oItem)==BASE_ITEM_POTIONS ||
   GetBaseItemType(oItem)==BASE_ITEM_ENCHANTED_POTION ||
   GetBaseItemType(oItem)==BASE_ITEM_ENCHANTED_SCROLL ||
   GetBaseItemType(oItem)==BASE_ITEM_HEALERSKIT ||
   GetBaseItemType(oItem)==BASE_ITEM_SPELLSCROLL)
  {
   oBox2 = GetObjectByTag("potbox");
   AssignCommand(oBox2, ActionTakeItem(oItem, oTarget));
  }

  //Put all weapons in the weapon box..
  else if(GetBaseItemType(oItem)== BASE_ITEM_BASTARDSWORD ||
   GetBaseItemType(oItem)== BASE_ITEM_BATTLEAXE ||
   GetBaseItemType(oItem)== BASE_ITEM_CLUB ||
   GetBaseItemType(oItem)== BASE_ITEM_DAGGER ||
   GetBaseItemType(oItem)== BASE_ITEM_DIREMACE ||
   GetBaseItemType(oItem)== BASE_ITEM_DOUBLEAXE ||
   GetBaseItemType(oItem)== BASE_ITEM_DWARVENWARAXE ||
   GetBaseItemType(oItem)== BASE_ITEM_GREATAXE ||
   GetBaseItemType(oItem)== BASE_ITEM_GREATSWORD ||
   GetBaseItemType(oItem)== BASE_ITEM_HALBERD ||
   GetBaseItemType(oItem)== BASE_ITEM_HANDAXE ||
   GetBaseItemType(oItem)== BASE_ITEM_HEAVYFLAIL ||
   GetBaseItemType(oItem)== BASE_ITEM_KAMA ||
   GetBaseItemType(oItem)== BASE_ITEM_KATANA ||
   GetBaseItemType(oItem)== BASE_ITEM_KUKRI ||
   GetBaseItemType(oItem)== BASE_ITEM_LIGHTFLAIL ||
   GetBaseItemType(oItem)== BASE_ITEM_LIGHTHAMMER ||
   GetBaseItemType(oItem)== BASE_ITEM_LIGHTMACE ||
   GetBaseItemType(oItem)== BASE_ITEM_LONGSWORD ||
   GetBaseItemType(oItem)== BASE_ITEM_MORNINGSTAR ||
   GetBaseItemType(oItem)== BASE_ITEM_QUARTERSTAFF ||
   GetBaseItemType(oItem)== BASE_ITEM_RAPIER ||
   GetBaseItemType(oItem)== BASE_ITEM_SCIMITAR ||
   GetBaseItemType(oItem)== BASE_ITEM_SCYTHE ||
   GetBaseItemType(oItem)== BASE_ITEM_SHORTSPEAR ||
   GetBaseItemType(oItem)== BASE_ITEM_SHORTSWORD ||
   GetBaseItemType(oItem)== BASE_ITEM_SICKLE ||
   GetBaseItemType(oItem)== BASE_ITEM_TRIDENT ||
   GetBaseItemType(oItem)== BASE_ITEM_TWOBLADEDSWORD ||
   GetBaseItemType(oItem)== BASE_ITEM_WARHAMMER ||
   GetBaseItemType(oItem)== BASE_ITEM_WHIP ||
   GetBaseItemType(oItem)== BASE_ITEM_HEAVYCROSSBOW ||
   GetBaseItemType(oItem)== BASE_ITEM_LIGHTCROSSBOW ||
   GetBaseItemType(oItem)== BASE_ITEM_LONGBOW ||
   GetBaseItemType(oItem)== BASE_ITEM_SHORTBOW ||
   GetBaseItemType(oItem)== BASE_ITEM_SLING)
  {
  oBox3 = GetObjectByTag("wepbox");
  AssignCommand(oBox3, ActionTakeItem(oItem, oTarget));
  }


  //Seperate the Armor & Worn Items into the armbox
  else if(GetBaseItemType(oItem)==BASE_ITEM_AMULET ||
   GetBaseItemType(oItem)==BASE_ITEM_ARMOR ||
   GetBaseItemType(oItem)==BASE_ITEM_BELT ||
   GetBaseItemType(oItem)==BASE_ITEM_BOOTS ||
   GetBaseItemType(oItem)==BASE_ITEM_BRACER ||
   GetBaseItemType(oItem)==BASE_ITEM_CLOAK ||
   GetBaseItemType(oItem)==BASE_ITEM_GLOVES ||
   GetBaseItemType(oItem)==BASE_ITEM_HELMET ||
   GetBaseItemType(oItem)==BASE_ITEM_LARGESHIELD ||
   GetBaseItemType(oItem)==BASE_ITEM_RING ||
   GetBaseItemType(oItem)==BASE_ITEM_SMALLSHIELD ||
   GetBaseItemType(oItem)==BASE_ITEM_TOWERSHIELD)
  {
   oBox4 = GetObjectByTag("armbox");
   AssignCommand(oBox4, ActionTakeItem(oItem, oTarget));
  }

  //now let's seperate all the miscellaneous items
  //Grenades, Trapkits, and Thieving Tools into the oBox
  else if(GetBaseItemType(oItem)==BASE_ITEM_MISCSMALL ||
   GetBaseItemType(oItem)==BASE_ITEM_MISCMEDIUM ||
   GetBaseItemType(oItem)==BASE_ITEM_MISCLARGE ||
   GetBaseItemType(oItem)==BASE_ITEM_MISCTHIN ||
   GetBaseItemType(oItem)==BASE_ITEM_MISCWIDE ||
   GetBaseItemType(oItem)==BASE_ITEM_MISCTALL ||
   GetBaseItemType(oItem)==BASE_ITEM_GRENADE ||
   GetBaseItemType(oItem)==BASE_ITEM_THIEVESTOOLS ||
   GetBaseItemType(oItem)==BASE_ITEM_TRAPKIT)
  {
   oBox5 = GetObjectByTag("obox");
   AssignCommand(oBox5, ActionTakeItem(oItem, oTarget));
  }

  //Put all ammunitions in the ammobox
  else if(GetBaseItemType(oItem)== BASE_ITEM_ARROW ||
          GetBaseItemType(oItem)== BASE_ITEM_BOLT ||
          GetBaseItemType(oItem)== BASE_ITEM_BULLET ||
          GetBaseItemType(oItem)== BASE_ITEM_DART ||
          GetBaseItemType(oItem)== BASE_ITEM_SHURIKEN ||
          GetBaseItemType(oItem)== BASE_ITEM_THROWINGAXE)
  {
  oBox6 = GetObjectByTag("ammobox");
  AssignCommand(oBox6, ActionTakeItem(oItem, oTarget));
  }

  //Seperate all Rods / Staves / Wands into the srwbox
  else if(GetBaseItemType(oItem)==BASE_ITEM_MAGICROD ||
   GetBaseItemType(oItem)==BASE_ITEM_MAGICSTAFF ||
   GetBaseItemType(oItem)==BASE_ITEM_MAGICWAND)
  {
   oBox7 = GetObjectByTag("rswbox");
   AssignCommand(oBox7, ActionTakeItem(oItem, oTarget));
  }

 //Otherwise put the item in the ibox!
 else
 {
  oBox = GetObjectByTag("ibox");
  AssignCommand(oBox, ActionTakeItem(oItem, oTarget));
 }

 //Continue the loop
 oItem = GetNextItemInInventory(oTarget);

//While loop end
}

//PROTOTYPE END
}

////////////////////////////////////////////////////////////////////////
//PROTOTYPE DEFINED
void Disperse(object oTarget)
{

 //This function will take the rest of the items on the PC..

//NOTE: oTarget = an Item

 //Declare major variables
 object oPC = GetLastUsedBy();
 object oBox; object oBox1; object oBox2; object oBox3; object oBox4;
 object oBox5; object oBox6;object oBox7;

 object oItem = oTarget;

//We don't want to take bags (just in case!)
if(!GetHasInventory(oItem))
{
   //If in fact the item in Undroppable, give it to the iBox
   if(GetItemCursedFlag(oItem)==TRUE)
   {
    oBox1 = GetObjectByTag("plotbox");
    AssignCommand(oBox1, ActionTakeItem(oItem, oPC));
   }

   //Serpate ALL Potions, Scrolls, & Healer's Kits into the potbox
   else if((GetBaseItemType(oItem)==BASE_ITEM_POTIONS ||
    GetBaseItemType(oItem)==BASE_ITEM_ENCHANTED_POTION ||
    GetBaseItemType(oItem)==BASE_ITEM_ENCHANTED_SCROLL ||
    GetBaseItemType(oItem)==BASE_ITEM_HEALERSKIT ||
    GetBaseItemType(oItem)==BASE_ITEM_SPELLSCROLL))
   {
    oBox2 = GetObjectByTag("potbox");
    AssignCommand(oBox2, ActionTakeItem(oItem, oPC));
   }

   //Put all weapons in the weapon box..
   else if((GetBaseItemType(oItem)== BASE_ITEM_BASTARDSWORD ||
    GetBaseItemType(oItem)== BASE_ITEM_BATTLEAXE ||
    GetBaseItemType(oItem)== BASE_ITEM_CLUB ||
    GetBaseItemType(oItem)== BASE_ITEM_DAGGER ||
    GetBaseItemType(oItem)== BASE_ITEM_DIREMACE ||
    GetBaseItemType(oItem)== BASE_ITEM_DOUBLEAXE ||
    GetBaseItemType(oItem)== BASE_ITEM_DWARVENWARAXE ||
    GetBaseItemType(oItem)== BASE_ITEM_GREATAXE ||
    GetBaseItemType(oItem)== BASE_ITEM_GREATSWORD ||
    GetBaseItemType(oItem)== BASE_ITEM_HALBERD ||
    GetBaseItemType(oItem)== BASE_ITEM_HANDAXE ||
    GetBaseItemType(oItem)== BASE_ITEM_HEAVYFLAIL ||
    GetBaseItemType(oItem)== BASE_ITEM_KAMA ||
    GetBaseItemType(oItem)== BASE_ITEM_KATANA ||
    GetBaseItemType(oItem)== BASE_ITEM_KUKRI ||
    GetBaseItemType(oItem)== BASE_ITEM_LIGHTFLAIL ||
    GetBaseItemType(oItem)== BASE_ITEM_LIGHTHAMMER ||
    GetBaseItemType(oItem)== BASE_ITEM_LIGHTMACE ||
    GetBaseItemType(oItem)== BASE_ITEM_LONGSWORD ||
    GetBaseItemType(oItem)== BASE_ITEM_MORNINGSTAR ||
    GetBaseItemType(oItem)== BASE_ITEM_QUARTERSTAFF ||
    GetBaseItemType(oItem)== BASE_ITEM_RAPIER ||
    GetBaseItemType(oItem)== BASE_ITEM_SCIMITAR ||
    GetBaseItemType(oItem)== BASE_ITEM_SCYTHE ||
    GetBaseItemType(oItem)== BASE_ITEM_SHORTSPEAR ||
    GetBaseItemType(oItem)== BASE_ITEM_SHORTSWORD ||
    GetBaseItemType(oItem)== BASE_ITEM_SICKLE ||
    GetBaseItemType(oItem)== BASE_ITEM_TRIDENT ||
    GetBaseItemType(oItem)== BASE_ITEM_TWOBLADEDSWORD ||
    GetBaseItemType(oItem)== BASE_ITEM_WARHAMMER ||
    GetBaseItemType(oItem)== BASE_ITEM_WHIP ||
    GetBaseItemType(oItem)== BASE_ITEM_HEAVYCROSSBOW ||
    GetBaseItemType(oItem)== BASE_ITEM_LIGHTCROSSBOW ||
    GetBaseItemType(oItem)== BASE_ITEM_LONGBOW ||
    GetBaseItemType(oItem)== BASE_ITEM_SHORTBOW ||
    GetBaseItemType(oItem)== BASE_ITEM_SLING))
  {
   oBox3 = GetObjectByTag("wepbox");
   AssignCommand(oBox3, ActionTakeItem(oItem, oPC));
  }


  //Seperate the Armor & Worn Items into the armbox
  else if((GetBaseItemType(oItem)==BASE_ITEM_AMULET ||
   GetBaseItemType(oItem)==BASE_ITEM_ARMOR ||
   GetBaseItemType(oItem)==BASE_ITEM_BELT ||
   GetBaseItemType(oItem)==BASE_ITEM_BOOTS ||
   GetBaseItemType(oItem)==BASE_ITEM_BRACER ||
   GetBaseItemType(oItem)==BASE_ITEM_CLOAK ||
   GetBaseItemType(oItem)==BASE_ITEM_GLOVES ||
   GetBaseItemType(oItem)==BASE_ITEM_HELMET ||
   GetBaseItemType(oItem)==BASE_ITEM_LARGESHIELD ||
   GetBaseItemType(oItem)==BASE_ITEM_RING ||
   GetBaseItemType(oItem)==BASE_ITEM_SMALLSHIELD ||
   GetBaseItemType(oItem)==BASE_ITEM_TOWERSHIELD))
  {
   oBox4 = GetObjectByTag("armbox");
   AssignCommand(oBox4, ActionTakeItem(oItem, oPC));
  }

  //now let's seperate all the miscellaneous items
  //Grenades, Trapkits, and Thieving Tools into the oBox
  else if((GetBaseItemType(oItem)==BASE_ITEM_MISCSMALL ||
   GetBaseItemType(oItem)==BASE_ITEM_MISCMEDIUM ||
   GetBaseItemType(oItem)==BASE_ITEM_MISCLARGE ||
   GetBaseItemType(oItem)==BASE_ITEM_MISCTHIN ||
   GetBaseItemType(oItem)==BASE_ITEM_MISCWIDE ||
   GetBaseItemType(oItem)==BASE_ITEM_MISCTALL ||
   GetBaseItemType(oItem)==BASE_ITEM_GRENADE ||
   GetBaseItemType(oItem)==BASE_ITEM_THIEVESTOOLS ||
   GetBaseItemType(oItem)==BASE_ITEM_TRAPKIT))
  {
   oBox5 = GetObjectByTag("obox");
   AssignCommand(oBox5, ActionTakeItem(oItem, oPC));
  }

  //Put all ammunitions in the ammobox
   else if((GetBaseItemType(oItem)== BASE_ITEM_ARROW ||
          GetBaseItemType(oItem)== BASE_ITEM_BOLT ||
          GetBaseItemType(oItem)== BASE_ITEM_BULLET ||
          GetBaseItemType(oItem)== BASE_ITEM_DART ||
          GetBaseItemType(oItem)== BASE_ITEM_SHURIKEN ||
          GetBaseItemType(oItem)== BASE_ITEM_THROWINGAXE))
   {
    oBox6 = GetObjectByTag("ammobox");
    AssignCommand(oBox6, ActionTakeItem(oItem, oPC));
   }

  //Seperate all Rods / Staves / Wands into the srwbox
  else if((GetBaseItemType(oItem)==BASE_ITEM_MAGICROD ||
           GetBaseItemType(oItem)==BASE_ITEM_MAGICSTAFF ||
           GetBaseItemType(oItem)==BASE_ITEM_MAGICWAND))
  {
   oBox7= GetObjectByTag("rswbox");
   AssignCommand(oBox7, ActionTakeItem(oItem, oPC));
  }

 //Otherwise put the item in the ibox!
 else
 {
  oBox = GetObjectByTag("ibox");
  AssignCommand(oBox, ActionTakeItem(oItem, oPC));
 }

//If statement end
}

//PROTOTYPE END
}

///////////////////////////////////////////////////////
void ReturnUndroppables(object oTarget)
{

//NOTE: oTarget = the plotBox;

object oPC = GetLastUsedBy();

object oItem;
oItem = GetFirstItemInInventory(oTarget);
while(GetIsObjectValid(oItem))
{
//return all containers to the PC!
if(GetItemCursedFlag(oItem)==TRUE)
{
//Give everything to the PC..
AssignCommand(oTarget, ActionGiveItem(oItem, oPC));
}

oItem = GetNextItemInInventory(oTarget);
}

//PROTOTYPE END
}

//////////////////////////////////////////////////////////////////////////
//PROTOTYPE DEFINED
void ClearAll()
{

//Forgot to remove all action ques from the boxes!
//This was a very hard error to find!

//Otherwise the system would not work 2 times!
 object oBox = GetObjectByTag("ibox");
 object oBox1 = GetObjectByTag("plotbox");
 object oBox2 = GetObjectByTag("potbox");
 object oBox3 = GetObjectByTag("wepbox");
 object oBox4 = GetObjectByTag("armbox");
 object oBox5 = GetObjectByTag("obox");
 object oBox6 = GetObjectByTag("ammobox");
 object oBox7= GetObjectByTag("rswbox");
 object oBox8= GetObjectByTag("ihold4u");

 AssignCommand(oBox,  ClearAllActions());
 AssignCommand(oBox1, ClearAllActions());
 AssignCommand(oBox2, ClearAllActions());
 AssignCommand(oBox3, ClearAllActions());
 AssignCommand(oBox4, ClearAllActions());
 AssignCommand(oBox5, ClearAllActions());
 AssignCommand(oBox6, ClearAllActions());
 AssignCommand(oBox7, ClearAllActions());
 AssignCommand(oBox8, ClearAllActions());

//PROTOTYPE END
}

//////////////////////////////////////////////////////////////
//PROTOTYPE DEFINED

void SortBoxes()
{
 object oBox = GetObjectByTag("ibox");
 object oBox1 = GetObjectByTag("plotbox");
 object oBox2 = GetObjectByTag("potbox");
 object oBox3 = GetObjectByTag("wepbox");
 object oBox4 = GetObjectByTag("armbox");
 object oBox5 = GetObjectByTag("obox");
 object oBox6 = GetObjectByTag("ammobox");
 object oBox7= GetObjectByTag("rswbox");
 object oBox8= GetObjectByTag("ihold4u");

 //?????????
 //ClearAll();

 //Start bagging the items in boxes with sorted items..
 //NOTE: This is most resource intensive part, and can be effected
 //by lagg, so.......
 DelayCommand(0.2, SortBox(oBox1));
 DelayCommand(0.4, SortBox(oBox2));
 DelayCommand(0.6, SortBox(oBox3));
 DelayCommand(0.8, SortBox(oBox4));
 DelayCommand(1.0, SortBox(oBox5));
 DelayCommand(1.2, SortBox(oBox6));
 DelayCommand(1.4, SortBox(oBox7));
 DelayCommand(1.8, SortBox(oBox));

 //We must delay the reuturn of all items
 //DelayCommand(15.0, GiveAll(oBox));
 //DelayCommand(15.1, GiveAll(oBox1));
 //DelayCommand(15.2, GiveAll(oBox2));
 //DelayCommand(15.3, GiveAll(oBox3));
 //DelayCommand(15.4, GiveAll(oBox4));
 //DelayCommand(15.5, GiveAll(oBox5));
 //DelayCommand(15.6, GiveAll(oBox6));
 //DelayCommand(15.7, GiveAll(oBox7));
 //DelayCommand(15.9, GiveAll(oBox8));

//PROTOTYPE END
}

/////////////////////////////////////////////////
//PROTOTYPE DEFINED

void StartSorting(object oTarget)
{

SendMessageToAllDMs("ATTENTION: Inventory System is currently working!!!");

 //Remove all Action Ques from all boxes FIRST!
 //ClearAll();

 //Return all Undroppable items to the PC (the iBag organizes these)
 DelayCommand(0.5, ReturnUndroppables(GetObjectByTag("plotbox")));

 //We must clear all Action Ques before doing all the major sorting
 //and returning the bags back to the PC.
 //DelayCommand(2.4, ClearAll());

 //Now sort all the boxes holding the sorted items..
 DelayCommand(2.7, SortBoxes());

//PROTOTYPE END
}


//////////////////////////////////////////////////////////////
//PROTOTYPE DEFINED
void SortHoldingTank(object oTarget)
{

//WARNING: DO NOT CHANGE ANY DELAY BELOW!!!!

 object oItem;
 float fTime = 8.0;
 oItem = GetFirstItemInInventory(oTarget);
 while(GetIsObjectValid(oItem))
 {
  //Take all items out of bags and put them in the Holding Box
  if(GetHasInventory(oItem))
  {
   fTime += 0.3;
   ClearBag(oItem);

   SetPlotFlag(oItem, FALSE);
   SetItemCursedFlag(oItem, FALSE);
   DelayCommand(fTime, DestroyObject(oItem, 0.0f));
  }
  //Otherwise leave the item alone!
  else
  {
   //Do nothing!
  }

  oItem = GetNextItemInInventory(oTarget);
 }
 //Next, Sort the items in the holding box
 //Need a slight delay to allow clear all actions to finish!
 DelayCommand(20.5, Seperate(oTarget));

 //Note, the seperating goes fast above, like within 3 seconds
 //HOWEVER, let's give it 10 seconds just in case!
 DelayCommand(30.5, StartSorting(oTarget));

}

/////////////////////////////////////////////////
//PROTOTYPE DEFINED
void SortContainer(object oTarget)
{

//NOTE: oTarget = The Box assigned to be sorted

 //Declare Major Variables
 object oPC = GetLastUsedBy();
 object oNPC = GetObjectByTag("ihold4u");
 string sTag = GetTag(oTarget);
 object oBox = GetObjectByTag(sTag);
 object oBag = GetItemPossessedBy(oNPC, sTag + "2");
 object oMine;
 string sName;

 //First Put all the items that will fit into the bag
 //on the NPC into the bag..
 oMine = GetFirstItemInInventory(oTarget);
 while(GetIsObjectValid(oMine)==TRUE)
 {

 AssignCommand(oTarget, ActionGiveItem(oMine, oBag));

 oMine = GetNextItemInInventory(oTarget);
 }

 //Make the bags unsellable and undroppable!!
 SetItemCursedFlag(oBag, TRUE);
 SetPlotFlag(oBag, TRUE);

//Determine what to name the Bags based on the box's tagname

if(GetTag(oTarget) == "ibox")
{
sName = "<cr� >Other Items";
}
if(GetTag(oTarget) == "plotbox")
{
sName = "<c�� >Plot Items";
}
if(GetTag(oTarget) == "potbox")
{
sName = "<c ��>Potions & Scrolls";
}
if(GetTag(oTarget) == "wepbox")
{
sName = "<c�  >Weapons";
}
if(GetTag(oTarget) == "armbox")
{
sName = "<c=w�>Armor & Clothing";
}
if(GetTag(oTarget) == "obox")
{
sName = "<c��~>Miscellaneous Items";
}
if(GetTag(oTarget) == "ammobox")
{
sName = "<c�� >Ammunition";
}
if(GetTag(oTarget) == "rswbox")
{
sName = "<c� >Rods/Staves/Wands";
}

 //Give the bag a the proper name with color :)
 DelayCommand(0.4, SetName(oBag, sName));

 //Give the PC the bag now that it has items in it.
 DelayCommand(0.6, AssignCommand(oNPC, ActionGiveItem(oBag, oPC)));

 //0.8 Seconds is the miminum delay here!
 DelayCommand(0.8, CheckBox(oTarget));

//PROTOTYPE END
}

//////////////////////////////////////////////////
//PROTOTYPE DEFINED
void SortBox(object oTarget)
{

//NOTE: oTarget = The Box Assigned to be sorted.

 //Declare Major Variables
 object oPC = GetLastUsedBy();
 object oNPC = GetObjectByTag("ihold4u");
 object oBox = GetObjectByTag(GetTag(oTarget));
 object oItem;

//We only continue if the box has items!
if((GetNum(oBox)==TRUE && GetNum(oBox)!=FALSE))
{
   //Give the NPC a Bag to store items in.
   //Give the bag the tagname of the box.
   //Had to add an extention to the tagname to correctly Find the bag!
   //DON'T LET ANY ERRORS OCCUR HERE!
   if(GetItemPossessedBy(oNPC, GetTag(oTarget) + "2")==OBJECT_INVALID)
   CreateItemOnObject("NW_IT_CONTAIN006", oNPC, 1, GetTag(oTarget) + "2");

   //Start putting items in the bag on the NPC.
   DelayCommand(0.3, SortContainer(oTarget));
}

//PROTOTYPES End
}

///////////////////////////////////////////////////////
void GiveAll(object oTarget)
{

object oItem;
object oPC = GetLastUsedBy();

oItem = GetFirstItemInInventory(oTarget);
while(GetIsObjectValid(oItem))
{
//Give everything to the PC inside this target box..
//Note: this function may not work, due to the fact that
//those items were assigned to bags!
AssignCommand(oTarget, ActionGiveItem(oItem, oPC));

//Continue to find all of the items in the box..
oItem = GetNextItemInInventory(oTarget);
}

//PROTOTYPE END
}

//////////////////////////////////////////////////////////////////////////

//PROTOTYPE DEFINED
void StartUp()
{
 //The holding box! (not an NPC)
 object oNPC2 = GetObjectByTag("ihold4u");

 DelayCommand(0.1, SortHoldingTank(oNPC2));

//PROTOTYPE END
}
/////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////
//////////WELCOME TO THE 800 CLUB WITH YOUR HOST GENISYS :p ///////////////
//////////////////////////////////////////////////////////////////////////
//THE END, WOOOOOOOSHHH!!!////////////////////FINAL EDITION!/////////////
////////////////////////////////////////////////////////////////////////