//THESE FUNCTIONS ARE USED IN THE QUERY TOOL AND AQUAMAP

function InvokeMap(layerName, selectedIdsTextBoxName, visibleInLegend)
{
    //gets the checked ids and shows them on the map
	document.getElementById(selectedIdsTextBoxName).value = GetCheckedIds();
	if(document.getElementById("txtSelectedIds").value == "")
	{
		alert("You must select at least one item to display on the map.");
		return;
	}
	showOnMapFromQueryTool(layerName, selectedIdsTextBoxName, visibleInLegend);
}

function GetCheckedIds()
{
	var checkedIds = "";
    var grid = ISGetObject("DG");
	var checkedRows = grid.RootTable.GetCheckedRows();

	for (var i=0; i<checkedRows.length; i++) 
	{
		checkedIds = checkedIds + checkedRows[i].keyValue + ",";
	}

	return checkedIds;
}

function CheckBoxClicked(gridId, tblName, colName, checkboxValue) 
{
    //adds or removes the object from the selected list, depending on if the check box is checked or not
	var grid = ISGetObject(gridId);
	var row = grid.Tables[tblName].ToRowObject(grid.CheckedRowContext);
	var keyCol = row.GetCell(document.getElementById("txtKeyName").value);
	
	if (checkboxValue) 
	{
		AddId(keyCol.Text);
	} 
	else 
	{
		RemoveId(keyCol.Text);
	}
	
	return true;
}

function ActionDispatched()
{
	var grid = ISGetObject("DG");
    var numOfRows = grid.RootTable.GetRowsCount(); 

	alert(numOfRows); 

	for(var idx=0; idx<numOfRows; idx++)
	{
		var row = grid.RootTable.GetRow(idx);
		if (row.Type == "Record") 
		{
			var keyCol = row.GetCell(document.getElementById("txtKeyName").value);
			if(IdIsSelected(keyCol.Text)==true)
			{
				row.Check();
			}
		}
	}
	
	return true;
}

function AddId(id) 
{
	if(IdIsSelected(id)==false)
	{
		var selectedIds = document.getElementById("txtSelectedIds").value;
		document.getElementById("txtSelectedIds").value = selectedIds + id + ',';
	}

	return 0;
}

function IdIsSelected(id)
{
	var selectedIds = document.getElementById("txtSelectedIds").value;
    var arr = selectedIds.split(",");
	for(var i=0; i<arr.length-1; i++)
	{
		if(arr[i] == id)
		{
			return true;
		}
	}
		
	return false;
}

function RemoveId(id)
{
	var selectedIds = document.getElementById("txtSelectedIds").value;
    var arr = selectedIds.split(",");
	var selIds = '';
	for(var i=0; i<arr.length-1; i++)
	{
		if(arr[i] != id)
		{
			selIds = selIds + arr[i] + ',';
		}
	}
		
	document.getElementById("txtSelectedIds").value = selIds;

	return false;
}
	
function getMap()
{
	if (navigator.appName == "Netscape")
	{
		return top.opener.top.mapFrame.document.embeds[0];
	}
	else
	{
		return top.opener.top.mapFrame.document.myMap;
	}
}
	
function CheckAll()
{
    //If the top-most checkbox is selected, then select all the checkboxes.
	for (var i=0;i<document.dynamic_query.elements.length;i++)
	{
		var e = document.dynamic_query.elements[i];
		if ((e.name != 'selectAll') && (e.type=='checkbox'))
		{
			e.checked = document.dynamic_query.selectAll.checked;
		}
	}
}
	
function CheckCheckAll()
{
    //If all the checkboxes are selected, then select the top-most checkbox, as well.
	var TotalBoxes = 0;
	var TotalOn = 0;
	for (var i=0; i<document.dynamic_query.elements.length; i++)
	{
		var e = document.dynamic_query.elements[i];
		if ((e.name != 'selectAll') && (e.type=='checkbox'))
		{
			TotalBoxes++;
			if (e.checked)
			{
				TotalOn++;
			}
		}
	}
	if (TotalBoxes==TotalOn)
	{
		document.dynamic_query.selectAll.checked=true;
	}
	else
	{
		document.dynamic_query.selectAll.checked=false;
	}
}

function showOnMapFromQueryTool(theLayer, SelectedIdsTextBox, visibleInLegend) 
{
    //alert("showOnMapFromQueryTool");
    //var KEYLIST = document.all[SelectedIdsTextBox].value;
    var KEYLIST = document.getElementById(SelectedIdsTextBox).value;
	//check to see if the last character is "," if so remove it
	if(KEYLIST.substr(KEYLIST.length-1, 1) == ",")
	{
		KEYLIST = KEYLIST.substr(0, KEYLIST.length-1);
	}

	if(KEYLIST == "")
	{
		alert('Please make a selection first');
		return;
	}
			
	var map = getMap();
	map.getMapLayers();
	map.getSelection().clear();
		
	if (map.getScale() < 200000)
	{
		map.zoomScale(55.159905,-118.818377,200000);
	}

	doSQLWhereFromQueryTool(theLayer, KEYLIST, visibleInLegend);
	doSelectionFromQueryTool(theLayer, KEYLIST);
}

function doSQLWhereFromQueryTool(theLayer, keyList, visibleInLegend)
{
    //alert("doSQLWhereFromQueryTool");
    //sets the layer's SQL statement with the keyList of object ids and makes the layer visible
	var map = getMap();
	if (map.isBusy() || map == null)
	{
		setTimeout("doSQLWhereFromQueryTool('" + theLayer + "','" + keyList + "');", 500);
		return;
	}
	else
	{
		map.setAutoRefresh(false);
		var mapLayer = map.getMapLayer(theLayer);
		if(theLayer == "QueryTool_Hydrants")
		{
		    mapLayer.setSQLWhere("MapGuideKey IN (" + keyList + ")");		
		}
		else if(theLayer == "QueryTool_Trees")
		{
		    mapLayer.setSQLWhere("COMPKEY IN (" + keyList + ")");
		}
		else if(theLayer == "QueryTool_2001Statistics_AgeSex" || theLayer == "QueryTool_2001Statistics_MarriedFamily" || theLayer == "QueryTool_2001Statistics_Labour" || theLayer == "QueryTool_2001Statistics_Education" || theLayer == "QueryTool_2001Statistics_SocioEconomic")
		{
		    mapLayer.setSQLWhere("EA_ID IN (" + keyList + ")");
		}
		else if(theLayer == "QueryTool_2001Statistics_Language" || theLayer == "QueryTool_2001Statistics_Citizenship")
		{
		    mapLayer.setSQLWhere("EU_ID IN (" + keyList + ")");
		}
		else if(theLayer == "QueryTool_1997Statistics_Age" || theLayer == "QueryTool_1997Statistics_Aboriginal" || theLayer == "QueryTool_1997Statistics_Education" || theLayer == "QueryTool_1997Statistics_Ethnicity" || theLayer == "QueryTool_1997Statistics_Families" || theLayer == "QueryTool_1997Statistics_Immigration" || theLayer == "QueryTool_1997Statistics_Income" || theLayer == "QueryTool_1997Statistics_Labour" || theLayer == "QueryTool_1997Statistics_Language")
		{
		    mapLayer.setSQLWhere("Geography IN (" + keyList + ")");
		}
		else
		{
		    mapLayer.setSQLWhere("FEATURE_ID IN (" + keyList + ")");		
		}
		mapLayer.setVisibility(true);
		mapLayer.setShowInLegend(visibleInLegend);
		map.setAutoRefresh(true);
		map.refresh();
	}
}

function doSelectionFromQueryTool(theLayer, keyList)
{
    //zoom to and highlight the list of objects
	var map = getMap();
	if (map.isBusy() || map == null)
	{
		setTimeout("doSelectionFromQueryTool('" + theLayer + "','" + keyList + "');", 500);
		return;
	}
	else
	{
		map.setAutoRefresh(false);
		var mapLayer = map.getMapLayer(theLayer);
		// Create an MGCollection object called 'mapFeatures'
		var mapFeatures = map.createObject("MGCollection");
		var keyListArray = keyList.split(",");

		for (var i=0; i<keyListArray.length; i++)
		{
			var e = keyListArray[i];
			mapFeatures.add(mapLayer.getMapObject(e));
		}

		// Select and display contents of 'mapFeatures'
		if (mapFeatures.size() > 0)
		{
			map.getSelection().addObjectsEx(mapFeatures, true);
			if (map.getSelection().getNumObjects() == 0)
			{
				var msg = "";
				msg = "No objects were selected.";
				msg = msg + "\nPossible reasons:";
				msg = msg + "\n1.  The '" + theLayer + "' may not be visible at the current scale.";
				msg = msg + "\n2.  No objects in the window met the query criteria."
				alert(msg);
			}
			else
			{
				map.zoomSelected();
				var theScale = map.getScale();
				var theLat = map.getLat();
				var theLon = map.getLon();
				if (theScale <= 5000) 
				{
					theScale = 5000;
				}
				else if ((theScale > 5000) && (theScale <= 10000))
				{
					theScale = theScale * 1.20;
				}
				else if ((theScale > 10000) && (theScale <= 15000))
				{
					theScale = theScale * 1.30;
				}
				else if ((theScale > 15000) && (theScale <= 20000))
				{
					theScale = 20000;
				}
				else if (theScale > 20000)
				{
					theScale = theScale;
				}
				map.zoomScale(theLat, theLon, theScale);
				top.opener.top.focus();
			}
		}
		else
		{
			alert("No objects where selected.");
		}
		map.setAutoRefresh(true);
		map.refresh();
	}
}
	
function doZoomOut()
{
	var map = getMap();
	if (map.isBusy() || map == null)
	{
		setTimeout("doZoomOut();", 500);
		return;
	}
	else
	{
		map.zoomOut();
	}
}

function openQueryToolWindow()
{
    if(queryToolWindow == null || queryToolWindow.closed == true)
    {
        var queryToolWindowProperties = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=900,height=450";
	    queryToolWindow = open("QueryTool.aspx", "queryToolWin", queryToolWindowProperties);
	}
	else
	{
	    queryToolWindow.focus();
	}
}

function valEntry(element)
{
    var myValue = element.value;
    var srchStr = /%/g;
    var replaceStr = "*";
    myValue = myValue.replace(srchStr, replaceStr);
    element.value = myValue;
}

//NOT USED BUT SHOULD BE IN SOME FORM - JM
function DisplayNumberOfSelectedRecords(selectedIds)
{
	var arr = selectedIds.split(",");
	var numberOfSelectedRecords = arr.length - 1;
	
	if(numberOfSelectedRecords == 0)
	{
	    document.getElementById("lblNumberOfSelectedRecords").innerHTML = ""; 
	    return false;
	}
	if(numberOfSelectedRecords == 1)
	{
	    document.getElementById("lblNumberOfSelectedRecords").innerHTML = "1 item selected"; 
	    return false;
	}
	if(numberOfSelectedRecords > 1)
	{
	    document.getElementById("lblNumberOfSelectedRecords").innerHTML = "" + numberOfSelectedRecords + " items selected"; 
	    return false;
	}
}

//new webgrid javascript

function GetIDsAndShowOnMap(layerNameTextBoxName, selectedIdsTextBoxName, visibleInLegendTextBoxName) {
    //alert("GetIDsAndShowOnMap: " + document.getElementById(selectedIdsTextBoxName).value);
    if (document.getElementById(selectedIdsTextBoxName).value == "") {
        alert("You must select at least one item to display on the map.");
        return;
    }
    var layerName = document.getElementById(layerNameTextBoxName).value;
    var visibleInLegend = document.getElementById(visibleInLegendTextBoxName).value;
    showOnMapFromQueryTool(layerName, selectedIdsTextBoxName, visibleInLegend);
}