var Survey = Class.create();
Survey.prototype = {
  initialize: function(url) {
    this.url = url;
    
    /*****************************/
    /* Handle Last Survey Date
    /*****************************/
    var d = new Date();
    var lDate = Cookie.get('last_survey_date', d.getTime()-1000*60*60*24*365);
    var lastDate = new Date(lDate);
    
    if (this.days_between(lastDate, d) >= 90) {
      // 90 day diff the customer is eligible
      this.isValid = true;
    }
    else {
      this.isValid = false;
    }
    
    /*****************************/
    /* Handle Page Views
    /*****************************/
    this.pageViews = Cookie.get("page_views", 0); 
    this.incrementPageViews(); // increment since the user is obviously seeing a page right now
    
    if (this.pageViews >= 4) {
	    if (this.isValid) {
	      this.serveSurvey();
	    }
    }
  },
  serveSurvey: function() {
    /*************************************************/ 
    /* first check and see if we are on a custom page
    /*************************************************/
    switch(this.url) {
      case "customer/account/logoutSuccess/":
        this.serveLogout();
        return;
        break;
      case "checkout/onepage/success/":
        this.serveSuccess();
        return;
        break;
    }
    
    var visitorNumber = Cookie.get("visitor_number", Math.ceil(Math.random()*100));
    if (visitorNumber < 4) {
      /* regex to make sure we aren't in checkout or login */
      var re = new RegExp("(checkout|login)");
      if (!re.exec(this.url)) {
        this.serveGeneral();
      }
      return;
    }
  },
  serveLogout: function() {
    Popup.open({url:"https://healthydirections.allegiancetech.com/cgi-bin/qwebcorporate.dll?idx=F28R6W"});
    this.setSurveyServed();
  },
  serveSuccess: function() {
    Popup.open({url:"https://healthydirections.allegiancetech.com/cgi-bin/qwebcorporate.dll?idx=55WPAK"});
    this.setSurveyServed();
  },
  serveGeneral: function() {
    Popup.open({url:"https://healthydirections.allegiancetech.com/cgi-bin/qwebcorporate.dll?idx=AVKASK"});
    this.setSurveyServed();
  },
  setSurveyServed: function() {
    var d = new Date();
    Cookie.set("last_survey_date", d);
  },
  incrementPageViews: function() {
    this.pageViews = this.pageViews + 1;
    Cookie.set("page_views", this.pageViews);
  },
  days_between: function(date1, date2) {
      // The number of milliseconds in one day
      var ONE_DAY = 1000 * 60 * 60 * 24;

      // Convert both dates to milliseconds
      var date1_ms = date1.getTime();
      var date2_ms = date2.getTime();

      // Calculate the difference in milliseconds
      var difference_ms = Math.abs(date1_ms - date2_ms);

      // Convert back to days and return
      return Math.round(difference_ms/ONE_DAY);
  }
};


var Cookie = {
  key: 'survey_cookies',  
    
  set: function(key, value) {  
   var cookies = this.getCookies();  
   cookies[key] = value;  
   var src = Object.toJSON(cookies).toString();  
   this.setCookie(this.key, src);  
  },  
    
  get: function(key){  
   if (this.exists(key)) {  
    var cookies = this.getCookies();  
    return cookies[key];  
   }  
   if (arguments.length == 2) {  
    return arguments[1];  
   }  
   return;  
  },  
    
  exists: function(key){  
   return key in this.getCookies();  
  },  
    
  clear: function(key){  
   var cookies = this.getCookies();  
   delete cookies[key];  
   var src = Object.toJSON(cookies).toString();  
   this.setCookie(this.key, src);  
  },  
    
  getCookies: function() {  
   return this.hasCookie(this.key) ? this.getCookie(this.key).evalJSON() : {};  
  },  
    
  hasCookie: function(key) {  
   return this.getCookie(key) != null;  
  },  
   
  setCookie: function(key,value) {  
   var expires = new Date();  
   expires.setTime(expires.getTime()+1000*60*60*24*365); // this needs to be 365 days in the future  
   document.cookie = key+'='+escape(value)+'; expires='+expires+'; path=/';  
  },  
   
  getCookie: function(key) {  
   var cookie = key+'=';  
   var array = document.cookie.split(';');  
   for (var i = 0; i < array.length; i++) {  
    var c = array[i];  
    while (c.charAt(0) == ' '){  
     c = c.substring(1, c.length);  
    }  
    if (c.indexOf(cookie) == 0) {  
     var result = c.substring(cookie.length, c.length);  
     return unescape(result);  
    };  
   }  
   return null;  
  }  
 };
 
 var Popup = {
   open: function(options)
   {
     this.options = {
       url: '#',
       width: 600,
       height: 500,
       name:"_blank",
       location:"no",
       menubar:"no",
       toolbar:"no",
       status:"yes",
       scrollbars:"yes",
       resizable:"yes",
       left:"",
       top:"",
       normal:false
     }
     Object.extend(this.options, options || {});

     if (this.options.normal){
     this.options.menubar = "yes";
     this.options.status = "yes";
     this.options.toolbar = "yes";
     this.options.location = "yes";
     }

     this.options.width = this.options.width < screen.availWidth?this.options.width:screen.availWidth;
     this.options.height=this.options.height < screen.availHeight?this.options.height:screen.availHeight;
     var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
     if (this.options.top!="")openoptions+=",top="+this.options.top;
     if (this.options.left!="")openoptions+=",left="+this.options.left;
     
     /* popup will not be a pop-under in FF4+ -- see http://support.mozilla.com/en-US/questions/806756 for details */
     var win2 = window.open(this.options.url, this.options.name,openoptions );
     //win2.blur();
     //window.focus();
     return false;
   }
 };

