
function invite(inviteurl,frmWidth,frmHeight,svyName,expire) {
//********************************************************************
// Display invitation if specific cookie not found  and set cookie
// Parameters
// inviteurl: URL of invite form
// frmWidth & frmHeight: dimensions of invitation window
// svyName: Name of the specific survey
// expire: Number of days before cookie expires
// 
// Created by Chris Miceli 2/25/2004
//********************************************************************

		// define variables
		var theCookie = document.cookie;
        var today = new Date();
        var expDate = new Date();

	   // Check if user has visited page before
	   if (theCookie.indexOf(svyName) == -1) {
 
        expDate = whenIs(today, expire);
		expDate = expDate.toGMTString();
	   	document.cookie = svyName + "; path=/; expires=" + expDate + ";";
		window.open(inviteurl, "", "toolbar=no,status=no,width=" + frmWidth + ",height=" + frmHeight + ",top=150,left=50");
		//window.open(surveyurl, "", "toolbar=no,status=no,width=270,height=220,top=150,left=50");

	   }
}

function invite(inviteurl,frmWidth,frmHeight,svyName,expire,path) {
//********************************************************************
// Display invitation if specific cookie not found  and set cookie
// Parameters
// inviteurl: URL of invite form
// frmWidth & frmHeight: dimensions of invitation window
// svyName: Name of the specific survey
// expire: Number of days before cookie expires
// path: cookie path
// 
// Created by Chris Miceli 2/25/2004
//********************************************************************

		// define variables
		var theCookie = document.cookie;
        var today = new Date();
        var expDate = new Date();

	   // Check if user has visited page before
	   if (readCookie(svyName) == null) {
		
			createCookie(svyName, svyName, expire);

			window.open(inviteurl, "", "toolbar=no,status=no,width=" + frmWidth + ",height=" + frmHeight + ",top=150,left=50");
	   }
}

function whenIs(anyDate, n){
	//-- Returns the date that is n days from any date.
	var newDate = new Date();
	newDate.setTime(anyDate.getTime()+(n*1000*60*60*24));
	return newDate;
}

//********************************************************************
// Display invitation if specific cookie not found  and set cookie
// Parameters
// Display invitation every nthe hit on page, every 5th customer or every 163rd customer.  
// Created by SFungafat , Modified RKumar June 2004
//********************************************************************
function intercept(surveyurl) {
	// define variables
	 var cookie_ls = document.cookie;
         var date_ls = new Date();
         var S = new Date();

	   // if not yet sampled
	   if (cookie_ls.indexOf("NEJM062004WEB") == -1) {
		  rnd.today=new Date();
		  rnd.seed=rnd.today.getTime();
 
                 function whenIs(anyDate, n){
                 //-- Returns the date that is n days from any date.
                    var newDate = new Date();
                    newDate.setTime(anyDate.getTime()+(n*1000*60*60*24));
                    return newDate;
                  }

		  function rnd() {
		  	rnd.seed = (rnd.seed*9301+49297) % 233280;
		      return rnd.seed/(233280.0);
		                 };

		  function rand(number) {
		  	return Math.ceil(rnd()*number);
		  					};   

		  sample=rand(5);  // for the sample grab 1 in 5
		  if (sample==5) {
 	                S = whenIs(date_ls, 3);
	                S = S.toGMTString();
	   	        document.cookie = "NEJM062004WEB; path=/; expires=" + S + ";";
window.open(surveyurl, "", "toolbar=no,status=no,width=600,height=300,top=150,left=50")
		          	}
	   }
}

//************************************************
// Set pop-up cookie based on country and IP type
// Parameters
// country: two character country code
// instFlag: NEJM Institution, 1=True, 0=False
// ** Use ISO 3166 standard 2-letter country codes **
//************************************************
function intlSurvey(country, instFlag)
{
/*
	var validCtys = "GB";
	if (cookiesAreEnabled())
	{
		country = country.toUpperCase()
		
		// Invite only displayed for valid countries
		if (country.indexOf(validCtys) > -1)
		{
			if (instFlag == 1)
			{
				//Set cookie one day for NEJM Institutions
				invite('http://survey.nejm.org/NEJMUK0608/popup.aspx',510,260,'NEJMGBINST',1, 'nejm.org');
			}
			else
			{
				//45 days for all others
				invite('http://survey.nejm.org/NEJMUK0608/popup.aspx',510,260,'NEJMGBNONINST',45, 'nejm.org');
			}
		
		}
	}
*/

}

//************************************************
// Set pop-up cookie based on country. 
// Parameters
// userCty: Country user from
// validCty: Country or countries to test against
// inviteurl: URL of invite form
// svyName: Name of the specific survey
// expire: Number of days before cookie expires
// frmWidth & frmHeight: dimensions of invitation window
//************************************************
function surveyByCty(userCty, validCty, inviteurl, svyName, expire, frmWidth, frmHeight)
{
	if (cookiesAreEnabled())
	{
		userCty = userCty.toUpperCase()
		validCty = validCty.toUpperCase()
		
		// Invite only displayed for valid countries
		if (userCty.indexOf(validCty) > -1)
		{
				//Display invite
				invite(inviteurl, frmWidth, frmHeight, svyName, expire);
		}
	}
}


// Checks if cookies are enabled in browser.
function cookiesAreEnabled() {
  setCookie( "foo", "bar" );
  if ( getCookie( "foo" ) ) {
    deleteCookie( "foo" );
    return true;
  } else {
    return false;
  }
}

function setCookie( name, value, expires, path, domain, secure ) {
  document.cookie = name + "=" + escape (value) +
  ( ( expires ) ? "; expires=" + expires : "" ) +
  ( ( path ) ? "; path=" + path : "" ) +
  ( ( domain ) ? "; domain=" + domain : "" ) +
  ( ( secure ) ? "; secure" : "" );
}

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;
}

/**
 *   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 ) );
}

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 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 {
        var 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;
}