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();
  }
  }
  
function ResetAllArchives(usersourceslist)
{
getElement('AATerm1').value = "";
getElement('AATerm2').value = "";
getElement('AATerm3').value = "";
getElement('AATerm4').value = "";

getElement('AASearchInTag1').value = "";
getElement('AASearchInTag2').value = "";
getElement('AASearchInTag3').value = "";
getElement('AASearchInTag4').value = "";

document.searchForm.AAOpConnectBox2[0].checked=false;
document.searchForm.AAOpConnectBox2[1].checked=true; //OR
document.searchForm.AAOpConnectBox2[2].checked=false;

document.searchForm.AAOpConnectBox3[0].checked=false;
document.searchForm.AAOpConnectBox3[1].checked=true; //OR
document.searchForm.AAOpConnectBox3[2].checked=false;

document.searchForm.AAOpConnectBox4[0].checked=false;
document.searchForm.AAOpConnectBox4[1].checked=true; //OR
document.searchForm.AAOpConnectBox4[2].checked=false;

if (getElement('AALimit3') != null)
{
	getElement('AALimit3').value="";
	getElement('AALimit3').options[0].selected=false;
}	

	var i,j;
	var sourcesSelect = document.getElementById("AALimit1");
	
	sourcesSelect.options.length=0;
	var source = usersourceslist.split("|");
	var gathering = false;
	j = 0;
	for (i=0;i<source.length;i++)
	{
		//alert(source[i].substring(2));
		sourcesSelect.options[j] = new Option(source[i].substring(2),source[i], false, false);
		j++;
	}
	sourcesSelect.size=5;

getElement('AALimit2').value = "";
getElement('AALimitHigh2').value = "";

//Resetting "selected" was not enough, so...
	var sortItems = ["(select)","Date","Publication","Collection"];
	var sortValues = ["","Date","Publication","Collection"];
	var sortKeySelect = document.getElementById("AASortKey");
	sortKeySelect.options.length=0;
	for (i=0;i<sortItems.length;i++)
	{
		sortKeySelect.options[i] = new Option(sortItems[i],sortValues[i], false, false);
	}	
}

function ResetAllAMCNTY()
{
var AATerm1 = document.getElementById("AATerm1");
AATerm1.value="";

//Simply resetting "selected" was n.g., so....
var j=0;
var holdStates = new Array(10);
var limit1Select = document.getElementById("AALimit1");
//Save the current contents which are the user's selections or access rights
for (i=0;i<limit1Select.length;i++)
{
	holdStates[i] = limit1Select[i].value;
	j++;
}

limit1Select.options.length=0;

for (i=0;i<j;i++)
{	 	
	limit1Select.options[i] = new Option(holdStates[i], holdStates[i], false, false);
}

document.searchForm.AALimit2.options.length=1;
document.searchForm.AALimit2.options[0] = new Option("Select state to see a list of its counties", "", false, false);
document.searchForm.AALimit2.size=1;

document.searchForm.AATerm1.value = "";

//Resetting "selected" was not enough, so...
	var sortItems = ["(select)","State","County"];
	var sortValues = ["","state","counties"];
	var sortKeySelect = document.getElementById("AASortKey");
	sortKeySelect.options.length=0;
	for (i=0;i<sortItems.length;i++)
	{
		sortKeySelect.options[i] = new Option(sortItems[i], sortValues[i], false, false);
	}
}

function pickSources(sourcesList)
{
//FIX HERE FOR MULTIPLE Selections - 
	//alert("pickSources");
	var typeSelectBox = document.getElementById("AALimit3");
	var lookFor = "";
	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(2));
		sourcesSelect.options[j] = new Option(source[i].substring(2),source[i], false, false);
		j++;
	}
	//if (j > 6)
	//	j = 6; 
	//sourcesSelect.size=j-1;
	sourcesSelect.size=5;
	if (lookFor.indexOf("a") >= 0)
	{
		turnOnCounty();
	}
	else
	{
		turnOffCounty();
	}		
}


function turnOnCounty()
{
	var i; 
	var searchInTag = document.getElementById("AASearchInTag1");
	i = searchInTag.options.length;
	if 	(i < 6)
	{
		searchInTag.options[4] = new Option("State (American County Histories)", "state", false, false);
		searchInTag.options[5] = new Option("County (American County Histories)", "county", false, false);
	}	

	searchInTag = document.getElementById("AASearchInTag2");	
	searchInTag.options[4] = new Option("State (American County Histories)", "state", false, false);
	searchInTag.options[5] = new Option("County (American County Histories)", "county", false, false);

	searchInTag = document.getElementById("AASearchInTag3");	
	searchInTag.options[4] = new Option("State (American County Histories)", "state", false, false);
	searchInTag.options[5] = new Option("County (American County Histories)", "county", false, false);

	searchInTag = document.getElementById("AASearchInTag4");	
	searchInTag.options[4] = new Option("State (American County Histories)", "state", false, false);
	searchInTag.options[5] = new Option("County (American County Histories)", "county", false, false);
}

function turnOffCounty()
{
	var searchInTag = document.getElementById("AASearchInTag1");
	var i = searchInTag.options.length;
	if 	(i == 6)
	{
		searchInTag.options.length = 4;
	}	
	
	searchInTag = document.getElementById("AASearchInTag2");
	searchInTag.options.length = 4;

	searchInTag = document.getElementById("AASearchInTag3");
	searchInTag.options.length = 4;

	searchInTag = document.getElementById("AASearchInTag4");
	searchInTag.options.length = 4;
}

//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')
  	{
		showAMCDropdowns();
		return;
	}
	else
	if (hasbothnewspaperandbooks.indexOf('a') < 0)
  	{
  		
  		hideAMCDropdowns();
  		return;
  	}
 	checkLimits1And3();
}


function checkLimits1And3()
{
	var AALimit1 = document.getElementById("AALimit1");//sources (titles)
	var AALimit3 = document.getElementById("AALimit3");//doc types
	var docTypeAll = 0, docTypeAMC = 0;
  	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
  				hideAMCDropdowns();
  				return;
  			}
		 }
 	}	
 	if (docTypeAMC)
 	{
 		showAMCDropdowns();
 		return;
 	}
 	//All is chosen; see if AMC is only source chosen
  	for (i=0;i<AALimit1.length;i++)
  	{
		 if (AALimit1.options[i].selected)
		 {
  			if (AALimit1.options[i].value != "caAmerican County Histories")
  			{
  				hideAMCDropdowns();
  				return;
  			}
  			else
  			{
  				docTypeAMC = 1;
  			}
  		}
 	}	
 	if (docTypeAMC)
 	{
 		showAMCDropdowns();
 	}
 	else
 	{
 		hideAMCDropdowns();
 	}
 	return;	
}

function showAMCDropdowns()
{
  	var showItem = getElement('AMCLimits');
  	//alert("show");
  	if (showItem != null)
  	{
  		showItem.style.visibility = "visible";
  		showItem.style.height='110px';
   	}
   	//getElement('sortx').style.display="none";
   	//showSpots();

}

function hideAMCDropdowns()
{
  	var showItem = getElement('AMCLimits');
  	//alert("hide");
  	if (showItem != null)
  	{
  		showItem.style.visibility = "hidden";
  		showItem.style.height='0px';
  	}
  	//getElement('sortx').style.display="block";
  	//showSpots();
  	
}

function showSpots()
{
	alert("hideAMCDropdowns showboxes.pixelTop "+showboxes.pixelTop
	+"\nhideAMCDropdowns hideboxes.pixelTop "+hideboxes.pixelTop
	+"\nhideAMCDropdowns searchbuttons.pixelTop "+searchbuttons.pixelTop
	+"\nhideAMCDropdowns limits.pixelTop "+limits.pixelTop
	+"\nhideAMCDropdowns AMCLimits.pixelTop "+AMCLimits.pixelTop
	+"\nhideAMCDropdowns sortx.pixelTop "+sortx.pixelTop);

	alert("hideAMCDropdowns showboxes.offsetHeight "+showboxes.offsetHeight
	+"\nhideAMCDropdowns hideboxes.offsetHeight "+hideboxes.offsetHeight
	+"\nhideAMCDropdowns searchbuttons.offsetHeight "+searchbuttons.offsetHeight
	+"\nhideAMCDropdowns limits.offsetHeight "+limits.offsetHeight
	+"\nhideAMCDropdowns AMCLimits.offsetHeight "+AMCLimits.offsetHeight
	+"\nhideAMCDropdowns sortx.offsetHeight "+sortx.offsetHeight);

	alert("hideAMCDropdowns showboxes.offsetTop "+showboxes.offsetTop
	+"\nhideAMCDropdowns hideboxes.offsetTop "+hideboxes.offsetTop
	+"\nhideAMCDropdowns searchbuttons.offsetTop "+searchbuttons.offsetTop
	+"\nhideAMCDropdowns limits.offsetTop "+limits.offsetTop
	+"\nhideAMCDropdowns AMCLimits.offsetTop "+AMCLimits.offsetTop
	+"\nhideAMCDropdowns sortx.offsetTop "+sortx.offsetTop);

}
