function $(id) {
	return(document.getElementById(id));
}
function open_win(URL, w, h, handle) {
	newWin = window.open(URL, handle, 'width='+w+',height='+h+',menubar=0,scrollbars=1,resizable=1,location=0,status=0');
	newWin.focus();
}
function VarItem(shortVar, longVar, selected, average, minimum, maximum, total, units) {
	this.shortVar = shortVar;
	this.longVar = longVar;
	this.selected = selected;
	this.average = average;
	this.minimum = minimum;
	this.maximum = maximum;
	this.total = total;
	this.units = units;
	return(this);
}
function PlotItem(nVars, vars, title, laxis_label, raxis_label, major_ticks, minor_ticks) {
	this.nVars = nVars;
	this.vars = vars;
	this.title = title;
	this.laxis_label = laxis_label;
	this.raxis_label = raxis_label;
	this.major_ticks = major_ticks;
	this.minor_ticks = minor_ticks;
	return(this);
}
function PlotVar(varID, summary, plottype, linetype, color, yaxis) {
	this.varID = varID;
	this.summary = summary;
	this.plottype = plottype;
	this.linetype = linetype;
	this.color = color;
	this.yaxis = yaxis;
	return(this);
}

//var AddVarText = '<?php echo $addVarText; ?>';
//var EditVarText = '<?php #echo $editVarText; ?>';
var DateTimeHelp = 'Date/Time Selection: <p>1. Select an overall date range. No data will be used outside of this range.</p><p>2. Select a within-year range to select all or a portion of each year. For all data in a year, select start = Jan 1 and end = Dec 31. You can select the same day for start and end to give you data from one day per year.</p><p>3. Select a within-day time range to select all or a portion of each day. For all data in a day, select start = 100 and end = 2400. You can select the same hour for start and end to give you data from one hour per day.</p>';
var TimestepHelp = 'Timestep Selection: <p>Raw data are collected every hour, every three hours, and daily.</p><p>Select the hour timestep to retrieve data at their original timestep with no summarization.</p><p>If you want data summarized by day, month, or year, select one of those timesteps. All raw data for a unique day/month/year, month/year, or year, respectively, will be summarized.</p><p>You can also have data summarized by day or by month across years. In this case, all raw data for a unique day/month or month, respectively, will be summarized.</p>';
var VarSelectHelp = 'Variable Selection: <p>To select variables of interest, click the + button in this section of the form to view a list of all variables available. Click on one or more variables to add them to your variable selection table. You can select multiple variable at one time or you can return to this selection list at any time.</p><p>When you have at least one variable selected, click on the variable name to view the list of available summary statistics. Select one or more to add them to the variable selection table. If you selected an hour timestep, then any summary statistic will simply return the raw data so just select Average. For all other timesteps, select Average, Minimum, Maximum, and/or Total</p><p>To remove a summary statistic from the list, click on the adjacent red X. To remove the entire variable and all summary statistics selected, click on the red X preceding the variable name.</p>';
var PlotSetupHelp = 'Plot Setup: <p>With at least one variable and summary statistic selected, click the Plot Setup \'Add\' button to define a graphic. Define general plot parameters such as title, left and right axis labels, and timesteps for the major and minor ticks on the x-axis. Select the tick intervals based on the range of your data and the summary timestep selected.</p><p>Select the variables you want to plot from the pull down box. Each time you select one, you\'ll get additional options to define its plot type (line, bar, histogram), line type (solid, dashed, dotted), color, and whether to add it to the left or right y-axis.</p><p>Histograms cannot be combined with any other plot type. You can define multiple histograms on the same plot, but they will be plotted separately. If all variables in a single plot are set to barplot, you will get a single multiple bar plot with multiple bars at each timestep. Multiple lines can also be drawn on a single line plot. A single plot can have one variable set as barplot and any number of variables drawn as lines. You cannot have more than one barplot variable combined with lines.</p><p>You can add more plots by clicking on the Plot Setup \'Add\' button. Each plot is listed by its title in the Plot Setup area. You can click on the linked plot number to display the options for a given plot.</p>';
var OutputHelp = 'Output: <p>Select a file format for downloaded data. Available text or ascii file formats are comma or tab delimited. A simple Excel format with a single worksheet is also available. Please note that Excel files are limited to 65,536 rows. If you request an Excel formatted file and your query results in more records than that, you will receive your output as a comma delimited file.</p><p>Select a file format for downloaded graphics. Standard png, tif, jpg, pdf, ps, and wmf formats are available. Specify an image size by entering the desired image width and height in inches. Raster images (png, jpg, tif), are created with 100 pixels per inch. If you want an 800px wide by 600px high image, enter 8 and 6 for the height and width, respectively. Multiple images from one run will all be the same size.';
var RecentQueriesHelp = 'Recent Queries: <p>Input and output files for each query are saved in a directory unique to each user\'s computer. Files for the eight most recent queries are available and can be seen by clicking the "Recent Queries" button. Click the button again to hide the listing.</p><p>Each query is listed by the date and time it was run. Clicking on the query opens a popup window that lists the contents of its directory</p><p><b>.RData</b> is the R workspace from the query.</p><p><b>Output data files</b> are listed in whatever format was selected.</p><p><b>Output graphics</b> are listed in whatever format was selected.</p><p><b>R_in.txt</b> is the input command file handed to R to perform the query and contains the arguments set by the user.</p><p><b>R_out.txt</b> is the output file from R that contains the input commands and any messages from R.</p><p>You can reload a previous query by clicking its "Load" button. This will set all date/time, variable, and plot options to what was set before.</p>';

var ActiveVarID = 0;
var nextPlotID = 0;
var ActivePlotID = 0;
//var PlotItems = Array();

function divTurnOn(onDiv) {
	divs = new Array('addvar', 'addsumm', 'addplot', 'helpbox', 'rightimage');
	for(d=0; d<divs.length; d++) {
		if(divs[d] == onDiv) $(onDiv).style.display = 'block';
		else $(divs[d]).style.display = 'none';
	}
}
function DisplayTools(type, mode, varid, section) {
	if(type == 'AddVar') {
		if(mode == 'show') {
			divTurnOn('addvar');
			$('fields').selectedIndex = -1;
		}
		else {
			divTurnOn('rightimage');
		}
	}
	else if(type == 'AddSumm') {
		if(mode == 'show') {
			divTurnOn('addsumm');
			$('activeID').innerHTML = VarItems[varid]['longVar'];
			$('summaries').selectedIndex = -1;
		}
		else {
			divTurnOn('rightimage');
		}
	}
	else if(type == 'AddPlot') {
		if(mode == 'show') {
			$('addplotSelect').innerHTML = makeAddPlotSelect(nextPlotID++);
			divTurnOn('addplot');
		}
		else {
			divTurnOn('rightimage');
		}
	}
	else if(type == 'Help') {
		if(mode == 'show') {
			if(section=='DateTime') $('helpcontent').innerHTML = DateTimeHelp;
			else if(section=='Timestep') $('helpcontent').innerHTML = TimestepHelp;
			else if(section=='VarSelect') $('helpcontent').innerHTML = VarSelectHelp;
			else if(section=='PlotSetup') $('helpcontent').innerHTML = PlotSetupHelp;
			else if(section=='Output') $('helpcontent').innerHTML = OutputHelp;
			else if(section=='RecentQueries') $('helpcontent').innerHTML = RecentQueriesHelp;
			divTurnOn('helpbox');
		}
		else {
			divTurnOn('rightimage');
		}
	}
}
function updateAddVar(newVarID, force) {
	if(VarItems[newVarID]['selected'] && !force) return;
	VarItems[newVarID]['selected'] = true;
	avBox = $('addVarBox');
	if(avBox.innerHTML == '') thisclass = 'vardiv1';
	else thisclass = 'vardiv';
	avBox.innerHTML += '<div id="var' + newVarID + '" class="' + thisclass + '"></div>';
	vBox = $('var' + newVarID);
	vBox.innerHTML = makeVarDisplay(newVarID);
}
function updateDeleteVar(delVarID) {
	yn = confirm('Are you sure you want to remove ' + VarItems[delVarID]['longVar'] + ' from your selection?');
	if(yn == 1) {
		vBox = $('var' + delVarID);
		vBox.parentNode.removeChild(vBox);
		VarItems[delVarID]['selected'] = false;
		VarItems[delVarID]['average'] = false;
		VarItems[delVarID]['minimum'] = false;
		VarItems[delVarID]['maximum'] = false;
		VarItems[delVarID]['total'] = false;
	}
}
function updateAddSumm(addSumm) {
	VarItems[ActiveVarID][addSumm] = true;
	vBox = $('var' + ActiveVarID);
	vBox.innerHTML = makeVarDisplay(ActiveVarID);
}
function addSummaries(VarID) {
	ActiveVarID = VarID;
	DisplayTools("AddSumm", "show", VarID)
}
function updateDelSumm(VarID, summ) {
	yn = confirm('Are you sure you want to remove ' + summ + ' ' + VarItems[VarID]['longVar'] + ' from your selection?');
	if(yn == 1) {
		VarItems[VarID][summ] = false;
		vBox = $('var' + VarID);
		vBox.innerHTML = makeVarDisplay(VarID);
	}
}
function makeVarDisplay(VarID) {
	if(!VarItems[VarID]['selected']) return('');
	summs = Array();
	n = 0;
	if(VarItems[VarID]['average']) { summs[n] = 'average'; n++; }
	if(VarItems[VarID]['minimum']) { summs[n] = 'minimum'; n++; }
	if(VarItems[VarID]['maximum']) { summs[n] = 'maximum'; n++; }
	if(VarItems[VarID]['total']) { summs[n] = 'total'; n++; }
	
	html = '<table cellspacing="0" cellpadding="0" border="0"><tr>';
	html += '<td><img src="images/delete12.png" onclick="updateDeleteVar(' + VarID + ')" onmouseover="this.src=\'images/delete12_on.png\'" onmouseout="this.src=\'images/delete12.png\'" align="top" /></td>';
	html += '<td width="130px"><a href="javascript:addSummaries(' + VarID + ')">' + VarItems[VarID]['longVar'] + '</a></td>';
	
	if(n > 0) {
		for(i=0; i<n; i++) {
			if(i > 0) html += '<tr><td></td><td></td>';
			html += '<td><img src="images/delete12.png" onclick="updateDelSumm(' + VarID + ', \'' + summs[i] + '\')" onmouseover="this.src=\'images/delete12_on.png\'" onmouseout="this.src=\'images/delete12.png\'" align="top" /></td>';
			if(i > 0) html += '<td class="summ" width="66px">' + summs[i] + '</td>';
			else html += '<td width="66px">' + summs[i] + '</td>';
		//	if(i > 0) html += '<td class="summ">'
		//	else html += '<td>';
		//	html += '<select id="plot_type' + VarID + '"><option value="none">No plot</option><option value="line">Line plot</option><option value="bar">Bar plot</option><option value="hist">Histogram</option></select>';
		//	html += '</td>';
			
		}
	}
//	else {
		html += '</tr>';
//	}
	html += '</table>';
	return(html);
}
function makeAddPlotSelect(plotID) {
	options = '';
	for(i=0; i<VarItems.length; i++) {
		if(VarItems[i]['selected']) {
			if(VarItems[i]['average']) options += '<option value="' + i + '-ave">Average ' + VarItems[i]['longVar'] + '</option>';
			if(VarItems[i]['minimum']) options += '<option value="' + i + '-min">Minimum ' + VarItems[i]['longVar'] + '</option>';
			if(VarItems[i]['maximum']) options += '<option value="' + i + '-max">Maximum ' + VarItems[i]['longVar'] + '</option>';
			if(VarItems[i]['total']) options += '<option value="' + i + '-tot">Total ' + VarItems[i]['longVar'] + '</option>';
		}
	}
	if(options == '') return('Please select one or more variables with data summary before defining a plot.');
	
	if(!PlotItems[plotID]) {
		PlotItems[plotID] = new PlotItem(0, Array(), 'Plot ' + (plotID+1), 'Left Axis Label', 'Right Axis Label', 'None', 'None');
		updateAddPlot(plotID);
		$('addplotList').innerHTML = '';
	}
	majtick = PlotItems[plotID]['major_ticks'];
	mintick = PlotItems[plotID]['minor_ticks'];
	html = '<p><b>Plot Title:</b> <input type="text" style="width: 160px;" value="' + PlotItems[plotID]['title'] + '" onchange="updatePlotDef(' + plotID + ', null, \'title\', this.value)" /><br />';
	html += '<b>L. Axis Label:</b> <input type="text" style="width: 140px;" value="' + PlotItems[plotID]['laxis_label'] + '" onchange="updatePlotDef(' + plotID + ', null, \'laxis_label\', this.value)" /><br />';
	html += '<b>R. Axis Label:</b> <input type="text" style="width: 140px;" value="' + PlotItems[plotID]['raxis_label'] + '" onchange="updatePlotDef(' + plotID + ', null, \'raxis_label\', this.value)" /><br />';
	html += '<b>X Axis (Time) Major Ticks:</b> <select onchange="updatePlotDef(' + plotID + ', null, \'major_ticks\', this.value)" />';
	html += '<option value="none"' + ((majtick=='none')?' selected':'') + '>None</option>';
	html += '<option value="year"' + ((majtick=='year')?' selected':'') + '>Year</option>';
	html += '<option value="month"' + ((majtick=='month')?' selected':'') + '>Month</option>';
	html += '<option value="day"' + ((majtick=='day')?' selected':'') + '>Day</option>';
	html += '<option value="hour"' + ((majtick=='hour')?' selected':'') + '>Hour</option>';
	html += '</select><br />';
	html += '<b>X Axis (Time) Minor Ticks:</b> <select onchange="updatePlotDef(' + plotID + ', null, \'minor_ticks\', this.value)" />';
	html += '<option value="none"' + ((mintick=='none')?' selected':'') + '>None</option>';
	html += '<option value="year"' + ((mintick=='year')?' selected':'') + '>Year</option>';
	html += '<option value="month"' + ((mintick=='month')?' selected':'') + '>Month</option>';
	html += '<option value="day"' + ((mintick=='day')?' selected':'') + '>Day</option>';
	html += '<option value="hour"' + ((mintick=='hour')?' selected':'') + '>Hour</option>';
	html += '</select></p>';
	html += '<p><select onchange="addPlotVar(' + plotID + ', this.value)" style="width: 220px;"><option value="-">Select variables to plot...</option>' + options + '</select></p>';
	
	return(html);
}
function reloadPlot(plotID) {
	$('addplotSelect').innerHTML = makeAddPlotSelect(plotID);
	$('addplotList').innerHTML = '';
	for(p=0; p<PlotItems[plotID]['nVars']; p++) {
		if(PlotItems[plotID]['vars'][p] != null) makePlotVarList(plotID, p);
	}
	divTurnOn('addplot');
}
function updatePlotDef(plotID, plotvarID, attrib, value) {
	if(attrib == 'title') {
		PlotItems[plotID]['title'] = value;
		$('plot' + plotID).innerHTML = makePlotDisplay(plotID);
	}
	else if(attrib == 'laxis_label') {
		PlotItems[plotID]['laxis_label'] = value;
	}
	else if(attrib == 'raxis_label') {
		PlotItems[plotID]['raxis_label'] = value;
	}
	else if(attrib == 'major_ticks') {
		PlotItems[plotID]['major_ticks'] = value;
	}
	else if(attrib == 'minor_ticks') {
		PlotItems[plotID]['minor_ticks'] = value;
	}
	else {
		PlotItems[plotID]['vars'][plotvarID][attrib] = value;
	}
}
function addPlotVar(plotID, varIDsumm) {
	id = varIDsumm.split('-');
	varID = id[0];
	varSumm = id[1];
	if(varID == '') return;
	
	PlotItems[plotID]['vars'][PlotItems[plotID]['nVars']] = new PlotVar(varID, varSumm, 'line', 'solid', 'black', 1);
	PlotItems[plotID]['nVars']++;
	//makePlotVarList(plotID, -1);
	reloadPlot(plotID);
}
function makePlotVarList(plotID, plotvarID) {
	if(plotvarID == -1) plotvarID = PlotItems[plotID]['nVars'] - 1;
	varID = PlotItems[plotID]['vars'][plotvarID]['varID'];
	longVar = VarItems[varID]['longVar']
	varSumm = PlotItems[plotID]['vars'][plotvarID]['summary'];
	ptype = PlotItems[plotID]['vars'][plotvarID]['plottype'];
	ltype = PlotItems[plotID]['vars'][plotvarID]['linetype'];
	color = PlotItems[plotID]['vars'][plotvarID]['color'];
	yaxis = PlotItems[plotID]['vars'][plotvarID]['yaxis'];
	
	longVar = varSumm.substr(0, 1).toUpperCase() + varSumm.substr(1) + '. ' + VarItems[varID]['longVar'];
	
	html = '<p><b>' + longVar + '</b>&nbsp;<img src="images/delete12.png" onclick="deleteFromPlotList(' + plotID + ', ' + plotvarID + ')" onmouseover="this.src=\'images/delete12_on.png\'" onmouseout="this.src=\'images/delete12.png\'" align="top" />';
	
	html += '<br />Plot Type: <select style="width: 80px;" onchange="updatePlotDef(' + plotID + ', ' + plotvarID + ', \'plottype\', this.value)">';
	html += '<option value="line"' + ((ptype=='line')?' selected':'') + '>Line</option>';
	html += '<option value="bar"' + ((ptype=='bar')?' selected':'') + '>Barplot</option>';
	html += '<option value="hist"' + ((ptype=='hist')?' selected':'') + '>Histogram</option>';
	html += '</select>';
	
	html += '<br />Color: <select style="width: 80px;" onchange="updatePlotDef(' + plotID + ', ' + plotvarID + ', \'color\', this.value)">';
	html += '<option value="black"' + ((color=='black')?' selected':'') + '>Black</option>';
	html += '<option value="blue"' + ((color=='blue')?' selected':'') + '>Blue</option>';
	html += '<option value="red"' + ((color=='red')?' selected':'') + '>Red</option>';
	html += '<option value="gray"' + ((color=='gray')?' selected':'') + '>Gray</option>';
	html += '<option value="green"' + ((color=='green')?' selected':'') + '>Green</option>';
	html += '<option value="orange"' + ((color=='orange')?' selected':'') + '>Orange</option>';
	html += '<option value="violet"' + ((color=='violet')?' selected':'') + '>Violet</option>';
	html += '<option value="brown"' + ((color=='brown')?' selected':'') + '>Brown</option>';
	html += '</select>';
	
	html += '<br />Line Type: <select style="width: 80px;" onchange="updatePlotDef(' + plotID + ', ' + plotvarID + ', \'linetype\', this.value)">';
	html += '<option value="solid"' + ((ltype=='solid')?' selected':'') + '>Solid</option>';
	html += '<option value="dashed"' + ((ltype=='dashed')?' selected':'') + '>Dashed</option>';
	html += '<option value="dotted"' + ((ltype=='dotted')?' selected':'') + '>Dotted</option>';
	html += '</select>';
	
	html += '<br />Y-axis: <select style="width: 80px;" onchange="updatePlotDef(' + plotID + ', ' + plotvarID + ', \'yaxis\', this.value)">';
	html += '<option value="1"' + ((yaxis==1)?' selected':'') + '>Left</option>';
	html += '<option value="2"' + ((yaxis==2)?' selected':'') + '>Right</option>';
	html += '</select>';
	
	html += '</p>';
	$('addplotList').innerHTML += html;
}
function updateAddPlot(newPlotID) {
	apBox = $('addPlotBox');
	if(apBox.innerHTML == '') thisclass = 'vardiv1';
	else thisclass = 'vardiv';
	apBox.innerHTML += '<div id="plot' + newPlotID + '" class="' + thisclass + '"></div>';
	pBox = $('plot' + newPlotID);
	pBox.innerHTML = makePlotDisplay(newPlotID);
}
function makePlotDisplay(PlotID) {
	html = '<table cellspacing="0" cellpadding="0" border="0"><tr>';
	html += '<td><img src="images/delete12.png" onclick="updateDeletePlot(' + PlotID + ')" onmouseover="this.src=\'images/delete12_on.png\'" onmouseout="this.src=\'images/delete12.png\'" align="top" /></td>';
	html += '<td><a href="javascript:reloadPlot(' + PlotID + ')">Plot ' + (PlotID+1) + ':</a> ' + PlotItems[PlotID]['title'] + '</td>';
	html += '</tr></table>';
	return(html);
}
function updateDeletePlot(delPlotID) {
	reloadPlot(delPlotID);
	yn = confirm('Are you sure you want to delete this plot definition?');
	if(yn == 1) {
		pBox = $('plot' + delPlotID);
		pBox.parentNode.removeChild(pBox);
		PlotItems[delPlotID] = null;
		divTurnOn('rightimage');
	}
}
function deleteFromPlotList(plotID, plotvarID) {
	yn = confirm('Are you sure you want to remove this variable from your plot definition?');
	if(yn == 1) {
		PlotItems[plotID]['vars'][plotvarID] = null;
		reloadPlot(plotID);
	}
}
function toggleRQ() {
	if($('recent_queries').style.display == 'none') $('recent_queries').style.display = 'block';
	else $('recent_queries').style.display = 'none';
}
function fillVars() {
	for(j=0; j<VarItems.length; j++) {
		if(VarItems[j]['selected']) {
		//	alert(j);
			updateAddVar(j, true);
		}
	}
	for(k=0; k<PlotItems.length; k++) {
		updateAddPlot(k);
	}
}
function clearForm() {
	yn = confirm('Are you sure you want to clear this form?');
	if(yn == 1) {
		window.location='index.php';
	}
}
function addIt(term, list, quotes) {
	newTerm = '';
	if(list != '') newTerm += ', ';
	if(quotes) newTerm += '\'';
	newTerm += term;
	if(quotes) newTerm += '\'';
	return(newTerm);
}
function preSubmit() {
//	$('sdate').value = $('sdateY').value + '-' + $('sdateM').value + '-' + $('sdateD').value;
//	$('edate').value = $('edateY').value + '-' + $('edateM').value + '-' + $('edateD').value;
	$('ssub').value = $('ssubM').value + '-' + $('ssubD').value;
	$('esub').value = $('esubM').value + '-' + $('esubD').value;
	
	field_list = '';
	summ_list = '';
	vars_selected = Array();
	for(i=0; i<VarItems.length; i++) {
		this_summ_list = '';
		if(VarItems[i]['selected']) {
			field_list += addIt(VarItems[i]['shortVar'], field_list, true);
			if(VarItems[i]['average']) {
				this_summ_list += addIt('ave', this_summ_list, true);
				vars_selected[vars_selected.length] = [VarItems[i]['shortVar'], 'ave'];
			}
			if(VarItems[i]['minimum']) {
				this_summ_list += addIt('min', this_summ_list, true);
				vars_selected[vars_selected.length] = [VarItems[i]['shortVar'], 'min'];
			}
			if(VarItems[i]['maximum']) {
				this_summ_list += addIt('max', this_summ_list, true);
				vars_selected[vars_selected.length] = [VarItems[i]['shortVar'], 'max'];
			}
			if(VarItems[i]['total']) {
				this_summ_list += addIt('tot', this_summ_list, true);
				vars_selected[vars_selected.length] = [VarItems[i]['shortVar'], 'tot'];
			}
			if(this_summ_list == '') {
			//	alert('All selected variables must have at least one summary statistic selected.');
				alert('Please select a summary statistic for ' + VarItems[i]['shortVar'] + '.');
				return(false);
			}
			summ_list += addIt(VarItems[i]['shortVar'] + '=c(' + this_summ_list + ')', summ_list, false);
		}
	}
	
	if(field_list == '') {
		alert('Please select at least one variable.');
		return(false);
	}
	plot_list = '';
	plotn = 0;
	for(i=0; i<PlotItems.length; i++) {
		this_plot = '';
		if(PlotItems[i] != null) {
			this_var = '';
			varn = 0;
			barn = 0;
			histn = 0;
			Yunits = ['', ''];
			
			for(j=0; j<PlotItems[i]['vars'].length; j++) {
				tvar = PlotItems[i]['vars'][j];
				if(tvar != null) {
					if(this_var != '') this_var += ', ';
					this_var += 'var' + varn + '=list(field=\'' + VarItems[tvar['varID']]['shortVar'] + '\', statistic=\'' + tvar['summary'] + '\', plottype=\'' + tvar['plottype'] + '\', linetype=\'' + tvar['linetype'] + '\', color=\'' + tvar['color'] + '\', yaxis=' + tvar['yaxis'] + ')';
					varn++;
					if(tvar['plottype'] == 'bar') barn++;
					else if(tvar['plottype'] == 'hist') histn++;
					if(Yunits[tvar['yaxis']-1] == '') Yunits[tvar['yaxis']-1] = VarItems[tvar['varID']]['units'];
					else if(Yunits[tvar['yaxis']-1] != VarItems[tvar['varID']]['units']) Yunits[tvar['yaxis']-1] = 'Error';
					var_ok = false;
					for(k=0; k<vars_selected.length; k++) {
						if(vars_selected[k][0] ==  VarItems[tvar['varID']]['shortVar'] && vars_selected[k][1] == tvar['summary']) var_ok = true;
					}
					if(!var_ok) {
						alert('Error in Plot ' + (plotn+1) + ': A variable (' + tvar['summary'] + ' ' + VarItems[tvar['varID']]['shortVar'] + ') is used in this plot, but is not present in your variable selection.');
						return(false);
					}
				}
			}
			if(this_var == '') {
				alert('Error in Plot ' + (plotn+1) + ': Please select at least one variable to plot.');
				return(false);
			}
			if(histn > 0 && histn != varn) {
				alert('Error in Plot ' + (plotn+1) + ': Histograms cannot be combined with other plot types in the same plot.');
				return(false);
			}
			if(barn > 1 && barn != varn) {
				alert('Error in Plot ' + (plotn+1) + ': When combining bars and lines in a single plot, only one variable can be a barplot.');
				return(false);
			}
			if(Yunits[0] == 'Error' || Yunits[1] == 'Error') {
				alert('Error in Plot ' + (plotn+1) + ': Variable units must match when using a common Y-axis.');
				return(false);
			}
			if(PlotItems[i]['major_ticks'] == 'None' && PlotItems[i]['minor_ticks'] == 'None') {
				alert('Error in Plot ' + (plotn+1) + ': You must select major and/or minor tick marks for the X-axis.');
				return(false);
			}
			
			if(plot_list != '') plot_list += ', ';
			plot_list += 'plot' + plotn + '=list(title=\'' + PlotItems[i]['title'] + '\', laxis=\'' + PlotItems[i]['laxis_label'] + '\', raxis=\'' + PlotItems[i]['raxis_label'] + '\', majtick=\'' + PlotItems[i]['major_ticks'] + '\', mintick=\'' + PlotItems[i]['minor_ticks'] + '\', vars=list(' + this_var + '))';
			plotn++;
			
			//alert('summ_list = ' + summ_list + '\n\nplot_list = ' + plot_list);
			//return(false);
		}
	}
	
	$('field_list').value = field_list; //'c(' + field_list + ')';
	$('summ_list').value = summ_list; //'list(' + summ_list + ')';
	$('plot_list').value = plot_list; //'list(' + plot_list + ')';
//	alert(plot_list);
//	return(false);
	return(true);
}


