/*******************************************************************************
*                                                                              *
*  Web Site  :  http://www.epshosting.com/                                         *
*  Author    :  John Serris                                                    *
*  Modified  :  09-Dec-2004                                                    *
*                                                                              *
*******************************************************************************/

var d = document, dE = d.documentElement;
var fixIEwidth = 0;

/* Stylesheet Switching
*******************************************************************************/

var stylesheet = {
	set : function(title) {
		var found = 0;
		for (var i = 0; (a = d.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;
					found = 1;
				}
			}
		}
		if (found == 0) {
			title = this.getPreferred();
			if (title) {
				this.set(title);
			}
		}
	},
	getActive : function() {
		for (var i = 0; (a = d.getElementsByTagName("link")[i]); i++) {
			if (a.getAttribute("rel").indexOf("style") != -1
			&& a.getAttribute("title") && !a.disabled) {
				return a.getAttribute("title");
			}
		}
		return null;
	},
	getPreferred : function() {
		for (var i = 0; (a = d.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;
	}
}

/* Cookies
*******************************************************************************/

var cookie = {
	write : function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days*24*60*60*1000));
			var expires = "; expires=" + date.toGMTString();
		} else {
			var expires = "";
		}
		d.cookie = name + "=" + value + expires + "; path=/";
	},
	read : function(name) {
		name += "=";
		var cr = d.cookie.split("; ");
		for (var i = 0, crL = cr.length; i < crL; i++) {
			var c = cr[i];
			if (c.indexOf(name) == 0) {
				return unescape(c.substring(name.length,c.length));
			}
		}
		return null;
	}
};

/* Style Switcher UI
*******************************************************************************/

/*function addStyleswitcher() {
	var switcher = d.getElementById("customise");
	var buttons = switcher.getElementsByTagName("a");

	for (var i = 0, bL = buttons.length; i < bL; i++) {
		buttons[i].onclick = function() {
			this.blur();
			stylesheet.set(this.hash.substring(1));
			return false;
		};
	}
}*/

/* Hover support for drop downs in IE (Opera 7.23 also seems to need this too?!)
*******************************************************************************/

function menuHover() {
	var menuItems = d.getElementById("nav").getElementsByTagName("LI");

	for (var i = 0, miL = menuItems.length; i < miL; i++) {
		menuItems[i].onmouseover = function() {
			this.className = "mnhover";
		}
		menuItems[i].onmouseout = function() {
			this.className = "";
		}

		// Add alpha transparencey in IE 5.5+
		var hasSub = menuItems[i].getElementsByTagName("UL")[0];
		if (hasSub && (typeof hasSub.filters == "object")) {
			hasSub.style.filter = "alpha(opacity=90)";
		}

	}
}

/* Search Form
*******************************************************************************/

/*function searchForm() {
	var sb = d.getElementById("s");
	sb.defVal = "Search EPS";
	sb.value = sb.defVal;
	sb.onfocus = function() {
		this.className = "focus";
		if (this.value == this.defVal) {
			this.value = '';
		}
	};
	sb.onblur = function() {
		this.className = "";
		if (isempty(this.value)) {
			this.value = this.defVal;
		}
	};
}*/

/* Prevent IE from breaking the layout
*******************************************************************************/

function checkMinWidth() {
	var w = d.getElementById("main");
	if (w.offsetWidth < 710) {
		fixIEwidth = 1;
		stylesheet.set("fixed");
	}
}

/* Make external links pop

*******************************************************************************/

function popExLinks() {
	var aTags = d.getElementsByTagName("a");
	
	for( var i=0; i<aTags.length; i++ ) {
		// all non-javascript/mailto href attributes pointing to external locations
                // or if it's a pdf document (ken 2005-03-23)
		if( aTags[i].href.indexOf("http://") == 0 && aTags[i].href.indexOf("http://"+d.domain) != 0 ||
                    aTags[i].href.indexOf("http://") == 0 && aTags[i].href.indexOf(".pdf") > 5 ) {
			aTags[i].target = "_blank";
		}
	}
}

/* Form Validation
*******************************************************************************/

function isempty(val) {
	var x = /^\s*$/;
	return x.test(val);
}

/* Perform on page load/unload
*******************************************************************************/

function init() {
	window.defaultStatus = "Electronic Productivity Solutions";
	if (d.getElementById) {
		var c = cookie.read("epsprefs");
		var title = c ? c : stylesheet.getPreferred();
		stylesheet.set(title);
	}
}

window.onload = function(e) {
	if (d.getElementById) {
		if (d.all) {
			menuHover();
		}
		// Opera wont switch css sometimes unless we do it onload! POS!
		if (navigator.userAgent.indexOf('Opera') != -1) {
			init();
			//alert("Your browser sucks!!");
		}
		//addStyleswitcher();
		//searchForm();
        popExLinks();
		if (d.all && !window.opera && fixIEwidth == 0) {
			checkMinWidth();
		}
	}
};
window.onunload = function(e) {
	if (d.getElementById) {
		title = stylesheet.getActive();
		if (title != "plaintext") {
			cookie.write("epsprefs", stylesheet.getActive(), 0);
		}
	}
};

if (d.all && !window.opera && fixIEwidth == 0) {
	window.onresize = function() {
		checkMinWidth();
	};
}

/* Perform as soon as we can!
*******************************************************************************/

init();
