//'Constant' values 
var DEFAULT_MAP = 0;
var LOOKUP_MAP = 1;
var ADDRESS_SEARCH = 2;

querystring.keys = new Array(); 
querystring.values = new Array(); 


function get_map_mode()
{
	//Declare local variables
	var type_param; 
	var search_param; 
	var map_mode;
	
	//Parse the query string
	querystring_parse(); 
	
	//Check to see if a valid 'type' parameter was included
	type_param = querystring("type");
	if(type_param != null && type_param != '')
	{
		//Flag that we are dealing with a lookup request
		map_mode = LOOKUP_MAP;
	}
	else 
	{

		//Check to see if a valid 'searchterms' parameter was included
		search_param = querystring("searchterms");

		if(search_param != null && search_param != '')
		{
			//Flag that we are dealing with a address search request
			map_mode = ADDRESS_SEARCH;
		}
		else 
		{

			//Flag that we are dealing with a general request
			map_mode = DEFAULT_MAP;
		}
		
	}
	
	//Return the flag indicating the type of request 
	return map_mode;

}
function getMapRequest()
{
	//Initalise local variables
	var mapRequest = "";

	
	//Get the map request to use
	mapRequest = document.batch_form.map_request.value;
      //mapRequest = "http://localhost:1819/TileCache/Default.aspx"
	//mapRequest = "http://l72595/gis/cgi-bin/siscgi.exe?"

	//alert('Map Request URL = ' + mapRequest);
	
	//Return the map request to use
	return mapRequest;
}

function getLookupRequest()
{
	//Initalise local variables
	var lookup_request = "";

	
	//Get the map request to use
	lookup_request = document.batch_form.lookup_request.value +"&format=png&LAYERS=swrefVcollectionVdatasetZsias_stateVcollectionZregistry";
	
	
	//Return the map request to use
	return lookup_request;
}

function getLookupResults()
{
	//Initalise local variables
	var lookup_results;
	
	//Get the xml lookup results
	lookup_results = document.batch_form.lookup_results.value;
	
	//Return the map scale to use
	return lookup_results;
	
}

function getMapBounds()
{
	//Initalise local variables
	var xMin;
	var yMin;
	var xMax;	
	var yMax;
	
	//Get the bounds of the map
	xMin = document.batch_form.x_min.value;
	yMin = document.batch_form.y_min.value;
	xMax = document.batch_form.x_max.value;	
	yMax = document.batch_form.y_max.value;	
	
	//Return the map request to use
	return new OpenLayers.Bounds(xMin, yMin, xMax, yMax);
	
}

function getMapCentre()
{
	//Initalise local variables
	var xCentre;
	var yCentre;
	
	//Get the requested map centre point
	xCentre = document.batch_form.x_centre.value;
	yCentre = document.batch_form.y_centre.value;
	
	//Return the map centre point to use
	return new OpenLayers.LonLat(xCentre, yCentre);
	
}

function getMapScale()
{
	//Initalise local variables
	var scale;
	
	//Get the requested map scale
	scale = document.batch_form.scale.value;
	
	//Return the map scale to use
	return scale;
	
}

function useAlphaTransparency()
{
	//Initalise local variables
	var alphaTransparency = false;

	
	//Determine if alpha transparency is to be used	
	alphaTransparency = document.batch_form.alpha_transparency.value;
	
	if(alphaTransparency == "true")
	{
		alphaTransparency = true;
	}
	
	//alert("Returning Alpha Transparency Flag = " + alphaTransparency);
	return alphaTransparency;
}

function get_resolutions()
{
	//var scales = [1100000, 500000, 100000, 25000, 10000, 4000, 2500, 1000];
	var scales = [1100000, 500000, 100000, 25000, 10000, 4000, 2500, 1000];
	var resolutions = new Array();
	var loop_counter; 
	
	//Set up the resoltions to use 
	for(loop_counter = 0; loop_counter < scales.length; loop_counter++)
	{
		resolutions[loop_counter] = OpenLayers.Util.getResolutionFromScale(scales[loop_counter], 'mm');	
	}

	//Return the array of resolutions for the map to use
	return resolutions;
	
}

function querystring_parse() 
{ 
	var query = window.location.search.substring(1); 
	var pairs = query.split("&"); 

	for (var i=0;i<pairs.length;i++) 
	{ 
	
		var pos = pairs[i].indexOf('='); 
		if (pos >= 0) 
		{ 
			var argname = pairs[i].substring(0,pos); 
			var value = pairs[i].substring(pos+1); 
			
			querystring.keys[querystring.keys.length] = argname; 
			querystring.values[querystring.values.length] = value; 
		} 
		
	} 
} 

function querystring(key) 
{ 
	var value = null; 
	
	for (var i=0;i<querystring.keys.length;i++) 
	{ 
		if (querystring.keys[i]==key) 
		{ 
			value = replaceSubstring(querystring.values[i], "+", " "); 
			break; 
		} 
	} 
	
	return value; 
} 

function get_searchterms()
{
	//Declare local variables
	var search_param; 
	
	//Parse the query string
	querystring_parse(); 
	
	//Check to see if a valid 'type' parameter was included
	search_param = querystring("searchterms");
	
	//return the supplied search terms
	return search_param;
}

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   
   if(inputString)
   {
	   if (fromString == "") {
		  return inputString;
	   }
	   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
		  while (temp.indexOf(fromString) != -1) {
			 var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			 var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			 temp = toTheLeft + toString + toTheRight;
		  }
	   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
		  var midStrings = new Array("~", "`", "_", "^", "#");
		  var midStringLen = 1;
		  var midString = "";
		  // Find a string that doesn't exist in the inputString to be used
		  // as an "inbetween" string
		  while (midString == "") {
			 for (var i=0; i < midStrings.length; i++) {
				var tempMidString = "";
				for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
				if (fromString.indexOf(tempMidString) == -1) {
				   midString = tempMidString;
				   i = midStrings.length + 1;
				}
			 }
		  } // Keep on going until we build an "inbetween" string that doesn't exist
		  // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
		  while (temp.indexOf(fromString) != -1) {
			 var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			 var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			 temp = toTheLeft + midString + toTheRight;
		  }
		  // Next, replace the "inbetween" string with the "toString"
		  while (temp.indexOf(midString) != -1) {
			 var toTheLeft = temp.substring(0, temp.indexOf(midString));
			 var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
			 temp = toTheLeft + toString + toTheRight;
		  }
	   } // Ends the check to see if the string being replaced is part of the replacement string or not
	   
	   
   }
   
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

