// JavaScript Document

/*
AutoBoolean forces words to be separated by a given boolean operator
in the named search boxes.
*/


function AutoBoolean(BooleanChar, targetList)
{
	var sep = "|";
	var targetArray = new Array();
	targetArray = targetList.split(sep);
	var i = 0;
	var CurrentTarget;
	var InitialTargetValue;
	
	
	
	for (i=0;i<targetArray.length;i++)
	{
		CurrentTarget = document.forms.qbe_form.elements[targetArray[i]];
		InitialTargetValue = CurrentTarget.value
	  InitialTargetValue = InitialTargetValue.replace(/ of | an | and | by | for | from | in | of | on | the | to | with /g, " ")
		if ( ! hasOperator(InitialTargetValue) )
		{
			CurrentTarget.value = makeQuery(BooleanChar, splitStr(InitialTargetValue, /\s+/));
		  
		}		
	}

}

/*
hasOperator() checks for existence of Inmagic boolean chars and others:
chars: & / ! ( ) "
*/
function hasOperator(str)
{
	//return str.match(/&|!|\||\(|\)|\"|\'/)
	return str.match(/&|!|\/|\(|\)|\"/)
}


function splitStr(strAddr, splitWith)
{

        var tmpStr  = new String(strAddr);
        return tmpStr.split(splitWith);

}


function makeQuery(BooleanChar, myArray)
{

	if ( ! myArray.length  )
	{ return '';	}

	var tempArray = new Array();
	var j = 0;
	tempArray[j++] = myArray[0];

	var i;
	for ( i = 1; i < myArray.length; i++ )
	{
		tempArray[j++] = BooleanChar;
		tempArray[j++] = myArray[i];
	}

	return tempArray.join(' ');

}
