/*********************************************************************
/ Morse.js
/
/ JavaScript functions for translating Morse code.  See also
/ Morse.html
/
/ The user can type English in the top box or Morse Code in the
/ bottom box, then click "To Morse" or "To English" to translate
/ between the two.
/
/ Only lower-case letters and spaces are supported.  Upper-case
/ letters are downshifted, and all other characters (including
/ line- and form- feeds) are removed by the translation.
/
/ The client browser must support RegExp objects to run this page.
/ IE and NS 4.0 or higher do.
/
/ 2000.8  SJL  Initial write.
*********************************************************************/

var mexpBadMorseChar = /[^ .-]/g;
var mexpBadEnglishChar = /[^a-z ]/g;

// The alphabets are in order of descending Morse code length.  This
// forces the ToEnglish function to treat -.-- as "y" and not "-w".
// There are two Morse versions because the dot is treated as a special
// character in regular expressions and so must be escaped.
/*
a .-   b -... c -.-. d -..  e .    f ..-. g --.  h .... i ..
j .--- k -.-  l .-.. m --   n -.   o ---  p .--. q --.- r .-.
s ...  t -    u ..-  v ...- w .--  x -..- y -.-- z --..
*/

var marrMorseOut = new Array ("-...", "-.-.", "..-.", "....", ".---", ".-..",
        ".--.", "--.-", "...-", "-..-", "-.--", "--..", "-..", "--.", "-.-",
        "---", ".-.", "...", "..-", ".--", ".-", "..", "--", "-.", ".", "-");
var marrMorseIn = new Array ("-\\\.\\\.\\\.", "-\\\.-\\\.",
        "\\\.\\\.-\\\.", "\\\.\\\.\\\.\\\.", "\\\.---", "\\\.-\\\.\\\.",
        "\\\.--\\\.", "--\\\.-", "\\\.\\\.\\\.-", "-\\\.\\\.-", "-\\\.--",
        "--\\\.\\\.", "-\\\.\\\.", "--\\\.", "-\\\.-", "---", "\\\.-\\\.",
        "\\\.\\\.\\\.", "\\\.\\\.-", "\\\.--", "\\\.-", "\\\.\\\.", "--",
        "-\\\.", "\\\.", "-");
var marrEnglish = new Array ("b", "c", "f", "h", "j", "l",
        "p", "q", "v", "x", "y", "z", "d", "g", "k",
        "o", "r", "s", "u", "w", "a", "i", "m", "n", "e", "t");


function ToMorse ()
{
        var strEnglish = (document.frmMorse.txtEnglish.value).toLowerCase ();
        var strMorse;
        var intI;
        var expE;

        strMorse = strEnglish.replace (mexpBadEnglishChar, "");
        for (intI = 0; intI < marrEnglish.length; intI++)
        {
                expE = new RegExp (marrEnglish [intI], "g");
                strMorse = strMorse.replace (expE, marrMorseOut [intI] + " ");
        }

        document.frmMorse.txtMorse.value = strMorse;
}


function ToEnglish ()
{
        var strMorse = document.frmMorse.txtMorse.value + " ";
        var strEnglish;
        var intI;
        var expE;

        strEnglish = strMorse.replace (mexpBadMorseChar, "");
        for (intI = 0; intI < marrMorseIn.length; intI++)
        {
                expE = new RegExp (marrMorseIn [intI] + " ", "g");
                strEnglish = strEnglish.replace (expE, marrEnglish [intI]);
        }

        document.frmMorse.txtEnglish.value = strEnglish;
}

