var alpre = new Array();
var alpost = new Array();

var ss = " "; // "*" for debugging

function init_aliases()
{
	var i;
	var s;
	for (i = 0; i < alias.length; i++)
	{
		if (alias[i] == "")  // allow for empty placeholders
			alpre[i] = "";
		else
		{
			s = alias[i].indexOf(" ");
			if (s == -1)
			{
				badalias("An alias without a space exists\n" +
					"which cannot be interpreted!\n");
				break;
			}
				
			alpre[i] = ss + alias[i].substring(0, s + 1);
			if (alpre[i] == " a " || alpre[i] == " t " || alpre[i] == " g " ||
				alpre[i] == " c " || alpre[i] == " u ")
			{
				bad_alias("ERROR: You cannot alias A, T, G, C, or U\n" +
					"because they are DNA/RNA residue names!\n");
				break;					
			}
			alpost[i] = alias[i].substring(s) + ss;
		}
	}
}

function bad_alias(msg)
{
	alert(msg + "\nThe alias feature will not work properly\n" +
		"until you fix the alias definitions and reload!\n" +
		"(Please edit file shared\\alias.js)");
}

function setup_show_aliases()
{
	var i;
	var s = "";
	for (i = 0; i < alias.length; i++)
	{
		if (alpre[i] != "")
			s += "<br>\"" + alpre[i] + "\"  \"" + alpost[i] + "\"\n";
		if (alpre[i].substring(0,4) == " coh")
		{
			s += "<font color=red>";
			s += "<br> &nbsp; &nbsp; Trailing \"]\" will be added during coh expansion.";
			s += "</font>";
		}
		if (alpost[i].substring(0,4) == " tb ")
		{
			s += "<font color=red>";
			s += "<br> &nbsp; &nbsp; \"tb\" is a <a href='hlp_cmd2.htm#cpe'>";
			s += "Command to PE</a>";
			s += "</font>";
		}
		if (alpre[i].substring(0,4) == " db ")
		{
			s += "<font color=red>";
			s += "<br> &nbsp; &nbsp; All \"db..\" aliases are for debugging";
			s += " (not for routine use).";
			s += "</font>";
		}
	}

	top.s_tmp = s;
}

function show_aliases()
{
	setup_show_aliases();
	top.help_aliases();
}

function alias_cmd(cmd)
{
	var i;
	if (!top.pref_alias_expansion)
		return cmd;

	// guarantee exactly one space at each end
	cmd = trim_ends(cmd);
	if (cmd == "go")
		return("unpause"); // this alias must survive if alias.js is customized.
	cmd = " " + cmd + " ";

	// pad word delimiters (,) with spaces
	cmd = substitute(" ( ", "(", cmd);
	cmd = substitute(" ) ", ")", cmd);
	cmd = substitute(" , ", ",", cmd);
	cmd = substitute(" ; ", ";", cmd);

	// expand aliases
	for (i = 0; i < alias.length; i++)
	{
		if (alpre[i] != "")
		{
//			if (alpre[i].substring(0,3) == " ss")
//				top.echo("!" + cmd);
			cmd = substitute(alpost[i], alpre[i], cmd);
//			if (alpre[i].substring(0,3) == " ss")
//				top.echo("!" + cmd);
		}
	}

	// singularize double spaces
	while(cmd.indexOf("  ") != -1)
		cmd = substitute(" ", "  ", cmd);

	// pack (,)
	cmd = substitute(" (", " ( ", cmd);
	cmd = substitute(") ", " ) ", cmd);
	cmd = substitute(", ", " , ", cmd);
	cmd = substitute("; ", " ; ", cmd);
	if (cmd.indexOf("color [x") != -1 && cmd.indexOf("]") == -1)
	{
		cmd = trim_ends(cmd);
		cmd += "]";
		cmd = substitute("[x", "[x ", cmd);
	}

	// NOT NECESSARY SINCE CAPITALIZATION WORKS
	// strip leading periods from escaped-aliases (e.g. .a -> a)
	// cmd = substitute(" ", " .", cmd);

	return(trim_ends(cmd));
}

