//javascript for searching for urban spaces by size

//zoom level works for most urban spaces
var sensibleZoom=17;

function createList()
{
	var AreaTarget = Number(document.params.area.value);
	var SpaceAreaTol = Number(document.params.areatol.value);
	var AreaVariation = ((AreaTarget * SpaceAreaTol) / 100);
	
	//set parameters for area
	var SpaceAreaMin = AreaTarget - AreaVariation;
	var SpaceAreaMax = AreaTarget + AreaVariation;

	
	//clear existing results and add top line
	document.all.SpaceListPanel.innerHTML = "Click name to preview in map:<br><br>";

	//debug test to see if the parameters are set right
	//alert("The area target is " + AreaTarget + " and the SpaceAreaTol is " + SpaceAreaTol);

	//read and check xml doc
	xmlDoc=loadXMLDoc("database.xml");


	//get arrays
	SpaceName = xmlDoc.getElementsByTagName("NAME");
	SpaceLatitude = xmlDoc.getElementsByTagName("LATITUDE");
	SpaceLongitude = xmlDoc.getElementsByTagName("LONGITUDE");
	SpaceLongLength = xmlDoc.getElementsByTagName("LONGDIM");
	SpaceShortLength = xmlDoc.getElementsByTagName("SHORTDIM");
	SpaceOrientation = xmlDoc.getElementsByTagName("ORIENTATION");
	SpaceCity = xmlDoc.getElementsByTagName("CITY");
	SpaceCountry = xmlDoc.getElementsByTagName("COUNTRY");
	SpaceShape = xmlDoc.getElementsByTagName("SHAPE");
	
	SpaceArea = xmlDoc.getElementsByTagName("AREA");



	var searchTotal = 0;
	var i=0;

	//for (i=0;i<=SpaceName.length;i++)

	for (i=0;i<=401;i++)
	{	  
		var area = (SpaceShortLength[i].childNodes[0].nodeValue * SpaceLongLength[i].childNodes[0].nodeValue) / 10000;

		if (area == 0) {area = (SpaceArea[i].childNodes[0].nodeValue / 10000);}
		
		var sensibleZoom=18;
		if (area > 1.5) {sensibleZoom=17;}
		if (area > 6) {sensibleZoom=16;}
		if (area > 10) {sensibleZoom=15;}

		var isShow = true;
		if (document.params.filterbyArea.checked == true && (area >= SpaceAreaMax || area <= SpaceAreaMin))
			isShow = false;
		if (document.params.filterbyGeometry.checked == true && ((SpaceShape[i].childNodes[0].nodeValue == "rectangular" && document.params.filterRectangular.checked == false)
			|| (SpaceShape[i].childNodes[0].nodeValue == "square" && document.params.filterSquare.checked == false)
			|| (SpaceShape[i].childNodes[0].nodeValue == "circular" && document.params.filterCircular.checked == false)
			|| (SpaceShape[i].childNodes[0].nodeValue == "triangular" && document.params.filterTriangular.checked == false)
  			|| (SpaceShape[i].childNodes[0].nodeValue == "elliptical" && document.params.filterElliptical.checked == false)
			|| (SpaceShape[i].childNodes[0].nodeValue == "trapezoidal" && document.params.filterTrapezoidal.checked == false)
			|| (SpaceShape[i].childNodes[0].nodeValue == "octagonal" && document.params.filterOctagonal.checked == false)
			|| (SpaceShape[i].childNodes[0].nodeValue == "pentagonal" && document.params.filterPentagonal.checked == false)
			|| (SpaceShape[i].childNodes[0].nodeValue == "semicircular" && document.params.filterSemicircular.checked == false)
			|| (SpaceShape[i].childNodes[0].nodeValue == "irregular" && document.params.filterIrregular.checked == false)))
			isShow = false;
		if (document.params.filterbyCountry.checked == true && document.params.filterbyCountryCombo.value != SpaceCountry[i].childNodes[0].nodeValue)
			isShow = false;

		if (isShow)
		{
			searchTotal++;	  
			if (searchTotal == 1) 
			{
				loadTheMap(SpaceLatitude[i].childNodes[0].nodeValue,SpaceLongitude[i].childNodes[0].nodeValue,sensibleZoom);
			}
			entry = searchTotal + ". <a href='javascript:loadTheMap(" + SpaceLatitude[i].childNodes[0].nodeValue + " , ";
			entry += SpaceLongitude[i].childNodes[0].nodeValue + "," + sensibleZoom + ");' style='color:#006e3a'>" + SpaceName[i].childNodes[0].nodeValue + ", ";
			entry += SpaceCity[i].childNodes[0].nodeValue + "</a><br>(" + SpaceShape[i].childNodes[0].nodeValue;
			if (SpaceShape[i].childNodes[0].nodeValue == "rectangular" || SpaceShape[i].childNodes[0].nodeValue == "square")
				entry += ", " + SpaceLongLength[i].childNodes[0].nodeValue + "m x " + SpaceShortLength[i].childNodes[0].nodeValue + "m";
			entry += "), area " + Math.round(area*1000)/1000 + " ha";
	  		document.all.SpaceListPanel.innerHTML += entry + "<br>";

		}
		
	}	
	document.all.statusTerms.innerHTML = "(" + searchTotal + " results)";
}


function loadTheMap(theLatitude,theLongitude,zoomLevel) 
{
	if (GBrowserIsCompatible())
	{
 	       var map = new GMap2(document.getElementById("map"));
	       map.setCenter(new GLatLng(theLatitude, theLongitude), zoomLevel);
	       map.setMapType(G_SATELLITE_MAP);
	       map.addControl(new GSmallMapControl());
                   map.addControl(new GMapTypeControl());
	}
}


function loadXMLDoc(FileName) 
{
	try //Internet Explorer
  	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	catch(e)
	{
		try //Firefox, Mozilla, Opera, etc.
    		{
    		xmlDoc=document.implementation.createDocument("","",null);
    		}
  		catch(e) {alert(e.message)}
  	}
	try 
  	{
  		xmlDoc.async=false;
 	 	xmlDoc.load(FileName);
  		return(xmlDoc);
  	}
	catch(e) {alert(e.message)}
	return(null);
}

function deselectAnySize() {
	document.params.anySize.checked = false;
	document.params.filterbyArea.checked = true;
}

function deselectAnyShape() {
	document.params.anyShape.checked = false;
	document.params.filterbyGeometry.checked = true;
}
