window.addEventListener("load", function() { blogcrypt_passwordmanager.init(); }, false); 
window.addEventListener("CheckboxStateChange", function() {blogcrypt_passwordmanager.parsekeydb(); }, false);

var blogcrypt_passwordmanager = {
	init: function()
	{
		document.getElementById("domain").value=window.arguments[0]; // domain mező beállítása
		blogcrypt_passwordmanager.parsekeydb(); //lista építése
	},
	
	// kulcs felvétele a listába
	addkeytolist: function(domain,keyid,password)
	{
		var row = document.createElement('listitem');
		// Create and attach 1st cell
		var cell = document.createElement('listcell');
		cell.setAttribute('label', domain );
		cell.setAttribute('value', domain );
		row.appendChild( cell );
		// Create and attach 2nd cell
		cell = document.createElement('listcell');
		cell.setAttribute('label', keyid );
		cell.setAttribute('value', keyid );
		row.appendChild( cell );
		// Create and attach 3rd cell
		cell = document.createElement('listcell');
		if(document.getElementById("show_passwords").checked)
		{
			cell.setAttribute('label', password );
			cell.setAttribute('value', password );
		}
		else
		{
			cell.setAttribute('label', "" );
			cell.setAttribute('value', "" );
		}
		row.appendChild( cell );
		// Attach row
		document.getElementById('keydatabase').appendChild( row );
	},
	
	// lsita törlése
	clearListbox:function() 
	{
		var listbox = document.getElementById('keydatabase'); 
		while (listbox.lastChild.localName == 'listitem') 
			listbox.removeChild(listbox.lastChild); 
	},
	
	// lista felépítése
	parsekeydb:function()
	{
		blogcrypt_passwordmanager.clearListbox();
		
		var key_db=blogcrypt_FileIO.get_passwd_db();
		for(var i=0; i<key_db.length-1;i++)
				blogcrypt_passwordmanager.addkeytolist(key_db[i][0],key_db[i][1],key_db[i][2]);
	},
	
	// kulcs felvétele az adatbázisba
	addkeytodb: function()
	{
		// adatok kiynerése
		var domain=document.getElementById('domain').value;
		var keyID=document.getElementById('keyid').value;
		var password=document.getElementById('password').value;
		if(domain!='' && keyID!='' && password!='') // ha valid az input
		{
			if(blogcrypt_FileIO.add_passwd_to_db(domain,keyID,password)==true) // hozzáadás
			{
				blogcrypt_passwordmanager.parsekeydb();
				document.getElementById('keyid').value='';
				document.getElementById('password').value=''; //sikeres felvétel esetén  a bevitelimezők törlése
			}
			else
			{
				alert("Failed to add key to DB!");
			}
		}
		else
			alert("Missing key parameter!");
	},
	
	// kulcs törlése
	deletekeyfromdb: function()
	{
		var selecteditem;
		var i=0;
		while(selecteditem=document.getElementById('keydatabase').getSelectedItem(i))
		{
			if(selecteditem!=null) // ha van kiválasztva elem
			{
			// adatok kinyerése
			var domain=selecteditem.childNodes[0].getAttribute('value');
			var keyID=selecteditem.childNodes[1].getAttribute('value');
			
			// visszajelzés kérése
			if(confirm("Key with\n\tDomain:"+domain+"\n\tKeyID:"+keyID+"\n going to be deleted!\n Are you sure?"))
			{	
				blogcrypt_FileIO.delete_passwd_from_db(domain,keyID); //törlés
			}
			}
		else
			{
			alert("No key selected!");
			}
		i++;
		}
		blogcrypt_passwordmanager.parsekeydb();
	},
	
	// exportálás
	passdbexport: function(keys)
	{
		
			var pass=prompt("Type in, the password of the exported file! \n Leaving it empty will make a printable plain export!");
			var errcorrcode=pidCrypt.MD5(keys);
			var outstring="<data>\n"+keys+"</data>\n<errcorrcode>"+errcorrcode+"</errcorrcode>";
			if(pass!=false)
			{
				outstring=blogcrypt_crypting_functions.encrypt(outstring,pass,"AES-CBC-MD5");
			}
			alert("Export data is on the clipboard!");
		   const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].  
		   getService(Components.interfaces.nsIClipboardHelper);  
			gClipboardHelper.copyString(outstring);  

		
	},
	
	// minden exportálása
	passdbexportall: function()
	{
		var key_db=blogcrypt_FileIO.read_key_db();
		blogcrypt_passwordmanager.passdbexport(key_db);
	},
	
	// kiválasztottak exportálása
	passdbexportselected: function()
	{
		var selecteditem;
		var i=0;
		var key_db="";
		while(selecteditem=document.getElementById('keydatabase').getSelectedItem(i))
		{
			if(selecteditem!=null)
			{
				var domain=selecteditem.childNodes[0].getAttribute('value');
				var keyID=selecteditem.childNodes[1].getAttribute('value');
				var pass=selecteditem.childNodes[2].getAttribute('value');
				var dbitem=domain+";"+keyID+";"+pass+"\n";
				key_db+=dbitem;
			}
		else
			{
			alert("No key selected!");
			}
		i++;
		}
		blogcrypt_passwordmanager.passdbexport(key_db);
	},
	
	//importálás
	passdbimport: function()
	{
			var strin=prompt("Paste the import data here!");
			var pass=prompt("Type in, the password of the Imported file!");
			
			if(pass!=false)
			{
				strin=blogcrypt_crypting_functions.decrypt(strin,pass,"AES-CBC-MD5");
			}
			
			var index1=strin.indexOf("</data>\n<errcorrcode>")
			var index2=strin.indexOf("</errcorrcode>")
			var str=strin.slice(7,index1);
			var errcorrcode=strin.slice(index1+21,index2);
			
			 // if(errcorrcode!=pidcrypt.md5(str))
			 // {
				 // alert("corrupted import file!\n aborting import!");
				 // return;
			 // }
			
			var rows=str.split('\n'); //splitting to rows
			var key_table=new Array();
			for(var i=0;i<rows.length;i++)
			{	//splitting the rows
				key_table[i]=rows[i].split(';');
				blogcrypt_FileIO.add_passwd_to_db(key_table[i][0],key_table[i][1],key_table[i][2]);
			}
			blogcrypt_passwordmanager.parsekeydb();
			
			
		

	}
}