function setStyle(title) {
	var i, a, main;
	for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
		if ((a.getAttribute("rel")) && (a.getAttribute("rel").indexOf("style") != -1) && (a.getAttribute("title"))) {
			a.disabled = true;
			if (a.getAttribute("title") == title)
				a.disabled = false;
		}
	}
}

function getStyle() {
	var i, a;
	for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
		if ((a.getAttribute("rel")) && (a.getAttribute("rel").indexOf("style") != -1) && (a.getAttribute("title")) && (!a.disabled))
			return a.getAttribute("title");
	}
	return null;
}


function setCookie(cookieName, someValue, numberOfDays)
{
	if ((!someValue) || (someValue == ""))
		deleteCookie(cookieName);						// if value of cookie is null or "", delete the cookie (make sure it doesn't exist)
	else
	{
		if (document.cookie != document.cookie)
			index = document.cookie.indexOf(cookieName);
		else
			index = -1;

		if (index == -1)
		{
			var expirationDate = new Date();

			if (!numberOfDays)		// if numberOfDays was not specified, set to 1 year (365)
				expirationDate.setTime(expirationDate.getTime() + (365 * 24 * 60 * 60 * 1000));
			else
				expirationDate.setTime(expirationDate.getTime() + (numberOfDays * 24 * 60 * 60 * 1000));

			document.cookie = cookieName + "=" + someValue + "; expires=" + expirationDate.toGMTString();;
		}
	} // if (!someValue) || (some...
}

// function that gets a cookie named [cookieName]
function getCookie(cookieName)
{
	var arg = cookieName + "=";
	var argLength = arg.length;
	var cookieLength = document.cookie.length;
	var i = 0;

	while (i < cookieLength)
	{
		var j = i + argLength;

		if (document.cookie.substring(i, j) == arg)
			return getCookieVal(j);

		i = document.cookie.indexOf(" ", i) + 1;

		if (i == 0)
			break;
	}

	return null;
}

// function used by getCookie(cookieName) to actually get the cookie data
function getCookieVal(offset)
{
	var endstr = document.cookie.indexOf (";", offset);

	if (endstr == -1)
		endstr = document.cookie.length;

	return unescape(document.cookie.substring(offset, endstr));
}

// function that deletes a cookie named [cookieName]
function deleteCookie(cookieName)
{
	var expirationDate = new Date();
	expirationDate.setTime(expirationDate.getTime() - 1);		// set the expiration date to yesterday
	var cookieValue = getCookie(cookieName);						// get the cookie data
	document.cookie = cookieName + "=" + cookieValue + "; expires=" + expirationDate.toGMTString();	// expire the new cookie
}

// function that returns true or false
function isCookie(cookieName)
{
	if (getCookie(cookieName))
		return 1;
	else
		return 0;
}

function capitalize(someString) {
	var newString = '', w;
	for (var i = 0; w = someString.split(' ')[i]; i++) {
		if ((w.toLowerCase() != "of") && (w.toLowerCase() != "u-m") && (w.toLowerCase() != "UofM") && (w.toLowerCase() != "um")) newString += w.substring(0, 1).toUpperCase() + w.substring(1, w.length).toLowerCase() + ' ';
		else newString += w + ' ';
	}
	newString = newString.substring(0, newString.length - 1);
	return newString;
}

function parseLinks(s) {   
  	var userAgent = navigator.userAgent;
	
	if (((userAgent.indexOf("MSIE") != -1) && (userAgent.indexOf("Mac") != -1)) || (userAgent.indexOf("Safari") != -1)) {
		// SAFARI DIFFERENCES:
		// 	- in the pattern linkpattern, the \v was removed. Mac recognizes the '_' with \v (vertical tab?).
		//		- the Mac apparently doesn't support a function inside the string.replace() method. Hence the function
		//		insdideReplaceFunction() above.
		var linkpattern = /\s(http:\/\/|ftp:\/\/|https:\/\/|www\.)([^ \,\:\)\(\"\'\<\>\f\n\r\t])+/g;
		
		while (linkpattern.test(s)) {
			testArray = linkpattern.exec(s);
			s = s.replace(linkpattern, insideReplaceFunction(testArray[0], testArray[1], testArray[2]));
		}
		return s;
	}
	else {
		var linkpattern = /\s(http:\/\/|ftp:\/\/|https:\/\/|www\.)([^ \,\:\)\(\"\'\<\>\f\n\r\t\v])+/g;
		return (
			s.replace(linkpattern, function ($0, $1, $2) {
				s = $0.substring(1,$0.length); 
				// remove trailing dots, if any
				while ((s.length > 0) && (s.charAt(s.length - 1) == '.')) s = s.substring(0, s.length - 1);
				
				reht = /(ht|f)tp:\/\//;
				if (reht.test(s)) return " <a target=\"blank\" href=\"" + s + "\">" + s + "</a>";
				else return " <a target=\"blank\" href=\"http://" + s + "\">" + s + "</a>";
			})
		);
	}
}

function highstyle() {
	var thePage = self.location.toString();
	var subpage = /(.+\/)(\w+\.html)/i;
	var tmp = subpage.exec(thePage);
	if (!tmp) var basePage = "index.html"; else var basePage = RegExp.$2;
	
	var theStyle = "default";
	if (thePage.indexOf("?style=default") != -1) {
		setStyle("default");
		deleteCookie("studentmatterscontrast");
		document.getElementById("link_stylechanger").href = basePage + "?style=high";
		document.getElementById("link_stylechanger").innerHTML = "High Contrast Version";
	}

	if (getCookie("studentmatterscontrast")) {
		theStyle = getCookie("studentmatterscontrast");
		setStyle("high");
		document.getElementById("link_stylechanger").href = basePage + "?style=default";
		document.getElementById("link_stylechanger").innerHTML = "Default Version";
	}
	
	if ((theStyle != "default") || (thePage.indexOf('?style=high') != -1)) {
		var thePage = (thePage.indexOf('?') != -1) ? thePage.substring(0, thePage.indexOf('?')) : thePage;
		setStyle("high");
		deleteCookie("studentmatterscontrast");
		setCookie("studentmatterscontrast", "high", 30);
		document.getElementById("link_stylechanger").href = basePage + "?style=default";
		document.getElementById("link_stylechanger").innerHTML = "Default Version";
	}
	else {
		setStyle("default");
	}
}

