/* N.B. NO TABS CAN BE ENTERED INTO AN HTML FORM SO WE DON'T NEED TO
WORRY ABOUT THEM. */

var qtmp;
var please = "Please phrase your search differently.";
var rules = "(See \"Rules\" for explanation.)";
var winid = 0;

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
function check_query()
{
	var q = document.query_form.tx.value;

	// clean up query q
	q = trim_ends(q);
	q = q.toLowerCase();
	// compress all multiple-space strings to single spaces
	var i;
	while ((i = q.indexOf("  ")) != -1)
		q = q.substring(0, i) + q.substring(i+1);

	q = expand_not(q);
	if (!valid_query(q))
		return false;
//	q = interpret(q); // see interpre.js
//	say_interpretation(q);

	var ex = "";	
	if (document.query_form.lite_xrd.checked)
		ex += "xrd";
	if (document.query_form.lite_nmr.checked)
	{
		if (ex != "")
			ex += " or ";
		ex += "nmr";
	}
	if (document.query_form.lite_mod.checked)
	{
		if (ex != "")
			ex += " or ";
		ex += "mod";
	}
	if (ex == "")
	{
		alert("Please check at least one type of coordinates to find.");
		return false;
	}
	if (ex.length >= 17) // all 3 checked
		document.query_form.ex.value = ""; // means all 3 to pdbmain
	else
		document.query_form.ex.value = ex;

  var xx = "";
  if (document.query_form.lite_nonredundant.checked)
    xx = "rep";
  document.query_form.xx.value = xx;

//	if (document.testform.test_checking.checked)
//	{
//		alert("Query is acceptable, would be submitted.\n" +
//			"Coordinates sought for: \"" + ex + "\"\n" +
//			"(query element ex = \"" + document.query_form.ex.value + "\")");
//		return false;		
//	}
	return true; // 0 is different from false here!
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
function valid_query(q)
{
	// assume ends trimmed of spaces and all spaces single

	if (q.indexOf("(") != -1 || q.indexOf(")") != -1)
	{
		alertwin("Sorry, parentheses () are not supported.\n" +
			"Precedence is left to right:\n" +
			"\"human or egg albumin\" means \"(human or egg) and albumin\".\n" +
			"Please phrase your search without parentheses.");
		return 0;
	}
	if (q.indexOf("or not") != -1)
	{
		alertwin("BAD LOGIC: \"or not\"\n" + please);
		return 0;
	}
/*
	if (q.indexOf("*") != -1 && q.indexOf("not") != -1)
	{
		alertwin("\"not\" with \"*\" is not supported.\n" +
			"Please use only one or the other.");
		return 0;
	}
*/

	// reject not followed by loose term
	var i0 = 0;
	var i1;
	var i2;
	while(1)
	{
		i1 = q.indexOf(" not ", i0);
		i2 = q.indexOf("not ", 0);
//		alert("i0=" + i0 + " i1=" + i1 + " i2=" + i2)
		if (i1 == -1 /* there is no imbedded " not " */
			&& (i0 != 0 || i2 != 0)) // there is no leading "not "
			break;

		if (i0 == 0 && i2 == 0)
			i1 = 4;
		else
			i1 = i1 + 5;
//	alert("i0=" + i0 + " i1=" + i1 + " i2=" + i2)
		while(q.charAt(i1) != " ")
		{
//		alert(q.charAt(i1));
			if (q.charAt(i1) == "*")
			{
				alertwin("Sorry, terms following \"not\" (or \"-\") may not include asterisk (*).\n"
					+ please);
				return 0;
			}
			i1++;
			if (i1 >= q.length)
				break;
		}
		i0 = i1;
//	alert("pre while " + i0 + " '" + q.charAt(i0) + "'");
		if (i0 >= q.length)
			break;
//	if (!confirm("while looping"))	break;
	}

	var qq;
	// delete asterisks not embedded in words
	qq = substitute(" ", "* ", q);
	qq = substitute(" ", " *", qq);
	// leading asterisk OK
	if (qq.charAt(0) == "*")
		qq = qq.substring(1);
	// terminal asterisk OK
	if (qq.charAt(qq.length - 1) == "*")
		qq = qq.substring(0, qq.length - 1);
	// if there are any asterisks remaining, they are embedded
	if (qq.indexOf("*") != -1)
	{
		alertwin("Asterisks (*) may not be embedded in words.\n" + please);
		return 0;
	}

	return 1;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
function expand_not(q)
{
	var i0 = 0;
	while ((i1 = q.indexOf("-", i0)) != -1)
	{
		i0 = i1 + 1;
		// skip if preceded by nonspace (ok if 1st char in query)
		if (i1 - 1 > 0)
			if (q.charAt(i1 - 1) != " ")
				continue;
		// skip if last char
		if (i1 == (q.length - 1))
			continue
		// skip if followed by space
		if (i1 + 1 < q.length)
			if (q.charAt(i1 + 1) == " ")
				continue;
		// dash is 1st char in query or preceded by space and followed by nonspace
		q = q.substring(0, i1) + "not " + q.substring(i1 + 1);

		// we already incremented i0 by one
		i0 += 3;
//		alert(i0 + " in \"" + q + "\" is " + q.charAt(i0));
	}
//	if (document.testform.test_checking.checked)
//		alert("After expansion of -x to not x:\n" + q);
	return (q);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
function alertwin(msg)
{
	alert(msg);
/* The problem with these is that they can be pushed in back and lost.
	window.open("alertwin.htm?v1=" + msg,
		"AlertWin" + winid++,
		"scrollbars=1,menubar=0,width=400,height=150,resizable=0");
*/
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
function substitute(news, olds, s)
{
	var at = 0;
	var front;
	var rear;
	var result;
	var delta = news.length - olds.length;
	while((at = s.indexOf(olds, at)) != -1)
	{
// These methods fail e.g. when news=\\ and olds=\
//		front = remove_after(olds, s);
		front = s.substring(0, at);
//		rear = start_after(olds, s);
		rear = s.substring(at + olds.length, s.length);
		s = front + news + rear;
//		alert(front + "\n" + rear + "\n" + s);
		at += delta + 1;
	}
	return(s);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* first_word(s) returns the first word of s, leaving s without its first word.
Thus, calling first_word(s) repeatedly chops one word off of s each time.
Assumes both ends are trimmed!
*/
function first_word(s)
{
	var f;
	var i1;
	i1 = s.indexOf(" ");
	if (i1 == -1)
	{
		f = s;
		s = "";
	}
	else
	{
		f = s.substring(0, i1);
		s = s.substring(i1 + 1);
	}
	s = trim_left(s);
	qtmp = s;
	return(f);
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
function trim_ends(s) // remove whitespace at both ends
{
	s = trim_left(s);
	return(trim_right(s));
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
function trim_left(s)
{
	s0 = 0;
	while (s.charAt(s0) == ' ' || s.charAt(s0) == '\t')
		s0++;
	return(s.substring(s0));
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
function trim_right(s)
{
	s1 = s.length - 1;
	while (s.charAt(s1) == ' ')
		s1--;
	return(s.substring(0, s1 + 1));
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */


