// JavaScript Document
function myQuery (eventID, queryType, coords)
{
	//alert(document.FormOne.ddlSection.options.length); 
	var Lon2 = '';
	var Lat2 = '';
	var region = document.getElementById("ddlGRegion").value;
	var c = '' + coords;
	var coordinates = c.split(',');
	if (coordinates.length == 2) //single point requested
	{
		var NumOfCoord = 2;
		var Lon1 = coordinates[0];
		if (Lon1 > 180)
		{
			Lon1 = Lon1 - 360;
		}
		var Lat1 = coordinates[1];
	}
	if (coordinates.length == 4)
	{
		var NumOfCoord = 4;
		var Lon1 = coordinates[0];
		if (Lon1 > 180)
		{
			Lon1 = Lon1 - 360;
		}
		var Lat1 = coordinates[1];
		Lon2 = coordinates[2];
		if (Lon2 > 180)
		{
			Lon2 = Lon2 - 360;
		}
		Lat2 = coordinates[3];
		//if (document.getElementById("ddlGRegion").value != "Discrete0")//can't make coord. selection if on All regions map
		//{
			document.FormOne.tbxWest.value = Lon1;
			document.FormOne.tbxEast.value = Lon2;
			document.FormOne.tbxSouth.value = Lat2;
			document.FormOne.tbxNorth.value = Lat1;
		//}
	}
	//We don't query metadata when only one sections is on the map
	if (document.FormOne.ddlSection.options.length == 1 || document.FormOne.ddlSection.selectedIndex == 0)
	{
		var request = new Subsys_JsHttpRequest_Js();
		request.onreadystatechange = function()				
		{
			if (request.readyState == 4)
			{
				if (request.responseJS)
				{
					if (request.responseJS.Sections[0].search("Sorry") == -1)//Sections are found
					{
						var l = request.responseJS.Sections.length;
						var Sections_url = "";
						var k = NoURLSections.length;
						for (var i=0; i < l; i++)
						{
							var url = "<a href='http://mercury.ornl.gov/ocean/query.jsp?query=section&BATDBList=oceanbt&BATTerm1=" + request.responseJS.Sections[i] + "' target='_blank'>" + request.responseJS.Sections[i] + "</a>";
							for (var j = 0; j < k; j++)
							{
								if (NoURLSections[j] == request.responseJS.Sections[i])
								{
									url = "<a href='http://mercury.ornl.gov/ocean/query.jsp?query=dataset&BATDBList=oceanbt&BATTerm1=Global%20Ocean%20Data%20Analysis%20Project%20(GLODAP)' target='_blank'>" + request.responseJS.Sections[i] + "</a>";	
									break;
								}
							}
							Sections_url = Sections_url + url; 
							if (l > 1 && l != (l-1))
							{
								Sections_url = Sections_url + ", ";
							}
						}
						var selected_reg_index = document.getElementById("ddlGRegion").selectedIndex;
						var region_name = document.FormOne.ddlGRegion.options[selected_reg_index].text;
						document.getElementById("metadata").innerHTML = "Geographical Region: " + region_name + "<br>Section(s): " + Sections_url;
					}
					else
					{
						document.getElementById("metadata").innerHTML = request.responseJS.Sections[0];
					}
				}
			}
		}
		//alert (region);
		request.cashing = true;
		request.open ('POST', 'mapquery.php', true);
		request.send ({NofC: NumOfCoord, reg: region, Lon1: Lon1, Lat1: Lat1, Lon2: Lon2, Lat2: Lat2});
	}
}