function cs_design_store($id, $fieldname, $fieldid, $arguments, $default, &$script)
{
    global $prefs;
    if ($prefs['storedsearch_enabled'] != 'y') {
        return;
    }
    $document = new DOMDocument();
    $element = $document->createElement('input');
    $element->setAttribute('type', 'submit');
    cs_design_setbasic($element, $fieldid, $fieldname, $arguments);
    $document->appendChild($element);
    $script .= "\n\n\$('#{$fieldid}').click(function() {\n\t\$(this).serviceDialog({\n\t\ttitle: \$(this).val(),\n\t\tcontroller: 'search_stored',\n\t\taction: 'select',\n\t\tsuccess: function (data) {\n\t\t\tcustomsearch.store_query = data.queryId;\n\t\t\tcustomsearch.load();\n\t\t}\n\t});\n\treturn false;\n});\n";
    return $document->saveHTML();
}
function cs_design_select($id, $fieldname, $fieldid, $arguments, $default, &$script)
{
    $document = new DOMDocument();
    $element = $document->createElement('select');
    cs_design_setbasic($element, $fieldid, $fieldname, $arguments);
    $document->appendChild($element);
    if (isset($arguments['_labels'])) {
        $labels = explode(',', $arguments['_labels']);
    }
    if (isset($arguments['_options'])) {
        $options = explode(',', $arguments['_options']);
    } else {
        $options = array();
    }
    if (isset($arguments['_mandatory']) && $arguments['_mandatory'] == 'y') {
        $mandatory = true;
    } else {
        $mandatory = false;
    }
    // leave a blank one in the front
    if (!$mandatory && !isset($arguments['multiple']) && !isset($arguments['size']) || isset($arguments['_firstlabel'])) {
        if (!empty($arguments['_firstlabel'])) {
            $label = $arguments['_firstlabel'];
        } else {
            $label = '';
        }
        $option = $document->createElement('option', $label);
        $option->setAttribute('value', '');
        $element->appendChild($option);
    }
    $script .= "\n\$('#{$fieldid}').change(function() {\n\tcustomsearch.add('{$fieldid}', {\n\t\tconfig: " . json_encode($arguments) . ",\n\t\tname: 'select',\n\t\tvalue: \$(this).val()\n\t});\n});\n";
    foreach ($options as $k => $opt) {
        if (!empty($labels[$k])) {
            $body = $labels[$k];
        } else {
            $body = $opt;
        }
        $option = $document->createElement('option', $body);
        $option->setAttribute('value', $opt);
        if ($default && in_array($opt, (array) $default)) {
            $option->setAttribute('selected', 'selected');
            $script .= "\n\$('#{$fieldid}').trigger('change');\n";
        }
        $element->appendChild($option);
    }
    return $document->saveHTML();
}
function cs_design_select($id, $fieldname, $fieldid, $arguments, $default, &$script, &$groups, $autosearchdelay = 0)
{
	$document = new DOMDocument;
	$element = $document->createElement('select');
	cs_design_setbasic($element, $fieldid, $fieldname, $arguments);
	$document->appendChild($element);

	if (isset($arguments['_labels'])) {
		$labels = explode(',', $arguments['_labels']);
	}
	if (isset($arguments['_options'])) {
		$options = explode(',', $arguments['_options']);
	} else {
		$options = array();
	}
	if (isset($arguments['_mandatory']) && $arguments['_mandatory'] == 'y') {
		$mandatory = true;
	} else {
		$mandatory = false;
	}
	// leave a blank one in the front
	if (!$mandatory && !isset($arguments['multiple']) && !isset($arguments['size']) || isset($arguments['_firstlabel'])) {
		if (!empty($arguments['_firstlabel'])) {
			$label = $arguments['_firstlabel'];
		} else {
			$label = '';
		}
		$option = $document->createElement('option', $label);
		$element->appendChild($option);
	}

	$script .= "$('#$fieldid').change(function() {";
	if ($autosearchdelay) {
		$script .= "	if (typeof(customsearch_timeout_$id)!='undefined') clearTimeout(customsearch_timeout_$id);\n";
	}
	$script .= "	var filter = new Object();
	filter.config = " . json_encode($arguments) . ";
	filter.name = 'select';
	filter.value = $(this).val();
	add_customsearch_$id('$fieldid', filter);
";
	if ($autosearchdelay) {
		$script .= "	if (!customsearch_quiet_$id) customsearch_timeout_$id = setTimeout('$(\'#customsearch_$id\').submit()', $autosearchdelay);\n";
	} 
	$script .= "});\n";

	foreach ($options as $k => $opt) {
		if (!empty($labels[$k])) {
			$body = $labels[$k];
		} else {
			$body = $opt;
		}		
		$option = $document->createElement('option', $body);
		$option->setAttribute('value', $opt);
		if ($default && in_array($opt, (array) $default)) {
			$option->setAttribute('selected', 'selected');
			$script .= "	customsearch_quiet_$id = true;
$('#$fieldid').trigger('change');
customsearch_quiet_$id = false;
";
		}
		$element->appendChild($option); 
	}
	return $document->saveHTML(); 
}
function cs_design_select($id, $fieldname, $fieldid, $arguments, $default, &$script)
{
    $document = new DOMDocument();
    $element = $document->createElement('select');
    cs_design_setbasic($element, $fieldid, $fieldname, $arguments);
    $document->appendChild($element);
    if (isset($arguments['_labels'])) {
        $labels = explode(',', $arguments['_labels']);
    }
    if (isset($arguments['_options'])) {
        $options = explode(',', $arguments['_options']);
    } else {
        $options = array();
    }
    // get the options for an ItemLink field - needs _trackerId and _field set in the {select} plugin
    if (empty($options) && empty($labels) && isset($arguments['_field']) && strpos($arguments['_field'], 'tracker_field_') === 0 && !empty($arguments['_trackerId'])) {
        $definition = Tracker_Definition::get($arguments['_trackerId']);
        $field = $definition->getFieldFromPermName(str_replace('tracker_field_', '', $arguments['_field']));
        if ($field['type'] === 'r') {
            $handler = TikiLib::lib('trk')->get_field_handler($field);
            $labels = $handler->getItemList();
            $options = array_keys($labels);
            $labels = array_values($labels);
        }
    }
    if (isset($arguments['_mandatory']) && $arguments['_mandatory'] == 'y') {
        $mandatory = true;
    } else {
        $mandatory = false;
    }
    // leave a blank one in the front
    if (!$mandatory && !isset($arguments['multiple']) && !isset($arguments['size']) || isset($arguments['_firstlabel'])) {
        if (!empty($arguments['_firstlabel'])) {
            $label = $arguments['_firstlabel'];
        } else {
            $label = '';
        }
        $option = $document->createElement('option', $label);
        $option->setAttribute('value', '');
        $element->appendChild($option);
    }
    $script .= "\n\$('#{$fieldid}').change(function() {\n\tcustomsearch.add('{$fieldid}', {\n\t\tconfig: " . json_encode($arguments) . ",\n\t\tname: 'select',\n\t\tvalue: \$(this).val()\n\t});\n});\n";
    foreach ($options as $k => $opt) {
        if (!empty($labels[$k])) {
            $body = $labels[$k];
        } else {
            $body = $opt;
        }
        $option = $document->createElement('option', $body);
        $option->setAttribute('value', $opt);
        if ($default && in_array($opt, (array) $default)) {
            $option->setAttribute('selected', 'selected');
            $script .= "\n\$('#{$fieldid}').trigger('change');\n";
        }
        $element->appendChild($option);
    }
    return $document->saveHTML();
}