



var over = false;
var N = document.all ? 0 : 1;
var X,Y;

var popupContainer = null;
var myContainer = null;
var animate = true;

if (N) {
    document.captureEvents(Event.MOUSEDOWN, Event.MOUSEMOVE, Event.MOUSEUP);
}

function QP_stopAnimation() {
    animate = false;
}

function QP_mouseDown(e) {
    if (over) {
	animate = false;
	popupContainer = document.getElementById("QuestionProPopupSurvey_" + config.surveyID);
	if (popupContainer) {
	    if (N) {
		X = e.layerX;
		Y = e.layerY;
		return false;
	    } else {
		popupContainer = popupContainer.style;
		X = event.offsetX;
		Y = event.offsetY;
	    }
	}
    }
}


function QP_mouseMove(e) {
    if (popupContainer) {
	if (N) {
	    popupContainer.style.left = (e.pageX - X);
	    popupContainer.style.top = (e.pageY - Y);
	} else {
	    popupContainer.pixelLeft = (event.clientX - X) + document.body.scrollLeft;
	    popupContainer.pixelTop = (event.clientY - Y) + document.body.scrollTop;
	    return false;
	}
    }
}

function QP_mouseUp() {
    popupContainer = null;
}

function QP_mouseOver() {
    over = true;
}

function QP_mouseOut() {
    over = false;
}


function QP_setCookie(name, value) {
  var expFromNow = 60*24*60*60*1000;  // Expires in 60 Days
  var exp = new Date(); 
  exp.setTime(exp.getTime() + expFromNow);
  document.cookie = (name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/;");
}
function QP_setExpTimeCookie() {
    QP_setCookie("TallinkQuest_timeoutPopup", (new Date()).getTime());
    QP_setCookie("Survey_" + config.surveyID, -1, 2);
    closeQPDiv();
}


function QP_getCookie(Name)  {
    var search = Name + "=";
    if (document.cookie.length > 0)  { 
	offset = document.cookie.indexOf(search);
	if (offset != -1) { 
	    offset += search.length;
	    end = document.cookie.indexOf(";", offset);
	    end = (end == -1 ? 
		   document.cookie.length :
		   end);
	    return unescape(document.cookie.substring(offset, end));
	}    
    }
}

function showContainer() {

  if (myContainer) {
      if (config.animationMode == 0) {
        myContainer.style.display = 'block';
      } else if(config.animationMode == 1) {
        animateTop();
      } else {
        animateLeft();
      }
  }
}

function animateTop() {
  var target = parseInt(myContainer.style.top);
  myContainer.style.top = 0;
  myContainer.style.display = 'block';  
  setTimeout("moveDivVertical(" + target + ")", 100);
}


function animateLeft() {
  var target = parseInt(myContainer.style.left);
  myContainer.style.left = 0;
  myContainer.style.display = 'block';
  
 
  setTimeout("moveDivHorizontal(" + target + ")", 100);
}


function moveDivVertical(targetLocation) {
  var currentLocation = parseInt(myContainer.style.top);
  if (animate && (currentLocation < targetLocation)) {
    myContainer.style.top = (currentLocation + 5);
    setTimeout("moveDivVertical(" + targetLocation + ")", 100);
  }  
}

function moveDivHorizontal(targetLocation) {
  var currentLocation = parseInt(myContainer.style.left);

  if (animate && (currentLocation < targetLocation)) {
    myContainer.style.left = (currentLocation + 5);
    setTimeout("moveDivHorizontal(" + targetLocation + ")", 100);
  }  
}


function closeQPDiv() {
  if (myContainer) {
    myContainer.style.display = 'none';
  }
  myContainer = null;  
}


function openPreview(loc) {
  var winl = (screen.width-800)/2;
  var wint = (screen.height-640)/2;
  var myRemote = launch(loc,"FreeSurveys1","height=640,width=800,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0,top="+wint + ",left=" + winl,"Help");
  if (myRemote) {
    myRemote.focus();  
  }
}

function launch(newURL, newName, newFeatures, orgName) {
    remote = open(newURL, newName, newFeatures);
    if (remote) {
      if (remote.opener == null)
	remote.opener = window;
      remote.opener.name = orgName;
    }
    return remote;
}

function startSurvey() {
  QP_setCookie("TallinkQuest_accepted", "1");
  closeQPDiv();
  openPreview(config.takeSurveyURL + config.surveyID);
}


function QP_setCookieClose(value, days) {
  QP_setCookie("Survey_" + config.surveyID, value, days);
  closeQPDiv();
}


function QP_popupMain() {

    var skipCount = (config.skipCount && config.skipCount > 0 ?
		 (Math.random() * 100) % config.skipCount : 1);

    skipCount = (config.skipCount && config.skipCount > 0 ?
	     (skipCount <= (config.skipCount / 2) ? 1 : 0) :
	     1);
    if ((QP_getCookie("TallinkQuest_timeoutPopup") == undefined || (new Date()).getTime() - parseInt(QP_getCookie("TallinkQuest_timeoutPopup")) > 300000) &&
        QP_getCookie("TallinkQuest_accepted")==undefined) {        
        if (skipCount == 1) {
            document.onmousedown = QP_mouseDown;
            document.onmousemove = QP_mouseMove;
            document.onmouseup = QP_mouseUp;
            if (config.popupMode != 0) {
                myContainer = QP_addDiv("QuestionProPopupSurvey_" + config.surveyID);
                myContainer.innerHTML = QP_getInnerContent();
                var styleText = 'display:none;border:1px solid #666666;font-family:verdana;font-size:12px;width:350px;background-color:#FFFFFF;;padding:0px; position:absolute; z-index:999; width:500px; left:36%;top: 120px;';

                QP_setStyle(myContainer, styleText);
				

                if (myContainer) {
                    if (config.invitationDelay == 0) {
                        showContainer();
                    } else {
                        setTimeout("showContainer()", config.invitationDelay * 1000);
                    }
                }
            } else {
                openPreview(config.takeSurveyURL + '?id=' + config.surveyID);
            }
        }
    }
}

function QP_getInnerContent() {
    var s = '<div  style="background:none repeat scroll 0 0 #00529B;color:#FFFFFF;font-size:14px;margin:6px;overflow:auto; cursor:move;height:50%">' +
	'<div style="margin:5px;float:left;font-weight:bold;width:70%">' +
  	  config.popupInvitationHeader +
	'</div>' +
	'<div style="margin:5px;float:right;cursor:pointer;width:15%" align="right" onclick="closeQPDiv()"><a style="font-family:Verdana;color:#FFFFFF;float:right;font-size:10px;font-weight:normal;margin:3px;padding:0;text-decoration:none;" href="javascript:closeQPDiv()">'+config.closeText+'</a></div>' +
	'</div>' +
	'<br>'+
	'<div style="padding: 5px 18px;">' +
	'<div >' +
	config.popupMessage +
	'</div>'+
	'<br>'+
	'<hr size="1">' +
	'<br>'+
	'<div>' +
	(config.isRightToLeft ?
	 '<a style="margin-left:'+config.marginBetwButton+';background:none repeat scroll 0 0 #00529B;font-family:verdana;color:#FFFFFF;font-size:11px;font-weight:bold;padding:5px;text-decoration:none;" href="javascript:startSurvey()">' +
	 config.surveyStartMessage +
	 '</a>' +
	 '&nbsp;' +
	 '<a style="background:none repeat scroll 0 0 #00529B;font-family:verdana;color:#FFFFFF;font-size:11px;font-weight:bold;padding:5px;text-decoration:none;" href="javascript:QP_setExpTimeCookie()">' +
	 config.popupInvitationLaterMessage +
	 '</a>'
	 :
	 '<div align="center">' +
	 '<a style="background:none repeat scroll 0 0 #00529B;font-family:verdana;color:#FFFFFF;font-size:11px;font-weight:bold;padding:5px;text-decoration:none;" href="javascript:QP_setExpTimeCookie()">' +
	 config.popupInvitationLaterMessage +
	 '</a>' +
	 '&nbsp;&nbsp;&nbsp;' +
	 '<a style="margin-left:'+config.marginBetwButton+';background:none repeat scroll 0 0 #00529B;font-family:verdana;color:#FFFFFF;font-size:11px;font-weight:bold;padding:5px;text-decoration:none;" href="javascript:startSurvey()">' +
	 config.surveyStartMessage +
	 '</b></a>' +
	 '</div>') +
	'</div>' +

	'<div style="height:10px;">&nbsp;</div>' +
	'</div>';
        return s;
} 


function QP_addDiv(divID) {
    var dialog = document.createElement('div');
    dialog.innerHTML = '<div id="' + divID + '"></div>';
    document.body.appendChild(dialog);
    return document.getElementById(divID);
}

function QP_setStyle(element, styleText) {
    element.setAttribute("style", styleText);
    element.style.cssText = styleText;
}


