
u=""; 

isGecko = isOpera = isIE = false;

if (window.opera) // first, because opera likes to spoof
{
  isOpera = true;

  if (!document.createComment)
    alert("You're using Opera 6 or older.  Please upgrade to Opera 7. (This tool works in Opera 7, Mozilla, and IE.)");
}
else if (navigator.userAgent.indexOf("Gecko") != -1)
{
  isGecko = true;

  if (navigator.userAgent.indexOf("rv:0.") != -1)
    alert("You are using an old version of Mozilla.  Please upgrade.");
}
else if (navigator.userAgent.indexOf("MSIE") != -1)
{
  isIE = true;
}
else
{
  alert("I'm not familiar with the browser you're using.  I'll pretend you're using Mozilla, but this tool probably won't work for you.");
  isGecko = true;
}

function singleQuoteEscape(s) { return s.replace(/\\/g,"\\\\").replace(/\'/g,"\\\'"); }
function doubleQuoteEscape(s) { return s.replace(/\\/g,'\\\\').replace(/\"/g,'\\\"'); }
function percentEscape(s)     { return s.replace(/\%/g,'%25'); }

function makeBookmarklet (styles)
{
  // Convert all whitespace, including newlines, to single spaces.
  styles = styles.replace(/\s+/g, " ");

  // Remove whitespace from the beginning or end of the styles.
  styles = styles.replace(/^\s/, "");
  styles = styles.replace(/\s$/, "");

  if (isIE)
  {
    // Two percentEscapes for being in two javascript URLs.
    // Two *quoteEscapes for being in two javascript strings in the generated bookmarklet.

    return "javascript:document.createStyleSheet(\"javascript:'" + percentEscape(doubleQuoteEscape(percentEscape(singleQuoteEscape(styles)))) + "'\").v"
  }
  else if (isGecko)
  {
    // Escape styles twice: once for javascript: urls (51355?), 
    // and once for data: urls (136538, fixed for 1.0 and for 1.1alpha).  
    // Call percentEscape as we generate the bookmarklet and then call
    // the more conservative escape() in the generated bookmarklet itself.

    return "javascript:styles='" + percentEscape(singleQuoteEscape(styles)) + "'; newSS = document.createElement('link'); newSS.rel = 'stylesheet'; newSS.href = 'data:text/css,' + escape(styles); document.documentElement.childNodes[0].appendChild(newSS); void 0"
  }
  else if (isOpera)
  {
    // Opera 7: 
    // * Create stylesheet like Mozilla
    //   * But don't append to document (heirarchy_request_error; unnecessary!)
    // * URL of stylesheet is javascript:, like in IE.
    // * javascript: URLs aren't %-escaped like they are in IE and Mozilla.

    return "javascript:styles='" + singleQuoteEscape(styles) + "'; newSS = document.createElement('link'); newSS.rel = 'stylesheet'; newSS.href = (\"javascript:'" + doubleQuoteEscape(singleQuoteEscape(styles)) + "'\"); void 0";
  }
}

function updateLink (styles,idlink)
{
//  styles = document.getElementById("idstyles").value
//  name = document.getElementById(idname).value;
  CL = document.getElementById(idlink);
  CL.href = makeBookmarklet(styles);
}

