// function to select right option from select
function select_option(select, option) {

	var options = document.getElementById(select);
	if (options) {
		options = options.getElementsByTagName("option");
	} else {
		return;
	}
	for (var i=0; i<options.length; i++) {
		if (options[i].value.toLowerCase() == option.toLowerCase()) {
			options[i].selected = true;
			break;
		}
	}
	return;
}

// function to only show Level Of English checkbox if else than Brit or American chosen
// defaults levelofenglish to good conversation if not brit or american
function showhide_loe() {
						
	var sel = document.getElementById('s_nationality').options[document.getElementById('s_nationality').selectedIndex].value;
	//document.getElementById('loe_cell').style.display = '';
	if(sel != 'British' && sel != 'American') {
		document.getElementById('loe_cell').style.display = '';
		//select_option("s_levelofenglish", "Good Conversation");
	} else {
		document.getElementById('loe_cell').style.display = 'none';
		//select_option("s_levelofenglish", "Native");
	}
	if (!sel) { document.getElementById('loe_cell').style.display = 'none'; }
}

// function to check that no more than 3 elements have been checked in the prefs cells
function click_check(input) {
	
	var thenode = input.parentNode;
	while (thenode.id == '') {
		thenode = thenode.parentNode;
	}
	//alert(thenode.id);
	var inputs = thenode.getElementsByTagName("input");
	var j=0;
	var lim=3;
	if (thenode.id == 'academia_div') { lim=12; }
	for (var i=0;i<inputs.length;i++) {
		if (inputs[i].checked) {
			j++;
		}
	}
	if (j>lim) {
		input.checked = 0;
		alert("Please choose a maximum of "+lim+" from this section.");
	}
}

// function to control the dynamic behaviour of the Preferences area
function switcher(divdisp, boxcheck, arrdown) {
	
	if (document.getElementById(divdisp).style.display == '') { 
		document.getElementById(divdisp).style.display = 'none';
		document.getElementById(arrdown).src = 'img/right-arrow.gif';
		return;
	}
	
	var the_divs = document.getElementById('pref_cell').getElementsByTagName('div');
	var the_imgs = document.getElementById('pref_cell').getElementsByTagName('img');
	var the_boxes = document.getElementById('pref_cell').getElementsByTagName('checkbox');
	
	for (var i=0;i<the_divs.length;i++) { the_divs[i].style.display = 'none'; }
	for (var i=0;i<the_imgs.length;i++) { the_imgs[i].src = 'img/right-arrow.gif'; }
	for (var i=0;i<the_boxes.length;i++) { the_boxes[i].checked = 0; }
	
	document.getElementById(divdisp).style.display = '';
	document.getElementById(arrdown).src = 'img/down-arrow.gif';
	boxcheck.checked = 1;
	return;
}

// another dynamic control function, from page 2
function switcher2(tabdisp, arrdown) {
	
	if (document.getElementById(tabdisp).style.display == '') { 
		document.getElementById(tabdisp).style.display = 'none';
		document.getElementById(arrdown).src = 'img/right-arrow.gif';
	} else {					
		document.getElementById(tabdisp).style.display = '';
		document.getElementById(arrdown).src = 'img/down-arrow.gif';
		boxcheck.checked = 1;
	}
	return;
}
