function WebForm() { };

WebForm.prototype.tmpEncode = {
    'text': 'foo',
    'hex': 'strToHex',
    'base64': 'Base64.encode'
};
WebForm.prototype.tmpDecode = {
    'text': 'foo',
    'hex': 'hexToStr',
    'base64': 'Base64.decode'
};

WebForm.prototype.hashMap = {
    'MD5': 'md5_binary(val)',
    'SHA-1': 'sha1_binary(val)',
    'SHA-224': 'str_sha(val, "SHA-224")',
    'SHA-256': 'sha2_binary(val)',
    'SHA-384': 'str_sha(val, "SHA-384")',
    'SHA-512': 'str_sha(val, "SHA-512")'
};

WebForm.prototype.encodeHash = function(v) {
    return strToHex(v);
};

WebForm.prototype.hash = function(m, val) {
    try {

        switch (m) {
        case 1:
            val = hexToStr(val);
            break;
        case 2:
            val = Base64.decode(val);
            break;
        }

        for (var item in this.hashMap) {
            $('#' + item).val(this.encodeHash(eval(this.hashMap[item])));
        }

    } catch(e) {
        $('#' + item).val('');
        alert("error" + e +" " + (e.name ? e.name : '') + " " + (e.message ? e.message : ''));
    }
};

function foo(str) {
    return str;
};

WebForm.prototype.getFormat = function(format) {
    var outputFormat = null;

    var colRadio = document.getElementsByName(format);

    for (var i = 0; i < colRadio.length; i++) {
        if (colRadio[i].checked) {
            outputFormat = colRadio[i].value;
            break;
        }
    }

    if (!outputFormat) throw "No " + format;

    return outputFormat;
};

WebForm.prototype.encrypt = function(m, val, key) {
    try {
        val = eval(this.tmpDecode[this.getFormat('plainFormat')] + "(val)");
        key = eval(this.tmpDecode[this.getFormat('plainFormat')] + "(key)");

        var output;

        switch (m) {
        case 'Blowfish':
            var t = new Blowfish;
            t.setKey(key);
            output = t.encrypt(val);
            break;
        case 'XXTEA':
        case 'XTEA':
        case 'TEA':
            eval("var t = new " + m);
            output = t.encrypt(val, key);
            break;
        default:
            throw "No encryption function " + m + "!";
        }

        return eval(this.tmpEncode[this.getFormat('cipherFormat')] + "(output)");
    } catch(e) {
        alert("error" + e +" " + (e.name ? e.name : '') + " " + (e.message ? e.message : ''));
    }
};

WebForm.prototype.decrypt = function(m, val, key) {
    try {
        val = eval(this.tmpDecode[this.getFormat('cipherFormat')] + "(val)");
        key = eval(this.tmpDecode[this.getFormat('plainFormat')] + "(key)");

        var output;

        switch (m) {
        case 'Blowfish':
            var t = new Blowfish;
            t.setKey(key);
            output = t.decrypt(val);
            break;
        case 'XXTEA':
        case 'XTEA':
        case 'TEA':
            eval("var t = new " + m);
            output = t.decrypt(val, key);
            break;
        default:
            throw "No encryption function " + m + "!";
        }

        return eval(this.tmpEncode[this.getFormat('plainFormat')] + "(output)");

    } catch(e) {
        alert("error" + e +" " + (e.name ? e.name : '') + " " + (e.message ? e.message : ''));
    }
};