#!./tclsh
# $Id: contacts 1160 2008-08-22 01:12:33Z mikes@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 contacts for Web Alpine 2.0 pages
#
#  Input:    PATH_INFO: [/<col_number>]/<folder_name>[/<uid_of_viewed_msg>
#            along with possible search parameters:
set contacts_args {
}

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

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

# WHILE TESTING/DEBUGGING
proc noimp {s} {
  return "onClick=return noImp('${s}');"
}

set dmsgs ""
proc dm {s} {
  global dmsgs

  lappend dmsgs $s
}


WPEval $contacts_args {
  # verify current collection/folder
  if {[catch {WPCmd PEFolder current} curfold]} {
    error [list _action browse "cannot determine default folder: $curfold"]
  } else {
    set c [lindex $curfold 0]
    set f [lindex $curfold 1]
  }

  set charset "UTF-8"
  set u 0

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

  cgi_html {
    cgi_head {
      cgi_content_type "text/html; charset=$charset"
      #      WPStdHttpHdrs "text/html; charset=$charset"
      cgi_title [wpPageTitle "Contacts"]
      cgi_base "href=$_wp(serverpath)/$_wp(appdir2)/"

      cgi_stylesheet css/menu.css
      cgi_stylesheet css/cbn/screen.css
      cgi_stylesheet css/cbn/contacts.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/button/button-min.js" {}
      cgi_script type=text/javascript language="JavaScript" src="$_wp(yui)/build/container/container.js" {}
      cgi_script type=text/javascript language="JavaScript" src="$_wp(yui)/build/datasource/datasource-beta-min.js" {}
      cgi_script type=text/javascript language="JavaScript" src="$_wp(yui)/build/menu/menu-min.js" {}
      # local libraries
      cgi_script language="JavaScript" src="lib/common.js" {}
      cgi_script language="JavaScript" src="lib/contacts.js" {}

      cgi_javascript {
	cgi_puts "var gCurrentAbook = 0;"
	cgi_puts "YAHOO.alpine.current.c = '$c';"
	cgi_puts "YAHOO.alpine.current.f = '$f';"
	cgi_puts "YAHOO.alpine.current.u = $u;"
	cgi_puts "function updateContactCount(nBooks,nContacts){"
	cgi_puts "  var el = document.getElementById('contactCount');"
	cgi_puts "  if(el) el.innerHTML = nContacts + ' Contacts';"
	cgi_puts "}"
	cgi_puts "function bodyOnLoad() {"
	cgi_puts " initMenus();"
	cgi_puts " if(YAHOO.env.ua.gecko > 0){ sizeVPHeight(); window.onresize = resizeVPHeight; }"
	cgi_puts " setNewMailCheckInterval([WPCmd PEInfo inputtimeout]);"
	cgi_puts " drawContactList('alpineContent',gCurrentAbook,{hdr:'on',sendto:'on',canedit:'on'});"
        cgi_puts "}"

	# WHILE TESTING/DEBUGGING
	cgi_puts "var impi = new Array(); function noImp(m){ var sm; if(impi\[m\]) {impi\[m\]++; if(impi\[m\] > 4) sm = '<b>Seriously,</b> ' + m + ' will <b>never</b> get implemented if you keep bothering me!'; else sm = m + ' is <b>still</b> not implemented!'; } else { sm = m + ' is not implemented yet!' ; impi\[m\] = 1 ;} showStatusMessage(sm,3); return false; }"

	cgi_puts "browserDetect();"
      }
    }

    cgi_body class=wap "onLoad=bodyOnLoad()" {

      wpCommonPageLayout contacts "$c" "$f" "$u" "Contacts" [list [cgi_cgi "$_wp(appdir2)/contacts"] Contacts 0 searchContent('contacts','clistContacts')] "" {
	# CONTEXT COMMANDS
	cgi_division class=hdrBtns {
	  cgi_put [cgi_url "[cgi_span "class=sp hdrBtnImg hbi1" ""][cgi_span "class=hdrBtnText" Print]" "print" "onClick=if(window.print){window.print();return false;}else return true;"]
	  cgi_put [cgi_url "[cgi_span "class=sp hdrBtnImg hbi2" ""][cgi_span "class=hdrBtnText" Settings]" "settings"]
	  cgi_put [cgi_url "[cgi_span "class=sp hdrBtnImg hbi3" ""][cgi_span "class=hdrBtnText" Help]" "javascript:openHelpWindow('contacts.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}"]
	}
      } {
	# TO 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_puts [cgi_url [cgi_span "class=sp spmbi spmb13" "New Contact"] "contacts/new" title="Create new contact" "onClick=return contactEditor({which:'add'},storeNewContact);"]

		  }
		  cgi_table_data class=wap {
		    cgi_puts [cgi_url [cgi_span "class=sp spmbi spmb14" "New Group"] "contacts/new/group" title="Create new group contact" "onClick=return contactEditor({which:'add',group:true},storeNewContact);"]
		  }
		  cgi_table_data  class="dv1" {
		    cgi_puts [cgi_img "img/cbn/div.gif"]
		  }
		  cgi_table_data class=wap {
		    cgi_puts [cgi_url [cgi_span "class=sp spmbi spmb9" "Send Email"] "send" title="Send email to selected contact" "onClick=return sendToContact();"]
		  }
		  cgi_table_data class=wap {
		    cgi_puts [cgi_url "[cgi_img "img/cbn/edit.gif" class=wap] Edit" "#" title="Edit selected contact" "onClick=return editCheckedContact();"]
		  }
		  cgi_table_data class=wap {
		    cgi_puts [cgi_url "[cgi_img "img/cbn/delete.gif" class=wap] Delete" "#" title="Delete selected contact" "onClick=return contactDelete();"]
		  }
		  cgi_table_data  class="dv1" {
		    cgi_puts [cgi_img "img/cbn/div.gif"]
		  }
		  cgi_table_data class=wap {
		    cgi_bullet_list class="menu" {
		      cgi_put "<li class=\"menuHdr\">[cgi_url "More Actions [cgi_img "img/cbn/menu.gif" class="wap menuDn menuImg"]" "#" "onClick=return false;"]<div>"
		      cgi_bullet_list {
			cgi_li [cgi_url "Import vCards" "#" [noimp "vCard import"]]
			cgi_li [cgi_url "Export vCards" "#" [noimp "vCard export"]]
		      }
		      cgi_puts "</div></li>"
		    }
		  }
		  cgi_table_data  width="100%" {
		    cgi_puts [cgi_nbspace]
		  }
		}
		cgi_puts "</tbody>"
	      }
	    }
	  }
	  cgi_puts "</tbody>"
	}
      } {
	# MAIN PAGE CONTENT
	cgi_puts "Loading..."
      } {
	# BOTTOM MENUBAR
	cgi_table class="wap toolbarTbl" cellpadding="0" cellspacing="0" {
	  cgi_puts "<tbody>"
	  cgi_table_row  {
	    cgi_table_data class="wap pageText" id=contactCount {
	      cgi_puts ""
	    }
	    cgi_table_data width="100%" {
	      cgi_puts [cgi_nbspace]
	    }
	  }
	  cgi_puts "</tbody>"
	}
      }

      # any debugging info to insert?
      foreach dmsg $dmsgs {
	cgi_html_comment "DEBUG: $dmsg"
	cgi_puts ""
      }
    }
  }
}
