home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Programy / nsb-install-8-0.exe / chrome / toolkit.jar / content / mozapps / autofill / autofill.js < prev    next >
Text File  |  2005-07-29  |  37KB  |  1,221 lines

  1. const AUTOFILL_DEBUG = false;
  2. function afDebug(msg) {
  3.     if (AUTOFILL_DEBUG)
  4.         dump('autofill.js: '+msg+'\n');
  5. }
  6.  
  7. var suppressEncryptedDatacards = false;
  8.  
  9. var afService = datacardUtils.afService;
  10.  
  11. //credit card information
  12. var cc_name_val;
  13. var cc_number_val;
  14. var cc_type_val;
  15. var cc_expire_month_val;
  16. var cc_expire_year_val;
  17. var cc_password_val;
  18. var cc_primary_val;
  19. var cardNumValue;
  20. var cardNumValueVerified;
  21. var passwordHasChanged = 0; //FALSE, Change Password flag
  22.  
  23. //---variables to store form field values -----------------
  24. var primary_val;
  25. var FFtitle_val;
  26. var fname_val;
  27. var lname_val;
  28. var email_val;
  29. var phone_val;
  30. var workphone_val;
  31. var gPrefService = null;
  32.  
  33. //primary address
  34. var street_val;
  35. var street2_val;
  36. var city_val;
  37. var state_val;
  38. var zip_val;
  39. var country_val;
  40.  
  41. //alternative address
  42. var alt_street_val;
  43. var alt_street2_val;
  44. var alt_city_val;
  45. var alt_state_val;
  46. var alt_zip_val;
  47. var alt_country_val;
  48.  
  49. var extraFields_val;
  50.  
  51. //---form fields --------------------------------------------
  52. var primary;
  53. var alternative;
  54. var dcProtect;
  55. var datacardAutofillList_val;
  56.  
  57. //credit card button
  58. var creditCardButton;
  59.  
  60. var fname;
  61. var lname;
  62. var email;
  63. var phone;
  64.  
  65. //primary address
  66. var street;
  67. var street2;
  68. var city;
  69. var state;
  70. var zip;
  71. var country;
  72.  
  73. var prevAutofillStatus;
  74. var prevPrimaryStatus;
  75.  
  76. var formHasNoData = 0; //FALSE
  77.  
  78. var kPREFContractID = "@mozilla.org/preferences-service;1";
  79. var kPREFIID = Components.interfaces.nsIPrefService;
  80. var kPBIID = Components.interfaces.nsIPrefBranch;
  81. var PREF = null;
  82.  
  83. var gTabSelectInit = false;
  84. var removeAllFlag = false;
  85. var clearFormDataCount = 0;
  86.  
  87. var currentDatacard = null;
  88. var currentDatacardFile = null;
  89. var currentDatacardIsEncrypted = false;
  90. var currentDatacardIsDirty = false;
  91.  
  92. function saveDatacardProperty(key, value)
  93. {
  94.     afDebug('saveDatacardProperty('+key+', '+value+')');
  95.     afService.SetDatacardFieldByType(currentDatacardFile,
  96.                                      nsIAutoFillService.FIELDTYPE_PROPERTY,
  97.                                      key,
  98.                                      value);
  99. }
  100.  
  101. function saveDatacardField(key, value)
  102. {
  103.     afDebug('saveDatacardField('+key+', '+value+')');
  104.     afService.SetDatacardFieldByType(currentDatacardFile,
  105.                                      nsIAutoFillService.FIELDTYPE_REGULAR,
  106.                                      key,
  107.                                      value);
  108. }
  109.  
  110. function saveDatacardAdvancedField(key, value)
  111. {
  112.     afDebug('saveDatacardAdvancedField('+key+', '+value+')');
  113.     afService.SetDatacardFieldByType(currentDatacardFile,
  114.                                      nsIAutoFillService.FIELDTYPE_ADVANCED,
  115.                                      key,
  116.                                      value);
  117. }
  118.  
  119. function getDictionaryValue(dict, key)
  120. {
  121.     if (dict.hasKey(key)) {
  122.         var val = dict.getValue(key);
  123.         val.QueryInterface(Components.interfaces.nsIPrefLocalizedString);
  124.         return val.toString();
  125.     } else {
  126.         return '';
  127.     }
  128. }
  129.  
  130.  
  131. function saveDatacard()
  132. {
  133.     // Only save if we need to
  134.     if (!currentDatacardIsDirty) return;
  135.  
  136.     // If the datacard is encrypted and the master password has not been
  137.     // entered, then we don't actually want to save it
  138.     if (suppressEncryptedDatacards && currentDatacardIsEncrypted) return;
  139.     
  140.     afDebug('saveDatacard()');
  141.     // formHasNoData = 0; //redundant, already set by read data function.
  142.  
  143.     FFtitle_val.value = FFtitle.value;
  144.     fname_val.value = fname.value;
  145.     lname_val.value = lname.value;
  146.     email_val.value = email.value;
  147.     phone_val.value = phone.value;
  148.     workphone_val.value = workphone.value;
  149.  
  150.     dcardLabel_val.value = dcardLabel.value;
  151.     dump("dcardLabel_val.value " + dcardLabel_val.value + "\n");
  152.     datacardAutofillList_val.value = datacardAutofillList.value;
  153.     dump("datacardAutofillList_val.value " + datacardAutofillList_val.value + "\n");
  154.  
  155.     var pri = primary.selected;
  156.     if (pri.toString() == 'true')
  157.     {
  158.         street_val.value = street.value;
  159.         street2_val.value = street2.value;
  160.         city_val.value = city.value;
  161.         zip_val.value = zip.value;
  162.         state_val.value = state.value;
  163.         country_val.value = country.value;
  164.     }
  165.     else
  166.     {
  167.         alt_street_val.value = street.value;
  168.         alt_street2_val.value = street2.value;
  169.         alt_city_val.value = city.value;
  170.         alt_zip_val.value = zip.value;
  171.         alt_state_val.value = state.value;
  172.         alt_country_val.value = country.value;
  173.     }
  174.     primary = document.getElementById("PriAddr");
  175.     alternative = document.getElementById("AltAddr");
  176.  
  177.     dump("Now Saving Autofill value\n");
  178.  
  179.     saveDatacardField('title', FFtitle_val.value);
  180.     saveDatacardField('firstname', fname_val.value);
  181.     saveDatacardField('lastname', lname_val.value);
  182.     saveDatacardField('email', email_val.value);
  183.     saveDatacardField('phone', phone_val.value);
  184.     saveDatacardField('work_phone', workphone_val.value);
  185.  
  186.     saveDatacardField('street', street_val.value);
  187.     saveDatacardField('street2', street2_val.value);
  188.     saveDatacardField('city', city_val.value);
  189.     saveDatacardField('zip', zip_val.value);
  190.     saveDatacardField('state', state_val.value);
  191.     saveDatacardField('country', country_val.value);
  192.  
  193.     saveDatacardField('alt_street', alt_street_val.value);
  194.     saveDatacardField('alt_street2', alt_street2_val.value);
  195.     saveDatacardField('alt_city', alt_city_val.value);
  196.     saveDatacardField('alt_zip', alt_zip_val.value);
  197.     saveDatacardField('alt_state', alt_state_val.value);
  198.     saveDatacardField('alt_country', alt_country_val.value);
  199.  
  200.     var advExtras = ripExtraFieldsFromTree();
  201.     for (var i = 0; i < advExtras.fields.length; i++) {
  202.         saveDatacardAdvancedField(advExtras.fields[i], advExtras.values[i]);
  203.     }
  204.  
  205.     var dcProtect_enabled = document.getElementById("dcProtect");
  206.     if (dcProtect_enabled.checked)
  207.         saveDatacardProperty("Encrypted", "1");
  208.     else
  209.         saveDatacardProperty("Encrypted", "0");
  210.  
  211.     saveDatacardProperty('Label', currentDatacard);
  212.     saveDatacardProperty('WhenToFill', datacardAutofillList_val.value);
  213.  
  214.     //Credit Card
  215.     CCardOK();
  216. }
  217.  
  218.  
  219. function readDatacard()
  220. {
  221.     afDebug('readDatacard()');
  222.     formHasNoData = 0;
  223.  
  224.     var propDict =
  225.         afService.GetDatacardFieldsByType(
  226.             currentDatacardFile, nsIAutoFillService.FIELDTYPE_PROPERTY);
  227.     if (suppressEncryptedDatacards) {
  228.         currentDatacardIsEncrypted = (getDictionaryValue(propDict, 'Encrypted') == '1');
  229.         if (currentDatacardIsEncrypted) return;
  230.     }
  231.     var regularDict =
  232.         afService.GetDatacardFieldsByType(
  233.             currentDatacardFile, nsIAutoFillService.FIELDTYPE_REGULAR);
  234.     var advancedDict =
  235.         afService.GetDatacardFieldsByType(
  236.             currentDatacardFile, nsIAutoFillService.FIELDTYPE_ADVANCED);
  237.  
  238.     FFtitle_val.value = getDictionaryValue(regularDict, 'title');
  239.     fname_val.value = getDictionaryValue(regularDict, 'firstname');
  240.     lname_val.value = getDictionaryValue(regularDict, 'lastname');
  241.     email_val.value = getDictionaryValue(regularDict, 'email');
  242.     phone_val.value = getDictionaryValue(regularDict, 'phone');
  243.     workphone_val.value = getDictionaryValue(regularDict, 'work_phone');
  244.  
  245.     street_val.value = getDictionaryValue(regularDict, 'street');
  246.     street2_val.value = getDictionaryValue(regularDict, 'street2');
  247.     city_val.value = getDictionaryValue(regularDict, 'city');
  248.     state_val.value = getDictionaryValue(regularDict, 'state');
  249.     zip_val.value = getDictionaryValue(regularDict, 'zip');
  250.     country_val.value = getDictionaryValue(regularDict, 'country');
  251.     if (country_val.value == '')
  252.         country_val.value = 'United States';
  253.  
  254.     alt_street_val.value = getDictionaryValue(regularDict, 'alt_street');
  255.     alt_street2_val.value = getDictionaryValue(regularDict, 'alt_street2');
  256.     alt_city_val.value = getDictionaryValue(regularDict, 'alt_city');
  257.     alt_state_val.value = getDictionaryValue(regularDict, 'alt_state');
  258.     alt_zip_val.value = getDictionaryValue(regularDict, 'alt_zip');
  259.     alt_country_val.value = getDictionaryValue(regularDict, 'alt_country');
  260.     if (alt_country_val.value == '')
  261.         alt_country_val.value = 'United States';
  262.  
  263.     var advKeys = advancedDict.getKeys({});
  264.     var pairs = new Array();
  265.     for (var k = 0; k < advKeys.length; k++) {
  266.         var key = advKeys[k];
  267.         var val = getDictionaryValue(advancedDict, key);
  268.         pairs[pairs.length] = key+'='+escape(val);
  269.     }
  270.     extraFields_val.value = pairs.join('|');
  271.     refreshExtraFieldsUI();
  272.  
  273.     dcardLabel_val.value = getDictionaryValue(propDict, 'Label');
  274.     afDebug("read datacard: " + dcardLabel_val.value);
  275.     datacardAutofillList_val.value = getDictionaryValue(propDict, 'WhenToFill');
  276.     dcardProtect_val.value = getDictionaryValue(propDict, 'Encrypted');
  277.  
  278.     if (dcardProtect_val.value == '1')
  279.         document.getElementById("dcProtect").checked = true;
  280.     else
  281.         document.getElementById("dcProtect").checked = false;
  282.  
  283.     primary_val.value = "true";
  284.     if (primary_val.value == 'true') {
  285.         document.getElementById("afillRadio").selectedIndex = 0;
  286.     } else {
  287.         document.getElementById("afillRadio").selectedIndex = 1;
  288.     }
  289.  
  290.     var pv = primary_val.value;
  291.     if (pv != "false" && pv != "true")
  292.         pv = "true";
  293.  
  294.     FFtitle.value = FFtitle_val.value;
  295.     fname.value=fname_val.value;
  296.     lname.value=lname_val.value;
  297.     email.value=email_val.value;
  298.     phone.value=phone_val.value;
  299.     workphone.value=workphone_val.value;
  300.     dcardLabel.value = dcardLabel_val.value;
  301.     datacardAutofillList.value=datacardAutofillList_val.value;
  302.     dcardProtect.value=dcardProtect_val.value;
  303.  
  304.     if (pv.toString()=='true') {
  305.         dump("Now Reading Primary data!\n");        
  306.         prevPrimaryStatus="true";
  307.         street.value=street_val.value;
  308.         street2.value=street2_val.value;
  309.         city.value=city_val.value;
  310.         zip.value=zip_val.value;
  311.         state.value=state_val.value;
  312.         country.value=country_val.value;
  313.         dump("primary:"+primary+"\n");
  314.         //primary.setAttribute("selected",'true');
  315.     } else {
  316.         dump("Now Reading Alternate data!\n");
  317.         prevPrimaryStatus="false";
  318.         street.value=alt_street_val.value;
  319.         street2.value=alt_street2_val.value;
  320.         city.value=alt_city_val.value;
  321.         zip.value=alt_zip_val.value;
  322.         state.value=alt_state_val.value;
  323.         country.value=alt_country_val.value;
  324.         //primary.setAttribute("selected", 'true');
  325.     }
  326.  
  327.     //Credit card
  328.     initCreditCard();
  329.     ccStartUp();
  330.     
  331.     currentDatacardIsDirty = false;
  332. }
  333.  
  334.  
  335. function Startup()
  336. {
  337.     afDebug("Startup() ~~~~~~~~~~~");
  338.  
  339.     // Populate countries
  340.     var countryMenuPopup = document.getElementById('countryType');
  341.     for (var i = 0; i < countryList.length; i++) {
  342.         var item = document.createElement('menuitem');
  343.         item.setAttribute('value', countryList[i]);
  344.         item.setAttribute('label', countryList[i]);
  345.         countryMenuPopup.appendChild(item);
  346.     }
  347.  
  348.     //---form fields --------------------------------------------
  349.     primary=document.getElementById("PriAddr");
  350.     alternative=document.getElementById("AltAddr");
  351.  
  352.     dcProtect=document.getElementById("dcProtect");
  353.     FFtitle=document.getElementById("Title");
  354.     fname=document.getElementById("FirstName");
  355.     lname=document.getElementById("LastName");
  356.     email=document.getElementById("EmailAddr");
  357.     phone=document.getElementById("PhoneNum");
  358.     workphone=document.getElementById("WorkPhoneNum");
  359.  
  360.     street=document.getElementById("StrAddr");
  361.     street2=document.getElementById("StrAddr2");
  362.     city = document.getElementById("City");
  363.     state=document.getElementById("State");
  364.     zip=document.getElementById("ZipCode");
  365.     country=document.getElementById("countryList");
  366.  
  367.     creditCardButton=document.getElementById("ccButton");
  368.  
  369.     dcardLabel = document.getElementById("datacardName");
  370.     datacardAutofillList = document.getElementById("datacardAutofillList");
  371.     dcardProtect = document.getElementById("dcProtect");
  372.  
  373.     //---variables to store form field values -----------------
  374.     primary_val=new Object();
  375.     FFtitle_val =new Object();FFtitle_val.value="";
  376.     fname_val=new Object();fname_val.value="";
  377.     lname_val=new Object();lname_val.value="";
  378.     email_val=new Object();email_val.value="";
  379.     phone_val=new Object();phone_val.value="";
  380.     workphone_val=new Object();workphone_val.value="";
  381.  
  382.     dcardLabel_val=new Object(); dcardLabel_val.value="";
  383.     datacardAutofillList_val=new Object(); datacardAutofillList_val.value="";
  384.     dcardProtect_val=new Object(); dcardProtect_val.value="";
  385.  
  386.     //primary address
  387.     street_val=new Object();street_val.value="";
  388.     street2_val=new Object();street2_val.value="";
  389.     city_val=new Object();city_val.value="";
  390.     state_val=new Object();state_val.value="";
  391.     zip_val=new Object(); zip_val.value="";
  392.     country_val=new Object();country_val.value="";
  393.  
  394.     //alternative address
  395.     alt_street_val=new Object(); alt_street_val.value="";
  396.     alt_street2_val=new Object(); alt_street2_val.value="";
  397.     dump("alt_street:"+alt_street_val.value+"!\n");
  398.     alt_city_val=new Object();alt_city_val.value="";
  399.     alt_state_val=new Object();alt_state_val.value="";
  400.     alt_zip_val=new Object();alt_zip_val.value="";
  401.     alt_country_val=new Object();alt_country_val.value="";
  402.  
  403.     // extra fields
  404.     extraFields_val=new Object();extraFields_val.value="";
  405.     refreshExtraFieldsUI();
  406.  
  407.     //control information
  408.     primary_val=new Object(); primary_val.value=" ";
  409.  
  410.     gTabSelectInit = true;
  411.     setTab();
  412.     populateDatacardList();
  413.     // dataCardAutoSaveChange();  Not used 
  414.     populateBlacklist();
  415.  
  416.     // Register event listener to tell when datacards are being changed
  417.     var deck = document.getElementById('datacardDetails-deck');
  418.     deck.addEventListener('change', onDatacardChanged, true);
  419.     deck.addEventListener('select', onDatacardChanged, true);
  420.     deck.addEventListener('textentered', onDatacardChanged, true);
  421.     deck.addEventListener('focus', onDatacardChanged, true);
  422.     deck.addEventListener('command', onDatacardChanged, true);
  423.     deck.addEventListener('keypress', onDatacardChanged, true);
  424.  
  425.     // setup event handler
  426.     window.top.hPrefWindow.registerOKCallbackFunc(onAutoFillOK);
  427.     
  428.     // load passcard manager
  429.     PasscardOnLoad();
  430.  
  431.     setTab();
  432. }
  433.  
  434.  
  435. function onDatacardChanged(event) {
  436.     afDebug('onDatacardChanged()');
  437.     currentDatacardIsDirty = true;
  438. }
  439.  
  440.  
  441. function refreshExtraFieldsUI() {
  442.     afDebug('refreshExtraFieldsUI('+extraFields_val.value+')');
  443.  
  444.     // Clear the tree
  445.     var treeChildren = document.getElementById('advancedTreeChildren');
  446.     while (treeChildren.childNodes.length)
  447.         treeChildren.removeChild(treeChildren.childNodes[treeChildren.childNodes.length-1]);
  448.  
  449.     if (extraFields_val.value.length) {
  450.         document.getElementById('advancedDeck').selectedIndex = 1;
  451.         var pairs = extraFields_val.value.split('|');
  452.         for (var i = 0; i < pairs.length; i++) {
  453.             var delim = pairs[i].indexOf('=');
  454.             var fieldName = pairs[i].substring(0,delim);
  455.             var value = unescape(pairs[i].substring(delim+1));
  456.             var keyCell = document.createElement('treecell');
  457.             keyCell.setAttribute('label', fieldName);
  458.             var valCell = document.createElement('treecell');
  459.             valCell.setAttribute('label', value);
  460.             var row = document.createElement('treerow');
  461.             row.appendChild(keyCell);
  462.             row.appendChild(valCell);
  463.             var item = document.createElement('treeitem');
  464.             item.appendChild(row);
  465.             treeChildren.appendChild(item);
  466.         }
  467.     } else {
  468.         document.getElementById('advancedDeck').selectedIndex = 0;
  469.     }
  470. }
  471.  
  472.  
  473. function ripExtraFieldsFromTree() {
  474.     var treeChildren = document.getElementById('advancedTreeChildren');
  475.     var pairs = new Object();
  476.     pairs.fields = new Array();
  477.     pairs.values = new Array();
  478.     for (var i = 0; i < treeChildren.childNodes.length; i++) {
  479.         var row = treeChildren.childNodes[i].firstChild;
  480.         var fieldIdx = pairs.fields.length;
  481.         pairs.fields[fieldIdx] = row.firstChild.getAttribute('label');
  482.         pairs.values[fieldIdx] = row.lastChild.getAttribute('label');
  483.     }
  484.     //var str = pairs.join('|');
  485.     //afDebug('ripExtraFieldsFromTree():\n ['+str+']');
  486.     return pairs;
  487. }
  488.  
  489.  
  490. function populateDatacardList() {
  491.     afDebug('populateDatacardList()');
  492.  
  493.     // Clear the tree
  494.     var treeChildren = document.getElementById('datacardTreeChildren');
  495.     while (treeChildren.childNodes.length)
  496.         treeChildren.removeChild(treeChildren.childNodes[treeChildren.childNodes.length-1]);
  497.  
  498.     // Ensure at least one datacard exists
  499.     var defaultCard;
  500.     if (!datacardUtils.DatacardsExist()) {
  501.         defaultCard = 'MyData';
  502.         datacardUtils.CreateDatacard(defaultCard);
  503.         datacardUtils.SetDefaultDatacard(defaultCard);
  504.     } else {
  505.         defaultCard = datacardUtils.GetDefaultDatacard();
  506.     }
  507.  
  508.     // Repopulate the tree
  509.     var datacards = datacardUtils.GetDatacardList();
  510.     suppressEncryptedDatacards = false;
  511.     for (var i = 0; i < datacards.length; i++) {
  512.         // See if this datacard is encrypted
  513.         var file = datacardUtils.GetDatacardFileByName(datacards[i]);
  514.         var propDict =
  515.             afService.GetDatacardFieldsByType(
  516.                 file, nsIAutoFillService.FIELDTYPE_PROPERTY);
  517.         var isEncrypted = propDict.getValue('Encrypted');
  518.         isEncrypted.QueryInterface(Components.interfaces.nsIPrefLocalizedString);
  519.         isEncrypted = (isEncrypted.toString() == '1');
  520.         if (isEncrypted && !suppressEncryptedDatacards) {
  521.             //if (suppressEncryptedDatacards) continue;
  522.             // This datacard is encrypted, so try loading a datafield so
  523.             // that the user gets prompted to enter master password.
  524.             try {
  525.                 afDebug(' - before encrypt check');
  526.                 afService.PokeEncryptedDatacard(file);
  527.                 suppressEncryptedDatacards = false;
  528.                 afDebug(' - after encrypt check');
  529.             } catch (ex) {
  530.                 afDebug(' - skipping encrypted');
  531.                 suppressEncryptedDatacards = true;
  532.             }
  533.         }
  534.         var cell = document.createElement("treecell");
  535.         cell.setAttribute('label', datacards[i]);
  536.         var cellProps = new Array();
  537.         if (datacards[i] == defaultCard) {
  538.             dump(' - marking default tree item: '+defaultCard+'\n');
  539.             cellProps[cellProps.length] = 'default';
  540.         }
  541.         if (isEncrypted && suppressEncryptedDatacards) {
  542.             cellProps[cellProps.length] = 'disabled';
  543.             cell.setAttribute('disabled', 'true');
  544.         }
  545.         cell.setAttribute('properties', cellProps.join(','));
  546.         var row = document.createElement("treerow");    
  547.         row.appendChild(cell);
  548.         var item = document.createElement("treeitem");
  549.         item.setAttribute('datacard', datacards[i]);
  550.         item.appendChild(row);
  551.         treeChildren.appendChild(item);
  552.     }
  553.  
  554.     // Since there are items in the list(s), the Remove All button
  555.     // should be enabled
  556.     document.getElementById('datacardRemoveAllButton').removeAttribute('disabled');
  557.     
  558.     // Always select the first datacard
  559.     document.getElementById('datacardTree').view.selection.select(0);
  560. }
  561.  
  562.  
  563. function populateBlacklist() {
  564.     afDebug('populateBlacklist()');
  565.  
  566.     // Clear the tree
  567.     var treeChildren = document.getElementById('datacardRejectsTreeChildren');
  568.     while (treeChildren.childNodes.length)
  569.         treeChildren.removeChild(treeChildren.childNodes[treeChildren.childNodes.length-1]);
  570.  
  571.     // Repopulate the tree
  572.     var blacklist = datacardUtils.GetBlacklistedSites();
  573.     for (var i = 0; i < blacklist.length; i++) {
  574.         var cell = document.createElement("treecell");
  575.         cell.setAttribute('label', blacklist[i]);
  576.         var row = document.createElement("treerow");    
  577.         row.appendChild(cell)
  578.         var item = document.createElement("treeitem");
  579.         item.setAttribute('host', blacklist[i]);
  580.         item.appendChild(row);
  581.         treeChildren.appendChild(item);    
  582.     }
  583. }
  584.  
  585.  
  586. function enableId(elmId) { document.getElementById(elmId).removeAttribute('disabled'); }
  587. function disableId(elmId) { document.getElementById(elmId).setAttribute('disabled','true'); }
  588.  
  589. const fickleFields = [ 'datacardAutofillList', 'dcProtect', 'dcSetAsDefaultBtn',
  590.                        'Title', 'FirstName', 'LastName', 'EmailAddr',
  591.                        'PhoneNum', 'WorkPhoneNum', 'PriAddr', 'AltAddr',
  592.                        'StrAddr', 'StrAddr2', 'City', 'State', 'countryList',
  593.                        'ZipCode', 'CardName', 'CardNumber', 'cardtypeList',
  594.                        'monthList', 'yearList', 'advancedTree' ];
  595.  
  596. function enableDatacardForm() {
  597.     afDebug('enableDatacardForm()');
  598.     for (var i = 0; i < fickleFields.length; i++) {
  599.         enableId(fickleFields[i]);
  600.     }
  601. }
  602.  
  603. function disableDatacardForm() {
  604.     afDebug('disableDatacardForm()');
  605.     for (var i = 0; i < fickleFields.length; i++) {
  606.         disableId(fickleFields[i]);
  607.         document.getElementById(fickleFields[i]).value = '';
  608.     }
  609.     document.getElementById('dcProtect').checked = true;
  610. }
  611.  
  612.  
  613. // Make sure the SelectSignon and SelectReject functions are
  614. // mutually exclusive
  615. var selectionMutex = 0;
  616.  
  617.  
  618. function SelectSignon() {
  619.     // Respect mutex
  620.     if (selectionMutex) return;
  621.     // Establish mutex
  622.     selectionMutex++;
  623.  
  624.     afDebug('SelectSignon()');
  625.  
  626.     // If there is NO current selection, bail
  627.     var tree = document.getElementById('datacardTree');
  628.     if (tree.currentIndex == -1) {
  629.         selectionMutex--;
  630.         return;
  631.     }
  632.  
  633.     // Clear the selection in the blacklist tree
  634.     document.getElementById('datacardRejectsTree').view.selection.clearSelection();
  635.  
  636.     // Make the details tabs visible
  637.     document.getElementById('datacardDetails-deck').selectedIndex = 1;
  638.  
  639.     // Save the previous datacard, if any
  640.     // (Note: this is not ideal... it means that the prefs Cancel button
  641.     // won't actually cancel the datacard change)
  642.     if (currentDatacard) saveDatacard();
  643.  
  644.     // Load the selected datacard
  645.     var item = tree.contentView.getItemAtIndex(tree.currentIndex);
  646.     var datacard = item.getAttribute('datacard');
  647.     afDebug('SelectSignon: attempt to load datacard: '+datacard);
  648.     currentDatacard = datacard;
  649.     currentDatacardFile = datacardUtils.GetDatacardFileByName(datacard);
  650.     suppressEncryptedDatacards = false;
  651.     var propDict =
  652.         afService.GetDatacardFieldsByType(
  653.             currentDatacardFile, nsIAutoFillService.FIELDTYPE_PROPERTY);
  654.     currentDatacardIsEncrypted = (getDictionaryValue(propDict, 'Encrypted') == '1');
  655.     var isMPset = IsMasterPasswordSet();
  656.     if (!isMPset) {
  657.         suppressEncryptedDatacards = false;
  658.     } else if (currentDatacardIsEncrypted) {
  659.         afDebug(" - it's ENCRYPTED");
  660.         try {
  661.             afService.PokeEncryptedDatacard(currentDatacardFile);
  662.             suppressEncryptedDatacards = false;
  663.         } catch (ex) {
  664.             afDebug(' - CAUGHT: '+ex);
  665.             suppressEncryptedDatacards = true;
  666.         }
  667.     }
  668.  
  669.     // Show datacard name
  670.     afDebug(' - setting datacard name label: '+datacard);
  671.     dcardLabel.value = datacard;
  672.  
  673.     if (suppressEncryptedDatacards) {
  674.         disableDatacardForm();
  675.         selectionMutex--;
  676.         return;
  677.     }
  678.     enableDatacardForm();
  679.  
  680.     readDatacard();
  681.  
  682.     // The 'Set as Default' button should only be enabled if this datacard
  683.     // is not already the default
  684.     if (datacard == datacardUtils.GetDefaultDatacard())
  685.         disableId('dcSetAsDefaultBtn');
  686.     else
  687.         enableId('dcSetAsDefaultBtn');
  688.  
  689.     // The Remove buttons should be enabled unless the selected datacard is
  690.     // the *only* datacard
  691.     if (tree.view.rowCount == 1) {
  692.         disableId('datacardRemoveButton');
  693.         disableId('datacardRemoveAllButton');
  694.     } else {
  695.         enableId('datacardRemoveButton');
  696.         enableId('datacardRemoveAllButton');
  697.     }
  698.  
  699.     // Release mutex
  700.     selectionMutex--;
  701. }
  702.  
  703.  
  704. function SelectReject() {
  705.     // Respect mutex
  706.     if (selectionMutex) return;
  707.     // Establish mutex
  708.     selectionMutex++;
  709.  
  710.     afDebug('SelectReject()');
  711.  
  712.     // Hide the datacard details tabs
  713.     document.getElementById('datacardDetails-deck').selectedIndex = 0;
  714.  
  715.     // Clear the selection in the datacards tree
  716.     document.getElementById('datacardTree').view.selection.clearSelection();
  717.  
  718.     // Since an item is selected, the Remove buttons should be enabled
  719.     document.getElementById('datacardRemoveButton').removeAttribute('disabled');
  720.     document.getElementById('datacardRemoveAllButton').removeAttribute('disabled');
  721.  
  722.     // Release mutex
  723.     selectionMutex--;
  724. }
  725.  
  726.  
  727. function datacardAddButton() {
  728.     window.openDialog("chrome://mozapps/content/autofill/addRenameDatacardDialog.xul",
  729.                       "_blank", "chrome,modal,resizable=no");
  730. }
  731.  
  732.  
  733. function callback_NewDatacardName(label) {
  734.     var name = datacardUtils.GenerateFilenamePrefixForDatacard(label);
  735.     datacardUtils.CreateDatacard(name);
  736.     populateDatacardList();
  737. }
  738.  
  739.  
  740. function datacardRemoveButton() {
  741.     // Figure out which tree has the selected item
  742.     var datacardTree = document.getElementById('datacardTree');
  743.     if (datacardTree.view.selection.count)
  744.         DeleteDatacard();
  745.     else
  746.         DeleteBlacklistSite();
  747. }
  748.  
  749.  
  750. function datacardRemoveAllButton() {
  751.     // Figure out which tree has the selected item
  752.     var datacardTree = document.getElementById('datacardTree');
  753.     if (datacardTree.view.selection.count)
  754.         DeleteAllDatacards();
  755.     else
  756.         ClearBlacklist();
  757. }
  758.  
  759.  
  760. function DeleteDatacard() {
  761.     // Delete the datacard
  762.     //var tree = document.getElementById('datacardTree');
  763.     //var item = tree.contentView.getItemAtIndex(tree.currentIndex);
  764.     //var datacard = item.getAttribute('datacard');
  765.     datacardUtils.DeleteDatacard(currentDatacard);
  766.     currentDatacard = null;
  767.     currentDatacardFile = null;
  768.     currentDatacardIsEncrypted = false;
  769.  
  770.     // Since the item that was selected is now gone, and we no longer know
  771.     // if anything is selected, the Remove buttons should be disabled
  772.     document.getElementById('datacardRemoveButton').setAttribute('disabled', 'true');
  773.     document.getElementById('datacardRemoveAllButton').setAttribute('disabled', 'true');
  774.  
  775.     // Refresh the list
  776.     populateDatacardList();
  777. }
  778.  
  779.  
  780. function DeleteAllDatacards(onlyDoEncrypted) {
  781.     datacardUtils.DeleteAllDatacards(onlyDoEncrypted);
  782.     document.getElementById('datacardRemoveButton').setAttribute('disabled', 'true');
  783.     document.getElementById('datacardRemoveAllButton').setAttribute('disabled', 'true');
  784.     currentDatacard = null;
  785.     currentDatacardFile = null;
  786.     currentDatacardIsEncrypted = false;
  787.     populateDatacardList();
  788. }
  789.  
  790.  
  791. function DeleteBlacklistSite() {
  792.     // Unlist the site
  793.     var tree = document.getElementById('datacardRejectsTree');
  794.     var item = tree.contentView.getItemAtIndex(tree.currentIndex);
  795.     var host = item.getAttribute('host');
  796.     datacardUtils.UnlistSite(host);
  797.  
  798.     // Since the item that was selected is now gone, and we no longer know
  799.     // if anything is selected, the Remove buttons should be disabled
  800.     document.getElementById('datacardRemoveButton').setAttribute('disabled', 'true');
  801.     document.getElementById('datacardRemoveAllButton').setAttribute('disabled', 'true');
  802.  
  803.     // Refresh the list
  804.     populateBlacklist();
  805. }
  806.  
  807.  
  808. function ClearBlacklist() {
  809.     var blacklist = datacardUtils.GetBlacklistedSites();
  810.     for (var i = 0; i < blacklist.length; i++) {
  811.         datacardUtils.UnlistSite(blacklist[i]);
  812.     }
  813.     document.getElementById('datacardRemoveButton').setAttribute('disabled', 'true');
  814.     document.getElementById('datacardRemoveAllButton').setAttribute('disabled', 'true');
  815.     populateBlacklist();
  816. }
  817.  
  818.  
  819. function SetDatacardAsDefault() {
  820.     afDebug('SetDatacardAsDefault()');
  821.     var tree = document.getElementById('datacardTree');
  822.     //var item = tree.contentView.getItemAtIndex(tree.currentIndex);
  823.     //var datacard = item.getAttribute('datacard');
  824.     datacardUtils.SetDefaultDatacard(currentDatacard);
  825.     var index = tree.currentIndex;
  826.     populateDatacardList();
  827.     tree.view.selection.select(index);
  828. }
  829.  
  830.  
  831. /**
  832.  * Handle Ok event
  833.  **/
  834. function onAutoFillOK()
  835. {
  836.     //afDebug("onAutoFillOK()");
  837.     // if this is not the page displayed, don't continue
  838.     var theTabBox = document.getElementById("autofilltabbox");
  839.      if (!theTabBox || theTabBox.selectedTab.id != "formfilltab")
  840.          return;
  841.  
  842.     if (reScanForm()) {
  843.         afDebug("rescan:"+reScanForm()+"\n");
  844.         saveDatacard();
  845.     }
  846. }
  847.  
  848.  
  849. function setTab()
  850. {
  851.   kPREFContractID = "@mozilla.org/preferences-service;1";
  852.   kPBIID = Components.interfaces.nsIPrefBranch;
  853.  
  854.   var prefServ  = Components.classes[kPREFContractID].getService(kPBIID);
  855.   var selectedTabId = prefServ.getCharPref('browser.preferences.formfillORpasscard');
  856.   dump("THE SELECTED TAB IS: " +selectedTabId+ "\n");
  857.   var theTabBox = document.getElementById("autofilltabbox");
  858.   var theTab = document.getElementById(selectedTabId);
  859.   theTabBox.selectedTab = theTab;
  860. }
  861.  
  862. var gCurrentRadioSelection = null;
  863. function afillRadioChange(radiobutton)
  864. {
  865.     dump("IN afillRadioChange: \n");
  866.     if ((radiobutton.getAttribute("id") == "PriAddr")
  867.         && (radiobutton.getAttribute("selected") == "true"))
  868.     {
  869.         if (gCurrentRadioSelection && (gCurrentRadioSelection == 'PriAddr')) {
  870.             // do nothing... this radio is already selected
  871.         } else {
  872.             priSelected();
  873.             gCurrentRadioSelection = 'PriAddr';
  874.         }
  875.     } else {
  876.         if (gCurrentRadioSelection && (gCurrentRadioSelection == 'AltAddr')) {
  877.             // do nothing... this radio is already selected
  878.         } else {
  879.             altSelected();
  880.             gCurrentRadioSelection = 'AltAddr';
  881.         }
  882.     }    
  883. }
  884.  
  885. function priSelected()
  886. { dump("ENTERING PRIMAY SELECTGED \n");
  887.   if (removeAllFlag == true && clearFormDataCount < 2)
  888.      {
  889.     dump("PRIMAY WARNING CLEARING ALL DATA");
  890.     refreshFormData();
  891.     processPrimaryData();
  892.     removeAllFlag = false;
  893.   clearFormDataCount = clearFormDataCount + 1;
  894.      }
  895.     else                    
  896.      {    
  897.      processPrimaryData();
  898.         
  899.     removeAllFlag = false;
  900.     clearFormDataCount = 0;
  901.     }
  902.     saveAddressData();
  903.          
  904. } //END priSelected
  905.  
  906. function altSelected()
  907. {
  908. dump("ENTERING ALTERNATE SELECTGED \n");
  909.  if (removeAllFlag == true && clearFormDataCount < 2)
  910.      {    
  911.      dump("ALTERNATE-WARNING CLEARING ALL DATA");
  912.     refreshFormData();
  913.     processAlternateData();    
  914.     removeAllFlag = false;
  915.            clearFormDataCount = clearFormDataCount + 1;
  916.      }
  917.     else        
  918.         {
  919.      processAlternateData();           
  920.      removeAllFlag = false;
  921.   clearFormDataCount = 0;
  922.        }
  923.        saveAddressData();
  924. } // function altSelected
  925.  
  926. function creditCardPassword()
  927. {
  928.    window.openDialog("chrome://mozapps/content/autofill/CCardPwdDialog.xul", "_blank",
  929.                       "chrome,dialog,modal");
  930. }
  931.  
  932. function checkPWD()
  933. {    
  934.     try
  935.     {
  936.         var propDict =
  937.             afService.GetDatacardFieldsByType(
  938.                 currentDatacardFile, nsIAutoFillService.FIELDTYPE_PROPERTY);
  939.         var cc_passwordCheck_val = new  Object();
  940.         cc_passwordCheck_val.value = getDictionaryValue(propDict, 'cc_password');    
  941.         dump("checking password "+cc_passwordCheck_val.value+"\n");
  942.     
  943.         if (cc_passwordCheck_val.value == "")
  944.         {    dump( "PASSWORD OR FILES NULL \n");
  945.             window.openDialog("chrome://mozapps/content/autofill/CCard.xul", "_blank","chrome,dialog,modal");
  946.         }
  947.         else
  948.         {    dump( "creditCardPassword");
  949.             creditCardPassword();
  950.         }
  951.     } //END TRY
  952.     catch (e)
  953.     {    dump( "checkPWD has FAILED\n");
  954.         window.openDialog("chrome://mozapps/content/autofill/CCard.xul", "_blank","chrome,dialog,modal");
  955.     }
  956.  
  957. }
  958.  
  959. function setPromptStatePref()
  960. { kPREFContractID = "@mozilla.org/preferences-service;1";
  961.   kPBIID = Components.interfaces.nsIPrefBranch;
  962.   var gPrefService  = Components.classes[kPREFContractID].getService(kPBIID);        
  963.     var formfilluser = document.getElementById("formfill_prompter").checked;
  964.     //alert (formfilluser);
  965.     // FALSE = CHECKED XUL ELEMENT and TRUE = UNCHECKED XUL ELEMENT
  966.     var userprompt;
  967.     if (!formfilluser)
  968.     userprompt = gPrefService.setBoolPref("formfill.user.prompt",true);
  969.     else
  970.     userprompt = gPrefService.setBoolPref("formfill.user.prompt",false);
  971.  
  972. }
  973.  
  974. function OnTabSelect()
  975. {
  976.     var theTabs = document.getElementById("formfill-passcard-tabs");
  977.     if (!theTabs)
  978.         return;
  979.         
  980.     var theTab = theTabs.selectedItem;
  981.     if (!theTab)
  982.         return;
  983.         
  984.     /* TODO JVL - remove if no longer necessary
  985.     if (theTab.id == "passcardtab")
  986.     {
  987.         // if the user cannot access the passcard panel due to
  988.         // an incorrect password, select the form fill panel
  989.         if (!SelectPasscardPanel(theTab))
  990.         {
  991.             theTabs.advanceSelectedTab(-1);
  992.         }
  993.     }
  994.     */
  995.     
  996.     // HACK: don't save the preference until the page has been fully initialized
  997.     //         because OnTabSelect() will be called before Startup()
  998.     if (gTabSelectInit)
  999.     {
  1000.         // set as the last visted tab
  1001.         var prefServ  = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  1002.  
  1003.         // ensure we retrieve the current tab
  1004.         theTab = theTabs.selectedItem;
  1005.         if (theTab)
  1006.           prefServ.setCharPref('browser.preferences.formfillORpasscard', theTab.id);
  1007.     }
  1008. }
  1009.  
  1010. function removeFormFillData()
  1011. {                                
  1012.     removeAllFlag = true;
  1013.     clearFormFillData();
  1014.     saveDatacard(); //clear buffer
  1015.  
  1016.     saveDatacardField('cc_name', '');
  1017.     saveDatacardField('cc_nunber', '');
  1018.     saveDatacardField('cc_type', '');
  1019.     saveDatacardField('cc_expire_month', '');
  1020.     saveDatacardField('cc_expire_year', '');
  1021.     saveDatacardField('cc_password', '');
  1022. }
  1023.  
  1024. function clearFormFillData()
  1025. {
  1026.     document.getElementById("Title").value = "";
  1027.     document.getElementById("FirstName").value = "";
  1028.     document.getElementById("LastName").value = "";
  1029.     document.getElementById("EmailAddr").value = "";
  1030.     document.getElementById("PhoneNum").value = "";    
  1031.     document.getElementById("WorkPhoneNum").value = "";
  1032.     
  1033.     document.getElementById("StrAddr").value = "";
  1034.     document.getElementById("StrAddr2").value = "";
  1035.     document.getElementById("City").value = "";
  1036.     document.getElementById("State").value = "";
  1037.     document.getElementById("ZipCode").value = "";
  1038.     document.getElementById("countryList").value = "United States";
  1039.     
  1040.     alt_street_val.value="";
  1041.     alt_street2_val.value="";
  1042.     alt_city_val.value="";
  1043.     alt_zip_val.value="";
  1044.     alt_state_val.value="";
  1045.     alt_country_val.value="";
  1046.     street.value="";
  1047.     street_val.value="";;
  1048.     street2.value="";
  1049.     street2_val.value="";
  1050.     city.value="";
  1051.     city_val.value="";;
  1052.     zip.value="";
  1053.     zip_val.value="";
  1054.     state.value="";
  1055.     state_val.value="";
  1056.     country.value="";
  1057.     country_val.value="";
  1058. }
  1059.  
  1060.  
  1061. function reScanForm()
  1062. {
  1063.     FFtitle=document.getElementById("Title");
  1064.     if (FFtitle.value != "") return true;
  1065.     
  1066.     fname=document.getElementById("FirstName");
  1067.     if (fname.value != "") return true;
  1068.     
  1069.     lname=document.getElementById("LastName");
  1070.     if (lname.value != "") return true;
  1071.     
  1072.     email=document.getElementById("EmailAddr");
  1073.     if (email.value != "") return true;
  1074.     
  1075.     phone=document.getElementById("PhoneNum");
  1076.     if (phone.value != "") return true;
  1077.     
  1078.     workphone=document.getElementById("WorkPhoneNum");
  1079.     if (workphone.value != "") return true;
  1080.     
  1081.     street=document.getElementById("StrAddr");
  1082.     if (street.value != "") return true;
  1083.     
  1084.     street2=document.getElementById("StrAddr2");
  1085.     if (street2.value != "") return true;
  1086.     
  1087.     city = document.getElementById("City");
  1088.     if (city.value != "") return true;
  1089.     
  1090.     state=document.getElementById("State");
  1091.     if (state.value != "") return true;
  1092.     
  1093.     zip=document.getElementById("ZipCode");
  1094.     if (zip.value != "") return true;
  1095.     
  1096.     country=document.getElementById("countryList");
  1097.     if (country.value != "") return true;
  1098.     
  1099.     dcardLabel = document.getElementById("datacardName");
  1100.     if (dcardLabel.value != "") return true;
  1101.     
  1102.     datacardAutofillList = document.getElementById("datacardAutofillList");
  1103.     if (datacardAutofillList.value != "") return true;
  1104.  
  1105.     dcardProtect = document.getElementById("dcProtect");
  1106.     if (dcardProtect.value != "") return true;
  1107.  
  1108.     return false;
  1109. }
  1110.  
  1111. function refreshFormData()
  1112. {
  1113.     street=document.getElementById("StrAddr");
  1114.     street2=document.getElementById("StrAddr2");
  1115.     city = document.getElementById("City");
  1116.     state=document.getElementById("State");
  1117.     zip=document.getElementById("ZipCode");
  1118.     country=document.getElementById("countryList");
  1119. }
  1120.  
  1121. function processPrimaryData()
  1122. {         //save alt address values
  1123.           alt_street_val.value=street.value;
  1124.           alt_street2_val.value=street2.value;
  1125.           alt_city_val.value=city.value;
  1126.           alt_zip_val.value=zip.value;
  1127.           alt_state_val.value=state.value;
  1128.           alt_country_val.value=country.value;
  1129.           //update UI
  1130.           street.value=street_val.value;
  1131.           street2.value=street2_val.value;
  1132.           city.value=city_val.value;
  1133.           zip.value=zip_val.value;
  1134.           state.value=state_val.value;
  1135.           country.value=country_val.value;
  1136. }
  1137. function processAlternateData()
  1138. { //save pri address values
  1139.       street_val.value=street.value;
  1140.       street2_val.value=street2.value;
  1141.       city_val.value=city.value;
  1142.       zip_val.value=zip.value;
  1143.       state_val.value=state.value;
  1144.       country_val.value=country.value;
  1145.       //update UI
  1146.       street.value=alt_street_val.value;
  1147.       street2.value=alt_street2_val.value;
  1148.       city.value=alt_city_val.value;
  1149.       zip.value=alt_zip_val.value;
  1150.       state.value=alt_state_val.value;
  1151.       country.value=alt_country_val.value;
  1152. }
  1153.  
  1154. function saveAddressData()
  1155. {
  1156.     FFtitle_val.value = FFtitle.value;
  1157.     fname_val.value = fname.value;
  1158.     lname_val.value = lname.value;
  1159.     email_val.value = email.value;
  1160.     phone_val.value = phone.value;
  1161.     workphone_val.value = workphone.value;
  1162.  
  1163.     var vtrue="true";
  1164.     var vfalse="false";
  1165.  
  1166.     var pri=primary.selected;
  1167.     if(pri.toString()==vtrue.toString())
  1168.     {
  1169.       street_val.value=street.value;
  1170.       street2_val.value = street2.value;
  1171.       city_val.value= city.value;
  1172.       zip_val.value = zip.value;
  1173.       state_val.value = state.value;
  1174.       country_val.value = country.value;
  1175.     }
  1176.     else
  1177.     {
  1178.       alt_street_val.value=street.value;
  1179.       alt_street2_val.value=street2.value;
  1180.       alt_city_val.value= city.value;
  1181.       alt_zip_val.value = zip.value;
  1182.       alt_state_val.value = state.value;
  1183.      alt_country_val.value = country.value;
  1184.     }
  1185.     primary=document.getElementById("PriAddr");
  1186.     alternative=document.getElementById("AltAddr");
  1187.  
  1188.     saveDatacardField('title', FFtitle_val.value);
  1189.     saveDatacardField('firstname', fname_val.value);
  1190.     saveDatacardField('lastname', lname_val.value);
  1191.     saveDatacardField('email', email_val.value);
  1192.     saveDatacardField('phone', phone_val.value);
  1193.     saveDatacardField('work_phone', workphone_val.value);
  1194.  
  1195.     saveDatacardField('street', street_val.value);
  1196.     saveDatacardField('street2', street2_val.value);
  1197.     saveDatacardField('city', city_val.value);
  1198.     saveDatacardField('zip', zip_val.value);
  1199.     saveDatacardField('state', state_val.value);
  1200.     saveDatacardField('country', country_val.value);
  1201.  
  1202.     saveDatacardField('alt_street', alt_street_val.value);
  1203.     saveDatacardField('alt_street2', alt_street2_val.value);
  1204.     saveDatacardField('alt_city', alt_city_val.value);
  1205.     saveDatacardField('alt_zip', alt_zip_val.value);
  1206.     saveDatacardField('alt_state', alt_state_val.value);
  1207.     saveDatacardField('alt_country', alt_country_val.value);
  1208. }
  1209.  
  1210. //the following are prototype for now...
  1211. function OndatacardPrefTabSelect() {}
  1212.  
  1213. function dataCardAutoSaveChange()
  1214. {
  1215.   dump('*** changing prompt checkbox\n');
  1216.   var bAutoSave = document.getElementById('dcAutosave').checked;
  1217.   document.getElementById('dcAutosavePrompt').disabled = (!bAutoSave);
  1218. }
  1219.  
  1220. //-------END AUTOFILL/FORMFILL--------------------
  1221.