home *** CD-ROM | disk | FTP | other *** search
/ PC World 2006 December / PCWorld_2006-12_cd.bin / komunikace / netscape / nsb-install-8-1-2.exe / chrome / browser.jar / content / browser / webmail.js < prev   
Text File  |  2006-01-06  |  85KB  |  2,417 lines

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