﻿// Crossbrowser unblockable popups
// global reference to itself (necessary to make it accessible through setTimeout)
globalThis = new Array();
// URL to the repository
repository = "http://www.festo.com/INetDomino/repository/festo2.css";
// Image to close the popup
//closeImg = "http://www3.festo.com/__C1257065004B57E8.nsf/beenden.gif";
closeImg = "http://www.festo.com/INetDomino/repository/beenden.gif";
/* ---------------------------------------------------------------------- *\
  Function    : JSWindow()
  Description : creates a new window
  Usage       : JSWindow("title", oContent, [x], [y], [id], [width])
  Attributes   : 
  title         - The title of the window as displayed in the titlebar
  xPos             - The number of pixels from the left the window appears when first open
  yPos            - The number of pixels from the top the window appears when first open
  width            - The width of the popup
  height        - The height of the popup    
  contentHTML         - The content as inline HTML (needs externalContent to be set false!)
  contentDivId        - The Id of the div containing the content of the popup(externalContent = false!)
  borderPadding        - The padding in the titlebar in pixel
  titleBarHeight    - The height of the titlebar in pixel
  draggable            - Flag if the popup should be draggable (true/false)
  waitBeforeShowing     - Time in seconds until the popup shows up after pageload
  closeWidth        - The width of the close image
  cookieName        - Name of the cookie (popup is shown only if cookie is not set!)      
  externalContent     - Flag if the content is inline or external
  externalContentURL     - URL with the content to be displayed in the popup    
  bgColor        - Background color
\* ---------------------------------------------------------------------- */
function JSWindow(settings)
{
    this.idNum=settings.get("idNum")==null?0:settings.get("idNum");
    // Position
    this.xPos = settings.get("xPos")==null?0:settings.get("xPos");
    this.yPos = settings.get("yPos")==null?0:settings.get("yPos");
    this.width = settings.get("width")==null?400:settings.get("width");
    this.height = settings.get("height")==null?400:settings.get("height");
    
    // Title
    this.title = settings.get("title")==null?"Pop-Up":settings.get("title");    
    this.titleLeftPadding=settings.get("titleLeftPadding")==null?5:settings.get("titleLeftPadding");
      this.titleTopPadding=settings.get("titleTopPadding")==null?2:settings.get("titleTopPadding");
      this.closeRightPadding=settings.get("closeRightPadding")==null?2:settings.get("closeRightPadding");
      this.closeTopPadding=settings.get("closeTopPadding")==null?2:settings.get("closeTopPadding");
      this.contentLeftRightPadding=settings.get("contentLeftRightPadding")==null?5:settings.get("contentLeftRightPadding");
      this.contentTopPadding=settings.get("contentTopPadding")==null?2:settings.get("contentTopPadding")
    this.titleBarHeight=settings.get("titleBarHeight")==null?25:settings.get("titleBarHeight");
    // Content
    this.externalContent = settings.get("externalContent")==null?true:settings.get("externalContent");
    this.externalContentURL = settings.get("externalContentURL")==null?"":settings.get("externalContentURL");
    this.contentHTML = settings.get("contentHTML")==null?null:settings.get("contentHTML");
    this.contentDivId = settings.get("contentDivId")==null?null:settings.get("contentDivId");
    // Properties
    this.draggable = settings.get("draggable")==null?false:settings.get("draggable");
    this.waitBeforeShowing = settings.get("waitBeforeShowing")==null?0:settings.get("waitBeforeShowing");
    this.closeWidth = settings.get("closeWidth")==null?15:settings.get("closeWidth");
    this.cookieDomain = settings.get("cookieDomain")==null?"festo.com":settings.get("cookieDomain");
    this.cookieName = settings.get("cookieName")==null?"showHideFestosurvey"+this.idNum:settings.get("cookieName");
    this.cookiePath = settings.get("cookiePath")==null?"/":settings.get("cookiePath");
    this.oncePerSleepTime=settings.get("oncePerSleepTime")==null?true:settings.get("oncePerSleepTime");
    this.sleepTimeWhenClosed=settings.get("sleepTimeWhenClosed")==null?3600*24:settings.get("sleepTimeWhenClosed");      this.sleepTimeWhenRejected=settings.get("sleepTimeWhenRejected")==null?3600*24*60:settings.get("sleepTimeWhenRejected");
    this.bgColor=settings.get("bgColor")==null?"#FFFFFF":settings.get("bgColor");
    this.popUpProbability=settings.get("popUpPropability")==null?100:settings.get("popUpPropability");
    this.iFrame = null;
    this.outerDiv=null;
    this.closeDiv=null;
    this.closeWrapperDiv;
    this.titleBarWrapperDiv;
    this.titleBarDiv=null;
    this.rejected = false;
    this.accepted = false;
}
// Displays the popup, if cookie is not set to block
JSWindow.prototype.show = function()
{
    if ((getCookie(this.cookieName) != "1") || !this.oncePerSleepTime)
    {
        
        if (this.popUpProbability == 100)
        {
            this.showInAnyCase();
        }
        else
        {
            setCookie(this.cookieName,"1",this.sleepTimeWhenClosed,this.cookieDomain,this.cookiePath);
            // If a random value, then a cookie must be set in any case
            if (getRandom(1,100) <= this.popUpProbability)
            {
                this.showInAnyCase();
            }
        }
    }
}
// Displays the popup without checking
JSWindow.prototype.showInAnyCase = function()
{
    // dividing the titlebar according to the close image
    var rightPart = this.closeWidth/this.width*100;
    rightPart = Math.round(rightPart+1)+1;
        // popup container
    this.outerDiv = document.createElement('div');
    this.outerDiv.setAttribute("id","outerPopUpDiv");
    this.outerDiv.style.position = 'absolute';
      this.outerDiv.style.zIndex="1000";
    this.outerDiv.style.left = ""+this.xPos+"px";
    this.outerDiv.style.top = ""+this.yPos+"px";
    this.outerDiv.style.width = ""+this.width+"px";
    this.outerDiv.style.height = ""+this.height+"px";
    this.outerDiv.className="allborder";
    this.outerDiv.style.padding = "0px";
    this.outerDiv.style.visibility = "hidden";
    this.outerDiv.style.backgroundColor = this.bgColor;
    this.outerDiv.jsWindow = this;
    this.outerDiv.onmousedown = JSWindow.prototype.onBringToFront;
     // header container
    this.headerWrapperDiv = document.createElement('div');
    this.headerWrapperDiv.style.padding ="0px";
    this.headerWrapperDiv.style.margin ="0px";
    this.headerWrapperDiv.style.border ="0px";    
    this.headerWrapperDiv.style.cssFloat = "left";    
    this.headerWrapperDiv.style.styleFloat ="left";
    this.headerWrapperDiv.style.width = "100%";
    this.headerWrapperDiv.style.height = ""+this.titleBarHeight+"px";
    this.headerWrapperDiv.style.position='static';
    this.headerWrapperDiv.className="darkbg";
    // titlebar container
    this.titleBarWrapperDiv = document.createElement('div');
    this.titleBarWrapperDiv.style.padding ="0px";
    this.titleBarWrapperDiv.style.margin ="0px";
    this.titleBarWrapperDiv.style.border ="0px";    
    this.titleBarWrapperDiv.style.cssFloat = "left";    
    this.titleBarWrapperDiv.style.styleFloat ="left";
    this.titleBarWrapperDiv.style.width = ""+(this.width-this.closeWidth-2*(this.titleLeftPadding-this.closeRightPadding))+"px";
    this.titleBarWrapperDiv.style.position='static';
    if(this.draggable)
    {
        this.titleBarWrapperDiv.jsWindow = this;
        this.titleBarWrapperDiv.onmousedown = JSWindow.prototype.divOnMouseDown;
        this.titleBarWrapperDiv.style.cursor ="move";
    }
   
    // titlebar
    this.titleBarDiv = document.createElement('div');
         this.titleBarDiv.style.padding ="0px";
    this.titleBarDiv.style.margin ="0px";
    this.titleBarDiv.style.border ="0px";
    this.titleBarDiv.style.width = "100%";
    this.titleBarDiv.style.fontSize = "12px";
    this.titleBarDiv.style.lineHeight = "15px";
    this.titleBarDiv.style.fontWeight = "bold";
    this.titleBarDiv.style.textAlign = "center";
    this.titleBarDiv.style.color = "#666";
    this.titleBarDiv.style.fontFamily = "Arial,sans-serif";
    this.titleBarDiv.style.position='relative';
        this.titleBarDiv.style.top=""+this.titleTopPadding+"px";
        this.titleBarDiv.style.left=""+this.titleLeftPadding+"px";
    this.titleBarDiv.innerHTML="<b>"+this.title+"</b>";
    // close button container
    this.closeWrapperDiv = document.createElement('div');
    this.closeWrapperDiv.style.position='static';
    this.closeWrapperDiv.style.padding ="0px";
    this.closeWrapperDiv.style.margin ="0px";
    this.closeWrapperDiv.style.border ="0px";
    this.closeWrapperDiv.style.cssFloat = "right";    
    this.closeWrapperDiv.style.styleFloat ="right";
    this.closeWrapperDiv.style.width = ""+(this.closeWidth+2*this.closeRightPadding)+"px";
    this.closeWrapperDiv.jsWindow = this;
    this.closeWrapperDiv.onmousedown = JSWindow.prototype.onClose;
    this.closeWrapperDiv.style.cursor ="pointer";
    // close button
    this.closeDiv = document.createElement('div');
    this.closeDiv.style.position='relative';
         this.closeDiv.style.padding ="0px";
    this.closeDiv.style.margin ="0px";
    this.closeDiv.style.border ="0px";
         this.closeDiv.style.top=""+this.closeTopPadding+"px";
         this.closeDiv.style.left="-"+this.closeRightPadding+"px";
    this.closeDiv.style.cssFloat = "right";    
    this.closeDiv.style.styleFloat ="right";    
    this.closeDiv.innerHTML ="<img src='" +closeImg+"'>";
    this.closeDiv.className="darkbg";
    // content wrapper
    this.contentWrapperDiv = document.createElement('div');
    this.contentWrapperDiv.style.position="static";
         this.contentWrapperDiv.style.padding ="0px";
    this.contentWrapperDiv.style.margin ="0px";
    this.contentWrapperDiv.style.border ="0px";
    this.contentWrapperDiv.style.cssFloat = "left";    
    this.contentWrapperDiv.style.styleFloat ="left";
    this.contentWrapperDiv.style.width = "100%";
    // content
    this.contentDiv = document.createElement('div');
    this.contentDiv.style.position="static";
      this.contentDiv.style.paddingLeft =""+this.contentLeftRightPadding+"px";
      this.contentDiv.style.paddingRight =""+this.contentLeftRightPadding+"px";
      this.contentDiv.style.paddingTop =""+this.contentTopPadding+"px";
    this.contentDiv.style.cssFloat = "left";    
    this.contentDiv.style.styleFloat ="left";
        
    // if the content is on an external page, create an iFrame to display it
    if (this.externalContent)
    {
        this.iFrame = document.createElement("iframe");
        this.iFrame.frameBorder=0;
        this.iFrame.scrolling ="auto";
        this.iFrame.id="contenIFrame";
        this.iFrame.name="contentIFrame";
        this.iFrame.src = this.externalContentURL;
        this.iFrame.style.height = ""+(this.height-this.titleBarHeight-this.contentTopPadding)+"px";
        this.iFrame.style.width=(this.width-2*(this.contentLeftRightPadding))+"px";
        this.iFrame.style.visibility ="hidden";
        this.contentDiv.appendChild(this.iFrame);
    }
    else
    {
        if(this.contentDivId != null)
        {
            var contentDivElement = document.getElementById(this.contentDivId);
            if (contentDivElement != null)
            {
                this.contentDiv.innerHTML = document.getElementById(this.contentDivId).innerHTML;
            }
            else
            {
                this.contentDiv.innerHTML = "";
            }
        }
        else
        {
            this.contentDiv.innerHTML = this.contentHTML;
        }
    }
    // setting all together
    this.titleBarWrapperDiv.appendChild(this.titleBarDiv);
    this.closeWrapperDiv.appendChild(this.closeDiv);
  this.contentWrapperDiv.appendChild(this.contentDiv);
  this.headerWrapperDiv.appendChild(this.titleBarWrapperDiv);
  this.headerWrapperDiv.appendChild(this.closeWrapperDiv);
    this.outerDiv.appendChild(this.headerWrapperDiv);
    //this.outerDiv.appendChild(this.closeWrapperDiv);
    this.outerDiv.appendChild(this.contentWrapperDiv);    
    window.document.body.appendChild(this.outerDiv);
     if (this.contentDiv.innerHTML != "")
     {
       // Wait if necessary (in the background the iFrame content is loaded)
       if (this.waitBeforeShowing > 0)
       {
            globalThis[this.idNum]=this;
              setTimeout("globalThis["+this.idNum+"].unhide()",this.waitBeforeShowing*1000);
       }
        else
      {
          this.unhide();
      }
      }
}
// display the popup
JSWindow.prototype.unhide = function()
{
    this.outerDiv.style.visibility='visible';
    if (this.iFrame != null)
    {
        this.iFrame.style.visibility ="visible";
    }
}
JSWindow.prototype.onBringToFront = function()
{
    this.jsWindow.bringToFront();
}
JSWindow.prototype.bringToFront = function()
{
    // if not already the last child of the document.body, make it so
    if ( document.body.childNodes[document.body.childNodes.length-1] !== this.outerDiv )
    {
        // move to bottom of document
        document.body.appendChild(this.outerDiv);
    }
}
JSWindow.prototype.divOnMouseDown = function()
{
    this.jsWindow.onMouseDown();
}
JSWindow.prototype.onMouseDown = function()
{
    // record that an onmousedown has just occurred
    this.bDown = true;
    
    // link from body to this JSWindow object
    document.body.jsWindow = this;
    // save body mouse handlers
    this.saveMouseMove = document.body.onmousemove;
    this.saveMouseUp = document.body.onmouseup;
    // set new handlers.
    document.body.onmousemove = JSWindow.prototype.bodyOnMouseMove;
    document.body.onmouseup = JSWindow.prototype.bodyOnMouseUp;
}
JSWindow.prototype.bodyOnMouseMove = function(evt)
{
    var e = window.event ? window.event : evt;
    this.jsWindow.onMouseMove(e);
    
}
JSWindow.prototype.onMouseMove = function(evt)
{
    // if mouse not down, stop the move (for IE only)
    if ( (document.all) && !(evt.button & 1) )
    {
        this.onMouseUp();
        return;
    }
    if ( this.bDown )
    {
        this.dx = parseInt(this.outerDiv.style.left, 10) - evt.clientX;
        this.dy = parseInt(this.outerDiv.style.top, 10) - evt.clientY;
        this.bDown = false;
    }
    else
    {
        this.outerDiv.style.left = Math.max((this.dx + evt.clientX),0) + "px";
        this.outerDiv.style.top = Math.max((this.dy + evt.clientY),0) + "px";
    }
}
JSWindow.prototype.bodyOnMouseUp = function()
{
    this.jsWindow.onMouseUp();
}
JSWindow.prototype.onMouseUp = function()
{
    document.body.onmouseup = this.saveMouseUp;
    document.body.onmousemove = this.saveMouseMove;
    document.body.jsWindow = null;
}
JSWindow.prototype.close = function()
{    
      setCookie(this.cookieName,"1",this.sleepTimeWhenRejected,this.cookieDomain,this.cookiePath);
      
      //setCookie(this.cookieName,"1",this.sleepTimeWhenRejected);
    
    if (!this.accepted)
    {
        if (this.rejected)
        {
            setCookie(this.cookieName,"1",this.sleepTimeWhenRejected,this.cookieDomain,this.cookiePath);
        }
        else
        {
            setCookie(this.cookieName,"1",this.sleepTimeWhenRejected,this.cookieDomain,this.cookiePath);
        }
    }
    
    // remove from browser document
    if (this.outerDiv != null)
    {
            this.outerDiv.style.display = "none";
            /*this.outerDiv.parentNode.removeChild(this.outerDiv);
        this.outerDiv = null;*/
    }
}
JSWindow.prototype.onClose = function()
{
    this.jsWindow.close();
}
// Cookie handling
function setCookie(name, cookieValue) {
    // read optional additional parameters
    var paramValues = setCookie.arguments;
    var paramCount = setCookie.arguments.length;
    var duration = (paramCount > 2) ? paramValues[2] : null;
    var domain = (paramCount > 3) ? paramValues[3] : null;
    var path = (paramCount > 4) ? paramValues[4] : null;
    var secure = (paramCount > 5) ? paramValues[5] : false;
    // calculate expiration date from duration in seconds
    var expireDate = null;
    if (duration != null)
    {
        expireDate = new Date();
         duration = expireDate.getTime() + (duration*1000);
         expireDate.setTime(duration);
     }
     
     // set Cookie
    var cookieString = name+"="+escape(cookieValue)+ 
        ((expireDate == null) ? "" : ("; expires="+expireDate.toGMTString() ) ) +
        ((path == null) ? "" : (";path="+path)) +
        ((domain == null) ? "" : ("; domain="+domain)) +
        ((secure == true) ? "; secure" :"");
    
      
    document.cookie = cookieString;
    
}
function getCookie(name) {
    name += "=";
    var i = 0;
    while ( i < document.cookie.length) {
        var j = i + name.length;
        if (document.cookie.substring(i, j) == name)
            return getCookieValue(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0)
            break;
    }
    return null;
}
function getCookieValue(position) {
    var endIndex = document.cookie.indexOf(";", position);
    if (endIndex == -1)
        endIndex = document.cookie.length;
    return unescape(document.cookie.substring(position, endIndex));
}
function enabledCookies(cdomain, cpath) 
{
     var returnValue = false;
     setCookie("CookieTest","success",30,cdomain,cpath);
     if (getCookie("CookieTest")=="success")
     {
       setCookie("CookieTest","",30,cdomain,cpath);
       returnValue=true;
     }
     return returnValue;
}
function popUpWindow(url,title,width,height)
{
            Attribute="toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,titlebar=no,width="+width+",height="+height;
    newWindow = window.open(url,title,Attribute);
    newWindow.focus();
    return newWindow;
}
function getRandom( min, max ) {
        if( min > max ) {
                return( -1 );
        }
        if( min == max ) {
                return( min );
        }
 
        var r = parseInt( Math.random() * ( max+1 ) );
 
        return( r + min <= max ? r + min : r );
}
