/* SEARCH NAVIGATION FUNCTIONS */

function clickClear(thisField, defaultText) {
	if (thisField.value == defaultText) {
		thisField.value = "";
	}
}

function clickRecall(thisField, defaultText) {
	if (thisField.value == "") {
		thisField.value = defaultText;
	}
}

function verifySearchTerm(thisField,defaultText) {
	var searchUrl;

	if (thisField.value == defaultText) {
		location.href= "http://content.nejm.org/search.dtl";
		return false;
	}
	else {
		searchUrl = "http://content.nejm.org/cgi/search?andorexactfulltext=and&andorexacttitleabs=and&excludeflag=TWEEK_element&search_tab=articles&searchtitle=Articles&hits=20&searchterm=" +
                    escape(thisField.value) +
                    "&sendit=GO&where=fulltext&sortspec=Score+desc+PUBDATE_SORTDATE+desc&fmonth=Nov&fyear=1996&tocsectionid=Original+Articles&tocsectionid=Special+Reports&tocsectionid=Special+Articles&tocsectionid=Videos+in+Clinical+Medicine&tocsectionid=Clinical+PracticeAORBClinical+Therapeutics&tocsectionid=Review+ArticlesAORBClinical+PracticeAORBClinical+Implications+of+Basic+ResearchAORBMolecular+MedicineAORBClinical+TherapeuticsAORBVideos+in+Clinical+Medicine&tocsectionid=EditorialsAORBPerspectiveAORBOutlookAORBBehind+the+Research&tocsectionid=Sounding+BoardAORBClinical+Debate&tocsectionid=Clinical+Implications+of+Basic+Research&tocsectionid=Health+Policy+ReportsAORBHealth+Policy+2001AORBQuality+of+Health+Care";
		location.href = searchUrl;
		return false;
	}
}

/* END SEARCH NAVIGATION FUNCTIONS */


/* INDIA ACCESS FUNCTIONS */

// The following two country code and institution states
// will cause the window to pop.
var triggerCountryCode = "IN";
var triggerInstitutionState = false;

// The following two variables provide the URL of the page
// to display and any special options.
var windowUrl = "http://www.nejm.org/india/info/pop.html";
var windowOptions = "width=515,height=425,resizable=yes,scrollbars=no,status=no,menubar=no,directories=no,toolbar=no";

// Pop up info window
function popInfoWindow(countryCode,isInstitution) {
    var infoWin;
    
    // If client accepts cookies    
    // and cookie is not set
    // and India and not institution
    if (cookiesAreEnabled()
        && getCookie("suppressInfoWindow") == null 
        && countryCode == triggerCountryCode
        && isInstitution == triggerInstitutionState) {
        
        // Set session cookie
        setCookie("suppressInfoWindow","true",null,"/","nejm.org");
        
        // If window does not exist or is closed,
        // pop it.
	    if (!infoWin || infoWin.closed) {
		    infoWin = window.open(windowUrl, "infoWin", windowOptions);
	    }
	    else { // Otherwise, focus on it
		    infoWin.focus();
	    }
    }
}

// Change cookie state, depending upon state of passed-in checkbox.
function changeCookieState(checkBox,cookieName) {
    if (checkBox.checked) {
	    setCookie(cookieName,"true","Wed, 01-Jan-2020 00:00:01 GMT","/","nejm.org");
	}
	else {
	    setCookie(cookieName,"true",null,"/","nejm.org");
	}
}

// Checks if cookies are enabled in browser.
function cookiesAreEnabled() {
  setCookie( "foo", "bar" );
  if ( getCookie( "foo" ) ) {
    deleteCookie( "foo" );
    return true;
  } else {
    return false;
  }
}

function getCookie( name ) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while ( i < clen ) {
    var j = i + alen;
    if ( document.cookie.substring(i, j) == arg ) return getCookieVal(j);
    i = document.cookie.indexOf( " ", i ) + 1;
    if ( i == 0 ) break;
  }
  return null;
}

function deleteCookie( name, path, domain ) {
  if ( getCookie( name ) ) {
    document.cookie = name + "=" +
    ( ( path ) ? "; path=" + path : "" ) +
    ( ( domain ) ? "; domain=" + domain : "" ) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function setCookie( name, value, expires, path, domain, secure ) {
  document.cookie = name + "=" + escape (value) +
  ( ( expires ) ? "; expires=" + expires : "" ) +
  ( ( path ) ? "; path=" + path : "" ) +
  ( ( domain ) ? "; domain=" + domain : "" ) +
  ( ( secure ) ? "; secure" : "" );
}

/**
 *   Helper function for getCookie()
 */
function getCookieVal( offset ) {
  var endstr = document.cookie.indexOf ( ";", offset );
  if ( endstr == -1 ) endstr = document.cookie.length;
  return unescape( document.cookie.substring( offset, endstr ) );
}

/* END INDIA ACCESS FUNCTIONS */