﻿// ------------ Global variables -----------------------
var urlPrefix = 'http://t.pollg.com/g34292';	
var surveyId = ''; // leave empty, generated later or read from cookie											
var surveyUrl = ''; // leave empty, generated later
var btn_TakeSurvey = ''; // leave empty, generated later

//uncomment only if all sites should use same cookie
//var cookieName = 'dlrmCookie';

var screenWidth = screen.width; 												
var screenHeight = screen.height; 												
												
var windowWidth ="400";												
var windowHeight ="300";												
												
var windowTop = "30";												
var windowLeft = "30";

// how many times popup is opened, maximum
var maxOpenTimes = 1;	
//how many times popup is opened, counter
var timesLaunched = 0;												
// ------------------------------------------------------


function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function generateId() {
   s1 = urlSiteId.toLowerCase();
   s2 = s1.substring(s1.length-5,s1.length);
   s = s2.replace('_', '');
   return (s+S4()+S4()+S4()+S4()+S4()+S4()+S4()+S4()+S4());
}
												
// Close popup window
function closePopup() {												
  document.getElementById("pop_up_HI").style.visibility="hidden";												
  document.getElementById("iframe_HI").style.visibility="hidden"; 												
}												

// Update cookie when survey is accepted in order to not give the survey again after taken once.
function surveyTaken(cookieName) {												
incrementCounter(maxOpenTimes);
}												

// Generate popup window (iframe)												
function generatePopup(){												
if (document.getElementById) {												
  surveyUrl = urlPrefix +urlSiteId+ '.asp?NID=' + surveyId;
  btn_TakeSurvey = "<a href=javascript:centerPopup(surveyUrl,800,650,'status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=yes');closePopup();surveyTaken(cookieName);><img src='" +acceptImage+ "' class='image' title='" +acceptTitle+ "'></a>";												
  document.write('<style type="text/css">')
  document.write('body, table, tr, td {')												
  document.write('margin:0px;')
  document.write('padding:0px;}')
  document.write('a:link, a:visited, a:hover, a:active {')												
  document.write('text-decoration:none;}')												
  document.write('table, tr, td {')												
  document.write('border-collapse:collapse;}')													
  document.write('table.popup {')												
  document.write('margin: 0px;')
  document.write('border: 0px;')											
  document.write('padding: 0px;}')												
  document.write('.image {')												
  document.write('border: 0px;}')												
  document.write('#tablecorps {')												
  document.write('background-image:url("' +bgImage+ '");')												
  document.write('margin:0px;')
  document.write('font-family:arial;')
  document.write('font-size:12px;')
  document.write('font-family:arial;')
  document.write('color:#000000;}')
  document.write('.espace {')												
  document.write('padding-left: 20px;')
  document.write('padding-right: 20px;}')													
  document.write('</style>')
												
  document.write('<iframe id="iframe_HI" width="'+windowWidth+'" height="'+windowHeight+'" STYLE="position:absolute; top:-100px; left:'+windowLeft+'%; visibility:hidden; z-index:987;"></iframe>') 												
  document.write('<DIV id="pop_up_HI" STYLE="position:absolute; top:-100px; left:'+windowLeft+'%;visibility:visible; z-index:988;">')												
  document.write('<table width="'+windowWidth+'" height="'+windowHeight+'" cellspacing=0" cellpadding="0" class="popup" align="center" id="tablecorps">') 												
  document.write('<tr height="24">')
  document.write('<td align="right" style="padding-right:5px;" colspan="2"><img src="' +wCloseImage+ '" title="' +wCloseTitle+ '" onClick="closePopup();"></td>')
  document.write('</tr>') 
  document.write('<tr height="70">')												
  document.write('<td align="left" valign="middle" colspan="2" class="espace"><img src="' +logoImage+ '" title=' +logoTitle + '></td>') 												
  document.write('</tr>') 												
  document.write('<tr height="206">')												
  document.write('<td class="espace" valign="top">' +popupText+ btn_TakeSurvey+'</td>')												
  document.write('</tr>')												
  document.write('</table>')												
  document.write('</div>')												
 }												
}

function centerPopup(page,w,h,options) {
  var top=(screen.height-h)/2;
  var left=(screen.width-w)/2;
  window.open(page,"","top="+top+",left="+left+",width="+w+",height="+h+","+options);
  document.getElementById("pop_up_HI").style.visibility="hidden";
  document.getElementById("iframe_HI").style.visibility="hidden"; 
}												


function getCookie(NameOfCookie){												
    if (document.cookie.length > 0) {              												
    begin = document.cookie.indexOf(NameOfCookie+"=");       												
    if (begin != -1) {           												
      begin += NameOfCookie.length+1;       												
      end = document.cookie.indexOf(";", begin);												
      if (end == -1) end = document.cookie.length;												
        return unescape(document.cookie.substring(begin, end));												
    } 												
  }												
  return null;												
}												

function setCookie(NameOfCookie, value, expiredays) {												
  //alert('setCookie, value: ' +value);
  var ExpireDate = new Date ();												
  ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));												
  document.cookie = NameOfCookie + "=" + escape(value) + 												
  ((expiredays == null) ? "" : "; expires=" +ExpireDate.toGMTString()+ "; path=" +cookiePath+ "; domain=" +cookieDomain);												
  }												
												
function delCookie (NameOfCookie) {												
  if (getCookie(NameOfCookie)) {												
    document.cookie = NameOfCookie + "=" +												
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";												
  }												
}	

function openPopup(count, max)												
{
 incrementCounter(1);												
 if (count<max) {												
   generatePopup();												
 }
 else {
  //alert('already opened max times, do not open');
 }
												
}												

// Updates popup launch time counter to the cookie
function incrementCounter(step)												
{
  arrData = getCookie(cookieName).split('|');
  t = eval(arrData[1]);
  //t += 1;
  t = t+step;
  arrData[1] = t;
  strData = arrData.join("|");
  setCookie(cookieName,strData,30); 												
}												


// If cookie already exist, read surveyId from cookie
if(getCookie(cookieName) != null){
  //alert('cookie exist');
  arrData = getCookie(cookieName).split('|');
  surveyId = arrData[0];
  timesLaunched = arrData[1];
  openPopup(timesLaunched,maxOpenTimes);
}
//create new cookie
else {
  //alert('no cookie');
  //generate new surveyId
  surveyId = generateId();
  var ExpireDate = new Date (); 												
  ExpireDate.setTime(ExpireDate.getTime() + (30 * 24 * 3600 * 1000));												
  document.cookie = cookieName + "=" +surveyId+ "|" +timesLaunched+ "; expires=" +ExpireDate.toGMTString()+ "; path="+cookiePath+"; domain="+cookieDomain;
  openPopup(timesLaunched,maxOpenTimes);

}
