/**
 * TerraDirectory JavaScript File. Used for the main page to set defaults based on user interaction with the forms
 * @author jgroffen - James Groffen - 20071106 - 12:21pm
 * @version $Id: catbrowse.js,v 1.10 2009-12-11 05:06:21 mkenihan Exp $
 */

src="catbrowse.js";

var ALL = "ALL";
var AOD_TYPE1 = "aod-type1";
var AOD_TYPE4 = "aod-type4";
var AUSCHOOL = "auschool";
var BUDGET = "budget";
var BP = "bp";
var CALTEX = "caltex";
var CITILAN = "citilan";
var COCONET = "coconet";
var COMMONWEALTH_BANK = "commbank";
var GAZETEER = "gazeteer";
var guang_rest = "guang_rest";
var HPRICE = "hprice";
var HWW = "hww";
var LOAD_TEST = "load-test";
var MEDICARE_DEMO = "medicare-demo";
var MOBIL = "mobil";
var PSMA = "psma";
var RESTAURANT = "restaurant";
var TELSTRA_WIRELESS = "telstra-wireless";

var directories = [AOD_TYPE1, AOD_TYPE4, AUSCHOOL, BUDGET, BP, CALTEX, CITILAN, COCONET, COMMONWEALTH_BANK, 
	GAZETEER, guang_rest, HPRICE, HWW, LOAD_TEST, MEDICARE_DEMO, MOBIL, PSMA, RESTAURANT, TELSTRA_WIRELESS];

/**
 * Initialize the form to use the PSMA engine.
 */
function init(engine) {
	if(engine == "") {
		engine = PSMA;
	}
	for (var i=0; i < document.directoryForm.engine.options.length; i++) {
		if (document.directoryForm.engine.options[i].value.toLowerCase() == engine.toLowerCase()){
			document.directoryForm.engine.options[i].selected = true;
		}
	}
	changeEngine(document.directoryForm.engine);
}

/**
 * When the user selects a different engine, set some more defaults.
 */
function changeEngine(engine) {
	displayFeatureTypeDropdown(engine);
	updateRequest();
}

/**
 * Changes the view to show only the feature categories for the specified engine.
 */
function displayFeatureTypeDropdown(engine) {
	
	// hide all engines
	for(var i = 0; i < directories.length; i++) {
		document.getElementById(directories[i]+"-features").style.display = "none";
	}
	
	// show for the selected engine
	document.getElementById(engine.value+"-features").style.display = "block";
}

function updateRequest() {

	var maxResp = "maximumResponses=\"100\"";

	var poiLoc = withinDistRequest();
	
	document.directoryForm.positionDistance.value = document.directoryForm.longLat.value.replace(" ", ",");

	var poiProp = properties();

	document.directoryForm.xmldoc.value =
		"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
		"<XLS version=\"1.1\" xmlns=\"http://www.opengis.net/xls\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:gml=\"http://www.opengis.net/gml\">\n" +
		"  <RequestHeader clientName=\"\" clientPassword=\"\"/>\n" +
		"  <Request " + maxResp +  " methodName=\"DirectoryRequest\" requestID=\"\" version=\"1.1\">\n" +
		"    <DirectoryRequest>\n" +
		poiLoc +
		"      <POIProperties directoryType=\"" + document.directoryForm.engine.value + "\">\n" + poiProp +
		"      </POIProperties>\n" +
		"    </DirectoryRequest>\n" +
		"  </Request>\n" +
		"</XLS>";
}

/**
 * Returns POILocation selection of the withinDistance request query
 */
function withinDistRequest() {

	var poiLoc = "";
	if(document.directoryForm.longLat.value != "") {
		poiLoc += "      <POILocation>\n" +
			"        <WithinDistance>\n" +
			"          <Position>\n" +
			"            <gml:Point>\n" +
			"              <gml:pos>\n" +
			"                " + document.directoryForm.longLat.value + "\n" +
			"              </gml:pos>\n" +
			"            </gml:Point>\n" +
			"          </Position>\n"

		if(document.directoryForm.maximumDistance.value != "") {
			poiLoc += "          <MaximumDistance uom=\"M\" value=\"" + document.directoryForm.maximumDistance.value + "\"/>\n";
		}

		poiLoc += "        </WithinDistance>\n"+
			"      </POILocation>\n";
	}

	return poiLoc;
}

/**
 * Returns the selected properties of the request query
 */
function properties() {

	var poiProp = "";
	
	var featureCodes = document.getElementById(document.directoryForm.engine.value+"-features");
	
	for (var i=0; i < featureCodes.length; i++) {
		if (featureCodes[i].selected){
			poiProp += "        <POIProperty name=\"SIC_code\" value=\"" + featureCodes[i].value + "\"/>\n";
		}
	}
	if (document.directoryForm.featureNames.value != ""){
			poiProp += "        <POIProperty name=\"POIName\" value=\"" + document.directoryForm.featureNames.value + "\"/>\n";
	}

	return poiProp;
}
