function Set_Cookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
		( name != document.cookie.substring( 0, name.length ) ) )
	{
	return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function setActiveStyleSheet(title) {
  var i, a, main, eltitle;
  	Delete_Cookie( 'altsheet', '/', '');
	Set_Cookie( 'altsheet', title, '', '/', '', '');
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("stylesheet") != -1 && (eltitle = a.getAttribute("title"))) {
      a.disabled = true;
      if((eltitle == title) || (eltitle == "common") || (eltitle == "print"))
			a.disabled = false;
    }
  }
}

function setActiveStyleSheetPrint() {
  var i, a, main, eltitle;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("stylesheet") != -1 && (eltitle = a.getAttribute("title"))) {
      if((eltitle == title) || (eltitle == "common") || (eltitle == "print")) {
			if (a.disabled) a.disabled = false;
		} else {
			if (!a.disabled) a.disabled = true;
		}
    }
  }
}

function setActiveStyleSheetReload(title) {
  var i, a, main, eltitle;
  	Delete_Cookie( 'altsheet', '/', '');
	Set_Cookie( 'altsheet', title, '', '/', '', '');
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("stylesheet") != -1 && (eltitle = a.getAttribute("title"))) {
      if((eltitle == title) || (eltitle == "common") || (eltitle == "print")) {
			if (a.disabled) a.disabled = false;
		} else {
			if (!a.disabled) a.disabled = true;
		}
    }
  }
  window.location.reload( true );
}

function setTheRightStyle() {
   var allcookies = document.cookie;
	var pos = allcookies.indexOf("altsheet=");
	if (pos != -1) {
		var start = pos + 9;
		var end = allcookies.indexOf(";", start);
		if (end == -1) end = allcookies.length;
		var value = allcookies.substring(start, end);
		setActiveStyleSheet(value);
	}else{
		setActiveStyleSheet('standard');
	}
}

function popup(mylink, windowname){
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
		href=mylink;
	else
		href=mylink.href;
	winParms='width=700,height=500,scrollbars=yes,resizable=yes';
	if (navigator.appName.indexOf("Microsoft")>=0) {
		winParms+=',left=10,top=25'
	}else{
		winParms+=',screenX=10,screenY=25'
	}
	window.open(href, 'Window', winParms);
	return false;
}

// ref: http://javascript.about.com/library/scripts/blprintready.htm
function printSpecial() {
	var i, sTitle;

	if (document.getElementById != null) {
		var html = '<HTML>\n';
		
		html += '<HEAD>\n';
		
		html += '<TITLE>Hospital Elder Life Program Printable Document</TITLE>\n';
		html += '<link href="../stylesheets/print.css" rel="stylesheet" type="text/css" media="all">\n';
		html += '\n</HEAD>\n';
		html += '<BODY>\n';
		
		html += '<div class="normal">\n';
		
		var printReadyElem = document.getElementById("printReadyJr");
		if (printReadyElem != null){
			html += printReadyElem.innerHTML;
		} else {
			printReadyElem = document.getElementById("printReady");
			if (printReadyElem != null) {
				html += printReadyElem.innerHTML;
			} else {
				alert("No printable areas defined.");
				return;
			}
			printReadyElem = document.getElementById("copyrightNotice");
			if (printReadyElem != null) {
				html += printReadyElem.innerHTML;
			}
		}
		
      html += '\n</div>\n';
		html += '</BODY>\n';
		html += '</HTML>\n';
		
		var printWin = window.open("", "printdoc", "menubar=0,status=0,toolbar=1,resizable=1,scrollbars=1");
		
		printWin.document.open();
		printWin.document.write(html);
		printWin.document.close();

	} else {
		alert("Sorry, this feature is only available in modern browsers.");
	}
}

function removeAspan(spanID) {
	document.getElementById(spanID).innerHTML = "";
}

function printAspan(spanID, noPrintSpan) {
	var i, sTitle;

	if (document.getElementById != null) {
		var html = '<HTML>\n';
		
		html += '<HEAD>\n';
		
		html += '<TITLE>Hospital Elder Life Program Printable Document</TITLE>\n';
		html += '<link href="../stylesheets/print.css" rel="stylesheet" type="text/css" media="all">\n';
		html += '\n</HEAD>\n';
		html += '<BODY>\n';
		
		html += '<div class="normal">\n';
		
		document.getElementById(noPrintSpan).style.visibility = 'hidden';
		
		var printReadyElem = document.getElementById(spanID);
		
		if (printReadyElem != null){
			html += printReadyElem.innerHTML;
		}
		printReadyElem = document.getElementById("copyrightNotice");
		if (printReadyElem != null) {
			html += printReadyElem.innerHTML;
		}
		
      html += '\n</div>\n';
		html += '</BODY>\n';
		html += '</HTML>\n';
		
		var printWin = window.open("", "printdoc", "menubar=0,status=0,toolbar=1,resizable=1,scrollbars=1");
		
		printWin.document.open();
		printWin.document.write(html);
		printWin.document.close();
		
		document.getElementById(noPrintSpan).style.visibility = 'visible';

	} else {
		alert("Sorry, this feature is only available in modern browsers.");
	}
}

