﻿function CopyToClipboardStrip( text )
{
	text = text.replace( /&nbsp;/g, ' ' );
	text = text.replace( /&quot;/g, '""' );
	text = text.replace( /&#39;/g, '""' );
	text = text.replace( /&amp;/g, '&' );
	text = text.replace( /&lt;/g, String.fromCharCode(60) );
	text = text.replace( /&gt;/g, String.fromCharCode(62) );
	return text;
}

function CopyToClipboard( text )
{
    if( window.clipboardData && clipboardData.setData )
	{
		clipboardData.setData("Text",CopyToClipboardStrip(text));
	}
	else
	{
		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=text;

		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);	   
	}

}