/************************************************************************
 * Name: functions.js
 * Purpose: functions to print and handle the form-elements for FINSKEN
 * Version: 1.8, 19.2.2004 
 * Author: Stefan dot Fronzek at ymparisto dot fi
 ************************************************************************/


/*
 * function to print radio-buttons from an array of options; the arguments
 * specify some text to be shown and which function to call when pressing
 * the radio-buttons
 */
function printSelector(
	arrTest, 				// the array with the options
	varSelection,		// the variable which had to be selected to show the options
	strTitle,				// a text used as title/headline for the options
	strSelected,		// a text shown after the selection
	strElementName,	// name of the form element (which is used by the function)
	strFunction,		// the function to call when selecting (usually: .submit)
	strHelpPage) {	// URL to documentation
	// check for "no selection" or for undefined object in case of an array variable
	if(varSelection == "no selection" || !varSelection) {
		document.writeln("<B><font face='Arial, Helvetica' color='red'>"+strTitle+"</FONT></B>");
		document.writeln("<A HREF='" + strHelpPage + "'><FONT size='-1' face='Arial, Helvetica'>Explanation</FONT></A>");
		document.writeln("<IMG src='./img/info.gif'><BR>");
		if(arrTest.length == 0) {
			document.writeln("No options available.");
		} else {
			for(var i=0; i<arrTest.length; i++) {
				document.write("<INPUT type='radio' name='"+strElementName+"' value='"+arrTest[i]+"' onClick='"+strFunction + "'> <FONT face='Arial, Helvetica'>"+arrTest[i]+"</FONT><BR>");
			}
		}
	} else {
		document.writeln("<font face='Arial, Helvetica'>"+strSelected+"<B>"+varSelection+"</FONT></B><BR>");
		//document.writeln("<TABLE><TR><TD valign='top'><FONT face='Arial, Helvetica'>Change selection:</FONT></TD>");
		document.writeln("<TABLE><TR>");
		for(var i=0; i<arrTest.length; i++) {
			document.write("<TD valign='top' bgcolor='#FFFFF0'><INPUT type='radio' name='"+strElementName+"' value='"+arrTest[i]+"' onClick='"+strFunction+"'");
			// check if this option is selected
			if(dynVariables[strElementName] == arrTest[i])
				document.write(" checked ");
			document.writeln("> <FONT face='Arial, Helvetica'>"+arrTest[i]+"</FONT></TD>");
		}
		document.writeln("</TR></TABLE>");
		document.writeln("<A HREF='" + strHelpPage + "'><FONT size='-1' face='Arial, Helvetica'>Explanation</FONT></A>");
		document.writeln("<IMG src='./img/info.gif'><BR>");
	}
}


/*
 * function to print checkbox-buttons from an array of options; multiple
 * selection is possible unlike with the function printSelector above;
 * the arguments specify some text to be shown and which function to call
 * when pressing the radio-buttons
 */
function printSelectorMultiple(
	arrTest, 				// the array with the options
	arrSelection,		// the array with the selected values (empty if no selection)
	strTitle,				// a text used as title/headline for the options
	strSelected,		// a text shown after the selection
	strElementName,	// name of the form element (which is used by the function)
	strFunction, 		// the function to call when selecting (usually: .submit)
	strHelpPage) {	// URL to documentation
	if(arrSelection.length == 0) {
		document.writeln("<B><font face='Arial, Helvetica' color='red'>"+strTitle+"</FONT></B>");
		document.writeln("<A HREF='" + strHelpPage + "'><FONT size='-1' face='Arial, Helvetica'>Explanation</FONT></A>");
		document.writeln("<IMG src='./img/info.gif'><BR>");
		if(arrTest.length == 0) {
			document.writeln("No options available.");
		} else {
			for(var i=0; i<arrTest.length; i++) {
				document.writeln("<INPUT type='checkbox' name='"+strElementName+"' value='"+arrTest[i]+"'> <FONT face='Arial, Helvetica'>"+arrTest[i]+"</FONT><BR>");
			}
			document.write("<A href='javascript:"+ strFunction + "'><FONT face='Arial, Helvetica'>Submit selection</FONT></A><BR>");
		}
	} else {
		document.writeln("<font face='Arial, Helvetica'>"+strSelected+"<B>"+arrSelection.join(" - ")+"</FONT></B><BR>");
		document.writeln("<TABLE><TR><TD><FONT face='Arial, Helvetica'>Change selection:</FONT></TD>");
		for(var i=0; i<arrTest.length; i++) {
			document.write("<TD valign='top' bgcolor='#FFFFF0'><INPUT type='checkbox' name='"+strElementName+"' value='"+arrTest[i]+"'");
			// check if this option is selected
			strTmp = dynVariables[strElementName].join("");
			if((strTmp.indexOf(arrTest[i]) != -1)) {
				document.write(" checked ");
			}
			document.writeln("> <FONT face='Arial, Helvetica'>"+arrTest[i]+"</FONT></TD>");
		}
		document.writeln("</TR></TABLE>");
		document.writeln("<A HREF='javascript:"+strFunction+"'><FONT face='Arial, Helvetica'>Submit selection</FONT></A>");
		document.writeln("(<A HREF='" + strHelpPage + "'><FONT size='-1' face='Arial, Helvetica'>Explanation</FONT></A>");
		document.writeln("<IMG src='./img/info.gif'>)<BR>");
	}
}

/*
 * function to print checkbox-buttons from an array of options; multiple
 * selection is possible only if an option of the array is selected already.
 * The arguments specify some text to be shown and which function to call
 * when pressing the radio-buttons
 */
function printSelectorMult2(
	arrTest, 				// the array with the options
	arrSelection,		// the array with the selected values (empty if no selection)
	strTitle,				// a text used as title/headline for the options
	strSelected,		// a text shown after the selection
	strElementName,	// name of the form element (which is used by the function)
	strFunction, 		// the function to call when selecting (usually: .submit)
	strHelpPage) {	// URL of a help page
	if(arrSelection.length == 0) {
		document.writeln("<B><font face='Arial, Helvetica' color='red'>"+strTitle+"</FONT></B>");
		document.writeln("<A HREF='" + strHelpPage + "'><FONT size='-1' face='Arial, Helvetica'>Explanation</FONT></A>");
		document.writeln("<IMG src='./img/info.gif'><BR>");

		if(arrTest.length == 0) {
			document.writeln("No options available.");
		} else {
			for(var i=0; i<arrTest.length; i++) {
				document.writeln("<INPUT type='checkbox' name='"+strElementName+"' value='"+arrTest[i]+"' onClick='"+strFunction+"'> <FONT face='Arial, Helvetica'>"+arrTest[i]+"</FONT><BR>");
			}
		}
	} else {
		document.writeln("<font face='Arial, Helvetica'>"+strSelected+"<B>"+arrSelection.join(" - ")+"</FONT></B><BR>");
		document.writeln("<TABLE><TR><TD><FONT face='Arial, Helvetica'>Change selection:</FONT></TD>");
		for(var i=0; i<arrTest.length; i++) {
			document.write("<TD valign='top' bgcolor='#FFFFF0'><INPUT type='checkbox' name='"+strElementName+"' value='"+arrTest[i]+"' onClick='"+strFunction+"'");
			// check if this option is selected
			strTmp = dynVariables[strElementName].join("");
			if((strTmp.indexOf(arrTest[i]) != -1)) {
				document.write(" checked ");
			}
			document.writeln("> <FONT face='Arial, Helvetica'>"+arrTest[i]+"</FONT></TD>");
		}
		document.writeln("</TR></TABLE>");
		document.writeln("<A HREF='" + strHelpPage + "'><FONT size='-1' face='Arial, Helvetica'>Explanation</FONT></A>");
		document.writeln("<IMG src='./img/info.gif'><BR>");
	}
}


/* action after choosing the environmental area, deselect
 * time-slice and variable if they have been selected already
 * and submit the form
 */
function submitEnv() {
	// only submit if 'acid deposition', 'socio-economic' or 'climate change'
	// is selected;
	// other sc. types not yet
	// available; when all fields are available, the following if-clause and
	// its according else-clause can be dropped
	//if(document.chooser.env[0].checked || document.chooser.env[2].checked || document.chooser.env[3].checked) {
	//if(!document.chooser.env[4].checked) {	// no sea level scenarios yet
		// climate change scenarios are handled in a different page
		document.chooser.action = "index.html";
		if(document.chooser.env[3].checked) {	// climate change has different page
			document.chooser.action = "index2.html";
		}
		// deselect the options that have been selected after STEP 2: scenario field
		if(document.chooser.arrTimeSel) {
			for(var i=0;i<document.chooser.arrTimeSel.length;i++)
				document.chooser.arrTimeSel[i].checked = false;
		}
		if(document.chooser.variable) {
			for(var i=0;i<document.chooser.variable.length;i++)
				document.chooser.variable[i].checked = false;
		}
	//} else {
		//for(var i=0;i<document.chooser.env.length;i++)
			//document.chooser.env[i].checked = false;
		//alert("At the moment, only the scenarios for ACID DEPOSITIONS and CLIMATE CHANGE are available. The scenario field that you selected will be published in late 2002.");
	//}
	document.chooser.submit();
	return 1;
}

/* action after selecting a sub-scenario:
 * before submitting the request, check if all sub-scenarios of a scenario
 * family have been deselected and deselect the scenario family also if that
 * is the case.
 */
function submitSubScen() {
	for(var i=0; i < document.chooser.arrSpScenSel.length; i++) {
		if(document.chooser.arrSpScenSel[i].checked == false) {
			if(document.chooser.arrSpScenSel[i].value == "A2-MESSAGE")
				document.chooser.arrScenSel[1].checked = false;
			if(document.chooser.arrSpScenSel[i].value == "B2-MESSAGE")
				document.chooser.arrScenSel[3].checked = false;
		}
	}
	document.chooser.submit();
	return 1;
}

// checking the position for array-of-arrays, i.e. the position of the
// scenario field, 0 for "no selection", 1 for "acid deposition", ...
function getArrayNumber() {
	if(env == "no selection") {
		return 0;
	} else {
		for(var n = 0; n < arrEnv.length; n++) {
			if(env == arrEnv[n]) {
				return n;
			}
		}
	}
}

// checking the position for the selected scenario
// this should return -1 for no selection, 0 for A1, 1 for A2, ...
function getScenNumber(strScen) {
	for(var i = 0; i < arrScen.length; i++) {
		if(strScen == arrScen[i]) return i;
	}
	return -1;
}

// opens a window that shows a small text describing a scenario
function showText(intScen) {
	if(document.layers) {	// this should check for Netscape 4.x
		document.layers[intScen].visibility = "show";
		return 1;
	} else {
		if(document.getElementById) {	// for DOM (IE >5 and N6)
			//document.getElementById(intScen).style.display = "block";
			var strText = strA1;
			if(intScen == 1) strText = strA2;
			if(intScen == 2) strText = strB1;
			if(intScen == 3) strText = strB2;
			document.getElementById("scen").childNodes[0].nodeValue = strText;
			document.getElementById("scen").style.visibility = "visible";
		} else {	// older browser versions
			winScenario = window.open("empty.html", "winScenario", "left=0,right=0,width=250,height=300,menubar=no,location=no,resizable=no,scrollbars=yes");
			//winScenario.document.close();
			winScenario.document.open();
			var strText = "Error: wrong argument of the function.";
			if(intScen == 0) strText = strA1;
			if(intScen == 1) strText = strA2;
			if(intScen == 2) strText = strB1;
			if(intScen == 3) strText = strB2;
			winScenario.document.write("<HTML><FONT size='-1'>"+strText+"<BR>[Source: IPCC]</FONT></HTML>");
			winScenario.document.close();
			winScenario.focus();
		}
		return 1;
	}
}

// closes the window opened by showText()
function closeText(intScen) {
	if(document.layers) {	// this should check for Netscape 4.x
		document.layers[intScen].visibility = "hide";
		return 1;
	} else {
		if(document.getElementById) {	// for DOM (IE >5 and N6)
			//document.getElementById("scen").style.visibility = "hidden";
			//document.getElementById(intScen).style.display = "none";
		} else {	// for older browser versions
			if(winScenario)
				winScenario.close();
			return 1;
		}
	}
}

/* function to set the scenario after clicking the scenario-image
 */
function setScenImage(intScenario) {
	//for(i=0;i<document.chooser.fam.length;++i) {
	document.chooser.arrScenSel[intScenario].checked = true;
	document.chooser.submit();
	//parent.scen = document.chooser.scen[intScenario].value;
	return 1;
}

// returns an HTML string that draws a sensitive scatter-plot of the climate
// models
/*
function getScatterPlotHTML() {
	var strHTML = "<MAP name='scatter'>";
	strHTML += "<area shape='circle' coords='225,150,10' href='#end' onClick='submitClModel(\"_h\")' alt='HADCM3 A2'>";
	strHTML += "<area shape='circle' coords='193,140,10' href='#end' onClick='submitClModel(\"\")' alt='CSRIRO A1'>";
	strHTML += "<area shape='circle' coords='206,126,10' href='#end' onClick='submitClModel(\"\")' alt='CSRIRO A2'>";
	strHTML += "</MAP>\n";
	strHTML += "<IMG src='cl/scatter2.gif' usemap='#scatter' border='0'>";
	return strHTML;
}

// submit the form after setting the climate model
function submitClModel(strModel) {
	if(document.chooser.clModel)
		document.chooser.clModel.value = strModel;
	document.chooser.submit();
	return 1;
}
*/

// showTimeSeries
function showTimeSeries(blnOpenAnyway) {
	if(document.chooser.l[document.chooser.l.selectedIndex].text != "--no selection--" || blnOpenAnyway) {
		// open new windows for each scenario fam and variable
		var strWinName = "ts" + variable.substring(0,2) + variable.substring(variable.length-2,variable.length-1);
		var strURL = "showts.html";
		winImage = window.open(strURL, strWinName, "left=0,top=0,menubar=yes,location=no,toolbar=yes,resizable=yes,scrollbars=yes");
		winImage.focus();
	}
	return;
}

// print a selectable list of locations for which a time-series can be shown
function printTimeSeriesList() {
	document.writeln("<select name='l' size='1' onChange='showTimeSeries(false);return false;'>");
	document.writeln("<option>--no selection--</option>");
	document.writeln("<option>Föglö/Degerby</option>");
	document.writeln("<option>Hamina</option>");
	document.writeln("<option>Hanko</option>");
	document.writeln("<option>Helsinki</option>");
	document.writeln("<option>Hämeenlinnä</option>");
	document.writeln("<option>Joensuu</option>");
	document.writeln("<option>Jyväskylä</option>");
	document.writeln("<option>Kaskinen</option>");
	document.writeln("<option>Kemi</option>");
	document.writeln("<option>Kevo</option>");
	document.writeln("<option>Kilpisjärvi</option>");
	document.writeln("<option>Kouvola</option>");
	document.writeln("<option>Kuopio</option>");
	document.writeln("<option>Mariehamn</option>");
	document.writeln("<option>Mikkeli</option>");
	document.writeln("<option>Mäntyluoto</option>");
	document.writeln("<option>Oulu</option>");
	document.writeln("<option>Pietarsaari</option>");
	document.writeln("<option>Raahe</option>");
	document.writeln("<option>Rauma</option>");
  document.writeln("<option>Rovaniemi</option>");
	document.writeln("<option>Turku</option>");
	document.writeln("<option>Vaasa</option>");
	document.writeln("</select>");
	return;
}

// scroll down to the end of the window
function scrollToEnd() {
	var y = 10000;	// number of pixels to scroll down
	               	// high default number if pageYOffset cannot be read
	//if (window.pageYOffset) {	// Netscape
		//y = window.pageYOffset;	// get the real length of document
	//}
	//alert(y + " // " + window.pageYOffset);
	window.scrollBy(0,y);
	return true;
}

/* FUNCTIONS TO CHECK THE BROWSER have been replaced with
	a more simple approach:
	var dom=document.getElementById		-> TRUE if DOM is supported (IE 5+ and Netscape 6+)
	var ns4=document.layers		-> TRUE if Netscape 4.x
 */


