﻿$.extend({ URLEncode: function(c)
{
    var o = ''; var x = 0; c = c.toString(); var r = /(^[a-zA-Z0-9_.]*)/;
    while (x < c.length)
    {
        var m = r.exec(c.substr(x));
        if (m != null && m.length > 1 && m[1] != '')
        {
            o += m[1]; x += m[1].length;
        } else
        {
            if (c[x] == ' ') o += '+'; else
            {
                var d = c.charCodeAt(x); var h = d.toString(16);
                o += '%' + (h.length < 2 ? '0' : '') + h.toUpperCase();
            } x++;
        } 
    } return o;
},
    URLDecode: function(s)
    {
        var o = s; var binVal, t; var r = /(%[^%]{2})/;
        while ((m = r.exec(o)) != null && m.length > 1 && m[1] != '')
        {
            b = parseInt(m[1].substr(1), 16);
            t = String.fromCharCode(b); o = o.replace(m[1], t);
        } return o;
    }
});

function CopyToClipboard(s)
{
    if (window.clipboardData && clipboardData.setData)
    {
        clipboardData.setData("Text", s);
    }
    else
    {
        // You have to sign the code to enable this or allow the action in about:config by changing
        user_pref("signed.applets.codebase_principal_support", true);
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

        var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);
        if (!clip) return;

        // create a transferable
        var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);
        if (!trans) return;

        // specify the data we wish to handle. Plaintext in this case.
        trans.addDataFlavor('text/unicode');

        // To get the data from the transferable we need two new objects
        var str = new Object();
        var len = new Object();

        var str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);

        var copytext = meintext;

        str.data = copytext;

        trans.setTransferData("text/unicode", str, copytext.length * [[[[2]]]]);

        var clipid = Components.interfaces.nsIClipboard;

        if (!clip) return false;

        clip.setData(trans, null, clipid.kGlobalClipboard);
    }
}

jQuery.copy = function(t)
{
    if (typeof t == 'undefined')
    {
        t = '';
    }
    d = document;
    if (window.clipboardData)
    {
        window.clipboardData.setData('Text', t);
    }
    else
    {
        var f = 'flashcopier';
        if (!d.getElementById(f))
        {
            var dd = d.createElement('div');
            dd.id = f;
            d.body.appendChild(dd);
        }
        d.getElementById(f).innerHTML = '';
        var i = '<embed src="/content/swf/copy.swf" FlashVars="clipboard=' + encodeURIComponent(t) + '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
        alert(i);
        d.getElementById(f).innerHTML = i;
    }
}
