#include "inc_item_tools"
int SellItems(object oBox)
    {
    object oItem = GetFirstItemInInventory(oBox);
    int iGold,iSell;
    while(GetIsObjectValid(oItem))
        {
        if (!GetPlotFlag(oItem) && !GetStolenFlag(oItem))
            {
            if (GetIsWeapon(oItem) || GetIsArmor(oItem))
                {
                if (!GetIsAmmo(oItem))
                    iSell = GetTotalEnchantmentValue(oItem);
                else
                    iSell = GetGoldPieceValue(oItem); //Cludge for ammo
                }
            else
                iSell = GetGoldPieceValue(oItem);
            iGold += iSell;
            DestroyObject(oItem);
            }
        oItem = GetNextItemInInventory(oBox);
        }
    return iGold;
    }

void main()
    {
    object oBox = GetObjectByTag("JunkBox");
    int iGold = SellItems(oBox);
    object oLeader = GetPCSpeaker();
    int iBargin = GetHighestPartySkill(oLeader,SKILL_PERSUADE);
    iBargin += GetHighestPartySkill(oLeader,SKILL_APPRAISE);
    if (iBargin > 40)
        iBargin = 40;
    iBargin += 50; // For a total of 50% to 90% of base value.
    iGold = (iGold * iBargin) / 100;
    GiveGoldToCreature(oLeader,iGold);
    }
