public int UpdateField(IGenField field)
        {
            IGenField _field = field;
            int _fieldIndex = -1;

            try
            {
                // find the form and field
                foreach (IGenForm _form in forms)
                {
                    if (_form.name.ToUpper() == _field.parentFormName.ToUpper())
                    {
                        for (int n = 0; n < _form.formFields.fields.Count; n++)
                        {
                            if (_form.formFields.fields[n].controlName.ToUpper() == _field.controlName.ToUpper())
                            {
                                _fieldIndex = n;
                                _form.formFields.fields[n] = _field;
                                break;
                            }
                        }
                        break;
                    }
                }

            }
            catch (Exception ex)
            {
                CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".UpdateField > " + ex.Message);
            }

            return _fieldIndex;
        }
        public string ResolveDSColumns(IGenField field)
        {
            string _value = field.compiledValue;
            string _formName = field.parentFormName.ToUpper();
            string _fieldName = field.name.ToUpper();

            try
            {

                // replace the compiled value
                field.compiledValue = _value;

            }
            catch (Exception ex)
            {
                CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".ResolveFields > " + ex.Message);
            }

            return _value;
        }
        public string ResolveFields(IGenField field)
        {
            string _value = field.compiledValue;
            string _formName = field.parentFormName.ToUpper();
            string _originalFormName = _formName;
            string _fieldName = field.name.ToUpper();
            string _originalFieldName = _fieldName;
            string _fieldAttribute = "VALUE";
            string _validAttributes = "VALUE;LIST;NAME;";

            try
            {
                // get the field(s)
                int _openBracket = _value.IndexOf('[');
                int _closeBracket = _value.IndexOf(']');

                // if no close bracket is found, exit with error...
                if (_closeBracket > _openBracket)
                {

                    while (_openBracket >= 0)
                    {
                        if (_closeBracket > _openBracket)
                        {
                            string _fieldReference = _value.Substring(_openBracket + 1, (_closeBracket - _openBracket - 1));
                            // is this a non explicit field? (no form. as a prefix)
                            string[] _parts = _fieldReference.ToUpper().Split('!');
                            if (_parts.Length > 0)
                            {
                                if (_parts.Length == 1)
                                {
                                    // just the field, it should be on the current form
                                    _fieldName = _parts[0];
                                    _formName = _originalFormName;
                                }
                                else
                                {
                                    if (_parts.Length == 2)
                                    {
                                        // could be form.field or field.attribute
                                        if (_validAttributes.IndexOf(_parts[1] + ";") >= 0)
                                        {
                                            // a valid attribute so assume field.attribute
                                            _fieldName = _parts[0];
                                            _fieldAttribute = _parts[1];
                                        }
                                        else
                                        {
                                            // not valid, so assume form.field and attribute = VALUE
                                            _formName = _parts[0];
                                            _fieldName = _parts[1];
                                            _fieldAttribute = "VALUE";
                                        }
                                    }
                                    else
                                    {
                                        // more than 2 assume form.field.attribute
                                        _formName = _parts[0];
                                        _fieldName = _parts[1];
                                        _fieldAttribute = _parts[2];
                                    }
                                }
                            }

                            string _formFieldIndexes = "ERR";
                            bool _fieldFound = false;

                            // find the formnumber:fieldnumber for the form and field referenced
                            for (int n = 0; n < forms.Count; n++)
                            {
                                if (forms[n].name.ToUpper() == _formName)
                                {
                                    // find the field
                                    for (int m = 0; m < forms[n].formFields.fields.Count; m++)
                                    {
                                        if (forms[n].formFields.fields[m].name.ToUpper() == _fieldName)
                                        {
                                            _formFieldIndexes = "{" + n + ":" + m + ":" + _fieldAttribute + "}";
                                            _fieldFound = true;
                                        }
                                    }
                                }
                            }

                            // was the field found?
                            if (!_fieldFound)
                            {
                                // add to compile error messages
                                string _msg = "Field {" + _originalFormName + "}-{" + _originalFieldName + "} references " + _fieldName + " but name not found or misspelled";
                                compileErrors = true;
                                compileErrorsMessages.Add(_msg);
                            }

                            if (_openBracket == 0)
                            {
                                _value = _formFieldIndexes + _value.Substring(_closeBracket + 1);
                            }
                            else
                            {
                                _value = _value.Substring(0, _openBracket) + _formFieldIndexes + _value.Substring(_closeBracket + 1);
                            }
                            _openBracket = _value.IndexOf('[');
                            _closeBracket = _value.IndexOf(']');
                        }
                        else
                        {
                            _openBracket = _closeBracket + 1;
                            _closeBracket = _value.IndexOf(']', _openBracket);
                        }

                    }
                }

                // replace the compiled value
                field.compiledValue = _value;

            }
            catch (Exception ex)
            {
                CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".ResolveFields > " + ex.Message);
            }

            return _value;
        }
        public IGenField GetField(int formIndex, int fieldIndex)
        {
            IGenField _field = new IGenField();

            try
            {
                IGenForm _form = GetForm(formIndex);
                _field = _form.formFields.GetField(fieldIndex);
            }
            catch (Exception ex)
            {
                CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".GetField(i, i) > " + ex.Message);
            }

            return _field;
        }
        //public string ResolveGDS(IGenForm form, IGenField field, string value)
        //{
        //    IGenForm _form = form;
        //    IGenField _field = field;
        //    string _value = value.ToUpper();
        //    try
        //    {
        //        _value = _value.ToUpper();
        //        // get the field(s)
        //        int _openBracket = _value.IndexOf("GDS(");
        //        if (_openBracket >= 0)
        //        {
        //            while (_openBracket >= 0)
        //            {
        //                int _closeBracket = _value.IndexOf(")", _openBracket);
        //                if (_closeBracket > _openBracket)
        //                {
        //                    string _fieldReference = _value.Substring(_openBracket + 4, (_closeBracket - _openBracket - 4)).ToUpper();
        //                    string _fieldOrdinal = "-1";
        //                    bool _fieldFound = false;
        //                    if (dataset.Count > 0)
        //                    {
        //                        // get the field names for the global dataset
        //                        string[] _fieldNames = dataset[0];
        //                        // find the ordinal in the current forms dataset
        //                        for (int j = 0; j < _fieldNames.Length; j++)
        //                        {
        //                            if (_fieldNames[j].ToUpper() == _fieldReference)
        //                            {
        //                                _fieldOrdinal = j.ToString();
        //                                _fieldFound = true;
        //                                break;
        //                            }
        //                        }
        //                    }
        //                    // did it find it?
        //                    if (!_fieldFound)
        //                    {
        //                        // add to compile error messages
        //                        string _msg = "Global Dataset Field " + _fieldReference + " not found or misspelled";
        //                        compileErrors = true;
        //                        compileErrorsMessages.Add(_msg);
        //                    }
        //                    if (_openBracket == 0)
        //                    {
        //                        _value = "GDS(" + _fieldOrdinal + _value.Substring(_closeBracket);
        //                    }
        //                    else
        //                    {
        //                        _value = _value.Substring(0, _openBracket) + "GDS(" + _fieldOrdinal + _value.Substring(_closeBracket);
        //                    }
        //                }
        //                _openBracket = _value.IndexOf("GDS(", _openBracket + 1);
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".ResolveGDS > " + ex.Message);
        //    }
        //    return _value;
        //}
        public string ResolveDS(string datasetName, IGenField field, string value, string prefix)
        {
            IGenField _field = field;
            string _value = value.ToUpper();
            string _datasetName = datasetName;

            try
            {
                // check for DS columns
                _value = _value.ToUpper();

                // format can be
                //      DS(field):row
                //      DS(cursorOrdinal!field):row
                //      DS(cursorName!field):row
                //      DSLOOKUP(cursorName!field, filter)
                //      PAGEBREAK(cursorName, true stmt, false stmt)

                // get the field(s)
                int _openBracket = _value.IndexOf(prefix.ToUpper());
                if (_openBracket >= 0)
                {
                    while (_openBracket >= 0)
                    {
                        int _closeBracket = _value.IndexOf(")", _openBracket);
                        string[] _fieldNames = { };

                        if (_closeBracket > _openBracket)
                        {
                            int _lenReference =  _closeBracket - _openBracket - prefix.Length;
                            string _fieldReference = _value.Substring(_openBracket + prefix.Length, _lenReference).ToUpper();
                            string _dsName = "";
                            string _fieldName = _fieldReference;
                            string _fieldOrdinal = "-1";
                            bool _fieldFound = false;
                            int _dsOrdinal = -1;

                            // see how many parts there are to the values in the parens
                            string[] _parts = _fieldReference.Split(',');

                            if (_parts.Length >= 1)
                            {
                                // now, the first part should be the ds!column spec.
                                _fieldName = _parts[0];

                                // check to see if the fieldname is already resolved to dsordinal:fieldordinal
                                if (_fieldName.IndexOf(':') < 0)
                                {
                                    string[] _dsParts = _fieldName.Split('!');

                                    if (_dsParts.Length > 1)
                                    {
                                        // a dsname or ordinal was specified
                                        _datasetName = _dsParts[0];
                                        _fieldName = _dsParts[1];
                                    }
                                    else
                                    {
                                        if (prefix.ToUpper().IndexOf("PAGEBREAK") == 0)
                                        {
                                            // a dsname or ordinal was specified
                                            _datasetName = _dsParts[0];
                                            _fieldName = "";
                                            _fieldOrdinal = "";
                                        }
                                    }

                                    // get the cursor associated with this form
                                    if (_datasetName != "")
                                    {
                                        _dsOrdinal = -1;

                                        // may be one of the ordinal, field or name,field formats
                                        if (CommonRoutines.IsNumeric(_datasetName) &&
                                                    (CommonRoutines.ConvertToInt(_datasetName) >= 0 && CommonRoutines.ConvertToInt(_datasetName) < datasets.Count))
                                        {
                                            _dsOrdinal = CommonRoutines.ConvertToInt(_datasetName);
                                            _fieldNames = datasets[_dsOrdinal].results[0];
                                            if (_fieldNames.Length < 1)
                                            {
                                                _dsOrdinal = -1;
                                            }
                                        }

                                        if (_dsOrdinal < 0)
                                        {
                                            // not found, see if it is there by name...
                                            for (int m = 0; m < datasets.Count; m++)
                                            {
                                                if (datasets[m].cursorName.ToUpper() == _datasetName.ToUpper())
                                                {
                                                    _fieldNames = datasets[m].fieldNames;
                                                    _dsOrdinal = m;
                                                    break;
                                                }
                                            }
                                        }

                                    }

                                    if (_dsOrdinal >= 0)
                                    {
                                        _fieldFound = false;

                                        if (_fieldName != "")
                                        {
                                            // find the ordinal in the current forms dataset
                                            // trim and upper each one to resolve correctly
                                            for (int j = 0; j < _fieldNames.Length; j++)
                                            {
                                                if (_fieldNames[j].Trim().ToUpper() == _fieldName.Trim().ToUpper())
                                                {
                                                    _fieldOrdinal = j.ToString();
                                                    _fieldFound = true;
                                                    break;
                                                }
                                            }

                                            // did it find it?
                                            if (!_fieldFound)
                                            {
                                                // add to compile error messages
                                                string _msg = "Form:" + _field.parentFormName + "  Dataset:" + _datasetName + "  Field:" + _fieldReference + " not found or misspelled";
                                                compileErrors = true;
                                                compileErrorsMessages.Add(_msg);
                                            }

                                            _fieldOrdinal = ":" + _fieldOrdinal;
                                        }

                                        string _tempValue = "";

                                        if (_openBracket > 0)
                                        {
                                            _tempValue = _value.Substring(0, _openBracket);
                                        }

                                        _tempValue = _tempValue + prefix + _dsOrdinal + _fieldOrdinal;

                                        for (int m = 1; m < _parts.Length; m++)
                                        {
                                            _tempValue = _tempValue + "," + _parts[m];
                                        }

                                        _tempValue = _tempValue + _value.Substring(_closeBracket);

                                        _value = _tempValue;
                                    }
                                }
                            }
                        }

                        _openBracket = _value.IndexOf(prefix, _openBracket + 1);
                    }

                }

            }
            catch (Exception ex)
            {
                CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".ResolveDS > " + ex.Message);
            }

            return _value;
        }
        /// <summary>
        /// string LoadFormGroup(string formFile)
        /// Load the form group
        /// </summary>
        /// <returns></returns>
        public int CreateForms(List<string> xmlRecords)
        {
            int _numForms = 0;
            string _tagName = "";
            string _tagValue = "";
            bool _formFlag = false;
            bool _fieldFlag = false;
            bool _ruleFlag = false;
            bool _datasetFlag = false;
            IGenForm _form = null;
            IGenField _field = null;
            IGenDataset _dataset = null;
            string _formGroupPath = "";
            bool _variableFlag = false;
            string _variableName = "";
            string _variableValue = "";

            try
            {
                // clear the forms collection
                forms.Clear();

                // clear the datasets
                datasets.Clear();

                // walk the XML and create the entities
                for (int _recNo=0;_recNo < xmlRecords.Count;_recNo++)
                //foreach (string _xmlRecord in xmlRecords)
                {
                    string _xmlRecord = xmlRecords[_recNo];

                    // parse the string
                    int _offset = _xmlRecord.IndexOf('=');
                    if (_offset > 0)
                    {
                        _tagName = _xmlRecord.Substring(0, _offset);
                        _tagValue = _xmlRecord.Substring(_offset + 1);
                    }
                    else
                    {
                        _tagName = _xmlRecord;
                        _tagValue = "";
                    }

                    _tagName = _tagName.Trim().ToUpper();
                    _tagValue = _tagValue.Trim();

                    if (_tagName != "")
                    {
                        // process the tag
                        switch (_tagName)
                        {

                            case "FORMGROUPPATH":
                                _formGroupPath = _tagValue;
                                break;

                            case "FORMS":
                                _formFlag = false;
                                _fieldFlag = false;
                                _ruleFlag = false;
                                _form = null;
                                break;

                            case "FORM":
                                _formFlag = true;
                                _fieldFlag = false;
                                _ruleFlag = false;
                                // create a form object
                                _form = new IGenForm();
                                _form.formGroupPath = _formGroupPath;
                                forms.Add(_form);
                                break;

                            case "FIELDS":
                                _formFlag = false;
                                _fieldFlag = false;
                                _ruleFlag = false;
                                _field = null;
                                break;

                            case "FIELD":
                                _formFlag = false;
                                _fieldFlag = true;
                                _ruleFlag = false;
                                _field = new IGenField();
                                _field.parentFormName = _form.name;
                                // add it
                                _form.AddField(_field);
                                break;

                            case "FONT":
                                break;

                            case "/FONT":
                                break;

                            case "PROPERTIES":
                                break;

                            case "/PROPERTIES":
                                break;

                            case "RULES":
                                break;

                            case "/RULES":
                                break;

                            case "RULE":
                                break;

                            case "/RULE":
                                break;

                            case "VARIABLES":
                                _variableFlag = true;
                                break;

                            case "/VARIABLES":
                                _variableFlag = false;
                                break;

                            case "VARIABLE":
                                break;

                            case "/VARIABLE":
                                // write the variable to the csa properties
                                if (_variableName != "")
                                {
                                    formVariables.Add(new string[] { _variableName, _variableValue });
                                }
                                break;

                            case "/FIELD":
                                _formFlag = false;
                                _fieldFlag = false;
                                _ruleFlag = false;
                                break;

                            case "/FORM":
                                _formFlag = false;
                                _fieldFlag = false;
                                _ruleFlag = false;
                                break;

                            case "/FORMS":
                                break;

                            // not the attribute tags
                            case "GROUP":
                                formGroupName = _tagValue;
                                break;

                            case "VERSION":
                                formGroupVersion = _tagValue;
                                break;

                            case "DATASOURCE":
                                // see if this datasource is valid by checking the connection pool.
                                originalDataSource = _tagValue;
                                dataSource = "";
                                for (int n = 0; n < DatabaseRoutines.connectionObjects.Count; n++)
                                {
                                    if (DatabaseRoutines.connectionObjects[n].connectionString.ToUpper() == _tagValue.ToUpper())
                                    {
                                        dataSource = _tagValue;
                                        break;
                                    }
                                }
                                // if datasource is blank, default to main connection
                                if (dataSource == "")
                                {
                                    dataSource = DatabaseRoutines.MainConnection;
                                }
                                break;

                            case "GROUPDATASET":
                                datasetName = "";
                                if (_tagValue != "")
                                {
                                    _dataset = new IGenDataset();
                                    _dataset.cursorName = formGroupName;
                                    datasetName = formGroupName;
                                    _dataset.sql = _tagValue;
                                    datasets.Add(_dataset);
                                    _dataset = new IGenDataset();
                                }
                                break;

                            case "FORMDATASET":
                                if (_formFlag)
                                {
                                    _form.datasetName = "";
                                    if (_tagValue != "")
                                    {
                                        _dataset = new IGenDataset();
                                        _dataset.cursorName = _form.name;
                                        _form.datasetName = _form.name;
                                        _dataset.sql = _tagValue;
                                        datasets.Add(_dataset);
                                        _dataset = new IGenDataset();
                                    }
                                }
                                break;

                            case "DATASET":
                                _datasetFlag = true;
                                _dataset = new IGenDataset();
                                break;

                            case "/DATASET":
                                _datasetFlag = false;
                                // add to the dataset collection
                                datasets.Add(_dataset);
                                _dataset = new IGenDataset();
                                break;

                            case "SQL":
                                if (_datasetFlag)
                                {
                                    _dataset.sql = _tagValue;
                                }
                                break;

                            case "DATASETNAME":
                                string _datasetName = _tagValue;
                                int _datasetOrdinal = -1;
                                // find it in the group datasets collection
                                for (int n = 0; n < datasets.Count; n++)
                                {
                                    if (datasets[n].cursorName.ToUpper() == _datasetName.ToUpper())
                                    {
                                        _datasetOrdinal = n;
                                        break;
                                    }
                                }
                                if (_formFlag)
                                {
                                    // add it to the datasetname list for the form
                                    _form.datasetOrdinals.Add(_datasetOrdinal);
                                    _form.datasetNames.Add(_datasetName);
                                    if (_form.datasetName == "")
                                    {
                                        // default name is the first one in the xml list
                                        _form.datasetName = _datasetName;
                                        _form.datasetOrdinal = _datasetOrdinal;
                                    }
                                }
                                else
                                {
                                    datasetName = _datasetName;        // for the GDS function
                                    datasetOrdinal = _datasetOrdinal;
                                }
                                break;

                            case "ROWSPERPAGE":
                                if (_formFlag)
                                {
                                    int _rowsPerPage = CommonRoutines.ConvertToInt(_tagValue);
                                    // check to see if this is a multipage form
                                    if (_form.multiPageForm.ToUpper() == "TRUE")
                                    {
                                        if (_rowsPerPage < 1)
                                        {
                                            // give an error!
                                            CommonRoutines.DisplayErrorMessage(_form.name + " - Rows per page must be greater than 0 if a multipage form");
                                        }
                                        else
                                        {
                                            _form.rowsPerPages.Add(_rowsPerPage);
                                            if (_form.rowsPerPage < 0)
                                            {
                                                _form.rowsPerPage = _rowsPerPage;
                                            }
                                        }
                                    }
                                }
                                break;

                            case "/SQL":
                                break;

                            case "IMAGE":
                                if (_formFlag)
                                {
                                    _form.imageName = _tagValue;
                                }
                                else
                                {
                                    if (_fieldFlag)
                                    {
                                        _field.imageName = _tagValue;
                                    }
                                    else
                                    {

                                    }
                                }
                                break;

                            case "COMMENTS":
                                if (_formFlag)
                                {
                                    _form.comments = _tagValue;
                                }
                                else
                                {
                                    if (_fieldFlag)
                                    {
                                        _field.comments = _tagValue;
                                    }
                                    else
                                    {
                                        comments = _tagValue;
                                    }
                                }
                                break;

                            case "NAME":
                                if (_formFlag)
                                {
                                    _form.name = _tagValue;
                                }
                                else
                                {
                                    if (_fieldFlag)
                                    {
                                        _field.name = _tagValue;
                                    }
                                    else
                                    {
                                        if (_datasetFlag)
                                        {
                                            _dataset.cursorName = _tagValue;
                                        }
                                        else
                                        {
                                            if (_variableFlag)
                                            {
                                                _variableName = _tagValue;
                                            }
                                        }
                                    }
                                }
                                break;

                            case "CREATESYMBOLICS":
                                if (_datasetFlag)
                                {
                                    _dataset.createSymbolics = CommonRoutines.ConvertToBool(_tagValue);
                                }
                                break;

                            case "FORMATMASK":
                                if (_formFlag)
                                {
                                }
                                else
                                {
                                    if (_fieldFlag)
                                    {
                                        _field.formatMask = _tagValue;
                                    }
                                }
                                break;

                            case "VISIBLE":
                                if (_formFlag)
                                {
                                    _form.visible = (_tagValue.ToUpper() == "TRUE") ? true : false;
                                }
                                else
                                {
                                    if (_fieldFlag)
                                    {
                                        _field.visible = (_tagValue.ToUpper() == "TRUE") ? true : false;
                                    }
                                    else
                                    {

                                    }
                                }
                                break;

                            case "FORMTYPE":
                                if (_formFlag)
                                {
                                    _form.formType = _tagValue;
                                }
                                break;

                            case "TITLE":
                                if (_formFlag)
                                {
                                    _form.title = _tagValue;
                                }
                                break;

                            case "MULTIPAGEFORM":
                                if (_formFlag)
                                {
                                    _form.multiPageForm = (_tagValue.ToUpper() == "TRUE") ? "True" : "False";
                                }
                                break;

                            case "PAGEBREAKS":
                                if (_formFlag)
                                {
                                    _form.pageBreaks = _tagValue;
                                }
                                break;

                            case "PROCESSINGORDER":
                                if (_formFlag)
                                {
                                    _form.processingOrder = CommonRoutines.ConvertToInt(_tagValue);
                                    if (_form.processingOrder == 0)
                                    {
                                        _form.processingOrder = 999;
                                    }
                                }
                                break;

                            case "TYPE":
                                if (_fieldFlag)
                                {
                                    _field.type = _tagValue;
                                }
                                else
                                {

                                }
                                break;

                            case "DATATYPE":
                                if (_fieldFlag)
                                {
                                    _field.dataType = _tagValue;
                                }
                                else
                                {

                                }
                                break;

                            case "ALIGNMENT":
                                if (_fieldFlag)
                                {
                                    _field.alignment = _tagValue;
                                }
                                else
                                {

                                }
                                break;

                            case "TOP":
                                if (_fieldFlag)
                                {
                                    _field.top = CommonRoutines.ConvertToInt(_tagValue);
                                }
                                else
                                {

                                }
                                break;

                            case "LEFT":
                                if (_fieldFlag)
                                {
                                    _field.left = CommonRoutines.ConvertToInt(_tagValue);
                                }
                                else
                                {

                                }
                                break;

                            case "WIDTH":
                                if (_fieldFlag)
                                {
                                    _field.width = CommonRoutines.ConvertToInt(_tagValue);
                                }
                                else
                                {

                                }
                                break;

                            case "HEIGHT":
                                if (_fieldFlag)
                                {
                                    _field.height = CommonRoutines.ConvertToInt(_tagValue);
                                }
                                else
                                {

                                }
                                break;

                            case "TAG":
                                if (_fieldFlag)
                                {
                                    _field.tag = _tagValue;
                                }
                                else
                                {

                                }
                                break;

                            case "FONTNAME":
                                if (_fieldFlag)
                                {
                                    _field.fontName = _tagValue;
                                }
                                else
                                {

                                }
                                break;

                            case "FONTSIZE":
                                if (_fieldFlag)
                                {
                                    _field.fontSize = CommonRoutines.ConvertToFloat(_tagValue);
                                }
                                else
                                {

                                }
                                break;

                            case "FONTBOLD":
                                if (_fieldFlag)
                                {
                                    _field.fontBold = (_tagValue.ToUpper() == "TRUE") ? true : false; ;
                                }
                                else
                                {

                                }
                                break;

                            case "FONTITALIC":
                                if (_fieldFlag)
                                {
                                    _field.fontItalic = (_tagValue.ToUpper() == "TRUE") ? true : false; ;
                                }
                                else
                                {

                                }
                                break;

                            case "FONTUNDERLINE":
                                if (_fieldFlag)
                                {
                                    _field.fontUnderline = (_tagValue.ToUpper() == "TRUE") ? true : false; ;
                                }
                                else
                                {

                                }
                                break;

                            case "TABINDEX":
                                if (_fieldFlag)
                                {
                                    _field.tabIndex = CommonRoutines.ConvertToInt(_tagValue);
                                }
                                else
                                {

                                }
                                break;

                            case "LIST":
                                if (_fieldFlag)
                                {
                                    _field.list = _tagValue;
                                }
                                else
                                {

                                }
                                break;

                            case "VALUE":
                                if (_fieldFlag)
                                {
                                    // if it is not an expression
                                    if (_tagValue.IndexOf('=') < 0)
                                    {
                                        if (_field.type.ToUpper().IndexOf("CHECKBOX") >= 0)
                                        {
                                            bool _checkValue = CommonRoutines.ConvertToBool(_tagValue);
                                            _field.value = _checkValue.ToString();
                                            _field.checkedFlag = _checkValue;
                                        }
                                    }
                                    else
                                    {
                                        _field.value = _tagValue;
                                    }
                                    _field.originalValue = _field.value;
                                    _field.compiledValue = _field.value;
                                }
                                else
                                {
                                    if (_variableFlag)
                                    {
                                        _variableValue = _tagValue;
                                    }
                                }
                                break;

                            case "TEXT":
                                if (_fieldFlag)
                                {
                                    _field.text = _tagValue;
                                }
                                else
                                {

                                }
                                break;

                            case "CAPTION":
                                if (_fieldFlag)
                                {
                                    _field.caption = _tagValue;
                                }
                                else
                                {

                                }
                                break;

                            case "INSTRUCTIONS":
                                if (_formFlag)
                                {
                                    _form.instructions = _tagValue;
                                }
                                else
                                {
                                    if (_fieldFlag)
                                    {
                                        _field.instructions = _tagValue;
                                    }
                                    else
                                    {

                                    }
                                }
                                break;

                            case "PRINTORIENTATION":
                                if (_formFlag)
                                {
                                    _form.printOrientation = _tagValue;
                                }
                                else
                                {
                                    if (_fieldFlag)
                                    {
                                    }
                                    else
                                    {

                                    }
                                }
                                break;

                            case "LAYER":
                                if (_fieldFlag)
                                {
                                    _field.layer = CommonRoutines.ConvertToInt(_tagValue);
                                }
                                else
                                {

                                }
                                break;

                            case "EDIFLAG":
                                if (_fieldFlag)
                                {
                                    _field.ediFlag = (_tagValue + "N").Substring(0, 1);
                                }
                                else
                                {

                                }
                                break;

                            case "EDINAME":
                                if (_fieldFlag)
                                {
                                    _field.ediName = _tagValue.ToUpper();
                                }
                                else
                                {

                                }
                                break;

                            default:
                                // all other tags
                                if (_fieldFlag)
                                {
                                    _field.AddProperty(_tagName, _tagValue);
                                }
                                else
                                {

                                }
                                break;

                        }
                    }
                }

            }
            catch (Exception ex)
            {
                CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".CreateForms > " + ex.Message);
            }

            _numForms = forms.Count;

            return _numForms;
        }
        public IGenField GetField(string formName, int fieldIndex)
        {
            IGenField _field = new IGenField();

            try
            {
                // find the form
                foreach (IGenForm _form in forms)
                {
                    if (_form.name.ToUpper() == formName.ToUpper())
                    {
                        _field = _form.formFields.GetField(fieldIndex);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".GetField(s, i) > " + ex.Message);
            }

            return _field;
        }
        public Control CreateFieldControl(Control pallet,
                                   IGenField field)
        {
            Control _control = new Control();

            try
            {
                _control = CreateFieldControl(pallet, field, false);
            }
            catch (Exception ex)
            {
                CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".CreateFieldControl(c, o) > " + ex.Message);
            }

            return _control;
        }
        public Control CreateFieldControl(Control pallet,
                                   IGenField field,
                                   bool runMode)
        {
            Control _control = new Control();
            Image _img = null;
            string _list = "";

            try
            {
                bool _makeOpaque = (ConfigRoutines.GetSetting("MakeLabelsOpaque").ToUpper().IndexOf('T') == 0) ? true : false;

                // create a control based on the field type
                _control = CreateControlOfType(field.type, runMode);

                _control.Name = field.name;
                _control.Text = field.text;

                if (runMode)
                {
                    switch (field.dataType.ToUpper())
                    {
                        case "TEXT":
                            break;

                        case "INTEGER":
                        case "NUMERIC":
                        case "CURRENCY":
                        case "DECIMAL":
                            _control.Text = IGenFormCommonRoutines.FormatValue(field.text, field.dataType, -1, field.formatMask);
                            field.alignment = "Right";
                            break;

                    }
                }
                _control.Left = field.left;
                _control.Top = field.top;
                _control.Width = field.width;
                _control.Height = field.height;
                _control.Parent = pallet;

                if (runMode)
                {
                    _control.BackColor = Color.LightBlue;
                }
                else
                {
                    if (field.layer > 0 && field.layer <= 7)
                    {
                        _control.BackColor = IGenFormCommonRoutines._layerColors[field.layer];
                    }
                    else
                    {
                        // if it is a enterable control, then show it as the nonSelectEnterableColor
                        switch (field.type.ToUpper())
                        {
                            case "LABEL":
                                _control.BackColor = nonSelectedColor;
                                break;

                            case "BUTTON":
                                _control.BackColor = buttonColor;
                                break;

                            case "TEXTBOX":
                                _control.BackColor = nonEnterableSelectedColor;
                                break;

                            case "COMBOBOX":
                                _control.BackColor = comboBoxColor;
                                break;

                            case "CHECKBOX":
                                _control.BackColor = checkBoxColor;
                                break;

                            default:
                                _control.BackColor = nonEnterableSelectedColor;
                                break;

                        }
                    }
                }

                if (runMode) //if (IGenFormCommonRoutines.runMode)
                {
                    _control.Visible = field.visible;
                }
                else
                {
                    _control.Visible = true;
                }

                // set the font
                string _fontName = field.fontName;
                if (_fontName != "")
                {
                    float _fontSize = field.fontSize;
                    if (_fontSize > 0)
                    {
                        Font _font = new Font(_fontName, _fontSize);
                        _control.Font = _font;
                    }
                }

                // format it

                field.controlContainer = _control;
                field.SetHandles();

                if (runMode)
                {
                    field.HideHandles();
                }

                _control.Tag = field;

                // add to the forms fields collection

                //_control.Enabled = false;
                //_control.BackColor = nonSelectedColor;

                if (runMode)
                {
                    // check for special types of fields
                    switch (field.type.ToUpper())
                    {
                        case "CHECKBOX":
                            CheckBox _checkBox = (CheckBox)_control;
                            _checkBox.Checked = field.checkedFlag;
                            break;

                        case "PICTURE":
                            // if the text var is not filled in, prompt for the image
                            PictureBox _pic = (PictureBox)_control;
                            string _fileName = field.imageName;
                            if (_fileName.IndexOf('\\') < 0)
                            {
                                _fileName = CommonRoutines.currentPath + "\\images\\" + _fileName;
                            }
                            if (CommonRoutines.FileExists(_fileName))
                            {
                                using (Image temp = Image.FromFile(_fileName))
                                {
                                    _pic.Image = new Bitmap(temp);
                                }
                                //_pic.ImageLocation = _fileName;
                            }
                            _pic.SizeMode = PictureBoxSizeMode.StretchImage;
                            break;

                        case "COMBOBOX":
                            ComboBox _comboBox = (ComboBox)_control;
                            // check to see if a list was given
                            _list = field.list;
                            if (_list != "")
                            {
                                // resolve the value
                                _comboBox.Items.Clear();

                                // is it a sql statement?
                                if (_list.ToUpper().IndexOf("=SQL(") == 0)
                                {
                                    // resolve it

                                }
                                string[] _listValues = _list.Split('|');
                                foreach (string _listValue in _listValues)
                                {
                                    _comboBox.Items.Add(_listValue);
                                }
                            }
                            // set the value
                            _comboBox.Text = field.comboValue;
                            break;

                        case "LISTBOX":
                            // check to see if a list was given
                            _list = field.list;
                            if (_list != "")
                            {
                                // resolve the value
                                ListBox _listBox = (ListBox)_control;
                                _listBox.Items.Clear();

                                string[] _listValues = _list.Split('|');
                                foreach (string _listValue in _listValues)
                                {
                                    _listBox.Items.Add(_listValue);
                                }
                            }
                            break;

                        case "TEXTBOX":
                            Label _textBox = (Label)_control;
                            if (field.alignment.ToUpper().IndexOf('C') == 0)
                            {
                                _textBox.TextAlign = ContentAlignment.BottomCenter;
                            }
                            else
                            {
                                if (field.alignment.ToUpper().IndexOf('R') == 0)
                                {
                                    _textBox.TextAlign = ContentAlignment.BottomRight;
                                }
                                else
                                {
                                    _textBox.TextAlign = ContentAlignment.BottomLeft;
                                }
                            }
                            break;

                        case "LABEL":
                            Label _label = (Label)_control;
                            if (runMode)
                            {
                                if (_makeOpaque)
                                {
                                    _label.BackColor = Color.White;
                                }
                                else
                                {
                                    _label.BackColor = Color.Transparent;
                                }
                            }
                            if (field.alignment.ToUpper().IndexOf('C') == 0)
                            {
                                _label.TextAlign = ContentAlignment.BottomCenter;
                            }
                            else
                            {
                                if (field.alignment.ToUpper().IndexOf('R') == 0)
                                {
                                    _label.TextAlign = ContentAlignment.BottomRight;
                                }
                                else
                                {
                                    _label.TextAlign = ContentAlignment.BottomLeft;
                                }
                            }
                            break;

                        case "SIGNATURE":
                            // create a sigplus control
                            //Signature _signature = (Signature)_control;
                            //_signature.BackColor = Color.LightBlue;
                            break;

                        default:
                            if (field.text == "")
                            {
                            }
                            break;

                    }
                }

                field.controlName = _control.Name;
                pallet.Controls.Add(_control);
                field.controlIndex = pallet.Controls.Count - 1;

            }
            catch (Exception ex)
            {
                CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".CreateFieldControl(c, o, b) > " + ex.Message);
            }

            return _control;
        }
        public IGenField GetField(string fieldName)
        {
            IGenField _field = new IGenField();

            try
            {
                _field = formFields.GetField(fieldName);
            }
            catch (Exception ex)
            {
                CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".GetField(s) > " + ex.Message);
            }

            return _field;
        }
        public int CompileFormValues(string useSchema)
        {
            int _numFields = 0;
            bool _fieldReferencesResolved = true;
            int _startIndex = 0;
            int _endIndex = 0;
            string _sql = "";
            string _tableName = "";
            string _schema = "";
            IGenForm _form = new IGenForm();
            IGenField _field = new IGenField();
            string _useSchema = useSchema.Trim();

            try
            {
                compileErrors = false;
                compileErrorsMessages.Clear();

                if (_useSchema.ToUpper() == "<DEFAULT>" || _useSchema == "")
                {
                    _useSchema = "dbo";
                }

                CommonRoutines.compileErrors.Items.Clear();

                DisplayProgress(0, 0);
                int _totalFieldsToCompile = 0;
                int _fieldToCompile = 0;

                CommonRoutines.compileErrors.Items.Add("Compiling...");

                sql = "";

                #region [Resolve Datasets]

                DisplayStatus("Resolving datasets...");

                foreach (IGenDataset _ds in datasets)
                {
                    if (_ds.sql != "")
                    {
                        // is this a WSTF table reference or a vw_WSTF view reference?
                        string _overrideSchema = ConfigRoutines.GetSetting("OverrideSchema").ToUpper();
                        if (_overrideSchema.IndexOf('T') == 0)
                        {
                            _sql = _ds.sql;
                            // get the table/view specified
                            int _fromOffset = _sql.ToUpper().IndexOf(" FROM ");
                            if (_fromOffset > 0)
                            {
                                // is there a where clause?
                                int _whereOffset = _sql.ToUpper().IndexOf(" WHERE ", _fromOffset);
                                if (_whereOffset > 0)
                                {
                                    _tableName = _sql.Substring(_fromOffset, _whereOffset - _fromOffset).Trim();
                                    // set a marker
                                    _sql = _sql.Substring(0, _fromOffset + 6) + "^" + _sql.Substring(_whereOffset);
                                }
                                else
                                {
                                    _tableName = _sql.Substring(_fromOffset);
                                    _sql = _sql.Substring(0, _fromOffset + 6) + "^";
                                }

                                _tableName = _tableName.Substring(5).Trim();
                                // is there a prefix on it?
                                int _lastPeriod = _tableName.LastIndexOf('.');
                                if (_lastPeriod > 0)
                                {
                                    _schema = _tableName.Substring(0, _lastPeriod);
                                    if (_schema.ToUpper() == "DBO")
                                    {
                                        _tableName = _tableName.Substring(_lastPeriod + 1);
                                        _schema = _useSchema;
                                    }
                                    else
                                    {
                                        _schema = "";
                                    }
                                }
                                else
                                {
                                    if (_tableName.ToUpper().IndexOf("WSTF") >= 0)
                                    {
                                        _schema = _useSchema;
                                    }
                                }

                                if (_schema != "")
                                {
                                    if (_tableName.ToUpper().IndexOf("WSTF") >= 0)
                                    {
                                        _tableName = "[" + _useSchema + "]." + _tableName;
                                    }
                                }

                                // replace the table
                                _sql = _sql.Replace("^", _tableName).Replace("..", ".");

                                _ds.sql = _sql;
                            }
                        }

                        // get the fields from the sql
                        string[] _fieldNames = DatabaseRoutines.GetSQLFields(_ds.sql);
                        _ds.fieldNames = _fieldNames;

                        // get a count of the rows this will find
                        _ds.numRows = _ds.GetRowCount();
                    }
                }

                #endregion

                #region [Compiling forms]

                DisplayStatus("Compiling forms...");

                // reset the forms with the original values
                for (int n = 0; n < forms.Count; n++)
                {
                    _form = forms[n];
                    _totalFieldsToCompile = _totalFieldsToCompile + _form.formFields.fields.Count;
                    _form.datasets.Clear();

                    // for each of the datasetordinals, create a dataset entry
                    for (int m = 0; m < _form.datasetOrdinals.Count; m++)
                    {
                        int _dsOrdinal = _form.datasetOrdinals[m];

                        if (_dsOrdinal < 0)
                        {
                            break;
                        }

                        if (m == 0)
                        {
                            _form.datasetOrdinal = _dsOrdinal;
                            _form.dataset = datasets[_dsOrdinal];
                        }

                        // clone a dataset
                        IGenDataset _formDataset = datasets[_dsOrdinal].Clone();
                        _formDataset.referenceDatasetOrdinal = _dsOrdinal;
                        _form.datasets.Add(_formDataset);
                    }

                    // set the page collection
                    if (_form.datasetOrdinal >= 0)
                    {
                        _form.DeterminePages();
                    }

                    for (int m = 0; m < _form.formFields.fields.Count; m++)
                    {
                        // put here to fix the first textbox not retaining entered values
                        _field = _form.formFields.fields[m];

                        switch (_field.type.ToUpper())
                        {
                            case "TEXTBOX":
                            case "COMBOBOX":
                            case "CHECKBOX":
                                break;

                            default:
                                _field.compiledValue = _field.originalValue;
                                _field.value = _field.originalValue;
                                _field.text = "";
                                _form.formFields.fields[m] = _field;
                                break;

                        }
                    }
                }

                #endregion

                bool _needsResolving = true;

                while (_needsResolving)
                {
                    _fieldReferencesResolved = true;
                    _needsResolving = false;

                    for (int n = 0; n < forms.Count; n++)
                    {
                        _form = forms[n];

                        // for each field, resolve any [field] references to their {formnumber:fieldnumber}
                        for (int m = 0; m < _form.formFields.fields.Count; m++)
                        {
                            // get the value
                            _field = _form.formFields.fields[m];

                            _fieldToCompile = _fieldToCompile + 1;
                            DisplayProgress(_fieldToCompile, _totalFieldsToCompile);

                            string _value = _field.compiledValue;
                            string _compiledValue = _value;
                            string _fieldName = _field.name;

                            if (_value.Trim() != "" && _field.type.ToUpper() != "TEXTBOX")
                            {
                                // now loop until no more fields references are found
                                _needsResolving = true;

                                DisplayStatus("Resolving field " + _field.name + ", value=" + _value);

                                while (_needsResolving)
                                {
                                    _needsResolving = false;
                                    bool _keepChecking = true;

                                    #region [Functions]
                                    // check for functions that need expanding
                                    if (_value.ToUpper().IndexOf("=SUM(") >= 0 || _value.IndexOf("=ADD(") >= 0 ||
                                                _value.ToUpper().IndexOf("=SUBTRACT(") >= 0 || _value.IndexOf("=SUB(") >= 0 ||
                                                _value.ToUpper().IndexOf("=MULTIPLY(") >= 0 || _value.IndexOf("=MULT(") >= 0 ||
                                                _value.ToUpper().IndexOf("=DIVIDE(") >= 0 || _value.IndexOf("=DIV(") >= 0)
                                    {
                                        _needsResolving = true;

                                        // get the start...
                                        int _index = _value.IndexOf('=');

                                        // sum the fields given
                                        _startIndex = _value.IndexOf('(');
                                        _endIndex = _value.IndexOf(')');
                                        string _fields = "";
                                        if (_endIndex <= _startIndex)
                                        {
                                            _needsResolving = false;
                                            compileErrors = true;
                                            compileErrorsMessages.Add(_fieldName + " has missing ending paren: " + _compiledValue);
                                        }

                                        if (_endIndex > _startIndex)
                                        {
                                            _fields = _value.Substring(_startIndex + 1, _endIndex - _startIndex - 1);
                                            if (_fields != "")
                                            {
                                                if (_fields.IndexOf('*') >= 0)
                                                {
                                                    // get the list, exclude the field on if it is part of the pattern
                                                    _fields = GetFieldNames(forms[n].name, _fieldName, _fields);
                                                    // not parse it out and create [field] + [field] + [field] + ...
                                                    string[] _parts = _fields.Split('~');
                                                    _fields = "";
                                                    for (int k = 0; k < _parts.Length; k++)
                                                    {
                                                        if (_parts[k].Trim() != "")
                                                        {
                                                            //_fields = _fields + "[" + _parts[k].Trim() + "] + ";
                                                            // now determine the operator
                                                            if (_value.ToUpper().IndexOf("=SUM(") >= 0 || _value.IndexOf("=ADD(") >= 0)
                                                            {
                                                                _fields = _fields + "[" + _parts[k].Trim() + "] + ";
                                                            }
                                                            else
                                                            {
                                                                if (_value.ToUpper().IndexOf("=SUBTRACT(") >= 0 || _value.IndexOf("=SUB(") >= 0)
                                                                {
                                                                    _fields = _fields + "[" + _parts[k].Trim() + "] - ";
                                                                }
                                                                else
                                                                {
                                                                    if (_value.ToUpper().IndexOf("=MULTIPLY(") >= 0 || _value.IndexOf("=MULT(") >= 0)
                                                                    {
                                                                        _fields = _fields + "[" + _parts[k].Trim() + "] / ";
                                                                    }
                                                                    else
                                                                    {
                                                                        if (_value.ToUpper().IndexOf("=DIVIDE(") >= 0 || _value.IndexOf("=DIV(") >= 0)
                                                                        {
                                                                            _fields = _fields + "[" + _parts[k].Trim() + "] * ";
                                                                        }
                                                                    }
                                                                }

                                                            }
                                                        }
                                                    }

                                                    // get rid of the last +
                                                    if (_fields.Length > 0)
                                                    {
                                                        _fields = _fields.Substring(0, _fields.Length - 2);
                                                    }
                                                }
                                                else
                                                {
                                                    char _listChar = ' ';
                                                    // is there a list (separated by comma)
                                                    if (_fields.IndexOf(',') > 0)
                                                    {
                                                        _listChar = ',';
                                                    }
                                                    else
                                                    {
                                                        if (_fields.IndexOf('+') > 0)
                                                        {
                                                            _listChar = '+';
                                                        }
                                                    }
                                                    if (_listChar != ' ')
                                                    {
                                                        string[] _fieldList = _fields.Split(_listChar);
                                                        if (_fieldList.Length > 1)
                                                        {
                                                            _fields = "";
                                                            for (int j = 0; j < _fieldList.Length; j++)
                                                            {
                                                                if (_fieldList[j].Trim() != "")
                                                                {
                                                                    _fields = _fields + "[" + _fieldList[j] + "] + ";
                                                                }
                                                            }
                                                            // get rid of the last +
                                                            _fields = _fields.Substring(0, _fields.Length - 2);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        _fields = "[" + _fields + "]";
                                                    }
                                                }

                                                // now replace the functional part
                                                if (_index == 0)
                                                {
                                                    _value = _fields + _value.Substring(_endIndex + 1);
                                                }
                                                else
                                                {
                                                    _value = _value.Substring(0, _index) + _fields + _value.Substring(_endIndex + 1);
                                                }
                                                if (_value.Length > 0)
                                                {
                                                    _value = "=" + _value;
                                                }
                                            }
                                            else
                                            {
                                                _value = "";
                                            }

                                            // replace the compiled value
                                            _field = _form.formFields.fields[m];
                                            _field.compiledValue = _value;
                                            _field.text = "";
                                            _form.formFields.fields[m] = _field;

                                        }
                                    }

                                    #endregion

                                    // check for fields
                                    if (_value.IndexOf('[') >= 0)
                                    {
                                        IGenField _tempField = _form.formFields.fields[m];
                                        // resolve the value to it's compiled format
                                        string _tempValue = ResolveFields(_tempField);
                                        _value = _tempValue;
                                        _fieldReferencesResolved = false;
                                        _numFields = _numFields + 1;
                                        _needsResolving = true;
                                    }

                                    #region [Check for GDS(]

                                    // check for GDS columns
                                    if (_value.ToUpper().IndexOf("GDS(") >= 0)
                                    {
                                        if (datasetName != "")
                                        {
                                            // replace the compiled value
                                            _field = _form.formFields.fields[m];
                                            _value = _field.value;
                                            // datasetname here is the default one specified for the form group
                                            _value = ResolveDS(datasetName, _field, _value, "GDS(");
                                        }
                                        else
                                        {
                                            _value = "";
                                        }
                                        _field.compiledValue = _value;
                                        _field.text = "";
                                        _form.formFields.fields[m] = _field;
                                        _keepChecking = false;
                                    }

                                    #endregion [Check for GDS(]

                                    #region [Check for DS(]

                                    if (_value.ToUpper().IndexOf("DS(") >= 0)
                                    {
                                        // replace the compiled value
                                        _field = _form.formFields.fields[m];
                                        //_value = _field.value;
                                        _value = ResolveDS(_form.datasetName, _field, _value, "DS(");
                                        _field.compiledValue = _value;
                                        _field.text = "";
                                        _form.formFields.fields[m] = _field;
                                        _keepChecking = false;
                                    }
                                    #endregion [Check for DS(]

                                    #region [Check for DSLOOKUP(]

                                    if (_value.ToUpper().IndexOf("DSLOOKUP(") >= 0)
                                    {
                                        // syntax:  DSLOOKUP(<dsname|dsordinal!>columnname, filter>
                                        // replace the compiled value
                                        _field = _form.formFields.fields[m];
                                        //_value = _field.value;
                                        _value = ResolveDS(_form.datasetName, _field, _value, "DSLOOKUP(");
                                        _field.compiledValue = _value;
                                        _field.text = "";
                                        _form.formFields.fields[m] = _field;
                                        _keepChecking = false;
                                    }
                                    #endregion [Check for DSLOOKUP(]

                                    #region [Check for PAGEBREAK(]

                                    if (_value.ToUpper().IndexOf("PAGEBREAK(") >= 0)
                                    {
                                        // syntax:  PAGEBREAK(dsname|dsordinal!, true stmt, false stmt>
                                        // replace the compiled value
                                        _field = _form.formFields.fields[m];
                                        //_value = _field.value;
                                        _value = ResolveDS(_form.datasetName, _field, _value, "PAGEBREAK(");
                                        _field.compiledValue = _value;
                                        _field.text = "";
                                        _form.formFields.fields[m] = _field;
                                        _keepChecking = false;
                                    }
                                    #endregion [Check for PAGEBREAK(]

                                    if (_keepChecking)
                                    {
                                        // regular
                                        #region [No functions found]
                                        // replace the compiled value
                                        _field = _form.formFields.fields[m];
                                        _field.compiledValue = _value;
                                        _field.text = "";
                                        _form.formFields.fields[m] = _field;
                                        #endregion
                                    }
                                }
                            }

                        }
                    }
                }

                DisplayStatus("Done!");

                DisplayProgress(0, 0);

                // were there any errors?
                if (compileErrors)
                {
                    DisplayStatus("Compiled with errors.");
                    CommonRoutines.compileErrors.Items.Add("Compiled with errors");

                    if (CommonRoutines.compileErrors == null)
                    {
                        CommonRoutines.compileErrors = new ListBox();
                    }
                    // output the errors
                    if (compileErrorsMessages.Count > 0)
                    {
                        // write out the compile errors.
                        CommonRoutines.WriteFile(".\\compile_errors.txt", compileErrorsMessages);

                        for (int n = 0; n < compileErrorsMessages.Count; n++)
                        {
                            CommonRoutines.compileErrors.Items.Add(compileErrorsMessages[n]);
                        }

                    }

                }

                CommonRoutines.compileErrors.Items.Add("Done.");

            }
            catch (Exception ex)
            {
                CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".CompileFormValues > " + ex.Message);
            }

            // output the fields for debugging
            OutputFormInfo("CompiledFields.txt");

            return _numFields;
        }
        public void AddField(IGenField field)
        {
            try
            {
                formFields.AddField(field);
            }
            catch (Exception ex)
            {
                CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".AddField > " + ex.Message);
            }

            return;
        }
        public IGenField GetField(int fieldIndex)
        {
            IGenField _field = new IGenField();

            try
            {
                if (fieldIndex >= 0 && fieldIndex < fields.Count)
                {
                    _field = fields[fieldIndex];
                }
            }
            catch (Exception ex)
            {
                CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".GetField(s, i) > " + ex.Message);
            }

            return _field;
        }
        public IGenField GetField(string fieldName)
        {
            IGenField _field = new IGenField();

            try
            {
                foreach (IGenField _igenField in fields)
                {
                    if (_igenField.name.ToUpper() == fieldName.ToUpper())
                    {
                        _field = _igenField;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".GetField(s, s) > " + ex.Message);
            }

            return _field;
        }
        public IGenField AddField(string fieldName,
                                   string fieldType,
                                   string fieldText,
                                   string fieldValue,
                                   Image fieldImage,
                                   Rectangle rect,
                                   string innerType)
        {
            IGenField _field = new IGenField();

            try
            {
                // create a field object and attach it
                _field = new IGenField(fieldName, fieldType, fieldText, rect.X, rect.Y, rect.Width, rect.Height, innerType);
                _field.name = fieldName;
                _field.type = fieldType;
                _field.text = fieldText;
                _field.value = fieldValue;
                _field.left = rect.X;
                _field.top = rect.Y;
                _field.width = rect.Width;
                _field.height = rect.Height;
                _field.innerType = innerType;
                // add to the fields collection
                AddField(_field);

            }
            catch (Exception ex)
            {
                CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".Createfield > " + ex.Message);
            }

            return _field;
        }
        public frmViewer(TabPage showTabPage)
        {
            InitializeComponent();

            try
            {
                InitializeApp();

                // set the page
                tabForms.TabPages.Clear();
                // create a tab from the one sent
                TabPage _newTabPage = new TabPage();
                // first control on the tab is it's pallet
                PictureBox _pallet = (PictureBox)showTabPage.Controls[0];
                PictureBox _newPallet = new PictureBox();
                _newPallet.Image = _pallet.Image;
                _newPallet.Name = _pallet.Name;
                _newPallet.Tag = _pallet.Tag;
                _newPallet.Top = _pallet.Top;
                _newPallet.Left = _pallet.Left;
                _newPallet.Width = _pallet.Width;
                _newPallet.Height = _pallet.Height;
                _newPallet.Visible = true;

                _newTabPage.Controls.Add(_newPallet);
                _newTabPage.Text = showTabPage.Text;
                _newTabPage.Name = showTabPage.Name;
                _newTabPage.Tag = showTabPage.Tag;

                foreach (Control _control in _pallet.Controls)
                {
                    Label _newControl = new Label();
                    IGenField _field = new IGenField();
                    _newControl.Name = _control.Name;
                    _newControl.Text = _control.Text;
                    _newControl.Tag = _control.Tag;
                    _newControl.Left = _control.Left;
                    _newControl.Top = _control.Top;
                    _newControl.Width = _control.Width;
                    _newControl.Height = _control.Height;
                    _newControl.BackColor = _control.BackColor;
                    _newControl.ForeColor = _control.ForeColor;
                    _newControl.Font = _control.Font;
                    _newControl.Visible = true;
                    _newControl.BringToFront();

                    if (_control.Tag != null)
                    {
                        _field = (IGenField)_control.Tag;
                        // determine the alignment
                        switch (_field.dataType.ToUpper())
                        {
                            case "NUMERIC":
                                _newControl.TextAlign = ContentAlignment.BottomRight;
                                break;

                            default:
                                _newControl.TextAlign = ContentAlignment.BottomLeft;
                                break;
                        }

                    }

                    _newPallet.Controls.Add(_newControl);
                }
                tabForms.TabPages.Add(_newTabPage);
                ShowNavigationButtons();

                // hide the buttons
                tbrMainSave.Visible = false;
                toolStripSeparator1.Visible = false;
                tbrMainPrint.Visible = false;
                tbrMainNavSep2.Visible = false;
                ResizeControls();
            }
            catch (Exception ex)
            {
                CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".frmViewer > " + ex.Message);
            }

            return;
        }