/**
  * Return HTML form element for editing.
  *
  * @param array $pa_element_info An array of information about the metadata element being edited
  * @param array $pa_options array Options include:
  *			usewysiwygeditor = overrides element level setting for visual text editor [Default=false]
  *			forSearch = settings and options regarding visual text editor are ignored [Default=false]
  *			class = the CSS class to apply to all visible form elements [Default=null]
  *			width = the width of the form element [Default=field width defined in metadata element definition]
  *			height = the height of the form element [Default=field height defined in metadata element definition]
  *			t_subject = an instance of the model to which the attribute belongs; required if suggestExistingValues lookups are enabled [Default is null]
  *			request = the RequestHTTP object for the current request; required if suggestExistingValues lookups are enabled [Default is null]
  *			suggestExistingValues = suggest values based on existing input for this element as user types [Default is false]		
  *
  * @return string
  */
 public function htmlFormElement($pa_element_info, $pa_options = null)
 {
     $va_settings = $this->getSettingValuesFromElementArray($pa_element_info, array('fieldWidth', 'fieldHeight', 'minChars', 'maxChars', 'suggestExistingValues', 'usewysiwygeditor', 'isDependentValue', 'dependentValueTemplate'));
     if (isset($pa_options['usewysiwygeditor'])) {
         $va_settings['usewysiwygeditor'] = $pa_options['usewysiwygeditor'];
     }
     if (isset($pa_options['forSearch']) && $pa_options['forSearch']) {
         unset($va_settings['usewysiwygeditor']);
     }
     $vs_width = trim(isset($pa_options['width']) && $pa_options['width'] > 0 ? $pa_options['width'] : $va_settings['fieldWidth']);
     $vs_height = trim(isset($pa_options['height']) && $pa_options['height'] > 0 ? $pa_options['height'] : $va_settings['fieldHeight']);
     $vs_class = trim(isset($pa_options['class']) && $pa_options['class'] ? $pa_options['class'] : '');
     $vs_element = '';
     if (!preg_match("!^[\\d\\.]+px\$!i", $vs_width)) {
         $vs_width = (int) $vs_width * 6 . "px";
     }
     if (!preg_match("!^[\\d\\.]+px\$!i", $vs_height)) {
         $vs_height = (int) $vs_height * 16 . "px";
     }
     if ($va_settings['usewysiwygeditor']) {
         $o_config = Configuration::load();
         if (!is_array($va_toolbar_config = $o_config->getAssoc('wysiwyg_editor_toolbar'))) {
             $va_toolbar_config = array();
         }
         AssetLoadManager::register("ckeditor");
         $vs_element = "<script type='text/javascript'>jQuery(document).ready(function() {\n\t\t\t\t\t\tvar ckEditor = CKEDITOR.replace( '{fieldNamePrefix}" . $pa_element_info['element_id'] . "_{n}',\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttoolbar : " . json_encode(array_values($va_toolbar_config)) . ", /* this does the magic */\n\t\t\t\t\t\t\twidth: '{$vs_width}',\n\t\t\t\t\t\t\theight: '{$vs_height}',\n\t\t\t\t\t\t\ttoolbarLocation: 'top',\n\t\t\t\t\t\t\tenterMode: CKEDITOR.ENTER_BR\n\t\t\t\t\t\t});\n\t\t\t\t\t\t\n\t\t\t\t\t\tckEditor.on('instanceReady', function(){ \n\t\t\t\t\t\t\t ckEditor.document.on( 'keydown', function(e) {if (caUI && caUI.utils) { caUI.utils.showUnsavedChangesWarning(true); } });\n\t\t\t\t\t\t});\n \t});\t\t\t\t\t\t\t\t\t\n</script>";
     }
     $va_opts = array('size' => $vs_width, 'height' => $vs_height, 'value' => '{{' . $pa_element_info['element_id'] . '}}', 'maxlength' => $va_settings['maxChars'], 'class' => $vs_class, 'id' => '{fieldNamePrefix}' . $pa_element_info['element_id'] . '_{n}', 'class' => "{$vs_class}" . ($va_settings['usewysiwygeditor'] ? " ckeditor" : ''));
     if (caGetOption('readonly', $pa_options, false)) {
         $va_opts['disabled'] = 1;
     }
     $vs_element .= caHTMLTextInput('{fieldNamePrefix}' . $pa_element_info['element_id'] . '_{n}', $va_opts);
     if ($va_settings['isDependentValue']) {
         AssetLoadManager::register('displayTemplateParser');
         $t_element = new ca_metadata_elements($pa_element_info['element_id']);
         $va_elements = $t_element->getElementsInSet($t_element->getHierarchyRootID());
         $va_element_dom_ids = array();
         foreach ($va_elements as $vn_i => $va_element) {
             if ($va_element['datatype'] == __CA_ATTRIBUTE_VALUE_CONTAINER__) {
                 continue;
             }
             $va_element_dom_ids[$va_element['element_code']] = "#{fieldNamePrefix}" . $va_element['element_id'] . "_{n}";
         }
         $vs_element .= "<script type='text/javascript'>jQuery(document).ready(function() {\n \t\t\t\t\tjQuery('#{fieldNamePrefix}" . $pa_element_info['element_id'] . "_{n}').html(caDisplayTemplateParser.processDependentTemplate('" . addslashes($va_settings['dependentValueTemplate']) . "', " . json_encode($va_element_dom_ids, JSON_FORCE_OBJECT) . "));\n \t\t\t\t";
         $vs_element .= "jQuery('" . join(", ", $va_element_dom_ids) . "').bind('keyup', function(e) { \n \t\t\t\t\tjQuery('#{fieldNamePrefix}" . $pa_element_info['element_id'] . "_{n}').html(caDisplayTemplateParser.processDependentTemplate('" . addslashes($va_settings['dependentValueTemplate']) . "', " . json_encode($va_element_dom_ids, JSON_FORCE_OBJECT) . "));\n \t\t\t\t});";
         $vs_element .= "});</script>";
     }
     $vs_bundle_name = $vs_lookup_url = null;
     if (isset($pa_options['t_subject']) && is_object($pa_options['t_subject'])) {
         $vs_bundle_name = $pa_options['t_subject']->tableName() . '.' . $pa_element_info['element_code'];
         if ($pa_options['request']) {
             if (isset($pa_options['lookupUrl']) && $pa_options['lookupUrl']) {
                 $vs_lookup_url = $pa_options['lookupUrl'];
             } else {
                 $vs_lookup_url = caNavUrl($pa_options['request'], 'lookup', 'AttributeValue', 'Get', array('max' => 500, 'bundle' => $vs_bundle_name));
             }
         }
     }
     if ($va_settings['suggestExistingValues'] && $vs_lookup_url && $vs_bundle_name) {
         $vs_element .= "<script type='text/javascript'>\n \t\t\t\t\tjQuery('#{fieldNamePrefix}" . $pa_element_info['element_id'] . "_{n}').autocomplete( \n\t\t\t\t\t\t{ \n\t\t\t\t\t\t\tsource: '{$vs_lookup_url}',\n\t\t\t\t\t\t\tminLength: 3, delay: 800\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n \t\t\t\t</script>\n";
     }
     return $vs_element;
 }