Looping Problem

I’m having troubles getting something to work properly. I have 2 text fields and a button. When you enter something in the first field (var ‘decoded’) and press the button ‘encode_mc’, the second field (var ‘encoded’) will display the result of a function called.

The function takes the characters entered and compares them to the value in another string. More specifically, it should move every key to the left once you ‘encode’ it, with the leftmost keys looping to the right side.

Currently, this works as it should unless you type more than one of the three end letters on the keyboard, for example, if you type “qaz” it only outputs “p” instead of the “plm” that it should output.

Does anyone know why this is happening, or how I could fix it?

Thanks!

num = "1234567890";
numE = "0123456789";
firstRow = "qwertyuiop";
firstRowE = "pqwertyuio";
secondRow = "asdfghjkl";
secondRowE = "lasdfghjk";
thirdRow = "zxcvbnm";
thirdRowE = "mzxcvbn";
//encodeOnPress
encode_mc.onRelease = function() {
    encoder();
};
function encoder() {
    encoded = "";
    trace("Encoder Function");
    for (i=0; i<decoded.length; i++) {
        currentChar = decoded.charAt(i);
        trace("Current: "+currentChar);
        if ((num.indexOf(currentChar, i)) != (-1)) {
            charPos = num.indexOf(currentChar, i);
            encodedChar = numE.charAt(charPos);
            encoded += encodedChar;
            trace("E: "+encodedChar);
        }
        if ((firstRow.indexOf(currentChar, i)) != (-1)) {
            charPos = firstRow.indexOf(currentChar, i);
            encodedChar = firstRowE.charAt(charPos);
            encoded += encodedChar;
            trace("E: "+encodedChar);
        }
        if ((secondRow.indexOf(currentChar, i)) != (-1)) {
            charPos = secondRow.indexOf(currentChar, i);
            encodedChar = secondRowE.charAt(charPos);
            encoded += encodedChar;
            trace("E: "+encodedChar);
        }
        if ((thirdRow.indexOf(currentChar, i)) != (-1)) {
            charPos = thirdRow.indexOf(currentChar, i);
            encodedChar = thirdRowE.charAt(charPos);
            encoded += encodedChar;
            trace("E: "+encodedChar);
        }
        trace(encoded);
    }
}

If you need clarification, ask…