function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      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;
		var switch_on = parent_page;
		if ( document.getElementById('nav_' + switch_on) != undefined ) document.getElementById('nav_' + switch_on).className = 'active';
		
		switch ( parent_page ) {
		
			case 'about_rouse' :
			case 'financial_expertise' :
			case 'meet_the_team' :
			case 'meet_the_team_paul_mcdine' :
			case 'meet_the_team_lisa_butler' :
			case 'meet_the_team_linda_boynton' :
			case 'meet_the_team_jenni_butler' :
			case 'meet_the_team_ben_silk' :
				document.getElementById('sub_nav_about_rouse').style.display = '';
				switch_on = 'meet_the_team';
				break;
		
			case 'financial_planning' :
			case 'what_is_financial_planning' :	
			case 'ten_things_to_know' :
			case 'what_is_important' :
			case 'how_we_invest' :
				document.getElementById('sub_nav_financial_planning').style.display = '';
				break;

			case 'financial_services' :
			case 'wrap_accounts' :
			case 'fee_only_advice' :
			case 'commission_services' :
			case 'investment_approach' :
				document.getElementById('sub_nav_financial_services').style.display = '';
				break;
		
		}
		
		switch ( parent_page ) {
		
			case 'meet_the_team' :
			case 'meet_the_team_paul_mcdine' :
			case 'meet_the_team_lisa_butler' :
			case 'meet_the_team_linda_boynton' :
			case 'meet_the_team_jenni_butler' :
			case 'meet_the_team_ben_silk' :
			if ( document.getElementById('nav_meet_the_team') != undefined ) document.getElementById('nav_meet_the_team').className = 'active';
			
		}

	}

//	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();
	}