
//************************
// Main function that calls all other functions
//************************
function searchList()
{
	var keyword = form.keyword.value;
	myKeyword = keyword.replace("'", "%27");  //converting apostrophes to %27 symbols
	
	if (form.keyword.value =="")
	{
		document.getElementById("message").innerHTML = "<div><p>Please enter a City or County.</p></div>";
		changeMap(0);
	}
	else 
	{
		reset();
		searchCounty();
		searchCity();
		analyzeSearch();
	}
}

//************************
// Secondary Main function that calls all other functions for **COUNTIES
//************************
function searchListForCounties()
{
	var keyword = form.keyword.value;
	myKeyword = keyword.replace("'", "%27");  //converting apostrophes to %27 symbols
	
	if (form.keyword.value =="")
	{
		document.getElementById("message").innerHTML = "<div>Please enter a City or County.</div>";
		changeMap(0);
	}
	else 
	{
		reset();
		searchCounty();
		analyzeSearch();
		document.getElementById("message").innerHTML = "<div><p>This is a city and county name.  Which are you referring to?</p><input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchListForCities()><label>City</label> <input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchListForCounties() checked><label>County</label> <p>Are you not sure which to select?</p><input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchNotSure()><label>Not Sure</label></p></div>"
	
	}
}

//************************
// Secondary Main function that calls all other functions for **CITIES
//************************
function searchListForCities()
{
	var keyword = form.keyword.value;
	myKeyword = keyword.replace("'", "%27");  //converting apostrophes to %27 symbols
	
	if (form.keyword.value =="")
	{
		document.getElementById("message").innerHTML = "<div><p>Please enter a City or County.</p></div>";
		changeMap(0);
	}
	else 
	{
		reset();
		searchCity();
		analyzeSearch();
		
		if (multipleDrop == 0) {
		document.getElementById("message").innerHTML = "<div><p>This is a city and county name.  Which are you referring to?</p><input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchListForCities() checked><label>City</label> <input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchListForCounties()><label>County</label> <p>Are you not sure which to select?</p><input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchNotSure()><label>Not Sure</label></p></div>"
		}
		
	}
}

function searchListForMultipleCities()
{
	var keyword = form.keyword.value;
	myKeyword = keyword.replace("'", "%27");  //converting apostrophes to %27 symbols
	
	if (form.keyword.value =="")
	{
		document.getElementById("message").innerHTML = "<div><p>Please enter a City or County.</p></div>";
		changeMap(0);
	}
	else 
	{
		reset();
		searchCity();
		analyzeSearch();
		document.getElementById("message").innerHTML = "<div><p>This city appears in more than one county.  Which county are you referring to?</p>" + messageOptions + "<p>Are you not sure which to select?</p><input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchNotSure()><label>Not Sure</label></p></div>";
	}
}

function searchListForMultipleCitiesWithCounties()
{
	var keyword = form.keyword.value;
	myKeyword = keyword.replace("'", "%27");  //converting apostrophes to %27 symbols
	
	if (form.keyword.value =="")
	{
		document.getElementById("message").innerHTML = "<div><p>Please enter a City or County.</p></div>";
		changeMap(0);
	}
	else 
	{
		reset();
		searchCity();
		analyzeSearch();
		if (multipleDrop == 0) {
		document.getElementById("message").innerHTML = "<div><p>This is a city and county name.  Which are you referring to?</p><input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchListForMultipleCitiesWithCounties() checked><label>City</label> <input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchListForCounties()><label>County</label> <p>Are you not sure which to select?</p><input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchNotSure()><label>Not Sure</label></p></div>";
	}
	}
}

function searchNotSure()
{
changeMap(9);



}

//************************
// Analyze the search results
//************************
function analyzeSearch()
{
	var extendedCounty = new Array();
	var searchCount = new Array(GulfCoastDistrictCountyNum, EastTexasDistrictCountyNum, 
								CentralTexasDistrictCountyNum, MetroplexDistrictCountyNum, 
								PanhandleDistrictCountyNum, NorthTexasDistrictCountyNum, 
								TexarkanaDistrictCountyNum, SouthAndWestDistrictCountyNum, 
								GulfCoastDistrictCityNum, EastTexasDistrictCityNum,  
								CentralTexasDistrictCityNum, MetroplexDistrictCityNum, 
								PanhandleDistrictCityNum, NorthTexasDistrictCityNum, 
								TexarkanaDistrictCityNum, SouthAndWestDistrictCityNum);
	
	//Setup countyFound variable
	for (var i=0; i<8; i++) 
	{
		if (searchCount[i] >= 1) 
		{
			countyFound+=searchCount[i];
		}
	}
	
	//Setup cityFound variable
	for (var i=8; i<16; i++) 
	{
		if (searchCount[i] >= 1) 
		{
			cityFound+=searchCount[i];
		}
	}
	
	//setup matchFound variable
	matchFound = countyFound + cityFound;
	
	
	/***Begin Use For Testing***/
//		alert(searchCount[0] + ", " + searchCount[1] + ", " + searchCount[2] + ", " + searchCount[3] + ", " + searchCount[4] + ", " + 
//				searchCount[5] + ", " + searchCount[6] + ", " + searchCount[7] + "\n " + searchCount[8] + ", " + searchCount[9] + ", " + 
//				searchCount[10] + ", " + searchCount[11] + ", " + searchCount[12] + ", " + searchCount[13] + ", " + searchCount[14] + ", " + searchCount[15]);
	/***End Use for Testing***/
	
	//No matches
	if (matchFound == 0)
	{
		document.getElementById("message").innerHTML = "<div><p>This city or county was not found.</p> <p> Please help us enhance our search by emailing <a href=mailto:afling@stewart.com?subject=Requesting&nbsp;Addition&nbsp;to&nbsp;Search&nbsp;Tool>afling@stewart.com</a> to add this city or county.</p></div>";
		changeMap(0);
	}
	
	//One match
	if (matchFound == 1)
	{
		document.getElementById("message").innerHTML = "";
		for (i=0; i<8; i++) 
		{
		if	(searchCount[i] >=1) {changeMap(i+1);}
		}
		for (i=8; i<16; i++) 
		{
		if	(searchCount[i] >=1) {changeMap(i-7);}
		}
		
	}
	
	//One city and one county with the district or different districts
	if ((cityFound == 1) && (countyFound == 1))
	{
				
			//if search results for 1 county and 1 city in same district then just display that district
			if ((searchCount[0] == 1) && (searchCount[8] == 1))
			{
				document.getElementById("message").innerHTML = "";
				changeMap(1);
			}
			else if ((searchCount[1] == 1) && (searchCount[9] == 1))
			{
				document.getElementById("message").innerHTML = "";
				changeMap(2);
			}
			else if ((searchCount[2] == 1) && (searchCount[10] == 1))
			{
				document.getElementById("message").innerHTML = "";
				changeMap(3);
			}
			else if ((searchCount[3] == 1) && (searchCount[11] == 1))
			{
				document.getElementById("message").innerHTML = "";
				changeMap(4);
			}
			else if ((searchCount[4] == 1) && (searchCount[12] == 1))
			{
				document.getElementById("message").innerHTML = "";
				changeMap(5);
			}
			else if ((searchCount[5] == 1) && (searchCount[13] == 1))
			{
				document.getElementById("message").innerHTML = "";
				changeMap(6);
			}
			else if ((searchCount[6] == 1) && (searchCount[14] == 1))
			{
				document.getElementById("message").innerHTML = "";
				changeMap(7);
			}
			else if ((searchCount[7] == 1) && (searchCount[15] == 1))
			{
				document.getElementById("message").innerHTML = "";
				changeMap(8);
			}
			
			//if search results for 1 county and 1 city in different districts then choose district
			else 
			{
				document.getElementById("message").innerHTML = "<div><p>This is a city and county name.  Which are you referring to?</p><input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchListForCities()><label>City</label> <input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchListForCounties()><label>County</label> <p>Are you not sure which to select?</p><input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchNotSure()><label>Not Sure</label></p></div>"
			}
	}
	
	
	
	
	//if no counties are found, then check each city individually for a match
	if ((cityFound > 1) && (countyFound==0)) {
	
		var separated = true;
		var messageOptions = "";
		var k=0;
		var j=0; 
		var temp = "";
		var newKeyword = "";
		var newMessageOptions = "";
		
		//2 or more in the same district
		for (i=8; i<16; i++) 
		{
			if (searchCount[i] == cityFound) 
			{
				document.getElementById("message").innerHTML = "";
				changeMap(i-7);
				separated=false;
			} 
		}
		
		//2 or more in different districts
		if (separated == true)
		{
			for (i=0; i<(extendedList.length); i=i+2) 
			{	
				if (myKeyword.toUpperCase() == extendedList[i].toUpperCase())
				{	
					extendedCounty[j] = extendedList[i+1];
					j++;					
				}
			}		
			for (var n=0; n<(extendedCounty.length); n++)
			{
				newKeyword = extendedCounty[n];
				searchMultipleCounty(n, newKeyword);			
			}
			document.getElementById("message").innerHTML = "<div><p>This city appears in more than one county.  Which county are you referring to?</p>" + newMessageOptions + " <p>Are you not sure which to select?</p><input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchNotSure()><label>Not Sure</label></p></div>";
			multipleDrop = 1;
			
			
		}
	}
	

if ((cityFound > 1) && (countyFound >= 1)) {

		document.getElementById("message").innerHTML = "<div><p>This is a city and county name.  Which are you referring to?</p><input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchListForMultipleCitiesWithCounties()><label>City</label> <input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchListForCounties()><label>County</label> <p>Are you not sure which to select?</p><input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=searchNotSure()><label>Not Sure</label></p></div>";
	
	}
	
function searchMultipleCounty(m, newKeyword) 
{ 

	//Gulf Coast District City
	for (i=0; i<(GulfCoastDistrictCounty.length); i++) 
	{	if (newKeyword.toUpperCase() == GulfCoastDistrictCounty[i].toUpperCase())
		{	
			newMessageOptions += "<input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=changeMap(1);><label>" + extendedCounty[m] + "</label><br>";
			//alert("newMessageOptions1: " + newMessageOptions);
		}
	}
	
	//East Texas City
	for (i=0; i<(EastTexasDistrictCounty.length); i++) 
	{	if (newKeyword.toUpperCase() == EastTexasDistrictCounty[i].toUpperCase())
		{	
			newMessageOptions += "<input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=changeMap(2);><label>" + extendedCounty[m] + "</label><br>";
			//alert("newMessageOptions2: " + newMessageOptions);
		}
	}

	//Central Texas City
	for (i=0; i<(CentralTexasDistrictCounty.length); i++) 
	{	if (newKeyword.toUpperCase() == CentralTexasDistrictCounty[i].toUpperCase())
		{	
			newMessageOptions += "<input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=changeMap(3);><label>" + extendedCounty[m] + "</label><br>";
			//alert("newMessageOptions3: " + newMessageOptions);
		}
	}

	//Metroplex City
	for (i=0; i<(MetroplexDistrictCounty.length); i++) 
	{	if (newKeyword.toUpperCase() == MetroplexDistrictCounty[i].toUpperCase())
		{	
			//alert("metro - " + extendedCounty[k]);
			newMessageOptions += "<input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=changeMap(4);><label>" + extendedCounty[m] + "</label><br>";
			//alert("newMessageOptions4: " + newMessageOptions);
		}
	}
	
	//Panhandle City
	for (i=0; i<(PanhandleDistrictCounty.length); i++) 
	{	if (newKeyword.toUpperCase() == PanhandleDistrictCounty[i].toUpperCase())
		{
			newMessageOptions += "<input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=changeMap(5);><label>" + extendedCounty[m] + "</label><br>";
			//alert("newMessageOptions5: " + newMessageOptions);
		}
	}
	
	//North Texas City
	for (i=0; i<(NorthTexasDistrictCounty.length); i++) 
	{	if (newKeyword.toUpperCase() == NorthTexasDistrictCounty[i].toUpperCase())
		{	
			newMessageOptions += "<input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=changeMap(6);><label>" + extendedCounty[m] + "</label><br>";
			//alert("newMessageOptions6: " + newMessageOptions);	
		}
	}
	
	//Texarkana City
	for (i=0; i<(TexarkanaDistrictCounty.length); i++) 
	{	if (newKeyword.toUpperCase() == TexarkanaDistrictCounty[i].toUpperCase())
		{	
			newMessageOptions += "<input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=changeMap(7);><label>" + extendedCounty[m] + "</label><br>";
			//alert("newMessageOptions7: " + newMessageOptions);
		}
	}
	
	//South and West City
	for (i=0; i<(SouthAndWestDistrictCounty.length); i++) 
	{	if (newKeyword.toUpperCase() == SouthAndWestDistrictCounty[i].toUpperCase())
		{				
			newMessageOptions += "<input type=radio name=LevelOneOption value=LevelOneOption onMouseUp=changeMap(8);><label>" + extendedCounty[m] + "</label><br>";
			//alert("newMessageOptions8: " + newMessageOptions);
		}
	}
}

}
