var Browser = {
  Version: function() {
    var version = 999; // we assume a sane browser
    if (navigator.appVersion.indexOf("MSIE") != -1)
      // bah, IE again, lets downgrade version number
      version = parseFloat(navigator.appVersion.split("MSIE")[1]);
    return version;
  }
}

/* This is much faster than using (el.innerHTML = str) when there are many
existing descendants, because in some browsers, innerHTML spends much longer
removing existing elements than it does creating new ones. */
function replaceHtml(el, html) {
        var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
        var newEl = document.createElement(oldEl.nodeName);
        
        // Preserve the element's id and class (other properties are lost)
        newEl.id = oldEl.id;
        newEl.setAttribute('id', oldEl.getAttribute('id'));
        newEl.className = oldEl.className;
        newEl.setAttribute('class', oldEl.getAttribute('class'));
        
        // Replace the old with the new
        newEl.innerHTML = html;
        oldEl.parentNode.replaceChild(newEl, oldEl);
        
        /* Since we just removed the old element from the DOM, return a reference
        to the new element, which can be used to restore variable references. */
        return newEl;
};

var html_loader_last_href = '';
    function handleLoadContent(c, href)
    {

      replaceHtml('html-load-content', c);

      updateHREF(href);


      if (top.window.onHTMLLoader_load)
        top.window.onHTMLLoader_load(href);

      updateLinks();
    }
    
    function isBinary(str)
    {
      if (str == undefined) return false;
      if (str.substring(str.length - 4, str.length) == '.jpg') return true;
      if (str.substring(str.length - 4, str.length) == '.gif') return true;
      if (str.substring(str.length - 4, str.length) == '.png') return true;
      if (str.substring(str.length - 4, str.length) == '.pdf') return true;
      if (str.substring(str.length - 4, str.length) == '.mp3') return true;
      if (str.substring(str.length - 4, str.length) == '.zip') return true;
      if (str.substring(str.length - 5, str.length) == '.jpeg') return true;

      if (str.indexOf('click.cfm') != -1) return true;
      return false;
    }

  var _hash = unescape(window.location.hash);
  if (Browser.Version() >= 7)
    setInterval('checkHash()', 500);
  function checkHash()
  {
    // _hash contains the most recently loaded page's recorded addresses
    // t_hash contains the URLs address.
    // if there's a mismatch, then we may want to reload!
    t_hash = unescape(window.location.hash)
    if ((t_hash != '#' + _hash) && (t_hash != _hash))
    {
      if (t_hash == '') 
        t_hash = '#/';
      if ((t_hash == '#/') && (_hash == '')) return;
      
      if (top.window.html_load_onunload)
        top.window.html_load_onunload();

      
      document.getElementById('html-load-content').innerHTML = '';
      htmlRPC.href = t_hash.substring(1);
      htmlRPC.href = sn_replace(htmlRPC.href, 'html_load', '1')
      htmlRPC.execute();
      _hash = t_hash;
    }
  }
  
    function updateHREF(href)
    {
      href2 = sn_replace(href, 'tz', '');
      href2 = sn_replace(href2, 'html_load', '');
      href2 = sn_replace(href2, 'html_load_execute', '');
      href2 = sn_replace(href2, 'html_load_scrollTo', '');
      href2 = sn_replace(href2, 'rnd', '');

      _hash = unescape(href2);
      
      top.location.hash = href2;
    }
    
    function cancelLink(cancel)
    {
      if ((cancel == null) || cancel)
      {
        cancelNextLink = true;
        return false;
      }      
    }
    
    function followLink(follow)
    {
      if ((follow == null) || follow)
      {
        followNextLink = true;
        return true;
      }
    }
    
    function setOnUnload(str)
    {
      top.window.html_load_onunload = new Function(str);
    }
    
  function sn_replace(sn, name, value)
  {
    name = name.toLowerCase();
    sn = sn.toLowerCase();
    loc = sn.indexOf('?' + name + '=') + 1;
        
    if (loc == 0)
      loc = sn.indexOf('&' + name + '=') + 1;
    
    if (!value) value = '';  
    
    value = value + '';
    
    var t;
    
    if (loc == 0)
    {
      // simplest case, the attribute does not exist in the sn

      if (value.length)
      {
        if (sn.length && (sn.indexOf('?') != -1))
        {
          t = sn + '&' + name + '=' + escape(value);
        }
        else if (sn.indexOf('?') == sn.length - 1)
          t = sn + name + '=' + escape(value);
        else
          t = sn + '?' + name + '=' + escape(value);
        
      }
      else 
        t = sn;
    }
    else
    {
        
      end_loc = sn.substr(loc, sn.length).indexOf('&') + loc;
      t = '';
      // if not the first item in the list, prepend the start of the list
      if (loc > 1)
        t += sn.substr(0, loc);
      
      if (value.length)
        t += name + '=' + escape(value);
      
      // if not the last item in the list, append the rest of the list
      if (end_loc > loc)
        t += sn.substr(end_loc);

    }
  
    while (t.indexOf('&&') != -1)
      t = t.replace('&&', '&');
      
    while (t.substr(t.length - 1) == '&')
      t = t.substr(0, t.length - 1);
      
    while (t.substr(t.length - 1) == '?')
      t = t.substr(0, t.length - 1);
    return t;
  }
  
    var html_load_modules = new Object();

    function loadModule(src, callback)
    {
      var script = document.createElement('script');
      script.setAttribute('type', 'text/javascript'); 
      script.type = 'text/javascript';
      
      if ((typeof(callback) != 'undefined') && callback)
      {
        if (script.readyState)
        {  //IE
          script.onreadystatechange = function()
          {
            if (script.readyState == "loaded" ||
                script.readyState == "complete")
            {
              script.onreadystatechange = null;
              callback();
            }
          };
        } else 
        {  //Others
          script.onload = function(){ callback(); };
        }

      }
      
      if (src.indexOf('?') == -1)
        script.setAttribute('src', src + '?rnd=' + Math.random());
      else
        script.setAttribute('src', src + '&rnd=' + Math.random());

      document.getElementsByTagName('head')[0].appendChild(script);
      html_load_modules[src] = true;
    }
    
    
    function loadModuleOnce(src, callback)
    {
      if (!html_load_modules[src])
        loadModule(src, callback);
      else
        if ((typeof(callback) != 'undefined') && callback)
          callback();
    }
    
    function addLoadEvent(func) 
    { 
      var oldonload = window.onload; 
      if (typeof window.onload != 'function') 
      { 
        window.onload = func; 
      } else 
      { 
        window.onload = function() 
        { 
          if (oldonload) 
            oldonload(); 
          func(); 
        } 
      } 
    } 
    
  function isHash(href)
  {
    if (!href) return false;
    return (href.substring(href.length - 1, href.length) == '#');
  }
  
  function containsHash(href)
  {
    if (!href) return false;
    return (href.indexOf('#') >= 0);
  }
  
  function findPos(obj) 
  {
  	var curleft = curtop = 0;
    if (obj.offsetParent) {
      do {
      			curleft += obj.offsetLeft;
      			curtop += obj.offsetTop;
      } while (obj = obj.offsetParent);
    }
  	return [curleft,curtop];
  }
  
  function cleanURL(url)
  {
    if (typeof(url) == 'undefined') url = top.location.href;
    if (url.indexOf('#') == -1) return url;
    var path = url.substring(url.indexOf('#') + 1);
    re = /^(https?):\/\/([^/]+)?/i;
    url.match(re);
    path = RegExp.$1 + '://' + RegExp.$2 + path;

    return path;
  }
  
  function appendIFrame()
  {
    var iframe = document.createElement('iframe');
    iframe.name = 'html-loader-iframe';
    iframe.id = 'html-loader-iframe';
    iframe.setAttribute('id', 'html-loader-iframe');
    iframe.style.width = '500px';
    iframe.style.height = '200px';
    iframe.style.borderWidth = '0';
    if (typeof(html_loader_debug) != 'undefined')
      iframe.style.display = 'block';
    else
      iframe.style.display = 'none';
    iframe.src = '/blank.cfm';
    
    document.getElementsByTagName('body')[0].appendChild(iframe);
  }