//============================================================+
// Convert morse<->alphanumeric|| Author:                     |
// A JavaScript Program        ||-----------------------------|
//                             || Nicola Asuni                |
//                             || Via Ugo Foscolo n.19        |
// ----------------------------|| 09045 Quartu Sant'Elena (CA)|
// start          : 29/11/2000 || info@technick.net           |
// last update    : 30/11/2000 || www.technick.net            |
//============================================================+
// (c) Copyright 1999-2000 Tecnick.com s.r.l.

//variables and arrays
var xi=0;

//Alpanumeric to morse array
//am[xi++] = new Array("Alpanumeric","Morse","Phonetic");
am = new Array();
am[xi++] = new Array(" "," "," - ");
am[xi++] = new Array("A",".-","Alfa");
am[xi++] = new Array("B","-...","Bravo");
am[xi++] = new Array("C","-.-.","Charlie");
am[xi++] = new Array("D","-..","Delta");
am[xi++] = new Array("E",".","Echo");
am[xi++] = new Array("F","..-.","Foxtrot");
am[xi++] = new Array("G","--.","Golf");
am[xi++] = new Array("H","....","Hotel");
am[xi++] = new Array("I","..","India");
am[xi++] = new Array("J",".---","Juliet");
am[xi++] = new Array("K","-.-","Kilo");
am[xi++] = new Array("L",".-..","Lima");
am[xi++] = new Array("M","--","Mike");
am[xi++] = new Array("N","-.","November");
am[xi++] = new Array("O","---","Oscar");
am[xi++] = new Array("P",".--.","Papa");
am[xi++] = new Array("Q","--.-","Quebec");
am[xi++] = new Array("R",".-.","Romeo");
am[xi++] = new Array("S","...","Sierra");
am[xi++] = new Array("T","-","Tango");
am[xi++] = new Array("U","..-","Uniform");
am[xi++] = new Array("V","...-","Victor");
am[xi++] = new Array("W",".--","Whisky");
am[xi++] = new Array("X","-..-","X-ray");
am[xi++] = new Array("Y","-.--","Yankee");
am[xi++] = new Array("Z","--..","Zulu");

//numbers
am[xi++] = new Array("0","-----","Zero");
am[xi++] = new Array("1",".----","One");
am[xi++] = new Array("2","..---","Two");
am[xi++] = new Array("3","...--","Three");
am[xi++] = new Array("4","....-","Four");
am[xi++] = new Array("5",".....","Five");
am[xi++] = new Array("6","-....","Six");
am[xi++] = new Array("7","--...","Seven");
am[xi++] = new Array("8","---..","Eight");
am[xi++] = new Array("9","----.","Nine");

//puntuaction
am[xi++] = new Array(".",".-.-.-","(Period)");
am[xi++] = new Array("?","..--..","(Question Mark)");
am[xi++] = new Array(",","--..--","(Comma)");
am[xi++] = new Array("-","-....-","(Hyphen)");
am[xi++] = new Array(":","---...","(Colon)");
am[xi++] = new Array(";","-.-.-","(Semicolon)");
am[xi++] = new Array("(","-.--.-","(Parentheses)");
am[xi++] = new Array(")","-.--.-","(Parentheses)");
am[xi++] = new Array("_","..--.-","(Underline)");
am[xi++] = new Array("/","-..-.","(Fraction Bar)");
am[xi++] = new Array("'",".----.","(Apostrophe)");

//special chars
am[xi++] = new Array("&Aacute;",".--.-","(A acute accent)");
am[xi++] = new Array("&Agrave;",".--.-","(A acute accent)");
am[xi++] = new Array("&Eacute;","..-..","(E acute accent)");
am[xi++] = new Array("&Egrave;","..-..","(E acute accent)");
am[xi++] = new Array("&Auml;",".-.-","(A umlaut)");
am[xi++] = new Array("&Ntilde;","--.--","(N tilde)");
am[xi++] = new Array("&Ouml;","---.","(O umlaut)");
am[xi++] = new Array("&Uuml;","..--","(U umlaut)");
am[xi++] = new Array("&AElig;",".-.-","(AE ligature)");
am[xi++] = new Array("&Oslash;","---.","(O slash)");
am[xi++] = new Array("&Aring;",".--.-","(A ring)");
am[xi++] = new Array("$","...-..-","(Dollar sign)");

//special codes
am[xi++] = new Array("(KN)","-.--.","");
am[xi++] = new Array("(AR)",".-.-.","(End of Message)");
am[xi++] = new Array("(CH)","----","");
am[xi++] = new Array("(ATTENTION)","-.-.-","(Attention)");
am[xi++] = new Array("(GO AHEAD)","-.-","(Invitation to transmit)");
am[xi++] = new Array("(SK)","...-.-","(End of Contact)");
am[xi++] = new Array("(SOS)","...---...","(International distress call)");
am[xi++] = new Array("(V)","...-","(Test letter)");
am[xi++] = new Array("(OK)",".-.","(Received)");
am[xi++] = new Array("(AS)",".-...","(Wait sign)");
am[xi++] = new Array("(E)",".","(Word understand)");
am[xi++] = new Array("(T)","-","(Word not understand)");
am[xi++] = new Array("(T)","-","(Number not understand)");
am[xi++] = new Array("(VE)","...-.","(Full text understand)");
am[xi++] = new Array("(IMI)","..--..","(Full text not understand)");
am[xi++] = new Array("(AR)",".-.-.","(Ende of transmission)");
am[xi++] = new Array("(EB)",".-...","(Wait)");
am[xi++] = new Array("(RF)",".-...-.","(Signalisation is bad)");
am[xi++] = new Array("(SL)","....-..","(Slower)");
am[xi++] = new Array("(PP)",".--..--.","(Lights lower)");
am[xi++] = new Array("(LL)",".-...-..","(Lights stronger)");
am[xi++] = new Array("(AB)",".--...","(Break the transmission)");
am[xi++] = new Array("(ERROR)","........","(Error)");

// --------------------------------------------------------------------------
// Convert Alphanumeric text in Morse Code
// --------------------------------------------------------------------------
function Alpha2Morse() 
{
  var i=0, j=0; //iterators
  var ch=""; //one char
  //reset morse txt
  document.morseform.morsetxt.value = "";
  
  //convert text to uppercase
  t = document.morseform.alphanumtxt.value.toUpperCase();
  tl = t.length;
  document.morseform.alphanumtxt.value = t;
  
  for(i=0; i<tl; i++)
  {
	ch = t.charAt(i);
	
	if (ch=="\n") {document.morseform.morsetxt.value += "\n";}
	else for(j=0; j<xi; j++)
	{
	if (ch==am[j][0])
		{ 
		document.morseform.morsetxt.value += am[j][1] + " "; 
		break;
		}
   	}
  }
 SpellText();
 return;
}

// --------------------------------------------------------------------------
// Convert Morse Code in Alphanumeric text 
// --------------------------------------------------------------------------
function Morse2Alpha() 
{
   var i=0, j=0; //iterators
   var t = ""; 
   var m = "";
   var s = 0; 
   var idx = 1;

// Read CW window...
   m = document.morseform.morsetxt.value;
   len = m.length;
   m += " ";
        
   while((idx != -1) && (idx < len)) 
   {
	  // Find first/next space and extract next Morse character...
      idx = m.indexOf(" ", s);
      if(idx != -1) 
	  {
		m1 = m.substring(s, idx);

		// Match Morse character with Array and copy Alpha to text...
         for(j=0; j<xi; j++) 
		 { if(m1 == am[j][1]) {t = t + am[j][0]; break; } }
      }

// Add an extra space between words...
      if(m.substring(idx+1, idx+2) == " ") {t = t + " "; idx++;}

// If CW character is not matched, move pointer and try again...
// (Helps with unpredictable characters introduced during cutting
//  and pasting into the CW window)...
      if((j==xi) && (m.substring(s,idx+1) != " ") && (idx < len)) 
	  {idx = idx - m1.length;}           
      idx++;
      s = idx;
   }

// Write text to text window...
   document.morseform.alphanumtxt.value = t;
 SpellText();
return;  
}

// --------------------------------------------------------------------------
// Spell text
// --------------------------------------------------------------------------
function SpellText() 
{
  var i=0, j=0; //iterators
  var ch=""; //one char
  
  document.morseform.spelltxt.value = "";
  //convert text to uppercase
  t = document.morseform.alphanumtxt.value.toUpperCase();
  tl = t.length;
  
  for(i=0; i<tl; i++)
  {
	ch = t.charAt(i);
	
	if (ch=="\n") {document.morseform.spelltxt.value += "\n";}
	else for(j=0; j<xi; j++)
	{
	if (ch==am[j][0])
		{ 
		document.morseform.spelltxt.value += am[j][2] + " "; 
		break;
		}
   	}
  }
 return;
}

// -------------------------------------------------------------------------
// END OF SCRIPT
// -------------------------------------------------------------------------



