window.onload = function() {
    function encodeData(val) {
        return encodeURIComponent(val.replace(/\r\n|\r|\n/g, "\r\n"));
    }
    document.body.removeChild(document.body.getElementsByTagName("p")[0]);
    var fresh = document.body.appendChild(document.createElement("a"));
    fresh.appendChild(document.createTextNode("New"));
    fresh.href = "jsuri.html";
    var raw = document.body.appendChild(document.createElement("textarea"));
    raw.focus();
    var q = window.location.href.indexOf("?source=");
    if (q != -1) {
        raw.value = decodeURIComponent(window.location.href.substr(q + 8));
    }
    var bm = document.createElement("input");
    bm.setAttribute("type", "text");
    bm.readOnly = true;
    bm.onclick = function() {
        bm.select();
    };
    bm.value = "javascript:";
    document.body.appendChild(bm);
    var perm = document.body.appendChild(document.createElement("a"));
    perm.appendChild(document.createTextNode("Permalink"))
    function update() {
        var enc = encodeData(raw.value); 
        bm.value =  "javascript:" + enc ;
        if (enc == "") {
            perm.href = "jsuri.html";
        } else {
            perm.setAttribute("href", "jsuri.html?source=" + enc);
        }
    }
    update();
    if (window.addEventListener) {
        raw.addEventListener("input", update, false);
    } else if (raw.attachEvent) {
        raw.attachEvent("onpropertychange", update);
    }
};