/*
morse code and phonetic alphabet in javascript
Author: Glenn Satchell, glenn@uniq.com.au, 31 October 2006
This code may be freely used for any purpose.

Requires:
<FORM ID="convert">
<INPUT TYPE="text" NAME="string">
<DIV ID="code">
*/

var dot  = '<IMG SRC="/images/dot.gif" ALT=".">';
var dash = '<IMG SRC="/images/dash.gif" ALT="-">';
var space = '<IMG SRC="/icons/blank.gif" ALT=" ">';

var morse = new Array();
morse['A'] = dot + dash;
morse['B'] = dash + dot + dot + dot;
morse['C'] = dash + dot + dash + dot;
morse['D'] = dash + dot + dot;
morse['E'] = dot;
morse['F'] = dot + dot + dash + dot;
morse['G'] = dash + dash + dot;
morse['H'] = dot + dot + dot + dot;
morse['I'] = dot + dot;
morse['J'] = dot + dash + dash + dash;
morse['K'] = dash + dot + dash;
morse['L'] = dot + dash + dot + dot;
morse['M'] = dash + dash;
morse['N'] = dash + dot;
morse['O'] = dash + dash + dash;
morse['P'] = dot + dash + dash + dot;
morse['Q'] = dash + dash + dot + dash;
morse['R'] = dot + dash + dot;
morse['S'] = dot + dot + dot;
morse['T'] = dash;
morse['U'] = dot + dot + dash;
morse['V'] = dot + dot + dot + dash;
morse['W'] = dot + dash + dash;
morse['X'] = dash + dot + dot + dash;
morse['Y'] = dash + dot + dash + dash;
morse['Z'] = dash + dash + dot + dot;
morse['1'] = dot + dash + dash + dash + dash;
morse['2'] = dot + dot + dash + dash + dash;
morse['3'] = dot + dot + dot + dash + dash;
morse['4'] = dot + dot + dot + dot + dash;
morse['5'] = dot + dot + dot + dot + dot;
morse['6'] = dash + dot + dot + dot + dot;
morse['7'] = dash + dash + dot + dot + dot;
morse['8'] = dash + dash + dash + dot + dot;
morse['9'] = dash + dash + dash + dash + dot;
morse['0'] = dash + dash + dash + dash + dash;
morse[' '] = '<BR>';
morse['.'] = dot + dash + dot + dash + dot + dash;
morse[','] = dash + dash + dot + dot + dash + dash;
morse[':'] = dash + dash + dash + dot + dot + dot;
morse['?'] = dot + dot + dash + dash + dot + dot;
morse['\''] = dot + dash + dash + dash + dash + dot;
morse['-'] = dash + dot + dot + dot + dot + dash;
morse['/'] = dash + dot + dot + dash + dot;
morse['('] = dash + dot + dash + dash + dot;
morse[')'] = dash + dot + dash + dash + dot + dash;
morse['"'] = dot + dash + dot + dot + dash + dot;
morse['='] = dash + dot + dot + dot + dash;
morse['@'] = dash + dash + dash + dot + dash + dash + dot;
morse['*'] = dash + dot + dot + dash;

var phonetic = new Array();
phonetic['A'] = 'alpha';
phonetic['B'] = 'bravo';
phonetic['C'] = 'charlie';
phonetic['D'] = 'delta';
phonetic['E'] = 'echo';
phonetic['F'] = 'foxtrot';
phonetic['G'] = 'golf';
phonetic['H'] = 'hotel';
phonetic['I'] = 'india';
phonetic['J'] = 'juliet';
phonetic['K'] = 'kilo';
phonetic['L'] = 'lima';
phonetic['M'] = 'mike';
phonetic['N'] = 'november';
phonetic['O'] = 'oscar';
phonetic['P'] = 'papa';
phonetic['Q'] = 'quebec';
phonetic['R'] = 'romeo';
phonetic['S'] = 'sierra';
phonetic['T'] = 'tango';
phonetic['U'] = 'uniform';
phonetic['V'] = 'victor';
phonetic['W'] = 'whiskey';
phonetic['X'] = 'x-ray';
phonetic['Y'] = 'yankee';
phonetic['Z'] = 'zulu';
phonetic['1'] = 'one';
phonetic['2'] = 'two';
phonetic['3'] = 'three';
phonetic['4'] = 'four';
phonetic['5'] = 'five';
phonetic['6'] = 'six';
phonetic['7'] = 'seven';
phonetic['8'] = 'eight';
phonetic['9'] = 'niner';
phonetic['0'] = 'zero';
phonetic['.'] = 'stop';
phonetic[','] = 'comma',
phonetic['!'] = 'exclaim';
phonetic['-'] = 'dash';
phonetic['='] = 'equals';
phonetic['+'] = 'plus';
phonetic['?'] = 'question';
phonetic[' '] = '<BR>';

/* convert the given parameter to the morse code or phonetic alphabet */
function show_codes(codeType) {
    /* initial strings to hold heading and code */
    var head = '';
    var code = '';
    /* get the value from the form */
    word = document.getElementById('convert').string.value.toUpperCase();
    if (word == '') word = 'BE PREPARED';
    /* only display the requested code */
    if (codeType == 'morse') {
	/* convert each char to morse and phonetic */
	for (var i = 0; i < word.length; i++) {
	    var c = word.charAt(i);
	    if (morse[c]) {
		code = code + ' ' + c + ' ' + morse[c];
		head = head + c;
	    }
	}
	document.getElementById('code').innerHTML = '<h3>' + head + '</h3>' + code;
    }
    else if (codeType == 'phonetic') {
	/* convert each char to morse and phonetic */
	for (var i = 0; i < word.length; i++) {
	    var c = word.charAt(i);
	    if (phonetic[c]) {
		code = code + ' ' + phonetic[c];
		head = head + c;
	    }
	}
	document.getElementById('code').innerHTML = '<h3>' + head + '</h3>' + code;
    }
    else {
	document.getElementById('code').innerHTML = '';
    }
    return true;
}

