<!-- page_edit new ----- -->
[loop search="
        fi=backup
        co=yes
        sf=sku
        se=[data session arg]
        st=db
        tf=mod_time
        to=nr
        rf=code,page_name,mod_time
        "]
[on-match]
<P>
<b>[loc]Backup copies[/loc]</b>
<BLOCKQUOTE>
[/on-match]
[list]
[page href="__UI_BASE__/page_edit"
	  form="
		ui_page=[cgi ui_page]
		ui_page_backup=[loop-code]
	  "][loop-param page_name]</A> (save time [convert-date][loop-param mod_time][/convert-date](
            [page href=__UI_BASE__/page_edit_diff
					form="
						ui_page=[cgi ui_page]
						ui_page_backup=[loop-code]
					"]diff</A>,
            [page href=__UI_BASE__/page_edit_diff
					form="
						ui_page=[cgi ui_page]
						ui_page_backup=[loop-code]
						context=1
					"]context diff</A> from [cgi ui_page])<BR>
[/list]
[on-match]
</BLOCKQUOTE>
[/on-match]
[/loop]

<form action="[area ui]" method=POST ENCTYPE="multipart/form-data">
<input type=hidden name=mv_session_id value="[data session id]">
<input type=hidden name=mv_todo value=back>
<input type=hidden name=ui_template_version value="[version]">

<table __UI_T_PROPERTIES__>
<tr id=rborder>
<td colspan=2>
<img src="bg.gif" height=1>
</td>
</tr>

[perl tables="[list-databases] __UI_META_TABLE__"] 

	# Some inits
	my $template_dir = $Variable->{UI_TEMPLATE_DIR} || 'templates';
	my $out = '';
	my $ver = $Tag->version();

	sub tmp_error {
		my $msg = errmsg(@_);
		my $messages = join "\n--message--\n", @messages;
		return <<EOF;
<TR class=rerror><td colspan=2 class=cerror>$msg</td></tr>
<TR class=rerror><td colspan=2 class=cerror><XMP>$messages</XMP></td></tr>
EOF
	}

	my $current;
	if (! $CGI->{ui_page_backup}) {
		$current = $Scratch->{ui_current_content} || '<!-- content -->';
	}
	else {
		$current = tag_data('backup', 'page_text', $CGI->{ui_page_backup});
		$current = $Tag->filter('decode_entities', $current);
		if(! length($current)) {
			return tmp_error(
					"Backup retrieval (%s) failed.",
					$CGI->{ui_page_backup},
					);
		}
	}


	my %hidden;
	my $control_display =  $CGI->{ui_content_edit}
						|| $Values->{ui_content_edit}
						|| $Variable->{UI_CONTENT_EDIT}
						|| 'template page components content';
#Log("current begins: " . substr($current, 0, 1000) );


	# Probably should offload this to mv_click=process_filter
	my @filters = grep /^ui_filter:/, keys %$CGI;
	foreach my $key (@filters) {
		my $val = delete $CGI->{$key};
		$key =~ s/ui_filter://;
		next unless $val;
		next unless defined $CGI->{$key};
		$CGI->{$key} = $Tag->filter($val, $CGI->{$key}, $key);
	}

	my $new;
	my $ary;
	my $tref;

	if($CGI->{ui_template_change}) {
#Log("found change of template");
		$t_name = $CGI->{ui_template_change};
		$new = 1;
	}
	elsif ($Scratch->{ui_template_structure}) {
		$tref = delete $Scratch->{ui_template_structure};
		$t_name = $tref->{ui_template_name};
	}
	else {
		$t_name = $CGI->{ui_template};
	}

#Log("template reference: " . uneval($tref) );

	$t_name =~ s:.*/::;
	my $t_file = "$template_dir/$t_name";

	push @messages, "template: $t_name; t_file=$t_file";

	### Add some code here to check for existing templates...
	
	my $exists = -f $t_file;

	TEMPLATE_READ:  {
		last TEMPLATE_READ if $tref;
		($ary) = $Tag->read_ui_template("$template_dir/$t_name");
		$tref = shift @$ary;
		ref($tref) =~ /HASH/ 
			or return tmp_error(
					"Template read error reading '%s'.",
					$t_name,
				);
		last TEMPLATE_READ if $tref;
		$tref = {};
		my $name_top = uc($t_name) . "_TOP";
		my $name_bot = uc($t_name) . "_BOTTOM";
		$tref->{ui_template} = $t_name;
		$tref->{ui_template_name} = $t_name;
		$tref->{ui_template_layout} = "$name_top, UI_CONTENT, $name_bot";
		$tref->{ui_template_description} = $t_desc;

		$tref->{ui_definition} = $tref->{ui_short_definition} = <<EOF;
ui_template: $t_name;
ui_template_name: $t_name;
ui_template_layout: $name_top, UI_CONTENT, $name_bot
ui_template_description: $t_desc
ui_template_version: $ver

EOF
		my $template_input;
		my $content;
		if( $Values->{ui_upload_template}
			and $Tag->value_extended(
					{
						name => 'ui_upload_template',
						test => 'isfile',
					}
				)
			)							
		{
push @messages, "template from upload:\n$tref->{ui_definition}";
			$template_input = $Tag->value_extended( {
													name => 'ui_upload_template',
													file_contents => 1,
													});
			$template_input =~ s{(.*)(<!--\s+begin\s+content\s+-->)}{$2}is;
			$template_top = $1;
			$template_top =~ s/^\s+//;
			$template_top =~ s/\s+$//;
			$template_input =~ s{(<!--\s+end\s+content\s+-->)\n*(.*)}{$1\n}is;
			$template_bot = $2;
			$template_bot =~ s/^\s+//;
			$template_bot =~ s/\s+$//;
			$content = $template_input;
			$content =~ s/^\s*<!--+\s+begin\s+content\s+--+>(?:\s*\n+)?//i;
			$content =~ s/(?:\n+\s*)?<!--+\s+end\s+content\s+--+>\s*$//i;
			push @messages, "Content:\n$content";
		}
	
   }

	$t_desc = $tref->{ui_template_description} if ! $t_desc;

######## BEGIN INIT TEMPLATES
		my $sep_template = <<EOF;
<tr class=rheader>
<td class=cheader colspan=2>Component ~NUMBER~ (~NAME~)</td>
</tr>
<tr class=rnorm>
<td class=clabel>Select component</td>
<td class=cdata>
	<table width="100%">
	<tr>
		<td class=cdata width="50%">
			~NAMESEL~
		</td>
		<td class=clabel width="15%">
			Type
		</td>
		<td class=cdata width="35%">
			~TYPESEL~
		</td>
	</tr>
	</table>
</tr>
EOF

		my $break_template = <<EOF;
<tr class=cmarq>
<td colspan=2><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>
EOF

		my $title_template = <<EOF;
<tr class=rborder>
<td colspan=2><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>
<tr class=rtitle>
<td colspan=2 class=ctitle><span style="font-size: larger; font-weight: bold">~TITLE~</span></td>
</tr>
<tr class=rborder>
<td colspan=2><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>
EOF

		my $control_template = <<EOF;
<tr>
   <td class=clabel width="$Variable->{UI_LEFT_WIDTH}"> 
     \$LABEL\$~META~
   </td>
   <td class=cdata> 
     <table cellspacing=0 cellmargin=0 width="100%">
       <tr> 
         <td class=cwidget> 
           \$WIDGET\$
         </td>
         <td class=chelp>~TKEY~<i>\$HELP\$</i>{HELPURL}<BR><A HREF="\$HELP_URL\$">help</A>{/HELPURL}</FONT></td>
       </tr>
     </table>
   </td>
</tr>
EOF

######## END INIT TEMPLATES

	sub title_row {
		my $title = shift;
		my $out = $title_template;
		$out =~ s/~TITLE~/$title/g;
		return $out;
	}

	if($tref and ! $template_top) {
		my @el = split /[\s,\0]+/, $tref->{ui_template_layout};
		my $i = -1;
		my $found;
		for (@el) {
			$i++;
			next unless $_ eq 'UI_CONTENT';
			$found = $i;
			last;
		}
		for($i = 0; $i < $found; $i++) {
			$template_top .= $Tag->var($el[$i], 2);
#Log("i=$i, reading top variable $el[$i]");
		}
		for($i = $found + 1; $i < @el; $i++) {
			$template_bot .= $Tag->var($el[$i], 2);
#Log("i=$i, reading bot variable $el[$i]");
		}
#Log("Template top: $template_top");
	}

	$control_display = $tref->{ui_template_edit}
		if $tref->{ui_template_edit};

	my %control_display = map { ($_, 1) } split /[\s\0,]/, $control_display;

#Log("t_name=$t_name");

	my $page = $CGI->{ui_page};

	$out .= title_row( errmsg("Page file: %s", $page ) );
	$hidden{ui_page}			= $page;
	$hidden{ui_template_layout} = $tref->{ui_template_layout};

	if($control_display{template}) {
		$out .= <<EOF;
<tr>
<td colspan=2 bgcolor=__UI_C_TOPBLOCKBAR__><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>
<tr>
	<td bgcolor=__UI_C_INTBLOCK__><b>Template</b> ($t_name)</td>
	<td bgcolor=__UI_C_INTBLOCK__><small>
<select name="ui_template" size=1>
<option value=""> Select a page template
EOF

	my ($data) = $Tag->read_ui_template("$template_dir/*");

	my @opt_out;
	for(@$data) {
		my $r = $_;
		my $name = $r->{ui_template_name};
		my $sel = '';
		my $label = $r->{ui_template_label} || $r->{ui_template_description};
		$sel = ' SELECTED' if $name eq $t_name;
		#Log("ref is " . $Tag->uneval( { ref => $r } ));
		push @opt_out, qq{<OPTION VALUE="$name"$sel>$label};
	}
	$out .= join "\n", @opt_out;
	$out .= <<EOF;
	</select>
EOF
	$out .= $Tag->button( {
					text => "Change template",
					body => q{
		mv_todo=back
		[js]onclick="this.form.target='_self'"[/js]
		ui_template_change=[cgi ui_template]
		mv_nextpage=@@MV_PAGE@@
},
		});
	$out .= <<EOF;
	 </small>
	</td>
</tr>
EOF
	}
	else {
		$out .= <<EOF;
<tr>
<td colspan=2 bgcolor=__UI_C_TOPBLOCKBAR__><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>
<tr>
	<td bgcolor=__UI_C_INTBLOCK__>
		<b>Template</b>
	</td>
	<td bgcolor=__UI_C_INTBLOCK__>
		$t_name -- $t_desc
		<INPUT type=hidden name="ui_template" value="$t_name">
	</td>
</tr>
EOF
	}

	$out .= <<EOF;
<tr>
	<td class=ralt>
		<b>Template sequence</b><br>
	</td>
	<td class=ralt VALIGN=top>
		$tref->{ui_template_layout}
	</td>
</tr>
<tr>
	<td class=ralt colspan=2>
		<small><i>UI_CONTENT is the content portion(s), all others refer
		to region variables elements.</i></small>
	</td>
</tr>
<tr class=rtitle>
<td colspan=2 class=ctitle><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>
EOF

	$content = '' if ! $content;

	$content_display = $content;

	my $compdir = $Variable->{UI_COMPONENT_DIR} || 'templates/components';
	my ($carray) = $Tag->read_ui_template("$compdir/*");
	my $chash = {};
	my $ctype = { EVERY => [] };
	for(@$carray) {
		my $name = $_->{ui_component};
		$chash->{$name} = $_;
		push @{$ctype->{EVERY}}, $name;
		my @ty = grep /\S/, split /[\s,\0]+/, $_->{ui_component_type};
		for(@ty) {
			$ctype->{$_} ||= [];
			push @{$ctype->{$_}}, $name;
		}
	}

	for(keys %$chash) {
		my $r = $chash->{$_};
		delete $r->{ui_current_content};
		delete $r->{ui_definition};
	}

	my $template_div = 0;

	for(\$template_top, \$template_bot) {
#Log("looking in template, length " . length($$_));
		while( $$_ =~ m{
				 [ \t]* 
				 (?:
				 	<!--+ \s+ begin \s+ component \s+ (\w+) \s+ (\w*) \s* --+>
				 	(.*?)
					<!--+ \s+ end \s+ component \s+ \1 \s+ --+> 
				 | 
				 	\[ include \s+ file \s*=\s*["'][^"]*/
						(?:\[control \s+ component \s+ )?
							(\w+)
						\]? ['"]
				 	\s*
					(?:group=["']?(\w+)['"]?)?
					.*?
					\]  \s*  \[control\]
				 )
			}gsix)
		{
			my $compname = $1 || $4;
			my $comptype = $2 || $5;
#Log("Found component $compname, group $comptype");
			my $current_content = $3 || '';
			
#Log ("Read component $compname=$chash->{$compname}.");
			push @messages, ("Read component $compname=$chash->{$compname}.");
#			push @messages, ("component $compname structuret:\n"
#							. $Tag->uneval({ ref => $carray } ) );
			if ($chash->{$compname}) {
				my $r = { %{$chash->{$compname}} } ;
				if($comptype) {
					$r->{ui_component_type_current} = $comptype;
				}
				push @cobj, $r;
			}
			else {
				my $desc	= -f "$compdir/$compname"
							? 'Static component, no controls'
							: 'non-existent component (creating later?)';
				push @cobj, {
					ui_component  => $compname,
					ui_component_name => $compname,
					ui_component_description => $desc,
				};
			}
		}

		$template_div = scalar(@cobj) unless $template_div;

		$$_ =~ s{
				 [ \t]*<!--+
				 	\s+ begin \s+ component \s+ (\S+) (?:\s*(\S+))? \s+ --+>
				 	(.*?)
				 <!--+ \s+end \s+component \s+ \1 \s+ --+> 
			}
			{[include file="$compdir/[control component $1]" group="$2"][control]}gsix;
		
	}

	$template_top_display = $template_top;
	$template_bot_display = $template_bot;
	
	for(\$template_top_display, \$template_bot_display, $content_display) {
		$$_ =~ s/\&/\&amp;/g;
		$$_ =~ s/\[/&#91;/g;
		$$_ =~ s/</&lt;/g;
	}

	$out .= <<EOF if $CGI->{ui_show_template};
<tr class=cmarq>
<td colspan=2><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>
<tr class=ralt>
	<td valign=top>
	<b>Top of template</b>
	<small><br><br>
	<A HREF="$Config->{VendURL}/ui_download/$page?mv_no_cache=$Session->{pageCount}"><img src="down.gif" border=0 height=11 width=10 alt="download $page">download content</a>
	<br><i>(will not reflect changes below unless saved)</I></small>
	</td>
	<td>
	<B>
	<small>
	<INPUT TYPE=file NAME=ui_upload_template_top><BR>
	[button text="Upload new template portion from file"]
		mv_todo=return
		[js]onclick="this.form.target='_self'"[/js]
		mv_nextpage=@@MV_PAGE@@
	[/button]</small></b>
	</td>
</tr>

<tr class=ralt>
	<td colspan=2 class=clabel>
		<b>or input below:</b><br>
		<TEXTAREA NAME=ui_template_top COLS=85 ROWS=20>$template_top_display</TEXTAREA>
	</td>
</tr>
<tr>
<td colspan=2 class=rtitle><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
EOF


	my $pref = $Tag->read_ui_page( { body => $current } );
	return tmp_error("No page content!") unless $pref;

	my @settings;

	my $global = $pref->{ui_page_setting} || {};

	if($pref->{ui_component}[0]) {
		@settings = @{ $pref->{ui_component} };
		for (my $i = 0; $i < @cobj; $i++) {
			my $grp = $cobj[$i]->{ui_component_type_current};
			my $name = $settings[$i]->{component};
			undef $grp if $grp eq 'ALL';
			my $ref = $chash->{$name} || $chash->{none};
			$cobj[$i] = { %$ref };
			$cobj[$i]->{ui_component_type_current} = $grp;
		}
	}

	my $content;

	length($current) and
		$current =~ m{<!--\s+begin\s+content\s+-->\n*(.*)\n*<!--\s+end\s+content\s+-->}is
		and $content = $1;


	my $idx = 0;
	my $num = $idx + 1;

	sub get_unified_ref {
		my ($name, $ref) = @_;
		my $uniref = {};
		for(keys %$ref) {
			next unless ref($ref->{$_}) eq 'HASH';
			$uniref->{$_} = $ref->{$_}{$name} || undef;
		}
		return $uniref;
	}

	sub comp_selector {
		my ($type, $curr, $name) = @_;
#Log("called type=$type curr=$curr name=$name");
		$name = 'ui_component' unless $name;
		my $all = $ctype->{ALL} || [ 'none' ];
		my $ary = $ctype->{$type} || $ctype->{EVERY};
		$ary = $ctype->{EVERY} unless @$ary;
#Log("ary is: " . uneval($ary) );
		my @comps;
		push @comps, @$ary, @$all;
		my %seen;
		@comps = grep !$seen{$_}++, @comps;

		my $out = qq{<SELECT NAME="$name">};
		for(@comps) {
			$out .= qq{<OPTION VALUE="$_"};
			$out .= " SELECTED" if $curr eq $_;
			$out .= q{>};
			$out .= defined $chash->{$_}{ui_component_label}
				  ? $chash->{$_}{ui_component_label}  
				  : $_;
		}
		return $out .= qq{</SELECT>};
	}

	sub type_selector {
		my ($type, $curr, $name) = @_;
#Log("called type=$type curr=$curr name=$name");
		$name = 'ui_component_type' unless $name;
		my @comps = grep /\S/, split /[\0\s,]+/, $type;
		unshift @comps, $curr;
		my %seen;
		@comps = grep !$seen{$_}++, @comps;
		
		my $out = qq{<SELECT NAME="$name">};
		for(@comps) {
			$out .= qq{<OPTION VALUE="$_"};
			$out .= " SELECTED" if $curr eq $_;
			$out .= qq{>$_};
		}
		return $out .= qq{</SELECT>};
	}

	my $vsize = $content =~ tr/\n/\n/;

	if($vsize < 20) {
		$vsize = 20;
	}
	elsif ($vsize > 200) {
		$vsize = 200;
	}
	else {
		$vsize++;
	}

	$Scratch->{ui_current_content} = $Tag->filter({
													op		=> 'encode_entities',
													body	=> $content,
													});

	my $content_widget;
	if($control_display{content}) {
		$content_widget = <<EOF;
<tr class=ralt>
	<td colspan=2 class=clabel>
		<TEXTAREA NAME=ui_content COLS=85 ROWS=$vsize>[scratch ui_current_content]</TEXTAREA>
	</td>
</tr>
EOF
	}
	else {
		$hidden{ui_content} = '[scratch ui_current_content]';
	}

	my $preamble_widget;
  PREAMBLE: {
  	last PREAMBLE unless $Scratch->{ui_page_preamble} = $pref->{ui_page_preamble};
	if($control_display{preamble}) {
		$preamble_widget = <<EOF;
<tr class=ralt>
	<td colspan=2 class=clabel>
		Page Preamble <I>(code executed after settings and before header)</I>
		<TEXTAREA NAME=ui_page_preamble COLS=85 ROWS=$vsize>[scratch ui_page_preamble]</TEXTAREA>
	</td>
</tr>
EOF
	}
	else {
		$hidden{ui_page_preamble} =  $Scratch->{ui_page_preamble};
	}
 }

	my $postamble_widget;
  POSTAMBLE: {
  	last POSTAMBLE unless $Scratch->{ui_page_postamble} = $pref->{ui_page_postamble};
	if($control_display{postamble}) {
		$postamble_widget = <<EOF;
<tr class=ralt>
	<td colspan=2 class=clabel>
		Page Postamble <I>(code executed after all other)</I>
		<TEXTAREA NAME=ui_page_postamble COLS=85 ROWS=$vsize>[scratch ui_page_postamble]</TEXTAREA>
	</td>
</tr>
EOF
	}
	else {
		$hidden{ui_page_postamble} = $Scratch->{ui_page_postamble};
	}
 }


	my @globals;
	if(! $control_display{page}) {
		$hidden{ui_page_setting_text} = $pref->{ui_page_setting_text};
	}
	elsif($tref->{ui_display_order}) {
		@globals = @{$tref->{ui_display_order}}
			if $tref->{ui_display_order};
	}

	push @controls, title_row("Page Settings")
		if @globals;
	for my $g (@globals) {
		my $set = $global->{$g};
		my $wtype = $tref->{widget}{$g} || '';
		if($wtype eq 'break') {
			push @controls, $break_template;
			next;
		}

		my $o = {};
		for(qw/ options height width filter prefilter help description /) {
			$o->{$_} = $tref->{$_}{$g}
				if ref $tref->{$_};
		}

		my $options = $o->{options} || '=No*,1=Yes';
#Log("r key $g set='$set' options=$options value=$Values->{qq!$g!}");

		$wtype = 'text_60' unless $wtype;
		$widget = $Tag->display( {
						name => "ui_control_$g",
						type => $wtype,
						passed => $options,
						rows =>  $o->{height},
						cols =>  $o->{width},
						override => $set,
						default => $set,
						help => $o->{help},
						label =>  $o->{description},
						filter => $o->{filter},
						pre_filter => $o->{prefilter},
						template => $control_template,
					});
		$widget =~ s/~[A-Z]{3,}~//g;
		push @controls, $widget;
	}

	if(! $control_display{components}) {
		@cobj = ();
		$hidden{ui_component_text} = $pref->{ui_component_text};
	}

	push @controls, $preamble_widget if $preamble_widget;
	
	push @controls, title_row("Top Components") if @cobj;
	for(my $i = 0; $i < @cobj; $i++) {
		my $ref = $cobj[$i];
		my $settings = $settings[$i] || {};
		if($template_div and $i >= $template_div) {
			push @controls, title_row("Content");
			push @controls, $content_widget;
			push @controls, title_row("Bottom Components");
			undef $template_div;
		}
		my $c_name      = $ref->{ui_component} || 'none';
		my $c_type      = $ref->{ui_component_type};
		my $c_type_curr = $ref->{ui_component_type_current};
		my $c_label     = $ref->{ui_component_label}
				        || $ref->{ui_component_description}
				        || 'no description';
		push @messages, "ui_component_name=$ref->{ui_component_name}";
		push @messages, "ui_component=$ref->{ui_component}";
		push @messages, "ui_component_label=$ref->{ui_component_label}";
		push @messages, "ui_component_description=$ref->{ui_component_description}";

		my @def;

		my $titlebar = $sep_template;
		$titlebar =~ s/~NAME~/$c_name/g;
		$titlebar =~ s/~NAMESEL~/comp_selector($c_type_curr || $c_type, $c_name, "$i.ui_component")/ge;
		$titlebar =~ s/~NUMBER~/$i + 1/eg;
		$titlebar =~ s/~TYPE~/$c_type/g;
		$titlebar =~ s/~TYPESEL~/type_selector($c_type, $c_type_curr, "$i.ui_component_type")/eg;
		$titlebar =~ s/~DESC~/$c_label/g;
		$titlebar =~ s/~VARNAME~/$i.ui_component/g;
		push @controls, $titlebar;
		my $def_string = $ref->{ui_definition};
		$def_string =~ s/"/&quot;/g;

		my $short_def = "ui_component: $c_name";

		my $r;
#Log("element=$ref->{element}(or $ref->{element})");

		if (ref $ref->{element}) {
				my $widget;
				my @order;
				if($ref->{ui_display_order}) {
					@order = @{$ref->{ui_display_order}};
				}
				else {
					@order = sort keys %{$ref->{element}};
				}
				for(@order) {
					$r = get_unified_ref($_, $ref);
					my $set;
					if(defined $settings->{$_}) {
						$set = $settings->{$_};
					}
					elsif (defined $global->{$_}) {
						$set = $global->{$_};
					}

					my $t_set = $r->{$_};
					my $wtype = $r->{widget} || '';
					my $label = $r->{label} || $r->{description} || $_;
					if($wtype eq 'break') {
						push @controls, $break_template;
						next;
					}
					my $options = $r->{options};
#Log("r key $_ set='$set' options=$options value=$Values->{$_} widget=$wtype deftext=$deftext");
push @messages, "r key $_ set='$set' options=$options value=$Values->{$_} widget=$wtype";
					my $help = $r->{help} || '';

					if(! $wtype) {
						$wtype = 'text';
						$r->{width} = 40
							if ! $r->{width};
					}
					$widget = $Tag->display( {
									name => "$i.ui_control_$_",
									type => $wtype,
									lookup => $r->{lookup},
									db => $r->{db},
									passed => $r->{options},
									rows =>  $r->{height},
									cols =>  $r->{width},
									override => $set,
									default => $set,
									help => $help,
									label =>  $label,
									filter => $r->{filter},
									pre_filter => $r->{prefilter},
									template => $control_template,
								});
					push @messages, "widget='$widget'";
					# No meta for now
					$widget =~ s/~[A-Z]{3,}~//g;
					push @controls, $widget;
				}
		}

	}

	if(defined $template_div) {
		push @controls, title_row("Content");
		push @controls, $content_widget;
		undef $template_div;
	}

	push @controls, $postamble_widget if $postamble_widget;

	$out .= join "\n", @controls;
	
	$out .= <<EOF if $CGI->{ui_show_template};
<tr>
<td colspan=2 class=rtitle><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>
<tr class=ralt>
	<td valign=top>
	<b>Bottom of template</b>
	<small><br><br>
	<A HREF="$Config->{VendURL}/ui_download/$page?mv_no_cache=$Session->{pageCount}"><img src="down.gif" border=0 height=11 width=10 alt="download $page">download content</a>
	<br><i>(will not reflect changes below unless saved)</I></small>
	</td>
	<td>
	<B>
	<small>
	<INPUT TYPE=file NAME=ui_upload_template_bot><BR>
	[button text="Upload new template portion from file"]
		mv_todo=return
		[js]onclick="this.form.target='_self'"[/js]
		mv_nextpage=@@MV_PAGE@@
	[/button]</small></b>
	</td>
</tr>
<tr class=ralt>
	<td colspan=2 class=clabel>
		or input below:<br>
		<TEXTAREA NAME=ui_template_bot COLS=85 ROWS=20>$template_bot_display</TEXTAREA>
	</td>
</tr>
EOF

	$hidden{ui_template_limit_tag} = $tref->{ui_template_limit_tag};

	my $hidden = '';
	for(keys %hidden) {
		$hidden .= qq{<input type=hidden name="$_" value="};
		$hidden .= $Tag->filter('entities', $hidden{$_});
		$hidden .= qq{">\n};
	}
	$Scratch->{tmp_hidden} = $hidden;
	return $out;
[/perl]

<tr>
<td colspan=2 class=rtitle><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>

<tr class=ralt>
<td colspan=2>
[set Preview]
mv_todo=back
mv_nextpage=__UI_BASE__/page_preview
[/set]

[set Save]
mv_nextpage=__UI_BASE__/page_save
[/set]

[scratchd tmp_hidden]
<INPUT TYPE=submit NAME=mv_click VALUE=Preview onClick="this.form.target='page_preview'; this.form.action='[area __UI_BASE__/page_preview]'">
<INPUT TYPE=hidden NAME=ui_return_to VALUE="__UI_BASE__/page">
<INPUT TYPE=submit NAME=mv_click VALUE=Save onClick="this.form.target='_self'; this.form.action='[area __UI_BASE__/page_save]'">
<INPUT TYPE=submit NAME=mv_click VALUE=Cancel onClick="this.form.target='_self'; this.form.action='[area __UI_BASE__/page]'"><br>
[comment]
<!-- may come back someday -->
<INPUT TYPE=checkbox NAME=ui_save_t_in_page VALUE=1 [cgi save_in_page]> Save template in page
[/comment]

</td>
</tr>

<tr>
<td colspan=2 class=rtitle><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>

</table>
</form>


