var latin_field, piqad_field;
var latin = [ "a", "b", "ch", "D", "e", "gh", "H", "I", "j", "l", "m", "n",
    "ng", "o", "p", "q", "Q", "r", "S", "t", "tlh", "u", "v", "w", "y", "’",
    "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
    ",", ".", "[mummification glyph]" ];
var piqad = [ "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", ""];
function init() {
    f = document.forms[0];
    latin_field = f.elements["latin"];
    piqad_field = f.elements["pIqaD"];
}

function clear_text(n) {
    // n = 1: clear latin
    // n = 2: clear both
    // n = 3: clear piqad
    if (n < 3) latin_field.value="";
    if (n > 1) piqad_field.value="";
}

function tolatin() {
    clear_text(1);
    var ptext = piqad_field.value;
    // loop through each letter in the pIqaD text area
    for (i = 0; i < ptext.length; i++) {
        var letter = ptext[i];
        // check for CRLF
        if (letter == "\u000D" && ptext[i+1] == "\u000A") {
            letter = "\u000D\u000A";
            i++;
        }
        var index = -1;
        // look for this letter in piqad[]
        for (j = 0; j < piqad.length; j++) {
            if (letter == piqad[j]) {
                index = j;
                break;
            }
        }
        // return text unmodified if it didn't match
        if (index == -1) latin_field.value += letter;
        else latin_field.value += latin[index];
    }
}

function topiqad() {
    clear_text(3);
    var ltext = latin_field.value;
    // loop through each letter in the pIqaD text area
    for (i = 0; i < ltext.length; i++) {
        var letter = ltext[i];
        // accept ASCII apostrophe, opening single quote, or 02BC modifier
        // letter apostrophe and make them all apostrophe
        if (letter == "'" || letter == "‘" || letter == "ʼ") letter = "’";
        // check for ch or gh
        else if ((letter == "c" || letter == "g") && ltext[i+1] == "h") {
            letter += "h";
            i++;
        }
        // check for ng
        else if (letter == "n" && ltext[i+1] == "g") {
            letter = "ng";
            i++;
        }
        // check for tlh
        else if (letter == "t" && ltext[i+1] == "l" && ltext[i+2] == "h") {
            letter = "tlh";
            i+=2;
        }
        // check for [mummification glyph]
        else if (letter == "[" && ltext.substring(i,i+21) == "[mummification glyph]") {
            letter = "[mummification glyph]";
            i+=20;
        }
        // check for CRLF
        else if (letter == "\u000D" && ltext[i+1] == "\u000A") {
            letter = "\u000D\u000A";
            i++;
        }
        var index = -1;
        // look for this letter in latin[]
        for (j = 0; j < latin.length; j++) {
            if (letter == latin[j]) {
                index = j;
                break;
            }
        }
        // return text unmodified if it didn't match
        if (index == -1) piqad_field.value += letter;
        else piqad_field.value += piqad[index];
    }
}

function show_all() {
    clear_text(2);
    for (i = 0; i < latin.length; i++) {
        latin_field.value += latin[i];
        piqad_field.value += piqad[i];
        if (i == 25 || i == 35) {
            latin_field.value += "\n\n";
            piqad_field.value += "\n\n";
        }
        else if (i < latin.length-1) {
            latin_field.value += " ";
            piqad_field.value += " ";
        }
    }
}

