sub HiddenFields($@){
    $INTERNAL{hash} = shift;
    $INTERNAL{saves} = $INTERNAL{q} . (join $INTERNAL{q}, @_) . $INTERNAL{q};
#    $INTERNAL{human} = join ',', @_;
#    print "<!-- $INTERNAL{hash}: $INTERNAL{human} -->";
    for (keys %{$INTERNAL{hash}}){
	print qq{<input type=hidden name="$_" value="${$INTERNAL{hash}}{$_}">}
	    unless $INTERNAL{saves} =~ /$INTERNAL{q}$_$INTERNAL{q}/;
    }
}

sub NoHeaders($){
    $_[0] =~ s/^.*?\n\n//;
    return $_[0]
}

sub Entity($;$$$$){
    $_[4] ||= 4;
    $_[0] =~ s/&/&amp;/g;
    $_[0] =~ s/\"/&quot;/g;
    $_[0] =~ s/</&lt;/g;
    $_[0] =~ s/>/&gt;/g;
    if ($_[1]){
	$_[0] =~ s/\n/<br>\n/g;
    }
    if ($_[2]){
	$_[0] =~ s/\t/' ' x $_[4]/eg;
    }
    if ($_[3]){
	$_[0] =~ s/  /&nbsp;&nbsp;/g;
    }
    return $_[0]
}

sub DecodeURI($;$){
    my $t = $_[0];
    $t =~ tr{+} { } unless ($_[1] == 1);
    $t =~ s{%([0-9A-Fa-f]{2})}
	   {pack('c',hex($1))}ge;
    return $t;
}

sub EncodeURI($;$){
    my $t = $_[0];
    $t =~ s{([^a-zA-Z0-9_\-.])}
           {uc sprintf("%%%02x",ord($1))}ge;
    $t =~ s{%20}{+}g if ($_[1] == 1);
    return $t;
}

sub AddCookie($){
    if ($header{'set-cookie'}){
	$header{'set-cookie'} .= "\nset-cookie: $_[0]";
    }else{
	$header{'set-cookie'} = $_[0];
    }
}

sub ReadFile($){
    my $o = $/; undef $/;    
    open (READFILE, $_[0]);
    my $r = <READFILE>;
    close READFILE;
    $/ = $o;
    return $r;
}

sub WriteFile($$){
    open (WRITEFILE, ">$_[0]");
    flock WRITEFILE, 2;
    print WRITEFILE $_[1];
    close WRITEFILE;
}
1;