# Functions for the simple blue theme

# Generate the list of category links, the title and the page body
# XXX sponsor link
sub theme_header
{
local ($title, $image, $help, $config, $nomodule, $nowebmin, $right,
       $header, $body, $below) = @_;

# HTML header
print "<!doctype html public \"-//W3C//DTD HTML 3.2 Final//EN\">\n";
print "<html>\n";
print "<head>\n";
local $charset = defined($force_charset) ? $force_charset : &get_charset();
if ($charset) {
        print "<meta http-equiv=\"Content-Type\" ",
              "content=\"text/html; Charset=$charset\">\n";
        }
print "<link rel='stylesheet' href='$gconfig{'webprefix'}/unauthenticated/style.css' type='text/css'>\n";

# Page title, with hostname and username in it
local $pagetitle;
if ($gconfig{'sysinfo'} == 1) {
	$pagetitle = sprintf "%s : %s on %s (%s %s)\n",
		$_[0], $remote_user, &get_display_hostname(),
		$os_type, $os_version;
	}
elsif ($gconfig{'sysinfo'} == 4) {
	$pagetitle = sprintf "%s on %s (%s %s)\n",
		$remote_user, &get_display_hostname(),
		$os_type, $os_version;
	}
else {
	$pagetitle = $title;
	}
if ($gconfig{'showlogin'} && $remote_user) {
	$pagetitle = $remote_user." : ".$pagetitle;
	}
print "<title>$pagetitle</title>\n";
print $header;
print "</head>\n";

# HTML body start
local $dir = $current_lang_info->{'dir'} ?
		"dir=\"$current_lang_info->{'dir'}\"" : "";
print "<body bgcolor=#ffffff link=#0000ee vlink=#0000ee text=#000000 ",
      "$dir $body topmargin=1 bottommargin=1 leftmargin=1 rightmargin=1>\n";

# Work out current category
local $currentcat;
if ($theme_index_page) {
	# Can get category from param to index page
	if (defined($in{'cat'})) {
		$currentcat = $in{'cat'};
		}
	else {
		$currentcat = $gconfig{'deftab'};
		}
	}
else {
	# Can get category from current module
	$currentcat = $module_info{'category'};
	}

# Category links, plus logout and sponsor buttons
local @blue_modules = &get_visible_module_infos();
local %cats = &list_categories(\@blue_modules);
local @cats = sort { $b cmp $a } keys %cats;
if (!defined($cats{$currentcat})) {
	$currentcat = $cats[0];
	}
local $one = @blue_modules == 1 && $gconfig{'gotoone'};
local $notabs = $gconfig{"notabs_${base_remote_user}"} == 2 ||
        $gconfig{"notabs_${base_remote_user}"} == 0 && $gconfig{'notabs'};
local $shown_cats = 0;
print "<table width=100% cellpadding=0 cellspacing=1><tr>\n";
if (@_ > 1 && !$one && $remote_user && !$notabs) {
	# Show module categories
	foreach my $c (@cats) {
		print "<td>\n";
		if ($currentcat eq $c) {
			print &blue_theme_box(
				"<u><a href='/?cat=$c'>$cats{$c}</a></u>");
			}
		else {
			print &blue_theme_box(
				"<a href='/?cat=$c'>$cats{$c}</a>");
			}
		print "</td>\n";
		$shown_cats++;
		}
	}

local $logout = $main::session_id ? "/session_login.cgi?logout=1"
			  : "/switch_user.cgi";
local $lotext = $main::session_id ? $text{'main_logout'}
				  : $text{'main_switch'};
if ($remote_user && @_ > 1 && $shown_cats) {
	# Show webmin.com link, sponsor link and logout link
	print "<td width=100%>\n";
	print &blue_theme_box("<a href=http://www.webmin.com/>Webmin Home</a>", "width=100%");
	print "</td>\n";
	print "<td align=right>\n";
	print &blue_theme_box("<a href='$gconfig{'webprefix'}$logout'>$lotext</a>");
	print "</td>\n";
	}
elsif ($remote_user) {
	# Defer logout link till right side
	$right .= "<br>" if ($right);
	$right .= "<a href='$gconfig{'webprefix'}$logout'>$lotext</a>";
	}
print "</tr></table>\n";

# Module index link, module config link, help link, page title and right
# side links
local @left;
if (!$nomodule) {
	# Module index link
	local $idx = $module_info{'index_link'};
	local $mi = $module_index_link || "/$module_name/$idx";
	local $mt = $module_index_name || $text{'header_module'};
	push(@left, "<a href='$gconfig{'webprefix'}$mi'>$mt</a>");
	}
if (!$ENV{'ANONYMOUS_USER'} && $help) {
	if (ref($help)) {
		push(@left, &hlink($text{'header_help'},
				   $help->[0], $help->[1]));
		}
	else {
		push(@left, &hlink($text{'header_help'}, $help));
		}
	}
if ($config) {
	# Module config link
	local %access = &get_module_acl();
	if (!$access{'noconfig'} && !$config{'noprefs'}) {
		local $cprog = $user_module_config_directory ?
				"uconfig.cgi" : "config.cgi";
		push(@left, "<a href='$gconfig{'webprefix'}/$cprog?$module_name'>$text{'header_config'}</a>");
		}
	}
if (@_ > 1 && (@left || $right || $title || $image)) {
	local $ttable = "<table width=100% cellpadding=0 cellspacing=0><tr>\n";
	if (@left || $right) {
		$ttable .= "<td width=15% valign=top align=left>".
			   join("<br>\n", @left).
			   "</td>\n";
		}
	# Title or image
	local $w = @left || $right ? 70 : 100;
	$ttable .= "<td width=$w% valign=top align=center>";
	$ttable .= $image ||
		   "<font size=+1>$title</font>";
	if ($ui_header_subtext) {
		$ttable .= "<br>$ui_header_subtext\n";
		}
	if ($below) {
		$ttable .= "<br>$below\n";
		}
	$ttable .= "</td>\n";
	# Right-side stuff, supplied by module
	if ($right || @left) {
		$ttable .= "<td width=15% valign=top align=right>".
			   $right.
			   "</td>\n";
		}
	$ttable .= "</table>\n";
	print &blue_theme_box($ttable, "width=100%");
	}

# Start box for the entire page
if (!$theme_no_table) {
	local $ua = "$gconfig{'webprefix'}/unauthenticated";
	print "<table cellpadding=0 cellspacing=0>\n";
	print "<tr><td width=8><img src=$ua/tl.gif width=8 height=8></td><td width=100%><img src=$ua/h.gif width=100% height=8></td><td width=8><img src=$ua/tr.gif width=8 height=8></td></tr>\n";
	print "<tr><td width=8 background=$ua/v.gif><img src=$ua/v.gif width=8 height=100%></td><td width=100%>\n";
	}
}

sub theme_footer
{
# Return links, in a blue box
local $returns;
for(my $i=0; $i+1<@_; $i+=2) {
    local $url = $_[$i];
    if ($url eq '/') {
        $url = "/?cat=$module_info{'category'}";
        }
    elsif ($url eq '' && $module_name) {
        local $idx = $module_info{'index_link'};
        $url = "/$module_name/$idx";
        }
    elsif ($url =~ /^\?/ && $module_name) {
        $url = "/$module_name/$url";
        }
    $url = "$gconfig{'webprefix'}$url" if ($url =~ /^\//);
    if ($i == 0) {
        $returns .= "&nbsp;<a href=\"$url\"><img alt=\"<-\" align=middle border=0 src=$gconfig{'webprefix'}/images/left.gif></a>\n";
        }
    else {
        $returns .= "&nbsp;|\n";
        }
    $returns .= "&nbsp;<a href=\"$url\">".&text('main_return', $_[$i+1])."</a>\n";
    }

# End box for the entire page
if (!$theme_no_table) {
	local $ua = "$gconfig{'webprefix'}/unauthenticated";
	print "</td><td width=8 background=$ua/v.gif><img src=$ua/v.gif width=8 height=100%></td></tr>\n";
	print "<tr><td width=8><img src=$ua/bl.gif width=8 height=8></td><td width=100%><img src=$ua/h.gif width=100% height=8></td><td width=8><img src=$ua/br.gif width=8 height=8></td></tr>\n";
	print "</table>\n";
	}
if ($returns) {
	print $returns,"<br>\n";
	}

print "</body></html>\n";
}

# theme_ui_print_header(subtext, args...)
# Print HTML for a header with the post-header line. The args are the same
# as those passed to header()
sub theme_ui_print_header
{
local ($ui_header_subtext, @args) = @_;
&header(@args);
if ($theme_no_table) {
	print "<hr>\n";
	}
}

sub theme_ui_pre_footer
{
local $rv;
if ($theme_no_table) {
        $rv .= "<hr>\n";
        }
return $rv;
}

# theme_ui_columns_row(&columns, &tdtags)
# Returns HTML for a row in a multi-column table
sub theme_ui_columns_row
{
local ($cols, $tdtags) = @_;
local $rv;
$rv .= "<tr $cb onMouseOver=\"this.className='mainhigh'\" onMouseOut=\"this.className='mainbody'\">\n";
local $i;
for($i=0; $i<@$cols; $i++) {
	$rv .= "<td ".$tdtags->[$i].">".
	       ($cols->[$i] eq "" ? "<br>" : $cols->[$i])."</td>\n";
	}
$rv .= "</tr>\n";
return $rv;
}

# theme_select_all_link(field, form, text)
# Adds support for row highlighting to the normal select all
sub theme_select_all_link
{
local ($field, $form, $text) = @_;
$form = int($form);
$text ||= $text{'ui_selall'};
return "<a href='#' onClick='f = document.forms[$form]; ff = f.$field; ff.checked = true; r = document.getElementById(\"row_\"+ff.id); if (r) { r.className = \"mainsel\" }; for(i=0; i<f.$field.length; i++) { ff = f.${field}[i]; ff.checked = true; r = document.getElementById(\"row_\"+ff.id); if (r) { r.className = \"mainsel\" } } return false'>$text</a>";
}

# theme_select_invert_link(field, form, text)
# Adds support for row highlighting to the normal invert selection
sub theme_select_invert_link
{
local ($field, $form, $text) = @_;
$form = int($form);
$text ||= $text{'ui_selinv'};
return "<a href='#' onClick='f = document.forms[$form]; ff = f.$field; ff.checked = !f.$field.checked; r = document.getElementById(\"row_\"+ff.id); if (r) { r.className = ff.checked ? \"mainsel\" : \"mainbody\" }; for(i=0; i<f.$field.length; i++) { ff = f.${field}[i]; ff.checked = !ff.checked; r = document.getElementById(\"row_\"+ff.id); if (r) { r.className = ff.checked ? \"mainsel\" : \"mainbody\" } } return false'>$text</a>";
}

sub theme_ui_checked_columns_row
{
local ($cols, $tdtags, $checkname, $checkvalue) = @_;
local $rv;
local $cbid = &quote_escape("${checkname}_${checkvalue}");
local $rid = &quote_escape("row_${checkname}_${checkvalue}");
$rv .= "<tr $cb id=\"$rid\" onMouseOver=\"this.className = document.getElementById('$cbid').checked ? 'mainhighsel' : 'mainhigh'\" onMouseOut=\"this.className = document.getElementById('$cbid').checked ? 'mainsel' : 'mainbody'\">\n";
$rv .= "<td ".$tdtags->[0].">".
       &ui_checkbox($checkname, $checkvalue, undef, 0, "onClick=\"document.getElementById('$rid').className = this.checked ? 'mainhighsel' : 'mainhigh';\"").
       "</td>\n";
local $i;
for($i=0; $i<@$cols; $i++) {
	$rv .= "<td ".$tdtags->[$i+1].">";
	if ($cols->[$i] !~ /<a\s+href|<input|<select|<textarea/) {
		$rv .= "<label for=\"".
			&quote_escape("${checkname}_${checkvalue}")."\">";
		}
	$rv .= ($cols->[$i] eq "" ? "<br>" : $cols->[$i]);
	if ($cols->[$i] !~ /<a\s+href/) {
		$rv .= "</label>";
		}
	$rv .= "</td>\n";
	}
$rv .= "</tr>\n";
return $rv;
}

sub theme_ui_radio_columns_row
{
local ($cols, $tdtags, $checkname, $checkvalue) = @_;
local $rv;
local $cbid = &quote_escape("${checkname}_${checkvalue}");
local $rid = &quote_escape("row_${checkname}_${checkvalue}");
$rv .= "<tr $cb id=\"$rid\" onMouseOver=\"this.className = document.getElementById('$cbid').checked ? 'mainhighsel' : 'mainhigh'\" onMouseOut=\"this.className = document.getElementById('$cbid').checked ? 'mainsel' : 'mainbody'\">\n";
$rv .= "<td ".$tdtags->[0].">".
       &ui_oneradio($checkname, $checkvalue, undef, 0, "onClick=\"for(i=0; i<form.$checkname.length; i++) { ff = form.${checkname}[i]; r = document.getElementById('row_'+ff.id); if (r) { r.className = 'mainbody' } } document.getElementById('$rid').className = this.checked ? 'mainhighsel' : 'mainhigh';\"").
       "</td>\n";
local $i;
for($i=0; $i<@$cols; $i++) {
	$rv .= "<td ".$tdtags->[$i+1].">";
	if ($cols->[$i] !~ /<a\s+href/) {
		$rv .= "<label for=\"".
			&quote_escape("${checkname}_${checkvalue}")."\">";
		}
	$rv .= ($cols->[$i] eq "" ? "<br>" : $cols->[$i]);
	if ($cols->[$i] !~ /<a\s+href/) {
		$rv .= "</label>";
		}
	$rv .= "</td>\n";
	}
$rv .= "</tr>\n";
return $rv;
}

# theme_ui_subheading(text, ...)
# Returns HTML for a section heading
sub disable_theme_ui_subheading
{
return "<font size=+1>".join("", @_)."</font><br>\n";
}

# theme_popup_header(title, headstuff, bodystuff)
sub theme_popup_header
{
local ($title, $headstuff, $bodystuff) = @_;
print "<!doctype html public \"-//W3C//DTD HTML 3.2 Final//EN\">\n";
print "<html>\n";
print "<head>\n";
print "<link rel='stylesheet' href='$gconfig{'webprefix'}/unauthenticated/style.css' type='text/css'>\n";
print "<title>$title</title>\n";
print $headstuff;
print "</head>\n";
print "<body bgcolor=#6696bc link=#000000 vlink=#000000 text=#000000 leftmargin=1 topmargin=1 marginwidth=1 marginheight=1 $bodystuff>\n";
}

sub theme_popup_footer
{
print "</body></html>\n";
}

sub theme_error
{
&header($text{'error'}, "");
print "<p>\n";
print "<h3>",($main::whatfailed ? "$main::whatfailed : " : ""),@_,"</h3>\n";
print "<p>\n";
if ($ENV{'HTTP_REFERER'} && $main::completed_referers_check) {
	&footer($ENV{'HTTP_REFERER'}, $text{'error_previous'});
	}
else {
	&footer();
	}
}

sub blue_theme_box
{
local ($text, $tabletags, $align) = @_;
$align ||= "center";
local $ua = "$gconfig{'webprefix'}/unauthenticated";
local $rv;
$rv .=  "<table cellpadding=0 cellspacing=0 $tabletags>";
$rv .=  "<tr><td width=8><img src=$ua/tl.gif width=8 height=8></td><td><img src=$ua/h.gif width=100% height=8></td><td width=8><img src=$ua/tr.gif width=8 height=8></td></tr>\n";
$rv .=  "<tr><td width=8 background=$ua/v.gif><img src=$ua/v.gif width=8 height=100%></td><td align=$align>$text</td><td width=8 background=$ua/v.gif><img src=$ua/v.gif width=8 height=100%></td></tr>\n";
$rv .=  "<tr><td width=8><img src=$ua/bl.gif width=8 height=8></td><td><img src=$ua/h.gif width=100% height=8></td><td width=8><img src=$ua/br.gif width=8 height=8></td></tr>\n";
$rv .=  "</table>\n";
return $rv;
}

# theme_generate_icon(image, title, link, [href], [width], [height],
#		      [before-title], [after-title])
# Prints HTML for an icon image
sub theme_generate_icon
{
local ($icon, $title, $link, $href, $w, $h, $before, $after) = @_;
local $wtag = !defined($w) ? "width=24" : $w ? "width=$w" : "";
local $htag = !defined($h) ? "height=25" : $w ? "height=$h" : "";
print "<table width=100% cellpadding=0 cellspacing=0><tr>";
if ($link) {
	print "<td align=left $wtag><a href='$link' $href><img src=$icon $wtag $htag border=0 align=middle></a></td> <td align=left>&nbsp;$before<a href='$link' $href>$title</a>$after</td>";
	}
else {
	print "<td align=left $wtag><img src=$icon $wtag $htag border=0 align=middle></td> <td align=left>&nbsp;$before$title$after</td>";
	}
print "</tr></table>\n";
}

