// Javascript utilities for the mapserv applications
// click the browse 0 or query mode 1. Note assumptions are made about the map template file.
// 1. The mapping form is the first form on the page.
// 2. You have a hidden mode variable.
// 3. Your mapping tools use the standard names.
// 4. imgext and fullext hidden values are set.

function init(){
	setTool(document.mapform.current_tool.value);
}

function setMode(val){
	if(document.mapform.mode){
		document.mapform.mode.value = val;
	}
	return true;
}

var imgdir = "/images/maps/toolbarbuttons";

function setTool(buttonName)
{
	if(document.mapform.java_on && document.mapform.java_on.value == 1){
		return;
	}

	document.mapform.current_tool.value = buttonName;
	//Find which function is requested and set MapServer variables
	window.status="";
	// Not all forms have all the buttons so check each one.
	if (buttonName == "zoomin")
	{
		if(document.zoomin){
			document.zoomin.src= imgdir + "/zoominon.gif";
			window.status="Current tool set to Zoom In";
		}
		if(document.zoomout){document.zoomout.src= imgdir + "/zoomoutoff.gif";}
		if(document.pan){document.pan.src= imgdir + "/panoff.gif";}
		if(document.query){document.query.src= imgdir + "/selectoff.gif";}
		setMode('browse');
		document.mapform.zoomdir.value = 1;
	}	
	else if(buttonName == "zoomout")
	{
		if(document.zoomout){
			document.zoomout.src= imgdir + "/zoomouton.gif";
			window.status="Current tool set to Zoom Out";
		}
		if(document.zoomin){document.zoomin.src= imgdir + "/zoominoff.gif";}
		if(document.pan){document.pan.src= imgdir + "/panoff.gif";}
		if(document.query){document.query.src= imgdir + "/selectoff.gif";}
		setMode('browse');
		document.mapform.zoomdir.value = -1;
	}
	else if(buttonName == "pan")
	{
		if(document.pan){
			document.pan.src= imgdir + "/panon.gif";
			window.status="Current tool set to Pan";
		}
		if(document.zoomin){document.zoomin.src= imgdir + "/zoominoff.gif";}
		if(document.zoomout){document.zoomout.src= imgdir + "/zoomoutoff.gif";}
		if(document.query){document.query.src= imgdir + "/selectoff.gif";}
		setMode('browse');
		document.mapform.zoomdir.value = 0;
	}
	else if(buttonName == "query")
	{
		if(document.query){
			document.query.src= imgdir + "/selecton.gif";
			window.status="Current tool set to Query";
		}
		if(document.zoomin){document.zoomin.src= imgdir + "/zoominoff.gif";}
		if(document.zoomout){document.zoomout.src= imgdir + "/zoomoutoff.gif";}
		if(document.pan){document.pan.src= imgdir + "/panoff.gif";}
		setMode('query');
	}
}

// a fist attempt at checking the map extent and turning off pan when
// the imgext equals the full map ext. Needs more work.
// Perhaps it should be based on the scale which gives us a better overall number. Independent of
// of the fullext.
// This version works with NS4.7, pandir images are hrefs for onClick event.
function checkPan(dir)
{
	document.mapform.pandir.value = dir;
	var imgext = document.mapform.imgext.value;
	if(imgext.split){
		var exts = imgext.split(' ');
		var fexts = document.mapform.fullext.value.split(' ');
		if((exts[2] - exts[0]) >= (fexts[2] - fexts[0])){
			return false;
		}
	}else{
		setTool('pan')
		document.mapform.submit();
		return false;
	}
	setTool('pan')
	document.mapform.submit();
	return false;
}

// Used by emolt_map.html.  When a datatype is selected, turn
// on the layername and submit the form.

function setLayer(sellist, layername)
{
	// select list 0 is just a message.
	if(sellist.selectedIndex == 0) { return true;}

	for(i = 0; i < document.mapform.layers.length; i++){
		var val = document.mapform.layers[i].value;
		if(val == layername){
			document.mapform.layers[i].checked = true;
			document.mapform.submit();
			return false;
		}
	}	
}

// Both of these are because toolbar buttons cannot be <input type="image">
// under some browser (if I remeber correctly). The image name does not get
// submitted.
function orig_ext()
{
	document.mapform.origext.value = "1";
	document.mapform.submit();
	return false;
}

function data_ext()
{
	document.mapform.dataext.value = "1";
	document.mapform.submit();
	return false;
}

function gmbis_ext(type)
{
	if(type == 1){
		document.mapform.dataext1.value = "1";
	}else{
		document.mapform.dataext2.value = "1";
	}
	document.mapform.submit();
	return false;
}

function rollOver(dir, stat)
{
	return true;
}


