/// <summary>
        /// Saves the contents of the editor
        /// </summary>
        /// <param name="indexToSave">Index of the item to save.</param>
        public void Save(int indexToSave)
        {
            EditorComboBoxItem selectedItem = (EditorComboBoxItem)cbxFields.Items[indexToSave];
            string             checkCode    = txtTextArea.Text.Replace("\n", "\r\n");

            checkCode = checkCode.Trim(Environment.NewLine.ToCharArray());

            IMetadataProvider metadata = GetMetadata();

            if (cbxEvents.SelectedIndex == 0 && (selectedItem).IDVal is IFieldWithCheckCodeBefore)
            {
                Field beforeField = (selectedItem).IDVal as Field;
                ((IFieldWithCheckCodeBefore)beforeField).CheckCodeBefore = checkCode;
                metadata.CreateControlBeforeCheckCode(beforeField.Id, ((IFieldWithCheckCodeBefore)beforeField).CheckCodeBefore, this.view);
            }
            else if (cbxEvents.SelectedIndex == 1 && (selectedItem).IDVal is IFieldWithCheckCodeAfter)
            {
                Field afterField = (selectedItem).IDVal as Field;
                ((IFieldWithCheckCodeAfter)afterField).CheckCodeAfter = checkCode;
                metadata.CreateControlAfterCheckCode(afterField.Id, ((IFieldWithCheckCodeAfter)afterField).CheckCodeAfter, this.view);
            }
            else if (cbxEvents.SelectedIndex == 1 && (selectedItem).IDVal is IFieldWithCheckCodeClick)
            {
                Field afterField = (selectedItem).IDVal as Field;
                ((IFieldWithCheckCodeClick)afterField).CheckCodeClick = checkCode;
                metadata.CreateControlAfterCheckCode(afterField.Id, ((IFieldWithCheckCodeClick)afterField).CheckCodeClick, this.view);
            }

            #region Replaced this code with the code above using the interfaces
            //if (((EditorComboBoxItem)cbxFields.SelectedItem).IDVal is InputTextBoxField)
            //{
            //    if (cbxEvents.SelectedIndex == 0)
            //    {
            //        ((InputTextBoxField)((EditorComboBoxItem)cbxFields.SelectedItem).IDVal).CheckCodeBefore = txtTextArea.Text;
            //        metadata.CreateControlBeforeCheckCode(((InputTextBoxField)((EditorComboBoxItem)cbxFields.SelectedItem).IDVal).Id, txtTextArea.Text, this.View);
            //    }
            //    else
            //    {
            //        ((InputTextBoxField)((EditorComboBoxItem)cbxFields.SelectedItem).IDVal).CheckCodeAfter = txtTextArea.Text;
            //        metadata.CreateControlAfterCheckCode(((InputTextBoxField)((EditorComboBoxItem)cbxFields.SelectedItem).IDVal).Id, txtTextArea.Text, this.View);
            //    }
            //}
            //else if (((EditorComboBoxItem)cbxFields.SelectedItem).IDVal is TableBasedDropDownField)
            //{
            //    if (cbxEvents.SelectedIndex == 0)
            //    {
            //        ((TableBasedDropDownField)((EditorComboBoxItem)cbxFields.SelectedItem).IDVal).CheckCodeBefore = txtTextArea.Text;
            //        metadata.CreateControlBeforeCheckCode(((TableBasedDropDownField)((EditorComboBoxItem)cbxFields.SelectedItem).IDVal).Id, txtTextArea.Text, this.View);
            //    }
            //    else
            //    {
            //        ((TableBasedDropDownField)((EditorComboBoxItem)cbxFields.SelectedItem).IDVal).CheckCodeAfter = txtTextArea.Text;
            //        metadata.CreateControlAfterCheckCode(((TableBasedDropDownField)((EditorComboBoxItem)cbxFields.SelectedItem).IDVal).Id, txtTextArea.Text, this.View);
            //    }
            //}
            //else if (((EditorComboBoxItem)cbxFields.SelectedItem).IDVal is CommandButtonField)
            //{
            //    ((CommandButtonField)((EditorComboBoxItem)cbxFields.SelectedItem).IDVal).CheckCodeClick = txtTextArea.Text;
            //    metadata.CreateControlAfterCheckCode(((CommandButtonField)((EditorComboBoxItem)cbxFields.SelectedItem).IDVal).Id, txtTextArea.Text, this.View);
            //}
            //else if (((EditorComboBoxItem)cbxFields.SelectedItem).IDVal is CheckBoxField)
            //{
            //    ((CheckBoxField)((EditorComboBoxItem)cbxFields.SelectedItem).IDVal).CheckCodeAfter = txtTextArea.Text;
            //    metadata.CreateControlAfterCheckCode(((CheckBoxField)((EditorComboBoxItem)cbxFields.SelectedItem).IDVal).Id, txtTextArea.Text, this.View);
            //}
            //else if (((EditorComboBoxItem)cbxFields.SelectedItem).IDVal is OptionField)
            //{
            //    ((OptionField)((EditorComboBoxItem)cbxFields.SelectedItem).IDVal).CheckCodeAfter = txtTextArea.Text;
            //    metadata.CreateControlAfterCheckCode(((OptionField)((EditorComboBoxItem)cbxFields.SelectedItem).IDVal).Id, txtTextArea.Text, this.View);
            //}
            //else if (((EditorComboBoxItem)cbxFields.SelectedItem).IDVal is YesNoField)
            //{
            //    ((YesNoField)((EditorComboBoxItem)cbxFields.SelectedItem).IDVal).CheckCodeAfter = txtTextArea.Text;
            //    metadata.CreateControlAfterCheckCode(((YesNoField)((EditorComboBoxItem)cbxFields.SelectedItem).IDVal).Id, txtTextArea.Text, this.View);
            //}
            #endregion

            else if ((selectedItem).IDVal is Page)
            {
                if (cbxEvents.SelectedIndex == 0)
                {
                    ((Page)(selectedItem).IDVal).CheckCodeBefore = checkCode;
                    metadata.CreatePageBeforeCheckCode(((Page)(selectedItem).IDVal).Id, checkCode, this.View);
                }
                else
                {
                    ((Page)(selectedItem).IDVal).CheckCodeAfter = checkCode;
                    metadata.CreatePageAfterCheckCode(((Page)(selectedItem).IDVal).Id, checkCode, this.View);
                }
            }
            else if ((selectedItem).Type.Equals("View"))
            {
                if (cbxEvents.SelectedIndex == 0)
                {
                    ((View)(selectedItem).IDVal).CheckCodeBefore = checkCode;
                    metadata.CreateViewBeforeCheckCode(((View)(selectedItem).IDVal).Id, checkCode);
                }
                else
                {
                    ((View)(selectedItem).IDVal).WebSurveyId = checkCode;
                    metadata.CreateViewAfterCheckCode(((View)(selectedItem).IDVal).Id, checkCode);
                }
            }
            else if ((selectedItem).Type.Equals("Record"))
            {
                if (cbxEvents.SelectedIndex == 0)
                {
                    ((View)(selectedItem).IDVal).RecordCheckCodeBefore = checkCode;
                    metadata.CreateRecordBeforeCheckCode(((View)(selectedItem).IDVal).Id, checkCode);
                }
                else
                {
                    ((View)(selectedItem).IDVal).RecordCheckCodeAfter = checkCode;
                    metadata.CreateRecordAfterCheckCode(((View)(selectedItem).IDVal).Id, checkCode);
                }
            }
            else
            {
                ((View)(selectedItem).IDVal).CheckCodeVariableDefinitions = checkCode;
                metadata.CreateCheckCodeVariableDefinition(((View)(selectedItem).IDVal).Id, checkCode);
                //if (ShouldUndefineVariables())
                //{
                //    UndefineVariables();
                //}
                //LoadDeclaredVariables();
            }
        }