<!-- // javascript functions that storm*.pl call
// All frame-dependencies go here.
// frames[0,1,2] == "stormTop", "stormMid", "stormBot" (set in storm.pl)
//
// Nothing in here or the storm*.pl frames should directly go to another page
// since the back button will then suck.  Use popUpImage() instead. setInst()
// cheats by using .replace()

var posx = 0;
var posy = 0;
var width = 0;
var height = 0;		// in IE (& others?), this is the frame's height
function capmouse(e){		// captures the mouse position
    if (document.addEventListener) {	// everyone but IE?
	posx = e.pageX;
	posy = e.pageY;
	width  = window.innerWidth +window.pageXOffset;
	height = window.innerHeight+window.pageYOffset;
    } else if (document.documentElement && document.documentElement.clientHeight) {		// IE7
	posx = window.event.x+document.documentElement.scrollLeft;
	posy = window.event.y+document.documentElement.scrollTop;
	width  = document.documentElement.clientWidth
	    +document.documentElement.scrollLeft;
	height = document.documentElement.clientHeight
	    +document.documentElement.scrollTop;
    } else if (document.all) {		// IE6
	posx = window.event.x+document.body.scrollLeft;
	posy = window.event.y+document.body.scrollTop;
	width  = document.body.clientWidth
	    +document.body.scrollLeft;
	height = document.body.clientHeight
	    +document.body.scrollTop;
    }
}
var timeoutKey;	//safari often leaves infoBox up, esp when changing frames. Kill
var clearKey=0;	//not really needed, but reduces number of outstanding timeouts
function helpClose2(localKey) {
    if (localKey == timeoutKey)	helpClose(0);	// false if another popup went
}
function helpClose(e) {
    clearTimeout(clearKey);
    document.getElementById("infoBox").style.visibility = "hidden";
}
function help(text, i_width) {
    if (document.getElementById) {
	var infobox = document.getElementById("infoBox");
//	var infobox = parent.stormTop.document.getElementById("infoBox");
	var timeoutMsecs = 5000;
	if (text.length > 300) timeoutMsecs = 15000;
	if (i_width == "") i_width = 100;
	infobox.style.width= i_width + "px";
	infobox.style.left
	    = (posx+i_width <= width ? posx+7 : posx-i_width-2) + "px";
	if (height<50) infobox.style.top = "3px";
	else infobox.style.top = posy - 5 + "px";
	infobox.style.visibility = "visible";
	infobox.innerHTML = text;
	timeoutKey = posx + "_" + posy;
	clearKey = setTimeout("helpClose2('" + timeoutKey + "')", timeoutMsecs);
    }
}
if (document.addEventListener) {		// everything but IE?
    document.addEventListener("mouseover", capmouse, true);	// was mousemove
    document.addEventListener("mouseout", helpClose, true);
} else if (document.all) {		// IE
    document.onmouseover = capmouse;	// was onmousemove, but that was slow
    document.onmouseout = helpClose;
} else confirm("Your browser is not supported. Many functions will fail");


// storm.pl runs out of vertical space with low res displays
function toStormPL(qstring) {	// scrollbars can't reach storm.pl's stormBot
    var url = "/cgi-bin/hurr/storm.pl";
    if (qstring != "") url += "?" + qstring;
    if (screen.height <= 600) window.open(url, "lowResWin", "width="
        +screen.width +",height=" +screen.height +",scrollbars");
    else parent.window.location = url;
}


// I want to name the target -> all popups go there, but Safari keeps 1st image
var popup = null;
function popUpImage(image) {	// currently, only Instrument.pm uses this
    popup = window.open(image, "", "width=400,height=400,resizable,scrollbars");
    if (popup.focus) {		// not all browsers have this method?
	setTimeout("popup.focus();", 100);
    }
}

// Only stormTop.pl uses the following, so no "parent.frames[0].document..."

function showIt(dot){			// parent.frames[0].document unneeded
    if (document.getElementById)
	{document.getElementById(dot).style.visibility = "visible";}
}
function hideIt(dot){			// parent.frames[0].document unneeded
    if (document.getElementById)
	{document.getElementById(dot).style.visibility = "hidden"}
}
function toggle(buttonName, dotsString) { // parent.frames[0].document unneeded
    var dots = new Array();
    dots = dotsString.split(',');	// sometimes dangling ,
    if (document.getElementById) {
	if (document.getElementById(buttonName).value == "Show") {
	    for(i=0; i < dots.length; i++) { if (dots[i]) showIt(dots[i]); }
	    document.getElementById(buttonName).value = "Hide";
	} else {
	    for(i=0; i < dots.length; i++) { if (dots[i]) hideIt(dots[i]); }
	    document.getElementById(buttonName).value = "Show";
	}
    }
}



//These functions handle downloading data, which is split between frames 0&2.
// The perl code defines checkboxes in frame2 with name and id (e.g. QS1_0_1)
// <inst><hit>_<col#>_<item#WithinCell>.  <hit> is NOT 2 digits since no printf
var insts = new Array;	// insts[0,1] == "QS","GI" (or reversed)
var cols = new Array;	// array of data types for inst[0], inst[1]...
var rows = new Array;	// # of hits. Note <hit> begins with 1
var curInst = -1;	// setInst() sets this. jsInit() sets the previous
var curFlyby = "";	// e.g. QS7. Becomes the part of the URL after #
var SELHITS = "selectInstHits";		// sync with stormTop.pl
var SELTYPS = "selectInstTypes";	// sync with stormTop.pl

// for each call, set insts[n], rows[n], cols[n]
// P1==<two-char inst>, P2==hits, P3==<comma-separated columns for inst>
function jsInit(inst,hits,columns) {
    var i;
    for (i = 0; i < insts.length; i++) {
	if (inst == insts[i]) {
	    alert("programming error: jsInit(" + inst + ") duplicated");
	    return;
	}
    }
    insts[i] = inst;
    rows[i] = hits;
    cols[i] = columns.split(",");
}

// When user selects an instrument, set frame0's two choosers, frame1's
// table, frame2's table IF...
// if inst != currentInst, do all three above. inst determines the 2 frames.
// elsif flyby != currentFlyby, only set frame2's table
function setInst(hurr, id, date0, dateN, inst, flyby) {	// must set frame0's
    var i;
    var newInst = -1;
    if (inst == '') {
	if (insts.length > 0) newInst = 0;	// just take the first one
	else return;
    } else {
	for (i = 0; i < insts.length; i++) {
	    if (inst == insts[i]) { newInst = i; break; }
	}
    }
    if (newInst < 0) { alert('unrecognized instrument ' + inst); return }
    var newMid = "/cgi-bin/hurr/stormMid.pl?inst=" + inst;
    var newBot = "/cgi-bin/hurr/stormBot.pl?hurr=" + hurr + "&id=" + id
	+ "&date0=" + date0 + "&dateN=" + dateN + "&inst=" + inst;
    if (flyby) newBot += "#" + flyby;
    if (newInst != curInst) {
	//Was parent.stormMid.document.location = newMid, but back button sucked
	//These worked for Opera 9.2, though some web pages had said it wouldn't
	parent.stormMid.location.replace(newMid);
	parent.stormBot.location.replace(newBot);
    } else if (curFlyby != flyby) {		// ne instead of != ???
	parent.stormBot.location.replace(newBot);
    } else return;
    curFlyby = flyby;
    curInst = newInst;
    if (document.getElementById) {	// run even if stormBot hasn't changed
	var selRow = parent.stormTop.document.getElementById(SELHITS);
	var selCol = parent.stormTop.document.getElementById(SELTYPS);
	selRow.options.length = 0;	// rows[curInst];
	var j;	// .length = 0 & setting .options[] backwards sucks on mozilla
	for (j = 0; j < rows[curInst]; j++) {
	    var text = insts[curInst];	// no sprintf in javascript
	    var dotNum = rows[curInst] - j;
	    if (dotNum < 10) text += "0";
	    text += dotNum;
	    selRow.options[j] = new Option(text, dotNum, 0,selected);
	}
	selCol.options.length = 0;
	for (j = 0; j < cols[curInst].length; j++) {
	    var text = cols[curInst][j];
	    var selected = 0;
	    //if (j < 3) selected=1;	// show multiple select
	    selCol.options[j] = new Option(text, j, 0, selected);
	}
	// stormBot's checkboxes may not be ready when stormTop calls setInst.
//	attempts = 0;	// Also, stormBot can't call setMany since it gets its
//	delayedSetMany();	// own image of this file.  So use this hack.
    }
} // setInst()

var attempts;
function delayedSetMany() {
    var dataID = insts[curInst] + "1_0_0";
    var colHeadID = insts[curInst] + "_0";
    if (parent.stormMid.document.getElementById(colHeadID) &&
	parent.stormBot.document.getElementById(dataID)) setMany();
    else if (++attempts < 3) setTimeout(delayedSetMany, 1000);
}

var ONCOLOR = "#bbffbb";		// was "#316ac5";
var OFFCOLOR = "#ffffee";
function setMany() {	// set the intersecion of frame2's checkboxes
    if (document.getElementById) {
	var selRow = parent.stormTop.document.getElementById(SELHITS);
	var selCol = parent.stormTop.document.getElementById(SELTYPS);
	var i, j;
	var inst = insts[curInst];
	var colMax = cols[curInst].length;
	var rowMax = rows[curInst];
	for (j = 0; j < colMax; j++) {
	    var colOn = selCol.options[j].selected;
	    var colID = "_" + selCol.options[j].value;
	    var colHeadID = inst + colID;
	    var colHead = parent.stormMid.document.getElementById(colHeadID);
	    colHead.style.backgroundColor = colOn ? ONCOLOR : OFFCOLOR;
	    //colHead.style.color = colOn ? "#ffffff" : "#000000";
	    for (i = 0; i < selRow.options.length; i++) {
		var rowOn = selRow.options[i].selected;
		var rowHeadID = inst + selRow.options[i].value;
		var rowHead =
		    parent.stormBot.document.getElementById(rowHeadID + "icon");
	        rowHead.style.backgroundColor = rowOn ? ONCOLOR : OFFCOLOR;
		var cellOn = rowOn && colOn;
		var cellID = rowHeadID + colID;
		var cell = parent.stormBot.document.getElementById(cellID);
		cell.style.backgroundColor = cellOn ? ONCOLOR : OFFCOLOR;
		for (dataNum = 0; 1; dataNum++) {
		    var dataID = cellID + "_" + dataNum;
		    var cbox = parent.stormBot.document.getElementById(dataID);
		    if (!cbox) break;
		    cbox.checked = cellOn;
		}
	    }
	}
    }
} // setHits()


function getData() {	// read checkboxes in frame 2, call /cgi-bin/sendTar.pl
    if (curInst < 0) { alert("Select instrument first"); return; }
    var hitNum,colNum,dataNum;	// colnum is the outside loop for more dir matches
    var uri = "";
    var dir = 0;		// shortens the URI
    var ls = 0;			// shortens the URI
    var oldDir = "";		// shortens the URI;
    for (colNum = 0; colNum <= cols[curInst].length; colNum++) {	// spin slower
	for (hitNum = 1; hitNum <= rows[curInst]; hitNum++) {
	    var hitID = insts[curInst] + hitNum;
	    if (!parent.stormBot.document.getElementById(hitID)) continue;
	    for (dataNum = 0; 1; dataNum++) {
		var dataID = hitID + "_" + colNum + "_" + dataNum;
		var checkbox = parent.stormBot.document.getElementById(dataID);
		if (!checkbox) break;	//varying # of cboxes per cell; no more now
		if (checkbox.checked) {
		    var fullpath = checkbox.value.match(/^(.*)\/([^\/]+)$/);
		    if (fullpath[1] != oldDir) {
			oldDir = fullpath[1];
			if (uri) uri += "&";
			uri += "dir"+dir + "=" + oldDir + "&ls"+ls + "=" + fullpath[2];
			dir++;
			ls++;
		    } else { uri += "," + fullpath[2]; }
		}
	    }
	}
    }
    // http://answers.yahoo.com/question/index?qid=20060711063622AAD079d -> 2083
    if (!uri) alert("No files chosen (in frame below) to download");
    else if (uri.length > 2080)
      alert("Too much to download. Please reduce the number of files selected");
    else parent.window.location="/cgi-bin/sendTar.pl?" + uri;
}
//-->
