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


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

/* Finds DOM element from name passed through and returns it */
function $$() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

/* 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() {
	for ( var i=0; i < arguments.length; i++ ) {
		$$(arguments[i]).style.display = ($$(arguments[i]).style.display != 'none' ? 'none' : '' );
	}
}

/* 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*$/, '');
}

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

