//----------------------------------------------------------------------------
//METHODS FOR ENCRYPTION
//----------------------------------------------------------------------------
var blogcrypt_encryption = {
	enCrypt: function()
	{
		var i=0;
		
		var selection;
        var node = document.popupNode;

		var focusedWindow = new XPCNativeWrapper(document.commandDispatcher.focusedWindow,
	    			'document',
	   		 	'getSelection()');
		
		// a kijelölt szöveg kinyerése
		if ((node instanceof HTMLTextAreaElement) ||
	    ((node instanceof HTMLInputElement) && (node.type == "text")))  
		{
	     selection = node.value.substring(node.selectionStart, node.selectionEnd);
		} else {
          selection = focusedWindow.getSelection().toString();
		}
		
		
		if (selection=="") return;
		
		//BBCode feldolgozás
		selection=parseBBCode(selection);
		
		//aktuális domain
		var domain=String(gBrowser.selectedBrowser.contentDocument.domain);
		//pushing the params to the dialog
		window.openDialog("chrome://blogcrypt/content/pswdchooser.xul","pswdchoos","chrome",selection,domain,node);
		
	},
	
	enCrypt_second_part : function( selection, method, keyID, domain,node)
	{
		// jelszó elkérése
		passwd=blogcrypt_FileIO.get_passwd_from_db(domain,keyID);
		//titkosítás
		var cryptedselection=blogcrypt_crypting_functions.encrypt("blogcrypt"+selection,passwd,method);//crypting
		
		// szóközök beszúrása a titkosított blokkba
		var i=0;
		for(i=0;i<cryptedselection.length;i++)
			{
				if(i%15==0)
				{
					cryptedselection=cryptedselection.slice(0,i)+" "+cryptedselection.slice(i);
				}
			}
		
		//a crypt tag összeálítása
		var cryptostring="\[crypt keyid="+keyID+" algo="+method+" length="+cryptedselection.length+"\]"+cryptedselection;
		
		//visszaírás
		if(node.value!=null)
		{
		var firstpart=node.value.substring(0, node.selectionStart);
		var endpart=node.value.substring(node.selectionEnd);
		node.value=firstpart+cryptostring+endpart;
		}
		else
		{
		const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].  
		getService(Components.interfaces.nsIClipboardHelper);
		gClipboardHelper.copyString(cryptostring); 
		alert("Crypted text is on the clipboard!");
		}
	}

}
