//***Code Is Intellectual Property Of CRM Metrix (Copyright 2000-2003).
//***Code Has Been Developed For The Sole Purpose Of CRM Metrix/Tide.com Project.
//***Licensed For CRMMetrix/Tide.com Project.


//********************
// Global Variables - Start
//********************

var testMode;
var entryRate;
var exitRate;
var runSurvey;
var cookieLifeSpan;
var watcherRunning;

// Turn survey on / off at global level
runSurvey = false;

// Set Test Mode = true when testing the application
testMode = false;

// Entry rate should be a number between 0 and 100, which indicates the percent of people who receive the entry (control) survey
// Exit rate should be a number between 0 and 100, which indicates the percent of people who receive the exit survey
// ****** Note:  Entry rate and Exit rate cannot added up to more than 100
entryRate = 6;
exitRate = 3;

// Cookie Life Span in Months
cookieLifeSpan = 6;


// Set watcherRunning to false by default  
watcherRunning = false;

//********************
// Global Variables - End
//********************


if (runSurvey)
// runSurvey - Start
{

  //Browser Sniffer
  var agt=navigator.userAgent.toLowerCase();
  var client=(agt.indexOf("msie"));


  //***Random Number Generated For Control Sample / Test Sample.
  var x;
  x = Math.random()*100;


  //***detn wether the user has entered thru a noncoded page
  var xstr;
  var tideReferrer = false;

  xstr = document.referrer;
  if (xstr.indexOf('tide.com',0) >= 1)
  {
    tideReferrer = true;
  }
  if (xstr.indexOf('clothesline.com',0) >= 1)
  {
    tideReferrer = true;
  }
  if (xstr.indexOf('atgwin2k',0) >= 1)
  {
    tideReferrer = true;
  }
  if (xstr.indexOf('divinecincinanti.net',0) >= 1)
  {
    tideReferrer = true;
  }


  //*************
  //***Exit Survey
  if (x > (100 - exitRate))
  {
    launchExitSurvey();
  }


  //****************
  //***Entry Survey
  if (!tideReferrer)
  {
    if (x < entryRate)
    {
      launchEntrySurvey();
    }
  }


  //****************
  //***NO Survey
  if ((x > entryRate) && (x < (100 - exitRate)))
  {
    noSurvey();
  }

// runSurvey - End
}


function launchEntrySurvey()
{
      var thiscookie;
      var s;

      thiscookie=document.cookie.split(";")

      for (i=0;i<thiscookie.length;i++)
      {
        if (thiscookie[i].split("=")[1]=="seen" && !testMode)
        {
          s=1;
        }
      }
           
      if (s!=1)
      {
        // Writing Cookie
        var expiredate;
        expiredate=new Date;
        expiredate.setMonth(expiredate.getMonth()+ cookieLifeSpan);
        document.cookie="popseen=seen;expires="+expiredate.toGMTString()+";path=/;";
        window.open('/CRMsurvey/crm_entry.jhtml','popentry','top=5,left=5,height=400,width=600,scrollbars=no,toolbars=no,location=no,resizable=yes');
      }
}

function launchExitSurvey()
{
    var thiscookie;
    var popSeen;

    thiscookie=document.cookie.split(";")

    for (i=0;i<thiscookie.length;i++)
    {
      if (thiscookie[i].split("=")[1]=="seen" && !testMode)
      {
        popSeen = true;
      }
    }

    if (!popSeen)
    {
      // Writing Cookie
      var expiredate;
      expiredate=new Date;
      expiredate.setMonth(expiredate.getMonth()+ cookieLifeSpan);
      document.cookie="popseen=seen;expires="+expiredate.toGMTString()+";path=/;";

      if (client>0)
        {win2=window.open('/CRMsurvey/crm_watch.jhtml','popstealth','height=4,width=4,scrollbars=no,toolbars=no,location=no,resizable=yes');}
      else
        {win2=window.open('/CRMsurvey/crm_watch.jhtml','popstealth','height=4,width=4,scrollbars=no,toolbars=no,location=yes,resizable=yes');}
      win2.blur();
      window.focus();
      watcherRunning = true;
	}

}

function noSurvey()
{
    var expiredate = new Date();
      expiredate.setMonth(expiredate.getMonth()+ cookieLifeSpan);

    // Writing the cookie , when the user sees no pop up.
    document.cookie="popseen=seen;expires="+expiredate.toGMTString()+";path=/;";
}

//*********************
// Kill Watcher Functionality - Start
//*********************

function killWatcher()
{
    if (watcherRunning)
    {
      //alert('killWatcher');
      win2 = window.open('','popstealth','height=4,width=4,scrollbars=no,toolbars=no,location=no,resizable=yes');
      win2.close();
    }
}

function setWatcherRunning()
{
    watcherRunning = true;
    //alert('watcherRunning');
    return true;
}

//*********************
// Kill Watcher Functionality - End
//*********************


//This function added to support leaving tide.com
//  and not giving exit survey
function proceedwithoutexit(strUrl)
{
	win2.close();
}
