function prepareSearch(inForm, blnNew)
{ // all the logic to prepare the search
	// needed every time
	inForm.D.value = inForm.Ntt.value;

	if(blnNew)
	{ // called by new search
		// get the the right text term
		return adjustTheNewSearchForm(inForm);
	}
	else
	{ // refine search
		// check for boolean search
		if(hasBooleanSearchTerm(inForm.Ntt.value))
		{ // have a boolean, so swap things
			inForm.Ntx.value = "mode+matchboolean";
		}
	}
}

function hasBooleanSearchTerm(strInput)
{
	var myPat = / (OR|AND|NOT) /gi;
	return (strInput.search(myPat) > 0);
}

function getQueryVariable(queryString, variable)
{
	var vars = queryString.split("&");
	for(var i=0;i<vars.length;i++) 
	{ 
    	var pair = vars[i].split("="); 
    	if (pair[0] == variable) 
      		return pair[1]; 
	}
	return '';
}

function adjustTheNewSearchForm(inForm)
{ // for new searches
	var myUrl = '';
	var myAuctionList = '';
	var myNVals = '';
	
	if(inForm.AuctionList)
		myAuctionList = inForm.AuctionList.options[inForm.AuctionList.options.selectedIndex].value; // need to do this for the sorry macs
	
	if(inForm.changedCategory && (inForm.changedCategory.value == "1"))	// changed the category dropdown so use it's qstrign as starting point		
		myNVals = inForm.NVals.options[inForm.NVals.options.selectedIndex].value; // need to do this for the sorry macs
	else
		myNVals = "Ne=" + getQueryVariable(window.location.search.substring(1), "Ne") + "&N=" + getQueryVariable(window.location.search.substring(1), "N");
	
	if(parseInt(myAuctionList) > 0)
		myNVals += '+' + myAuctionList; // add the auction dropdown value to the N
		
	if(inForm.chkNotSold && inForm.chkNotSold.checked) // they checked to do the not sold so mess with url
		myNVals = "Ne=230&N=" + getQueryVariable(myNVals, "N") + "+232&chkNotSold=1";
	else
		myNVals = "N=" + getQueryVariable(myNVals, "N") + "&chkNotSold=0";
		
	if (inForm.defaultTextBoxText && (inForm.Ntt.value == inForm.defaultTextBoxText.value) ) inForm.Ntt.value = '';
	if(inForm.Ntt.value == '') // no text specified
		myUrl = inForm.action + "?Ntk=" + encodeURIComponent(inForm.Ntk.value) + "&" + myNVals;
	else
		myUrl = inForm.action + "?Ntk=" + encodeURIComponent(inForm.Ntk.value) + "&Ntt=" + encodeURIComponent(inForm.Ntt.value) + "&Nty=1&" + myNVals;

	// check for boolean search
	if(hasBooleanSearchTerm(inForm.Ntt.value))
	{ // have a boolean, so swap things
		myUrl += "&D=" + encodeURIComponent(inForm.Ntt.value) + "&Ntx=mode+matchboolean";
	}

	if(inForm.Ns.value != '')
		myUrl += "&Ns=" + inForm.Ns.value;
	location.href = myUrl; 
	return false;
}

function adjustTheHomeForm(inForm)
{ // called on endeca homepages
	var myUrl = '';
	var myNVals = inForm.NVals.options[inForm.NVals.options.selectedIndex].value; // need to do this for the sorry macs
	if (inForm.Ntt.value == inForm.Ntt.defaultValue ) inForm.Ntt.value = '';
	if(inForm.Ntt.value == '') // no text specified
		myUrl = inForm.action + "?" + myNVals;
	else
		myUrl = inForm.action + "?Ntk=SI_Titles&Ntt=" + encodeURIComponent(inForm.Ntt.value) + "&" + myNVals;

	// check for boolean search
	if(hasBooleanSearchTerm(inForm.Ntt.value))
	{ // have a boolean, so swap things
		myUrl += "&D=" + encodeURIComponent(inForm.Ntt.value) + "&Ntx=mode+matchboolean";
	}

	location.href = myUrl + '&ic=homepage_search';

	return false;
}
