home *** CD-ROM | disk | FTP | other *** search
/ Mundo do CD-ROM 118 / cdrom118.iso / internet / webaroo / WebarooSetup.exe / Webaroo.msi / _B9CD756C0FBF423184DEEA3BE756F224 < prev    next >
Encoding:
Text File  |  2006-03-07  |  31.2 KB  |  933 lines

  1. /** 
  2.    Copyright (c) 2005, Brad Neuberg, bkn3@columbia.edu
  3.    http://codinginparadise.org
  4.    
  5.    Permission is hereby granted, free of charge, to any person obtaining 
  6.    a copy of this software and associated documentation files (the "Software"), 
  7.    to deal in the Software without restriction, including without limitation 
  8.    the rights to use, copy, modify, merge, publish, distribute, sublicense, 
  9.    and/or sell copies of the Software, and to permit persons to whom the 
  10.    Software is furnished to do so, subject to the following conditions:
  11.    
  12.    The above copyright notice and this permission notice shall be 
  13.    included in all copies or substantial portions of the Software.
  14.    
  15.    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  16.    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  17.    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
  18.    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
  19.    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT 
  20.    OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 
  21.    THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22.    
  23.    The JSON class near the end of this file is
  24.    Copyright 2005, JSON.org
  25. */
  26.  
  27. /** An object that provides DHTML history, history data, and bookmarking 
  28.     for AJAX applications. */
  29. window.dhtmlHistory = {
  30.    /** Initializes our DHTML history. You should
  31.        call this after the page is finished loading. */
  32.    /** public */ initialize: function() {
  33.       // only Internet Explorer needs to be explicitly initialized;
  34.       // other browsers don't have its particular behaviors.
  35.       // Basicly, IE doesn't autofill form data until the page
  36.       // is finished loading, which means historyStorage won't
  37.       // work until onload has been fired.
  38.       if (this.isInternetExplorer() == false) {
  39.          return;
  40.       }
  41.          
  42.       // if this is the first time this page has loaded...
  43.       if (historyStorage.hasKey("DhtmlHistory_pageLoaded") == false) {
  44.          this.fireOnNewListener = false;
  45.          this.firstLoad = true;
  46.          historyStorage.put("DhtmlHistory_pageLoaded", true);
  47.       }
  48.       // else if this is a fake onload event
  49.       else {
  50.          this.fireOnNewListener = true;
  51.          this.firstLoad = false;   
  52.       }
  53.    },
  54.              
  55.    /** Adds a history change listener. Note that
  56.        only one listener is supported at this
  57.        time. */
  58.    /** public */ addListener: function(callback) {
  59.       this.listener = callback;
  60.       
  61.       // if the page was just loaded and we
  62.       // should not ignore it, fire an event
  63.       // to our new listener now
  64.       if (this.fireOnNewListener == true) {
  65.          this.fireHistoryEvent(this.currentLocation);
  66.          this.fireOnNewListener = false;
  67.       }
  68.    },
  69.    
  70.    /** public */ add: function(newLocation, historyData) {
  71.       // most browsers require that we wait a certain amount of time before changing the
  72.       // location, such as 200 milliseconds; rather than forcing external callers to use
  73.       // window.setTimeout to account for this to prevent bugs, we internally handle this
  74.       // detail by using a 'currentWaitTime' variable and have requests wait in line
  75.       var self = this;
  76.       var addImpl = function() {
  77.          // indicate that the current wait time is now less
  78.          if (self.currentWaitTime > 0)
  79.             self.currentWaitTime = self.currentWaitTime - self.WAIT_TIME;
  80.             
  81.          // remove any leading hash symbols on newLocation
  82.          newLocation = self.removeHash(newLocation);
  83.          
  84.          // IE has a strange bug; if the newLocation
  85.          // is the same as _any_ preexisting id in the
  86.          // document, then the history action gets recorded
  87.          // twice; throw a programmer exception if there is
  88.          // an element with this ID
  89.          var idCheck = document.getElementById(newLocation);
  90.          if (idCheck != undefined || idCheck != null) {
  91.             var message = 
  92.                "Exception: History locations can not have "
  93.                + "the same value as _any_ id's "
  94.                + "that might be in the document, "
  95.                + "due to a bug in Internet "
  96.                + "Explorer; please ask the "
  97.                + "developer to choose a history "
  98.                + "location that does not match "
  99.                + "any HTML id's in this "
  100.                + "document. The following ID "
  101.                + "is already taken and can not "
  102.                + "be a location: " 
  103.                + newLocation;
  104.                
  105.             throw message; 
  106.          }
  107.          
  108.          // store the history data into history storage
  109.          historyStorage.put(newLocation, historyData);
  110.          
  111.          // indicate to the browser to ignore this upcomming 
  112.          // location change
  113.          self.ignoreLocationChange = true;
  114.  
  115.          // indicate to IE that this is an atomic location change
  116.          // block
  117.          this.ieAtomicLocationChange = true;
  118.                  
  119.          // save this as our current location
  120.          self.currentLocation = newLocation;
  121.          
  122.          // change the browser location
  123.          window.location.hash = newLocation;
  124.          
  125.          // change the hidden iframe's location if on IE
  126.          if (self.isInternetExplorer())
  127.             self.iframe.src = "/webaroo/e29f1fe6/blank?" + newLocation;
  128.             
  129.          // end of atomic location change block
  130.          // for IE
  131.          this.ieAtomicLocationChange = false;
  132.       };
  133.  
  134.       // now execute this add request after waiting a certain amount of time, so as to
  135.       // queue up requests
  136.       window.setTimeout(addImpl, this.currentWaitTime);
  137.    
  138.       // indicate that the next request will have to wait for awhile
  139.       this.currentWaitTime = this.currentWaitTime + this.WAIT_TIME;
  140.    },
  141.    
  142.    /** public */ isFirstLoad: function() {
  143.       if (this.firstLoad == true) {
  144.          return true;
  145.       }
  146.       else {
  147.          return false;
  148.       }
  149.    },
  150.    
  151.    /** public */ isInternational: function() {
  152.       return false;
  153.    },
  154.    
  155.    /** public */ getVersion: function() {
  156.       return "0.05";
  157.    },
  158.    
  159.    /** Gets the current hash value that is in the browser's
  160.        location bar, removing leading # symbols if they are present. */
  161.    /** public */ getCurrentLocation: function() {
  162.     var currentLocation = "";
  163.     var url = window.location.href;
  164.     if(url.indexOf("#") > -1) 
  165.     {
  166.         var url_elements = url.split("#");
  167.         currentLocation = url_elements[url_elements.length-1];
  168.     } 
  169.       //var currentLocation = this.removeHash(window.location.hash);
  170.          
  171.       return currentLocation;
  172.    },
  173.    
  174.    
  175.    
  176.    
  177.    
  178.    /** Our current hash location, without the "#" symbol. */
  179.    /** private */ currentLocation: null,
  180.    
  181.    /** Our history change listener. */
  182.    /** private */ listener: null,
  183.    
  184.    /** A hidden IFrame we use in Internet Explorer to detect history
  185.        changes. */
  186.    /** private */ iframe: null,
  187.    
  188.    /** Indicates to the browser whether to ignore location changes. */
  189.    /** private */ ignoreLocationChange: null,
  190.  
  191.    /** The amount of time in milliseconds that we should wait between add requests. 
  192.        Firefox is okay with 200 ms, but Internet Explorer needs 400. */
  193.    /** private */ WAIT_TIME: 200,
  194.  
  195.    /** The amount of time in milliseconds an add request has to wait in line before being
  196.        run on a window.setTimeout. */
  197.    /** private */ currentWaitTime: 0,
  198.    
  199.    /** A flag that indicates that we should fire a history change event
  200.        when we are ready, i.e. after we are initialized and
  201.        we have a history change listener. This is needed due to 
  202.        an edge case in browsers other than Internet Explorer; if
  203.        you leave a page entirely then return, we must fire this
  204.        as a history change event. Unfortunately, we have lost
  205.        all references to listeners from earlier, because JavaScript
  206.        clears out. */
  207.    /** private */ fireOnNewListener: null,
  208.    
  209.    /** A variable that indicates whether this is the first time
  210.        this page has been loaded. If you go to a web page, leave
  211.        it for another one, and then return, the page's onload
  212.        listener fires again. We need a way to differentiate
  213.        between the first page load and subsequent ones.
  214.        This variable works hand in hand with the pageLoaded
  215.        variable we store into historyStorage.*/
  216.    /** private */ firstLoad: null,
  217.    
  218.    /** A variable to handle an important edge case in Internet
  219.        Explorer. In IE, if a user manually types an address into
  220.        their browser's location bar, we must intercept this by
  221.        continiously checking the location bar with an timer 
  222.        interval. However, if we manually change the location
  223.        bar ourselves programmatically, when using our hidden
  224.        iframe, we need to ignore these changes. Unfortunately,
  225.        these changes are not atomic, so we surround them with
  226.        the variable 'ieAtomicLocationChange', that if true,
  227.        means we are programmatically setting the location and
  228.        should ignore this atomic chunked change. */
  229.    /** private */ ieAtomicLocationChange: null,          
  230.    
  231.    /** Creates the DHTML history infrastructure. */
  232.    /** private */ create: function() {
  233.       // get our initial location
  234.       var initialHash = this.getCurrentLocation();
  235.       
  236.       // save this as our current location
  237.       this.currentLocation = initialHash;
  238.       
  239.       // write out a hidden iframe for IE and
  240.       // set the amount of time to wait between add() requests
  241.       if (this.isInternetExplorer()) {
  242.          document.write("<iframe style='border: 0px; width: 1px; "
  243.                                + "height: 1px; position: absolute; bottom: 0px; "
  244.                                + "right: 0px; visibility: visible;' "
  245.                                + "name='DhtmlHistoryFrame' id='DhtmlHistoryFrame' "
  246.                                + "src='/webaroo/e29f1fe6/blank?" + initialHash + "'>"
  247.                                + "</iframe>");
  248.          // wait 400 milliseconds between history
  249.          // updates on IE, versus 200 on Firefox
  250.          this.WAIT_TIME = 400;
  251.       }
  252.       
  253.       // add an unload listener for the page; this is
  254.       // needed for Firefox 1.5+ because this browser caches all
  255.       // dynamic updates to the page, which can break some of our 
  256.       // logic related to testing whether this is the first instance
  257.       // a page has loaded or whether it is being pulled from the cache
  258.       var self = this;
  259.       window.onunload = function() {
  260.          self.firstLoad = null;
  261.       };
  262.       
  263.       // determine if this is our first page load;
  264.       // for Internet Explorer, we do this in 
  265.       // this.iframeLoaded(), which is fired on
  266.       // page load. We do it there because
  267.       // we have no historyStorage at this point
  268.       // in IE, which only exists after the page
  269.       // is finished loading for that browser
  270.       if (this.isInternetExplorer() == false) {
  271.          if (historyStorage.hasKey("DhtmlHistory_pageLoaded") == false) {
  272.             this.ignoreLocationChange = true;
  273.             this.firstLoad = true;
  274.             historyStorage.put("DhtmlHistory_pageLoaded", true);
  275.          }
  276.          else {
  277.             // indicate that we want to pay attention
  278.             // to this location change
  279.             this.ignoreLocationChange = false;
  280.             // For browser's other than IE, fire
  281.             // a history change event; on IE,
  282.             // the event will be thrown automatically
  283.             // when it's hidden iframe reloads
  284.             // on page load.
  285.             // Unfortunately, we don't have any 
  286.             // listeners yet; indicate that we want
  287.             // to fire an event when a listener
  288.             // is added.
  289.             this.fireOnNewListener = true;
  290.          }
  291.       }
  292.       else { // Internet Explorer
  293.          // the iframe will get loaded on page
  294.          // load, and we want to ignore this fact
  295.          this.ignoreLocationChange = true;
  296.       }
  297.       
  298.       if (this.isInternetExplorer()) {
  299.             this.iframe = document.getElementById("DhtmlHistoryFrame");
  300.       }                                                              
  301.  
  302.       // other browsers can use a location handler that checks
  303.       // at regular intervals as their primary mechanism;
  304.       // we use it for Internet Explorer as well to handle
  305.       // an important edge case; see checkLocation() for
  306.       // details
  307.       var self = this;
  308.       var locationHandler = function() {
  309.          self.checkLocation();
  310.       };
  311.       setInterval(locationHandler, 100);
  312.    },
  313.    
  314.    /** Notify the listener of new history changes. */
  315.    /** private */ fireHistoryEvent: function(newHash) {
  316.       // extract the value from our history storage for
  317.       // this hash
  318.       var historyData = historyStorage.get(newHash);
  319.  
  320.       // call our listener      
  321.       this.listener.call(null, newHash, historyData);
  322.    },
  323.    
  324.    /** Sees if the browsers has changed location.  This is the primary history mechanism
  325.        for Firefox. For Internet Explorer, we use this to handle an important edge case:
  326.        if a user manually types in a new hash value into their Internet Explorer location
  327.        bar and press enter, we want to intercept this and notify any history listener. */
  328.    /** private */ checkLocation: function() {
  329.       // ignore any location changes that we made ourselves
  330.       // for browsers other than Internet Explorer
  331.       if (this.isInternetExplorer() == false
  332.          && this.ignoreLocationChange == true) {
  333.          this.ignoreLocationChange = false;
  334.          return;
  335.       }
  336.       
  337.       // if we are dealing with Internet Explorer
  338.       // and we are in the middle of making a location
  339.       // change from an iframe, ignore it
  340.       if (this.isInternetExplorer() == false
  341.           && this.ieAtomicLocationChange == true) {
  342.          return;
  343.       }
  344.       
  345.       // get hash location
  346.       var hash = this.getCurrentLocation();
  347.       
  348.       // see if there has been a change
  349.       if (hash == this.currentLocation)
  350.          return;
  351.          
  352.       // on Internet Explorer, we need to intercept users manually
  353.       // entering locations into the browser; we do this by comparing
  354.       // the browsers location against the iframes location; if they
  355.       // differ, we are dealing with a manual event and need to
  356.       // place it inside our history, otherwise we can return
  357.       this.ieAtomicLocationChange = true;
  358.       
  359.       if (this.isInternetExplorer()
  360.           && this.getIFrameHash() != hash) {
  361.          this.iframe.src = "/webaroo/e29f1fe6/blank?" + hash;
  362.       }
  363.       else if (this.isInternetExplorer()) {
  364.          // the iframe is unchanged
  365.          return;
  366.       }
  367.          
  368.       // save this new location
  369.       this.currentLocation = hash;
  370.       
  371.       this.ieAtomicLocationChange = false;
  372.       
  373.       // notify listeners of the change
  374.       this.fireHistoryEvent(hash);
  375.    },  
  376.  
  377.    /** Gets the current location of the hidden IFrames
  378.        that is stored as history. For Internet Explorer. */
  379.    /** private */ getIFrameHash: function() {
  380.       // get the new location
  381.       var historyFrame = document.getElementById("DhtmlHistoryFrame");
  382.       var doc = historyFrame.contentWindow.document;
  383.       var hash = new String(doc.location.search);
  384.  
  385.       if (hash.length == 1 && hash.charAt(0) == "?")
  386.          hash = "";
  387.       else if (hash.length >= 2 && hash.charAt(0) == "?")
  388.          hash = hash.substring(1); 
  389.     
  390.     
  391.       return hash;
  392.    },          
  393.    
  394.    /** Removes any leading hash that might be on a location. */
  395.    /** private */ removeHash: function(hashValue) {
  396.       if (hashValue == null || hashValue == undefined)
  397.          return null;
  398.       else if (hashValue == "")
  399.          return "";
  400.       else if (hashValue.length == 1 && hashValue.charAt(0) == "#")
  401.          return "";
  402.       else if (hashValue.length > 1 && hashValue.charAt(0) == "#")
  403.          return hashValue.substring(1);
  404.       else
  405.          return hashValue;     
  406.    },          
  407.    
  408.    /** For IE, says when the hidden iframe has finished loading. */
  409.    /** private */ iframeLoaded: function(newLocation) {
  410.       // ignore any location changes that we made ourselves
  411.       if (this.ignoreLocationChange == true) {
  412.          this.ignoreLocationChange = false;
  413.          return;
  414.       }
  415.       
  416.       // get the new location
  417.       var hash = new String(newLocation.search);
  418.       if (hash.length == 1 && hash.charAt(0) == "?")
  419.          hash = "";
  420.       else if (hash.length >= 2 && hash.charAt(0) == "?")
  421.          hash = hash.substring(1);
  422.       
  423.       // move to this location in the browser location bar
  424.       // if we are not dealing with a page load event
  425.       if (this.pageLoadEvent != true) {
  426.          window.location.hash = hash;
  427.       }
  428.  
  429.       // notify listeners of the change
  430.       this.fireHistoryEvent(hash);
  431.    },
  432.  
  433.    /** Determines if this is Internet Explorer. */
  434.    /** private */ isInternetExplorer: function() {
  435.       var userAgent = navigator.userAgent.toLowerCase();
  436.       if (document.all && userAgent.indexOf('msie')!=-1) {
  437.          return true;
  438.       }
  439.       else {
  440.          return false;
  441.       }
  442.    }
  443. };
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456. /** An object that uses a hidden form to store history state 
  457.     across page loads. The chief mechanism for doing so is using
  458.     the fact that browser's save the text in form data for the
  459.     life of the browser and cache, which means the text is still
  460.     there when the user navigates back to the page. See
  461.     http://codinginparadise.org/weblog/2005/08/ajax-tutorial-saving-session-across.html
  462.     for full details. */
  463. window.historyStorage = {
  464.    /** If true, we are debugging and show the storage textfield. */
  465.    /** public */ debugging: false,
  466.    
  467.    /** Our hash of key name/values. */
  468.    /** private */ storageHash: new Object(),
  469.    
  470.    /** If true, we have loaded our hash table out of the storage form. */
  471.    /** private */ hashLoaded: false, 
  472.    
  473.    /** public */ put: function(key, value) {
  474.        this.assertValidKey(key);
  475.        
  476.        // if we already have a value for this,
  477.        // remove the value before adding the
  478.        // new one
  479.        if (this.hasKey(key)) {
  480.          this.remove(key);
  481.        }
  482.        
  483.        // store this new key
  484.        this.storageHash[key] = value;
  485.        
  486.        // save and serialize the hashtable into the form
  487.        this.saveHashTable(); 
  488.    },
  489.    
  490.    /** public */ get: function(key) {
  491.       this.assertValidKey(key);
  492.       
  493.       // make sure the hash table has been loaded
  494.       // from the form
  495.       this.loadHashTable();
  496.       
  497.       var value = this.storageHash[key];
  498.  
  499.       if (value == undefined)
  500.          return null;
  501.       else
  502.          return value; 
  503.    },
  504.    
  505.    /** public */ remove: function(key) {
  506.       this.assertValidKey(key);
  507.       
  508.       // make sure the hash table has been loaded
  509.       // from the form
  510.       this.loadHashTable();
  511.       
  512.       // delete the value
  513.       delete this.storageHash[key];
  514.       
  515.       // serialize and save the hash table into the 
  516.       // form
  517.       this.saveHashTable();
  518.    },
  519.    
  520.    /** Clears out all saved data. */
  521.    /** public */ reset: function() {
  522.       this.storageField.value = "";
  523.       this.storageHash = new Object();
  524.    },
  525.    
  526.    /** public */ hasKey: function(key) {
  527.       this.assertValidKey(key);
  528.       
  529.       // make sure the hash table has been loaded
  530.       // from the form
  531.       this.loadHashTable();
  532.       
  533.       if (typeof this.storageHash[key] == "undefined")
  534.          return false;
  535.       else
  536.          return true;
  537.    },
  538.    
  539.    /** Determines whether the key given is valid;
  540.        keys can only have letters, numbers, the dash,
  541.        underscore, spaces, or one of the 
  542.        following characters:
  543.        !@#$%^&*()+=:;,./?|\~{}[] */
  544.    /** public */ isValidKey: function(key) {
  545.       // allow all strings, since we don't use XML serialization
  546.       // format anymore
  547.       return (typeof key == "string");
  548.       
  549.       /*
  550.       if (typeof key != "string")
  551.          key = key.toString();
  552.       
  553.       
  554.       var matcher = 
  555.          /^[a-zA-Z0-9_ \!\@\#\$\%\^\&\*\(\)\+\=\:\;\,\.\/\?\|\\\~\{\}\[\]]*$/;
  556.                      
  557.       return matcher.test(key);*/
  558.    },
  559.    
  560.    
  561.    
  562.    
  563.    /** A reference to our textarea field. */
  564.    /** private */ storageField: null,
  565.    
  566.    /** private */ init: function() {
  567.       // write a hidden form into the page
  568.       var styleValue = "position: absolute; top: -1000px; left: -1000px;";
  569.       if (this.debugging == true) {
  570.          styleValue = "width: 30em; height: 30em;";
  571.       }   
  572.       
  573.       var newContent =
  574.          "<form id='historyStorageForm' " 
  575.                + "method='GET' "
  576.                + "style='" + styleValue + "'>"
  577.             + "<textarea id='historyStorageField' "
  578.                       + "style='" + styleValue + "'"
  579.                               + "left: -1000px;' "
  580.                       + "name='historyStorageField'></textarea>"
  581.          + "</form>";
  582.       document.write(newContent);
  583.       
  584.       this.storageField = document.getElementById("historyStorageField");
  585.    },
  586.    
  587.    /** Asserts that a key is valid, throwing
  588.        an exception if it is not. */
  589.    /** private */ assertValidKey: function(key) {
  590.       if (this.isValidKey(key) == false) {
  591.          throw "Please provide a valid key for "
  592.                + "window.historyStorage, key= "
  593.                + key;
  594.        }
  595.    },
  596.    
  597.    /** Loads the hash table up from the form. */
  598.    /** private */ loadHashTable: function() {
  599.       if (this.hashLoaded == false) {
  600.          // get the hash table as a serialized
  601.          // string
  602.          var serializedHashTable = this.storageField.value;
  603.          
  604.          if (serializedHashTable != "" &&
  605.              serializedHashTable != null) {
  606.             // destringify the content back into a 
  607.             // real JavaScript object
  608.             this.storageHash = eval('(' + serializedHashTable + ')');  
  609.          }
  610.          
  611.          this.hashLoaded = true;
  612.       }
  613.    },
  614.    
  615.    /** Saves the hash table into the form. */
  616.    /** private */ saveHashTable: function() {
  617.       this.loadHashTable();
  618.       
  619.       // serialized the hash table
  620.       var serializedHashTable = JSON.stringify(this.storageHash);
  621.       
  622.       // save this value
  623.       this.storageField.value = serializedHashTable;
  624.    }   
  625. };
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636. /** The JSON class is copyright 2005 JSON.org. */
  637. Array.prototype.______array = '______array';
  638.  
  639. var JSON = {
  640.     org: 'http://www.JSON.org',
  641.     copyright: '(c)2005 JSON.org',
  642.     license: 'http://www.crockford.com/JSON/license.html',
  643.  
  644.     stringify: function (arg) {
  645.         var c, i, l, s = '', v;
  646.  
  647.         switch (typeof arg) {
  648.         case 'object':
  649.             if (arg) {
  650.                 if (arg.______array == '______array') {
  651.                     for (i = 0; i < arg.length; ++i) {
  652.                         v = this.stringify(arg[i]);
  653.                         if (s) {
  654.                             s += ',';
  655.                         }
  656.                         s += v;
  657.                     }
  658.                     return '[' + s + ']';
  659.                 } else if (typeof arg.toString != 'undefined') {
  660.                     for (i in arg) {
  661.                         v = arg[i];
  662.                         if (typeof v != 'undefined' && typeof v != 'function') {
  663.                             v = this.stringify(v);
  664.                             if (s) {
  665.                                 s += ',';
  666.                             }
  667.                             s += this.stringify(i) + ':' + v;
  668.                         }
  669.                     }
  670.                     return '{' + s + '}';
  671.                 }
  672.             }
  673.             return 'null';
  674.         case 'number':
  675.             return isFinite(arg) ? String(arg) : 'null';
  676.         case 'string':
  677.             l = arg.length;
  678.             s = '"';
  679.             for (i = 0; i < l; i += 1) {
  680.                 c = arg.charAt(i);
  681.                 if (c >= ' ') {
  682.                     if (c == '\\' || c == '"') {
  683.                         s += '\\';
  684.                     }
  685.                     s += c;
  686.                 } else {
  687.                     switch (c) {
  688.                         case '\b':
  689.                             s += '\\b';
  690.                             break;
  691.                         case '\f':
  692.                             s += '\\f';
  693.                             break;
  694.                         case '\n':
  695.                             s += '\\n';
  696.                             break;
  697.                         case '\r':
  698.                             s += '\\r';
  699.                             break;
  700.                         case '\t':
  701.                             s += '\\t';
  702.                             break;
  703.                         default:
  704.                             c = c.charCodeAt();
  705.                             s += '\\u00' + Math.floor(c / 16).toString(16) +
  706.                                 (c % 16).toString(16);
  707.                     }
  708.                 }
  709.             }
  710.             return s + '"';
  711.         case 'boolean':
  712.             return String(arg);
  713.         default:
  714.             return 'null';
  715.         }
  716.     },
  717.     parse: function (text) {
  718.         var at = 0;
  719.         var ch = ' ';
  720.  
  721.         function error(m) {
  722.             throw {
  723.                 name: 'JSONError',
  724.                 message: m,
  725.                 at: at - 1,
  726.                 text: text
  727.             };
  728.         }
  729.  
  730.         function next() {
  731.             ch = text.charAt(at);
  732.             at += 1;
  733.             return ch;
  734.         }
  735.  
  736.         function white() {
  737.             while (ch != '' && ch <= ' ') {
  738.                 next();
  739.             }
  740.         }
  741.  
  742.         function str() {
  743.             var i, s = '', t, u;
  744.  
  745.             if (ch == '"') {
  746. outer:          while (next()) {
  747.                     if (ch == '"') {
  748.                         next();
  749.                         return s;
  750.                     } else if (ch == '\\') {
  751.                         switch (next()) {
  752.                         case 'b':
  753.                             s += '\b';
  754.                             break;
  755.                         case 'f':
  756.                             s += '\f';
  757.                             break;
  758.                         case 'n':
  759.                             s += '\n';
  760.                             break;
  761.                         case 'r':
  762.                             s += '\r';
  763.                             break;
  764.                         case 't':
  765.                             s += '\t';
  766.                             break;
  767.                         case 'u':
  768.                             u = 0;
  769.                             for (i = 0; i < 4; i += 1) {
  770.                                 t = parseInt(next(), 16);
  771.                                 if (!isFinite(t)) {
  772.                                     break outer;
  773.                                 }
  774.                                 u = u * 16 + t;
  775.                             }
  776.                             s += String.fromCharCode(u);
  777.                             break;
  778.                         default:
  779.                             s += ch;
  780.                         }
  781.                     } else {
  782.                         s += ch;
  783.                     }
  784.                 }
  785.             }
  786.             error("Bad string");
  787.         }
  788.  
  789.         function arr() {
  790.             var a = [];
  791.  
  792.             if (ch == '[') {
  793.                 next();
  794.                 white();
  795.                 if (ch == ']') {
  796.                     next();
  797.                     return a;
  798.                 }
  799.                 while (ch) {
  800.                     a.push(val());
  801.                     white();
  802.                     if (ch == ']') {
  803.                         next();
  804.                         return a;
  805.                     } else if (ch != ',') {
  806.                         break;
  807.                     }
  808.                     next();
  809.                     white();
  810.                 }
  811.             }
  812.             error("Bad array");
  813.         }
  814.  
  815.         function obj() {
  816.             var k, o = {};
  817.  
  818.             if (ch == '{') {
  819.                 next();
  820.                 white();
  821.                 if (ch == '}') {
  822.                     next();
  823.                     return o;
  824.                 }
  825.                 while (ch) {
  826.                     k = str();
  827.                     white();
  828.                     if (ch != ':') {
  829.                         break;
  830.                     }
  831.                     next();
  832.                     o[k] = val();
  833.                     white();
  834.                     if (ch == '}') {
  835.                         next();
  836.                         return o;
  837.                     } else if (ch != ',') {
  838.                         break;
  839.                     }
  840.                     next();
  841.                     white();
  842.                 }
  843.             }
  844.             error("Bad object");
  845.         }
  846.  
  847.         function num() {
  848.             var n = '', v;
  849.             if (ch == '-') {
  850.                 n = '-';
  851.                 next();
  852.             }
  853.             while (ch >= '0' && ch <= '9') {
  854.                 n += ch;
  855.                 next();
  856.             }
  857.             if (ch == '.') {
  858.                 n += '.';
  859.                 while (next() && ch >= '0' && ch <= '9') {
  860.                     n += ch;
  861.                 }
  862.             }
  863.             if (ch == 'e' || ch == 'E') {
  864.                 n += 'e';
  865.                 next();
  866.                 if (ch == '-' || ch == '+') {
  867.                     n += ch;
  868.                     next();
  869.                 }
  870.                 while (ch >= '0' && ch <= '9') {
  871.                     n += ch;
  872.                     next();
  873.                 }
  874.             }
  875.             v = +n;
  876.             if (!isFinite(v)) {
  877.                 error("Bad number");
  878.             } else {
  879.                 return v;
  880.             }
  881.         }
  882.  
  883.         function word() {
  884.             switch (ch) {
  885.                 case 't':
  886.                     if (next() == 'r' && next() == 'u' && next() == 'e') {
  887.                         next();
  888.                         return true;
  889.                     }
  890.                     break;
  891.                 case 'f':
  892.                     if (next() == 'a' && next() == 'l' && next() == 's' &&
  893.                             next() == 'e') {
  894.                         next();
  895.                         return false;
  896.                     }
  897.                     break;
  898.                 case 'n':
  899.                     if (next() == 'u' && next() == 'l' && next() == 'l') {
  900.                         next();
  901.                         return null;
  902.                     }
  903.                     break;
  904.             }
  905.             error("Syntax error");
  906.         }
  907.  
  908.         function val() {
  909.             white();
  910.             switch (ch) {
  911.                 case '{':
  912.                     return obj();
  913.                 case '[':
  914.                     return arr();
  915.                 case '"':
  916.                     return str();
  917.                 case '-':
  918.                     return num();
  919.                 default:
  920.                     return ch >= '0' && ch <= '9' ? num() : word();
  921.             }
  922.         }
  923.  
  924.         return val();
  925.     }
  926. };
  927.  
  928.  
  929.  
  930. /** Initialize all of our objects now. */
  931. window.historyStorage.init();
  932. window.dhtmlHistory.create();
  933.