/* 
======================================
Common Javascript functions - used thoughout the website
======================================
*/

/* used to open pages in a new window - define url, name, width and height */
function wopen(url, name, w, h){
	var win = window.open(url, name,
	'width=' + w + ', height=' + h + ', ' +
	'location=no, menubar=no, ' +
	'status=no, toolbar=no, scrollbars=no, resizable=no');
	win.resizeTo(w, h);
	win.focus();
}

/* opens a new tab with a specific url*/
function newtab(url){
	var tab;
	tab=window.open(url,'name');
	if (window.focus) {tab.focus()}
}

/* Finds the DOM element and hides the element if it is not already hidden, and sets the display style to nothing if it is already hidden so it displays */
function toggle(obj){
	var el = document.getElementById(obj);
	if(el.style.display == ''){ el.style.display = 'none'; }
	el.style.display = (el.style.display != 'none' ? 'none' : 'block' );
}

/* when focusing on the email signup input get rid of the text that is already in there is it equals 'Enter your email' */
function emailfocus(){
	var val = document.getElementById('email');
	if (val.value == 'Enter your email') {
		val.value = '';
	}	
}

/* when un-focusing/blurring the email signup input put the text 'enter your email' in, if the value is equal to nothing */
function emailblur(){
	var val = document.getElementById('email');
	if (val.value == '') {
		val.value = 'Enter your email';
	}	
}

/* Trim whitespace from the start and end of a string */
function trim(str){
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

/* hide contact div notes or show a specific div containing notes for a contact type */
function showNotes(){
	var selectbox = document.getElementById('type');
	var notes = document.getElementById('optionnotes').getElementsByTagName('p');
				
	for (var i=0; i<selectbox.options.length; i++) {
		notes[i].style.display = (notes[i].id == 'opt' + selectbox.options[selectbox.selectedIndex].value ? 'block' : 'none');
	};
	callEqualise(); // equalise columns after each note change so page columns are level still
}

/* For adding functions to the page loadListener when a page loads.
So if you want 5 js functions to run when the page loads, then use this function, and add the function to the loadListener.
Or alternatively if you have jquery included on the page you can put it inside one of these
$(document).ready(function(){} or $(function() { }
 */
function addLoadListener(fn){
	if (typeof window.addEventListener != 'undefined'){
		window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined'){
		document.addEventListener('load', fn, false);
	}
	else if (typeof window.attachEvent != 'undefined'){
		window.attachEvent('onload', fn);
	}
	else{
	var oldfn = window.onload;	
		if (typeof window.onload != 'function'){
			window.onload = fn;
		}	
		else{
			window.onload = function(){
				oldfn();
				fn();
			};
		}
	}
}
