
var IE = (document.all) ? 1: 0;
var DOM = (document.getElementById) ? 1 : 0;
var MAC = ((navigator.appVersion.indexOf("PPC")>0 || navigator.appVersion.indexOf("Mac") >0)) ? 1 : 0;

function getElement(elementName)
{
	var element = null;
	if ((DOM) || (MAC && IE))
	{
		//alert("DOM");
		element = document.getElementById(elementName);
	}
	else if (IE)
	{
		//alert("IE");
		element = eval("document.all."+elementName);
	}
	return element;
} 

var yposstart = "200px";

document.onkeydown =
  function (evt) {
    var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
    if (keyCode == 13) {
      document.searchForm.submit();
      return false;
    }
    else
      return true;
  };
  
  function ShowMoreSearchBoxes()
  {
  //alert("ShowMoreSearchBoxes");
  var showItem = getElement('showboxes');
  var hideItem = getElement('hideboxes');
  showItem.style.visibility='hidden';
  showItem.style.height='1px';
  showItem.style.pixelTop=yposstart;
  hideItem.style.pixelTop=yposstart;
  hideItem.style.height='240px';
  hideItem.style.visibility='visible';
  //getElement('limits').style.pixelTop=yposstart+hideItem.offsetHeight;
  //alert ("limit top: "+getElement('limits').style.pixelTop);

  }
  
  function HideAddedSearchBoxes()
  {
  //alert("HideAddedSearchBoxes"); //good 
  var showItem = getElement('showboxes');
  var hideItem = getElement('hideboxes');
  hideItem.style.visibility='hidden';
  hideItem.style.height='1px';
  hideItem.style.pixelTop=yposstart;
  showItem.style.pixelTop=yposstart;
  showItem.style.height='40px';
  showItem.style.visibility='visible';
  //getElement('limits').style.pixelTop=yposstart+showItem.offsetHeight;
  //alert ("limit top: "+getElement('limits').style.pixelTop);
  }

  function HideAddedSearchBoxesConditionally()
  {
  var term2 = getElement('AATerm2');
  var term3 = getElement('AATerm3');
  var term4 = getElement('AATerm4'
  );
  if (term2.value == "" && term3.value == "" && term4.value == "") 
  {
  		HideAddedSearchBoxes();
  }
  else
  {
  		ShowMoreSearchBoxes();
  }
  }

//type onchange function
function pickSources(sourcesList)
{
	//alert("pickSources");
	var typeSelectBox = document.getElementById("AALimit3");
	var lookFor = "";
	var imageQual = false;
	var type; //= typeSelectBox.options[typeSelectBox.selectedIndex].value;
	
	for (var i = 0; i < typeSelectBox.length; i++) 
	{
		if (typeSelectBox[i].selected) 
		{
			type = typeSelectBox.options[i].value;
			//alert("type: "+type);
			// Item has been selected
	
			if (type == "Book")
				lookFor=lookFor+"b";
			else
			if (type == "Newspapers")
				lookFor=lookFor+"n";
			else	
			if (type == "American Counties")
				lookFor=lookFor+"a";	
		}
	}
	if (lookFor == "")
	{
		lookFor = "bna";
	}	
				
	//alert("lookFor: "+lookFor);	
		
	var i,j;
	var sourcesSelect = document.getElementById("AALimit1");
	
	sourcesSelect.options.length=0;
	//alert("sourcesList: "+sourcesList);
	var source = sourcesList.split("|");
	var gathering = false;
	j = 0;
	for (i=0;i<source.length;i++)
	{
		if (lookFor.indexOf(source[i].substring(1,2))<0)
		{
			continue;
		}
		//alert(source[i].substring(3));
		sourcesSelect.options[j] = new Option(source[i].substring(3),source[i], false, false);
		if (source[i].substring(2,3) != 0)
		{
			imageQual = true;
		}
		j++;
	}

	sourcesSelect.size=5;
	AMCQual = (lookFor.indexOf("a") >= 0);
	doAMCQualifiers(AMCQual);
	//alert("doImageQualifiers a "+imageQual);
	doImageQualifiers(imageQual);
}


function doAMCQualifiers(doAMCQual)
{
	//alert("doAMCQualifiers  "+doAMCQual);
	var searchInTag = document.getElementById("AASearchInTag1");
	if (searchInTag == null)
	{
		return;
	}
	var optionsLength = searchInTag.options.length;
	var hasAMC = false, hasImage = false;
	var i;
	if (optionsLength > 4)
	{
		for (i=0;i<optionsLength;i++)
		{
			if (searchInTag.options[i].value == "state")
				hasAMC = true;
			else
			if (searchInTag.options[i].value == "image_type")
				hasImage = true;
		}			
	}
	for (i=1;i<=4;i++)
	{
		searchInTag = document.getElementById("AASearchInTag"+i);
		if (doAMCQual && hasAMC)
		{
			continue;	
		}
		if (!doAMCQual && hasAMC)
		{
	 		if (hasImage)
	 		{
	 			searchInTag.options[4].value = searchInTag.options[6].value;
	 			searchInTag.options[4].text = searchInTag.options[6].text; 
	 			//searchInTag.options[5].value = searchInTag.options[7].value;//description now out
	 			//searchInTag.options[5].text = searchInTag.options[7].text;
	 			searchInTag.options.length = 5;
	 			continue;
	 		}
	 		else
	 		{
	 			searchInTag.options.length = 4;
	 			continue;
	 		}				 
		}
		if (doAMCQual && !hasAMC)
		{
			if (hasImage)
			{
				searchInTag.options.length = 4;
				addAMCQual(i);
				continue;
			}
			if (!hasImage)
			{
				addAMCQual(i);
				continue;
			}
		}
		if (!doAMCQual && !hasAMC)
		{
			continue;
		}
		//Should not get here!
		alert("doAMCQualifiers sink");
	}
	//alert("doAMCQualifiers length: "+document.getElementById("AASearchInTag1").options.length);
	return;
}

function doImageQualifiers(doImageQual)
{
	//alert("doImageQualifiers  "+doImageQual);
	var searchInTag = document.getElementById("AASearchInTag1");
	if (searchInTag == null)
	{
		return;
	}
	var optionsLength = searchInTag.options.length;
	var hasAMC = false, hasImage = false;
	var i;
	if (optionsLength > 4)
	{
		for (i=0;i<optionsLength;i++)
		{
			if (searchInTag.options[i].value == "state")
				hasAMC = true;
			else
			if (searchInTag.options[i].value == "image_type")
				hasImage = true;
		}			
	}
	for (i=1;i<=4;i++)
	{
		searchInTag = document.getElementById("AASearchInTag"+i);
		if (doImageQual && hasImage)
		{
			continue;	
		}
		if (!doImageQual && hasImage)
		{
	 		searchInTag.options.length = searchInTag.options.length - 1;
	 		continue;
		}
		if (doImageQual && !hasImage)
		{
			//alert("(doImageQual && !hasImage)");
			addImageQual(i);
			continue;
		}
		if (!doImageQual && !hasImage)
		{
			continue;
		}
		alert("doImageQualifiers sink");
	}
	//alert("doImageQualifiers length: "+document.getElementById("AASearchInTag1").options.length);
	return;

}
//Fixes size of select box on screen reload with old choices.
//NEED TO REPAINT
function adjustSourcesSelectSize()
{
	var sourcesSelect = document.getElementById("AALimit1");
	var j = sourcesSelect.size;	
	if (j > 5)
		j = 5; 
	sourcesSelect.size=j;
}

//Hides or shows state/county dropdown depending on access rights or branding selection
function hideOrShowAMC(hasbothnewspaperandbooks)
{
	//alert("hideOrShowAMC");
  	if (hasbothnewspaperandbooks == 'a')
  	{
		showAMCLimits();
		return;
	}
	else
	if (hasbothnewspaperandbooks.indexOf('a') < 0)
  	{
  		
  		hideAMCLimits();
  		return;
  	}
 	checkLimits1And3();
}

function checkLimits1And3()
{
	var AALimit1 = document.getElementById("AALimit1");//sources (titles)
	var AALimit3 = document.getElementById("AALimit3");//doc types
	var docTypeAll = 0, docTypeAMC = 0;
	if (AALimit3 != null) //It may be that the user has only one type
	{
	  	for (i=0;i<AALimit3.length;i++)
	  	{
			 if (AALimit3.options[i].selected)
			 {
	  			if (AALimit3.options[i].value == "")
	  			{
	  				docTypeAll = 1;
	  			}
	  			else
	  			if (AALimit3.options[i].value == "American Counties")
	   			{
	  				docTypeAMC = 1;
	  			}
	  			else
	  			{
	  				//doc_type = Books or Newspapers
	  				hideAMCLimits();
	  				doAMCQualifiers(false);
	  				return;
	  			}
			 }
	 	}	
	 	if (docTypeAMC == 1)
	 	{
	 		showAMCLimits();
	 		doAMCQualifiers(true);
	 		return;
	 	}
 	}
 	//Only one type or All is chosen; see if AMC is only source chosen
 	var numSelected = 0;
  	for (i=0;i<AALimit1.length;i++)
  	{
		 if (AALimit1.options[i].selected)
		 {
		 	numSelected = numSelected+1;
  			if (AALimit1.options[i].value.indexOf("ca0American County Histories")<0)//fix for ...MD, ...NY, etc.
  			{
  				hideAMCLimits();
  				doAMCQualifiers(false);
  				return;
  			}
  			else
  			{
  				docTypeAMC = 1;
  			}
  		}
 	}	
 	if (docTypeAMC == 1)
 	{
 		//American Counties chosen in AALimit1
 		showAMCLimits();
 		doAMCQualifiers(true);
 	}
 	else
 	if (numSelected == 0)
 	{
 		//Nothing selected in AALimit1
 		hideAMCLimits();
 		doAMCQualifiers(true);
 	}
 	else
 	{
 		//Shouldn't get here
 		hideAMCLimits();
 		doAMCQualifiers(false);
 	}
 	return;	
}

function showAMCLimits()
{
  	var showItem = getElement('AMCLimits');
  	//alert("show");
  	if (showItem != null)
  	{
  		showItem.style.visibility = "visible";
  		showItem.style.height='110px';
   	}
   	//getElement('sortx').style.display="none";
   	//showSpots();

}

function hideAMCLimits()
{
  	var showItem = getElement('AMCLimits');
  	//alert("hide");
  	if (showItem != null)
  	{
  		showItem.style.visibility = "hidden";
  		showItem.style.height='0px';
  	}
  	//getElement('sortx').style.display="block";
  	//showSpots();
  	
}

//Hides or shows image type limit depending on access rights or branding selection
function hideOrShowImageTypes(imagetypes)
{
	//alert("hideOrShowImageTypes: "+imagetypes);
  	if (imagetypes == '0')
  	{
  		//alert("hide image types: "+imagetypes);
		hideImageLimits();
		return;
	}
	else
 	checkLimits1And3ForImageStuff();
}


function checkLimits1And3ForImageStuff()
{
	var AALimit1 = document.getElementById("AALimit1");//sources (titles)
	var AALimit3 = document.getElementById("AALimit3");//doc types
	var docHasImages = 0;
  	//See if image sources are only sources available
  	for (i=0;i<AALimit1.length;i++)
  	{
  		//alert("value length"+AALimit1.options[i].value+"xxx");
 		if (AALimit1.options[i].value != "" && AALimit1.options[i].value.substring(2,3) != "0")
  		{
  			docHasImages = docHasImages+1;
  		}	
  	}

  	if (docHasImages == AALimit1.length)
  	{
	  	showImageLimits();
	  	//alert("doImageQualifiers b true");
 		doImageQualifiers(true);
  		return;
  	}	
  	if (docHasImages == 0)
  	{
  		hideImageLimits();
  		//alert("doImageQualifiers c false");
  		doImageQualifiers(false);
  		return;
  	}

  	//Now find out if one or more docs with images are the only ones selected
  	ndocHasImages = 0;
  	var numSelected = 0;
  	for (i=0;i<AALimit1.length;i++)
  	{
 		 if (AALimit1.options[i].selected)
		 {
		 	numSelected = numSelected+1;
  			if (AALimit1.options[i].value.substring(2,3) == '0')
  			{
  				hideImageLimits();
  				//alert("doImageQualifiers d false");
  				doImageQualifiers(false);
  				return;
  			}
  			else
  			{
  				ndocHasImages = ndocHasImages + 1;
  			}
  		}	
 	}	
 	if (ndocHasImages > 0)
 	{
 		//all pubs selected in AALimit1 have images
		showImageLimits();
		//alert("doImageQualifiers e true");
		doImageQualifiers(true);
	}
 	else
 	if (numSelected == 0 && docHasImages > 0)
 	{
 		//Nothing selected in AALimit1 but pub type in AALimit3 has images available
 		hideImageLimits();
 		//alert("doImageQualifiers f true");
 		doImageQualifiers(true);
 	}
	else
	{
		//No pubs selected
		hideImageLimits();
		//alert("doImageQualifiers g false");
		doImageQualifiers(false);	
	}		
 	return;	
}

function showImageLimits()
{
  	var showItem = getElement('imageLimits');
  	//alert("show");
  	if (showItem != null)
  	{
  		showItem.style.visibility = "visible";
  		showItem.style.height='55px';
   	}
}

function hideImageLimits()
{
  	var showItem = getElement('imageLimits');
  	//alert("hide");
  	if (showItem != null)
  	{
  		showItem.style.visibility = "hidden";
  		showItem.style.height='0px';
  	}
}


function addAMCQual(i)
{
	var searchInTag = document.getElementById("AASearchInTag"+i);
	var searchInTagLength = searchInTag.options.length;
	if (searchInTag != null)
	{
		searchInTag.options[searchInTagLength] = new Option("State (American County Histories)", "state", false, false);
		searchInTag.options[searchInTagLength+1] = new Option("County (American County Histories)", "county", false, false);
	}
	searchInTag.options.length = searchInTagLength+2;
}

function addImageQual(i)
{
	var searchInTag = document.getElementById("AASearchInTag"+i);
	var searchInTagLength = searchInTag.options.length;
	if (searchInTag != null)
	{
		searchInTag.options[searchInTagLength] = new Option("Image Type", "image_type", false, false);
		//searchInTag.options[searchInTagLength+1] = new Option("Image Description", "image_description", false, false);
	}
	searchInTag.options.length = searchInTagLength+1;
}

function showSpots()
{
	alert("hideAMCLimits showboxes.pixelTop "+showboxes.pixelTop
	+"\nhideAMCLimits hideboxes.pixelTop "+hideboxes.pixelTop
	+"\nhideAMCLimits searchbuttons.pixelTop "+searchbuttons.pixelTop
	+"\nhideAMCLimits limits.pixelTop "+limits.pixelTop
	+"\nhideAMCLimits AMCLimits.pixelTop "+AMCLimits.pixelTop
	+"\nhideAMCLimits sortx.pixelTop "+sortx.pixelTop);

	alert("hideAMCLimits showboxes.offsetHeight "+showboxes.offsetHeight
	+"\nhideAMCLimits hideboxes.offsetHeight "+hideboxes.offsetHeight
	+"\nhideAMCLimits searchbuttons.offsetHeight "+searchbuttons.offsetHeight
	+"\nhideAMCLimits limits.offsetHeight "+limits.offsetHeight
	+"\nhideAMCLimits AMCLimits.offsetHeight "+AMCLimits.offsetHeight
	+"\nhideAMCLimits sortx.offsetHeight "+sortx.offsetHeight);

	alert("hideAMCLimits showboxes.offsetTop "+showboxes.offsetTop
	+"\nhideAMCLimits hideboxes.offsetTop "+hideboxes.offsetTop
	+"\nhideAMCLimits searchbuttons.offsetTop "+searchbuttons.offsetTop
	+"\nhideAMCLimits limits.offsetTop "+limits.offsetTop
	+"\nhideAMCLimits AMCLimits.offsetTop "+AMCLimits.offsetTop
	+"\nhideAMCLimits sortx.offsetTop "+sortx.offsetTop);

}

