//javascript for searching for housing schemes


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

function deselectAnyDensity() {
	document.params.anyDensity.checked = false;
	document.params.filterbyDensity.checked = true;
}

function deselectAnyHeight() {
	document.params.anyHeight.checked = false;
	document.params.filterbyHeight.checked = true;
}

function deselectAnySiteArea() {
	document.params.anySiteArea.checked = false;
	document.params.filterbySiteArea.checked = true;
}


function showInfo() {
	document.all.theinfobox.style.visibility = "visible";
}

function hideInfo() {
	document.all.theinfobox.style.visibility = "hidden";
}

function showaddentry() {
	document.all.theaddentry.style.visibility = "visible";
}

function hideaddentry() {
	document.all.theaddentry.style.visibility = "hidden";
}

function addEntry() {

	alert ("Thank you for suggesting " + document.submitproject.NewProjectName.value + " for the database.");
	
	theMessage = "mailto:support@holisticcity.co.uk?subject=New Housing Project (" + document.submitproject.NewProjectName.value + ")&body=";
	theMessage += "Thank you for suggesting an item for the database.%0A%0APlease review the information below and send this email.%0A%0A";
	theMessage += "<b>Project Name: </b>" + document.submitproject.NewProjectName.value + "%0A";
	theMessage += "<b>Contributor: </b>" + document.submitproject.NewProjectContributor.value + "%0A";
	theMessage += "<b>Website: </b>" + document.submitproject.NewProjectContributorWebsite.value + "%0A";
	theMessage += "<b>Total Dwellings: </b>" + document.submitproject.NewProjectTotalDwellings.value + "%0A";
	theMessage += "<b>Total Houses: </b>" + document.submitproject.NewProjectTotalHouses.value + "%0A";
	theMessage += "<b>Total Flats/Maisonettes: </b>" + document.submitproject.NewProjectTotalFlats.value + "%0A";
	theMessage += "<b>Density: </b>" + document.submitproject.NewProjectDensity.value + " dw/ha%0A";
	theMessage += "<b>Latitude: </b>" + document.submitproject.NewProjectLatitude.value + " degrees%0A";
	theMessage += "<b>Longitude: </b>" + document.submitproject.NewProjectLongitude.value + " degrees%0A";
	theMessage += "<b>Includes: </b>";
	if (document.submitproject.NewProjectOneStorey.checked == true) {theMessage += "1,";}
	if (document.submitproject.NewProjectTwoStorey.checked == true) {theMessage += "2,";}
	if (document.submitproject.NewProjectThreeStorey.checked == true) {theMessage += "3,";}
	if (document.submitproject.NewProjectFourStorey.checked == true) {theMessage += "4,";}
	if (document.submitproject.NewProjectFiveStorey.checked == true) {theMessage += "5,";}
	if (document.submitproject.NewProjectSixStorey.checked == true) {theMessage += "6+,";}
	theMessage += " storeys";

	document.location.href = theMessage;

	hideaddentry();
}

function createList() {

	var UnitTarget = Number(document.params.units.value);
	var UnitTol = Number(document.params.unitstol.value);

	//set max and min for number of units
	var UnitsMin = UnitTarget - UnitTol;
	var UnitsMax = UnitTarget + UnitTol;

	//alert ("The unit target is " + UnitTarget + " and the unit tol is " + UnitTol);

	//set max and min for density
	var DensityTarget = Number(document.params.density.value);
	var DensityTol = Number(document.params.densitytol.value);

	//set parameters for density
	var DensityMin = DensityTarget - DensityTol;
	var DensityMax = DensityTarget + DensityTol;

	//clear existing results and add top line
	document.all.ProjectListPanel.innerHTML = "Your Search Results:<br><br>";

	
	xmlDoc=loadXMLDoc("housingprojects.xml");

	//alert ("XML Document loaded");

	//get arrays
	SchemeName = xmlDoc.getElementsByTagName("NAME");
	SchemeSource = xmlDoc.getElementsByTagName("SOURCE");
	SchemeReference = xmlDoc.getElementsByTagName("REFERENCE");
	SchemeLatitude = xmlDoc.getElementsByTagName("LATITUDE");
	SchemeLongitude = xmlDoc.getElementsByTagName("LONGITUDE");
	SchemeCity = xmlDoc.getElementsByTagName("CITY");
	SchemeCountry = xmlDoc.getElementsByTagName("COUNTRY");
	SchemeRegion = xmlDoc.getElementsByTagName("REGION");
	SchemeTotal = xmlDoc.getElementsByTagName("DWELLINGS");
	SchemeHouses = xmlDoc.getElementsByTagName("HOUSES");
	SchemeFlats = xmlDoc.getElementsByTagName("FLATS");
	SchemeOneStorey = xmlDoc.getElementsByTagName("ONESTOREY");
	SchemeTwoStorey = xmlDoc.getElementsByTagName("TWOSTOREY");
	SchemeThreeStorey = xmlDoc.getElementsByTagName("THREESTOREY");
	SchemeFourStorey = xmlDoc.getElementsByTagName("FOURSTOREY");
	SchemeFiveStorey = xmlDoc.getElementsByTagName("FIVESTOREY");
	SchemeSixStorey = xmlDoc.getElementsByTagName("MORESTOREYS");
	SchemeArchitect = xmlDoc.getElementsByTagName("ARCHITECT");
	SchemeWaterside = xmlDoc.getElementsByTagName("WATERSIDE");
	SchemeLevelchange = xmlDoc.getElementsByTagName("LEVELCHANGE");
	SchemeRowhouse = xmlDoc.getElementsByTagName("ROWHOUSE");
	SchemeTower = xmlDoc.getElementsByTagName("TOWER");
	SchemeSiteArea = xmlDoc.getElementsByTagName("SITEAREA");
	SchemeDensity = xmlDoc.getElementsByTagName("DENSITY");

	//alert ("arrays loaded");

	var searchTotal = 0;
	var i=1;

	//alert ("Starting search");
var entry = "";

	for (i=1;i<=190;i++)
	{	

		//assumes record will be shown unless proved otherwise
		var isShow = true;


		//CHECK TOTAL NUMBER OF DWELLINGS

		var units = SchemeTotal[i].childNodes[0].nodeValue;
		//alert("The record being searched has " + units + "units, and the max, min is " + UnitsMax + "," + UnitsMin);

		//if there is no total dwellings value
		if (SchemeTotal[i].childNodes[0].nodeValue == "-" && SchemeHouses[i].childNodes[0].nodeValue == "-" && SchemeFlats[i].childNodes[0].nodeValue == "-") {isShow = false;}

		if (document.params.filterbyArea.checked == true && units > UnitsMax || units < UnitsMin) {isShow = false;}

		//CHECK DENSITY

		var theDensity = SchemeDensity[i].childNodes[0].nodeValue;
		//alert("The record being searched has " + density + "dw/ha, and the max, min is " + DensityMax + "," + DensityMin);

		//if there is no density value
		if (document.params.filterbyDensity.checked == true && SchemeDensity[i].childNodes[0].nodeValue == "-")  {isShow = false;}
		if (document.params.filterbyDensity.checked == true && theDensity > DensityMax || theDensity < DensityMin) {isShow = false;}

		//CHECK IF RIGHT NUMBER OF HOUSES AND FLATS

		var houses = SchemeHouses[i].childNodes[0].nodeValue;
		if (houses == null) {houses = 0;}
		if (houses == "-") {houses = "no";}
		var flats = SchemeFlats[i].childNodes[0].nodeValue;
		if (flats == null) {flats = 0;}
		if (flats == "-") {flats = "no";}

		//alert ("No of flats is " + flats + " and number of houses is " + houses);
		

		//alert ("Checked area - isShow is " + isShow);

		if (document.params.Houses.checked == true && flats > 0) {isShow = false;}
		if (document.params.Houses.checked == true && houses == "no") {isShow = false;}
		if (document.params.Flats.checked == true && houses > 0 || flats =="?") {isShow = false;}
		if (document.params.Flats.checked == true && flats == "no") {isShow = false;}

		//CHECK IF SITE AREA MEETS CRITERIA

		var theArea = SchemeSiteArea[i].childNodes[0].nodeValue;

		//if (document.params.filterbySiteArea.checked == true && theArea > areaMax) {isShow = false;}
		//if (document.params.filterbySiteArea.checked == true && theArea < areaMin) {isShow = false;}

		//alert ("theArea is " + theArea + " and isShow is " + isShow);


		//CHECK IF STOREY HEIGHTS MEET CRITERIA

		if (document.params.filterbyHeight.checked == true) {
			var thisOne = 0;
			if (document.params.filterbyHeight.checked == true && document.params.filterOneStorey.checked == true && SchemeOneStorey[i].childNodes[0].nodeValue == "X") {thisOne = 1;}
			if (document.params.filterbyHeight.checked == true && document.params.filterTwoStorey.checked == true && SchemeTwoStorey[i].childNodes[0].nodeValue == "X") {thisOne = 1;}
			if (document.params.filterbyHeight.checked == true && document.params.filterThreeStorey.checked == true && SchemeThreeStorey[i].childNodes[0].nodeValue == "X") {thisOne = 1;}
			if (document.params.filterbyHeight.checked == true && document.params.filterFourStorey.checked == true && SchemeFourStorey[i].childNodes[0].nodeValue == "X") {thisOne = 1;}
			if (document.params.filterbyHeight.checked == true && document.params.filterFiveStorey.checked == true && SchemeFiveStorey[i].childNodes[0].nodeValue == "X") {thisOne = 1;}
			if (document.params.filterbyHeight.checked == true && document.params.filterSixStorey.checked == true && SchemeSixStorey[i].childNodes[0].nodeValue == "X") {thisOne = 1;}
			if (thisOne != 1) {isShow = false;}
		}
			

		//CHECK FOR TAGS - to be added in future versions	

		//if (document.params.filterWaterside.checked == false && SchemeWaterside[i].childNodes[0].nodeValue == "X") {isShow = false}
		//if (document.params.filterLevelChange.checked == false && SchemeLevelchange[i].childNodes[0].nodeValue == "X") {isShow = false}
		//if (document.params.filterRowHouse.checked == false && SchemeRowhouse[i].childNodes[0].nodeValue == "X") {isShow = false}
		//if (document.params.filterTower.checked == false && SchemeTower[i].childNodes[0].nodeValue == "X") {isShow = false}

		if (isShow)
		{
			searchTotal++;

			//SET THE GOOGLE MAPS ZOOM LEVEL TO BE APPROPRIATE FOR SIZE OF PROJECT (if no site area available, estimates using no. of dwellings)

			sensibleZoom = 18;

			if (SchemeSiteArea[i].childNodes[0].nodeValue < 1) {sensibleZoom = 19;}
			if (SchemeSiteArea[i].childNodes[0].nodeValue > 6) {sensibleZoom = 17;}
			if (SchemeSiteArea[i].childNodes[0].nodeValue > 10) {sensibleZoom = 16;}
			if (SchemeSiteArea[i].childNodes[0].nodeValue > 20) {sensibleZoom = 15;}
			if (SchemeSiteArea[i].childNodes[0].nodeValue > 50) {sensibleZoom = 14;}
			if (SchemeSiteArea[i].childNodes[0].nodeValue == "-") {
				if (SchemeTotal[i].childNodes[0].nodeValue < 25) {sensibleZoom = 19;}
				if (SchemeTotal[i].childNodes[0].nodeValue > 100) {sensibleZoom = 17;}
				if (SchemeTotal[i].childNodes[0].nodeValue > 200) {sensibleZoom = 16;}
				if (SchemeTotal[i].childNodes[0].nodeValue > 500) {sensibleZoom = 15;}
				if (SchemeTotal[i].childNodes[0].nodeValue > 1000) {sensibleZoom = 14;}
			}


			//CREATES ENTRY AND PLACES IT IN THE PROJECTLISTPANEL DIV

			entry += "<p class='resultsHeader'><img src='http://www.holisticcity.co.uk/images/stories/placemark.png' border='0' style='position:relative; top:5px; left:0px;'>&nbsp;&nbsp;";
			entry += searchTotal + ".&nbsp;&nbsp;&nbsp;";

			entry += "<span style='color:#000000'>&nbsp;" + SchemeName[i].childNodes[0].nodeValue + ", " + SchemeCity[i].childNodes[0].nodeValue + ", " + SchemeCountry[i].childNodes[0].nodeValue + " - ";
			entry += "&nbsp;<a href='javascript:loadTheMap(" + SchemeLatitude[i].childNodes[0].nodeValue + "," + SchemeLongitude[i].childNodes[0].nodeValue + "," + sensibleZoom + ")' style='color:#006e3a'>";
			entry += "view in map" + "</a><br>";


			
			entry += "<p class='results'>";

			if (SchemeTotal[i].childNodes[0].nodeValue != "-") {entry += "Total Dwellings: " + SchemeTotal[i].childNodes[0].nodeValue;}
			else {entry +="No info on total no. of dwellings";}

			if (houses != "no" || flats != "no") {entry += "&nbsp;(" + houses + " houses, " + flats + " flats)";}

			if (SchemeArchitect[i].childNodes[0].nodeValue != "-") {entry += "<br>Designer: " + SchemeArchitect[i].childNodes[0].nodeValue + "";}

			if (theArea != "-") {entry += "<br>Site Area: " + theArea + " ha";}
			if (theDensity != "-") {entry += "<br>Density: " + theDensity + " dw/ha";}

			//lists included storey-heights

			entry += "<br>Includes: ";
			done = 0;
			if (SchemeOneStorey[i].childNodes[0].nodeValue == "X") {entry += "1 storey buildings";done = 1;}
			if (SchemeTwoStorey[i].childNodes[0].nodeValue == "X") {if (done == 1) {entry += ", ";} entry += "2 storey buildings";done = 1;}
			if (SchemeThreeStorey[i].childNodes[0].nodeValue == "X") {if (done == 1) {entry += ", ";} entry += "3 storey buildings";done = 1;}
			if (SchemeFourStorey[i].childNodes[0].nodeValue == "X") {if (done == 1) {entry += ", ";} entry += "4 storey buildings";done = 1;}
			if (SchemeFiveStorey[i].childNodes[0].nodeValue == "X") {if (done == 1) {entry += ", ";} entry += "5 storey buildings";done = 1;}
			if (SchemeSixStorey[i].childNodes[0].nodeValue == "X") {if (done == 1) {entry += ", ";} entry += "6+ storey buildings";done = 1;}
			if (done == 0) {entry += "no info on storey heights available";}
			

			entry += "<br>Source: <span style='font-size:9pt'>" + SchemeSource[i].childNodes[0].nodeValue + "</span>";
			//adds link to source page if available
			if (SchemeReference[i].childNodes[0].nodeValue != "-") {entry += "&nbsp;-&nbsp;<a href='" + SchemeReference[i].childNodes[0].nodeValue + "' style='color:#006e3a;font-size:9pt'>go to site</a>&nbsp;<br>";}
			
			//document.all.ProjectListPanel.innerHTML += entry + "<br>";

		}

	}

	//document.all.ProjectListPanel.innerHTML += "Search Complete (" + searchTotal + " results)";
	document.all.ProjectListPanel.innerHTML+= entry;
}

function loadTheMap(theLatitude,theLongitude,zoomLevel) 
{

	//loads the Google Map at specified latitude, longitude and at specified zoom level

	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);
}