// copyright 2006, Peter Tyrrell, Andornot Consulting

/* EVENTS */
// attach rewind to page load
addEvent(window, "load", RewindSearch);

// event listener
function addEvent(o,e,f)
{
	if (o.addEventListener){ o.addEventListener(e,f,true); return true; }
	else if (o.attachEvent){ return o.attachEvent("on"+e,f); }
	else { return false; }
}

/* HELPERS */

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };
String.prototype.trimSeparator = function(separator) 
{
	var output = this;
	if (output.indexOf(separator + separator) != -1)
	{
		output = output.replace(separator + separator, separator);	
	}
	if (output.indexOf(separator) == 0)
	{
		output = output.substring(separator.length, output.length);
	}
	else if (output.lastIndexOf(separator) == (output.length - separator.length))
	{
		output = output.substring(0, (output.length - separator.length));
	} 
	return output;
}

/*
GetQueryParamByKey() returns the query parameter value if any match found for key.
*/
function GetQueryParamByKey(key, strQuery) // returns string
{
	var output = "";
	if (!strQuery)
	{
		strQuery = window.location.search.substring(1);
	}
	var arrPairs = strQuery.split("&");
	var i = 0, pos = 0;
	
	for (i=0;i<arrPairs.length;i++)
	{
		pos = arrPairs[i].indexOf('=');
		if (pos >= 0)
		{
			if (key.toLowerCase() == arrPairs[i].substring(0,pos).toLowerCase())
			{
				output = arrPairs[i].substring(pos+1);
				break;
			}
		}
	}
	return output;
}

/*
ResetAll() clears hidden inputs that contain QIx values
*/

function ResetAll()
{
    var oInmagicForm;
    var i;
	
	oInmagicForm = document.getElementById("qbe_form");
	if (!oInmagicForm)
	{
		oInmagicForm = document.getElementsByName("qbe_form")[0];
	}
	if (oInmagicForm)
	{
	    for (i = 0; i < oInmagicForm.elements.length; i++)
	    {
	        if (oInmagicForm.elements[i].type == "hidden" && oInmagicForm.elements[i].name.toUpperCase().substring(0,2) == "QI")
	        {
	            oInmagicForm.elements[i].value = "";
	        }    
	    }
		
	}    
}

/*
ResetAll(oForm) is an overload that doesn't assume qbe_form
*/

function ResetAll(oForm)
{
    var i;
	
	if (oForm)
	{
	    for (i = 0; i < oForm.elements.length; i++)
	    {
	        if (oForm.elements[i].type == "hidden" && oForm.elements[i].name.toUpperCase().substring(0,2) == "QI")
	        {
	            oForm.elements[i].value = "";
	        }    
	    }
		
	}    
}

/*
ValidateForm(oForm) ensures some search text has been entered in QIx or QY inputs
*/

function ValidateForm(oForm)
{
    var isValid = false;
    var i;
	
    for (i = 0; i < oForm.elements.length; i++)
    {
        if (oForm.elements[i].name.toUpperCase().substring(0,2) == "QI" || oForm.elements[i].name.toUpperCase().substring(0,2) == "QY")
        {
            if (oForm.elements[i].value != "")
            {
                isValid = true;
                return true;
            }
        }
    }
		
	alert("Please enter search criteria.");
	return false;
}

/*

*/

function GetTextbaseName()
{
	var output = "";
	var target = document.getElementById('TN');
	if (target == null)
	{
		target = document.getElementsByName('TN')[0];	
		output = target.value;
	}
	return output;
}



/*********************************************************************
AutoBoolean "google-izes" Inmagic text inputs:
forces words/terms 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;
	var oForm;
	
	oForm = document.forms.qbe_form;
	if (oForm == null)
	{
		oForm = document.forms[0];		
	}
	
	for (i=0;i<targetArray.length;i++)
	{
		CurrentTarget = oForm.elements[targetArray[i]];
		InitialTargetValue = CurrentTarget.value.trim();
		CurrentTarget.value = makeAutobooleanQuery(BooleanChar, splitStr(InitialTargetValue, /("[^"]*")|([^"\s]+(\s|$))/g));
	}
}

function splitStr(strAddr, splitWith)
{
        var tmpStr  = new String(strAddr);
        return tmpStr.match(splitWith);
}

function makeAutobooleanQuery(BooleanChar, myArray)
{
	if (!myArray)
	{ 
		return '';
	}

	var tempArray = new Array();
	var i = 0;
	var j = 0;
	var str = '';
	var AddBooleanNext = true;

	for (i = 0; i < myArray.length; i++)
	{
		str = myArray[i].trim();
		
		if (str != '')
		{	
				switch (str)
				{
					case '!':
						tempArray[j++] = str;
						if (j > 0)
						{
							AddBooleanNext = false;
						}
						break;
					case '/':
						tempArray[j++] = str;
						if (j > 0)
						{
							AddBooleanNext = false;
						}
						break;
					case '&':
						tempArray[j++] = str;
						if (j > 0)
						{
							AddBooleanNext = false;
						}
						break;
					case ':':
						tempArray[j++] = str;
						if (j > 0)
						{
							AddBooleanNext = false;
						}
						break;
					case 'a':
						break;
					case 'an':
						break;
					case 'and':
						break;
					case 'by':
						break;
					case 'de':
						break;
					case 'des':
						break;
					case 'du':
						break;
					case 'et':
						break;
					case 'for':
						break;
					case 'from':
						break;
					case 'in':
						break;
					case 'l':
						break;
					case 'la':
						break;
					case 'les':
						break;
					case 'of':
						break;
					case 'on':
						break;
					case 'par':
						break;
					case 'pour':
						break;
					case 'sur':
						break;
					case 'the':
						break;
					case 'to':
						break;
					case 'with':
						break;
					default:
						if (str.match(/^(w|p){1}\d+$/g) != null)
						{
							tempArray[j++] = str;
							AddBooleanNext = false;
							break;
						}
						else
						{
							if (AddBooleanNext == true && j > 0)
							{
								tempArray[j++] = BooleanChar;
							}
							tempArray[j++] = str;
							AddBooleanNext = true;
						}
				}
			//}
			
		}
	}

	return tempArray.join(' ');

}