<script>
//------------------------------------
// ctrlscript.inc
// client side javascript to manage the cutout controls
// Alex Szalay, 2003-03-10
//------------------------------------
	var max_scale = 1200.0;

	//----------------------------------
	// set the scale value directly
	//----------------------------------
	function setscale(p) {
		if (p==0) p = parseFloat(document.getjpeg.scale.value);
		if(isNaN(p)) p=0.396127;
		return setzoombar(p);
	}

	//----------------------------------
	// adjust the zoom value up or down
	//----------------------------------
	function stepzoom(factor) {
		var p = parseFloat(document.getjpeg.scale.value);
		if(isNaN(p)) p=0.396127;
		p *= factor;
		return setzoombar(p);
	}

	//----------------------------------
	// set the zoom value
	//----------------------------------
	function setzoom(zm) {
		return setzoombar(0.396127/Math.pow(2,zm));
	}

	//----------------------------------
	// set the scale value and the zoombar image
	//----------------------------------	
	function setzoombar(p) {
		if (p<0.015) p=0.015;
		if (p>max_scale) p = max_scale;
		document.getjpeg.scale.value = p;
		drawzoombar();
		return resubmit();
	}
	
	//----------------------------------
	// draw the correct zoombar image
	//----------------------------------
	function drawzoombar() {
		var p = document.getjpeg.scale.value;
		var z = Math.round(Math.log(0.396127/p)/Math.log(2))
		if (z> 5) z = 5;
		if (z<-7) z= -7;
		document.zoombar.src = img[5-z];
	}
	
	//----------------------------------
	// set the option string value
	//----------------------------------
	function setopt(t,c) {
		var o = document.getjpeg.opt.value;
		var r = new RegExp(c);
		if (t.checked && o.indexOf(c)==-1) o += c;			
		if (!t.checked) {
			var n = o.indexOf(c);
			if (n>-1) o = o.substring(0,n)+o.substring(n+1);
		}
		document.getjpeg.opt.value = o;
		return resubmit();	
	}
	
	//----------------------------------
	// set the option checkboxes 
	//----------------------------------
	function setoptstr(flag) {
		var v = String(document.getjpeg.opt.value).toUpperCase();
		var p = ["G","L","P","S","T","O","B","C","F",
			"M","Q","I","H","K","U","V","Z","X","Y","W","A","E","R","N","J","D"];
		var f = ["Grid","Label","PhotoObjs","SpecObjs","TargetObjs","Outline",
			"BoundingBox","Frames","Fields","Masks","Plates","InvertImage",
			"Tiles","Chunks","Stripes","Staves","TiGeom","TileBox","SkyBox","Wedges",
			"Sectorlets","Sectors","Runs","Segments","Camcols","Primary"];
		var o = "";
		for (var i=0; i<p.length;i++) {
			var state = (v.indexOf(p[i])>-1);
			if (state) o += p[i];
			document.getjpeg[f[i]].checked=state;
		}

		// hack to accommodate debug flag
		if (v.indexOf("?")>-1)o += "?";		// must not be available in main site DR1

		document.getjpeg.opt.value = o;

		if (flag==1) return resubmit();	
		else return false;

	}
	
	
	//------------------------------------
	// set and validate the ra value
	//------------------------------------
	function setra() {
		var v = parseFloat(document.getjpeg.ra.value);
		if(isNaN(v)) v=180.0;
		if (v<0) v+= 360;
		if (v>360) v-= 360;
		document.getjpeg.ra.value = v;
		return false;
	}
		
	//------------------------------------
	// set and validate the dec value
	//------------------------------------
	function setdec(d) {
		var v = parseFloat(document.getjpeg.dec.value);
		if(isNaN(v)) v=0.0;
		if (v<-90) v = -90;
		if (v>90)  v =  90;
		document.getjpeg.dec.value = v;
		return false;
	}
	
	//------------------------------------
	// set and validate the width
	//------------------------------------
	function setwidth() {
		var v = parseInt(document.getjpeg.width.value);
		if(isNaN(v)) v =  400;
		if (v<64)	 v =   64;
		if (v>2048)  v = 2048;
		document.getjpeg.width.value = v;
		return false;
	}

	//------------------------------------
	// set and validate the width
	//------------------------------------
	function setheight() {
		var v = parseInt(document.getjpeg.height.value);
		if(isNaN(v)) v =  400;
		if (v<64)	 v =   64;
		if (v>2048)  v = 2048;
		document.getjpeg.height.value = v;
		return false;
	}

	//--------------------------------------
	// validate all parameters
	//--------------------------------------
	function validate() {
		if (document.getjpeg.ra) setra();
		if (document.getjpeg.dec) setdec();
		if (document.getjpeg.width) setwidth();
		if (document.getjpeg.height) setheight();
	}
	
	//--------------------------------------
	// declare storage for zoombar images
	//--------------------------------------
	var img = new Object();
		
	//--------------------------------------
	// preload the zoombar images
	//--------------------------------------
	function loadZoombar() {
		var n;
		img.length = 13;
		for (var i = 0; i<13; i++) {
			var fname = ("images/zoombar"+i+".jpg");
			img[i] = fname;
		}
	}

	//--------------------------------------
	// submit the form
	//--------------------------------------	
	function resubmit() {
		validate();
		document.getjpeg.submit();
		return false;
	}

	function fmt(num,total,digits) {
		var n = new Number(num);
		if (n==0) return n;
		var exp = Math.floor(Math.log(Math.abs(n))/Math.LN10);
		var scale = Math.pow(10.,digits);
		if (total>0) 
			scale = Math.pow(10., Math.min(digits,total-exp-1));
		return Math.floor(scale*n+0.5)/scale;
	}

	//---------------------------------
	// point top window to chart
	//---------------------------------
	function gotochart() {
		var s = "chart.asp";
		if (branchname!='list') {
			s+= "?ra="   + fmt(document.getjpeg.ra.value,10,5);
			s+= "&dec="  + fmt(document.getjpeg.dec.value,10,5);
			s+= "&scale="+ fmt(document.getjpeg.scale.value,8,4);
			s+= "&opt="  + document.getjpeg.opt.value;
			s+= "&width=512&height=512";
		}
		top.document.location.href = s;
		return false;
	} 

	//--------------------------------------
	// point top window to the navi
	//--------------------------------------
	function gotonavi() {
		var s = "navi.asp";
		if (branchname!='list') {
			s+= "?ra="   + fmt(document.getjpeg.ra.value,10,5);
			s+= "&dec="  + fmt(document.getjpeg.dec.value,10,5);
			s+= "&opt="  + document.getjpeg.opt.value;
		}
		top.document.location.href = s;
		return false;
	} 

	//--------------------------------------
	// open new window for the Explorer
	//--------------------------------------
	function gotoExp() {
		var s = "../explore/obj.asp";
		if (branchname!='list') {
			s += "?ra="+document.getjpeg.ra.value;
			s += "&dec="+document.getjpeg.dec.value;
		}
		var w = window.open(s,"EXPLORE");
		w.focus();
		return false;
	}


</script>
