/* SEARCH NAVIGATION FUNCTIONS */

function clickClear(thisField, defaultText) {
	if (thisField.value == defaultText) {
		thisField.value = "";
		thisField.style.color = "#000";
	}
}

function clickRecall(thisField, defaultText) {
	if (thisField.value == "") {
		thisField.value = defaultText;
		thisField.style.color = "#000";
	}
}

// for SLI search forms

function verifySearch(thisField,defaultText) {
	var searchUrl;

	if (thisField.value == defaultText || thisField.value == "Keyword, citation, or author") {
		location.href= "http://search.nejm.org";
		return false;
	}
	else {
        return true;
	}
}

/* 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 */

