function BuildFormSubmission()

{
   var UrlToOpen = '';

   if (document.forms['boolean'].SA1.value)
   {
      UrlToOpen += ApplySearchType(document.forms['boolean'].SC1.value, document.forms['boolean'].SA1.value, document.forms['boolean'].Type1.value);
   }

   if (document.forms['boolean'].SA2.value)
   {

      if (UrlToOpen.length)
      {
         UrlToOpen += document.forms['boolean'].AON2.value + ' ';	
      }

      UrlToOpen += ApplySearchType(document.forms['boolean'].SC2.value, document.forms['boolean'].SA2.value, document.forms['boolean'].Type2.value);
   }


   if (document.forms['boolean'].SA3.value)
   {
      if (UrlToOpen.length)
      {
         UrlToOpen += document.forms['boolean'].AON3.value + ' ';	
      }
      UrlToOpen += ApplySearchType(document.forms['boolean'].SC3.value, document.forms['boolean'].SA3.value, document.forms['boolean'].Type3.value);
   }	

   document.fake.pm_BT.value = UrlToOpen;

   document.fake.ds_svGeneric_MH.value = document.fake.pm_MH.value;
	
	// Dedupe Flag ?

	if(document.fake.pm_DDF.checked)
	{
		document.fake.ds_svGeneric_DDF.value="Y";
	}
	else
	{
		document.fake.ds_svGeneric_DDF.value="N";
	}
	
	// USer Sort
//	for(i = 0; i <  document.forms['boolean'].pm_USI.length; i++)
//	{
//		if( document.forms['boolean'].pm_USI[i].selected)
//		{
//			document.fake.pm_USI.value=document.forms['boolean'].pm_USI[i].value;
//		}
//	}
	
   document.fake.submit();

}






function jsIndent(HierVal)
{

   var iCount = 0;
   var chunks = HierVal.split(".");

   for (iCount=2 ; iCount < chunks.length ; iCount++)
   {
      document.write("...");
   }

}





function ApplySearchType(SearchCode, SearchArgument, SearchType)
{
	var SearchString = "";
	var LogicString;
	var Array = SearchArgument.split(/\s+/);

	if (SearchType == "PHRASE")
	{
		for (var i = 0; i < Array.length; i++)
		{
			if (SearchString.length)
				SearchString += ' ';

			SearchString += Array[i];
		}	

		if (SearchString.length)
			SearchString = '(' + SearchCode + ' "' + SearchString + '") ';
	}
	else
	{
		if (SearchType == "ANY")
		{
			LogicString = "OR";
		}
		else
		{
			LogicString = "AND";
		}

		for (var i = 0; i < Array.length; i++)
		{
			if (SearchString.length)
				SearchString += ' ' + LogicString + ' ';

			SearchString += SearchCode + ' ' + Array[i];
		}		
		if (SearchString.length)
			SearchString = '(' + SearchString + ') ';
	}

	return(SearchString);
}





function validate_form_advanced()
{

   if ( document.forms['boolean'].SA1.value == "" && document.forms['boolean'].SA2.value == "" && document.forms['boolean'].SA3.value == "" )
   {
      alert ( "Please enter a term to search on." );
      return false;
   }
	
   BuildFormSubmission();

   return true;
   
}

// function to validate TF2 Querybox input
function validate_querybox()
{
	//alert("validate_querybox");
   if ( document.forms['boolean_header'].SA.value == "")
   {
      alert ( "Please enter a term to search on." );
      return false;
   }
   else
   {
   		BuildQueryBoxSubmission();	
		return true;
	}
}

// function to build TF2 Querybox submission URL
function BuildQueryBoxSubmission()
{
   var UrlToOpen = '';
   var Type = '';

   //alert("BuildQueryBoxSubmission");
   	for(i = 0; i <  document.forms['boolean_header'].Type.length; i++)
   	{
	   if(document.forms['boolean_header'].Type[i].checked)
	   	{
		   Type = document.forms['boolean_header'].Type[i].value;
		}
	}
   
   if (document.forms['boolean_header'].SA.value)
   {
	  //alert ('GKEY, ' + document.forms['boolean_header'].SA.value + ', ' + Type);
      UrlToOpen += ApplySearchType('GKEY', document.forms['boolean_header'].SA.value, Type);
   }

   document.fake_header.pm_BT.value = UrlToOpen;

   document.fake_header.ds_svGeneric_MH.value = document.fake_header.pm_MH.value;
	
   //alert("pm_BT = " + document.fake_header.pm_BT.value);
	// Dedupe Flag ?

		document.fake_header.ds_svGeneric_DDF.value="Y";
	
	// USer Sort
//	for(i = 0; i <  document.forms['boolean_header'].pm_USI.length; i++)
//	{
//		if( document.forms['boolean_header'].pm_USI[i].selected)
//		{
//			document.fake_header.pm_USI.value=document.forms['boolean'].pm_USI[i].value;
//		}
//	}
	
   document.fake_header.submit();

}

// function to validate TF2 Basic Search input
function validate_basicsearch()
{
	//alert("validate_basicsearch");
   if ( document.forms['boolean'].SA1.value == "")
   {
      alert ( "Please enter a term to search on." );
      return false;
   }
   else
   {

		BuildSearchFormSubmission();	
		return true;
   }
}

// function to Build TF2 Basic Search query
function BuildSearchFormSubmission()
{
   var UrlToOpen = '';
   var Type = '';
   var USI = '';

   //alert("BuildSearchFormSubmission");
   // determine Type
   	for(i = 0; i <  document.forms['boolean'].Type.length; i++)
   	{
	   if(document.forms['boolean'].Type[i].checked)
	   	{
		   Type = document.forms['boolean'].Type[i].value;
		}
	}
   
	// get the Search String
   if (document.forms['boolean'].SA1.value)
   {
	  //alert ('GKEY, ' + document.forms['boolean'].SA.value + ', ' + Type);
      UrlToOpen += ApplySearchType('GKEY', document.forms['boolean'].SA1.value, Type);
   }

   //alert("UrlToOpen = " + UrlToOpen);
   // determine the collections
   
   if (document.forms['boolean'].search_all_formats.checked)
   {
	   document.fake.pm_CL[0].value = document.forms['boolean'].search_all_formats.value;
   }
   else
   {
	   if (document.forms['boolean'].photographs.checked)
	   {
		   document.fake.pm_CL[0].value = document.forms['boolean'].photographs.value;
	   }
	   if (document.forms['boolean'].paintings.checked)
	   {
		   document.fake.pm_CL[1].value = document.forms['boolean'].paintings.value;
	   }
	   if (document.forms['boolean'].maps.checked)
	   {
		   document.fake.pm_CL[2].value = document.forms['boolean'].maps.value;
	   }
	   if (document.forms['boolean'].cartoons.checked)
	   {
		   document.fake.pm_CL[3].value = document.forms['boolean'].cartoons.value;
	   }
	   if (document.forms['boolean'].ephemera.checked)
	   {
		   document.fake.pm_CL[4].value = document.forms['boolean'].ephemera.value;
	   }
	   if (document.forms['boolean'].manuscripts.checked)
	   {
		   document.fake.pm_CL[5].value = document.forms['boolean'].manuscripts.value;
	   }

   }
   
   /*alert("second :cl[0] = " + document.fake.pm_CL[0].value);
   alert("second :cl[1] = " + document.fake.pm_CL[1].value);
   alert("second :cl[2] = " + document.fake.pm_CL[2].value);
   alert("second :cl[3] = " + document.fake.pm_CL[3].value);
   alert("second :cl[4] = " + document.fake.pm_CL[4].value);
   alert("second :cl[5] = " + document.fake.pm_CL[5].value);*/
   
   // determine USI (User Sort Option)
   	for(i = 0; i <  document.forms['boolean'].pm_USI.length; i++)
   	{
	   if(document.forms['boolean'].pm_USI[i].checked)
	   	{
		   USI = document.forms['boolean'].pm_USI[i].value;
		}
	}
   	document.fake.pm_USI.value = USI;
		   
   document.fake.pm_BT.value = UrlToOpen;

   document.fake.ds_svGeneric_MH.value = document.fake.pm_MH.value;
	
	// Dedupe Flag ?

		document.fake.ds_svGeneric_DDF.value="Y";
	
	// USer Sort
//	for(i = 0; i <  document.forms['boolean'].pm_USI.length; i++)
//	{
//		if( document.forms['boolean'].pm_USI[i].selected)
//		{
//			document.fake.pm_USI.value=document.forms['boolean'].pm_USI[i].value;
//		}
//	}
	
   document.fake.submit();

}

// TF2 Advanced Search Validate
function validate_advancedsearch()
{

   if ( document.forms['boolean'].SA1.value == "" && document.forms['boolean'].SA2.value == "" && document.forms['boolean'].SA3.value == "" )
   {
      alert ( "Please enter a term to search on." );
      return false;
   }
   else
   {	
   		BuildAdvancedSearchFormSubmission();
  		return true;
	}
   
}

// function to Build TF2 Advanced Search query
function BuildAdvancedSearchFormSubmission()
{
   var UrlToOpen = '';
   var Type = '';
   var USI = '';

   //alert("BuildSearchFormSubmission");
   // determine Type
   /*	for(i = 0; i <  document.forms['boolean'].Type.length; i++)
   	{
	   if(document.forms['boolean'].Type[i].checked)
	   	{
		   Type = document.forms['boolean'].Type[i].value;
		}
	}*/
   
	// get the Search String
   if (document.forms['boolean'].SA1.value)
   {
      UrlToOpen += ApplySearchType(document.forms['boolean'].SC1.value, document.forms['boolean'].SA1.value, document.forms['boolean'].Type1.value);
   }

   if (document.forms['boolean'].SA2.value)
   {

      if (UrlToOpen.length)
      {
         UrlToOpen += document.forms['boolean'].AON2.value + ' ';	
      }

      UrlToOpen += ApplySearchType(document.forms['boolean'].SC2.value, document.forms['boolean'].SA2.value, document.forms['boolean'].Type2.value);
   }


   if (document.forms['boolean'].SA3.value)
   {
      if (UrlToOpen.length)
      {
         UrlToOpen += document.forms['boolean'].AON3.value + ' ';	
      }
      UrlToOpen += ApplySearchType(document.forms['boolean'].SC3.value, document.forms['boolean'].SA3.value, document.forms['boolean'].Type3.value);
   }	

   document.fake.pm_BT.value = UrlToOpen;

   //alert("UrlToOpen = " + UrlToOpen);
   // determine the collections
   
   if (document.forms['boolean'].search_all_formats.checked)
   {
	   document.fake.pm_CL[0].value = document.forms['boolean'].search_all_formats.value;
   }
   else
   {
	   if (document.forms['boolean'].photographs.checked)
	   {
		   document.fake.pm_CL[0].value = document.forms['boolean'].photographs.value;
	   }
	   if (document.forms['boolean'].paintings.checked)
	   {
		   document.fake.pm_CL[1].value = document.forms['boolean'].paintings.value;
	   }
	   if (document.forms['boolean'].maps.checked)
	   {
		   document.fake.pm_CL[2].value = document.forms['boolean'].maps.value;
	   }
	   if (document.forms['boolean'].cartoons.checked)
	   {
		   document.fake.pm_CL[3].value = document.forms['boolean'].cartoons.value;
	   }
	   if (document.forms['boolean'].ephemera.checked)
	   {
		   document.fake.pm_CL[4].value = document.forms['boolean'].ephemera.value;
	   }
	   if (document.forms['boolean'].manuscripts.checked)
	   {
		   document.fake.pm_CL[5].value = document.forms['boolean'].manuscripts.value;
	   }

   }
   
   /*alert("second :cl[0] = " + document.fake.pm_CL[0].value);
   alert("second :cl[1] = " + document.fake.pm_CL[1].value);
   alert("second :cl[2] = " + document.fake.pm_CL[2].value);
   alert("second :cl[3] = " + document.fake.pm_CL[3].value);
   alert("second :cl[4] = " + document.fake.pm_CL[4].value);
   alert("second :cl[5] = " + document.fake.pm_CL[5].value);*/
   
   // determine USI (User Sort Option)
   	for(i = 0; i <  document.forms['boolean'].pm_USI.length; i++)
   	{
	   if(document.forms['boolean'].pm_USI[i].checked)
	   	{
		   USI = document.forms['boolean'].pm_USI[i].value;
		}
	}
   	document.fake.pm_USI.value = USI;
		   
   //document.fake.pm_BT.value = UrlToOpen;

   document.fake.ds_svGeneric_MH.value = document.fake.pm_MH.value;
	
	// Dedupe Flag ?

		document.fake.ds_svGeneric_DDF.value="Y";
	
	// USer Sort
//	for(i = 0; i <  document.forms['boolean'].pm_USI.length; i++)
//	{
//		if( document.forms['boolean'].pm_USI[i].selected)
//		{
//			document.fake.pm_USI.value=document.forms['boolean'].pm_USI[i].value;
//		}
//	}
	
   document.fake.submit();

}

// function to validate TF2 Search Again input
function validate_basicsearchagain()
{
	//alert("validate_querybox");
   if ( document.forms['boolean_footer'].SA.value == "")
   {
      alert ( "Please enter a term to search on." );
      return false;
   }
   else
   {
   		BuildSearchAgainBoxSubmission();	
		return true;
	}
}

// function to build TF2 SearchAgain submission URL
function BuildSearchAgainBoxSubmission()
{
   var UrlToOpen = '';
   var Type = '';

   //alert("BuildQueryBoxSubmission");
   	for(i = 0; i <  document.forms['boolean_footer'].Type.length; i++)
   	{
	   if(document.forms['boolean_footer'].Type[i].checked)
	   	{
		   Type = document.forms['boolean_footer'].Type[i].value;
		}
	}
   
   if (document.forms['boolean_footer'].SA.value)
   {
	  //alert ('GKEY, ' + document.forms['boolean_header'].SA.value + ', ' + Type);
      UrlToOpen += ApplySearchType('GKEY', document.forms['boolean_footer'].SA.value, Type);
   }

   document.fake_footer.pm_BT.value = UrlToOpen;

   document.fake_footer.ds_svGeneric_MH.value = document.fake_footer.pm_MH.value;
	
   //alert("pm_BT = " + document.fake_header.pm_BT.value);
	// Dedupe Flag ?

		document.fake_footer.ds_svGeneric_DDF.value="Y";
	
	// USer Sort	
//	for(i = 0; i <  document.forms['boolean_footer'].pm_USI.length; i++)
//	{
//		if( document.forms['boolean_footer'].pm_USI[i].selected)
//		{
//			document.fake_footer.pm_USI.value=document.forms['boolean'].pm_USI[i].value;
//		}
//	}
	
   document.fake_footer.submit();

}

// Function to conduct resorting of search results
function ReSortResults(Srchstring)
{
	
	/*//alert('StartResortofResults');
	// Pass the Order Type
	document.forms['fake_footer'].pm_USI.value = document.forms['resorter'].sortby.value
	// Pass the chunksize
	document.forms['fake_footer'].pm_MH.value = document.forms['resorter'].resultsno.value
	document.forms['fake_footer'].ds_svGeneric_MH.value = document.forms['resorter'].resultsno.value
	// Pass the searchstring
	document.forms['fake_footer'].pm_BT.value = Srchstring;
	//alert('StopResortofResults');
	document.fake_footer.submit();
	return false;*/
	
	var reSortURL;
	//alert(Srchstring);
	reSortURL = unescape(document.forms['resorter'].OUTPUTXSL.value);
	while (reSortURL.indexOf("~~") > -1)
	{
		reSortURL = reSortURL.replace("~~",document.forms['resorter'].resultsno.value);
	}
	//alert('reSortURL= ' + reSortURL);
	document.forms['resorter'].logicRouterURL.value = document.forms['resorter'].logicRouterURL.value + reSortURL;
	//alert("document.forms['reSorterSubmit'].action = " + document.forms['reSorterSubmit'].action);
	//alert('document.forms['resorter'].logicRouterURL.value ' + document.forms['resorter'].logicRouterURL.value);
	location.href = document.forms['resorter'].logicRouterURL.value;
	alert("Refreshing results to show " + document.forms['resorter'].resultsno.value + " results per page.");
	return false;
	
	
}

// function to form the search for the Browse term
function getBrowseSearch(SrchArg)
{
	document.forms['fake_footer'].pm_BT.value = ApplySearchType('GKEY', SrchArg, 'PHRASE');
	//alert(document.forms['fake_footer'].pm_BT.value);
	document.fake_footer.submit();
	return false;
}

