__NAME__ ui_order_button_build
[calc]
	$Scratch->{tables_to_open} = join " ", @{$Config->{ProductFiles}};
	return;
[/calc]
[perl tables="[scratch tables_to_open]"]
	delete $Scratch->{item_result};
	delete $Scratch->{test_button};
	my $result = <<EOF;
<tr>
<td colspan=2  class=rnorm VALIGN=TOP>
	<B>Resulting button</B><BR>
<TEXTAREA ROWS=5 COLS=70 NAME="item_result">
EOF
	$result =~ s/\s+$//;
	my @parms;
	BUILD: {
		if(! $CGI->{item_id}) {
			$result .= "ERROR: no item selected";
			last BUILD;
		}
		my (@items) = split /\0/, $CGI->{item_id};
		for (@items) {
			push @parms, "mv_order_item=$_";
		}
		my @ones;
		QUANTITY: {
			last QUANTITY if ! length $CGI->{item_quantity};
			$CGI->{item_quantity} =~ s/[\s\0,]+$//;
			@ones = split /[\0,\s]/, $CGI->{item_quantity};
			for(@ones) {
				if(/[^\d.]/) {
					$result .= "Error: item quantities must be numbers";
					last BUILD;
				}
				if( ! $Config->{FractionalItems} and $_ =~ /\./) {
					$result .= "Error: item quantities must be integers with FractionalItems=no";
					last BUILD;
				}
				push @parms, "mv_order_quantity=$_";
			}
		}
		if(length $CGI->{item_separate}) {
			push @parms, "mv_separate_items=$CGI->{item_separate}";
		}
		if(length $CGI->{item_destination}) {
			push @parms, "mv_nextpage=$CGI->{item_destination}";
		}
		if($CGI->{item_button} > 1) {
			my @fly;
			if(! $CGI->{description}) {
				$result .= "Error: on-the-fly item requires description";
				last BUILD;
			}
			push @fly, "description=$CGI->{description}";
			if(! $CGI->{price}) {
				$result .= "Error: on-the-fly item requires price";
				last BUILD;
			}
			push @fly, "price=$CGI->{price}";
			push @parms, "mv_order_fly=" . join("|", @fly);;
		}
		if(length $CGI->{item_group}) {
			push @parms, "mv_order_group=$CGI->{item_group}";
		}
		if(length $CGI->{affiliate}) {
			push @parms, "mv_source=$CGI->{affiliate}";
		}
		for my $mod (@{$Config->{UseModifier}}) {
			if($CGI->{"item_modifier_$mod"}) {
				@ones = split /\s*,\s*/, $CGI->{"item_modifier_$mod"};
				for(@ones) {
					push @parms, qq!mv_order_$mod=$_!;
				}
			}
		}
	}
	my $button;
	my $isbutton;
	if(@parms) {
		if($CGI->{item_button} == 1 || $CGI->{item_button} == 3) {
			$isbutton = 1;
			my $url = $Config->{VendURL} . '/order';
			$button = qq{<FORM ACTION="$url" METHOD=POST>\n};
			for(@parms) {
				my ($name, $value) = split /=/, $_, 2;
				$value =~ s/"/&quot;/g;
				$button .= qq{<INPUT TYPE=hidden NAME=$name VALUE="$value">\n};
			}
			$button .= qq{<INPUT TYPE=submit VALUE="Test button"></FORM>};
		}
		else {
			$button = $Tag->area(
							{
								href => 'order',
								form => join ("\n", @parms),
							}
						);
			$button =~ s/\bmv_session_id=\w+&?//;
		}
		$result .= $button;
	}
	$result .= "</TEXTAREA>\n";
	if ($isbutton and $button) {
		$Scratch->{test_button} = $button;
		$Scratch->{test_button} =~ s/METHOD=/TARGET=item_test METHOD=/;
	}
	else {
		$Scratch->{test_button} = qq{<B><A HREF="$button" TARGET=item_test><FONT COLOR=__CONTRAST__>Test order link</FONT></A></B><BR>&nbsp;};
	}
	$result .= "</td></tr>\n\n";
	$Scratch->{item_result} = $result;
[/perl]
__END__
