function startup()
{
	isDom = (document.getElementById) ? true : false;
	if (isDom) {
		set_netscape_events();
		map_init();
	} else {
		mapjs('FALSE');
	}
}

function set_netscape_events()
{
	document.onmousedown = windowaction;
	document.onmousemove = movemap;
}

function map_init()
{
	var isMapmaker = document.NextForm.isMapmaker.value;
	if (document.nomap.toggle.value != 1 && isMapmaker == 'TRUE') {
		
		var viewscale = document.NextForm.viewscale.value;
		var mapsize = document.NextForm.mapsize.value;
		var printscale = parseInt(document.NextForm.printscale.value);
		var orientation = document.NextForm.orientation.value;
		
		// image_name["mapbox"].set_src("images/map/mapbox/mapbox.gif");
		
		// Get the upper left screen position of the mapwindow
		var indexmap = image_name["indexmap"];
		var index_left = indexmap.get_left();
		var index_top = indexmap.get_top();
		
		var pixelsize = 0;
		var mapwidth = 0;
		var mapheight = 0;
		var indexWidth = 0;
		
		// Calculate the mapbox offsets
		pixelsize = document.NextForm.pixelsize.value;
		indexWidth = document.NextForm.mapimg_width.value;
		
		var left = indexWidth/2 - parseInt( image_name["mapbox"].get_width()/2 );
		var top =  indexWidth/2 - parseInt( image_name["mapbox"].get_height()/2);

		// Calculate the center coordinates of the mapbox
		var map_x = document.NextForm.xcenter.value;
		var map_y = document.NextForm.ycenter.value;
		var x = document.NextForm.xorigin.value;
		var y = document.NextForm.yorigin.value;
			
		var off_x = parseInt((map_x - x)/pixelsize);
		var off_y = parseInt((map_y - y)/pixelsize);
		
		// Place the map box
		object_name["mapdiv"].place(index_left+left+off_x, index_top+top-off_y);
		object_name["mapdiv"].reveal();
		object_name["mapmove"].place(index_left+left+off_x, index_top+top-off_y);
		object_name["mapmove"].reveal();
		
	}
}

function windowaction(e)
{
	var isMapmaker = document.NextForm.isMapmaker.value;
	
	if (isMapmaker == 'TRUE') {
	
		var mapdiv = object_name["mapdiv"];
		var mapbox = image_name["mapbox"];
		var left = mapdiv.get_left();
		var top = mapdiv.get_top();
		var width = mapbox.get_width();
		var height = mapbox.get_height();
		
		var cen_left = left + width/2 - 25
		var cen_right = left + width/2 + 25
		var cen_top = top + height/2 - 25
		var cen_bottom = top + height/2 + 25
		
		Math.max(document.body.scrollTop,document.documentElement.scrollTop)
		
		if( window.event ) {
			e = window.event;
			scrollX = Math.max(document.body.scrollLeft,document.documentElement.scrollLeft);
			scrollY = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
			clickX = scrollX + e.clientX;
			clickY = scrollY + e.clientY;
			
		} else {
			clickX = e.pageX;
			clickY = e.pageY;
		}
		
		if ((clickX > cen_left) && (clickX < cen_right) && (clickY > cen_top) && (clickY < cen_bottom)) {
			// We're in the main mapwindow, so go to toggle
			toggle(e);
		}
	}
}

var move;
var orient;
function toggle(e)
{
	if (move) {
		move = null;
		object_name["maplock"].conceal();
		object_name["mapmove"].reveal();
	} else {
		if( window.event ) {
			scrollX = Math.max(document.body.scrollLeft,document.documentElement.scrollLeft);
			scrollY = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
			clickX = scrollX + e.clientX;
			clickY = scrollY + e.clientY;
		} else {
			clickX = e.pageX;
			clickY = e.pageY;
		}
		move = 'TRUE';
		object_name["maplock"].reveal();
		object_name["mapmove"].conceal();
	}
}

function movemap(e)
{
  if (move) {
	if (document.nomap.toggle.value != 1) {
		var new_x, new_y;
		
		var indexmap = image_name["indexmap"];
		var index_left = indexmap.get_left();
		var index_top = indexmap.get_top();
		
		if( window.event ) {
			e = window.event;
			scrollX = Math.max(document.body.scrollLeft,document.documentElement.scrollLeft);
			scrollY = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
			clickX = scrollX + e.clientX;
			clickY = scrollY + e.clientY;
		} else {
			clickX = window.scrollX + e.clientX;
			clickY = window.scrollY + e.clientY;
		}
		e.cancelBubble = true;
		
		
		var viewscale = document.NextForm.viewscale.value;
		var mapsize = document.NextForm.mapsize.value;
		var printscale = parseInt(document.NextForm.printscale.value);
		var orientation = document.NextForm.orientation.value;
		
		var pixelsize = 0;
		var mapwidth = 0;
		var mapheight = 0;
		var indexWidth = 0;
		
		// Calculate the mapbox offsets
		pixelsize = document.NextForm.pixelsize.value;
		indexWidth = parseInt(document.NextForm.mapimg_width.value);

		var thewidth = image_name["mapbox"].get_width();
		var theheight = image_name["mapbox"].get_height();
		
		center_x = clickX;
		center_y = clickY;
		new_x = clickX-thewidth/2;
		new_y = clickY-theheight/2;
		if (new_x < index_left) {new_x = index_left;center_x = new_x + thewidth/2}
		if (new_y < index_top) {new_y = index_top;center_y = new_y + theheight/2}
		if (new_x > index_left+indexWidth-thewidth) {new_x = index_left+indexWidth-thewidth;center_x = new_x+thewidth/2}
		if (new_y > index_top+indexWidth-theheight) {new_y = index_top+indexWidth-theheight;center_y = new_y+theheight/2}
		 
		object_name["mapdiv"].place( new_x, new_y );
		object_name["maplock"].place( new_x, new_y );
		object_name["mapmove"].place( new_x, new_y );
		
		// Calculate map x and y from image coordinates
		var x = document.NextForm.xorigin.value;
		var y = document.NextForm.yorigin.value;
		var map_x = parseInt(((center_x - index_left - (indexWidth/2)) * pixelsize)) + parseInt(x); 
		var map_y = y - parseInt(((center_y - index_top - (indexWidth/2)) * pixelsize));
		
		document.NextForm.xcenter.value = map_x; 
		document.NextForm.ycenter.value = map_y;
		document.displayform.xcenter.value = map_x; 
		document.displayform.ycenter.value = map_y;
	}
  }
}

function viewscale(scale,viewtype)
{
	document.displayform.viewscale.value = scale;
	document.displayform.viewsource.value = viewtype;
	document.displayform.submit();
}

function mapjs(on)
{
	if (on == 'TRUE') {
		document.displayform.js.value = 'TRUE';
		document.displayform.submit();
	} else {
		document.displayform.js.value = 'FALSE';
		document.displayform.submit();
	}
}

function openPreview()
{
	var newWin;
    var mapparams = document.PreviewForm.mapparams.value;
	var xcenter = document.NextForm.xcenter.value;
	var ycenter = document.NextForm.ycenter.value;
	var previewUrl = 'map_preview.cfm?x=' + xcenter + '&y=' + ycenter + '&mapparams=' + mapparams;
	newWin = window.open(previewUrl, 'preview', 'width=600,height=600,resizable,scrollbars');
	newWin.focus();
}
function switchView()
{
    var mapparams = document.PreviewForm.mapparams.value;
	var xcenter = document.NextForm.xcenter.value;
	document.PreviewForm.xcenter.value = xcenter;
	var ycenter = document.NextForm.ycenter.value;
	document.PreviewForm.ycenter.value = ycenter;
	document.PreviewForm.submit();
}
function clearOverlay()
{
	document.displayform.clearOverlay.value = 'TRUE';
	document.displayform.submit();
}