//////////////////////////////////////////////////////////////////////////
// Copyright 2007,2008. Paul Baker Integrations, Consulting, & Software L.L.C.
//
// No unauthorized use, modification, or distribution of this code is 
// permitted without written consent and/or contractual agreement.
//
//////////////////////////////////////////////////////////////////////////

var selCount = 0;
var bWarning = false;

function doShowAll(site,catalog,template,sorton)
{
	var strURL = "/photos/netpub/server.np?quickfind&catalog=" + catalog + "&site=" + site + "&template=" + template + "&sorton=" + encodeURIComponent(sorton) ; 
	document.location = strURL;
}


function doTBSearch(pField, tbInput)
{
	clearForm();
	checkMaxSelected('lbStates',4);
	var strSearch = document.getElementById(tbInput).value;
	var aSplit = strSearch.split(' ');
	var aSearch = ['','','','','','','','','','','','','','','','','','']
	var aValues = ['','','','','','','','','']
	var aOps = ['','','','','','','','','']
	var aJoins =  ['','','','','','','','','']
	
	var tempWord = "";
	var bOpenQ = false;
	var iCounter = 0;
	
	var max = 18;
	if (aSplit.length < 10)
	{
		max = aSplit.length;
	}
	
	for (w = 0; w < max;w++)
	{
		if (aSplit[w].indexOf('"') == 0)
		{
			bOpenQ = true;
			if (tempWord == "")
			{
				tempWord = aSplit[w];
			}
			else
			{
				tempWord = tempWord + " " + aSplit[w];
			}
		}
		else if (aSplit[w].indexOf('"')> 0)
		{
			tempWord = tempWord + " " + aSplit[w];
			aSearch[iCounter] = tempWord;
			iCounter++;
			bOpenQ = false;
		}
		else
		{
			if(bOpenQ == false)
			{
				aSearch[iCounter] = aSplit[w];
				iCounter++;
			}
			else
			{
				tempWord = tempWord + " " + aSplit[w];
			}
		}
	}

	tempWord = "";
	iCounter = 0;
	for (w = 0; w < aSearch.length; w++)
	{
		tempWord = aSearch[w];
		if(w == 0)
		{
			if(tempWord.indexOf('"') == 0)
			{
				aOps[0] = "matches";
			}
			else
			{
				aOps[0] = "contains";
			}
			if(tempWord.indexOf('"') == 0)
			{
//alert(tempWord);
				tempWord=tempWord.substring(1,(tempWord.length-1));
//alert(tempWord);
			}
			aValues[0] = tempWord;
			iCounter++;
		}
		else
		{
			if(tempWord.toLowerCase() == "and")
			{
				aJoins[iCounter - 1] = "and";
			}
			else if(tempWord.toLowerCase() == "or")
			{
				aJoins[iCounter - 1] = "or";
			}
			else
			{
				if(tempWord.indexOf('"') == 0)
				{
					aOps[iCounter] = "matches";
				}
				else
				{
					aOps[iCounter] = "contains";
				}
				//aJoins[iCounter] = "and";
				aValues[iCounter] = tempWord;
				iCounter++;
			}
		}
	}

//alert(pField);
//alert(aValues.toString());
//alert(aOps.toString());
//alert(aJoins.toString());

	var nfCount = 0;
	var baseField = 'af2field';
	var baseOp = 'af2op';
	var baseValue = 'af2value';
	var baseJoin = 'af2join';
	
	///
	var lbSelect = document.getElementById('lbStates');
	var maxSelected = 4;
	var selectedOptions = 0;
	  
	  for (var s = 0; s < lbSelect.options.length; s++) 
	  {
		if (lbSelect.options[s].selected) 
		{
		  if (selectedOptions < maxSelected) 
		  {
			  if(lbSelect.options[s].value !="")
			  {
				nfCount++
				document.getElementById(baseField + nfCount).value='BLM Office';
				document.getElementById(baseOp + nfCount).value='starts with';
				document.getElementById(baseValue + nfCount).value=lbSelect.options[s].value;
				document.getElementById(baseJoin + nfCount).value='or';
				
					
				selectedOptions++;
			  }
		  } 
		}
	  }
	if(nfCount > 0)
	{
		document.getElementById(baseJoin + nfCount).value='and';
	}
	
	for(i = 0; i <= 4; i++)
	{
		nfCount++
		
		if(aValues[i]!="")
		{
			document.getElementById(baseField + nfCount).value=pField;
			document.getElementById(baseOp + nfCount).value=aOps[i];
			document.getElementById(baseValue + nfCount).value=aValues[i];
		}

		if (nfCount < 9)
		{
			//alert(i);
			document.getElementById(baseJoin + nfCount).value=aJoins[i];
		}		
	}
	
	if(aValues[4] != "")
	{
		alert("You have entered more than 4 keywords or phrases.\n\n Additional keywords or phrases will be ignored.");
		
	}
	
	document.af2.submit();

//alert('here');
//return true;
}



function doKeywordSearchSubmit(e)
{
	var key;
	 if(window.event)
	 {
          key = window.event.keyCode;     //IE
     
	 }
	 else
	 {
          key = e.which;     //firefox
	 }
	 
     if(key == 13)
	 {
          doTBSearch('Keywords','tbKeywords');
	 }
	 else
	 {
          return true;
	 }

}


function clearForm()
{
		document.af2.field.value="";
		document.af2.op.value="";
		document.af2.value.value="";
		document.af2.join.value="";
		document.af2.field2.value="";
		document.af2.op2.value="";
		document.af2.value2.value="";
		document.af2.join2.value="";
		document.af2.field3.value="";
		document.af2.op3.value="";
		document.af2.value3.value="";
		document.af2.join3.value="";
		document.af2.field4.value="";
		document.af2.op4.value="";
		document.af2.value4.value="";
		document.af2.join4.value="";
		document.af2.field5.value="";
		document.af2.op5.value="";
		document.af2.value5.value="";
		document.af2.join5.value="";
		document.af2.field6.value="";
		document.af2.op6.value="";
		document.af2.value6.value="";
		document.af2.join6.value="";
		document.af2.field7.value="";
		document.af2.op7.value="";
		document.af2.value7.value="";
		document.af2.join7.value="";
		document.af2.field8.value="";
		document.af2.op8.value="";
		document.af2.value8.value="";
		document.af2.join8.value="";
		document.af2.field9.value="";
		document.af2.op9.value="";
		document.af2.value9.value="";
}

function checkMaxSelected (id, maxSelected) 
{
	select = document.getElementById(id);
	selCount = 0;
	bWarning = false;
 	selectedOptions = 0;

  for (var i = 0; i < select.options.length; i++) 
  {
    if (select.options[i].selected)// && !select.storeSelections[i]) 
	{
      if (selectedOptions < maxSelected) 
	 {
       selectedOptions++;
      }
      else 
	  {
        selCount ++;
		bWarning = true;
        select.options[i].selected = false;
      }
    }
    
  }
  if(bWarning)
  {
	alert("You have selected more than 4 states. \n The last " + selCount + " items will be deselected.");  
	selCount = 0;
	bWarning = false;
  }
  
} 



