home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 December / PCWorld_2005-12_cd.bin / komunikace / netscape / nsb-install-8-0.exe / chrome / browser.jar / content / browser / webmail.js < prev   
Text File  |  2005-09-26  |  82KB  |  2,306 lines

  1. var gWebMailObv;
  2. var webmail = {
  3.     WEBMAIL_DEBUG : true,
  4.     services : Array(),
  5.     Prefs : null,
  6.     WEBMAIL_NS : "http://netscape.com/spui/components/email/services/1.0#",
  7.  
  8.     WEBMAIL_RES_NAME : null,
  9.     WEBMAIL_RES_INBOX : null,
  10.     WEBMAIL_RES_PCDOMAIN : null,
  11.     WEBMAIL_RES_PCUSERNAME : null,
  12.     WEBMAIL_RES_PCPASSWORD : null,
  13.     WEBMAIL_RES_WRITE : null,
  14.     WEBMAIL_RES_ADDRESS : null,
  15.     WEBMAIL_RES_NOTIFY : null,
  16.     WEBMAIL_RES_USERNAME : null,
  17.     WEBMAIL_RES_ICON : null,
  18.     WEBMAIL_RES_AUTOLOGURL : null,
  19.     WEBMAIL_RES_NEEDLOGIN : null,
  20.     WEBMAIL_RES_URLCAPTURE : null,
  21.     WEBMAIL_RES_DEFAULTURL : null,
  22.     WEBMAIL_RES_COMPOSEFIELDS : null,
  23.     RDFService : null,
  24.     RDFDataSource : null,
  25.     RDFLoadObserver : null,
  26.     CheckedNumbers : Array(),
  27.     CapturedData : Array(),
  28.     AutologObservers : Array(),
  29.     mDefaultAccount : null,
  30.     mCustomAccountsArray: Array(),
  31.  
  32.     Init : function () {
  33.         // Initialize the webmail preferences branch object
  34.         this.debug("webmail.Init() called");
  35.         if (!this.Prefs) {
  36.             this.Prefs=gPrefService.getBranch("browser.webmail.");
  37.         }
  38.         if (!this.RDFService) {
  39.             this.RDFService = Components.classes["@mozilla.org/rdf/rdf-service;1"]
  40.                                         .getService(Components.interfaces.nsIRDFService);
  41.             this.WEBMAIL_RES_NAME = this.RDFService.GetResource(this.WEBMAIL_NS+'name');
  42.             this.WEBMAIL_RES_INBOX = this.RDFService.GetResource(this.WEBMAIL_NS+'inboxuri');
  43.             this.WEBMAIL_RES_PCDOMAIN = this.RDFService.GetResource(this.WEBMAIL_NS+'passcarddomain');
  44.             this.WEBMAIL_RES_PCUSERNAME = this.RDFService.GetResource(this.WEBMAIL_NS+'passcardusername');
  45.             this.WEBMAIL_RES_PCPASSWORD = this.RDFService.GetResource(this.WEBMAIL_NS+'passcardpassword');
  46.             this.WEBMAIL_RES_WRITE = this.RDFService.GetResource(this.WEBMAIL_NS+'newemailuri');
  47.             this.WEBMAIL_RES_ADDRESS = this.RDFService.GetResource(this.WEBMAIL_NS+'addressbookuri');
  48.             this.WEBMAIL_RES_NOTIFY = this.RDFService.GetResource(this.WEBMAIL_NS+'notify');
  49.             this.WEBMAIL_RES_USERNAME = this.RDFService.GetResource(this.WEBMAIL_NS+'usernametext');
  50.             this.WEBMAIL_RES_ICON = this.RDFService.GetResource(this.WEBMAIL_NS+'icon');
  51.             this.WEBMAIL_RES_AUTOLOGURL = this.RDFService.GetResource(this.WEBMAIL_NS+'autologurl');
  52.             this.WEBMAIL_RES_NEEDLOGIN = this.RDFService.GetResource(this.WEBMAIL_NS+'needlogin');
  53.             this.WEBMAIL_RES_URLCAPTURE = this.RDFService.GetResource(this.WEBMAIL_NS+'urlcapture');
  54.             this.WEBMAIL_RES_DEFAULTURL = this.RDFService.GetResource(this.WEBMAIL_NS+'defaulturl');
  55.             this.WEBMAIL_RES_COMPOSEFIELDS = this.RDFService.GetResource(this.WEBMAIL_NS+'composefields');
  56.             this.RDFDataSource = null;
  57.             this.mCustomAccountsArray = new Array();
  58.         }
  59.         if (!this.RDFLoadObserver) {
  60.             this.RDFLoadObserver = {
  61.                 onBeginLoad : function(sink){},
  62.                 onInterrupt : function(sink){},
  63.                 onResume : function(sink){},
  64.                 onError : function(sink,status,msg){},
  65.                 onEndLoad : function(sink) {
  66.                     RDFService = Components.classes["@mozilla.org/rdf/rdf-service;1"]
  67.                                            .getService(Components.interfaces.nsIRDFService);
  68.                     sink.removeXMLSinkObserver(this);
  69.                     sink.QueryInterface(Components.interfaces.nsIRDFDataSource);
  70.                     this.parent.debug('Loaded datasource: '+sink.URI);
  71.                     resources = sink.GetAllResources();
  72.                     while (resources.hasMoreElements()) {
  73.                         res = resources.getNext();
  74.                         res.QueryInterface(Components.interfaces.nsIRDFResource);
  75.                         this.parent.HandleResource(sink, res);
  76.                         RDFService.UnregisterResource(res);
  77.                     }
  78.                     RDFService.UnregisterDataSource(sink);
  79.                     this.parent.services.sort(sort_services);
  80.                     for (var i=0;i<this.parent.services.length;i++) {
  81.                         this.parent.debug("services["+i+"]="+this.parent.services[i].name);
  82.                     }
  83.                     this.parent.fixMenus();
  84.                     webmail.fixCustomAccountMenus();
  85.                 },
  86.             };
  87.             this.RDFLoadObserver.parent = this;
  88.         }
  89.         // Get pref value for URL to grab supported components from
  90.         var WEBMAIL_SUPPORTED_SERVICES = gPrefService.getCharPref("browser.webmail.services");
  91.  
  92.         // Start loading RDF file of supported components
  93.         this.RDFDataSource = this.RDFService.GetDataSource(WEBMAIL_SUPPORTED_SERVICES);
  94.  
  95.         // Register a listener to pick things up when the file is done
  96.         // loading.  (Control will go to the onEndLoad function...)
  97.         this.RDFDataSource.QueryInterface(Components.interfaces.nsIRDFXMLSink);
  98.         this.RDFDataSource.addXMLSinkObserver(this.RDFLoadObserver);
  99.     },
  100.  
  101.     readDefault : function () {
  102.         if (this.mDefaultAccount) {
  103.             this.read(this.mDefaultAccount);
  104.         } else {
  105.             this.openManage(null);
  106.         }
  107.     },
  108.  
  109.     writeDefault : function () {
  110.         if (this.mDefaultAccount) {
  111.             this.write(this.mDefaultAccount);
  112.         } else {
  113.             this.openManage(null);
  114.         }
  115.     },
  116.  
  117.     addressDefault : function () {
  118.         if (this.mDefaultAccount) {
  119.             this.address(this.mDefaultAccount);
  120.         } else {
  121.             this.openManage(null);
  122.         }
  123.     },
  124.  
  125.     getServiceByName : function (wantname) {
  126.         // Return a service by name
  127.         for (var i = 0;i<this.services.length;i++) {
  128.             if (wantname == this.services[i].name) {
  129.                 return this.services[i];
  130.             }
  131.         }
  132.         return null;
  133.     },
  134.  
  135.     setChecked : function (accountName,num) {
  136.           for (var i=0;i<this.CheckedNumbers.length;i++){
  137.               if (this.CheckedNumbers[i].name == accountName) {
  138.                   this.CheckedNumbers[i].value=num;
  139.                   return;
  140.               }
  141.           }
  142.           var last=this.CheckedNumbers.length;
  143.           this.CheckedNumbers[last] = new Object();
  144.           this.CheckedNumbers[last].name = new String(accountName);
  145.           this.CheckedNumbers[last].value = new Number(num);
  146.     },
  147.  
  148.     getChecked : function (accountName) {
  149.           for (var i=0;i<this.CheckedNumbers.length;i++){
  150.               if (this.CheckedNumbers[i].name == accountName) {
  151.                   return this.CheckedNumbers[i].value;
  152.               }
  153.           }
  154.           return 0;
  155.     },
  156.  
  157.     getServiceById : function (wantid) {
  158.         // Return a service by id
  159.         for (var i=0;i<this.services.length;i++) {
  160.             if (wantid == this.services[i].id) {
  161.                 return this.services[i];
  162.             }
  163.         }
  164.         return null;
  165.     },
  166.  
  167.     dumpMenu : function (menuId) {
  168.         try {
  169.             var temp = document.getElementById(menuId).firstChild;
  170.               this.debug("Menu: "+menuId+" dump:");
  171.               while(temp) {
  172.                   this.debug("\tItem ("+temp.id+")"+temp.label);
  173.                   temp = temp.nextSibling;
  174.               }
  175.         } catch (ex) {
  176.             this.debug("can't dump menu: "+menuId);
  177.             this.debug(ex);
  178.         }
  179.     },
  180.  
  181.  
  182.     // MERC - JCH: Get all custom accounts and set their icons if possible
  183.     fixCustomAccountMenus: function () {
  184.  
  185.         // Get all webmail accounts stored as prefs
  186.         var count = {value:0};
  187.         var prefAccounts;
  188.         try {
  189.             prefAccounts = this.Prefs.getChildList("provider.", count);
  190.         } catch (ex) {
  191.             return;
  192.         }
  193.  
  194.         var arrayCount = 0;
  195.         mCustomAccountsArray = new Array();
  196.         for (var i=0; i<prefAccounts.length; i++) {
  197.  
  198.             // Get the name of the account's provider
  199.             if (this.Prefs.prefHasUserValue(prefAccounts[i]) && 
  200.                (this.Prefs.getPrefType(prefAccounts[i]) == this.Prefs.PREF_STRING)) {
  201.  
  202.                 // If it is not a custom account, go to next provider
  203.                 if (this.Prefs.getCharPref(prefAccounts[i]) != "webmail:other") {
  204.                     continue;
  205.                 }
  206.  
  207.                 mCustomAccountsArray[arrayCount] = new Object();
  208.                 
  209.                 // Custom provider found, record name
  210.                 var accountName = prefAccounts[i].substr(9);
  211.                 mCustomAccountsArray[arrayCount].name = prefAccounts[i].substr(9);
  212.  
  213.  
  214.                 // Record the inbox url for account
  215.                 var inboxPrefName = "InboxURL."+accountName;
  216.                 var inboxUrl = "";
  217.                 if (this.Prefs.prefHasUserValue(inboxPrefName) && 
  218.                    (this.Prefs.getPrefType(inboxPrefName) == this.Prefs.PREF_STRING)) {    
  219.                    inboxUrl = this.Prefs.getCharPref(inboxPrefName);
  220.                    mCustomAccountsArray[arrayCount].url = this.Prefs.getCharPref(inboxPrefName);
  221.                 }
  222.  
  223.                 arrayCount++;
  224.                 
  225.                 var hostName;
  226.                 var protocol;
  227.                 var endDomain;    
  228.                 
  229.                 // Check if inbox url is basically valid
  230.                 var start = inboxUrl.indexOf("://");
  231.  
  232.                 // If some scheme was provided and length is valid.
  233.                 // Note: assuming that a valid protocol has at least four letters.
  234.                 if ((start > 3) && ((start + 3) <= (inboxUrl.length -1))) {
  235.                     start = start + 3;
  236.                     protocol = inboxUrl.substring(0, start);
  237.                     var tempstr = inboxUrl.substring(start);
  238.                     endDomain = tempstr.indexOf("/");
  239.  
  240.                     // If there is a slash after '://', set defaults.
  241.                     // Otherwise, assume a valid domain is defined.
  242.                     if (endDomain != 0) {
  243.                         // Construct hostname => "http://bla.bla.bla"
  244.                         if (endDomain != -1) {
  245.                             hostName = protocol + inboxUrl.substring(start, endDomain);
  246.                         } else {
  247.                             hostName = protocol + inboxUrl.substring(start, inboxUrl.length);
  248.                         }
  249.  
  250.                         // Retrieve favicons from site using a short delay before execution
  251.                         setTimeout("webmail.connectToRemoteSite('" + inboxUrl + "', '" + hostName + "')", 50);
  252.                         continue;
  253.                     }
  254.                 }
  255.  
  256.                 // If we get here, assume that inbox url is garbage and set defaults
  257.                 this.setCustomAccountIcons(inboxUrl);
  258.  
  259.             } // End if
  260.         } // End for
  261.     },
  262.  
  263.     // MERC - JCH: Set custom account(s) menu item favicon
  264.     setCustomAccountIcons : function(aUri, faviconUri) {
  265.  
  266.         // If no input favicons, set default icons
  267.         if (!faviconUri)
  268.             faviconUri = "chrome://browser/skin/icons/mail_sm.png";
  269.  
  270.         var accountName;
  271.         for (var i=0; i<mCustomAccountsArray.length; i++) {
  272.             if (mCustomAccountsArray[i].url == aUri) {
  273.                 accountName = mCustomAccountsArray[i].name;
  274.                 document.getElementById(accountName).setAttribute("image", faviconUri);
  275.                 document.getElementById(accountName).setAttribute("class", "menu-iconic");
  276.  
  277.                 // If this webmail account is the default account, set the toolbar button icon as well
  278.                 if (accountName == this.mDefaultAccount) {
  279.                     document.getElementById("webmail-menu-image").setAttribute("image", faviconUri);
  280.                      document.getElementById("webmail-menu-image").setAttribute("tooltiptext", "Webmail");
  281.                      document.getElementById("webmail-toolbarbutton").setAttribute("tooltiptext", "Webmail");
  282.                      document.getElementById("webmail-readmenuitem").setAttribute("src", faviconUri);
  283.                 }
  284.             }
  285.         }
  286.     },
  287.  
  288.  
  289.     connectToRemoteSite: function(aUri, hostName) {
  290.  
  291.         var xmlhttp = new XMLHttpRequest();
  292.         xmlhttp.onload = function(event) { webmail.myCallBackFunc(event, hostName, aUri); }
  293.         //xmlhttp.onload = this.myCallBackFunc;
  294.         xmlhttp.overrideMimeType("text/xml"); // And override content-type to avoid breakage
  295.  
  296.         try {
  297.             xmlhttp.open("GET", aUri, true, null, null);
  298.         } catch (ex) {}
  299.  
  300.         xmlhttp.url=aUri;
  301.         xmlhttp.send('');
  302.     },
  303.  
  304.  
  305.     myCallBackFunc: function(event, hostName, myURL) {
  306.  
  307.         var httpRequest = event.target;
  308.         var response;
  309.  
  310.         //Make sure we received a valid response
  311.         switch(httpRequest.readyState) {
  312.         case 1,2,3:
  313.             debug('Bad Ready State: '+ httpRequest.status);
  314.             this.setCustomAccountIcons(myURL);
  315.             return false;
  316.             break;
  317.         case 4:
  318.             if(httpRequest.status !=200) {
  319.                 debug('The server respond with a bad status code: '+httpRequest.status);
  320.                 this.setCustomAccountIcons(myURL);
  321.                 return false;
  322.             } else {
  323.                 response = httpRequest.responseText;
  324.             }
  325.             break;
  326.         }
  327.  
  328.         // MERC (rpaul) string mangling.. for now doesn't appear we can get valid
  329.         // xml back from html pages... will look into this later, dom manipulations
  330.         // would be alot cleaner
  331.  
  332.         // loop through the document looking for <link tags
  333.         var currIndex = 0;
  334.         var linkTagStartPos;
  335.         var linkTagEndPos;
  336.         var linkTag;
  337.         var favicon = "";
  338.         var faviconUri = "";
  339.  
  340.         while (currIndex != -1) {
  341.             var validXmlTag = true;
  342.             linkTagStartPos = response.indexOf("<link", currIndex);
  343.             linkTagEndPos = response.indexOf("/>", linkTagStartPos);
  344.             if (linkTagEndPos == -1) {
  345.                 linkTagEndPos = response.indexOf(">", linkTagStartPos);
  346.                 validXmlTag = false;
  347.             }
  348.             linkTag = response.substring(linkTagStartPos, linkTagEndPos+1);
  349.  
  350.             // make the extracted tag valid if needed by appending a "/>" to close the tag
  351.             if (!validXmlTag) {
  352.                 var validTag = linkTag.substring(0, linkTag.length - 1) + "/>";
  353.             }
  354.  
  355.             parser = new DOMParser();
  356.  
  357.             var xmlDocString = '<?xml version="1.0"?>\n<!DOCTYPE doc [\n<!ATTLIST d id ID #IMPLIED>\n]>\n<doc>\n  <foo xmlns="foobar">One</foo> <x:bar xmlns:x="barfoo">Two</x:bar>\n  ' + validTag + '\n</doc>\n';
  358.  
  359.               //this.debug(xmlDocString);
  360.               var doc = parser.parseFromString(xmlDocString, "text/xml");
  361.               var linkTags = doc.getElementsByTagName('link');
  362.  
  363.               // sanity check
  364.               if (linkTags.length) {
  365.                   if (linkTags[0].getAttribute("rel") == "icon" ||
  366.                       linkTags[0].getAttribute("rel") == "shortcut icon")
  367.                   {
  368.                       // found the favicon tag, so break out of loop
  369.                       favicon = linkTags[0].getAttribute("href");
  370.                       break;
  371.                   }
  372.               } else {
  373.                   break;
  374.               }
  375.  
  376.               currIndex = linkTagEndPos+1;
  377.  
  378.         } // end while
  379.  
  380.         // if we can't get the favicon from the xmlhttp request, try to get it off the root of the server
  381.         if (favicon == "") {
  382.             // check to see if <hostname>/favicon.ico exists, otherwise set the icon as
  383.             // the default netscape
  384.             xmlhttp = new XMLHttpRequest();
  385.             xmlhttp.open("GET", hostName + "/favicon.ico",true);
  386.             xmlhttp.onreadystatechange=function() {
  387.                   if (xmlhttp.readyState==4) {
  388.                       faviconUri = hostName + "/favicon.ico";
  389.                   } else {
  390.                       faviconUri = "chrome://browser/skin/icons/mail_sm.png";
  391.                   }
  392.             }
  393.             xmlhttp.send(null)
  394.         } else {
  395.             faviconUri = hostName + "/" + favicon;
  396.         }
  397.  
  398.         // Iterate through the custom accounts array and set favicons for matching urls
  399.         this.setCustomAccountIcons(myURL, faviconUri);
  400.         
  401.     },
  402.  
  403.     getEmailAddress: function (accountName, defaultService, defaultProvider) {
  404.  
  405.         if (defaultProvider != "webmail:other") {
  406.             var providerParts = defaultProvider.split(':');
  407.             var domain = providerParts[providerParts.length-1];
  408.         } else {
  409.             // for other webmail accounts set the tooltip text to "webmail"
  410.             // since a username is not provided
  411.             return "Webmail";
  412.         }
  413.  
  414.         var myUserid;
  415.         var myPassword;
  416.         var mKey="webmail:"+accountName;
  417.         var pc = GetWebmailPasscard(defaultService.passcarddomain, mKey);
  418.         if (pc) {
  419.             webmail.debug('Found a passcard for this account, un/pw = '+pc.user+'/'+pc.password);
  420.             myUserid = pc.user;
  421.             myPassword = pc.password;
  422.         } else {
  423.             webmail.debug('\nWEBMAIL ERROR: Did not find passcard for mKey = '+mKey+'\n');
  424.             return "Webmail";
  425.         }
  426.  
  427.         // couldn't get a valid key entry
  428.         if (!myUserid && !myPassword) {
  429.             webmail.debug("userID or Password not found, but needed");
  430.             return "Webmail";
  431.         }
  432.         // if the sign in name is the email address, return it
  433.         if (defaultService.username == "Email Address" || defaultService.username == "E-mail Address") {
  434.             return myUserid;
  435.         }
  436.  
  437.         //otherwise construct the email address
  438.         // provider is split up by ':' we can get the domain for the account here
  439.         var emailAddress = myUserid + '@' + domain + '.com';
  440.  
  441.         return emailAddress;
  442.     },
  443.  
  444.  
  445.     fixMenus : function () {
  446.         this.debug('fixMenus()');
  447.         // Initialize the webmail menu and create any necessary mini-webmail menus
  448.         // look for default account
  449.         this.dumpMenu("webmail-menu");
  450.         var defaultWebmail = null;
  451.         var defaultProvider = null;
  452.         var checkUpdates = null;
  453.         var numCustomAccounts = 0;
  454.  
  455.         if (this.Prefs.prefHasUserValue("default") && 
  456.            (this.Prefs.getPrefType("default") == this.Prefs.PREF_STRING)) {
  457.             defaultWebmail = this.Prefs.getCharPref("default");
  458.         } else {
  459.             this.debug("No Default Webmail provider set");
  460.         }
  461.  
  462.         // MERC - JCH: Do processing of default account if it exists. If it
  463.         // doesn't exist, don't prevent adding other webmail accounts.
  464.         if (defaultWebmail && (defaultWebmail != "")) {
  465.  
  466.  
  467.             // verify that default is valid
  468.             try {
  469.                 defaultProvider = this.Prefs.getCharPref("provider."+defaultWebmail);
  470.                 this.debug("Default Provider information found");
  471.             } catch(ex) {
  472.                 this.debug("No provider listed for default account");
  473.                 return;
  474.             }
  475.  
  476.             // MERC - JCH: Set the 'global' default account name variable here
  477.             this.mDefaultAccount = defaultWebmail;
  478.  
  479.             var defaultService = this.getServiceById(defaultProvider);
  480.  
  481.  
  482.             if (defaultService) {
  483.                 try {
  484.                     // setting default service images
  485.                     document.getElementById("webmail-menu-image").setAttribute("image", defaultService.icon);
  486.                     document.getElementById("webmail-menu-image").setAttribute("tooltiptext", "Webmail");
  487.                     document.getElementById("webmail-toolbarbutton").setAttribute("tooltiptext", "Webmail");
  488.                     document.getElementById("webmail-readmenuitem").setAttribute("src", defaultService.icon);
  489.                 } catch (ex) {
  490.                     this.debug('Unable to set tooltips and icon srcs');
  491.                 }
  492.             } 
  493.  
  494.             // modify top of webmail menu as per prefs
  495.             // set the name
  496.             // set the inbox
  497.             // set the write
  498.             // set the addressbook
  499.             try {
  500.                 checkUpdates=this.Prefs.getCharPref("checkUpdates."+defaultWebmail);
  501.             } catch (ex) {
  502.                 checkUpdates="false";
  503.             }
  504.             if (document.getElementById('webmail-button')) {
  505.                 //document.getElementById("webmail-menu-label").setAttribute("webmailAccel",defaultWebmail);
  506.                 document.getElementById("webmail-readmenuitem").setAttribute("webmailAccel",defaultWebmail);
  507.                 document.getElementById("webmail-toolbarbutton").setAttribute("webmailAccel",defaultWebmail);
  508.                 if (checkUpdates && checkUpdates=="true") {
  509.                       this.debug("submitting checkNewWebmail("+defaultWebmail+")");
  510.                       setTimeout("checkNewWebmail('"+defaultWebmail+"');",5000);
  511.                       this.debug("returned.");
  512.                   }
  513.                 document.getElementById("webmail-readmenuitem").removeAttribute("disabled");
  514.                 document.getElementById("webmail-readmenuitem").label="Read "+defaultWebmail;
  515.                 document.getElementById("webmail-readmenuitem").setAttribute("oncommand","webmail.read('"+defaultWebmail+"');");
  516.  
  517.                  if (defaultService && defaultService.writeurl.length > 0)
  518.                  {
  519.                      document.getElementById("webmail-writemenuitem").setAttribute("oncommand","webmail.write('"+defaultWebmail+"');");
  520.                      document.getElementById("webmail-writemenuitem").removeAttribute("disabled");
  521.                      document.getElementById("webmail-writemenuitem").removeAttribute("hidden");
  522.                  } else {
  523.                      document.getElementById("webmail-writemenuitem").setAttribute("hidden", "true");             
  524.                  }
  525.                  
  526.                  if (defaultService && defaultService.addressurl.length > 0)
  527.                  {
  528.                      document.getElementById("webmail-addressmenuitem").setAttribute("oncommand","webmail.address('"+defaultWebmail+"');");
  529.                      document.getElementById("webmail-addressmenuitem").removeAttribute("disabled");
  530.                      document.getElementById("webmail-addressmenuitem").removeAttribute("hidden");         
  531.                  } else {
  532.                      document.getElementById("webmail-addressmenuitem").setAttribute("hidden", "true");
  533.                  }
  534.             }
  535.         } else if (document.getElementById('webmail-button')) {
  536.             // MERC - JCH: If no default account defined then set global var to null.
  537.             this.mDefaultAccount = null;
  538.             document.getElementById("webmail-menu-image").setAttribute("image","chrome://browser/skin/icons/mail_sm.png");
  539.             document.getElementById("webmail-readmenuitem").label="Read E-mail";
  540.             document.getElementById("webmail-readmenuitem").setAttribute("disabled", "true");
  541.             document.getElementById("webmail-readmenuitem").removeAttribute("src");
  542.             document.getElementById("webmail-writemenuitem").label="Write New E-Mail";
  543.             document.getElementById("webmail-writemenuitem").setAttribute("disabled", "true");
  544.             document.getElementById("webmail-addressmenuitem").label="Address Book";
  545.             document.getElementById("webmail-addressmenuitem").setAttribute("disabled", "true");
  546.         } // End of default account processing.
  547.  
  548.         // get list of all accounts
  549.         var childcount = new Object;
  550.         try {
  551.             var myprefs=this.Prefs.getChildList("provider.",childcount);
  552.         } catch (ex) {
  553.             myprefs=new Array();
  554.         }
  555.         myprefs.sort();
  556.         var i;
  557.         var accountName;
  558.         var first=true;
  559.         var newpopup;
  560.         var newitem;
  561.         var newmenu;
  562.         var temp;
  563.         var nextMenu;
  564.         var myService;
  565.         var addrMenu = document.getElementById("webmail-addressmenuitem");
  566.         var curMenu;
  567.         if (addrMenu) {
  568.             curMenu = document.getElementById("webmail-addressmenuitem").nextSibling;
  569.             while (curMenu && curMenu.id != "webmail-menu-mainsep") {
  570.                 nextMenu=curMenu.nextSibling;
  571.                 try {
  572.                     this.debug("removing child: ("+curMenu.id+") "+curMenu.label);
  573.                     delete document.getElementById("webmail-menu").removeChild(curMenu);
  574.                 } catch (ex) {
  575.                     this.debug("Error removing child:\n"+ex);
  576.                 }
  577.                 curMenu = nextMenu;
  578.             }
  579.         }
  580.         if (curMenu) {
  581.             this.debug("curMenu.id ="+curMenu.id);
  582.         }
  583.         this.debug("Finished deleting, starting rebuild");
  584.         this.dumpMenu("webmail-menu");
  585.  
  586.         for (i=0; i<myprefs.length; i++) {
  587.             this.debug("webmail pref ("+myprefs[i]+") found");
  588.             // ignore default account
  589.             accountName=myprefs[i].substr(9);
  590.             //          if (accountName == defaultWebmail) {
  591.             //              continue;
  592.             //          }
  593.             var temp=null;
  594.             try {
  595.                 temp = this.Prefs.getCharPref(myprefs[i]);
  596.             } catch (ex){}
  597.             if (!temp || temp == "") continue;
  598.  
  599.             this.debug("making menu item, provider="+temp);
  600.             myService=this.getServiceById(temp);
  601.             // build up sub-menu for each account
  602.             if (first){
  603.                 var newsep = document.createElement("menuseparator");
  604.                 newsep.setAttribute("id","webmail-menu-altsep");
  605.                 var webmailMenu = document.getElementById("webmail-menu");
  606.                 if (webmailMenu)
  607.                     webmailMenu.insertBefore(newsep,document.getElementById("webmail-menu-mainsep"));
  608.                 first = false;
  609.             }
  610.             newpopup=document.createElement("menupopup");
  611.             newpopup.setAttribute("label","mpop "+accountName);
  612.             
  613.             newitem = document.createElement("menuitem");
  614.             newitem.setAttribute("label","Read "+accountName);
  615.             newitem.setAttribute("tooltiptext","Go to inbox for "+accountName);
  616.             newitem.setAttribute("webmailAccel",accountName);
  617.             newitem.setAttribute("oncommand","webmail.read('"+accountName+"');");
  618.             newpopup.appendChild(newitem);
  619.             
  620.  
  621.             if (myService && (myService.writeurl.length > 0)) 
  622.             {
  623.                 newitem = document.createElement("menuitem");
  624.                 newitem.setAttribute("label","New Email");
  625.                 newitem.setAttribute("tooltiptext","Create a new Email using "+accountName);
  626.                 newitem.setAttribute("oncommand","webmail.write('"+accountName+"');");
  627.                 newpopup.appendChild(newitem);
  628.             } 
  629.             // MERC - JCH : If the account is a custom account and it has a write url defined. 
  630.             else if (temp == "webmail:other")
  631.             {
  632.                 if (this.Prefs.prefHasUserValue("WriteURL."+accountName) && 
  633.                    (this.Prefs.getPrefType("WriteURL."+accountName) == this.Prefs.PREF_STRING))
  634.                 {
  635.                     newitem = document.createElement("menuitem");
  636.                     newitem.setAttribute("label","New Email");
  637.                     newitem.setAttribute("tooltiptext","Create a new Email using "+accountName);
  638.                     newitem.setAttribute("oncommand","webmail.write('"+accountName+"');");
  639.                     newpopup.appendChild(newitem);
  640.                 }
  641.             }
  642.     
  643.             if (myService && myService.addressurl.length > 0)
  644.             {
  645.                 newitem = document.createElement("menuitem");
  646.                 newitem.setAttribute("tooltiptext","Go to the address book for "+accountName);
  647.                 newitem.setAttribute("label","Address Book");
  648.                 newitem.setAttribute("oncommand","webmail.address('"+accountName+"');");
  649.                 newpopup.appendChild(newitem);
  650.             }
  651.             // MERC - JCH : If the account is a custom account and it has an address book url defined. 
  652.             else if (temp == "webmail:other")
  653.             {
  654.                 if (this.Prefs.prefHasUserValue("AddressURL."+accountName) && 
  655.                    (this.Prefs.getPrefType("AddressURL."+accountName) == this.Prefs.PREF_STRING))
  656.                 {
  657.                     newitem = document.createElement("menuitem");
  658.                     newitem.setAttribute("tooltiptext","Go to the address book for "+accountName);
  659.                     newitem.setAttribute("label","Address Book");
  660.                     newitem.setAttribute("oncommand","webmail.address('"+accountName+"');");
  661.                     newpopup.appendChild(newitem);
  662.                 }
  663.             }
  664.  
  665.             newmenu = document.createElement("menu");
  666.             newmenu.appendChild(newpopup);
  667.             
  668.             newmenu.setAttribute("label",accountName);
  669.             newmenu.setAttribute("tooltiptext",accountName+" webmail account");
  670.             if (defaultWebmail && accountName == defaultWebmail) {
  671.                 newmenu.setAttribute("style","font-weight:bold;");
  672.             }
  673.             newmenu.setAttribute("webmailAccel",accountName);
  674.  
  675.             if (myService) {
  676.                   newmenu.setAttribute("image",myService.icon);
  677.                   newmenu.setAttribute("class","menu-iconic");
  678.             } else {
  679.                 // Set a custom account id using its unique name
  680.                 newmenu.setAttribute("id", accountName);
  681.             }
  682.  
  683.             var webmailMenu = document.getElementById("webmail-menu");
  684.             if (webmailMenu)
  685.                 webmailMenu.insertBefore(newmenu,document.getElementById("webmail-menu-mainsep"));
  686.             //create mini-webmail menu if needed.
  687.             try {
  688.                 // MERC - JCH: Display webmail account as a separate button if pref set
  689.                 this.displayAccountButton(accountName);
  690.             } catch (ex) {
  691.                 this.debug("Display Separately Failed");
  692.                 this.debug(ex);
  693.             }
  694.               try {
  695.                   checkUpdates=this.Prefs.getCharPref("checkUpdates."+accountName);
  696.               } catch (ex) {
  697.                   checkUpdates="false";
  698.               }
  699.             if (checkUpdates && checkUpdates=="true") {
  700.                   this.debug("submitting checkNewWebmail("+accountName+")");
  701.                   setTimeout("checkNewWebmail('"+accountName+"');",5000);
  702.             }
  703.         }
  704.  
  705.         try {
  706.             this.dumpMenu("webmail-menu");
  707.             // MERC (rpaul) if the user creates a webmail account, then set the label to "Webmail"
  708.             document.getElementById('webmail-toolbarbutton').setAttribute('label', 'Webmail');
  709.         } catch (ex) { }
  710.     },
  711.  
  712.     // MERC - JCH: Display webmail account as a separate button if 'dispSep' pref set to 'true' and 
  713.     // button doesn't exist.
  714.     displayAccountButton : function(accountName) {
  715.  
  716.         // Construct the pref name
  717.         var prefName = "dispSep."+accountName;
  718.         if (this.Prefs.prefHasUserValue(prefName) && 
  719.            (this.Prefs.getPrefType(prefName) == this.Prefs.PREF_STRING)) {
  720.  
  721.             // Get the pref value for the display separately property
  722.             var dispSep = this.Prefs.getCharPref(prefName);
  723.             if (dispSep == "true") {
  724.                 // Determine where to place the button IF it is okay to add it to 
  725.                 // the toolbar...it might be on the palette
  726.                 var webmailBtn = document.getElementById('webmail-button');
  727.                 if (webmailBtn) {
  728.                     var toolbar = webmailBtn.parentNode;
  729.                     while ((toolbar) &&
  730.                            (toolbar.localName != 'toolbar') &&
  731.                            (toolbar.localName != 'multibartray'))
  732.                     {
  733.                         toolbar = toolbar.parentNode;
  734.                     }
  735.                 }
  736.                 
  737.                 if (!toolbar) {
  738.                     throw "No toolbar found for webmail-button";
  739.                 }
  740.                 
  741.                 var newId = 'webmail-toolbaritem-'+accountName;
  742.  
  743.                 // Check if button is in the currentSet for the toolbar.
  744.                 // This will prevent removing it from the palette if it's there.
  745.                 var currentSet = toolbar.currentSet.split(',');
  746.                 var found = false;
  747.                 for (var i = 0; i < currentSet.length; i++) {
  748.                     if (currentSet[i] == newId) {
  749.                         found = true;
  750.                         break;
  751.                     }
  752.                 }
  753.  
  754.                 // Button is in the current set, so don't add it
  755.                 if (found)
  756.                     return;
  757.  
  758.                 // If it's not in the current set, add it
  759.                 found = false;
  760.                 for (var i = 0; i < currentSet.length; i++) {
  761.                     if (currentSet[i] == 'webmail-button') {
  762.                         currentSet[i] = 'webmail-button,'+newId;
  763.                         found = true;
  764.                         break;
  765.                     }
  766.                 }
  767.                 if (!found) {
  768.                     dump(' - adding item to END of toolbar\n');
  769.                     var lastItem = currentSet[currentSet.length-1];
  770.                     currentSet[currentSet.length] = lastItem+','+newId;
  771.                 }
  772.                 var newCurrentSet = currentSet.join(',');
  773.                 //toolbar.currentSetAtLoadTime = newCurrentSet;
  774.  
  775.                 // If the button has been added already, call returns true
  776.                 var alreadyAddedButton = this.addEmailButton(accountName);
  777.                 if (webmailBtn.nextSibling && !alreadyAddedButton) {
  778.                     toolbar.insertItem(newId, webmailBtn.nextSibling, null, false);
  779.                     toolbar.currentSetAtLoadTime = toolbar.currentSet;
  780.                 }
  781.             }
  782.         }
  783.     },
  784.  
  785.     openManage : function (accountName) {
  786.         this.debug("opening dialog, accountName="+accountName);
  787.         if (accountName) {
  788.             window.openDialog('chrome://browser/content/manageEmailDialog.xul',
  789.             'manageEmailDialog','modal=yes',accountName);
  790.         } else {
  791.             window.openDialog('chrome://browser/content/manageEmailDialog.xul',
  792.             'manageEmailDialog','modal=yes');
  793.         }
  794.         this.debug("done dialog, accountName="+accountName);
  795.     },
  796.  
  797.     /**
  798.      * Parse the description for a particular resource
  799.      **/
  800.     HandleResource : function(datasource, res) {
  801.         // See if this resource specifies an actual webmail component
  802.         //this.debug("Attempting to handle resource: "+res.Value);
  803.  
  804.         if (datasource.hasArcOut(res, webmail.WEBMAIL_RES_NAME) &&
  805.             datasource.hasArcOut(res, this.WEBMAIL_RES_INBOX) &&
  806.             datasource.hasArcOut(res, this.WEBMAIL_RES_WRITE) &&
  807.             datasource.hasArcOut(res, this.WEBMAIL_RES_ADDRESS) &&
  808.             datasource.hasArcOut(res, this.WEBMAIL_RES_NOTIFY))
  809.         {
  810.             var new_service = this.services.length;
  811.             this.services[new_service] = new Object();
  812.             this.debug("new service is #"+new_service);
  813.             var info = datasource.GetTarget(res, this.WEBMAIL_RES_NAME, true);
  814.             info.QueryInterface(Components.interfaces.nsIRDFLiteral);
  815.             this.services[new_service].name = new String(info.Value);
  816.             info = datasource.GetTarget(res, this.WEBMAIL_RES_INBOX, true);
  817.             info.QueryInterface(Components.interfaces.nsIRDFLiteral);
  818.             this.services[new_service].inboxurl = new String(info.Value);
  819.             if (datasource.hasArcOut(res, this.WEBMAIL_RES_PCDOMAIN)) {
  820.                 info = datasource.GetTarget(res, this.WEBMAIL_RES_PCDOMAIN, true);
  821.                 info.QueryInterface(Components.interfaces.nsIRDFLiteral);
  822.                 this.services[new_service].passcarddomain = new String(info.Value);
  823.             } else {
  824.                 var host = this.services[new_service].inboxurl;
  825.                 host = host.replace(/.*:\/\/(.*)/,"$1"); // Remove http:// = mail.yahoo.com/help/test.php?test=yes
  826.                 if (host.indexOf("/") > 0)
  827.                     host = host.substring(0,host.indexOf("/")); // Remove /help/test.php?test=yes = mail.yahoo.com
  828.                 var domain = host.replace(/.*\.+(\w+\.\w+)$/,"$1"); // Remove mail. = yahoo.com
  829.                 this.services[new_service].passcarddomain = new String(domain);
  830.             }
  831.             if (datasource.hasArcOut(res, this.WEBMAIL_RES_PCUSERNAME)) {
  832.                 info = datasource.GetTarget(res, this.WEBMAIL_RES_PCUSERNAME, true);
  833.                 info.QueryInterface(Components.interfaces.nsIRDFLiteral);
  834.                 this.services[new_service].passcardusername = new String(info.Value);
  835.             }
  836.             if (datasource.hasArcOut(res, this.WEBMAIL_RES_PCPASSWORD)) {
  837.                 info = datasource.GetTarget(res, this.WEBMAIL_RES_PCPASSWORD, true);
  838.                 info.QueryInterface(Components.interfaces.nsIRDFLiteral);
  839.                 this.services[new_service].passcardpassword = new String(info.Value);
  840.             }
  841.             info = datasource.GetTarget(res, this.WEBMAIL_RES_WRITE, true);
  842.             info.QueryInterface(Components.interfaces.nsIRDFLiteral);
  843.             this.services[new_service].writeurl = new String(info.Value);
  844.             info = datasource.GetTarget(res, this.WEBMAIL_RES_ADDRESS, true);
  845.             info.QueryInterface(Components.interfaces.nsIRDFLiteral);
  846.             this.services[new_service].addressurl = new String(info.Value);
  847.             info = datasource.GetTarget(res, this.WEBMAIL_RES_NOTIFY, true);
  848.             info.QueryInterface(Components.interfaces.nsIRDFLiteral);
  849.             this.services[new_service].notify = new String(info.Value);
  850.             this.services[new_service].id = new String(res.Value);
  851.  
  852.             if (datasource.hasArcOut(res,this.WEBMAIL_RES_USERNAME)) {
  853.                 info = datasource.GetTarget(res, this.WEBMAIL_RES_USERNAME, true);
  854.                 info.QueryInterface(Components.interfaces.nsIRDFLiteral);
  855.                 this.services[new_service].username = new String(info.Value);
  856.             } else this.services[new_service].username = new String("User Name");
  857.  
  858.             if (datasource.hasArcOut(res,this.WEBMAIL_RES_ICON)) {
  859.                 info = datasource.GetTarget(res, this.WEBMAIL_RES_ICON, true);
  860.                 info.QueryInterface(Components.interfaces.nsIRDFLiteral);
  861.                 this.services[new_service].icon = new String(info.Value);
  862.             } else this.services[new_service].icon = new String("chrome://browser/skin/icons/mail_sm.png");
  863.  
  864.             if (datasource.hasArcOut(res,this.WEBMAIL_RES_AUTOLOGURL)) {
  865.                 info = datasource.GetTarget(res, this.WEBMAIL_RES_AUTOLOGURL, true);
  866.                 info.QueryInterface(Components.interfaces.nsIRDFLiteral);
  867.                 this.services[new_service].autologurl = new String(info.Value);
  868.                 this.services[new_service].autologurl = this.services[new_service].autologurl.replace(/;/g,"&");
  869.             } else this.services[new_service].autologurl = this.services[new_service].inboxurl;
  870.  
  871.             if (datasource.hasArcOut(res,this.WEBMAIL_RES_NEEDLOGIN)) {
  872.                 info = datasource.GetTarget(res, this.WEBMAIL_RES_NEEDLOGIN, true);
  873.                 info.QueryInterface(Components.interfaces.nsIRDFLiteral);
  874.                 this.services[new_service].needlogin = new String(info.Value);
  875.             } else this.services[new_service].needlogin = null;
  876.  
  877.             if (datasource.hasArcOut(res,this.WEBMAIL_RES_URLCAPTURE)) {
  878.                 info = datasource.GetTarget(res, this.WEBMAIL_RES_URLCAPTURE, true);
  879.                 info.QueryInterface(Components.interfaces.nsIRDFLiteral);
  880.                 this.services[new_service].urlcapture = new String(info.Value);
  881.             } else this.services[new_service].urlcapture = null;
  882.  
  883.             if (datasource.hasArcOut(res,this.WEBMAIL_RES_DEFAULTURL)) {
  884.                 info = datasource.GetTarget(res, this.WEBMAIL_RES_DEFAULTURL, true);
  885.                 info.QueryInterface(Components.interfaces.nsIRDFLiteral);
  886.                 this.services[new_service].defaulturl = new String(info.Value);
  887.             } else this.services[new_service].defaulturl = null;
  888.  
  889.             if (datasource.hasArcOut(res,this.WEBMAIL_RES_COMPOSEFIELDS)) {
  890.                 info = datasource.GetTarget(res, this.WEBMAIL_RES_COMPOSEFIELDS, true);
  891.                 info.QueryInterface(Components.interfaces.nsIRDFLiteral);
  892.                 this.services[new_service].composefields = new String(info.Value);
  893.             } else this.services[new_service].composefields = null;
  894.  
  895.             this.debug("Webmail service, "+res.Value+", added to list with usernametext="+this.services[new_service].username);
  896.             this.debug("   icon="+this.services[new_service].icon);
  897.             this.debug("   defaulturl="+this.services[new_service].defaulturl);
  898.             this.debug("   urlcapture="+this.services[new_service].urlcapture);
  899.             this.debug("   needlogin="+this.services[new_service].needlogin);
  900.             this.debug("   composefields="+this.services[new_service].composefields);
  901.         }
  902.     },
  903.  
  904.  
  905.     debug : function(msg) {
  906.         if (gPrefService.getPrefType("webmail.debug") && gPrefService.getBoolPref("webmail.debug"))
  907.             dump('WEBMAIL: '+msg+'\n');
  908.     },
  909.  
  910.  
  911.     addEmailButton : function(accountName) {
  912.         this.debug('addEmailButton('+accountName+')');
  913.  
  914.         var mItem;
  915.         var mPopup;
  916.         var tbButton;
  917.         var tbItem=null;
  918.         var stack;
  919.         var vbox;
  920.         var previewBox;
  921.         var draggableBox;
  922.         var myID="webmail-toolbaritem-"+accountName;
  923.         var myService;
  924.         var myProvider;
  925.         var mSpacer;
  926.         var spacer2,spacer3,hbox,vbox2;
  927.         var toolbox = document.getElementById('navigator-toolbox');
  928.         var palette = toolbox.palette;
  929.  
  930.         // Check if button already exists - if it does, assume that it is OK
  931.         try {
  932.             tbItem=document.getElementById(myID);
  933.         } catch (ex) {}
  934.  
  935.         if (tbItem) {
  936.             return true;
  937.         }
  938.  
  939.         // MERC - JCH: Check if account is already in palette so as not to create duplicates
  940.         var isItem = palette.getElementsByAttribute("id", myID);
  941.  
  942.         if (isItem.length > 0)
  943.             return true;
  944.  
  945.         try {
  946.             myProvider=this.Prefs.getCharPref("provider."+accountName);
  947.         } catch (ex) {}
  948.         if (myProvider && myProvider != "") {
  949.             myService=this.getServiceById(myProvider);
  950.         }
  951.  
  952.         // Construction of the menupopup
  953.         mPopup=document.createElement("menupopup");
  954.         mItem=document.createElement("menuitem");
  955.         mItem.setAttribute("label","Read "+accountName);
  956.         if (myService) {
  957.             mItem.setAttribute("image",myService.icon);
  958.             mItem.setAttribute("class","menuitem-iconic");
  959.         }
  960.         mItem.setAttribute("webmailAccel",accountName);
  961.         mItem.setAttribute("tooltiptext","Go to inbox for "+accountName);
  962.         mItem.setAttribute("oncommand","webmail.read('"+accountName+"');");
  963.         mPopup.appendChild(mItem);
  964.         
  965.         // JMC - If we don't have a valid compose page, don't show the button
  966.         if (myService && myService.writeurl.length > 0)
  967.         {
  968.             mItem=document.createElement("menuitem");
  969.             mItem.setAttribute("label","Write Mail");
  970.             mItem.setAttribute("tooltiptext","Create a new Email using "+accountName);
  971.             mItem.setAttribute("oncommand","webmail.write('"+accountName+"');");
  972.             mPopup.appendChild(mItem);
  973.         }
  974.         
  975.         // JMC - If we don't have a valid addressbook page, don't show the button
  976.         if (myService && myService.addressurl.length > 0)
  977.         {
  978.             mItem=document.createElement("menuitem");
  979.             mItem.setAttribute("label","Address Book");
  980.             mItem.setAttribute("tooltiptext","Go to the address book for "+accountName);
  981.             mItem.setAttribute("oncommand","webmail.address('"+accountName+"');");
  982.             mPopup.appendChild(mItem);
  983.         }
  984.         
  985.         mItem=document.createElement("menuseparator");
  986.         mPopup.appendChild(mItem);
  987.         mItem=document.createElement("menuitem");
  988.         mItem.setAttribute("label","Manage Email Account...");
  989.         mItem.setAttribute("tooltiptext","Manage "+accountName+" webmail account information");
  990.         mItem.setAttribute("oncommand","this.parentNode.hidePopup();webmail.openManage('"+accountName+"');");
  991.         mPopup.appendChild(mItem);
  992.         mPopup.setAttribute("position","at_pointer");
  993.         mPopup.setAttribute("label","NewEmailButton");
  994.         mPopup.setAttribute("id","webmail-menu-"+accountName);
  995.  
  996.         var popupSet = document.createElement("popupset");
  997.         popupSet.appendChild(mPopup);
  998.  
  999.         // construct fancy looking button
  1000.         var imageLeft=document.createElement("image");
  1001.         imageLeft.setAttribute("id","webmail-button-left-edge");
  1002.         spacer=document.createElement("spacer");
  1003.         spacer.setAttribute("id","webmail-button-slice");
  1004.         spacer.setAttribute("flex","1");
  1005.         var imageRight=document.createElement("image");
  1006.         imageRight.setAttribute("id","webmail-button-right-edge");
  1007.         hbox=document.createElement("hbox");
  1008.         hbox.appendChild(imageLeft);
  1009.         hbox.appendChild(spacer);
  1010.         hbox.appendChild(imageRight);
  1011.         hbox.setAttribute("align","center");
  1012.         hbox.setAttribute("pack","end");
  1013.         hbox.setAttribute("style","width: 80px;");
  1014.         hbox.setAttribute("id","webmailClickStyleTarget-"+accountName);
  1015.         var clickStyleTarget = hbox;
  1016.  
  1017.         spacer=document.createElement("spacer");
  1018.         spacer.setAttribute("style","height: 1px;");
  1019.         vbox=document.createElement("vbox");
  1020.         vbox.appendChild(spacer);
  1021.         //vbox.appendChild(hbox);
  1022.  
  1023.         var box=document.createElement("box");
  1024.         box.setAttribute("flex","1");
  1025.         box.setAttribute("id","webmailPopupTarget-"+accountName);
  1026.         box.setAttribute("style","margin-top: 1px;");
  1027.         hbox=document.createElement("hbox");
  1028.         hbox.appendChild(box);
  1029.         hbox.setAttribute("style","width: 80px;");
  1030.         hbox.setAttribute("align","stretch");
  1031.  
  1032.         spacer=document.createElement("spacer");
  1033.         spacer.setAttribute("flex","1");
  1034.         spacer.setAttribute("style","min-width: 6px;");
  1035.         image2=document.createElement("image");
  1036.         if (myService) {
  1037.             image2.setAttribute("src",myService.icon);
  1038.         } else {
  1039.             image2.setAttribute("src","chrome://browser/skin/icons/mail_sm.png");
  1040.         }
  1041.         image2.setAttribute("left","0");
  1042.         image2.setAttribute("top","0");
  1043.         image2.setAttribute("class","toolbarbutton-icon rolloverFlash");
  1044.         var imageButton = document.createElement('toolbarbutton');
  1045.         imageButton.setAttribute('image','chrome://browser/skin/icons/mail_sm.png');
  1046.         imageButton.setAttribute('tooltiptext','Webmail');
  1047.         imageButton.setAttribute('id','webmail-menu-image-'+accountName);
  1048.         spacer2=document.createElement("spacer");
  1049.         spacer2.setAttribute("flex","1");
  1050.  
  1051.         //construction of the toolbarbutton (visible)
  1052.         tbButton=document.createElement("toolbarbutton");
  1053. //        tbButton.setAttribute("type","menu-button");
  1054.         tbButton.setAttribute("oncommand","if (event.target==this) webmail.read('"+accountName+"');");
  1055.  
  1056.         tbButton.setAttribute("tooltiptext", this.getEmailAddress(accountName, myService, myProvider))
  1057.         tbButton.setAttribute("id","webmail-tb-button-"+accountName);
  1058.         tbButton.setAttribute("allowtransparency","true");
  1059.         tbButton.setAttribute("class","webmail-toolbarbutton");
  1060.         tbButton.setAttribute("allowTransparency","true");
  1061.  
  1062.         // MERC - JCH: Limit button label length to prevent button from growing too large.
  1063.         var shortAcctName = null;
  1064.         if (accountName.length > 7) {
  1065.             shortAcctName = accountName.substr(0,7)+"...";
  1066.             tbButton.setAttribute("label", shortAcctName);
  1067.         } else {
  1068.             shortAcctName = accountName;
  1069.             tbButton.setAttribute("label", accountName);
  1070.         }
  1071.  
  1072.         tbButton.setAttribute("baselabel", shortAcctName);
  1073.         tbButton.setAttribute("style","min-width: 60px !important;");
  1074.  
  1075.         // Make the button have acceltext when webmail checking is done.
  1076.         tbButton.setAttribute("webmailAccel",accountName);
  1077.         //tbButton.appendChild(mPopup);
  1078.         spacer3=document.createElement("spacer");
  1079.         spacer3.setAttribute("flex","1");
  1080.         spacer3.setAttribute("style","min-width: 4px !important;");
  1081.         var dropmarker = document.createElement('dropmarker');
  1082.         dropmarker.setAttribute('id','webmail-dropmarker-'+accountName);
  1083.         dropmarker.setAttribute('class','webmail-dropmarker');
  1084.         dropmarker.setAttribute('align','center');
  1085.         imageRight=document.createElement("image");
  1086.         //imageRight.setAttribute("style","list-style-image: inherit; margin-right: 6px;");
  1087.         dropmarker.setAttribute("onmousedown","document.getElementById('webmailClickStyleTarget-"+accountName+
  1088.             "').setAttribute('clickDown','true');");
  1089.         dropmarker.setAttribute("onmouseup","document.getElementById('webmailClickStyleTarget-"+accountName+
  1090.             "').removeAttribute('clickDown');");
  1091.         dropmarker.setAttribute("onmouseout","document.getElementById('webmailClickStyleTarget-"+accountName+
  1092.             "').removeAttribute('clickDown');");
  1093.  
  1094.         dropmarker.setAttribute("onclick","document.getElementById('webmail-menu-"+accountName+
  1095.                                 "').showPopup(document.getElementById('webmailPopupTarget-"+accountName+
  1096.                                 "'),-1,-1,'popup','bottomleft','bottomleft');");
  1097.  
  1098.         dropmarker.appendChild(imageRight);
  1099.         var hbox2=document.createElement("hbox");
  1100.         hbox2.appendChild(spacer);
  1101.         hbox2.appendChild(imageButton);
  1102.         hbox2.appendChild(spacer2);
  1103.         hbox2.appendChild(tbButton);
  1104.         hbox2.appendChild(spacer3);
  1105.         hbox2.appendChild(dropmarker);
  1106.         hbox2.setAttribute("tooltip",accountName+" Webmail Account");
  1107.         hbox2.setAttribute("align","center");
  1108.         hbox2.setAttribute("pack","end");
  1109.  
  1110.         var spacer4 = document.createElement("spacer");
  1111.         spacer4.setAttribute("style","height: 1px;");
  1112.         stack=document.createElement("stack");
  1113.         stack.appendChild(spacer4);
  1114.         stack.appendChild(clickStyleTarget);
  1115.         stack.appendChild(hbox);
  1116.         stack.appendChild(hbox2);
  1117.         stack.setAttribute("align","center");
  1118.         stack.setAttribute("pack","center");
  1119.         stack.setAttribute("id","webmail-button-container-"+accountName);
  1120.         stack.setAttribute("style","width: 80px;");
  1121.  
  1122.         spacer=document.createElement("spacer");
  1123.         spacer.setAttribute("style","width:4px !important");
  1124.         spacer2=document.createElement("spacer");
  1125.         spacer2.setAttribute("style","width:4px !important");
  1126.  
  1127.         hbox=document.createElement("hbox");
  1128.         hbox.appendChild(spacer);
  1129.         hbox.appendChild(stack);
  1130.         hbox.appendChild(spacer2);
  1131.  
  1132.         spacer=document.createElement("spacer");
  1133.         spacer.setAttribute("style","min-width:2px !important");
  1134.         spacer.setAttribute("flex","1");
  1135.         spacer2=document.createElement("spacer");
  1136.         spacer2.setAttribute("style","min-width:2px !important");
  1137.         spacer2.setAttribute("flex","1");
  1138.         spacer2.setAttribute("id","webmailPopupTarget-"+accountName);
  1139.  
  1140.         vbox=document.createElement("vbox");
  1141.         vbox.appendChild(spacer);
  1142.         vbox.appendChild(hbox);
  1143.         vbox.appendChild(spacer2);
  1144.         vbox.setAttribute("class","webmail-content");
  1145.  
  1146.         // construction of preview toolbarbutton
  1147.         previewBox=document.createElement("toolbarbutton");
  1148.         newImage=document.createElement("image");
  1149.         if (myService) {
  1150.             newImage.setAttribute("src",myService.icon);
  1151.         } else {
  1152.             newImage.setAttribute("src","chrome://browser/skin/icons/mail_sm.png");
  1153.         }
  1154.         previewBox.appendChild(newImage);
  1155.         previewBox.setAttribute("class","webmail-preview");
  1156.         previewBox.setAttribute("mousethrough","never");
  1157.         previewBox.setAttribute("label",accountName);
  1158.         previewBox.setAttribute("style","min-width: 60px !important;");
  1159.  
  1160.         // construction of draggable toolbarbutton
  1161.         draggableBox=document.createElement("toolbarbutton");
  1162.         draggableBox.setAttribute("class","webmail-draggable");
  1163.         draggableBox.setAttribute("mousethrough","never");
  1164.         draggableBox.setAttribute("label",accountName);
  1165.         draggableBox.setAttribute("style","min-width: 60px !important;");
  1166.  
  1167.         // construction of the toolbaritem
  1168.         tbItem=document.createElement("toolbaritem");
  1169.         tbItem.setAttribute("label",accountName);
  1170.         var myID="webmail-toolbaritem-"+accountName;
  1171.         tbItem.setAttribute("id",myID);
  1172.         tbItem.setAttribute("class","webmail-toolbaritem");
  1173.         tbItem.setAttribute("title",accountName);
  1174.         tbItem.setAttribute("toolgroup","personal");
  1175.         stack=document.createElement("stack");
  1176.         stack.appendChild(popupSet);
  1177.         stack.appendChild(vbox);
  1178.         stack.appendChild(previewBox);
  1179.         stack.appendChild(draggableBox);
  1180.         tbItem.appendChild(stack);
  1181.  
  1182.         var toolbar = toolbox.firstChild;
  1183.         var placed = false;
  1184.         while (toolbar) {
  1185.             if (toolbar.currentSetAtLoadTime) {
  1186.                 this.debug(' looking at toolbar: '+toolbar.id+', currentSet: '
  1187.                            +toolbar.currentSetAtLoadTime);
  1188.                 var currentSet = toolbar.currentSetAtLoadTime.split(',');
  1189.                 for (var i = 0; i < currentSet.length; i++) {
  1190.                     this.debug('  '+currentSet[i]);
  1191.                     if (currentSet[i] == myID) {
  1192.                         // Found it!
  1193.                         placed = this.placeItemRelative(tbItem, toolbar, currentSet, i,10);
  1194.                         if (!placed) {
  1195.                             // That didn't work, so just tack it on the end
  1196.                             toolbar.appendChild(tbItem);
  1197.                             this.debug('   placed at end of toolbar!');
  1198.                             placed = true;
  1199.                         }
  1200.                         break;
  1201.                     }
  1202.                 }
  1203.             }
  1204.             if (toolbar.localName == 'multibar')
  1205.                 toolbar = toolbar.firstChild;
  1206.             else if (toolbar.localName == 'multibartray' && (!toolbar.nextSibling)) {
  1207.                 toolbar = toolbar.parentNode.nextSibling;
  1208.             } else {
  1209.                 toolbar = toolbar.nextSibling;
  1210.             }
  1211.         }
  1212.         var paletteItem = tbItem;
  1213.         if (placed) {
  1214.             paletteItem = tbItem.cloneNode(true);
  1215.         }
  1216.         // Regardless of whether this component got placed on a toolbar, we still
  1217.         // need to put it in the tool palette as well, because that represents the
  1218.         // set of all available toolbar items.
  1219.         palette.appendChild(paletteItem);
  1220.         return placed;
  1221.     },
  1222.  
  1223.     removeMenus : function(accountName) {
  1224.         var myItem;
  1225.         var myItems;
  1226.  
  1227.         try {
  1228.             while (myItem=document.getElementById("webmail-toolbaritem-"+accountName)) {
  1229.                 delete myItem.parentNode.removeChild(myItem);
  1230.             }
  1231.             var toolbox = document.getElementById('navigator-toolbox');
  1232.             var palette = toolbox.palette;
  1233.             myItems=palette.getElementsByAttribute("id","webmail-toolbaritem-"+accountName);
  1234.             for (var i=0;i<myItems.length;i++) {
  1235.                 delete myItems[i].parentNode.removeChild(myItems[i]);
  1236.             }
  1237.         } catch (ex) {
  1238.             this.debug("error removing menus for: "+accountName);
  1239.             this.debug(ex);
  1240.         }
  1241.     },
  1242.  
  1243.  
  1244.     setAutoLoginPref : function (accountName, myService, autoLogFlag) {
  1245.         // Get a domain name from the mail service login URL
  1246.         var domain = myService.passcarddomain;
  1247.         if (!domain) {
  1248.             var host = myService.inboxurl.replace(/.*:\/\/(.*)/,"$1");    // Remove http://
  1249.             if (host.indexOf("/") > 0)
  1250.                 host = host.substring(0,host.indexOf("/"));            // Remove /help/test.php?test=yes = mail.yahoo.com
  1251.             var domain = host.replace(/.*\.+(\w+\.\w+)$/,"$1");        // Remove mail. = yahoo.com
  1252.             //domain = 'aol.com'; //hackety hack
  1253.         }
  1254.         this.debug(' - domain: '+domain);
  1255.  
  1256.         // Get the passcard for the mail service login site
  1257.         var webmailPasscard = GetWebmailPasscard(domain, "webmail:"+accountName);
  1258.         if (!webmailPasscard) return;
  1259.         
  1260.         var passcardFlag;
  1261.         if (autoLogFlag) 
  1262.             passcardFlag = '2';
  1263.         else
  1264.             passcardFlag = '0';
  1265.         
  1266.         // Ensure this passcard is set up properly for auto login
  1267.         //this.debug(' autologin used to be: '+webmailPasscard.autologin);
  1268.         //webmailPasscard.autologin = '2';  // fill & submit
  1269.         //this.debug(' autologin is now: '+webmailPasscard.autologin);
  1270.         var passcardMgr = Components.classes["@mozilla.org/passwordmanager;1"]
  1271.                                     .getService(Components.interfaces.nsIPasswordManager);
  1272.         passcardMgr.updateSignon(
  1273.             webmailPasscard.host, webmailPasscard.passcard, webmailPasscard.user,
  1274.             webmailPasscard.password, passcardFlag, webmailPasscard.protect,
  1275.             webmailPasscard.lastUsed, webmailPasscard.uniqueID, webmailPasscard.advancedFields);
  1276.         },
  1277.  
  1278.     autoLogin : function(accountName, myService, action, aExtra) {
  1279.         this.debug('autoLogin(accountName:'+accountName+')');
  1280.  
  1281.         
  1282.         // Open a new tab on the login URL
  1283.         this.debug(' - opening new tab...');
  1284.         var newTab = gBrowser.addTabAt("about:blank");
  1285.         this.mBrowser = gBrowser.getBrowserForTab(newTab);
  1286.         this.mBrowser.webNavigation.loadURI(
  1287.             myService.inboxurl,
  1288.             Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE,
  1289.             null, null, null);
  1290.         gBrowser.selectedTab = newTab;
  1291.     },
  1292.  
  1293.     getCaptureByAccount : function(aAccountName) {
  1294.         for (var i=0;i<this.CapturedData.length;i++) {
  1295.             if (this.CapturedData[i].account==aAccountName) {
  1296.                 return this.CapturedData[i].data;
  1297.             }
  1298.         }
  1299.         return null;
  1300.     },
  1301.  
  1302.     setCaptureData : function(aAccountName,aData) {
  1303.         for (var i=0;i<this.CapturedData.length;i++) {
  1304.             if(this.CapturedData[i].account==aAccountName) {
  1305.                 this.CapturedData[i].data = aData;
  1306.                 return;
  1307.             }
  1308.         }
  1309.         var nData=this.CapturedData.length;
  1310.         this.CapturedData[nData]=new Object();
  1311.         this.CapturedData[nData].data = new String(aData);
  1312.         this.CapturedData[nData].account = new String(aAccountName);
  1313.     },
  1314.  
  1315.     getActionURLFromPref: function(prefix, accountName) {
  1316.         var myURL;        
  1317.         if (this.Prefs.prefHasUserValue(prefix+accountName) && 
  1318.            (this.Prefs.getPrefType(prefix+accountName) == this.Prefs.PREF_STRING))
  1319.         {
  1320.             myURL = this.Prefs.getCharPref(prefix+accountName);
  1321.         } else {
  1322.             switch (prefix) {
  1323.                 case "InboxURL.":
  1324.                     myURL=this.getServiceById("webmail:feed:netscape").inboxurl;
  1325.                 case "WriteURL.":
  1326.                     myURL=this.getServiceById("webmail:feed:netscape").writeurl;
  1327.                 case "AddressURL.":
  1328.                     myURL=this.getServiceById("webmail:feed:netscape").addressurl;
  1329.                 }
  1330.         }
  1331.         return myURL;
  1332.     },
  1333.  
  1334.     doAction : function(accountName, action, aExtra) {
  1335.         
  1336.         var myURL;
  1337.         var myProvider;
  1338.         // Determine the provider
  1339.         if (this.Prefs.prefHasUserValue("provider."+accountName) && 
  1340.            (this.Prefs.getPrefType("provider."+accountName) == this.Prefs.PREF_STRING)) {
  1341.             myProvider=this.Prefs.getCharPref("provider."+accountName);
  1342.         } else {
  1343.             return;
  1344.         }
  1345.         
  1346.         var myService=this.getServiceById(myProvider);
  1347.                 
  1348.         if (this.Prefs.prefHasUserValue("autolog."+accountName) &&
  1349.            (this.Prefs.getPrefType("autolog."+accountName) == this.Prefs.PREF_STRING) && 
  1350.             this.Prefs.getCharPref("autolog."+accountName) =="true")
  1351.         {
  1352.             this.setAutoLoginPref(accountName, myService, true);
  1353.         }
  1354.         
  1355.         switch (action) {
  1356.             case "read":
  1357.                 if (!myProvider || myProvider == "webmail:other") {
  1358.                     myURL = this.getActionURLFromPref("InboxURL.", accountName);
  1359.                 } else {    
  1360.                     myURL=myService.inboxurl;        
  1361.                 }            
  1362.                 break;
  1363.                 
  1364.             case "write":
  1365.             case "mailto":
  1366.                 if (!myProvider || myProvider == "webmail:other") {
  1367.                     myURL = this.getActionURLFromPref("WriteURL.", accountName);                    
  1368.                 } else {            
  1369.                     myURL=myService.writeurl;    
  1370.                 }
  1371.                 break;
  1372.                 
  1373.             case "address":
  1374.                 if (!myProvider || myProvider == "webmail:other") {
  1375.                     myURL = this.getActionURLFromPref("AddressURL.", accountName);                    
  1376.                 } else {
  1377.                     myURL=myService.addressurl;                
  1378.                 }
  1379.                 break;
  1380.             
  1381.         }
  1382.         loadURIWithOpenPref(myURL,'browser.tabs.personaltoolbarbutton.open',null,null);
  1383.         
  1384.     },
  1385.  
  1386.     doActionOld : function(accountName,action,aExtra) {
  1387.         var myProvider;
  1388.         var myService;
  1389.         var myURL;
  1390.         this.debug("doAction ("+accountName+","+action+") called");
  1391.         var doneauto=false;
  1392.         if (action.substr(0,9)=="continue_") {
  1393.             action=action.substr(9);
  1394.             doneauto=true;
  1395.         }
  1396.         // Determine the provider
  1397.         try {
  1398.             myProvider=this.Prefs.getCharPref("provider."+accountName);
  1399.         } catch (ex) {
  1400.             this.debug("no provider for account: "+accountName);
  1401.         }
  1402.         // Check that the provider is valid
  1403.         if (myProvider) {
  1404.             myService=this.getServiceById(myProvider);
  1405.         }
  1406.         // Determine the URL to go to
  1407.         if (!myProvider ||(myProvider && myProvider == "webmail:other")) {
  1408.             var prefix;
  1409.             if (action == "read") {
  1410.                 prefix="InboxURL.";
  1411.             } else if (action == "write" || action == "mailto") {
  1412.                 prefix="WriteURL.";
  1413.             } else if (action == "address") {
  1414.                 prefix="AddressURL.";
  1415.             } else {
  1416.                 this.debug("doAction called with invalid action!!!!!");
  1417.                 return;
  1418.             }
  1419.             try {
  1420.                 myURL = this.Prefs.getCharPref(prefix+accountName);
  1421.             } catch (ex) {
  1422.                 myURL = this.getServiceById("webmail:feed:netscape").inboxurl;
  1423.             }
  1424.         } else if (myService) {
  1425.             if (action == "read") {
  1426.                 myURL=myService.inboxurl;
  1427.             } else if (action == "write" || action == "mailto") {
  1428.                 myURL=myService.writeurl;
  1429.             } else if (action == "address") {
  1430.                 myURL=myService.addressurl;
  1431.             } else {
  1432.                 this.debug("doAction called with invalid action!!!!!");
  1433.                 return;
  1434.             }
  1435.             // if this service has "urlcapture" data needed, it needs to have been
  1436.             // set for this account, otherwise the default applies.  If no url
  1437.             // capture data is needed, just use the standard read/write/address url
  1438.             if (myService.urlcapture) {
  1439.                 this.debug("There is urlcapture defined for this webmail provider.");
  1440.                 var myCapture=this.getCaptureByAccount(accountName);
  1441.                 if (myCapture) {
  1442.                     this.debug("Capture data is defined");
  1443.                     this.debug("Capture data is: "+myCapture);
  1444.                     varlist=myService.urlcapture.split(';');
  1445.                     valuelist=myCapture.split(';');
  1446.                     if (varlist.length != valuelist.length) {
  1447.                         webmail.debug("FATAL problem with urlcapture.  Reverting to defaulturl.");
  1448.                         if (myService.defaulturl) {
  1449.                             myURL=myService.defaulturl
  1450.                         } else {
  1451.                             this.debug("Bad situation: urlcapture specified with no defaulturl");
  1452.                             this.debug("Attempting to use url without replacement -- probable failure");
  1453.                         }
  1454.                     } else {
  1455.                         for (var i=0;i<varlist.length;i++) {
  1456.                             this.debug("var="+varlist[i]);
  1457.                             this.debug("value="+valuelist[i]);
  1458.                             if (varlist[i] == ":host:") {
  1459.                                 myURL=myURL.replace(/\/\/.+?\//,"//"+valuelist[i]+"/");
  1460.                             } else {
  1461.                                 var x=new RegExp("!"+varlist[i],"gi");
  1462.                                 myURL=myURL.replace(x,valuelist[i]);
  1463.                             }
  1464.                         }
  1465.                     }
  1466.                 } else if (myService.defaulturl) {
  1467.                     this.debug("No URLCapture information defined");
  1468.                     myURL=myService.defaulturl
  1469.                 } else {
  1470.                     this.debug("Bad situation: urlcapture specified with no defaulturl");
  1471.                     this.debug("Attempting to use url without replacement -- probable failure");
  1472.                 }
  1473.             }
  1474.         }
  1475.         if (!myURL || myURL=="") {
  1476.             if (action == "read") {
  1477.                 myURL=this.getServiceById("webmail:feed:netscape").inboxurl;
  1478.             } else if (action == "write" || action == "mailto") {
  1479.                 myURL=this.getServiceById("webmail:feed:netscape").writeurl;
  1480.             } else if (action == "address") {
  1481.                 myURL=this.getServiceById("webmail:feed:netscape").addressurl;
  1482.             } else {
  1483.                 this.debug("doAction called with invalid action!!!!!");
  1484.                 return;
  1485.             }
  1486.         }
  1487.         this.debug("myURL="+myURL);
  1488.         // Check if autologon is to be done
  1489.         if (this.Prefs.getPrefType("autolog."+accountName) == this.Prefs.PREF_STRING) {
  1490.             this.debug("autolog is turned on, attempting to log in");
  1491.             autolog=this.Prefs.getCharPref("autolog."+accountName);
  1492.         } else {
  1493.             this.debug("autolog is not turned on or no pref exists");
  1494.             this.debug("this.Prefs.getPrefType('autolog."+accountName+"')="+this.Prefs.getPrefType("autolog."+accountName));
  1495.             this.debug("this.Prefs.PR_STRING="+this.Prefs.PREF_STRING);
  1496.             autolog="false";
  1497.         }
  1498.         if (!doneauto && autolog=="true") {
  1499.             
  1500.             // JMC - hack hack, bypass this ugly stuff for now
  1501.             if (action == 'read') {
  1502.                 this.autoLogin(accountName,myService,action,aExtra);
  1503.                 return;
  1504.             }
  1505.             
  1506.             // Do autologin only if needed
  1507.             this.debug("autolog is true, testing URL");
  1508.             // Determine "needauto" RegExp
  1509.             var needAuto;
  1510.             if (myService.needlogin) {
  1511.                 needAuto = new RegExp(myService.needlogin,"gi");
  1512.             } else {
  1513.                 needAuto = new RegExp("(login again)|(expired)|(login page)","gi");
  1514.             }
  1515.             this.debug("needAuto="+needAuto.source);
  1516.             // Try to access the URL
  1517.             var req = new XMLHttpRequest();
  1518.             try {
  1519.                 req.open("POST",myService.autologurl,true);
  1520.             } catch (ex) {
  1521.                 this.debug("error in open POST");
  1522.                 this.debug(ex);
  1523.             }
  1524.             req.onreadystatechange = function() {
  1525.                 res = null;
  1526.                 if (req.readyState != 4) {
  1527.                     webmail.debug("Ready State = "+req.readyState);
  1528.                     return;
  1529.                 }
  1530.                 if ( req.status && (req.status / 100) == 2)
  1531.                     res = req.responseText;
  1532.                 else if (req.status && req.status == 405) {
  1533.                     // Protocol not supported - use GET
  1534.                     req2 = new XMLHttpRequest();
  1535.                     try {
  1536.                         req2.open("GET",myService.autologurl,true);
  1537.                     } catch (ex) {
  1538.                         webmail.debug("error in open GET");
  1539.                         webmail.debug(ex);
  1540.                     }
  1541.                     req2.onreadystatechange = function() {
  1542.                         if (req2.readyState != 4) {
  1543.                             webmail.debug("Ready State = "+req2.readyState);
  1544.                             return;
  1545.                         }
  1546.                         if ( req2.status && (req2.status / 100) == 2)
  1547.                             res = req2.responseText;
  1548.                         if (res) {
  1549.                             var matchedNeedlogin=res.match(needAuto);
  1550.                             if (matchedNeedlogin) {
  1551.                                 // Auto login
  1552.                                 webmail.debug("got a match for need login");
  1553.                                 webmail.debug("attempting auto log in");
  1554.                                 webmail.debug("match = "+matchedNeedlogin);
  1555.                                 webmail.autoLogin(accountName,myService,action,aExtra);
  1556.                                 return;
  1557.                             } else {
  1558.                                 webmail.debug("Failed to match needlogin");
  1559.                                 webmail.debug("Assuming that the URL loads fine");
  1560.  
  1561.                                 // TODO MSD: DO NOT CHECK THIS IN WITH FOLLOWING LINE UNCOMMENTED
  1562.                                 //this.debug("res="+res);
  1563.                             }
  1564.                         } else {
  1565.                             webmail.debug("res is not defined, req.status="+req.status);
  1566.                             webmail.debug("proceeding with autologin");
  1567.                             webmail.autoLogin(accountName,myService,action,aExtra);
  1568.                             return;
  1569.                         }
  1570.                         if (action=="mailto") {
  1571.                             if (myService && myService.composefields) {
  1572.                                 webmail.finishHandleURI(myURL,aExtra,myService.composefields);
  1573.                             } else {
  1574.                                 webmail.finishHandleURI(myURL,aExtra);
  1575.                             }
  1576.                         } else {
  1577.                             loadURIWithOpenPref(myURL,'browser.tabs.personaltoolbarbutton.open',null,null);
  1578.                         }
  1579.                     } //req2.onreadystatechange
  1580.                     try {
  1581.                         req2.send("");
  1582.                     } catch (ex) {
  1583.                         webmail.debug("error in req2.send");
  1584.                         webmail.debug(ex);
  1585.                         webmail.autoLogin(accountName,myService,action,aExtra);
  1586.                     }
  1587.                     return;
  1588.                 } // 405 status (wrong protocol)
  1589.                 if (res) {
  1590.                     var matchedNeedlogin=res.match(needAuto);
  1591.                     if (matchedNeedlogin) {
  1592.                         // Auto login
  1593.                         webmail.debug("got a match for need login");
  1594.                         webmail.debug("attempting auto log in");
  1595.                         webmail.debug("match = "+matchedNeedlogin);
  1596.                         webmail.autoLogin(accountName,myService,action,aExtra);
  1597.                         return;
  1598.                     } else {
  1599.                         webmail.debug("Failed to match needlogin");
  1600.                         webmail.debug("Assuming that the URL loads fine");
  1601.  
  1602.                         // TODO MSD: DO NOT CHECK THIS IN WITH FOLLOWING LINE UNCOMMENTED
  1603.     //                    this.debug("res="+res);
  1604.                     }
  1605.                 } else {
  1606.                     webmail.debug("res is not defined, req.status="+req.status);
  1607.                     webmail.debug("proceeding with autologin");
  1608.                     webmail.autoLogin(accountName,myService,action,aExtra);
  1609.                     return;
  1610.                 }
  1611.                 if (action=="mailto") {
  1612.                     if (myService && myService.composefields) {
  1613.                         webmail.finishHandleURI(myURL,aExtra,myService.composefields);
  1614.                     } else {
  1615.                         webmail.finishHandleURI(myURL,aExtra);
  1616.                     }
  1617.                 } else {
  1618.                     loadURIWithOpenPref(myURL,'browser.tabs.personaltoolbarbutton.open',null,null);
  1619.                 }
  1620.  
  1621.             } // req.onreadystatechange()
  1622.             try {
  1623.                 req.send("");
  1624.             } catch (ex) {
  1625.                 this.debug("error in req.send");
  1626.                 this.debug(ex);
  1627.                 this.autoLogin(accountName,myService,action,aExtra);
  1628.             }
  1629.             return;
  1630.         } // if (!doneauto && autolog=="true")
  1631.         if (action=="mailto") {
  1632.             if (myService && myService.composefields) {
  1633.                 this.finishHandleURI(myURL,aExtra,myService.composefields);
  1634.             } else {
  1635.                 this.finishHandleURI(myURL,aExtra);
  1636.             }
  1637.         } else {
  1638.             loadURIWithOpenPref(myURL,'browser.tabs.personaltoolbarbutton.open',null,null);
  1639.         }
  1640.     },
  1641.  
  1642.     read : function(accountName) {
  1643.         this.doAction(accountName,"read");
  1644.     },
  1645.  
  1646.     write : function(accountName) {
  1647.         this.doAction(accountName,"write");
  1648.     },
  1649.  
  1650.     address : function(accountName) {
  1651.         this.doAction(accountName,"address");
  1652.     },
  1653.  
  1654.     finishAction : function(aAccountName,aAction,aExtra) {
  1655.         setTimeout("webmail.doAction('"+aAccountName+"','continue_"+aAction+"','"+aExtra+"');",1);
  1656.     },
  1657.  
  1658.  
  1659.     handleURI : function (aURI) {
  1660.         // aHref: URI object
  1661.         // load "write" URL of default and then fill in form
  1662.         // from the uri
  1663.         // Is this really a mailto: uri?
  1664.         this.debug("RECEIVED A MAILTO URI!!!!!");
  1665.         this.debug("***********************\n"+aURI+"\n***********************");
  1666.         if (aURI.scheme != "mailto:"){
  1667.             // TODO: If not - pass it away or something????
  1668.         }
  1669.         // Check Prefs
  1670.         var doMailto=false;
  1671.         if (this.Prefs.getPrefType("mailto")==this.Prefs.PREF_BOOL) {
  1672.             doMailto=this.Prefs.getBoolPref("mailto");
  1673.         }
  1674.         if (!doMailto) {
  1675.             // TODO: Pass to other services or drop on floor??????
  1676.         }
  1677.         if (this.Prefs.getPrefType("default") != this.Prefs.PREF_STRING) {
  1678.             this.debug("No default set!! Unable to process mailto: HREF.");
  1679.             var x=new Error();
  1680.             x.message="No default webmail account defined.  Unable to process HREF's";
  1681.             throw x;
  1682.         }
  1683.         var accountName=this.Prefs.getCharPref("default");
  1684.  
  1685.         // Remainder of processing happens in finishHandleURI()
  1686.  
  1687.         this.doAction(accountName,"mailto",aURI);
  1688.     },
  1689.  
  1690.     handleURIEndLoad : function (aRequest,aStatus,aWebProgress) {
  1691.         webmail.debug("in handleURIEndLoad");
  1692.         webmail.debug("this.mAddress = "+this.mAddress);
  1693.         const nsIChannel = Components.interfaces.nsIChannel;
  1694.       var urlStr = aRequest.QueryInterface(nsIChannel).URI.spec;
  1695.       if (urlStr.indexOf(this.mFinalURL)>=0){
  1696.           webmail.debug("reached "+this.mFinalURL+", proceeding to fill in To: information.");
  1697.             var myinputs=aWebProgress.DOMWindow.document.getElementsByTagName("input");
  1698.             var toNames, subjectNames;
  1699.             if (this.mComposeFields) {
  1700.                 if (this.mComposeFields.match(/to=([^;]+)/i)) {
  1701.                     toNames = [ RegExp.$1 ];
  1702.                 }
  1703.                 if (this.mComposeFields.match(/subject=([^;]+)/i)) {
  1704.                     subjectNames = [ RegExp.$1 ];
  1705.                 }
  1706.             }
  1707.             if (!toNames) {
  1708.                 var toNames=[ "mailTo", "to_compose", "to", "msg_to", "tofield", "send_to" ];
  1709.             }
  1710.             if (!subjectNames) {
  1711.                 var subjectNames=[ "msg_subject", "subject", "mailSubject", "Subj" ];
  1712.             }
  1713.             var myTo, mySubj, myPossibleTo, myPossibleSubj;
  1714.             for (var i=0;i<myinputs.length;i++){
  1715.                 webmail.debug("checking "+myinputs[i].name+" against list of To: fields");
  1716.                 for (var j=0;j<toNames.length;j++) {
  1717.                     if (myinputs[i].name && myinputs[i].name==toNames[j]) {
  1718.                         myTo=myinputs[i];
  1719.                     }
  1720.                 }
  1721.                 if (!myTo && myinputs[i].name && myinputs[i].name.match(/to/i)) {
  1722.                     myPossibleTo=myinputs[i];
  1723.                 }
  1724.                 for (var j=0;j<subjectNames.length;j++) {
  1725.                     if (myinputs[i].name && myinputs[i].name==subjectNames[j]) {
  1726.                         mySubj=myinputs[i];
  1727.                     }
  1728.                 }
  1729.                 if (!mySubj && myinputs[i].name && myinputs[i].name.match(/subj/i)) {
  1730.                     myPossibleSubj=myinputs[i];
  1731.                 }
  1732.             }
  1733.             if (!myTo && myPossibleTo) myTo=myPossibleTo;
  1734.             if (myTo && this.mAddress) {
  1735.                 myTo.value=this.mAddress;
  1736.             }
  1737.             if (!mySubj && myPossibleSubj) mySubj=myPossibleSubj;
  1738.             if(mySubj && this.mSubject) {
  1739.                 mySubj.value=this.mSubject;
  1740.             }
  1741.               this.mBrowser.removeProgressListener(this);
  1742.           } else {
  1743.               webmail.debug("failed to find "+this.mFinalURL+" in "+urlStr);
  1744.           }
  1745.     },
  1746.  
  1747.     finishHandleURI : function (aWriteURL,aMailtoURI,aComposeFields) {
  1748.         // I should be able to navigate to aWriteURL and fill in the form using aMailtoURI
  1749.         var myAddress;
  1750.         if (aMailtoURI.match(/mailto:([^?&]+)/)) {
  1751.             myAddress=RegExp.$1;
  1752.         }
  1753.         var mySubject;
  1754.         if (aMailtoURI.match(/mailto:[^?&]+[?&]subject=([^?&]+)/)) {
  1755.             mySubject=RegExp.$1;
  1756.         }
  1757.         this.debug("aMailtoURI="+aMailtoURI);
  1758.         this.debug("myAddress="+myAddress);
  1759.         this.debug("mySubject="+mySubject);
  1760.         myEndLoad=new WebmailEndLoadListener(aWriteURL,true);
  1761.         myEndLoad.mAddress=myAddress;
  1762.         myEndLoad.mSubject=mySubject;
  1763.         myEndLoad.mFinalURL=aWriteURL;
  1764.         myEndLoad.mComposeFields=aComposeFields;
  1765.         myEndLoad.endDocumentLoad=this.handleURIEndLoad;
  1766.         myEndLoad.openIt();
  1767.     },
  1768.  
  1769.  
  1770.     placeItemRelative : function(newItem, toolbar, currentSet, index, maxIterations) {
  1771.         // Look for toolbar buttons that are progressively further away in
  1772.         // the currentSet
  1773.  
  1774.         // JMC - Special case, index = 0
  1775.         if (index == 0) {
  1776.             var nextItem = toolbar.firstChild;
  1777.             if (nextItem) {
  1778.                 toolbar.insertBefore(newItem, nextItem);
  1779.             } else {
  1780.                 toolbar.appendChild(newItem);
  1781.             }
  1782.             return true;
  1783.         }
  1784.  
  1785.         var j = 1;
  1786.         var placed = false;
  1787.         if (!maxIterations) maxIterations = 10;
  1788.         while (!placed && (j < maxIterations)) {
  1789.             // First try placing it AFTER the previous item
  1790.             var nextItem;
  1791.             if (index >= j) {
  1792.                 // this doesnt work for spacers and other anonymous elements
  1793.                 var prevItem = document.getElementById(currentSet[index-j]);
  1794.                 if (!prevItem)
  1795.                 {
  1796.                     // Count the number of these special elements which occur in the toolbarset
  1797.                     // BEFORE this point, and then get the xth of them
  1798.                     var specialCountLimit = 0;
  1799.                     for (var i=0; i < index; i++) {
  1800.                         if (currentSet[i] == currentSet[index-j])
  1801.                             specialCountLimit++;
  1802.                     }
  1803.  
  1804.                     var paletteItem = toolbar.firstChild;
  1805.                     var specialCount =0;
  1806.  
  1807.                     while (paletteItem) {
  1808.                         var paletteId = paletteItem.id;
  1809.                         var stringLen = currentSet[index-j].length;
  1810.                         var aId = currentSet[index-j];
  1811.                         if (paletteId.substring(0,stringLen) == aId) {
  1812.                             specialCount++;
  1813.                             if (specialCount >= specialCountLimit) {
  1814.                                 prevItem = paletteItem;
  1815.                                 break;
  1816.                             }
  1817.                         }
  1818.                         paletteItem = paletteItem.nextSibling;
  1819.                     }
  1820.                 }
  1821.                 if (prevItem) {
  1822.                     nextItem = prevItem.nextSibling;
  1823.                     if (prevItem.nextSibling) {
  1824.                         toolbar.insertBefore(newItem, prevItem.nextSibling);
  1825.                         this.debug('   placed after '+currentSet[index-j]+'!');
  1826.                         placed = true;
  1827.                     } else {
  1828.                         toolbar.appendChild(newItem);
  1829.                         placed = true;
  1830.                     }
  1831.                 }
  1832.             }
  1833.             if (!placed && (index < currentSet.length-j)) {
  1834.                 // That didn't work, so try placing it BEFORE the next item
  1835.                 nextItem = document.getElementById(currentSet[index+1]);
  1836.                 if (!nextItem)
  1837.                 {
  1838.                     // Count the number of these special elements which occur in the toolbarset
  1839.                     // BEFORE this point, and then get the xth of them
  1840.                     var specialCountLimit = 0;
  1841.                     for (var i=index; i > 0; i--) {
  1842.                         if (currentSet[i] == currentSet[index+1])
  1843.                             specialCountLimit++;
  1844.                     }
  1845.  
  1846.                     var paletteItem = toolbar.lastChild;
  1847.                     var specialCount =0;
  1848.  
  1849.                     while (paletteItem) {
  1850.                         var paletteId = paletteItem.id;
  1851.                         var stringLen = currentSet[index+1].length;
  1852.                         var aId = currentSet[index+1];
  1853.                         if (paletteId.substring(0,stringLen) == aId) {
  1854.                             specialCount++;
  1855.                             if (specialCount >= specialCountLimit) {
  1856.                                 nextItem = paletteItem;
  1857.                                 break;
  1858.                             }
  1859.                         }
  1860.                         paletteItem = paletteItem.previousSibling;
  1861.                     }
  1862.                 }
  1863.                 if (nextItem) {
  1864.                     toolbar.insertBefore(newItem, nextItem);
  1865.                     this.debug('   placed before '+currentSet[index+1]+'!');
  1866.                     placed = true;
  1867.                 }
  1868.             }
  1869.             j++;
  1870.         }
  1871.         return placed;
  1872.     },
  1873.  
  1874. }
  1875.  
  1876. function sort_services(first, second) {
  1877.     if (first.id == "webmail:feed:netscape") {
  1878.         return -1;
  1879.     }
  1880.     if (second.id == "webmail:feed:netscape") {
  1881.         return 1;
  1882.     }
  1883.     if (first.name < second.name) {
  1884.         return -1;
  1885.     } else {
  1886.         return 1;
  1887.     }
  1888. }
  1889.  
  1890. function checkNewWebmail(accountName) {
  1891.     // Confirm that the account wants updates
  1892.     webmail.debug("checkNewWebmail("+accountName+") called");
  1893.     var checkUpdates;
  1894.     var notify;
  1895.     var oldNum=0;
  1896.     var numNew=0;
  1897.     var changed=false;
  1898.     if (webmail.Prefs.getPrefType("checkUpdates."+accountName) == webmail.Prefs.PREF_STRING) {
  1899.         checkUpdates=webmail.Prefs.getCharPref("checkUpdates."+accountName);
  1900.     } else {
  1901.         checkUpdates="false";
  1902.     }
  1903.     if (!checkUpdates || checkUpdates == "false") return;
  1904.     // Check for last update and compare to current time
  1905.     var now = (new Date()).getTime();
  1906.     webmail.debug("now="+now);
  1907.     var then = 0;
  1908.     if (webmail.Prefs.getPrefType("lastCheck."+accountName) == webmail.Prefs.PREF_STRING) {
  1909.         var lastCheck = webmail.Prefs.getCharPref("lastCheck."+accountName);
  1910.         //then=new Date(lastCheck);
  1911.         then = lastCheck;
  1912.     }
  1913.     webmail.debug("then="+then);
  1914.     var interval = 0;
  1915.     if (webmail.Prefs.getPrefType("emailfreq."+accountName)) {
  1916.         var sInterval = webmail.Prefs.getCharPref("emailfreq."+accountName);
  1917.         interval = 60000 * sInterval;
  1918.     }
  1919.     var diff = now - then;
  1920.     webmail.debug("Time since last check = "+diff);
  1921.     webmail.debug("interval = "+interval);
  1922.     if (diff > interval) {
  1923.         webmail.Prefs.setCharPref("lastCheck."+accountName,now);
  1924.     } else {
  1925.         setTimeout("checkNewWebmail('"+accountName+"');",interval);
  1926.         return;
  1927.     }
  1928.     try {
  1929.         // Determine the value to put into the acceltext
  1930.  
  1931.         var doesExist=webmail.Prefs.getPrefType("provider."+accountName);
  1932.         if (doesExist) {
  1933.             var provider=webmail.Prefs.getCharPref("provider."+accountName);
  1934.         }
  1935.         if (!provider || provider=="")
  1936.             return;
  1937.  
  1938.         var myService=webmail.getServiceById(provider);
  1939.         if (!myService)
  1940.             return;
  1941.  
  1942.         var notify=myService.notify;
  1943.         if (!notify || notify=="")
  1944.             return;
  1945.  
  1946.         webmail.debug("notify="+notify);
  1947.         var params=notify.match(/[^;]+/g);
  1948.         if (!params || params.length < 1)
  1949.             return;
  1950.         webmail.debug('found '+params.length+' params');
  1951.  
  1952.         if (params[0].substr(0,7) == "scrape=") {
  1953.             webmail.debug('processing scrape');
  1954.             var nURL=params[0].substr(7);
  1955.             var nPre=null;
  1956.             var nPost=null;
  1957.             var nDoAuto=true; // autologin is default
  1958.             var nGetStr=null;
  1959.             var isPost=false;
  1960.             for (var i=1;i<params.length;i++) {
  1961.                 if (params[i].substr(0,4) == "pre=") nPre=params[i].substr(4);
  1962.                 else if (params[i].substr(0,5) =="post=") nPost=params[i].substr(5);
  1963.                 else if (params[i] == "neverauto") nDoAuto=false;
  1964.                 else if (params[i].substr(0,8) == "httpget=") {
  1965.                     nGetStr=params[i].substr(8);
  1966.                 } else if (params[i].substr(0,9) == "httppost=") {
  1967.                     nGetStr=params[i].substr(9);
  1968.                     isPost=true;
  1969.                 }
  1970.             }
  1971.             if (!nPre || !nPost) {
  1972.                 webmail.debug("pre or post info missing for scrape");
  1973.                 return;
  1974.             }
  1975.             if (nGetStr) {
  1976.                 webmail.debug('processing get-string');
  1977.                 nGetStr=nGetStr.replace(/\|/g,"&");
  1978.                 if ((nGetStr.indexOf("!password") > -1) ||
  1979.                     (nGetStr.indexOf("!userid") > -1)) {
  1980.                     // Get userId and password and do replacement
  1981.                     var myUserid   = null;
  1982.                     var myPassword = null;
  1983.                     var fHost={ value: "" }
  1984.                     var fUser={ value: "" }
  1985.                     var fPassword={ value: "" }
  1986.                     var mKey="webmail:"+accountName;
  1987.  
  1988.                     // MERC - JCH: Get passcard info
  1989.                     var domain = myService.passcarddomain;
  1990.                     var pc = GetWebmailPasscard(domain, mKey);
  1991.                     if (pc) {
  1992.                         webmail.debug('Found a passcard for this account, un/pw = '+pc.user+'/'+pc.password);
  1993.                         myUserid = pc.user;
  1994.                         myPassword = pc.password;
  1995.                     } else {
  1996.                         webmail.debug('\nWEBMAIL ERROR: Did not find passcard for mKey = '+mKey+'\n');
  1997.                         return;
  1998.                     }
  1999.  
  2000.                     if (!myUserid && !myPassword) {
  2001.                         webmail.debug("userID or Password not found, but needed");
  2002.                         return;
  2003.                     }
  2004.                     if (nGetStr.indexOf("!password") > -1)
  2005.                         nGetStr = nGetStr.replace(/\!password/g,myPassword);
  2006.                     if (nGetStr.indexOf("!userid") > -1)
  2007.                         nGetStr = nGetStr.replace(/\!userid/g,myUserid);
  2008.                 }
  2009.                 webmail.debug("URI string: "+nGetStr);
  2010.                 //nGetStr=encodeURI(nGetStr);
  2011.  
  2012.                 if (!isPost) {
  2013.                 //    nURL=nURL+"?"+nGetStr;
  2014.                     webmail.debug("Query string: "+nGetStr);
  2015.                     nURL=nURL+"?"+encodeURI(nGetStr);
  2016.                 }
  2017.             }
  2018.             //if (nDoAuto) {
  2019.             //    webmail.autoLogin(accountName,myService);
  2020.             //}
  2021.             webmail.debug("myX should be "+nPre+"\\d+"+nPost);
  2022.             var myX= new RegExp(nPre+"\\d+"+nPost,"");
  2023.             webmail.debug("myX="+myX.source);
  2024.             var proto="GET";
  2025.             if (isPost) {
  2026.                 proto="POST";
  2027.             }
  2028.             var req = new XMLHttpRequest();
  2029.             try {
  2030.                 req.open(proto,nURL,true);
  2031.             } catch (ex) {
  2032.                 webmail.debug("error in "+proto);
  2033.                 webmail.debug(ex);
  2034.             }
  2035.             webmail.debug("done open()");
  2036.             req.onerror = function(ex) {
  2037.                 dump("WEBMAIL REQUEST: req.onerror called\n");
  2038.                 dump(ex);
  2039.             }
  2040.             req.onprogress = function (ex) {
  2041.                 dump("WEBMAIL REQUEST: req.progress called\n");
  2042.             }
  2043.             req.onreadystatechange = function () {
  2044.                 dump("WEBMAIL REQUEST: req.onreadystatechange called\n");
  2045.                 if (req.readyState==4) {
  2046.                     // put all post processing here
  2047.                     var res = null;
  2048.                     if ( req.status && (req.status / 100) == 2) {
  2049.                         res = req.responseText;
  2050.                     }    else {
  2051.                         webmail.debug("req.status="+req.status);
  2052.                     }
  2053.                     if (!res){
  2054.                         return;
  2055.                     }
  2056.                     var ans=res.match(myX);
  2057.                     webmail.debug("res.match(myX)="+ans);
  2058.                     if (ans) {
  2059.                         var myText=ans[0];
  2060.                         var preX=new RegExp(nPre,"");
  2061.                         var temp=myText.substr(myText.match(preX)[0].length);
  2062.                         numNew=temp.match(/\d+/)[0];
  2063.                     } else {
  2064.                         webmail.debug("no match for myX in: \n"+res);
  2065.                         return;
  2066.                     }
  2067.                     //oldNum=webmail.getChecked(accountName);
  2068.                     webmail.setChecked(accountName,numNew);
  2069.                     //var netNew=numNew-oldNum;
  2070.                     var netNew = numNew;
  2071.                     if (netNew < 0) netNew=0;
  2072.                     // Get the list of elements which need to be updated
  2073.                     var updateElements = null;
  2074.                     try {
  2075.                         updateElements = document.getElementsByAttribute("webmailAccel",accountName);
  2076.                     } catch (ex) {}
  2077.  
  2078.                     if (updateElements && updateElements.length > 0) {
  2079.                         for (var i=0;i<updateElements.length;i++) {
  2080.                           // Update the elements with the new values or remove the
  2081.                           // acceltext.
  2082.                           if (updateElements[i].localName == "toolbarbutton") {
  2083.                             webmail.debug("~~~~~~~ Toolbarbutton mail notification "+updateElements[i].localName+" ~~~~~~~~~~");
  2084.                               // toolbarbuttons don't accept acceltext - must change the label and
  2085.                               // tooltiptext
  2086.                               if (checkUpdates == "true" && netNew > 0) {
  2087.                                   webmail.debug("updating element: "+updateElements[i].id + " type: "+updateElements[i].localName);
  2088.                                   updateElements[i].setAttribute("label",updateElements[i].getAttribute("baselabel") + "   " +netNew);
  2089.                                   updateElements[i].setAttribute("tooltiptext",accountName + " webmail account has " +netNew +" new messages");
  2090.                               } else if (numNew) {
  2091.                                   updateElements[i].setAttribute("label",updateElements[i].getAttribute("baselabel"));
  2092.                                   updateElements[i].setAttribute("tooltiptext",accountName + " webmail account has " +netNew +" new messages");
  2093.                               } else {
  2094.                                   updateElements[i].setAttribute("label",updateElements[i].getAttribute("baselabel"));
  2095.                                   updateElements[i].setAttribute("tooltiptext",accountName + " webmail account");
  2096.                               }
  2097.                           } else if (updateElements[i].localName == "description") {
  2098.                             webmail.debug("~~~~~~~ Description mail notification "+updateElements[i].localName+" ~~~~~~~~~~");
  2099.                               if (checkUpdates == "true" && netNew > 0) {
  2100.                                   webmail.debug("updating element: "+updateElements[i].id + " type: "+updateElements[i].localName);
  2101.                                   updateElements[i].setAttribute("value",updateElements[i].getAttribute("baselabel") + "   " +netNew);
  2102.                               } else {
  2103.                                   updateElements[i].setAttribute("value",updateElements[i].getAttribute("baselabel"));
  2104.                               }
  2105.                           } else {
  2106.                             webmail.debug("~~~~~~~ Undefined mail notification "+updateElements[i].localName+" ~~~~~~~~~~");
  2107.                               if (checkUpdates == "true" && netNew > 0) {
  2108.                                   webmail.debug("updating element: "+updateElements[i].id + " type: "+updateElements[i].localName);
  2109.                                   updateElements[i].setAttribute("tooltiptext",accountName + " webmail account has " +netNew +" new messages");
  2110.                                   updateElements[i].setAttribute("acceltext",netNew);
  2111.                               } else {
  2112.                                   updateElements[i].setAttribute("tooltiptext",accountName + " webmail account");
  2113.                                   updateElements[i].removeAttribute("acceltext");
  2114.                               }
  2115.                               if (checkUpdates == "true") {
  2116.                                   updateElements[i].setAttribute("tooltiptext",accountName + " webmail account has " +netNew +" new messages");
  2117.                               } else {
  2118.                                   updateElements[i].setAttribute("tooltiptext",accountName + " webmail account");
  2119.                               }
  2120.                           }
  2121.                         }
  2122.                     }
  2123.                     // Notify the user of new e-mail, if requested and different.
  2124.                     try {
  2125.                         notify = webmail.Prefs.getCharPref("notification."+accountName);
  2126.                     } catch (ex) {
  2127.                         notify = "None";
  2128.                     }
  2129.                     var myProvider;
  2130.                     try {
  2131.                         myProvider = webmail.Prefs.getCharPref("provider."+accountName);
  2132.                     } catch (ex) {
  2133.                         myProvider = "webmail:feed:netscape";
  2134.                     }
  2135.                     var myService = webmail.getServiceById(myProvider);
  2136.                     var myImage;
  2137.                     if (myService) {
  2138.                         myImage = myService.icon;
  2139.                     } else {
  2140.                         myImage = "chrome://browser/skin/throbber16-single.gif";
  2141.                     }
  2142.                     var notifyObserver = {
  2143.                         onAlertFinished : function (aAlertCookie) {},
  2144.                         myAccount : new String(),
  2145.                         onAlertClickCallback : function (aAlertCookie) {
  2146.                             webmail.read(this.myAccount);
  2147.                         }
  2148.                     }
  2149.                     notifyObserver.myAccount=accountName;
  2150.                     webmail.debug("Notification for account ("+accountName+") is "+notify);
  2151.                     if (checkUpdates=="true" && netNew> 0) {
  2152.                         var message="You have "+netNew+" new e-mails in your "+
  2153.                                             accountName+" webmail account";
  2154.                         if (notify == "Toaster") {
  2155.                             webmail.debug("attempting Toaster notification");
  2156.                             var alerts = Components.classes["@mozilla.org/alerts-service;1"]
  2157.                                                     .getService(Components.interfaces.nsIAlertsService);
  2158.                             alerts.showAlertNotification(myImage,
  2159.                             "New Email", message, true, "", notifyObserver);
  2160.                         } else if (notify == "Info-Bar") {
  2161.                             webmail.debug("attempting Info-Bar notification");
  2162.                             var extraData = new Array();
  2163.                             extraData.push(myImage);
  2164.                             extraData.push(message);
  2165.                             displayNotificationBar("webmail", extraData);
  2166.                         }
  2167.                     }
  2168.                 } else {
  2169.                     webmail.debug("req state = "+req.readyState);
  2170.                 }
  2171.             }
  2172.             try {
  2173.                 if (isPost) {
  2174.                     req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  2175.                     req.send(nGetStr);
  2176.                 } else {
  2177.                     req.send("");
  2178.                 }
  2179.             } catch (ex) {
  2180.                 webmail.debug("error in req.send");
  2181.                 webmail.debug(ex);
  2182.             }
  2183.             webmail.debug("done send()");
  2184.         }
  2185.     } catch (ex) {
  2186.         webmail.debug("checkNewWebmail failed oddly");
  2187.         webmail.debug(ex);
  2188.     }
  2189.     // submit the next update
  2190.     if (checkUpdates=="true") {
  2191.         var nTime;
  2192.         try {
  2193.             nTime = webmail.Prefs.getCharPref("emailfreq."+accountName);
  2194.         } catch (ex) {}
  2195.         if (nTime && nTime > 0) {
  2196.             setTimeout("checkNewWebmail('"+accountName+"');",60000*nTime);
  2197.         }
  2198.     }
  2199. }
  2200.  
  2201. function WebmailEndLoadListener(aURL,aSelectBrowser) {
  2202.     this.mURL = aURL;
  2203.     this.mSelectBrowser = aSelectBrowser;
  2204. }
  2205.  
  2206. WebmailEndLoadListener.prototype = {
  2207.  
  2208.     mURL : null,
  2209.     mBrowser : null,
  2210.     mSelectBrowser : null,
  2211.     mFinalURL : null,
  2212.     mAddress : null,
  2213.     mSubject : null,
  2214.     mLastChange : null,
  2215.  
  2216.     openIt : function () {
  2217.         if (this.mSelectBrowser) {
  2218.             gBrowser.selectedTab=gBrowser.addTabAt("about:blank");
  2219.             this.mBrowser = gBrowser.getBrowserForTab(gBrowser.selectedTab);
  2220.         } else {
  2221.             this.mBrowser = gBrowser.getBrowserForTab(gBrowser.addTabAt("about:blank"));
  2222.         }
  2223.         this.mBrowser.loadURI("about:blank");
  2224.         this.mBrowser.addProgressListener(this);
  2225.         this.mBrowser.webNavigation.browserEngine = 'Gecko';
  2226.         this.mBrowser.webNavigation.loadURI(this.mURL,Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE,null,null,null);
  2227.         this.mLastChange = new Date();
  2228.     },
  2229.  
  2230.     onProgressChange : function (aWebProgress, aRequest,
  2231.                                     aCurSelfProgress, aMaxSelfProgress,
  2232.                                     aCurTotalProgress, aMaxTotalProgress) {
  2233.       this.mLastChange = new Date();
  2234.         webmail.debug ("onPregressChange Called");
  2235.         const nsIWebProgressListener = Components.interfaces.nsIWebProgressListener;
  2236.         const nsIChannel = Components.interfaces.nsIChannel;
  2237.         if (aRequest) {
  2238.                 try {
  2239.                 var urlStr = aRequest.QueryInterface(nsIChannel).URI.spec;
  2240.               } catch(ex) {urlStr="undefined!";}
  2241.           } else {
  2242.               urlStr="undefined";
  2243.           }
  2244.         webmail.debug("urlStr="+urlStr);
  2245.     },
  2246.  
  2247.     onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus)
  2248.     {
  2249.         this.mLastChange = new Date();
  2250.         webmail.debug ("onStateChange Called");
  2251.         const nsIWebProgressListener = Components.interfaces.nsIWebProgressListener;
  2252.         const nsIChannel = Components.interfaces.nsIChannel;
  2253.         if (aRequest) {
  2254.                 try {
  2255.                 var urlStr = aRequest.QueryInterface(nsIChannel).URI.spec;
  2256.               } catch(ex) {urlStr="undefined!";}
  2257.           } else {
  2258.               urlStr="undefined";
  2259.           }
  2260.         webmail.debug("urlStr="+urlStr);
  2261.         if (aStateFlags & nsIWebProgressListener.STATE_STOP) {
  2262.             webmail.debug("State is Stop");
  2263.           if (aStateFlags & nsIWebProgressListener.STATE_IS_NETWORK) {
  2264.               webmail.debug("State is network");
  2265.             if (aRequest) {
  2266.                 webmail.debug("running endDocumentLoad function in Progress Listener");
  2267.                 webmail.debug("browser engine string=" +    this.mBrowser.webNavigation.browserEngine );
  2268.               this.endDocumentLoad(aRequest, aStatus, aWebProgress);
  2269.                     }
  2270.           }
  2271.         }
  2272.     },
  2273.  
  2274.     onLocationChange : function(aWebProgress, aRequest, aLocation) {
  2275.         webmail.debug ("onLocationChange Called");
  2276.         if (aLocation) {
  2277.                 var urlStr = aLocation.spec;
  2278.             } else {
  2279.                 urlStr="undefined";
  2280.             }
  2281.         webmail.debug("urlStr="+urlStr);
  2282.         this.mLastChange = new Date();
  2283.     },
  2284.  
  2285.     onStatusChange : function(aWebProgress, aRequest, aStatus, aMessage) {
  2286.         this.mLastChange = new Date();
  2287.     },
  2288.  
  2289.     onSecurityChange : function(aWebProgress, aRequest, aState) {
  2290.         this.mLastChange = new Date();
  2291.     },
  2292.  
  2293.     QueryInterface : function(aIID)
  2294.     {
  2295.         if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  2296.             aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
  2297.             aIID.equals(Components.interfaces.nsISupports))
  2298.         return this;
  2299.         throw Components.results.NS_NOINTERFACE;
  2300.     },
  2301.  
  2302.     endDocumentLoad : function(aRequest, aStatus, aWebProgress) {
  2303.         webmail.debug("Running the default endDocumentLoad function");
  2304.     }
  2305. }
  2306.