// JavaScript Document

function myQuery (eventID, queryType, coords)
{
	//if queryType = 0 single point is requested, if = 1 we have a square
	var Lon2 = '';
	var Lat2 = '';
	if (queryType == 0) //single point requested
	{
		//var NumOfCoord = 2;
		var Lon1 = coords[0];
		if (Lon1 > 180) //if we are on Pacific/Indian map
		{
			Lon1 = Lon1 - 360;
		}
		var Lat1 = coords[1];
	}
	if (queryType == 1)//square requested
	{
		//var NumOfCoord = 4;
		var Lon1 = coords[0];
		if (Lon1 > 180) //if we are on Pacific/Indian map
		{
			Lon1 = Lon1 - 360;
		}
		var Lat1 = coords[1];
		Lon2 = coords[2];
		if (Lon2 > 180)
		{
			Lon2 = Lon2 - 360;
		}
		Lat2 = coords[3];
	}
	document.getElementById("tbxWest").value = Lon1;
	document.getElementById("tbxEast").value = Lon2;
	document.getElementById("tbxNorth").value = Lat1;
	document.getElementById("tbxSouth").value = Lat2;
	
	//We don't query metadata when only one line is on the map
	if (document.getElementById("ddlShip").selectedIndex == 0)
	{
		//Progress indicator
		document.getElementById("metadata").innerHTML = '<br/><span style="color: red; font-weight: bold; font-size:larger;">Please wait while data is loaded</span><br/><img id="progress" src="/waves/images/progress.gif"/>';
		var xhr = createXMLHttpRequest();
		xhr.onreadystatechange = function()
		{
			if(xhr.readyState == 4)//Request finished
			{
				if (xhr.status == 200)
				{
					var string = "";
					//alert (xhr.responseText);
					if (xhr.responseText.search(/Sorry/) != -1 || xhr.responseText.search(/Error/) != -1)
					{
						string = "<br/><br/>" + xhr.responseText;
					}
					else
					{
						var arrMeta = xhr.responseText.split("||");
						var l = arrMeta.length;
						//var string = "";
						for (var i = 0; i < l -1 ; i++)
						{
							var arrLine = arrMeta[i].split("|");
							string = string + "<b>Ship/Experiment:</b> " + arrLine[0] + "; " + "<b>Leg:</b> " + arrLine[1] + "<br />" + "<b>Cruise name:</b> " + arrLine[2] + "<br />" + "<b>Observer:</b> " + arrLine[3] + "<br />" + "<b>Departure port:</b> " + arrLine[4] + "; " + "<b>Departute date:</b> " + arrLine[5] + "<br />" + "<b>Arrive port:</b> " + arrLine[6] + "; " + "<b>Arrive date:</b> " + arrLine[7] + "<br />" + "<b>Comments:</b> " + arrLine[8] + "<hr />";
						}
					}
					document.getElementById("metadata").innerHTML = string;
				}
				else
				{
					alert ("An AJAX error occured");
				}
			}
		}
		if (queryType == 0) //single point requested
		{
			var coordinates = Lon1 + "," + Lat1;
		}
		if (queryType == 1)//square requested
		{
			var coordinates = Lon1 + "," + Lat1 + "," + Lon2 + "," + Lat2;
		}
		var db = document.getElementById("db").value
		xhr.open("GET", "mapquery.php?coordinates=" + coordinates + "&db=" + db , true);
		xhr.send(null);
	}
}