/* 
Business rule: For pages where this function is called, Show popup unless visitor has already bought the product or if visitor has already been shown popup, or if visitor is known to be exiting this page to visit another page in the website.

Implementation notes:
Cookie is used to indicate whether to display popup in the following ways:
- If visitor has bought product set cookie value to "N" (for Never show popup) with expiration date of 10 years hence.
- If visitor has just clicked a link to an internal page set cookie value to "O" (for don't show Once) with expiration of Session.
- If visitor has already been shown popup set cookie value to "N" (for Never show popup) with expiration of Session.
- If cookie does not exist or has other value show popup.
  
*/

function doOnload() {
  var sCV = getCookie('sppp'); 
  
  if (sCV == 'O') { 
    deleteCookie('sppp');
  }
}

function doOnunload() { 
  var sCV = getCookie('sppp'); 
  //alert('onunload '+sCV);
  if ((sCV != 'N') && (sCV != 'O')) {
    // COMMENT OUT WINDOW.OPEN WHILE XMAS DISCOUNT RUNNING
    // window.open('discount.php', 'discountwin', 'width=230,height=440');
    setCookie("sppp", "N"); 
    
  } else {
    if (sCV == 'O') { deleteCookie('sppp'); }
  }
}

function doOnAnyClick(e) { 
  if (!e) var e = window.event;
  var tg = (e.target) ? e.target : e.srcElement;
  //alert('onanyclick tagname='+tg.tagName+' classname='+tg.className+' cookieval='+sCV);

  if (tg.className == "inside") {
    var sCV = getCookie('sppp'); 
    if (sCV != 'N') {
      setCookie("sppp", "O"); 
    }
  }
  
  if (tg.className == "buynow") {
    var dExp = new Date(); 
    dExp.setTime(dExp.getTime() + (10*365*24*60*60*1000)); // 10 years in future   
    setCookie('sppp', 'N', dExp);  
  }
}

document.onclick = doOnAnyClick;

