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 emailpage() {
	var strEmailTo = prompt("Recipient's email address:","");
	if (strEmailTo == null) {
		alert("You must enter an email address.")
	} else {
		var strEmailSubject = document.title;
		var strEmailLink = document.location.href;
//		var strBody = document.body.innerHTML;
//		var bodyRegExp = /<!-- BEGIN MAIN BODY -->([.\s\S]*?)<!-- END MAIN BODY -->/;
//		var arrBody = strBody.match(bodyRegExp);
//		var strEmailBody = arrBody[1];
//		strEmailBody = "<h3>" + strEmailSubject + "</h3>" + strEmailBody;
//		var htmlBegRegExp = /<[^>]*>/g;
//		var htmlEndRegExp = /<\/[^>]*>/g;
//		strEmailBody = strEmailBody.replace(htmlEndRegExp,""); 
//		strEmailBody = strEmailBody.replace(htmlBegRegExp,"\n");
		strEmailBody = "Please see the following Web document...\n\n" + "Source:  National Institute of Diabetes & Digestive & Kidney Diseases (NIDDK)" + "\n" + "Title:      " + strEmailSubject + "\n" + "Link:      " + strEmailLink + "\n";
		var docMessage = "mailto:" + strEmailTo + "?subject=" + escape(strEmailSubject) + "&body=" + escape(strEmailBody);
		window.location = docMessage;
		window.focus();
	}
}

// cookie to maintain the state and to retrive for x number of days
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 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 printCurrentPage() 
{ 
	var sOption="toolbar=yes,location=no,directories=yes,menubar=yes,scrollbars=yes"; 
	var sWinHTMLSendToPrinter = "<a href='javascript:window.print()'><img src='/niddk.nih.gov/images/printTool.gif' alt='Send to Printer' width='16' height='13' border='0'></a>&nbsp;";
	sWinHTMLSendToPrinter += "<font class=fontSmall><a href='javascript:window.print()'>Send To Printer</a></font>";
	sWinHTMLSendToPrinter += "&nbsp;|&nbsp;<font class=screen><a href='javascript:window.close()'>Close</a></font><br><br>";	
	sWinHTMLSendToPrinter += "<img src='/niddk.nih.gov/images/WhiteTagline.gif' alt='The National Institute of Diabetes and Digestive and Kidney Diseases (NIDDK)'></font>";

	var sWinHTMLBody = document.getElementById('ContentBody').innerHTML; 	
	var printCurrent = window.open("","",sOption); 
	
	printCurrent.document.write('<html><link href=/niddk.nih.gov/css/olderBrowsers.css rel=stylesheet><body>'); 
	printCurrent.document.write(sWinHTMLSendToPrinter);
	printCurrent.document.write(sWinHTMLBody);
	printCurrent.document.write('</body></html>'); 
	printCurrent.document.close();
}

function addToFavorites()
{
	var url=document.location.href;
	var title = document.title;
	if (window.external)
	{
	window.external.AddFavorite(url,title)
	}
	else
	{ 
	alert("Sorry! Your browser doesn't support this function.");
	}
 }
window.onload = function(e) 
{
	var cookie = readCookie("TextSizeSelection");
	var title = cookie ? cookie : getPreferredStyleSheet();
	
	setActiveStyleSheet(title);
	
	//printCurrentPage();
}

window.onunload = function(e) 
{
	var title = getActiveStyleSheet();
	createCookie("TextSizeSelection", title, 30);
}

var cookie = readCookie("TextSizeSelection");
var title = cookie ? cookie : getPreferredStyleSheet();

setActiveStyleSheet(title);



