
function getImageMapSrc(mapName, fileName) {
	return getMapServerString(mapName, fileName,'nquery') + "&searchmap=true&XSL_LOCATION="+gDocumentRoot+"/imagemap.xsl&MAP_ID=" + mapName;
}

function getMapServerString(mapName, fileName, mode) {
	var ticktock = new Date();
	var utime = ticktock.getTime();
	var layerOpts = getLayerOptions(mapName);
	return gMapServerRoot+"?moose_seed="+utime+"&map="+fileName+"&mode="+mode+"&mapsize="+imageWidth+"+"+imageHeight+"&mapext="+mapextMinX+"+"+mapextMinY+"+"+mapextMaxX+"+"+mapextMaxY+"&"+layerOpts;
}

/*function getMapServerString(MapFile) {
	return '/gis/mapserver/cgi-bin/mapserv.pl?map='+MapFile+'&mode=map';
}*/


/*
 * MapServerRequest
 * This class does all of the error handling an necessary string parsing to make 
 * a call to map server.
 *	MapServer - String with MapServer URL
 */

function MapServerRequest(MapServer) {

	var attempts = 0;
	var maxAttempts = 3;

	this.mapserver = MapServer;

	// These are some default coordinates, in Ramsey County Coordinates
	// They are probably not useful for most people....
	// The Coords Field should really get set by the program...
	var defaultPtA = new Point(539368,134025);
	var defaultPtB = new Point(604711,222260);
	this.coords = new LineSegment(defaultPtA, defaultPtB);
	this.imageSize = new Point(640,480);
	this.mapfile = '';

	this.LayerParameters = new Array();

	this.setupRequest = function(MapFile) {
		this.mapfile = MapFile;
	}

	this.setupCoordinates = function(PtA, PtB) {
		this.coords = new LineSegment(PtA, PtB);
	}

	this.getMapServer = function() {
		return this.mapfile;
	}

	this.getParameters = function(mode) {
		var seedTime = new Date(); 
		var MapServerString = 'searchmap=1';
		MapServerString += '&mapsize='+this.imageSize.x+'+'+this.imageSize.y;
		MapServerString += '&mapext='+this.coords.minX()+'+'+this.coords.minY()+'+'+this.coords.maxX()+'+'+this.coords.maxY();
		MapServerString += '&mode='+mode;
		MapServerString += '&seed='+seedTime.getTime();
		for(var param in this.LayerParameters) {
			MapServerString += '&'+param+'='+this.LayerParameters[param];
		}
		var dispMapFile = this.mapfile.substring(this.mapfile.lastIndexOf('/')+this.mapfile.lastIndexOf('\\')+2);
		return MapServerString;
	}

	this.buildMapServerString = function (mode) {
		var MapServerString = this.mapfile;
		MapServerString += '?searchmap=1';
		MapServerString += '&mapsize='+this.imageSize.x+'+'+this.imageSize.y;
		MapServerString += '&mapext='+this.coords.minX()+'+'+this.coords.minY()+'+'+this.coords.maxX()+'+'+this.coords.maxY();
		MapServerString += '&mode='+mode;

		for(var param in this.LayerParameters) {
			MapServerString += '&'+param+'='+this.LayerParameters[param];
		}
		var dispMapFile = this.mapfile.substring(this.mapfile.lastIndexOf('/')+this.mapfile.lastIndexOf('\\')+2);
		logDebugMessage('MapServer',false,'Request Type: '+mode+' Mapfile: '+dispMapFile,MapServerString);
		return MapServerString;
	}



	this.onVerify = function () {
		alert('Should be able to load an Image.');
	}

	this.onError = function () {
		if(confirm('Error with loading '+ this.mapfile + '. Press OK to see the error or CANCEL to close this box.')) {
			window.open(this.buildMapServerString('nquery'));
		}
	}

	this.onEmpty = function () {
		alert('MS Msg: Empty Dataset');
	}

	this.verifyImagePrototype = function(imgType) {
		/*
		 * IE does not like Ajax downloading JPEG's.
		 */

		var mySelf = this;
		if(!document.all) {
			var http = getHTTPObject();
			http.open("GET", this.buildMapServerString(imgType));

			http.onreadystatechange = function () { 
				if(http.readyState == 4) {
					if (http.status == 200){
						mySelf.onVerify(http);
					} else {
						mySelf.onError(http);
					}
				}
			};

			http.send(null);
		} else {
			var img = new Image();
			img.src = this.buildMapServerString('map');
			img.onload = function() {
				mySelf.onVerify();			
			}
		}
	}

	this.verifyImage = function() {
		this.verifyImagePrototype('map');
	}

	this.getLegend = function() {
		this.verifyImagePrototype('legend');
	}

	this.resetAttempts = function() {
		attempts = 0;
	}

	this.getAttempts = function() {
		return attempts;
	}

	this.setMaxAttempts = function(n) {
		maxAttempts = n;
	}

	this.canAttempt = function() {
		return(attempts < maxAttempts);
	}

	this.getImageMap = function() {
		var http = getHTTPObject();

		var mySelf = this;
		http.open("POST", this.getMapServer(), true);
		logDebugMessage('Viewer', false, 'Get Image Map', this.getMapServer()+'?'+this.getParameters('nquery'));
		http.onreadystatechange = function () { 
			if(http.readyState == 4) {
				var testString = http.responseText;
				if(testString.match(/MapServer Message/i)) {
					mySelf.onError(http);
				} else {
					mySelf.onVerify(http);
				}
			}
		};

		var parameters = this.getParameters('nquery');
		http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		http.setRequestHeader('Content-length', parameters.length);
		http.setRequestHeader('Connection', 'close');
		attempts++;
		http.send(parameters);
	}

}
