function setCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function deleteCookie(name) {
  setCookie(name,"",-1);
}


var affiliate_toolkit = {
	exit_popup_enable: true,
	exit_popup_text: 'some text - Click OK',
	exit_popup_url: 'http://madafak',
	
	exit_popup_alert: false,
	exit_popup_display_limit: 0,
	
	exit_popup_delay_activation: 0
}


/*
var exit_popup_enable = true;

var exit_popup_text = "We would request you to sign up for the OrangeCopper Blog weekly newsletter before leaving – Click OK";
var thank_you_text = "Thank you! Please fill your email in top right corner form on the Blog page loading";

var exit_popup_url = "http://7digit.net/test";

var exit_popup_alert = false;

// show limit & reset count
var exit_popup_display_limit = 0;
var exit_popup_reset_count_days = 1;

var exit_popup_delay_activation = 0;
*/



//on ready
function disableExitPopup() {
  affiliate_toolkit.exit_popup_enable = false;
  window.onbeforeunload = null;
}


function getHostname(str) {
	var re = new RegExp('^(?:f|ht)tp(?:s)?\://([^/]+)', 'im');
	return str.match(re)[1].toString();
}


function isOutLink(url) {
  if (url.length < 8) 
    return false;

  if (url.substr(0, 7) != 'http://')
    return false;

  var current_hostname = window.location.hostname;
  var outgoing_hostname = getHostname(url);
  
  return (current_hostname != outgoing_hostname) ? true : false;
}


$(document).ready(function() {
  
  //alert("dik");

  $('a').each(function(index) {
    $(this).click(function() {
      var href = $(this).attr('href');
      
      if (!isOutLink(href)) {
        //alert("je outlink");
        disableExitPopup();
      }
      
/*
      else alert("ni outlink");    
      return false;
*/

    });
  });
  
  
  $('form').each(function(index) {
    //alert("form 1");
    $(this).submit(function() {
      disableExitPopup();
    });
  });
 

  enableExitPopup();
});

function enableExitPopup() {
  affiliate_toolkit.exit_popup_enable = true;

	
  if (affiliate_toolkit.exit_popup_alert) {
    $(window).unload(function() {

      if (showPopupCheck()) {
        showPopupAlert();
      }
    });
  }
  else {
    window.onbeforeunload = function() { 
      
      if (showPopupCheck()) {
        window.location.href = affiliate_toolkit.exit_popup_url;
        return affiliate_toolkit.exit_popup_text;
      }
      
    }
  }
}


function showPopupAlert() {
  if (confirm(affiliate_toolkit.exit_popup_text)) {
		//alert(thank_you_text);
		//window.open(exit_popup_url);
		window.location.href = affiliate_toolkit.exit_popup_url;
	}
}


function showPopupCheck() {
  if (!affiliate_toolkit.exit_popup_enable)
    return false;
  
  var count = getCookie("actionCount");
  
  if (count == null) {
    setCookie("actionCount", 1, 356);
  } else {
    
    //alert("count: " + count);
    
    if ( affiliate_toolkit.exit_popup_display_limit > 0 && count > affiliate_toolkit.exit_popup_display_limit ) //limit exceeded
      return false; 
    
    count++;
    setCookie("actionCount", count, 356);
  }
  
  return true;
}
