

// JavaScript Document
// TimeSearch index page specific code

var oldstate = "alloff";
var newstate = "";

	function togglenow(szDivID , iState) { //function to switch layers on or off
			if(document.layers)	   //NN4+
			{
			   document.layers[szDivID].visibility = iState ? "show" : "hide";
			}
			else if(document.getElementById)	  //gecko(NN6) + IE 5+
			{
				var obj = document.getElementById(szDivID);
				obj.style.visibility = iState ? "visible" : "hidden";
			}
			else if(document.all)	// IE 4
			{
				document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
			}

	}

	
	function showonoff(id) { //toggle variable showing state of areas / themes (+lhcol) or options layers
			if (id == "options" && oldstate == "optionson") {
				var szDivID = "options";
				var iState = 0;  
				newstate = "alloff";
				togglenow(szDivID , iState);
			}
			if (id == "options" && oldstate != "optionson") {
				var szDivID = "options";
				var iState = 1;  
				newstate = "optionson";
				togglenow(szDivID , iState);
			}
		
			if (id == "lhcol" && oldstate == "alloff") {
				var szDivID = "lhcol";
				var iState = 0;  
				togglenow(szDivID , iState);
			}
			if (id == "lhcol" && oldstate != "alloff") {
				var szDivID = "lhcol";
				var iState = 1;  
				togglenow(szDivID , iState);
			}
			if (id == "areas" && oldstate == "themeson") { // areas clicked but themes layer is currently on so switch off
				var szDivID = "themes";
				var iState = 0;  // 1 visible, 0 hidden
				togglenow(szDivID , iState); // call function to actually switch layers on or off	
			}
			if (id == "themes" && oldstate == "areason") { // themes clicked but area layer is currently on so switch off
				var szDivID = "areas";
				var iState = 0;  
				togglenow(szDivID , iState); 
			}
			if (id == "areas" && oldstate == "alloff" || id == "areas" && oldstate == "themeson") { // area layer is to be switched on
				var szDivID = "areas";
				var iState = 1; 
				newstate = "areason";
				togglenow(szDivID , iState); 	
			}
			if (id == "areas" && oldstate == "areason") { // area layer is to be switched off
				var szDivID = "areas";
				var iState = 0;  
				newstate = "alloff";
				togglenow(szDivID , iState);
			}
			if (id == "themes" && oldstate == "alloff" || id == "themes" && oldstate == "areason") { // themes layer is to be switched on
				var szDivID = "themes";
				var iState = 1; 
				newstate = "themeson";
				togglenow(szDivID , iState); 	
			}
			if (id == "themes" && oldstate == "themeson") { // themes layer is to be switched off
				var szDivID = "themes";
				var iState = 0;  
				newstate = "alloff";
				togglenow(szDivID , iState);
			}
			oldstate = newstate;
//	alert ("id = " + id  + " \n oldstate = " + oldstate )
	}
	
function clickareas(){//areas button is clicked
		if (oldstate=='optionson'){
			showonoff('options');
		}
		if (oldstate=='themeson'){		}
		showonoff('areas'); 
		showonoff('lhcol');

		createView(TREES['AREAS']);
		return true;
}

function clickthemes(){ // themes button is clicked
	if (oldstate=='optionson'){
		showonoff('options');
	}
	if (oldstate=='areason'){	}
	showonoff('themes');
	showonoff('lhcol');	
	
	createView(TREES['THEMES']);
	return true;
}

// functions to submit values from tree views single or both.
			function recur_nodes(o_node, s_seperator){
				var a_items = o_node.a_children;
				var a_value = [];
				for(var i = 0; i < a_items.length; i++) {
					//if(a_items[i].a_children) 
					{
					//condition to check whether 'node_id' key is defined and its value is a number
						if(a_items[i].n_state & 4)
							if( a_items[i].a_config[2]) //CB changed so text would be recognised && typeof(a_items[i].a_config[2]['node_id'])=='number')
								a_value[a_value.length] = a_items[i].a_config[2]['node_id'];
						if(!checkChilds(a_items[i])) {
							var s_value = recur_nodes(a_items[i],s_seperator);
							if(s_value)	a_value[a_value.length] = s_value;
						}
					}
				}
				return a_value.join(s_seperator);
			}

			function prepare_sel_nodes(o_form, s_input, n_tree, s_seperator, b_submit) {
				var s_value = recur_nodes(TREES[n_tree],s_seperator);
				o_form.elements[s_input].value = s_value;
				if(b_submit) o_form.submit();
//				alert('>'+s_value+'<')
				return false;
			}

			function alter_submit(s_form, s_inputa, n_treea, s_inputb, n_treeb, s_seperator, b_submit, s_timelineid, s_sort2, s_direction, s_keywords){
				var o_form = document.forms[s_form];
				prepare_sel_nodes(o_form, s_inputa, n_treea, s_seperator, 0);
				prepare_sel_nodes(o_form, s_inputb, n_treeb, s_seperator, 0);
				//CB Also add sort2 and direction (if they exist). And, if someone has chosen previous or next, there is no year sort
				if (s_direction=='PREV')	{
					o_form.elements["topsort"].value = s_sort2;
					o_form.elements["direction"].value = s_direction;
				}
				if (s_direction=='NEXT')	{

					o_form.elements["bottomsort"].value = s_sort2;
					o_form.elements["direction"].value = s_direction;
				}
				else	{
					//do nothing
				}
				if (typeof(s_keywords)!='undefined')	{
					o_form.elements["keywords"].value = s_keywords;
				}
				else	{
					s_keywords = document.forms['keywords'].elements["keywords"].value
					o_form.elements["keywords"].value = s_keywords;
				}
				//o_form.elements["sort2"].value = s_sort2;
				//If there is a sort value given, then blank the year, the sort value overrides it
				if (typeof(sort2)!='undefined')	{
					o_form.elements["getyear"].value = "";
				}
				if (s_timelineid!=0)	{
					o_form.elements["timelineid"].value = s_timelineid;;
				}
				else	{
					o_form.elements["timelineid"].value = "";
				}
				//CB End
				if(b_submit) o_form.submit();
				//alert('>'+s_value+'<')
				return false;
			}
			function prepare_twin_values(s_form, s_inputa, n_treea, s_inputb, n_treeb, s_seperator, c_submit){
				var o_form = document.forms[s_form];
				var a_selected = TREES[n_treea].find_item_by_state(4);
				var a_value = [];
				var b_selected = TREES[n_treeb].find_item_by_state(4);
				var b_value = [];
				for(var i = 0; i < a_selected.length; i++) {
					//condition to check whether 'node_id' key is defined and its value is a number
					if(a_selected[i].a_config[2] && typeof(a_selected[i].a_config[2]['node_id'])=='number')
						a_value[a_value.length] = a_selected[i].a_config[2]['node_id'];
				}
				for(var j = 0; j < b_selected.length; j++) {
					//condition to check whether 'node_id' key is defined and its value is a number
//					alert(b_selected[j].a_config[0]+'\n'+b_selected[j].a_config[2])
					if(b_selected[j].a_config[2] && typeof(b_selected[j].a_config[2]['node_id'])=='number')
						b_value[b_value.length] = b_selected[j].a_config[2]['node_id'];
				}
				var s_valuea = a_value.join(s_seperator)
				o_form.elements[s_inputa].value = s_valuea;
				var s_valueb = b_value.join(s_seperator)
				o_form.elements[s_inputb].value = s_valueb;
				if(c_submit) {
					o_form.submit();
				}
			}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function TreesOnload(){
	for(var i in TREES)	createView(TREES[i]);
}


//CB - submitting the wordsearch ONLY (since it specifically sets wordsite to 1)
function submit_search()	{
	location.href="default.asp?conid=8&wordsite=1&searchword=" + window.treesform.searchfor.value;
}

//Selecting all checkboxes
function select_all()	{
	//var oElements = document.forms["mainarea"].tags("INPUT");
	var oElements = document.forms["mainarea"].getElementsByTagName("INPUT");
	for (var i=0; i < oElements.length; i++)	{
		if (oElements[i].type=='checkbox')	{
			oElements[i].checked = true;
		}
	}
}

//Deselecting all checkboxes
function deselect_all()	{
	//var oElements = document.forms["mainarea"].tags("INPUT");
	var oElements = document.forms["mainarea"].getElementsByTagName("INPUT");
	for (var i=0; i < oElements.length; i++)	{
		if (oElements[i].type=='checkbox')	{
			oElements[i].checked = false;
		}
	}	
}

//Get a fixed timeline where area and theme are specified..
function get_timeline(area, theme, start_date, end_date)	{
	var sQueryString = '';
	sQueryString = "conid=" + document.forms["trees"].conid.value;
	sQueryString += "area=" + area;
	sQueryString += "theme=" + theme;
	if (start_date!=0)	{
		sQueryString += "start_date=" + start_date;
	}
	if (end_date!=0)	{
		sQueryString += "end_date=" + end_date;
	}
	location.href = "default.asp?" + sQueryString;
}

//Check whether something is selected, before submitting the form
function checkSelection()	{
	var sCount = "";
	var iCount = 0;
	//var oElements = document.forms["mainarea"].tags("INPUT");
	var oElements = document.forms["mainarea"].getElementsByTagName("INPUT");
	for (var i=0; i < oElements.length; i++)	{
		if (oElements[i].type=='checkbox')	{
			if (oElements[i].checked)	{
				sCount = "checked";
				iCount = iCount + 1;
			}
		}
	}
	if (sCount=="checked")	{

		document.forms["mainarea"].submit();
	}
	else	{
		alert("You have made no selections");
	}
}

//Set up the ad / bc gifs
function clickyear(type)	{
	if (type=='bc')	{
		var src = document.forms["trees"].bbc_bc.getAttribute('src');
		var srcad = document.forms["trees"].bbc_ad.getAttribute('src');
		if (src.lastIndexOf('BC.gif') > 0)	{
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_o'+ftype);
			var fsrc = srcad.replace('AD_o'+ftype, 'AD.gif');
			document.forms["trees"].bbc_bc.setAttribute('src',hsrc);
			document.forms["trees"].bbc_ad.setAttribute('src',fsrc);
			if (document.forms["trees"].getyear.value > 0)	{
				document.forms["trees"].getyear.value = -1 * document.forms["trees"].date.value
			}
			//alert(document.forms["trees"].getyear.value);
		}
		else	{
			var hsrc = src.replace('BC_o.gif', 'BC.gif');
			var fsrc = srcad.replace('AD'+ftype, 'AD_o.gif');
			document.forms["trees"].bbc_bc.setAttribute('src',hsrc);
			document.forms["trees"].bbc_ad.setAttribute('src',fsrc);
			if (document.forms["trees"].getyear.value < 0)	{
				document.forms["trees"].getyear.value = -1 * document.forms["trees"].datevalue
			}
			//alert(document.forms["trees"].getyear.value);
		}
	}
	if (type=='ad')	{
		var src = document.forms["trees"].bbc_ad.getAttribute('src');
		var srcad = document.forms["trees"].bbc_bc.getAttribute('src');
		if (src.lastIndexOf('AD.gif') > 0)	{
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_o'+ftype);
			var fsrc = srcad.replace('BC_o.gif', 'BC.gif');
			document.forms["trees"].bbc_ad.setAttribute('src',hsrc);
			document.forms["trees"].bbc_bc.setAttribute('src',fsrc);
			if (trees.getyear.value < 0)	{
				trees.getyear.value = -1 * trees.getyear.value
			}
		}
		else	{
			var hsrc = src.replace('AD_o.gif', 'AD.gif');
			var fsrc = srcad.replace('BC.gif', 'BC_o.gif');
			document.forms["trees"].bbc_ad.setAttribute('src',hsrc);
			document.forms["trees"].bbc_bc.setAttribute('src',fsrc);
			if (trees.getyear.value > 0)	{
				trees.getyear.value = -1 * trees.getyear.value
		        }
		}	
	
	}
}

function getparm( name ) // function to grab value of known parameter from URL used by openpopwinscroll()
{
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}


function highlightyear(){
	var getdate = getparm('getyear');
	if (getdate == "") { 
	}else{
		if (getdate <1){
			clickyear('bc');
		}else{
			clickyear('ad');
		}
	}
}

//Check the email on registration
function checkEmail()	{
	if (document.forms["register"].email.value > '') {
		document.register.submit();
	}
	else	{
		alert('Please fill in your email address');
	}
}

//functions for forcing form submission on enter key 26/3/07
function checkEnter(e, datefield){ //date field
var characterCode
	 if(e && e.which){
	 e = e
	 characterCode = e.which //netscape
	 }
	 else{
	 e = event
	 characterCode = e.keyCode //IE
	 }	 
	 if(characterCode == 13){
	 treesform.getyear.value=datefield;
 	 alter_submit('trees', 'tree_selection1', 'AREAS', 'tree_selection2', 'THEMES', '\n', 1, 0); 	 
	return false
	 }
return true
	
}

function checksearchEnter(e, searchfield){ //search field
var characterCode
	 if(e && e.which){
	 e = e
	 characterCode = e.which
	 }
	 else{
	 e = event
	 characterCode = e.keyCode
	 }	 
	 if(characterCode == 13){
	 treesform.searchfor.value=searchfield;
 	 treesform.wordsite.value=1;submit_search(); 
	return false
	 }
return true
	
}

