//******************************************************************************************************************************
//* City
//******************************************************************************************************************************
function CityInfo(x, thead, tbody) 
{
	var th, tr, td;
	var selectedCity = '';
	
	selectedCity = [x];
	DrawTableHeader(thead);	
	dataSw = "Y";
	
	for (var i = 0; i < 431; i++) 
	{
		if (selectedCity == lodge[i][4]) 
		{
			DrawTableData(tbody, i);
		}
	}
}		
 
//******************************************************************************************************************************
//* County
//******************************************************************************************************************************
function CountyInfo(x, thead, tbody) 
{
	var selectedCounty = '';
	
	selectedCounty = [x];
	DrawTableHeader(thead);	
	dataSw = "Y";
	
	for (var i = 0; i < 431; i++) 
	{
		if (selectedCounty == lodge[i][5]) 
		{
			DrawTableData(tbody, i);
		}
	}
}

//******************************************************************************************************************************
//* District
//******************************************************************************************************************************
function DistrictInfo(x, thead, tbody) 
{
	var selectedDistrict = '';
	
	selectedDistrict = [x];
	DrawTableHeader(thead);	
	dataSw = "Y";
	
	for (var i = 0; i < 431; i++) 
	{
		if (selectedDistrict == lodge[i][2]) 
		{
			DrawTableData(tbody, i);
		}
	}
}

//******************************************************************************************************************************
//* Lodge Name
//******************************************************************************************************************************
function LodgeInfo(x, thead, tbody) 
{
	var selectedLodge = '';
	
	selectedLodge = [x];
	DrawTableHeader(thead);	
	dataSw = "Y";
	
	for (var i = 0; i < 431; i++) 
	{
		if (selectedLodge == lodge[i][1]) 
		{
			DrawTableData(tbody, i);
		}
	}
}

//******************************************************************************************************************************
//* Lodge Number
//******************************************************************************************************************************
function NumberInfo(x, thead, tbody) 
{
	var selectedNumber = '';
	
	selectedNumber = [x];
	DrawTableHeader(thead);	
	dataSw = "Y";
	
	for (var i = 0; i < 431; i++) 
	{
		if (selectedNumber == lodge[i][0]) 
		{
			DrawTableData(tbody, i);
		}
	}
}

//******************************************************************************************************************************
//* Meeting Night
//******************************************************************************************************************************
function MeetingInfo(x, thead, tbody) 
{
	var selectedMeeting = '';
	
	selectedMeeting = [x];
	DrawTableHeader(thead);	
	dataSw = "Y";
	
	for (var i = 0; i < 431; i++) 
	{
		if (selectedMeeting == lodge[i][6]) 
		{
			DrawTableData(tbody, i);
		}
	}
}

//******************************************************************************************************************************
//* Delete rows
//******************************************************************************************************************************
function ClearTable(table)
{
	while (table.rows.length > 0)
	{
		table.deleteRow(0);
	}
}

//******************************************************************************************************************************
//* Draw table header
//******************************************************************************************************************************
function DrawTableHeader(thead)
{
	thead = document.getElementById(thead);
	ClearTable(thead);
	tr = thead.insertRow(thead.rows.length);
	
	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	td.innerHTML = "<b>Lodge</b>";

	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	td.innerHTML = "<b>Number</b>";

	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	td.innerHTML = "<b>District</b>";

	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	td.innerHTML = "<b>Street</b>";

	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	td.innerHTML = "<b>City</b>";

	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	td.innerHTML = "<b>County</b>";

	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	td.innerHTML = "<b>Business Meeting</b>";

	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	td.innerHTML = "<b>Meeting</b>";

	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	td.innerHTML = "<b>Supper</b>";

	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	td.innerHTML = "<b>eMail</b>";
}

//******************************************************************************************************************************
//* Draw table data
//******************************************************************************************************************************
function DrawTableData(tbody, i)
{
	
	tbody = document.getElementById(tbody);
	if (dataSw == "Y")
	{
		ClearTable(tbody);		
		dataSw = "N";
	}
	tr = tbody.insertRow(tbody.rows.length);

	//--------------------------------------------------------------------------------------------------------------------
	// Name
	//--------------------------------------------------------------------------------------------------------------------
	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
//	document.write(lodge[i][2])

	if (lodge[i][9] == "NA")  
	{
		td.innerHTML = lodge[i][1];
	}
	else
	{
		td.innerHTML = '<a href="'+lodge[i][9]+'" target="_blank">'+lodge[i][1]+'</a>';
	}

	//--------------------------------------------------------------------------------------------------------------------
	// Number
	//--------------------------------------------------------------------------------------------------------------------
	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	td.innerHTML = lodge[i][0];

	//--------------------------------------------------------------------------------------------------------------------
	// District
	//--------------------------------------------------------------------------------------------------------------------
	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	td.innerHTML = lodge[i][2];

	//--------------------------------------------------------------------------------------------------------------------
	// Street
	//--------------------------------------------------------------------------------------------------------------------
	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	if (lodge[i][3] == "NA")
	{
		td.innerHTML = "Not Avaliable";
	}
	else
	{
//		td.innerHTML = lodge[i][3];

		var a = lodge[i][3].length;
		var b = lodge[i][3];
		var c = " ";

		for (var x = 0; x < a; x++)
		{
			var d = b.charAt(x);
		        if (d == ' ')
		        {
				c = c + '+';
			}
			else
			{
				c = c + d;
			}
		}

		td.innerHTML = '<a href="http://www.mapquest.com/maps/map.adp?city='+lodge[i][4]+'&state=SC&address='+c+'&country=us&zoom=8">'+lodge[i][3]+'</a>';
	}


	//--------------------------------------------------------------------------------------------------------------------
	// City
	//--------------------------------------------------------------------------------------------------------------------
	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	td.innerHTML = lodge[i][4];

	//--------------------------------------------------------------------------------------------------------------------
	// County
	//--------------------------------------------------------------------------------------------------------------------
	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	td.innerHTML = lodge[i][5];

	//--------------------------------------------------------------------------------------------------------------------
	// Business Meeting
	//--------------------------------------------------------------------------------------------------------------------
	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	td.innerHTML = lodge[i][6];

        //--------------------------------------------------------------------------------------------------------------------
	// Business Meeting Time
	//--------------------------------------------------------------------------------------------------------------------
	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	if (lodge[i][7] == "NA")
	{
		td.innerHTML = ".";
	}
	else
	{
		td.innerHTML = lodge[i][7];
	}
	//--------------------------------------------------------------------------------------------------------------------
	// Supper 
	//--------------------------------------------------------------------------------------------------------------------
	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	if (lodge[i][8] == "NA")
	{
		td.innerHTML = ".";
	}
	else
	{
		td.innerHTML = lodge[i][8]
	}

	//--------------------------------------------------------------------------------------------------------------------
	// eMail
	//--------------------------------------------------------------------------------------------------------------------
	td = tr.insertCell(tr.cells.length);
	td.setAttribute("class", "ctr");
	if (lodge[i][10] == "NA")
	{
		td.innerHTML = ".";
	}
	else
	{
		td.innerHTML = '<a href="mailto:'+lodge[i][10]+'?subject=Initiated from scgrandlodge.org">click here</a>';
	}

}