// Cookie auslesen:
function getCookieValue(a)
{
  const b = document.cookie.match('(^|;)\\s*' + a + '\\s*=\\s*([^;]+)');
  return b ? b.pop() : '';
}

function export_keys()
{
  var inhalt="";
  inhalt+=getCookieValue("did");
  inhalt+="#";
  inhalt+=localStorage.getItem("public_key");
  inhalt+="#";
  inhalt+=localStorage.getItem("private_key");

 var element = document.createElement('a');
 //element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(inhalt));
 element.setAttribute('href', 'data:text/plain;charset=ISO-8859-1,' + encodeURIComponent(inhalt));
 element.setAttribute('download', "webms-identity.txt");

 element.style.display = 'none';
 document.body.appendChild(element);

 element.click();

 document.body.removeChild(element);
 //alert("Sicherung erfolgreich!");
}

function einlesen()
{
    const asText = document.getElementById('dateiimport');

    const file = asText.files[0];
    const textType = /text.*/;

    if (file.type.match(textType)) {
        const reader = new FileReader();

        reader.onload = function(e) {

            erg = reader.result;
            teile=erg.split("#");
            import_keys(teile[0],teile[1],teile[2]);
        }

        reader.readAsText(file);
    } else {
        alert("Dateityp nicht unterstützt");
    }
}

function import_keys(cookie,public_key,private_key)
{
  localStorage.setItem("public_key",public_key);
  localStorage.setItem("private_key",private_key);

  document.cookie = 'did2='+cookie+';path=/';
  //document.cookie = 'i_cookie='+document.getElementById('cookie').value+';path=/';

  alert("Import erfolgreich!");
  document.location.href="/";
}
