	
   	// ////////////////////////////////////////////////////////////
   	// Cookie functions
   	// ////////////////////////////////////////////////////////////
	
	function setCookie(name,value) {
		var c = name + "=" + value + "; path=/";
		document.cookie = c;
	}
	
	function getCookie(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 deleteCookie(name) {
		// set cookie expires to the past.
		var expires = new Date();
		expires.setTime(expires.getTime() - 1);
		document.cookie = 
			name + "=; path=/; expires=" + expires.toGMTString();
	}
	
   	// ////////////////////////////////////////////////////////////
   	// Globals
   	// ////////////////////////////////////////////////////////////

	// The application lives at the /utd path in the development enviromnent
	// but / path on a server.  Modify the URLs as appropriate.  
	// A better way might be to set this in the application context within 
	// Java and read it here.
	if (document.location.href.indexOf("/utd/")>0){
	   	var surveyWindowUrl = '/utd/online/feedback/survey.do';
	   	var blankWindowUrl = '/utd/emptySurvey.jsp';
	} else {
	   	var surveyWindowUrl = '/online/feedback/survey.do';
	   	var blankWindowUrl = '/emptySurvey.jsp';
	}

   	var surveyWindowName = 'Survey';
   	var surveyWindowDecoration = 
			'dependent=no,'
			+ 'location=no,directories=no,status=yes,'
			+ 'menubar=no,scrollbars=yes,resizable=yes';

	// Cookie names
	var surveyStateCookieName = "SurveyState";
	var surveyIdCookieName = "surveyId";
	
   	// States.
   	var nullState      = null;
   	var blankState     = "blank";   // Blank and waiting to be loaded or closed.
   	var loadingState   = "loading"; // About to be loaded.
   	var pendingState   = "pending"; // Survey to be given, but window closed.
   	var waitingState   = "waiting";	// Window loaded and watching.
   	var openedState    = "opened";  // opened to user.
   	var completedState = "completed";
   	var declinedState  = "declined";
   	var closingState   = "closing";
   	
   	// ////////////////////////////////////////////////////////////
   	// Statefunctions
   	// ////////////////////////////////////////////////////////////
   	
   	function getState() {
   		var c = getCookie(surveyStateCookieName);
   		if (c == null) {
   			return null;
   		}
   		return new String(c);
   	}

   	function setState(state) {
   		if (state == null) {
   			deleteCookie(surveyStateCookieName);
   		} else {
   			setCookie(surveyStateCookieName,state);
   		}
   	}

   	// ////////////////////////////////////////////////////////////
   	// Functions for search and toc screens.
   	// ////////////////////////////////////////////////////////////

	function searchOnClick() {
		var state = getState();
		var surveyId = getCookie(surveyIdCookieName);
		if (surveyId != null) {
			if (   state != completedState
			    && state != declinedState
			    && state != openedState) {
				if (isProfessionalUrl()) {
					openBlankWindow();
				}
				window.focus();
			}
		}
	}
   	// ////////////////////////////////////////////////////////////
   	// Functions for topic display window.
   	// ////////////////////////////////////////////////////////////

	function topicOnLoad() {
		var state = getState();
		if (state == loadingState) {
			openSurveyWindow();
		}
	}
	
	function topicOnUnload() {
	
	}

   	// ////////////////////////////////////////////////////////////
   	// Functions for survey window manipulation
   	// ////////////////////////////////////////////////////////////
	
	function openBlankWindow() {
		setState(blankState);
		var newwin = window.open(
				blankWindowUrl, 
				surveyWindowName, 
				surveyWindowDecoration
			);
		window.focus();
		newwin.blur();
	}

	function isProfessionalUrl(){
		if (document.location.href.indexOf("/utd/online/")>0
			|| document.location.href.indexOf("/online/")>0){
			return true;
		} else {
			return false;
		}
	}
	
	function openSurveyWindow() {
		var newwin = window.open(
				surveyWindowUrl, 
				surveyWindowName, 
				surveyWindowDecoration
			);
		newwin.blur();
		window.focus();
		return newwin;
	}

   	// ////////////////////////////////////////////////////////////
   	// This is called from the menu options:
   	// 		- New Search
   	//		- Table of Contents
   	//		- My UpToDate
   	// It opens a survey window if appropriate.
   	// This handles the case of a user becoming active in a survey
   	// by clicking on See Links - ie - there is no survey window
   	// opened by the TOC/Results page.  If the survey window is
   	// there it will bring itself into focus automatically.

	function topic_checkForPendingSurvey() {
		var state = getState();

		if (state != null && state == pendingState) {
			openSurveyWindow();
		}
	}
   	
   	// ////////////////////////////////////////////////////////////
   	// Functions for survey display window.
   	// - The window is originally "blur"ed in the background.
   	// - The checkParent method is called every second to determine
   	//   whether the parent window is still open and viewing a 
   	//   topic.  If not, then this window claims focus.
   	// - The SurveyWindow cookie is used to signal to others that
   	//   this window is loaded and ready to go.
   	// ////////////////////////////////////////////////////////////

   	function surveyPageOnLoad() {
   		// current state is loadingState.
		setState(waitingState);
	    checkParent();
    }
    
	function surveyPageOnUnload() {
		var state = getState();
		if (   state == openedState
			|| state == waitingState) {
			// User has closed window.
			setState(declinedState);
		}
    }

    function checkParent() {
		document.title = new Date();
		var ref = "?";
		try {
			ref = window.opener.location.href;
		} catch (e) {
		}
		
		if (ref.indexOf('topic.do') < 0) {
			setState(openedState);
			window.resizeTo(800, 600);
			window.focus();
		} else {
			setTimeout("checkParent()",1000);
		}
    }
    
    function blankSurveyCheckSelf() {
		var state = getState();
		var surveyId = getCookie(surveyIdCookieName);
		if (state == closingState || surveyId == null || surveyId == "0") {
			setState(null);
			window.close();
		} else {
			setTimeout("blankSurveyCheckSelf();", 1000);
		}
	}

   	// ////////////////////////////////////////////////////////////
    