/*
Cookie anlegen
*/
function cookieSetzen(){
if(document.loginform.cookiesetzen.checked){
var ttlogin = document.loginform.kflogin.value;
var ttpasswort = document.loginform.kfpasswort.value;
var jetzt= new Date();
var ttdatum = new Date(jetzt.getTime()+1000*60*60*24*7);
setCookie("ttkflogin", encrypt_cookie(ttlogin, 16), ttdatum);
setCookie("ttkfpass", encrypt_cookie(ttpasswort, 16), ttdatum);
}
}

function setCookie(name, value,
expires, path, domain, secure) {
document.cookie = name + "=" +
escape(value) +
((expires) ? "; expires=" +
expires.toUTCString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" +
domain : "") +
((secure) ? "; secure" : "");
}

/*
Cookie auslesen
*/

function cookieAuslesen(){
if(document.loginform){
var flogin = getCookie("ttkflogin");
var fpasswd = getCookie("ttkfpass");
if((flogin!=null)&&(fpasswd!=null)){
//alert(fpasswd);
document.loginform.kflogin.value = decrypt_cookie(flogin, 16);
document.loginform.kfpasswort.value = decrypt_cookie(fpasswd, 16);
}
}
}

function getCookie(name) {
var dc = document.cookie;
if (dc.indexOf(name + "=") != -1) {
var begin = dc.indexOf(name) +
name.length + 1;
var end = (dc.indexOf(";", begin)
!= -1) ? dc.indexOf(";", begin) :
dc.length;
return unescape(dc.substring(begin,
end));
}
else return null;
}

/* Guess what */
var legal_characters= "!\"¤$%&/()=?`ÁÒ¦¢[]|{}­À'QWERTZUIOP†*ASDFGHJKL…€'>YXCVBNM;:_1234567890§«qwertzuiopŸasdfghjklšŠ<yxcvbnm,.-Ç·Û¨ ½¬Ú¿¹¥±Œâ¶Ä©»¼Æ@Ï¾Ô´²ÅÃº~µ°ÉÐ#'Èãäüýÿçó¯¸¡ðÕ®Îßöõîíìªê³àþÖÑ¡^"

function encrypt_cookie(cookie_value, encrypt_key) { 

     var cookie_character
     var character_location
     var encrypted_location
     var encrypted_character

     // This variable holds the encrypted cookie characters
     var encrypted_string = ""

     // Run through each character in the cookie value
     for (var counter = 0; counter < cookie_value.length; counter++) { 

         // Get the current cookie character
         cookie_character = cookie_value.substring(counter, counter + 1)

         // Get the character's location in the string of legal characters
         character_location = legal_characters.indexOf(cookie_character)

         // XOR the character location with the encrypt_key
         encrypted_location = character_location ^ encrypt_key

         // Use the encrypted location to specify the encrypted 
         // character within the string of legal characters
         encrypted_character = legal_characters.substring(encrypted_location, 
                                               encrypted_location + 1) 

         // Add the encrypted character to the string
         encrypted_string += encrypted_character 

    } 
    return encrypted_string
}

function decrypt_cookie(encrypted_string, encrypt_key) { 

    var cookie_character
    var character_location
    var encrypted_location
    var encrypted_character

    // This variable holds the decrypted cookie value
    var cookie_value = ""

    // Run through each character in the encrypted string
    for  (var counter = 0; counter < encrypted_string.length; counter++) { 

         // Get the current encrypted character
         encrypted_character = encrypted_string.substring(counter, counter + 1)

         // Get the character's location in the string of legal characters
         encrypted_location = legal_characters.indexOf(encrypted_character) 

         // XOR the character location with the encrypt_key
         character_location = encrypted_location ^ encrypt_key

         // Use the character location to specify the plain text 
         // character within the string of legal characters
         cookie_character = legal_characters.substring(character_location, 
                            character_location + 1)

         // Add the plain text character to the string
         cookie_value += cookie_character 
    } 
    return cookie_value
} 