public string getLastValidTagField(SimObject material, string invalidTag)
        {
            string tag, tagField = "";

            for (int i = 0; material.getFieldValue("materialTag" + i, -1) != ""; i++)
                {
                tag = material.getFieldValue("materialTag" + i, -1);
                // Can't equal our invalid tag
                if (tag == invalidTag)
                    continue;

                // Set our last found tag
                tagField = "materialTag" + i;
                }

            return tagField;
        }
        public string getLastTagField(SimObject material)
        {
            string tagField = "";

            for (int i = 0; material.getFieldValue("materialTag" + i, -1) != ""; i++)
                tagField = "materialTag" + i;

            return tagField;
        }
        public void deleteMaterial(SimObject materialName, ArrayObject secondFilter, string secondFilterName)
        {
            ArrayObject MaterialFilterAllArray = "MaterialFilterAllArray";
            ArrayObject UnlistedMaterials = "UnlistedMaterials";
            PersistenceManager MaterialSelectorPerMan = "MaterialSelectorPerMan";
            GuiCheckBoxCtrl MaterialFilterAllArrayCheckbox = "MaterialFilterAllArrayCheckbox";

            GuiDynamicCtrlArrayControl filterArray = this.FOT("filterArray");

            if (!materialName.isObject())
                return;

            for (int i = 0; i <= MaterialFilterAllArray.countValue(materialName); i++)
                {
                int index = MaterialFilterAllArray.getIndexFromValue(materialName);
                MaterialFilterAllArray.erase(index);
                }
            MaterialFilterAllArrayCheckbox.setText("All ( " + (MaterialFilterAllArray.count() - 1) + " ) ");

            GuiCheckBoxCtrl checkbox = secondFilter + "Checkbox";
            for (int k = 0; k <= secondFilter.countValue(materialName); k++)
                {
                int index = secondFilter.getIndexFromValue(materialName);
                secondFilter.erase(index);
                }

            checkbox.setText(secondFilterName + " ( " + (secondFilter.count() - 1) + " ) ");

            for (int i = 0; materialName.getFieldValue("materialTag" + i, -1) != ""; i++)
                {
                string materialTag = materialName.getFieldValue("materialTag" + i, -1);

                for (uint j = (uint) this.staticFilterObjects; j < filterArray.getCount(); j++)
                    {
                    if (materialTag == ((SimObject) ((SimSet) filterArray.getObject(j)).getObject(0))["filter"])
                        {
                        int count = Util.getWord(((GuiCheckBoxCtrl) ((SimSet) filterArray.getObject(j)).getObject(0)).getText(), 2).AsInt();
                        count--;
                        ((GuiCheckBoxCtrl) ((SimSet) filterArray.getObject(j)).getObject(0)).setText(materialTag + " ( " + count + " )");
                        }
                    }
                }

            UnlistedMaterials.add("unlistedMaterials", materialName);

            if (materialName.getFilename() != "" && materialName.getFilename() != "tools/gui/MaterialSelector.ed.gui" && materialName.getFilename() != "tools/materialEditor/scripts/materialEditor.ed.cs")
                {
                MaterialSelectorPerMan.removeObjectFromFile(materialName);
                MaterialSelectorPerMan.saveDirty();
                }

            this.preloadFilter();
            //MaterialSelector.selectMaterial( "WarningMaterial" );
        }
        public string getTagField(SimObject material, string tag)
        {
            string tagField = "";
            for (int i = 0; material.getFieldValue("materialTag" + i, -1) != ""; i++)
                {
                string loopTag = material.getFieldValue("materialTag" + i, -1);
                if (tag == loopTag)
                    {
                    tagField = "materialTag" + i;
                    break;
                    }
                }

            return tagField;
        }
        public void updateSelection(SimObject material, string previewImagePath)
        {
            GuiDynamicCtrlArrayControl filterArray = this.FOT("filterArray");
            GuiDynamicCtrlArrayControl materialCategories = this.FOT("materialCategories");
            GuiTextCtrl previewSelectionText = this.FOT("previewSelectionText");
            GuiBitmapCtrl previewSelection = this.FOT("previewSelection");

            // the material selector will visually update per material information
            // after we move away from the material. eg: if we remove a field from the material,
            // the empty checkbox will still be there until you move fro and to the material again

            bool isMaterialBorder = false;
            isMaterialBorder = this.FOT(material + "Border").isObject();
            if (isMaterialBorder)
                ((GuiButtonCtrl) this.FOT(material + "Border")).setStateOn(true);

            bool isMaterialBorderPrevious = false;
            isMaterialBorderPrevious = this.FOT(sGlobal["$prevSelectedMaterialHL"] + "Border").isObject();
            if (isMaterialBorderPrevious)
                ((GuiButtonCtrl) this.FOT(sGlobal["$prevSelectedMaterialHL"] + "Border")).setStateOn(false);

            materialCategories.deleteAllObjects();
            this.selectedMaterial = material;
            this.selectedPreviewImagePath = previewImagePath;
            previewSelectionText.setText(material.name);
            previewSelection.setBitmapX(previewImagePath);

            // running through the existing list of categorynames in the left, so yes
            // some might exist on the left only temporary if not given a home
            for (uint i = (uint) this.staticFilterObjects; i < filterArray.getCount(); i++)
                {
                string filter = ((SimObject) ((SimSet) filterArray.getObject(i)).getObject(0))["filter"];

                #region GuiCheckBoxCtrl ()        oc_Newobject72

                ObjectCreator oc_Newobject72 = new ObjectCreator("GuiCheckBoxCtrl", "");
                oc_Newobject72["materialName"] = material.name;
                oc_Newobject72["Profile"] = "ToolsGuiCheckBoxListProfile";
                oc_Newobject72["position"] = "5 2";
                oc_Newobject72["Extent"] = "118 18";
                oc_Newobject72["Command"] = "MaterialSelector.updateMaterialTags( $ThisControl.materialName, $ThisControl.getText(), $ThisControl.getValue() );";
                oc_Newobject72["text"] = filter;

                #endregion

                GuiCheckBoxCtrl checkbox = oc_Newobject72.Create();

                materialCategories.add(checkbox);
                // crawl through material for categories in order to check or not
                bool filterFound = false;
                for (int j = 0; material.getFieldValue("materialTag" + j, -1) != ""; j++)
                    {
                    string tag = material.getFieldValue("materialTag" + j, -1);

                    if (tag == filter)
                        {
                        filterFound = true;
                        break;
                        }
                    }

                if (filterFound)
                    checkbox.setStateOn(true);
                else
                    checkbox.setStateOn(false);
                }

            sGlobal["$prevSelectedMaterialHL"] = material;
        }