var keywords_array = new Array();


function getFrameWindow(frame_name)
{
        var frame_window = null;

        if(frame_name == "_self")
        {
                return window;
        }
        if (frame_name != null && frame_name.length > 0)
        {
                frame_window = parent.frames[frame_name]
        }
        else
        {
                frame_window = window;
        }

        return frame_window;
}


function trim(string)
{
	var newstring = string;

	while (newstring.charAt(0) == " ")
	{
		newstring = newstring.substring(1, newstring.length);
	}
	while (newstring.charAt(newstring.length - 1) == " ")
	{
		newstring = newstring.substring(0, newstring.length - 1);
	}
	return newstring;
}


function makeKeywordSQML(keywords_array, operator_element)
{
	var theQuery = "";
	var stag = "<" + operator_element + ">";
	var etag = "</" + operator_element + ">";

	if (keywords_array.length == 1)
	{
		theQuery = "<term type=\"keyword\">" + keywords_array[0] + "</term>";
	}
	else
	{
		for (var loop = 0; loop < keywords_array.length; ++loop)
		{
			if ((loop + 1) == keywords_array.length)
			{
				theQuery = theQuery + "<term type=\"keyword\">" + keywords_array[loop] + "</term>";
				for (j = 0; j < (keywords_array.length - 1); ++j)
				{
					theQuery = theQuery + "</right>" + etag;
				}
			}
			else
			{
				theQuery = theQuery + stag + "<left><term type=\"keyword\">" + keywords_array[loop] + "</term></left><right>";
			}
		}
	}

	//alert("Query: " + theQuery);
	return theQuery;
}

function makeHeadingsSQML(headings_array, operator_element)
{

	var theQuery = "";
	var stag = "<" + operator_element + ">";
	var etag = "</" + operator_element + ">";

	if (headings_array.length == 1)
	{
		theQuery = "<heading id=\"" + headings_array[0] + "\"><left><term type=\"all\"/></left><right><term type=\"all\"/></right></heading>";
	}
	else
	{
		for (var i = 0; i < headings_array.length; ++i)
		{
			if ((i + 1) == headings_array.length)
			{
				theQuery += "<heading id=\"" + headings_array[i] + "\"><left><term type=\"all\"/></left><right><term type=\"all\"/></right></heading>";
				for (var j = 0; j < (headings_array.length - 1); ++j)
				{
					theQuery += "</right></or>";
				}
			}
			else
			{
				theQuery += "<or><left><heading id=\"" + headings_array[i] + "\"><left><term type=\"all\"/></left><right><term type=\"all\"/></right></heading></left><right>";
			}
		}
	}

	//alert("Query: " + theQuery);
	return theQuery;
}


function getHitlimit(theForm)
{
	var val = 300;
	var item = theForm.hitlimit.selectedIndex;
	if (item >= 0)
	{
		val = theForm.hitlimit.options[item].text;
	}
	return val;
}


function getKeywordOperator(theForm)
{
	var val = "and";

	for (var i = 0; i < theForm.operator.length; ++i)
	{
		if (theForm.operator[i].checked == true)
		{
			val = theForm.operator[i].value;
			break;
		}
	}
	return val;
}


function getSelections(theSelect)
{
	var selections = new Array();
	for (var i = 0; i < theSelect.length; ++i)
	{
		if (theSelect[i].selected)
		{
		  if (theSelect[i].value != "") { selections[selections.length] = theSelect[i].value; }
		}
    }
	return selections;
}


function hasSelections()
{
	if (keywords_array.length > 0) { return true; }
	return false;
}


function setArrays(theForm)
{
	var theKeywords = trim(theForm.keywords.value);
	if (theKeywords.length > 0)
	{
		keywords_array = theKeywords.split(" ");
	}
}

function submitQuery()
{
	var PROXIMITY_RANGE = 5;
	var theForm = document.search_form;
	var theIndex = "";
	var keywordQuery = "";
	var theQuery = "";
	var theOperator = "";
	var theHitlimit = 300;

	setArrays(theForm);

	//theOperator = getKeywordOperator(theForm);
	theOperator = "and";

	theHitlimit = "50";

	if (keywords_array.length > 0)
	{
		keywordQuery = makeKeywordSQML(keywords_array, theOperator);
	    keywordQuery = "<and><left><term type=\"all\"/></left><right>" + keywordQuery + "</right></and>";
	}
	if (keywordQuery.length > 0)
	{
		theQuery = keywordQuery;
	}

    //theQuery = "<query><hitlimit>" + theHitlimit + "</hitlimit>" + theQuery + "</query>";
    theQuery = "<query>" + theQuery + "</query>";
    //alert(theQuery);
    theForm.query.value = theQuery;

	return true;
}


