/***************************************************************
**** PUBLIC DECLARATIONS -- PLEASE USE THIS METHODS ONLY  
**** 1. survey_immediate - pops up the survey immediately 
**** 2. survey_onexit    - pops up the survey upon exit of the page
****
**** Variables 
**** 1. name   - Survey Name/unique ID
**** 2. expire - Cookie expiration by month
**** 3. study  - Survey URL
**** 4. height - Height of window
**** 5. width  - Width of window
**** 6*4. sample - Sample Size -- Implementation of sample size in this survey_onexit
***************************************************************/
function survey_immediate(name,expire,study) { psurvey_internal(name,expire,study,"IMMEDIATE",".doubleclick.net"); }
function survey_onexit(name,expire,study,sample) { 
   if (sample!= null) DCLKsampleRate = sample;
   psurvey_internal(name,expire,study,"EXIT",".doubleclick.net","0",380,620); 
}

function survey_onexit_external(name,expire,study,sample) { 
   if (sample!= null) DCLKsampleRate = sample;
   psurvey_internal(name,expire,study,"EXIT","","0",380,620); 
}

function survey_popup(name,expire,study,height,width) { 
	psurvey_internal(name,expire,study,"IMMEDIATE",".doubleclick.net","1",height,width); 
}

/***************************************************************
***** DO NOT USE
***** INTERNAL USE ONLY
***************************************************************/
function survey_debug(name,expire,study, domain) { psurvey_internal(name,internal,"EXIT", domain); } 

/***************************************************************
**** PAGE VARIABLE DECLARATIONS
***************************************************************/

var DCLKcookExpire;		// Cookie Expiration
var DCLKsampleRate = 100;	// Sample Rate - Unused
var DCLKstudyName;		// Study Name
var DCLKcookie; 		// Cookie Name
var DCLKstudyUrl;		// Study URL
var DCLKsampled;		// Sampled Already?
var DCLKonexit;			// On Window Exit Functionality
var DCLKonunload_orig = window.onunload; 	// Original onunload saved


/***************************************************************
**** PRIVATE SURVEY DECLARATIONS
***************************************************************/

function psurvey_internal(name,expire,study, survey_type, survey_domain, under, height,width) {
  DCLKcookie="DCLK"+name;
  DCLKcookExpire   = expire;
  DCLKstudyName =name;
  DCLKstudyUrl	=study;
  if( DCLKsampled ) return;
  var agt=navigator.userAgent.toLowerCase();
  if ( agt.indexOf("msie 3") != -1 ) return;	
  if ( (agt.indexOf("msie") >= 0 && agt.indexOf("mac") >= 0) ) return; 
  if ( agt.indexOf("x11") != -1 ) return;		
  if ( agt.indexOf("mozilla/2") != -1 ) return;
  if ( parseInt(navigator.appVersion) <= 2) return;
  psurvey_function(survey_type, survey_domain,under, height,width);
  
}

function psurvey_function(survey_type, survey_domain, under, height,width) {
  if (pcookie_defined(DCLKcookie) )   return;
  // alert(Math.random()*100);
  // alert(DCLKsampleRate);
  if ( ( Math.random() * 100 ) > DCLKsampleRate ) return;
  DCLKsampled = 1; 
  pcookie(DCLKcookie,1, psurvey_month_add(new Date(), DCLKcookExpire), survey_domain );
 
  var tset = "scrollbars=1,titlebar=1,resizable=1,";
  if (height != null) tset +="height="+height;
  if (width != null) tset +=",width="+width;

  var script_i = psurvey_string(tset);
  DCLKonexit=script_i;
  psurvey_aexit(script_i);
  if ( survey_type == "IMMEDIATE" ) { eval(script_i); }
  else if ( survey_type == "EXIT" ) window.onunload=psurvey_onexit;
  if ( under == "1" ) self.focus();
}

function psurvey_aexit(script) {
  if ( DCLKonunload_orig == null ) return; 
  var t_unload = DCLKonunload_orig.toString();
  var index1 = t_unload.indexOf("{") + 1;
  var index2 = t_unload.lastIndexOf("}");
  if ( index1 == -1 ) return;
  if ( index2 == -1 ) return;
  DCLKonexit += t_unload.substring(index1, index2);
}

function psurvey_month_add(obj, expLength) {
  obj.setTime(obj.getTime()+expLength*30*24*60*60*1000);
  return obj.toGMTString();  
}

function psurvey_string(settings) { return "window.open(DCLKstudyUrl, 'DCLKsurvey','"+settings+"');";  }
function psurvey_onexit() { eval(DCLKonexit ); }
function psurvey_display(s){ eval(psurvey_string(s)); }


//** TEST RESET THE COOKIE THAT'S IN THE SURVEY 
function survey_reset(cookie) { 
  window.onunload = DCLKonunload_orig;
  DCLKsampled = 0; 
  pcookie_expire("DCLK"+cookie); 
  pcookie("DCLK"+cookie,"","1/1/1980",".doubleclick.net"); 
}



/***************************************************************
**** GENERAL COOKIE FUNCTIONS - CAREFUL TO USE
***************************************************************/

function pcookie(name, value, expire, domain) {
  var t_cookie = name+"="+value+";";
  t_cookie += "expires="+expire+";";
  t_cookie += "path=/;";
  if ( domain != null && domain.length != 0 ) t_cookie += "domain="+domain+";";
  document.cookie=t_cookie;
}

function pcookie_expire(name) { pcookie(name,"","1/1/1980"); }

function pcookie_retrieve(name) {
  var tA = document.cookie.split(';');
  for ( var i = 0; i < tA.length; ++i ) {
    var item = tA[i];
    if ( item.charAt(0) == ' ' ) item = item.substring(1);
    var x = item.indexOf(name+"=");
    if ( x == 0 )  { return item.substring(item.indexOf("=")+1); }
  }
  return "";
}

function pcookie_defined(name) { 
  if ( pcookie_retrieve(name) == "" ) return 0;
  return 1;
} 
