// RedSheriff Survey Initiator v3.04
// COPYRIGHT 2001 Red Sheriff Limited

var caution = false
var wnd
var domain  = window.location.host
var htm

function setCookie(name, value, expires, path, domain, secure) {
   var curCookie = name + "=" + escape(value) +
   ((expires) ? "; expires=" + expires.toGMTString() : "") +
   ((path) ? "; path=" + path : "") +
   ((domain) ? "; domain=" + domain : "") +
   ((secure) ? "; secure" : "")
   if (!caution || (name + "=" + escape(value)).length <= 4000)
      document.cookie = curCookie
   else if (confirm("Cookie exceeds 4KB and will be cut!"))
      document.cookie = curCookie
}

function getCookie(name) {
   var prefix = name + "="
   var cookieStartIndex = document.cookie.indexOf(prefix)
   if (cookieStartIndex == -1)
      return null
      var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex +
      prefix.length)
      if (cookieEndIndex == -1)
         cookieEndIndex = document.cookie.length
         return unescape(document.cookie.substring(cookieStartIndex +
         prefix.length,
         cookieEndIndex))
}

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 fixDate(date) {
   var base = new Date(0)
   var skew = base.getTime()
   if (skew > 0)
      date.setTime(date.getTime() - skew)
}

function random() { 
   random.seed = (random.seed*random.a + random.c) % random.m; 
   return random.seed / random.m; 
}

function ScriptError(msg, url, lno) {
   var str;
   str = "Initiator script error: in " + url + " at line " + lno + "\n" + msg;
   alert(str);
}

window.onerror = ScriptError;

random.m=714025; 
random.a=4096; 
random.c=150889; 
random.seed = (new Date()).getTime()%random.m;

//-- Begin -- Survey initiator parameters
var samplingRate = 0;
var startType    = "onDomainExit";
var days         = 180;
var surveyUrl    = 'http://secure-au.imrworldwide.com/survey/re6002.htm';
var sFeatures   = "width=600,height=550,top=20,left=20,scrollbars=yes,resizable=no";
var delay        = 0;
var cookieName = escape("re6001");
var rsDomain	= ""
var rsPath	= "/"
var bChecked = false;
//-- End -- Survey initiator parameters

function IsSurveyCriteriaMet() {
   if( random() <= samplingRate ) {
      var invited = getCookie(cookieName);
      if(!invited) {
         var expiry  = new Date();
         fixDate(expiry);
         expiry.setTime(expiry.getTime() + (days * 24 * 60 * 60 * 1000))
         setCookie(cookieName, "1", expiry, rsPath, rsDomain);
         return 1;
      }
   }
   return 0;
}

function Try() {
   DoSurvey();
}

function DoSurvey() {
   window.open(surveyUrl, "", sFeatures);
}

function nop() {
   alert('nop');
}

function openPopup() {
   wnd = open("", "RedSheriffDOM", "width=50,height=50,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no,status=no", "replace=true");
   wnd.document.open("text/html");
   wnd.document.write(htm);
   wnd.document.close();
}

function CheckDomain() {
   htm = '<html>\n<head>\n<title>Popup<\/title>\n<\/head>\n<body bgcolor=\"\#FFFFFF\">';
   htm += "\n";
   htm += "\n<SCRIPT LANGUAGE=\"JavaScript\">";
   htm += "\nvar bdone=false;";
   htm += "\n";

   htm += "\nfunction closeWindow(){";
   htm += "\nwindow.close();";
   htm += "\n}";
   htm += "\n";

   htm += "\nfunction openSurvey(){";
   htm += "\nwindow.open(";
   htm += "\'" + surveyUrl + "\',";
   htm += '"",';
   htm += '"' + sFeatures + '");';
   htm += "\nsetTimeout(\"closeWindow();\",2000);";
   htm += "\n}";
   htm += "\n";

   htm += "\nfunction CheckDomain(url){";
   htm += "\nif(url.indexOf(" + '"' + domain + '")== -1){';
   htm += "\nopenSurvey();";
   htm += "\n}\n}";
   htm += "\n";

   htm += "\nfunction hide(){";
   htm += "\nwindow.blur();";
   htm += "\n}";
   htm += "\n";

   htm += "\nfunction errHandler(msg, url, ln){";
   htm += "\nif(bdone==false){";
   htm += "\nbdone=true;";
   htm += "\nopenSurvey();";
   htm += "\n}";
   htm += "\nreturn true;\n}"
   htm += "\n";

   htm += "\nfunction check(){";
   var idx = navigator.appVersion.indexOf("MSIE");
   var ie5 = false;
   var ie  = false;
   if( idx > 0 ) {
      ie = true;
      var ver = parseInt(navigator.appVersion.substr(idx+5));
      if( ver >= 5 ) {
         ie5 = true;
      }
   }
   if( ie5==true ) {
      htm += "\ntry{";
      htm += "\nCheckDomain(window.opener.top.location.href);";
      htm += "\nsetTimeout(\"check();\", 5000);";
      htm += "\n}";
      htm += "\catch(exception){";
      htm += "\n" + 'errHandler("","","");'
      htm += "\n}";
   }
   else {
      htm += "\nCheckDomain(window.opener.top.location.href);";
   htm += "\nsetTimeout(\"check();\", 2500);";
   }
   htm += "\n}";
   htm += "\n";

   htm += "\nfunction start(){";
   htm += "\nwindow.blur(); ";
   htm += "\nwindow.onfocus = hide; ";
   htm += "\nthis.window.onerror = errHandler;";
   htm += "\ncheck();"
   htm += "\n}";
   htm += "\n";

   htm += "\nstart();";
   htm += "\n<\/SCRIPT>";
   htm += "\n";
   htm += "\n<\/body>\n<\/html>";

   openPopup();
}

if( startType == "onEntry" ) {
   if( IsSurveyCriteriaMet() == 1 ) {
      Try();
   }
}
else if( startType == "onPageExit" ) {
   if( IsSurveyCriteriaMet() == 1 ) {
      window.onunload = Try;
   }
}
else if(startType == "onDomainExit") {
   if( IsSurveyCriteriaMet() == 1 ) {
      CheckDomain();
   }
}
