
// Cookie functions
var exitrescueCookie = {

   set : function(name, value, days) {
      var date = new Date();
      if (days !== undefined && typeof days == "object") {
         date = days;
      }
      else {

      // Default to a 1 year cookie
      if (days === undefined) { days = 365; }

      // Format date string
      var date = new Date();
      date.setTime(date.getTime() + (days * 86400000));

      }

      // Set cookie name, value, and expiration date
      //window.status = name + "=" + value + "; expires=" + date.toGMTString() + "; path=/";
      document.cookie = name + "=" + value + "; expires=" + date.toGMTString() + "; path=/";
   },

   get : function(name) {
      // Find the cookie's value in the document cookie string
      var results = document.cookie.match(
         new RegExp("(?:^|; )" + name + "=" + "(.*?)(?:$|;)")
      );

      // Return the value if a match was found, undefined otherwise
      if (results && results.length > 1) { return results[1]; }
      return undefined;
   },

   clear : function(name) {
      // Erase a cookie
      exitrescueCookie.set(name, "", -1);
   }

};

var exitrescue_url, exitrescue_message;
var exitrescue_limit, exitrescue_timeout, exitrescue_delay;
var exitrescue_click;

// Cookie shared with Action PopUp, Action Optin, Action Comments and Exit Rescue
var actionCount = parseInt(exitrescueCookie.get("actionCount"), 10);

if (exitrescue_delay == undefined) { exitrescue_delay = 0; }
else { exitrescue_delay = parseInt(exitrescue_delay); }
if (isNaN(exitrescue_delay)) { exitrescue_delay = 0; }

function exitrescue_enable() {

   if (exitrescue_url == undefined) {
      exitrescue_url = "http://228ccbki-1120fm6natb5g6a1l.hop.clickbank.net/";
   }

   if (exitrescue_message == undefined) {
      exitrescue_message = "Please Wait!\n\nHere is My Offer! Let Me Show You How To Build Your Own Free Energy Generator And Never Pay FOr Electricity Again!";
   }

   if (exitrescue_limit == undefined) {
      exitrescue_limit = 0;
   }

   if (exitrescue_timeout === undefined) {
      exitrescue_timeout = '1 hour';
   }

   if (isNaN(actionCount)) { actionCount = 0; }
   if (isNaN(exitrescue_limit)) { exitrescue_limit = 0; }

   if (exitrescue_limit > 0 && actionCount >= exitrescue_limit) {
      return;
   }

   window.onbeforeunload = function() {
      actionCount++;

      var actionTimeObject = new Date();
      actionTimeObject = Date.parse('+' + exitrescue_timeout);

      exitrescueCookie.set("actionCount", actionCount, actionTimeObject);

      if (exitrescue_url != '') {
         window.location = exitrescue_url;
      }

      if (exitrescue_message == '') {
         return exitrescue_message + "Click 'Cancel' right now!";
      }
      return exitrescue_message + "\n\nClick 'Cancel' right now!";
   }
}

function exitrescue_disable() {
   window.onbeforeunload = null;
}

jQuery(function() {
   if (exitrescue_delay > 0) {
      setTimeout("exitrescue_enable();", exitrescue_delay*1000);
   }
   else {
      exitrescue_enable();
   }

   jQuery("a").each(function(i, obj) {
      var href = jQuery(obj).attr("href");

      if (exitrescue_click == undefined || href.match(exitrescue_click)) {
         jQuery(this).bind("click", exitrescue_disable);
      }
      else {
         /* Do nothing -- do not disable this link */
      }
   });

   jQuery("form").each(function(i, obj) {
      var href = jQuery(obj).attr("action");
      if (exitrescue_click != undefined && href.match(exitrescue_click)) {
         jQuery(this).bind("submit", exitrescue_disable);
      }
      else {
         /* Do nothing -- do not disable this link */
      }
   });
});