function add_new_tree() {

	var tbody = document.getElementById('tree_table').getElementsByTagName('tbody')[0];
	var num_rows = document.getElementById('tree_table').getElementsByTagName('tr').length;
	
	var new_id = num_rows;
	
	var row = document.createElement('tr');
	
	//	type field
	var type_field = document.createElement('input');
	type_field.type = 'text';
	type_field.className = 'field_text';
	type_field.setAttribute('onFocus','this.className=\'field_text_focus\'');
	type_field.setAttribute('onBlur','this.className=\'field_text\'');
	type_field.name = 'tree_type_' + new_id;
	type_field.id = 'tree_type_' + new_id;
	type_field.style.width = '150px';
	
	//	age field
	var age_field = document.createElement('input');
	age_field.type = 'text';
	age_field.className = 'field_text';
	age_field.setAttribute('onFocus','this.className=\'field_text_focus\'');
	age_field.setAttribute('onBlur','this.className=\'field_text\'');
	age_field.name = 'tree_age_' + new_id;
	age_field.id = 'tree_age_' + new_id;
	age_field.style.width = '50px';
	
	//	variety field
	var variety_field = document.createElement('input');
	variety_field.type = 'text';
	variety_field.className = 'field_text';
	variety_field.setAttribute('onFocus','this.className=\'field_text_focus\'');
	variety_field.setAttribute('onBlur','this.className=\'field_text\'');
	variety_field.name = 'tree_variety_' + new_id;
	variety_field.id = 'tree_variety_' + new_id;
	variety_field.style.width = '500px';
	
	//	pruned field
	var pruned_field = document.createElement('input');
	pruned_field.type = 'checkbox';
	pruned_field.name = 'tree_pruned_' + new_id;
	pruned_field.id = 'tree_pruned_' + new_id;
	pruned_field.value = 'yes';
	
	//	type cell
	var type_cell = document.createElement('td');
	type_cell.appendChild(type_field);
	row.appendChild(type_cell);
	
	//	age cell
	var age_cell = document.createElement('td');
	age_cell.appendChild(age_field);
	row.appendChild(age_cell);
	
	//	variety cell
	var variety_cell = document.createElement('td');
	variety_cell.appendChild(variety_field);
	row.appendChild(variety_cell);
	
	//	pruned cell
	var pruned_cell = document.createElement('td');
	pruned_cell.appendChild(pruned_field);
	row.appendChild(pruned_cell);
	
	tbody.appendChild(row);
	type_field.focus();

}

function validate_survey() {

	var error_message = '<div class="message_negative"><p>Some of the information appears to be missing&hellip;</p><ul>';
	var errors = 0;
	var error_container = document.getElementById('error_container');
	error_container.innerHTML = '';
	
	var firstname = document.getElementById('firstname');
	var lastname = document.getElementById('lastname');
	var address_1 = document.getElementById('address_1');
	var town = document.getElementById('town');
	var county = document.getElementById('county');
	var post_code = document.getElementById('post_code');
	var email = document.getElementById('email');
	
	if ( firstname.value == '' ) {
		error_message += '<li>You have not entered your <a href="#firstname">first name</a></li>';
		errors = 1;
	}
	
	if ( lastname.value == '' ) {
		error_message += '<li>You have not entered your <a href="#lastname">last name</a></li>';
		errors = 1;
	}
	
	if ( address_1.value == '' ) {
		error_message += '<li>You have not entered the <a href="#address_1">first line of your address</a></li>';
		errors = 1;
	}
	
	if ( town.value == '' ) {
		error_message += '<li>You have not entered your <a href="#town">town</a></li>';
		errors = 1;
	}
	
	if ( county.value == '' ) {
		error_message += '<li>You have not entered your <a href="#county">county</a></li>';
		errors = 1;
	}
	
	if ( post_code.value == '' ) {
		error_message += '<li>You have not entered your <a href="#post_code">post code</a></li>';
		errors = 1;
	}
	
	if ( email.value != '' && ! (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value)) ) {
		error_message += '<li>The <a href="#email">email address</a> you entered doesn\'t look correct</li>';
		errors = 1;
	}
	
	if ( errors == 1 ) {
		error_message += '</ul></div>';
		error_container.innerHTML = error_message;
		return false;
	} else {
		return true;
	}
	

}


//	show the rota form
	function show_rota_form(session_id,date,start,end,user_id,rota_id) {
		var rota_form = document.getElementById('rota_form');
		rota_form.style.display = '';
		var fk_rota_session_id = document.getElementById('fk_rota_session_id');
		var fk_hs_user_id = document.getElementById('fk_hs_user_id');
		var date_field = document.getElementById('date');
		var start_hour_field = document.getElementById('start_hour');
		var start_minute_field = document.getElementById('start_minute');
		var end_hour_field = document.getElementById('end_hour');
		var end_minute_field = document.getElementById('end_minute');
		var rota_id_field = document.getElementById('rota_id');
		
		for ( var i = 0; i < fk_rota_session_id.options.length; i++ ) {
			if ( fk_rota_session_id.options[i].value == session_id ) fk_rota_session_id.options[i].selected = true;	
		}
		date_field.value = date;
		
		var start_array = start.split(':');
		var end_array = end.split(':');
		
		for ( var m = 0; m < start_hour_field.options.length; m++ ) {
			if ( start_hour_field.options[m].value == start_array[0] ) start_hour_field.options[m].selected = true;
		}
		for ( var n = 0; n < start_minute_field.options.length; n++ ) {
			if ( start_minute_field.options[n].value == start_array[1] ) start_minute_field.options[n].selected = true;
		}

		for ( var o = 0; o < end_hour_field.options.length; o++ ) {
			if ( end_hour_field.options[o].value == end_array[0] ) end_hour_field.options[o].selected = true;
		}
		for ( var p = 0; p < end_minute_field.options.length; p++ ) {
			if ( end_minute_field.options[p].value == end_array[1] ) end_minute_field.options[p].selected = true;
		}
		
		for ( var a = 0; a < fk_hs_user_id.options.length; a++ ) {
			if ( fk_hs_user_id.options[a].value == user_id ) fk_hs_user_id.options[a].selected = true;
		}
		rota_id_field.value = rota_id;
		
	}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}

//	bookmark a page
	function bookmark_page( url, title ) {
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}


function bookmarksite(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}

//	limit number of characters in a textarea field
	function limit_characters( limit_field, limit_count_field, limit_num ) {
		var limit_field = document.getElementById(limit_field);
		var limit_count_field = document.getElementById(limit_count_field);
		if ( limit_field != undefined ) {
			if ( limit_field.value.length > limit_num ) {
				limit_field.value = limit_field.value.substring( 0, limit_num );
			} else {
				limit_count_field.innerHTML	= (limit_num - limit_field.value.length) + ' characters remaining';
			}		
		}	
	}


//	show events on the home page
		function show_events(type) {
			var type;
			var tab_summary = document.getElementById('tab_summary');
			var tab_water = document.getElementById('tab_water');
			var tab_ashore = document.getElementById('tab_ashore');
			var container_summary = document.getElementById('event_summary');
			var container_water = document.getElementById('event_water');
			var container_ashore = document.getElementById('event_ashore');
			switch ( type ) {
				case 'water' :
					tab_summary.className = 'off';
					tab_water.className = 'on';
					tab_ashore.className = 'off';
					container_summary.style.display = 'none';
					container_water.style.display = '';
					container_ashore.style.display = 'none';
					break;
				case 'ashore' :
					tab_summary.className = 'off';
					tab_water.className = 'off';
					tab_ashore.className = 'on';
					container_summary.style.display = 'none';
					container_water.style.display = 'none';
					container_ashore.style.display = '';
					break;
				default :
					tab_summary.className = 'on';
					tab_water.className = 'off';
					tab_ashore.className = 'off';
					container_summary.style.display = '';
					container_water.style.display = 'none';
					container_ashore.style.display = 'none';
					break;
					
			}
		}


//	when the search field gets the focus
	function search_focus() {
		var search = document.getElementById('q');
		if ( search.value == 'Search...' ) {
			search.value = '';
		} else {
			search.select();
		}
		search.className = 'field_search_focus';
	}
	
//	when the search field loses the focus
	function search_blur() {
		var search = document.getElementById('q');
		if ( search.value == '' ) {
			search.value = 'Search...';
		}
		search.className = 'field_search';
	}

// 	retrieve a url parameter
	function get_url_parameter( name ) {
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
		if( results == null ) {
			return "";
		} else {
			return results[1];
		}
	}

//	preload images
	function preload_images () {
		arImageSrc = new Array ();
		arImageList = new Array ();
		for (counter in arImageSrc) {
			arImageList[counter] = new Image();
			arImageList[counter].src = arImageSrc[counter];
		}
	}

//	convert date and time to unixtimestamp
	function date_to_unixtime(target_field, month_el, day_el, year_el, hour_el, minute_el) {
		var target = document.getElementById(target_field);
		var year = document.getElementById(year_el).value;
		var month = document.getElementById(month_el).value;
		var day = document.getElementById(day_el).value;
		var hour = document.getElementById(hour_el).value;
		var minute = document.getElementById(minute_el).value;
		var second = '01';
		
		var errors = 0;
		var error_message = 'The following elements appear to be missing or incorrect' + "\n";
		if ( year.length != 4 ) {
			error_message += ' - you must enter a 4 digit year' + "\n";
			errors = 1;
		}
		if ( month > 12 ) {
			error_message += ' - the month must be between 1 and 12' + "\n";
			errors = 1;
		}
		if ( day > 31 ) {
			error_message += ' - the day must be between 1 and 31' + "\n";
			errors = 1;
		}
		if ( hour > 24 ) {
			error_message += ' - the hour must be 24 hour (e.g. 13 = 1pm)' + "\n";
			errors = 1;
		}
		if ( minute > 59 ) {
			error_message += ' - the minutes must be between 0 and 59' + "\n";
			errors = 1;
		}
		if ( errors == 1 ) {
			alert( error_message );
		} else {
			var humDate = new Date(Date.UTC(year, (nozeros(month)-1), nozeros(day), nozeros(hour), nozeros(minute), nozeros(second))); 
			target.value = (humDate.getTime()/1000.0);
		}
	
	}

//	get the current page
	function get_current_page() {
		var fullpath = location.pathname;
		var patharray = fullpath.split('/');
		var folderposition = patharray.length - 1;
		var filename = patharray[folderposition];
		var filearray = filename.split('.');
		var nav = filearray[0];
		return nav;
	}

//	open external links in a new window - replaces target="_blank"
	function externalLinks () { 
		if ( ! document.getElementsByTagName ) return; 
		var anchors = document.getElementsByTagName("a"); 
		for ( var i=0; i < anchors.length; i++ ) { 
			var anchor = anchors[i]; 
			if ( anchor.getAttribute("href") && anchor.getAttribute("rel") == "external" ) anchor.target = "_blank"; 
		} 
	} 

//	add given page to favourites (bookmark)
	function setBookmark ( url, str ) {
		if ( str == '' ) str = url;
		if ( document.all ) window.external.AddFavorite( url, str );
		else alert( 'Sorry, this function only works in Internet Explorer.\n\nPlease press CTRL and D to add a bookmark to \n"' + str + '".' );
	}

//	toggle a given elements visibility
	function toggle (el) {
		if ( document.getElementById(el) == undefined ) return false;
		if ( document.getElementById(el).style.display == 'none' ) {
			document.getElementById(el).style.display = '';
		} else {
			document.getElementById(el).style.display = 'none';
		}
	}

//	show a given element
	function show (el) {
		if ( document.getElementById(el) != undefined ) document.getElementById(el).style.display = '';
	}

//	hide a given element
	function hide (el) {
		if ( document.getElementById(el) != undefined ) document.getElementById(el).style.display = 'none';
	}

//	manage the show/hide buttons
	function showhide( source_el, target_el ) {
		var source = document.getElementById(source_el);
		var target = document.getElementById(target_el);
		toggle(target_el);
		if ( target.style.display == 'none' ) {
			source.innerHTML = 'Show &darr;';
			source.className = 'showhide';
		} else {
			source.innerHTML = 'Hide &uarr;';
			source.className = 'hideshow';
		}
	}


//	activate the correct navigation
	function activate_nav() {
		var nav = get_current_page();
		if ( nav == '' ) {
			nav = 'index';
		}
		if ( nav == 'search' || nav == 'email' ) {
			nav = 'blank';
		}
		var parent_page = nav;
		switch ( nav ) {
		
			case 'wwlp_aims' :
			case 'wwlp_board' :
				parent_page = 'about_wwlp';
				break;
				
			case 'west_wight_importance' :
			case 'west_wight_formation' :
			case 'west_wight_watercourses' :
			case 'west_wight_ecology' :
			case 'west_wight_influence' :
				parent_page = 'west_wight';
				break;
				
			case 'projects_learn' :
			case 'projects_conserve' :
			case 'projects_celebrate' :
			case 'projects_access' :
			case 'wwlp_projects' :
				parent_page = 'projects';
				break;
				
			case 'downloads' :
			case 'maps' :
			case 'poems' :
				parent_page = 'downloads';
				break;
				
			case 'grants_criteria' :
			case 'grants_case_studies' :
			case 'grants_downloads' :
				parent_page = 'grants';
				break;
				
			case 'newsletter' :
				parent_page = 'news';
				break;
		
		}
		if ( document.getElementById('nav_' + parent_page) != undefined ) document.getElementById('nav_' + parent_page).className = 'selected';
		if ( document.getElementById('sub_nav_' + nav) != undefined ) document.getElementById('sub_nav_' + nav).className = 'selected';

	}

//	make a textarea grow
	function extend_textarea(el) {
		if ( document.getElementById(el) == undefined ) return false;
		var el = document.getElementById(el);
		if ( el.value.length > 150 ) {
			el.style.height = "100px";
		} else {
			el.style.height = "50px";
		}
	}

//	populate a business card
	function populate_business_card(card_id,card_type) {
		xmlHttp = initiate_ajax();
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				document.getElementById('business_card_content').innerHTML=xmlHttp.responseText;
			}
		}
		xmlHttp.open("GET","business_card.php?card_id=" + card_id + "&card_type=" + card_type,true);
		xmlHttp.send(null);
	}



//	initialise an httpRequest object
	function initiate_ajax() {
		var xmlHttp;
		try {

			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		}
		catch (e) {

			// Internet Explorer
			try {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
		return xmlHttp;
	}


//	perform the following functions when the page loads
	window.onload = function(e) {
		externalLinks();
		activate_nav();
		initiate_ajax();
	}
