/** 
 * TerraDirectory JavaScript File. Used for the main page to generate the XML request and change values based on users interaction with the forms. 
 * @author bschiavo - Benjamin Schiavone - 20060523 - 3:24pm
 * @author chartman - Chris Hartman - 14/03/2007 7:52:26 AM Refactor
 * @version $Id: request_generator.js,v 1.3 2007-11-21 23:52:44 sgreen Exp $
 */
 
src="gui_generator.js";

/**
 * Simply changes the Nearest lon value in the XML when the user changes the Nearest lon value in the form.
 * Also updates a hidden variable called PositionNearest
 * which is used for lon and lats which are sent to the server.
 */
function changeLonNear(val) {
	document.directoryForm.longitudeNearest.value = val.value;
	updateRequest();
}

/**
 * Simply changes the Nearest lat value in the XML when the user changes the Nearest lat value in the form.
 * Also updates a hidden variable called PositionNearest
 * which is used for lon and lats which are sent to the server.
 */
function changeLatNear(val) {
	document.directoryForm.latitudeNearest.value = val.value;
	updateRequest();
}

/**
 * Simply changes the WithinDistance lon value in the XML when the user changes the WithinDistance lon value in the form.
 * Also updates a hidden variable called PositionDistance
 * which is used for lon and lats which are sent to the server.
 */
function changeLonDist(val) {
	document.directoryForm.longitudeDistance.value = val.value;
	updateRequest();
}

/**
 * Simply changes the WithinDistance lat value in the XML when the user changes the WithinDistance lat value in the form.
 * Also updates a hidden variable called PositionDistance
 * which is used for lon and lats which are sent to the server.
 */
function changeLatDist(val) {
	document.directoryForm.latitudeDistance.value = val.value;
	updateRequest();
}

/**
 * Updates the PositionDistance value which is basically the lon and lat combined for the WinthinDistance feature.
 */
function updatePositionDistance() {
	document.directoryForm.positionDistance.value = document.directoryForm.longitudeDistance.value + "," + document.directoryForm.latitudeDistance.value
}

/**
 * Updates the PositionNearest value which is basically the lon and lat combined for the Nearest feature.
 */
function updatePositionNearest() {
	document.directoryForm.positionNearest.value = document.directoryForm.longitudeNearest.value + "," + document.directoryForm.latitudeNearest.value
}

/**
 * Updates any fields that need to be set manual including the XML request.
 */
function updateRequest() {

	updatePositionDistance();
	updatePositionNearest();
	
	updateXML();
}

/**
 * When the lon, lat or radius is changed in the form, then the XML is updated in the XML text area.
 * This function displays the XML data.
 */
function updateXML() {

	var maxResp = "";
	if(document.directoryForm.maximumResponses.value > 0) {
		maxResp = "maximumResponses=\"" + document.directoryForm.maximumResponses.value + "\"";
	}

	var poiLoc = "";

	if(document.directoryForm.longitudeNearest.value != "" && document.directoryForm.latitudeNearest.value != "") {
		poiLoc = nearestRequest();
	} else if(document.directoryForm.longitudeDistance.value != "" && document.directoryForm.latitudeDistance.value != ""){
		poiLoc = withinDistRequest();
	} else if(document.directoryForm.polygonPoints.value != "" ){
		poiLoc = withinBoundaryRequest();
	}

	var poiProp = properties();

	document.directoryXMLForm.xls.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 nearest request query
 */
function nearestRequest() {

	var poiLoc = "";
	if(document.directoryForm.longitudeNearest.value != "" && document.directoryForm.latitudeNearest.value != "") {

		poiLoc += "      <POILocation>\n" +
			"        <Nearest>\n" +
			"          <Position>\n" +
			"            <gml:Point>\n" +
			"              <gml:pos>\n" +
			"                " + document.directoryForm.longitudeNearest.value + " " + document.directoryForm.latitudeNearest.value + "\n" +
			"              </gml:pos>\n" +
			"            </gml:Point>\n";

		if(document.directoryForm.longitudeDistance.value != "" && document.directoryForm.latitudeDistance.value != ""
			&&  document.directoryForm.maximumDistance.value != "") {

			poiLoc += "            <gml:CircleByCenterPoint numArc=\"1\">\n" +
				"              <gml:pos>\n" +
				"                " + document.directoryForm.longitudeDistance.value + " " + document.directoryForm.latitudeDistance.value + "\n" +
				"              </gml:pos>\n" +
				"              <gml:radius uom =\"position\">\n" +
				"                " + document.directoryForm.maximumDistance.value + "\n"+
				"              </gml:radius>\n" +
				"            </gml:CircleByCenterPoint>\n";
		}

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

	return poiLoc;
}

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

	var poiLoc = "";
	if(document.directoryForm.longitudeDistance.value != "" && document.directoryForm.latitudeDistance.value != "") {
		poiLoc += "      <POILocation>\n" +
			"        <WithinDistance>\n" +
			"          <Position>\n" +
			"            <gml:Point>\n" +
			"              <gml:pos>\n" +
			"                " + document.directoryForm.longitudeDistance.value + " " + document.directoryForm.latitudeDistance.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 POILocation selection of the withinBoundary request query
 */
function withinBoundaryRequest() {
	var pos = document.directoryForm.polygonPoints.value;
	var start = 0;
	var end = 0;
	
	var poiLoc = "";
	poiLoc += "      <POILocation>\n" +
			"        <WithinBoundary>\n" +
			"          <AOI>\n" +
			"            <gml:Polygon>\n" +
			"              <gml:exterior>\n" +
			"                <gml:LinearRing>\n";
	
	end = pos.indexOf(",");
	while(end > -1) {
		poiLoc += "                  <gml:pos>\n" +
				"                    " + pos.substring(0, end) + "\n" +
				"                  </gml:pos>\n";  
		pos = pos.substring(end + 1);
		end = pos.indexOf(",");
	}
	
	if(pos.length > 0) {
		poiLoc += "                  <gml:pos>\n" +
				"                    " + pos + "\n" +
				"                  </gml:pos>\n";  
	}
	
	poiLoc += "                </gml:LinearRing>\n" +
			"              </gml:exterior>\n" +
			"            </gml:Polygon>\n" +
			"          </AOI>\n" +
			"        </WithinBoundary>\n"+
			"      </POILocation>\n";

	return poiLoc;
}

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

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

	return poiProp;
}

/**
 * Changes the forms transport method to HTTP GET.
 * Calculated number of selected feature codes, aborts submit and 
 * displays alert if more than 45 feature codes are selected
 */
function changeMethodToGet() {
	
	var list = document.getElementById("featureCodes");
	var numSelected = 0;
	for(var i=0; i<list.length; i++) {
		if(list.options[i].selected) {
			numSelected++;
		}
	}
	if (numSelected > 45) {
		alert("You have selected more than 45 Features Codes with GET." + "\n" +
		" The request may exceed the maximum allowed length. " + "\n" +
		" Please either use a POST, or select a Feature Type");
		return;
	}
	document.directoryXMLForm.method = "GET";
	document.directoryXMLForm.submit();
}

/**
 * Changes the forms transport method to HTTP POST.
 */
function changeMethodToPost() {

	document.directoryXMLForm.method = "POST";
}

/**
 * For a HTML, or XML request (Only when the code is pasted into the text area), checks to see if the request is for a PSMA, ALL CODES and a Maximum
 * Distance which is 100000 meters or longer. If this criteria is met, then ask the user if they are sure they want to do this as it can cause an
 * extremely long wait for the application to process the data if there is a lot of Points of Interest selected.
 */
function checkBeforeSubmit() {
	if (document.directoryForm.engine.options[0].selected == true
	&& document.directoryForm.featureCodes.options[0].selected == true
	&& document.directoryForm.maximumDistance.value >= 50000) {
		var okToSubmit = confirm("Are You Sure You Want To Perform This Query On All Codes With A Radius of " + document.directoryForm.maximumDistance.value + "?");
		if (!okToSubmit)
			return false;
	}
	return true;
}

