  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;
  }    
