// Copyright 2009 Convergys. All rights reserved.
// Date: 2009-10-02

var Nth=0.0015;
// var Nth=1;
var isIE=false;
var isMoz=false;
var inviteHeight=192;
var inviteWidth=384;
var job='usps';
var surveyURL='http://survey1.sendyouropinions.com/'+job;
var popDomain = '.usps.com';
var cookieName=job;

function okBrowser() {
   var browserName = navigator.appName; 
   var vers = parseInt(navigator.appVersion);
   var isok = false;
   switch (browserName) {
      case 'Microsoft Internet Explorer':
         if (vers > 3) {
            isok=true;
            isIE=true;
            }
         break;
      case 'Netscape':
         if (vers > 4) {
            isok=true;
            isMoz=true;
            }
         break;   
      }
   if (navigator.userAgent=='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; KTXN)') isok=false;
   return isok;
}

function acceptSurvey() {
   clearInterval(centobj.savedIntervalId);
   var myResource = new Image();
   myResource.src = 'http://dev-survey1.sendyouropinions.com/uspsinvite/yes.gif?state=2&job='+job+'&x=' + Math.random();
   window.open(surveyURL);
   visInvite('hidden');
}

function declineSurvey() {
   clearInterval(centobj.savedIntervalId);
   var myResource = new Image();
   myResource.src = 'http://dev-survey1.sendyouropinions.com/uspsinvite/no.gif?state=3&job='+job+'&x=' + Math.random();
   visInvite('hidden');
}


function createInvite() {
   var myResource = new Image();
   myResource.src = 'http://dev-survey1.sendyouropinions.com/uspsinvite/log.gif?state=1&job='+job+'&x=' + Math.random();

   htmlstring= '<table cellpadding="0" cellspacing="0" style="background:#ffffff;width:'+inviteWidth+'px;">\
    <tr style="height:7px; background:#ffffff;"><td></td></tr>\
    <tr ><td style="text-align:center;"><img src="http://www.usps.com/survey/newlogo.gif" border="0" width="146" height="40"></td></tr>\
    <tr style="height:5px; background:#d72623;"><td></td></tr>\
    <tr style="height:14px; background:#074d95;"><td></td></tr>\
    <tr style="height:5px; background:#44ace3;"><td></td></tr>\
    <tr><td>\
    <table border="0" cellpadding="2" bgcolor="#ffffff"><tr><td>\
    <p><div style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #000000;">We are conducting a survey\
    to find out what visitors think about USPS.com. Your answers will help us serve you better. Would you like to participate?</div></p>\
    <div align="center" style="margin-bottom: 15px">\
    <input type="button" value="  Yes  " onclick="acceptSurvey();" />&nbsp;&nbsp; \
    <input type="button" value=" No " onclick="declineSurvey();" />\
    </div>\
    <div style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #000000; margin-top: 0px;">\
    Review the <a href="http://www.usps.com/common/docs/privpol.htm/" target="_blank">Privacy Statement</a> for this survey.</div></td></tr>\
    </td></tr></table>\
    <tr style="height:5px; background:#44ace3;"><td></td></tr>\
    <tr style="height:13px; background:#074d95;"><td></td></tr>\
    </table>'; 

   document.write('<div id="surveylayer" style="position:absolute; left:0; top:0; z-index:11; visibility: hidden; border:1px #7d7d7d solid; width:'+(inviteWidth+2)+'px;height='+inviteHeight+'px;">'+htmlstring+'</div>');
}
function visInvite(x) {
   if (isIE) document.all["surveylayer"].style.visibility = x;
   else if (isMoz) document.getElementById("surveylayer").style.visibility = x;
}


function leftPos() {
   if (isIE) return document.body.scrollLeft;
   else if (isMoz) return window.pageXOffset;
   else return 0;
}

function topPos() {
   if (isIE) return document.body.scrollTop;
   else if (isMoz) return window.pageYOffset;
   else return 0;
}    

function winWidth() {
   if (isIE) return document.body.offsetWidth - 20;  //20 is approx for scroll bar
   else if (isMoz) return window.innerWidth;
   else return 0;
}

function winHeight() {
   if (isIE) return document.body.offsetHeight;
   else if (isMoz) return window.innerHeight;
   else return 0;
}
    
function moveInvite(x,y) {
   if (isIE) {
      document.all["surveylayer"].style.posLeft = x;
      document.all["surveylayer"].style.posTop = y;
      }
   else {
      document.getElementById("surveylayer").style.left = x;
      document.getElementById("surveylayer").style.top = y;
      }
}

function centerObj()
{
    this.test=0;
    this.delay=5;    
    this.w = this.h = 0;
    this.lastX = this.lastY = 0;
    this.marginX = this.marginY = 0;
    this.savedIntervalId = 0;
 
    this.init = fnc_init;
    this.adjust = fnc_adjust;
    this.resize = fnc_resize;

    function fnc_init(x, y) {
//       x=(winWidth()-inviteWidth)/2;
       y=(winHeight()-inviteHeight)/2 ;
       visInvite('visible');
       this.w = inviteWidth;
       this.h = inviteHeight;
       this.lastX = x;
       this.lastY = y;
       this.resize();
       this.adjust();
       window.onresize = new Function('centobj.resize()');
       this.savedIntervalId = setInterval('centobj.adjust()', this.delay);
       }
    
    function fnc_adjust() {
       var t = this.delay;
       var x = this.lastX;
       var y = this.lastY;
       var dx = Math.abs(leftPos() + this.marginX - x);
       var dy = Math.abs(topPos() + this.marginY - y);
       var d = Math.sqrt(dx * dx + dy * dy);
       var c = Math.round(d / 50);

       if (leftPos() + this.marginX > x) { x = x + t + c; }
       if (leftPos() + this.marginX < x) { x = x - t - c; }
       if (topPos() + this.marginY > y) { y = y + t + c; }
       if (topPos() + this.marginY < y) { y = y - t - c; }
       moveInvite(x, y);
       this.lastX = x;
       this.lastY = y;       
       }
    
    function fnc_resize() {
       this.marginX = Math.round(winWidth() / 2) - Math.round(this.w / 2);
       this.marginY = Math.round(winHeight() / 2) - Math.round(this.h / 2);
       }    
  return this;
}

function setCookie() {
   var currentDate = new Date();  
   var expireDate = new Date();
   expireDate.setTime(currentDate.getTime() + (90 * (1000 * 60 * 60 * 24)));
   document.cookie = cookieName + '=1; path=/; expires=' + expireDate.toGMTString() + '; domain=' + popDomain;
   return true;
}

function haveSurveyCookie() {
   return (document.cookie.indexOf(cookieName) != -1)
}

function CheckNth() {
   if (Nth==1) return true;
   else if (Nth > Math.random()) return true;
   else return false;
}

if (okBrowser()) {
   if (CheckNth()) {
      if (!haveSurveyCookie() ) {
         setCookie(2);
         createInvite();
         var centobj = new centerObj();
         centobj.init(0,0);
         }
      }
   }

