#!./tclsh
# $Id: settings 1266 2009-07-14 18:39:12Z hubert@u.washington.edu $
# ========================================================================
# Copyright 2008 University of Washington
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# ========================================================================

#  view.tcl
#
#  Purpose:  CGI script settings for Web Alpine 2.0 settings page
#
#  Input:    
#            
set settings_args {
}

# inherit global config
source ./alpine.tcl
source ./foldercache.tcl
source ./common.tcl

set script_base "$_wp(serverpath)/$_wp(appdir)/$_wp(ui2dir)/"

# TEST
proc cgi_suffix {args} {
  return ""
}

set labels 0
proc set_feature {feature desc} {
  global labels
  cgi_checkbox $feature=1 [set_checked [WPCmd PEInfo feature $feature]] id="label_[incr labels]"
  cgi_put " <label for=\"label_${labels}\">$desc</label>"
}

proc set_checked {checked {token checked}} {
  if {$checked > 0} {
    return $token
  }

  return ""
}

proc set_variable {var {desc ""}} {
  global labels
  if {[string length $desc]} {
    cgi_put "<label for=\"label_[incr labels]\">${desc}</label>"
  }
  cgi_text ${var}=[var_value $var] id="label_${labels}"
}


WPEval $settings_args {

  if {0 == [catch {WPCmd PEFolder current} curfold]} {
    set c [lindex $curfold 0]
    set f [lindex $curfold 1]
  } else {
    set c 0
    set f inbox
  }

  set charset "UTF-8"

  cgi_http_head {
    WPStdHttpHdrs "text/html; charset=$charset"
  }

  cgi_html {
    cgi_head {
      cgi_content_type "text/html; charset=$charset"
      cgi_title [wpPageTitle "Settings"]
      cgi_base "href=$script_base"
      cgi_stylesheet css/cbn/screen.css
      # Yahoo Styles
      cgi_stylesheet $_wp(yui)/build/container/assets/container-core.css
      cgi_stylesheet $_wp(yui)/build/menu/assets/skins/sam/menu.css
      cgi_stylesheet $_wp(yui)/build/button/assets/skins/sam/button.css
      # YahooUI libraries
      cgi_script type=text/javascript language="JavaScript" src="$_wp(yui)/build/utilities/utilities.js" {}
      cgi_script type=text/javascript language="JavaScript" src="$_wp(yui)/build/container/container-min.js" {}
      cgi_script type=text/javascript language="JavaScript" src="$_wp(yui)/build/datasource/datasource-min.js" {}
      cgi_script type=text/javascript language="JavaScript" src="$_wp(yui)/build/menu/menu-min.js" {}
      cgi_script type=text/javascript language="JavaScript" src="$_wp(yui)/build/button/button-min.js" {}
      # local libraries
      cgi_script language="JavaScript" src="lib/common.js" {}
      cgi_script language="JavaScript" src="lib/settings.js" {}
      cgi_javascript {
	cgi_puts "YAHOO.alpine.cgi_root = '$_wp(serverpath)';"
	cgi_puts "YAHOO.alpine.cgi_base = '$script_base';"
	cgi_puts "YAHOO.alpine.current.incoming = [WPCmd PEFolder isincoming $c];"
	cgi_puts "YAHOO.alpine.current.c = $c;"
	cgi_puts "YAHOO.alpine.current.f = \"$f\";"
	cgi_puts "function bodyOnLoad() {"
	cgi_puts " if(YAHOO.env.ua.gecko > 0){ sizeVPHeight(); window.onresize = resizeVPHeight; }"
	cgi_puts " setCheckMailFunction('gCheck', newMailCheck);"
	cgi_puts " setNewMailCheckInterval([WPCmd PEInfo inputtimeout]);"
        cgi_puts "}"
	cgi_puts "browserDetect();"
      }
    }

    cgi_body class=wap "onLoad=bodyOnLoad()" {
      cgi_puts {<iframe name="formResponse" id="formResponse" src="img/cbn/spritelib.gif"></iframe>}
      wpCommonPageLayout {settings {
	cgi_hr
	cgi_division {
	  cgi_put [cgi_url [cgi_span "class=sp splci tbi5" "Back to $f"] browse/$c/$f class=wap title="Return to $f without saving changes"]
	}
	cgi_hr
	cgi_division class="ftitle bld" {
	  cgi_put "Basic Settings"
	}
	cgi_division "class=\"bld sel\"" {
	  cgi_put [cgi_url [cgi_span "class=sp splcs splc12" "General Preferences"] # class=wap id=Page1 "onClick=return settingsPage(this);"]
	}
	cgi_division class=bld {
	  cgi_put [cgi_url [cgi_span "class=sp splcs splc12" "Personal Preferences"] # class=wap id=Page2 "onClick=return settingsPage(this);"]
	}
	cgi_division class=bld {
	  cgi_put [cgi_url [cgi_span "class=sp splcs splc12" "News and Weather"] # class=wap id=Page3 "onClick=return settingsPage(this);"]
	}
	cgi_division class="ftitle bld" "style=\"margin-top: 2em;\"" {
	  cgi_put [cgi_url "[cgi_img img/cbn/f_plus.gif class=wap]  Advanced Settings" # class=wap "onClick=return toggleAdvance(this);"]
	}
	cgi_division id=advancedSettings {
	  cgi_division class=bld {
	    cgi_put [cgi_url [cgi_span "class=sp splcs splc12" "Message List"] # class=wap id=Page4 "onClick=return settingsPage(this);"]
	  }
	  cgi_division class=bld {
	    cgi_put [cgi_url [cgi_span "class=sp splcs splc12" "Message View"] # class=wap id=Page5 "onClick=return settingsPage(this);"]
	  }
	  cgi_division class=bld {
	    cgi_put [cgi_url [cgi_span "class=sp splcs splc12" "Folders"] # class=wap id=Page8 "onClick=return settingsPage(this);"]
	  }
	  cgi_division class=bld {
	    cgi_put [cgi_url [cgi_span "class=sp splcs splc12" "Compose"] # class=wap id=Page6 "onClick=return settingsPage(this);"]
	  }
	  cgi_division class="ftitle bld" "style=\"margin-top: 2em;\"" {
	    cgi_put "Server Settings"
	  }
	  cgi_division class=bld {
	    cgi_put [cgi_url [cgi_span "class=sp splcs splc12" "Mail Servers"] # class=wap id=Page10 "onClick=return settingsPage(this);"]
	  }
	  cgi_division class=bld {
	    cgi_put [cgi_url [cgi_span "class=sp splcs splc12" "Directory Servers"] # class=wap id=Page9 "onClick=return settingsPage(this);"]
	  }
	}
	if {[info exists _wp(filter_link)] || [info exists _wp(vacation_link)]} {
	  cgi_division class="ftitle bld" "style=\"margin-top: 2em;\"" {
	    cgi_put "External Settings"
	  }
	}
	if {[info exists _wp(filter_link)]} {
	  cgi_division class=bld {
	    cgi_put [cgi_url [cgi_span "class=sp splcs splc12" "Message Filtering"] $_wp(filter_link) class=wap target=_blank]
	  }
	}
	if {[info exists _wp(vacation_link)]} {
	  cgi_division class=bld {
	    cgi_put [cgi_url [cgi_span "class=sp splcs splc12" "Vacation Auto-Reply"] $_wp(vacation_link) class=wap target=_blank]
	  }
	}
      }} "$c" "$f" 0 Settings \
	  [list [cgi_cgi "$_wp(appdir)/$_wp(ui2dir)/browse/${c}/${f}"] Settings 0 searchContent('settings','alpineContent')] "" {
	# CONTEXT COMMANDS
	cgi_division class=hdrBtns {
	  cgi_javascript {
	    cgi_put "if(window.print) document.write('[cgi_buffer {cgi_put [cgi_url "[cgi_span "class=sp hdrBtnImg hbi1" ""][cgi_span "class=hdrBtnText" Print]" "print" "onClick=return printContent()"]}]');"
	  }

	  cgi_put [cgi_url "[cgi_span "class=sp hdrBtnImg hbi3" ""][cgi_span "class=hdrBtnText" Help]" "javascript:openHelpWindow('settings.html');" class=wap]
	  cgi_put [cgi_url "[cgi_span "class=sp hdrBtnImg hbi4" ""][cgi_span "class=hdrBtnText" "Sign out"]" "../../session/logout.tcl?cid=[WPCmd PEInfo key]&sessid=${sessid}"]
	}
      } {
	# TOP MENUBAR
	cgi_anchor_name "toolbar"
	cgi_table  class="toolbarTbl" cellpadding="0" cellspacing="0" {
	  cgi_puts "<tbody>"
	  cgi_table_row  {
	    cgi_table_data {
	      cgi_table class="toolbarTbl" cellpadding="0" cellspacing="0" {
		cgi_puts "<tbody>"
		cgi_table_row {
		  cgi_table_data class="wap" {
		    cgi_put [cgi_url [cgi_span "class=sp spmbi spmb10" "Save Settings"] "#" title="Save Settings Changes" "onClick=return saveSettings();"]
		  }
		  cgi_table_data class="wap" {
		    cgi_put [cgi_url [cgi_span "class=sp spmbi spmb21" "Reset to Default Settings"] "#" "onClick=panelConfirm('Restoring default settings will erase all custom settings.<p>Are you sure you want to restore default settings?',{text:'Restore Defaults',fn:restoreDefaultSettings}); return false;" title="Reset to Default Settings"]
		  }
		  cgi_table_data class="wap" {
		    cgi_put [cgi_url [cgi_span "class=sp spmbi spmb12" "Cancel"] "browse/$c/$f" title="Cancel Settings Changes"]
		  }
		  cgi_table_data  width="100%" {
		    cgi_puts [cgi_nbspace]
		  }
		}
		cgi_puts "</tbody>"
	      }
	    }
	  }
	  cgi_puts "</tbody>"
	}
      } {
	cgi_form $_wp(appdir)/$_wp(ui2dir)/conduit/settings.tcl "enctype=multipart/form-data" id=settingsForm target=formResponse {
	  cgi_text "restore=false" id=restore type=hidden notab
	  cgi_table id=settingsPage1 "class=\"fields settings\"" cellpadding="0" cellspacing="0" {
	    cgi_puts "<tbody>"
	    cgi_puts "<tr><td class=title colspan=2><h2>General Preferences</h2></td></tr>"
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_puts "Message Display"
		#[cgi_br]<a href="#" title="more info..." onClick="help_popup1.showPopup('anchor1');return false;" name="anchor1" id="anchor1"><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		if {[info exists _wp(themes)]} {
		  cgi_put "Theme: "
		  cgi_select theme {
		    foreach theme $_wp(themes) {
		      cgi_option [lindex $theme 0] value=[lindex $theme 1]
		    }
		  }
		  cgi_br
		}
		cgi_put "Display "
		set n [var_value wp-indexlines]
		cgi_select wp-indexlines {
		  for {set i 10} {$i <= $_wp(indexlinesmax)} {incr i 5} {
		    cgi_option $i value=$i [set_checked [expr {$i == $n}] selected]
		  }
		}
		cgi_put " messages per page"
		cgi_br
		cgi_put "Wrap Plain Text message at "
		set n [WPCmd PEConfig columns]
		cgi_select wrapColumn {
		  for {set i 20} {$i < 130} {incr i 4} {
		    cgi_option $i value=$i [set_checked [expr {$i == $n}] selected]
		  }
		}
		cgi_put " characters"
	      }
	    }
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Folders"
		#[cgi_br]<a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		cgi_put "Display "
		if {[catch {WPSessionState left_column_folders} n]} {
		  set n $_wp(fldr_cache_def)
		}

		cgi_select folderCache {
		  for {set i 5} {$i <= $_wp(fldr_cache_max)} {incr i} {
		    cgi_option $i value=$i [set_checked [expr {$i == $n}] selected]
		  }
		}
		cgi_put " recent folders in left column"
	      }
	    }
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Reply[cgi_nbspace]Options"
		#[cgi_br]<a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		set_feature include-header-in-reply "Include headers in replies"
		cgi_br
		set_feature include-attachments-in-reply "Include attachments in replies"
		cgi_br
		set_feature signature-at-bottom "Append signature below reply text"
		cgi_br
		set_feature strip-from-sigdashes-on-reply "Strip signatures when replying"
	      }
	    }
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Forwarding[cgi_nbspace]Options"
		#[cgi_br]<a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		set as_attach [WPCmd PEInfo feature forward-as-attachment]
		cgi_radio_button forwardAs=inline id="forward_inline" [set_checked [expr {$as_attach == 0}]]
		cgi_put " <label for=forward_inline>Forward messsages inline</label>"
		cgi_br
		cgi_radio_button forwardAs=attached id="forward_attachment" [set_checked $as_attach]
		cgi_put " <label for=forward_attachment>Forward messsages as attachments</label>"
	      }
	    }
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Sent[cgi_nbspace]Message[cgi_nbspace]Options"
		cgi_put "<p><span class=tips><strong>Tip:</strong> Set folder blank to prevent saving of Sent messages</span>"
		#[cgi_br]<a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>

	      }
	      cgi_table_data class="body" {
		set_variable default-fcc "Name of your Sent mail folder (also called \"Fcc\"): "
		cgi_br
		set_feature fcc-without-attachments "Save sent messages to Sent folder without attachments"
	      }
	    }
	    cgi_puts "</tbody>"
	  }
	  cgi_table id=settingsPage2 "class=\"fields settings\"" "style=\"display: none;\"" cellpadding="0" cellspacing="0" {
	    cgi_puts "<tbody>"
	    cgi_puts "<tr><td class=title colspan=2><h2>Personal[cgi_nbspace]Information</h2></td></tr>"
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Display[cgi_nbspace]Name"
		#<br><a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		set_variable personal-name
	      }
	    }
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "User[cgi_nbspace]Domain"
		#<br><a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		set_variable user-domain
	      }
	    }
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Email[cgi_nbspace]Signature"
		#<br><a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		#<input name="signature" id="no_sig" type="radio" checked />
		#<label for="no_sig">No Signature</label><br>
		#<input name="signature" id="use_sig" type="radio" />
		#<label for="use_sig">Append Signature when composing messages</label><br>
		cgi_textarea signature=[join [WPCmd PEInfo rawsig] "\n"] cols="72" rows="5" title=Signature
	      }
	    }
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Alternate[cgi_nbspace]Addresses"
		cgi_put "<p><span class=tips><b>Tip:</b> List alternate email addresses "
		cgi_put "you use. Reply All will not include"
		cgi_put "these email addresses when replying.</span>"
		#<br><a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		cgi_put "Add/Remove Addresses: <input type=button name=addCH value=Add onClick=\"return listAdd('altAddrTable','altAddr');\"/>"
		set varval [WPCmd PEConfig varget alt-addresses]
		cgi_text "altAddrs=[llength [lindex $varval 0]]" id=altAddrs type=hidden notab
		cgi_table id=altAddrTable {
		  set n 0
		  foreach svr [lindex $varval 0] {
		    incr n
		    cgi_table_row {
		      cgi_table_data {
			cgi_text altAddr${n}=[string trim $svr] size="45"
			cgi_put [cgi_url [cgi_img img/cbn/remove.gif class=wap] # "onClick=return removeTableRow(this);"]
		      }
		    }
		  }
		}
	      }
	    }
	    cgi_puts "</tbody>"
	  }
	  cgi_table id=settingsPage3 "class=\"fields settings\"" "style=\"display: none;\"" cellpadding="0" cellspacing="0" {
	    cgi_puts "<tbody>"
	    cgi_puts "<tr><td class=title colspan=2><h2>News[cgi_nbspace]and[cgi_nbspace]Weather</h2></td></tr>"

	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Headline[cgi_nbspace]News"
		#<br><a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		cgi_put "RSS URL: "
		cgi_text rss-news=[var_value rss-news] size="45"
	      }
	    }
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Weather[cgi_nbspace]Bar"
		#<br><a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		cgi_put "RSS URL: "
		cgi_text rss-weather=[var_value rss-weather] size="45"
	      }
	    }
	    cgi_puts "</tbody>"
	  }
	  cgi_table id=settingsPage4 "class=\"fields settings\"" "style=\"display: none;\"" cellpadding="0" cellspacing="0" {
	    cgi_puts "<tbody>"
	    cgi_puts "<tr><td class=title colspan=2><h2>Message[cgi_nbspace]List</h2></td></tr>"

	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Message List"
	      }
	      cgi_table_data class="body" {
		cgi_table {
		  cgi_put "<tbody>"
		  cgi_table_row {
		    cgi_table_data {
		      cgi_put "Sort Order (default): "
		    }
		    cgi_table_data {
		      set val [WPCmd PEConfig varget sort-key]
		      cgi_select sort-key {
			foreach k [lindex $val 2] {
			  cgi_option $k value=$k [set_checked [expr {0 == [string compare -nocase $k [lindex $val 0]]}] selected]
			}
		      }
		    }
		  }
		  cgi_table_row {
		    cgi_table_data {
		      cgi_put "Start display at:"
		    }
		    cgi_table_data {
		      set val [WPCmd PEConfig varget incoming-startup-rule]
		      cgi_select incoming-startup-rule {
			foreach k [lindex $val 2] {
			  cgi_option $k value=$k [set_checked [expr {0 == [string compare -nocase $k [lindex $val 0]]}] selected]
			}
		      }
		    }
		  }
		  cgi_put "</tbody>"
		}
	      }
	    }
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Message[cgi_nbspace]Handling"
	      }
	      cgi_table_data class="body" {
		set_variable read-message-folder "Name of the folder to hold your read messages:  "
		cgi_br
		set_feature auto-move-read-msgs "Automatically Move Read Messages"
	      }
	    }
	    cgi_puts "</tbody>"
	  }
	  cgi_table id=settingsPage5 "class=\"fields settings\"" "style=\"display: none;\"" cellpadding="0" cellspacing="0" {
	    cgi_puts "<tbody>"
	    cgi_puts "<tr><td class=title colspan=2><h2>Message View</h2></td></tr>"

	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Display[cgi_nbspace]Headers"
	      }
	      cgi_table_data class="body" {
		cgi_put "Add/Remove Header Names: <input type=button name=addCH value=Add onClick=\"return listAdd('viewerHdrsTable','viewerHdr');\"/>"
		set varval [WPCmd PEConfig varget viewer-hdrs]
		cgi_text "viewerHdrs=[llength [lindex $varval 0]]" id=viewerHdrs type=hidden notab
		cgi_table id=viewerHdrsTable {
		  set n 0
		  foreach svr [lindex $varval 0] {
		    incr n
		    cgi_table_row {
		      cgi_table_data {
			cgi_text viewerHdr${n}=[string trim $svr] size="45"
			cgi_put [cgi_url [cgi_img img/cbn/remove.gif class=wap] # "onClick=return removeTableRow(this);"]
		      }
		    }
		  }
		}
	      }
	    }

	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Display[cgi_nbspace]Links[cgi_nbspace]in[cgi_nbspace]Messages"
		cgi_put "<p><span class=tips><b>Tip:</b> Applies only to plain text messages.</span>"
		#<br><a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		cgi_table {
		  cgi_put "<tbody>"
		  cgi_table_row {
		    cgi_table_data {
		      set_feature enable-msg-view-urls "Display complete URLs as links"
		    }
		  }
		  cgi_table_row {
		    cgi_table_data {
		      set_feature enable-msg-view-web-hostnames "Display hostnames and incomplete URLs as links"
		    }
		  }
		  cgi_table_row {
		    cgi_table_data {
		      set_feature enable-msg-view-addresses "Display Email addresses as links"
		    }
		  }
		  cgi_put "</tbody>"
		}
	      }
	    }
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Rich[cgi_nbspace]Text[cgi_nbspace]Display"
		#<br><a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		set_feature render-html-internally "Show rich text messages as plain text"
	      }
	    }
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Anti-phishing"
		cgi_put "<p><span class=tips><b>Tip:</b> This feature aids in identifying fake links.  Applies only when rich text shown as plain text.</span>"
		#<br><a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		set_feature quell-server-after-link-in-html "Hide appended real hostname after links"
	      }
	    }
	    cgi_puts "</tbody>"
	  }
	  cgi_table id=settingsPage6 "class=\"fields settings\"" "style=\"display: none;\"" cellpadding="0" cellspacing="0" {
	    cgi_puts "<tbody>"
	    cgi_puts "<tr><td class=title colspan=2><h2>Compose</h2></td></tr>"
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Compose[cgi_nbspace]Headers"
		cgi_put "<p><span class=tips><b>Tip:</b> Default header fields displayed in Compose</span>"
	      }
	      cgi_table_data class="body" {
		cgi_put "Add/Remove Header Names: <input type=button name=addCH value=Add onClick=\"return listAdd('composeHdrsTable','composeHdr');\"/>"
		set varval [WPCmd PEConfig varget default-composer-hdrs]
		cgi_text "composeHdrs=[llength [lindex $varval 0]]" id=composeHdrs type=hidden notab
		cgi_table id=composeHdrsTable {
		  set n 0
		  foreach svr [lindex $varval 0] {
		    incr n
		    cgi_table_row {
		      cgi_table_data {
			cgi_text composeHdr${n}=[string trim $svr] size="45"
			cgi_put [cgi_url [cgi_img img/cbn/remove.gif class=wap] # "onClick=return removeTableRow(this);"]
		      }
		    }
		  }
		}
	      }
	    }
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Custom[cgi_nbspace]Headers"
	      }
	      cgi_table_data class="body" {
		cgi_put "Add/Remove Headers: <input type=button name=addhdr value=Add onClick=\"return customHdrAdd('customHdrTbl');\"/>"
		set varval [WPCmd PEConfig varget customized-hdrs]
		cgi_text "customHdrFields=[llength [lindex $varval 0]]" id=customHdrFields type=hidden notab
		cgi_table id=customHdrTbl {
		  set nh 0
		  foreach hdr [lindex $varval 0] {
		    incr nh
		    cgi_table_row {
		      set n [string first {:} $hdr]
		      switch -- $n {
			-1 -
			0 -
			1 {
			  cgi_table_data colspan=2 {
			    cgi_text "customHdrField${nh}=[cgi_quote_html $hdr]" type=hidden notab
			    cgi_put "$hdr"
			  }
			}
			default {
			  cgi_table_data {
			    set field_name [string range $hdr 0 [expr {$n - 1}]]
			    cgi_put $field_name
			    cgi_text "customHdrField${nh}=[cgi_quote_html $field_name]" type=hidden notab
			  }
			  cgi_table_data {
			    cgi_text customHdrData${nh}=[string trim [string range $hdr [incr n] end]] size="45"
			    cgi_put [cgi_url [cgi_img img/cbn/remove.gif class=wap] # "onClick=return removeTableRow(this);"]
			  }
			}
		      }
		    }
		  }
		}
	      }
	    }
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Message[cgi_nbspace]Encoding"
		#[cgi_br]<a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		set_variable posting-character-set "Send messages using character encoding: "
	      }
	    }

	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Reply[cgi_nbspace]Options"
		#[cgi_br]<a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		set_variable reply-leadin "Reply intro string: "
		cgi_br
		set_variable reply-indent-string "Reply prefix: "
	      }
	    }
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Flowed[cgi_nbspace]Text[cgi_nbspace]Handling"
		#[cgi_br]<a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		set_feature quell-flowed-text "Do not send text as Flowed Text"
	      }
	    }
	    cgi_puts "</tbody>"
	  }
	  cgi_table id=settingsPage7 "class=\"fields settings\"" "style=\"display: none;\"" cellpadding="0" cellspacing="0" {
	    cgi_puts "<tbody>"
	    cgi_puts "<tr><td class=title colspan=2><h2>Filters</h2></td></tr>"

	    cgi_puts "</tbody>"
	  }
	  cgi_table id=settingsPage8 "class=\"fields settings\"" "style=\"display: none;\"" cellpadding="0" cellspacing="0" {
	    cgi_puts "<tbody>"
	    cgi_puts "<tr><td class=title colspan=2><h2>Folders</h2></td></tr>"
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Draft[cgi_nbspace]Folder"
		#[cgi_br]<a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		set_variable postponed-folder
	      }
	    }
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Trash[cgi_nbspace]Folder"
		#[cgi_br]<a href="#" title="more info..."><img src="img/cbn/help_sm.gif"></a>
	      }
	      cgi_table_data class="body" {
		set_variable trash-folder
	      }
	    }
	    cgi_puts "</tbody>"
	  }
	  if {0 == [catch {WPCmd PEConfig varget ldap-servers} varval]} {
	    cgi_table id=settingsPage9 "class=\"fields settings\"" "style=\"display: none;\"" cellpadding="0" cellspacing="0" {
	      cgi_puts "<tbody>"
	      cgi_puts "<tr><td class=title colspan=2><h2>Directory[cgi_nbspace]Servers</h2></td></tr>"

	      cgi_table_row {
		cgi_table_data class="title" {
		  cgi_put "LDAP[cgi_nbspace]Server"
		}
		cgi_table_data class="body" {
		  cgi_put "Add/Remove Servers: <input type=button name=addldap value=Add onClick=\"return listAdd('ldapServerTable','ldapServer');\"/>"
		  cgi_text "ldapServers=[llength [lindex $varval 0]]" id=ldapServers type=hidden notab
		  cgi_table id=ldapServerTable {
		    set n 0
		    foreach svr [lindex $varval 0] {
		      incr n
		      cgi_table_row {
			cgi_table_data {
			  cgi_text ldapServer${n}=[string trim $svr] size="45"
			  cgi_put [cgi_url [cgi_img img/cbn/remove.gif class=wap] # "onClick=return removeTableRow(this);"]
			}
		      }
		    }
		  }
		}
	      }

	      cgi_puts "</tbody>"
	    }
	  }
	  cgi_table id=settingsPage10 "class=\"fields settings\"" "style=\"display: none;\"" cellpadding="0" cellspacing="0" {
	    cgi_puts "<tbody>"
	    cgi_puts "<tr><td class=title colspan=2><h2>Mail[cgi_nbspace]Servers</h2></td></tr>"
	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "Inbox[cgi_nbspace]Server"
		cgi_put "<p><span class=tips><b>Tip:</b> Server name inside brackets, folder name after</span>"
	      }
	      cgi_table_data class="body" {
		cgi_text inbox-path=[var_value inbox-path] size="45"
	      }
	    }

	    cgi_table_row {
	      cgi_table_data class="title" {
		cgi_put "SMTP[cgi_nbspace]Server"
	      }
	      cgi_table_data class="body" {
		cgi_put "Add/Remove Servers: <input type=button name=addsmtp value=Add onClick=\"return listAdd('smtpServerTable','smtpServer');\"/>"
		set varval [WPCmd PEConfig varget smtp-server]
		cgi_text "smtpServers=[llength [lindex $varval 0]]" id=smtpServers type=hidden notab
		cgi_table id=smtpServerTable {
		  set n 0
		  foreach svr [lindex $varval 0] {
		    incr n
		    cgi_table_row {
		      cgi_table_data {
			cgi_text smtpServer${n}=[string trim $svr] size="45"
			cgi_put [cgi_url [cgi_img img/cbn/remove.gif class=wap] # "onClick=return removeTableRow(this);"]
		      }
		    }
		  }
		}
	      }
	    }

	    cgi_puts "</tbody>"
	  }
	}
      } {
	# BOTTOM MENUBAR
	cgi_table class="wap toolbarTbl" cellpadding="0" cellspacing="0" {
	  cgi_puts "<tbody><tr><td>&nbsp;</td></tr></tbody>"
	}
      }
    }
  }
}
