//----------------------------------------------------------
//          _    _                          _      _
//  ____ __(_)__| |___ _ _ __ _ _ _  _ _ __| |_   (_)___
// (_-< '_ \ / _` / -_) '_/ _| '_| || | '_ \  _|_ | (_-<
// /__/ .__/_\__,_\___|_| \__|_|  \_, | .__/\__(_)/ /__/
//    |_|                         |__/|_|       |__/
//
//----------------------------------------------------------
// File      : spidercrypt.js
// Author    : Richard Lewis
// Project   : /mnt/web/localhost/htdocs/js/spidercrypt/
// Syntax    : javascript
// Date      : Sat 09 Feb 2008
// Copyright : Richard Lewis 2008
//----------------------------------------------------------
// spidercrypt.js - SpiderCrypt JavaScript
//----------------------------------------------------------

function goSpiderCrypt() {
    // onload

    interfaceSetup();
}
//----------------------------------------------------------

function interfaceSetup() {
    // interface setup

    var SpiderCrypt = getEle('SpiderCrypt');

    var keyTxt = crtTxt('Key');

    var keyInp = crtEle('input');
    keyInp.id  = 'key';

    var txtTxt = crtTxt('Plain Text or Cipher Code');
    txtTxt.id  = 'txt';

    var txtInp = crtEle('textarea');
    txtInp.id  = 'text';

    var btBar       = crtEle('div');
    btBar.className = 'btbar';

    var encryptBt       = crtEle('span');
    encryptBt.className = 'bt';

    encryptTxt = crtTxt('Encrypt');

    encryptBt.onclick = function() {
        encrypt();
    };

    var decryptBt       = crtEle('span');
    decryptBt.className = 'bt';

    decryptBt.onclick = function() {
        decrypt();
    };

    decryptTxt = crtTxt('Decrypt');

    var output       = crtEle('div');
    output.id        = 'spiderOut';
    output.className = 'output';

    var txtOut = crtEle('div');
    txtOut.id  = 'txtOut';

    encryptBt.appendChild(encryptTxt);
    decryptBt.appendChild(decryptTxt);

    btBar.appendChild(encryptBt);
    btBar.appendChild(decryptBt);

    output.appendChild(txtOut);

    SpiderCrypt.appendChild(keyTxt);
    SpiderCrypt.appendChild(br());
    SpiderCrypt.appendChild(keyInp);
    SpiderCrypt.appendChild(br());
    SpiderCrypt.appendChild(br());
    SpiderCrypt.appendChild(txtTxt);
    SpiderCrypt.appendChild(br());
    SpiderCrypt.appendChild(txtInp);
    SpiderCrypt.appendChild(br());
    SpiderCrypt.appendChild(br());
    SpiderCrypt.appendChild(btBar);
    SpiderCrypt.appendChild(br());
    SpiderCrypt.appendChild(output);

    keyInp.focus();
}
//----------------------------------------------------------

function encrypt() {
    // encrypt

    var keyE  = getEle('key');
    var txtE  = getEle('txt');
    var textE = getEle('text');

    txtE.nodeValue = 'Plain Text';

    SpiderCrypt.keyReset();

    SpiderCrypt.userKey   = keyE.value;
    SpiderCrypt.plainText = textE.value; 

    SpiderCrypt.initSpiderKey();
    SpiderCrypt.initSubKey();

    SpiderCrypt.encrypt();

    var txtOut = getEle('txtOut');
    
    clrEle(txtOut);

    SpiderCrypt.formatCipherCode();

    txtOut.appendChild(crtTxt('Cipher Code :'));
    txtOut.appendChild(br());
    txtOut.appendChild(br());
    txtOut.appendChild(crtTxt(SpiderCrypt.cipherCodeNL));
}
//----------------------------------------------------------

function decrypt() {
    // decrypt

    var keyE  = getEle('key');
    var txtE  = getEle('txt');
    var textE = getEle('text');

    txtE.nodeValue = 'Cipher Text';

    SpiderCrypt.keyReset();

    SpiderCrypt.userKey    = keyE.value;
    SpiderCrypt.cipherCode = textE.value; 

    SpiderCrypt.initSpiderKey();
    SpiderCrypt.initSubKey();

    var txtOut = getEle('txtOut');

    clrEle(txtOut);

    if (SpiderCrypt.decrypt()) {
        SpiderCrypt.decodePlain();

        txtOut.appendChild(crtTxt('Plain Text :'));
        txtOut.appendChild(br());
        txtOut.appendChild(br());
        txtOut.appendChild(crtTxt(SpiderCrypt.plainText));
    }
    else {
        txtOut.appendChild(crtTxt('Error in Cipher Code'));
    }
}
//----------------------------------------------------------

if (window.addEventListener)
    window.addEventListener("load", goSpiderCrypt, false);

else if (window.attachEvent)
    window.attachEvent("onload", goSpiderCrypt);
//----------------------------------------------------------
