home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / mail.xpi / bin / chrome / messenger.jar / content / messenger / aw-identity.js < prev    next >
Text File  |  2001-08-15  |  7KB  |  215 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is mozilla.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  * Alec Flett <alecf@netscape.com>
  22.  * Seth Spitzer <sspitzer@netscape.com>
  23.  * Hσkan Waara <hwaara@chello.se>
  24.  */
  25.  
  26. var gCurrentDomain;
  27. var gPrefsBundle;
  28.  
  29. function validate(data)
  30. {
  31.   var name = document.getElementById("fullName").value;
  32.  
  33.   if (!name) {
  34.     var alertText = gPrefsBundle.getString("enterName");
  35.     window.alert(alertText);
  36.     return false;
  37.   }
  38.   if (!validateEmail()) return false;
  39.  
  40.   parent.UpdateWizardMap();
  41.   
  42.   return true;
  43. }
  44.  
  45. // this is kind of wacky.. basically it validates the email entered in
  46. // the text field to make sure it's in the form "user@host"..
  47. //
  48. // However, if there is a current domain (retrieved during onInit)
  49. // then we have to do some special tricks:
  50. // - if the user ALSO entered an @domain, then we just chop it off
  51. // - at some point it would be useful to keep the @domain, in case they
  52. //   wish to override the domain.
  53. function validateEmail()
  54. {
  55.   var emailElement = document.getElementById("email");
  56.   var email = emailElement.value;
  57.   var emailArray = email.split('@');
  58.  
  59.   if (gCurrentDomain) {
  60.     // check if user entered an @ sign even though we have a domain
  61.     if (emailArray.length >= 2) {
  62.       email = emailArray[0];
  63.       emailElement.value = email;
  64.     }
  65.     
  66.     if (!email.length || containsIllegalChar(email)) {
  67.       var alertText = gPrefsBundle.getString("enterValidEmailPrefix");
  68.       window.alert(alertText);
  69.       return false;
  70.     }
  71.   }
  72.  
  73.   else {
  74.     if (emailArray.length != 2 ||
  75.         !emailArray[0].length ||
  76.         !emailArray[1].length || 
  77.         containsIllegalChar(emailArray[0]) ||
  78.         containsIllegalChar(emailArray[1])) {
  79.       alertText = gPrefsBundle.getString("enterValidEmail");
  80.       window.alert(alertText);
  81.       return false;
  82.     }
  83.   }
  84.     
  85.   return true;
  86. }
  87.  
  88. // This function checks for common illegal
  89. // characters. This shouldn't be too strict, since
  90. // we do more extensive tests later. -Hσkan
  91. function containsIllegalChar(aString)
  92. {
  93.   for (var i=0; i < aString.length; i++) {
  94.     var code = aString.charCodeAt(i);
  95.     if (code > 127)
  96.       return true; // non-ASCII
  97.     else if (code == 9 || code == 10 || code == 11 || code == 32)
  98.       return true; // white space
  99.     else if (code == 64)
  100.       return true; // '@'
  101.   }
  102.   return false;
  103.  
  104. function onInit()
  105. {
  106.   gPrefsBundle = document.getElementById("bundle_prefs");
  107.   setEmailDescriptionText();
  108.   checkForDomain();
  109.   checkForFullName(); 
  110.   checkForEmail(); 
  111. }
  112.  
  113. // Use email example data that ISP has provided. ISP data, if avaialble
  114. // for the choice user has made, will be read into CurrentAccountData. 
  115. // Default example data from properties will be used when the info is missing. 
  116. function setEmailDescriptionText()
  117. {
  118.     var emailDescText = document.getElementById("emailDescText");
  119.     var emailFieldLabel = document.getElementById("emailFieldLabel");
  120.     var currentAccountData = parent.gCurrentAccountData;
  121.    
  122.     var displayText =  null;
  123.     var emailFieldLabelData =  null;
  124.     var setDefaultEmailDescStrings = true; 
  125.  
  126.     // Get values for customized data from current account 
  127.     if (currentAccountData)
  128.     {
  129.         var emailProvider  = currentAccountData.emailProviderName;
  130.         var sampleEmail    = currentAccountData.sampleEmail;
  131.         var sampleUserName = currentAccountData.sampleUserName;
  132.         var emailIDDesc    = currentAccountData.emailIDDescription;
  133.         var emailIDTitle   = currentAccountData.emailIDFieldTitle;
  134.  
  135.         if (emailProvider  &&
  136.             sampleEmail    &&
  137.             sampleUserName &&
  138.             emailIDDesc    &&
  139.             emailIDTitle)
  140.         {
  141.             // Get email description data
  142.             displayText = gPrefsBundle.getFormattedString("customizedEmailText",
  143.                                                           [emailProvider,
  144.                                                            emailIDDesc,
  145.                                                            sampleEmail,
  146.                                                            sampleUserName]);
  147.  
  148.             // Set emailfield label
  149.             emailFieldLabelData =  emailIDTitle;
  150.             emailFieldLabel.setAttribute("value", emailFieldLabelData);
  151.  
  152.             // Need to display customized data. Turn off default settings.
  153.             setDefaultEmailDescStrings = false; 
  154.         }
  155.     }
  156.  
  157.     if (setDefaultEmailDescStrings)
  158.     {
  159.         // Check for obtained values and set with default values if needed
  160.         var username = gPrefsBundle.getString("exampleEmailUserName"); 
  161.         var domain = gPrefsBundle.getString("exampleEmailDomain"); 
  162.  
  163.         displayText = gPrefsBundle.getFormattedString("defaultEmailText",
  164.                                                       [username, domain]);
  165.     }
  166.  
  167.     // Create a text nodes with text to be displayed
  168.     var emailDescTextNode       =  document.createTextNode(displayText);
  169.  
  170.     // Display the dynamically generated text for email description 
  171.     emailDescText.appendChild(emailDescTextNode);
  172. }
  173.  
  174. // retrieve the current domain from the parent wizard window,
  175. // and update the UI to add the @domain static text
  176. function checkForDomain()
  177. {
  178.   var accountData = parent.gCurrentAccountData;
  179.   if (!accountData) return;
  180.   if (!accountData.domain) return;
  181.  
  182.   // save in global variable
  183.   gCurrentDomain = accountData.domain;
  184.   
  185.   var postEmailText = document.getElementById("postEmailText");
  186.   postEmailText.setAttribute("value", "@" + gCurrentDomain);
  187. }
  188.  
  189. function checkForFullName() {
  190.     var name = document.getElementById("fullName");
  191.     if (name.value=="") {
  192.         try {
  193.             var userInfo = Components.classes["@mozilla.org/userinfo;1"].getService(Components.interfaces.nsIUserInfo);
  194.             name.value = userInfo.fullname;
  195.         }
  196.         catch (ex) {
  197.             // dump ("checkForFullName failed: " + ex + "\n");
  198.         }
  199.     }
  200. }
  201.  
  202. function checkForEmail() {
  203.     var email = document.getElementById("email");
  204.     if (email.value=="") {
  205.         try {
  206.             var userInfo = Components.classes["@mozilla.org/userinfo;1"].getService(Components.interfaces.nsIUserInfo);
  207.             email.value = userInfo.emailAddress;
  208.         }
  209.         catch (ex) {
  210.             // dump ("checkForEmail failed: " + ex + "\n"); 
  211.         }
  212.     }
  213. }
  214.