[update values]
[if cgi ui_clone_options]
[and cgi ui_clone_id]
[perl interpolate=1 tables="[cgi mv_data_table]"]
	my $db = $Db{[cgi mv_data_table]}
		or return;
	my ($k,$v);
	$db->clone_row($CGI->{ui_clone_id}, $CGI->{sku});
	$db->clone_set('sku', $CGI->{ui_clone_id}, $CGI->{sku});
	return;
[/perl]
[/if]

[if cgi sku]
    [tag flag write]options[/tag]
    [perl tables="options __UI_ITEM_TABLES__"]
        my $otab = 'options';
        my $odb = $Db{$otab};

        foreach(sort keys %{$CGI}) {
            next unless /^opt_group_(.*)/;
            my $key = $1;

            my $name = $CGI->{"opt_group_$key"};
            my $value = $CGI->{"opt_value_$key"};
            my $label = $CGI->{"opt_label_$key"};

            next unless $name && $value;

            unless($key) { 	
				if($name =~ /\W/) {
					$Tag->error({
						name => 'Set option groups',
						set => errmsg(qq{Group name "%s" contains non-word characters, will not set}, $name),
					});
					next;
				}
				$key = $CGI->{sku}."-$name"; 
			}

            my @value = split("\r\n",$value);

            my %seen = ();
            my $hasdefault = 0;

            my($left,$right);
            map {
                my $default = 0;
                s/[,\r\n]//g;
                if(s/\*//g) { $default = 1; $hasdefault = 1; }

                if($v) {
                    if(/=/) {
                        ($left,$right) = split('=',$_);
                    } else {
                        $right = $_;
                        $left = substr($right,0,3);
                    }

                    while($seen{$left}++) { $left++; }

                    $_ = join('=',$left,$right);
                    if($default) { $_ .= "*"; }
                }
            } @value;

            my $value = join(",\n",@value);

			$key =~ s/_/-/g; # javascript won't handle form names with '-'

            $odb->set_field($key,'sku',$CGI->{sku});
            $odb->set_field($key,'o_group',$name);
            $odb->set_field($key,'o_value',$value);
            $odb->set_field($key,'o_widget','select');
			$odb->set_field($key,'o_label',$label);
        }

        return '';
    [/perl]
[/if]


<FORM ACTION="[area @@MV_PAGE@@]" METHOD="post">
[if scratch ui_failure]
<P>
<BLOCKQUOTE>
<FONT COLOR="__CONTRAST__">[scratch ui_failure][set ui_failure][/set]</FONT>
</BLOCKQUOTE>
<P>
&nbsp;
[/if]
[if scratch ui_message]
<P>
<BLOCKQUOTE>
<FONT COLOR="__CONTRAST__">[scratch ui_message][set ui_message][/set]</FONT>
</BLOCKQUOTE>
<P>
&nbsp;
[/if]
<INPUT TYPE=hidden NAME=sku              VALUE="[cgi item_id]">
<INPUT TYPE=hidden NAME=ui_page_title    VALUE="[cgi ui_page_title]">
<INPUT TYPE=hidden NAME=ui_page_title    VALUE="[cgi ui_page_banner]">
<INPUT TYPE=hidden NAME=ui_return_to     VALUE="@@MV_PAGE@@">
<INPUT TYPE=hidden NAME=mv_action        VALUE=back>

<TABLE BORDER=0><TR><TD VALIGN=TOP>

[query list=1 table=options sql="select * from options where sku='[filter op=sql interpolate=1][cgi item_id][/filter]' and o_group is not null"]
[list]
[if-sql-data options o_group]
[calc] $Scratch->{mod_code} = q{[sql-code]}; $Scratch->{mod_code} =~ s/-/_/g; return;[/calc]
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=3 BGCOLOR="[sql-alternate 2]__UI_T_ROW_EVEN__[else]__UI_T_ROW_ODD__[/else][/sql-alternate]">
<TR><TD VALIGN=CENTER>Name: <INPUT TYPE=text SIZE=20 NAME="opt_group_[scratch mod_code]" VALUE="[filter entities][sql-param o_group][/filter]">

<A HREF="[area href='@@MV_PAGE@@'
               form='deleterecords=1
                     ui_delete_id=[sql-code]
                     item_id=[cgi item_id]
                     mv_data_table=options
                     mv_click=db_maintenance
                     mv_action=back
                     mv_nextpage=@@MV_PAGE@@
                    '
         ]"><IMG SRC="delete.gif" ALT="[L]Delete[/L]" ALIGN=CENTER BORDER=0></A>
<br>[L]Label[/L]: <INPUT TYPE=text SIZE=20 NAME="opt_label_[scratch mod_code]" VALUE="[filter entities][sql-param o_label][/filter]">
<INPUT TYPE=hidden NAME="reset_[scratch mod_code]" VALUE="[filter entities][sql-param o_label][/filter]">
<script><!--
document.write('<br><INPUT TYPE=checkbox [sql-calc]q{[sql-param o_label]} eq q{[sql-param o_group]} ? 'CHECKED' : undef;[/sql-calc]\n' +
'	onClick="if (this.checked) { this.form.opt_label_[scratch mod_code].value = this.form.opt_group_[scratch mod_code].value; } else { this.form.opt_label_[scratch mod_code].value = this.form.reset_[scratch mod_code].value; }">\n' +
'<font size=2>[L]Set label to name[/L]</font>');
// -->
</script>
</TD></TR>
[tmp o_value][perl]
    my @vals = split(',',q{[sql-param o_value]});
    map { s/[\r\n]//g; } @vals;
    return join("\n",@vals);
[/perl][/tmp]

<TR><TD>
<TEXTAREA ROWS=5 COLS=30 NAME="opt_value_[scratch mod_code]">[scratch o_value]</TEXTAREA><br>
[page href="admin/flex_editor"
		form="
			mv_data_table=options
			item_id=[sql-code]
			ui_return_to=admin/item_option
			ui_return_to=item_id=[cgi item_id]
			ui_data_fields=code o_widget o_width o_height
		"]Widget type edit</A>
</TD></TR>
</TABLE>
[/if-sql-data]
[/list]
[/query]

<BR><BR><BR>
[button text="[L]Commit Changes[/L]"]
mv_nextpage=@@MV_PAGE@@
[/button]

</TD><TD><PRE>                          </PRE></TD><TD VALIGN=TOP>

<B>[L]Create a new option[/L]:</B><BR>
[L]Name[/L]: <INPUT TYPE=text SIZE=20 NAME="opt_group_" VALUE="">
<br>[L]Label[/L]: <INPUT TYPE=text SIZE=20 NAME="opt_label_">
<script><!--
document.write('<br><INPUT TYPE=checkbox\n' +
'	onClick="if (this.checked) { this.form.opt_label_.value = this.form.opt_group_.value; } else { this.form.opt_label_.value = \'\'; }">\n' +
'<font size=2>[L]Set label to name[/L]</font>');
// -->
</script>
<BR>
<TEXTAREA ROWS=5 COLS=30 NAME="opt_value_"></TEXTAREA>
<BR>
[button text="[L]Create option[/L]"][/button]
<BR><BR>

<HR>

<BR><BR><B>[L]Clone an existing option set[/L]:</B><BR>

[query
	table="[cgi mv_data_table]"
	list=1
	prefix=clone
	sql="select DISTINCT sku from [cgi mv_data_table] where o_group <> ''"
	more=1]
<SELECT NAME=ui_clone_id>
<OPTION VALUE=""> --
[list]
[if-clone-data options o_enable]
<OPTION VALUE="[clone-code]">[clone-filter 20][clone-description][/clone-filter]
[/if-clone-data]
[/list]
</SELECT>[more-list]<BR>[more]<BR>[/more-list][/query]&nbsp;[button text="[L]Clone options[/L]"]ui_clone_options=1[/button]<BR>
</FORM>

</TD></TR></TABLE>

