/* © 2008 Red Eye International Ltd. */
/* This script was created by RedEye and permission for use granted exclusively to Dorling Kindersley, 12/12/2008. */
/* For all enquiries please contact RedEye - http://www.RedEye.com, Tel: 0845 094 1114 */


document.body.onclick = HideSurvey

/* ------------------------------------------------------------------------------------ */
/* GLOBAL SURVEY SETTINGS                                                               */
/* ------------------------------------------------------------------------------------ */

var bSurveysEnabled  =    1                           // Enables or disables ALL surveys
var iPopupDelay      =    2                           // Delay in seconds before the popup window is displayed.
var sImageLocation   =    './redsurvey/images/'       // The location of survey image files

/* ------------------------------------------------------------------------------------ */
/* DO NOT EDIT BELOW THIS LINE                                                          */
/* ------------------------------------------------------------------------------------ */

var SURVEY_ID       = eval ( sLoadedSurvey + '.ID'      )
var SURVEY_Title    = eval ( sLoadedSurvey + '.TITLE'   )
var SURVEY_Text1    = eval ( sLoadedSurvey + '.TEXT1'   )
var SURVEY_Text2    = eval ( sLoadedSurvey + '.TEXT2'   )
var SURVEY_Text3    = eval ( sLoadedSurvey + '.TEXT3'   )
var SURVEY_Text4    = eval ( sLoadedSurvey + '.TEXT4'   )
var SURVEY_Text5    = eval ( sLoadedSurvey + '.TEXT5'   )
var SURVEY_Text6    = eval ( sLoadedSurvey + '.TEXT6'   )
var SURVEY_Text7    = eval ( sLoadedSurvey + '.TEXT7'   )
var SURVEY_Text8    = eval ( sLoadedSurvey + '.TEXT8'   )
var SURVEY_Text9    = eval ( sLoadedSurvey + '.TEXT9'   )
var SURVEY_Text10   = eval ( sLoadedSurvey + '.TEXT10'  )
var SURVEY_Enabled  = eval ( sLoadedSurvey + '.ENABLED' )
var SURVEY_Type     = eval ( sLoadedSurvey + '.TYPE'    )
var SURVEY_Location = eval ( sLoadedSurvey + '.URL'     )
var SURVEY_Height   = eval ( sLoadedSurvey + '.HEIGHT'  )
var SURVEY_Width    = eval ( sLoadedSurvey + '.WIDTH'   )

var PopupHTML = ''
var AddThesePixels = 0 

PopupHTML += '<div id="redsurvey_div" style="display:none;">\n'
PopupHTML += '  <div id="shadow">\n'
PopupHTML += '    <div id="modal_main">\n'
PopupHTML += '      <div id="modal_main_header" style="height:35px;"><a href="JavaScript:HideSurvey();"\n'
PopupHTML += 'class="close_button" tabindex="1">Close</a> \n'
PopupHTML += '      </div>\n'
PopupHTML += '      <div id="modal_main_body">\n'
PopupHTML += '        <h4>'+SURVEY_Title+'</h4>\n'
PopupHTML += '        <p>'+SURVEY_Text1+'</p>\n'

for (var i=2; i<=10; i++) {
  var sTmp = eval ('SURVEY_Text' + i);

  if (sTmp != '') {
    PopupHTML += '<p>' + sTmp + '</p>\n'
    AddThesePixels += 100
  }
}

PopupHTML += '        <a href="JavaScript:AcceptSurvey();" class="yes" tabindex="2" title="Yes, I want to participate in this survey">Yes</a>\n'
PopupHTML += '<a href="JavaScript:DeclineSurvey();" class="no" tabindex="3" title="No, I do not want to participate in this survey">No</a>\n '
PopupHTML += '		<br class="clear" />\n'
PopupHTML += '      </div>\n'
PopupHTML += '    </div>\n'
PopupHTML += '  </div>\n'
PopupHTML += '</div>\n'

document.write(PopupHTML)

if ( (bSurveysEnabled == 1) && (SURVEY_Enabled == 'true') ) {
    var iSleep = iPopupDelay * 1000
    setTimeout(PopupSurvey, iSleep)
}

function PopupSurvey() {
  var redsurvey = document.getElementById('redsurvey_div')

  if ( GetCookie(SURVEY_ID) != 'true' ) {                                                                
   var sTop
   if (document.body.scrollTop) {
     sTop = document.body.scrollTop
   } else {
     sTop = document.documentElement.scrollTop
   }

      redsurvey.style.top     = sTop + 50 + (screen.height / 5) + 'px'
      redsurvey.style.display = 'block'    
  }
}


function HideSurvey() {
  var redsurvey = document.getElementById('redsurvey_div')
  redsurvey.style.display = 'none'
}

function GetCookie(sCookieName) {
   var Cookie

   if ( document.cookie == '' ) { return false }
   var ArrayOfCookies = document.cookie.split(';')

   for (var i=0; i<ArrayOfCookies.length; i++) {
      Cookie = ArrayOfCookies[i]
      if (Cookie.indexOf(sCookieName+'=') > -1) {
        return 'true'
      }
   }

   return 'false'
}

function SetCookie(sName,sValue,iNumOfDays) {
  var TargetDate = new Date()
  var ExpiryDate

  TargetDate.setTime(TargetDate.getTime()+(iNumOfDays*24*60*60*1000))

  var ExpiryDate = "; expires="+TargetDate.toGMTString();
  document.cookie = sName+"=" + sValue + ExpiryDate + "; path=/"
}

function DeclineSurvey() {
  var DeclineDate = new Date()
  HideSurvey()
  SetCookie(SURVEY_ID,'N=' + DeclineDate.toGMTString(),90)
}

function AcceptSurvey() {
  var AcceptDate = new Date()
  HideSurvey()
  SetCookie(SURVEY_ID,'Y=' + AcceptDate.toGMTString(),90)

  DisplayMainSurveyWindow()
}

function DisplayMainSurveyWindow() {
  var NewWindow  

  var iLeft = ( screen.width  / 2 ) - ( SURVEY_Width  / 2 )
  var iTop  = ( screen.height / 2 ) - ( SURVEY_Height / 2 )

  if (SURVEY_Type == 'now') {
  	NewWindow = window.open(SURVEY_Location,'redsurvey','resizable=1,scrollbars=1,height='+SURVEY_Height+',width='+SURVEY_Width+',toolbar=0,menubar=0,left='+iLeft+',top='+iTop)    
  } else {	  	  	  	
    NewWindow = window.open(SURVEY_Location,'redsurvey','resizable=1,scrollbars=1,height='+SURVEY_Height+',width='+SURVEY_Width+',toolbar=0,menubar=0,left='+iLeft+',top='+iTop)
    NewWindow.blur()
    window.focus()        
    window.resizeBy(1,1)
  }
  
}

