var agevalid=0;
var topvalid=0;
var ageval="";
var topicval="";
var ageind=0;
var topicind=0;
var arraycells=0;
var optvals= new String;

// Function resets the form and flags
function resetform() {
agevalid=0;
topvalid=0;
ageval="";
topicval="";
ageind=0;
topicind=0;
document.solform1.reset();
}

// Function validates inputs and subs the form if all ok
function submitform() {
checkage();
checktopic();
if (!agevalid)
{
	alert("Only 'Age range: 14-16' and 'Topics list: Energy and energy transfer' is searchable in the preview version of scienceonline");
}
else {
	if (!topvalid)
	{
		alert("Only 'Age range: 14-16' and 'Topics list: Energy and energy transfer' is searchable in the preview verision of scienceonline");
	}
}

if (agevalid && topvalid) {
//	document.solform1.submit(); NOT NEEDED FOR FREE AREA
	top.location.href="/search/search_results.html"
	window.status=topicval;
	//resetform();
	}
}

// Function validates the age range selection
function checkage() {
	ageind=document.forms.solform1.ageselect.selectedIndex;
	ageval=document.forms.solform1.ageselect.options[ageind].value;
	if (ageval != '2') {
		agevalid=0;
	}
	else {
		agevalid=1;
	}
//window.status=ageval;
}

// Function validates the topic selection
function checktopic() {
	topicind=document.forms.solform1.topicselect.selectedIndex;
	topicval=document.forms.solform1.topicselect.options[topicind].value;
	if (topicval != 15) {
		topvalid=0;
	}
	else {
		topvalid=1;
	}
//window.status=topicval;
}

topicsarray = new Array ("Cells and Life Processes&clp",
"Humans&hum",
"Plants&pla",
"Genetics and Inheritance&gai",
"Variation - Adaptation and Evolution&vae",
"Environment&env",
"Microbiology&mic",
"Materials - Properties and Separation&mps",
"Atoms Molecules and Ions&ami",
"Patterns of Chemical Change&pcc",
"Carbon Compounds&car",
"Chemical Industry&che",
"Geological Change&geo",
"Forces and Movement&for",
"Energy and Energy Transfer&eet",
"Electricity and Magnetism&eam",
"Light - Sound and Waves&lsw",
"Earth and Space&eas",
"Radioactivity&rad"
);

arraycells=topicsarray.length;

// function to build the selection element
function buildtopics(arraycells) {
	document.write('<select name=\"topicselect\" onchange=\"checktopic\(\)\;\">');
	document.write('<option value=\"\"\>Select a Topic\<\/option>');

	sortedkeys=topicsarray.sort();
	var counter=0;
	for (counter=0; counter < arraycells; counter++) {
		optvals=sortedkeys[counter];
		optvalbits=optvals.split("&");
		optname=optvalbits[0];
		optcode=optvalbits[1];
		document.write('<option value =\"' + optcode + '\">' + optname + '<\/option>');
	}
	document.write('<\/select>');
	document.write('<\/form>');
}

