/* Converts text to and from phonetic alphabet */

var nato = new Object();
nato.A = "Alpha";
nato.B = "Bravo";
nato.C = "Charlie";
nato.D = "Delta";
nato.E = "Echo";
nato.F = "Foxtrot";
nato.G = "Golf";
nato.H = "Hotel";
nato.I = "India";
nato.J = "Juliet";
nato.K = "Kilo";
nato.L = "Lima";
nato.M = "Mike";
nato.N = "November";
nato.O = "Oscar";
nato.P = "Papa";
nato.Q = "Quebec";
nato.R = "Romeo";
nato.S = "Sierra";
nato.T = "Tango";
nato.U = "Uniform";
nato.V = "Victor";
nato.W = "Whiskey";
nato.X = "X-ray";
nato.Y = "Yankee";
nato.Z = "Zulu";
// replace spaces with hyphen (can overrule later depending upon show space option)
nato[" "] = "-";

function to_phonetic()
{
	var plain_text;
	var phonetic_text = "";

	if (document.phoneticForm.plaintext.value.length == 0)
	{
		alert("No plain text. Please enter or paste plain text in the box above.");
		return;
	}

	// move form value into plain_text (as caps)
	plain_text = document.phoneticForm.plaintext.value.toUpperCase();

	for (i = 0; i < plain_text.length; i++) 
	{
		this_char = plain_text.charAt(i);
		// if failed to encode then we include char as is
		if (isdefined (nato, this_char)) this_char_enc = nato[this_char];
		else {this_char_enc = this_char;}
		// if not first we add a space to seperate from previous
		if (i != 0) phonetic_text += " ";
		phonetic_text += this_char_enc;
	}
// replace phonetic text
document.phoneticForm.phonetictext.value = phonetic_text;

}

// ## under construction
function from_phonetic()
{
	var plain_text = "";
	var phonetic_text_array = new Array();

	if (document.phoneticForm.phonetictext.value.length == 0)
	{
		alert("No phonetic text. Please enter or paste plain text in the box below.");
		return;
	}

	// split into words
	phonetic_text_array = document.phoneticForm.phonetictext.value.split(" ");

	for (i = 0; i < phonetic_text_array.length; i++) 
	{
		// note that this_char can be a string - if we are unable to convert to a single character
		this_char = lookup_nato(phonetic_text_array[i]);
		// if failed to encode then we include char as is - short string
		if (this_char == "") {this_char = phonetic_text_array[i];}
		plain_text += this_char;
	}
// replace phonetic text
document.phoneticForm.plaintext.value = plain_text;

}


// looks up findword in the nato object
// returns object name (ie key) or "" if insuccessful 
function lookup_nato (findword)
{
	// ignore case so first turn findword to caps
	findword = findword.toUpperCase();
	for (var i in nato)
	{
		testword = nato[i].toUpperCase();
	  	if (testword == findword) return i;
	}
// not found so return ""
return "";
}

// check if object is defined - ie exits in the pseudo object hash
function isdefined(object, variable)
{
	return (typeof(eval(object)[variable]) == "undefined")? false: true;
}

