window.onload = function(e) {
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
	externalLinks();
}

window.onunload = function(e) {
		var title = getActiveStyleSheet();
		if (title != '' && title != null) {
	        createCookie("style", title, 365);
	    }
	     else{
	        createCookie("style", "normal", 365);
	    }
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

// Converts links with an attribute of rel="external" to 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";
				 anchor.title = "This link will open in a new window.";
		}
	}
}

// Clears input values
function clearDefault(objInput,strDefaultText) {
	if (objInput.value==strDefaultText) objInput.value = '';
}

// Restores the default text, if the input box is empty
function restoreDefault(objInput,strDefaultText) {
	if (objInput.value=='') objInput.value = strDefaultText;
}

// This is for the AAA links, top right
function setActiveStyleSheet(title) {
		var i, a, main;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
				if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
				  a.disabled = true;
				  if(a.getAttribute("title") == title) a.disabled = false;
				}
		}
}

function getActiveStyleSheet() {
		var i, a;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
				if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
		}
		return null;
}

function getPreferredStyleSheet() {
		var i, a;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
				if(a.getAttribute("rel").indexOf("style") != -1
				   && a.getAttribute("rel").indexOf("alt") == -1
				   && a.getAttribute("title")
				   ) return a.getAttribute("title");
		}
		return null;
}

function createCookie(name,value,days) {

		if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
		}
		else expires = "";
		document.cookie = name+"="+value+expires+"; path=/";


}

function deleteCookie(name) {
document.cookie = name +
'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
}

function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
}

function setUpStyleSwitch() {
	// Does "styleswitch" exist?
	if (document.getElementById('styleswitch')) {
		// Writes the HTML
		document.getElementById('styleswitch').innerHTML = '<a href="#" onclick="setActiveStyleSheet(\'Normal\');" title="Normal text size">A <span>(normal text size)</span></a><a href="#" onclick="setActiveStyleSheet(\'Large\');" title="Large text size" class="large">A <span>(large text size)</span></a><a href="#" onclick="setActiveStyleSheet(\'Extra-large\');" title="Extra-large text size" class="xl">A <span>(Extra-large text size)</span></a>';
	}
}
// End AAA functions

// Social Bookmak Bar 2007 Martin Wendt
function $(e) {
	return (typeof e == 'string') ? document.getElementById(e) : e;
}
// encoded URL (strip parameters)
var thisUrl   = encodeURIComponent (location.href.split('?') [0]);
var thisTitle = encodeURIComponent (document.title);
var bmbDefaultHint='';

var aUrl = new Array();
aUrl ['google']     = { url:  'http://www.google.com/bookmarks/mark?op=add&bkmk=' + thisUrl + '&title=' + thisTitle + ' ',
		                hint: 'Bookmark at Google' };
aUrl ['facebook']      = { url:  'http://www.facebook.com/sharer.php?t=' + thisTitle + '&amp;u=' + thisUrl,
		                hint: 'Add to Facebook' };
aUrl ['twitter']      = { url:  'http://twitthis.com/twit?url=' + thisUrl,
		                hint: 'Add to Twitter' };

function writeBookmarkHtml (mode) {
	document.write ('<a href=' + aUrl[mode].url
	+ ' title="' + aUrl[mode].hint + '"'
	+ ' onmouseover="$(\'idBmbHint\').innerHTML=this.title"'
	+ ' onmouseout="$(\'idBmbHint\').innerHTML=bmbDefaultHint"'
	+ ' onclick="javascript: pageTracker._trackPageview(\'/outgoing/social-network/' + mode + '\');"'
	+ ' target="_blank">'
	+ '<img src="/img/icons/' + mode + '.gif" width="16" height="16" alt="' + aUrl[mode].hint + '" />'
	+ '</a> ');
}
function pageTools() {
	document.write('<div id="pagetools"> <p id="socialBook" title="Share this page">Share | ');
/* this works, but we loose control about the icon order:
	for (var u in aUrl) {
		writeBookmarkHtml (u);
	}*/
	writeBookmarkHtml ('facebook');
	writeBookmarkHtml ('google');
	writeBookmarkHtml ('twitter');
	document.write('<em id="idBmbHint">' + bmbDefaultHint + '</em></p> <p>');
	// Email icon
	document.write('<a href="mailto:?subject=Page%20on%20BM%20Savings&amp;body=' + location.href + '">Email this page to a friend<img src="/img/icon-email.gif" width="18" height="17" alt="" /></a> ');
	// Print icon
	if(window.print) {
		document.write('<a href="#" onclick="window.print(); return false;">Print this page<img src="/img/icon-print.gif" width="18" height="17" alt="" /></a>');
	}
	document.write('</p> </div>');
}
// End social media bar

