addLoadEvent(setOffsetCookie);

//function to load several other functions onload
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

//removes whitespace from beginning and end of a string
function trim(str) {	
	var char1 = str.charAt(0); //first character of str
	var charlst = str.charAt(str.length-1); //last character of str
	
	//if first char is a space or carraige return
	if(char1 == " " || char1 == "\n") { 
		//remove first character
		str = trim(str.substring(1));
	}
	//if lastchar is a space or carraige return
	if (charlst == " " || charlst == "\n") {
		//remove last character
		str = trim(str.substring(0,str.length-1));
	}
	return str;
}

function show(id) {
	//checkpoints
	if (!document.getElementById) return false;
	//if tag with id `id` is not found return false
	if (!document.getElementById(id)) return false;

	//create object
	var obj = document.getElementById(id);
	//set object display to show
	obj.style.display = "";
}

function hide(id) {
	//checkpoints
	if (!document.getElementById) return false;
	//if tag with id `id` is not found return false
	if (!document.getElementById(id)) return false;

	//create object
	var obj = document.getElementById(id);
	
	//set object view to hidden
	obj.style.display = "none";
}

function showHide(id) {
	//checkpoints
	if (!document.getElementById) return false;
	//if tag with id `id` is not found return false
	if (!document.getElementById(id)) return false;	

	//set object
	var obj = document.getElementById(id);

	//if currently set to hide
	if (obj.style.display == "none") {
		//show the element
		obj.style.display = "";
	}
	//else if currently set to show
	else {
		//hide element
		obj.style.display = "none";
	}
}

function createCookie(name,value,expires) {
	if (expires) {
		var date = new Date();
		var ms   = expires*24*60*60*1000;
		date.setTime(date.getTime() + ms);
		var expires = "expires=" + date.toGMTString() + ";";
	}

	//write cookie
	document.cookie = name + "=" + value + ";" + expires + "path =/";
}

function readCookie(name) {	
	//get array with all cookies
	var cookies = document.cookie.split(';');

	//loop through array
	for(var i=0; i < cookies.length; i++) {
		//set friendlier var name
		var cookie = cookies[i];

		/* remove any whitespace from beginning 
			and end of cookie string */
		cookie = trim(cookie);

		//add equal sign the name string
		var name = name + "=";

		/* if we find the name from parameter and
			the name from cookie matches */
		if (cookie.indexOf(name) == 0) {
			/* get the actual value from cookie
				by removing the first part (the name) */
			var value = cookie.substring(name.length,cookie.length);
			
			return value;
		}
	}
	return null;
}

function setOffsetCookie() {
	//get total offset between user and GMT 00:00 (in minutes)
	var date = new Date();
	var timezone = date.getTimezoneOffset();

	//writes cookie with offset value, expires after a year
	createCookie('GMT_offset',timezone,365);
}

//adds `string` as a new element in `array`
function addToArray(array,string) {
	//create index of a new element
	var index = array.length + 1;

	//set index of array to 'string' value
	array[index] = string;

	return array; 
}

//removes `string` element from `array`
function removeFromArray(array,string) {
	//loop through `array`
	for (var i=0; i <= array.length; i++) {
		//if element `array[i]` matches `string`
		if (array[i] == string) {
			//delete element `array[i]`
			delete array[i];
		}
	}

	return array;
}

//if array2 elements are in array1, delete them
function removeArrayFromArray(array1,array2) {
	//loop through `array2` array
	for (var i=0; i <= array2.length; i++) {
		//remove `array2[i]` element from `array`
		array1 = removeFromArray(array1,array2[i]);
	}

	return array1;
}

//returns true if `string` is in `array`
function searchThroughArray(array,string) {
	//loop through `array`
	for (var i=0; i <= array.length; i++) {
		//if `array[i]` element matches `string`
		if (array[i] == string) {
			return true;
		}
	}

	return false;
}
