"use strict";

$('tr').click(function()
{
    //Clicking anywhere in a row highlights that row's input
    $('textarea', this).focus();
});

function doConversions(value, base)
{
    //If value is blank, just clear everything
    if(value=="")
    {
        $('#bin, #hex, #dec, #octal').val('').css({'height':'auto'});
        return;
    }

    var bi=bigInt(value, base);

    //Binary
    if(base!=2)
    {
        $('#bin').val(formatBin(bi.toString(2)));
        resizeTextarea($('#bin'));
    }

    //Hexadecimal
    if(base!=16)
    {
        $('#hex').val(formatHex(bi.toString(16)));
        resizeTextarea($('#hex'));
    }

    //Decimal
    if(base!=10)
    {
        $('#dec').val(formatDec(bi.toString(10)));
        resizeTextarea($('#dec'));
    }

    //Octal
    if(base!=8)
    {
        $('#octal').val(formatOctal(bi.toString(8)));
        resizeTextarea($('#octal'));
    }
}

function formatBin(bin)
{
    return bin.replace(/(.)(?=(.{8})+$)/g, '$1 ');;
}

function formatHex(hex)
{
    return hex.replace(/(.)(?=(.{2})+$)/g, '$1 ');;
}

function formatDec(dec)
{
    return dec.replace(/(.)(?=(.{3})+$)/g, '$1,');
}

function formatOctal(octal)
{
    return octal;
}

function resizeTextarea(textarea)
{
    textarea.css({'height':'auto'}).height(textarea.get(0).scrollHeight);
}

//Handle binary input
$('#bin').on('input', function()
{
    //Remove non-binary characters
    var text=$(this).val().replace(/[^0-1]/g,'');

    doConversions(text, 2);
    $(this).val(formatBin(text));
});

//Handle hexadecimal input
$('#hex').on('input', function()
{
    //Remove non-hexadecimal characters
    var text=$(this).val().replace(/[^0-9a-fA-F]/g,'');

    doConversions(text, 16);
    $(this).val(formatHex(text));
});

//Handle decimal input
$('#dec').on('input', function()
{
    //Remove non-decimal characters
    var text=$(this).val().replace(/[^0-9]/g,'');

    doConversions(text, 10);
    $(this).val(formatDec(text));
});

//Handle octal input
$('#octal').on('input', function()
{
    //Remove non-octal characters
    var text=$(this).val().replace(/[^0-7]/g,'');

    doConversions(text, 8);
    $(this).val(formatOctal(text));
});

@@ -1497,32 +1497,61 @@
 rmatBin(text));%0A
+    resizeTextarea($(this));%0A
 %7D);%0A%0A//Handle he
@@ -1753,32 +1753,61 @@
 rmatHex(text));%0A
+    resizeTextarea($(this));%0A
 %7D);%0A%0A//Handle de
@@ -1999,24 +1999,53 @@
 Dec(text));%0A
+    resizeTextarea($(this));%0A
 %7D);%0A%0A//Handl
@@ -2208,16 +2208,16 @@
 xt, 8);%0A
-
     $(th
@@ -2240,16 +2240,45 @@
 tal(text));%0A
+    resizeTextarea($(this));%0A
 %7D);%0A

Fix resizing of the active input field