// Eurisko - IBM Javascript
// Version 1.1 // Date: January, 21st 2002
// Revised on December, 11th 2003
//
// JS operation: 
// 1) Check for browser capabilities of storing cookies 
//    Store and get an empty cookie just to be sure 
// 
// 2) Look for 'msp' cookie 
// 3) if not found and if random number is greater than fixed trigger -> link to pupLoc opening a new window

	
// *************************************
// Function to get the value of a cookie
// *************************************
  
function getCookieValue(cookieName)
{
   var cookieValue = document.cookie;
   var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");

   if (cookieStartsAt == -1) {
      cookieStartsAt = cookieValue.indexOf(cookieName + "=");
   }

   if (cookieStartsAt == -1) {
      cookieValue = null;
   } else {
      cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
      var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
      if (cookieEndsAt == -1) {
         cookieEndsAt = cookieValue.length;
      }
      cookieValue = unescape(cookieValue.substring(cookieStartsAt,cookieEndsAt));
   }

   return cookieValue;
}

// ****************************************************
// Function to set a cookie value and a expiration date
// ****************************************************
function SetCookie (name,value,expires,path,domain,secure)
{
	document.cookie = name + "=" + escape (value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

  
function OLD_setCookie(cookieName, cookieValue, cookiePath, cookieExpires)
{
   cookieValue = escape(cookieValue);

   if (cookieExpires == "")
   {
      var nowDate = new Date();
      nowDate.setMonth(nowDate.getMonth() + 6);
      cookieExpires = nowDate.toGMTString();
   }

   if (cookiePath != "")
   {
      cookiePath = ";Path=" + cookiePath;
   }

   document.cookie = cookieName + "=" + cookieValue + 
      ";expires=" + cookieExpires + cookiePath;
}

// ***************************
// Function to delete a cookie
// ***************************
  
function DeleteCookie (dcname) { 
  
  var exp = new Date(); 
  exp.setTime(exp.getTime() - 1); 
  var cval = ReadCookie (dcname); 
  document.cookie = dcname + "=" + cval + "; expires=" + exp.toGMTString(); 
}

function ReadCookie(dcname) {
  var dcexist = dcname + "="; 
  var dc = document.cookie; 
  if (dc.length > 0) { 
    offset = dc.indexOf(dcexist); 
    if  (offset != -1) { 
      offset += dcexist.length; 
      end = dc.indexOf(";", offset); 
      if (end == -1)  end = dc.length;  
      return unescape(dc.substring(offset,  end));
    }
  } 
  return null; 
}

// ******************************
// Function to link to PerlScript
// ******************************
  
function PUP(CountryCode,LanguageCode,eSite,ShowPopTrigger) { 
  
  var rdnum = Math.random();
  var attr ="toolbar=no,menubar=no,scrollbars=yes,resizable=yes,width=570,height=430";

	if( site == "cvm" ){
		attr="toolbar=no,menubar=no,scrollbars=yes,resizable=yes,width=570,height=318";
	}

// Test if user's browser is enabled to receive cookies
 
  var testname = "IBMSurveyTest";
  var testdate = new Date();
  testdate.setTime( testdate.getTime() + (5 * 60 * 1000) ); // set for 5 minutes (arbitrary)

  SetCookie( testname, "isEnabled", testdate, "../default.htm", ".ibm.com", "" );

  if (ReadCookie( testname ) != null)  {
    DeleteCookie( testname );

// Test if survey's cookie is already present
	var valCookie = ReadCookie('msp');
	
	if (valCookie == null && rdnum <  ShowPopTrigger) {		

		pupLoc = "../../https@www.ibm.com/default.htm";

		if( (typeof(staging) != "undefined") && (staging == "yes")){
			pupLoc = "../../wwwstage.ibm.com/default.htm";
		}

	  pupLoc += "../cgi-bin/survey/esites/popup.pl@lang=" + LanguageCode + "&country=" + CountryCode + "&site=" + eSite;

	  if( (typeof(version) != "undefined") && (version != null) ){ 
		pupLoc += "&version=" + version;
	  }

	  if( (typeof(module) != "undefined") && (module != null) ){ 
		pupLoc += "&module=" + module;
	  }
	  
//	  Add escaped URL information to the pupLoc value		
	  var parent = location.href;
	  parent = escape(parent);
	  pupLoc += "&URL=" + parent;
	 
	  zview=window.open(pupLoc,"view",attr); 
	}	
  }
}






// **********************
// Main
// **********************

PUP( country, language, site, rate );


