﻿//-- =============================================
//-- Author:       JW
//-- Date:         29-Aug-07
//-- Description:  DHTML popup function (issue 848)
//-- =============================================

function DHTMLCookie(strArticleID,iExpiry) {
    // Generic cookie function, dynamic day expiry
    // strArticleID = unique article ID, creates unique cookie names per article
    var cookie_date = new Date ( ); // current date & time
    cookie_date.setTime(cookie_date.getTime()+(iExpiry*24*60*60*1000)); // set date iExpiry days into the future
    document.cookie = 'DHTMLSavings'+strArticleID+'=stop; expires='+cookie_date.toGMTString()+';' // sets cookie (iExpiry day expiry) to stop popup reappearing
}

function SubmitValidation(strArticleID) {
    // Form validation + submit
    if (EmailCheck( trimString(document.getElementById("DHTMLemail").value) ) == false){
        alert('Please enter a valid email address.');
		return false;
    }else{
        document.DHTMLReg.submit();
        // Set cookie once user registers to prevent popup from appearing
        DHTMLCookie(strArticleID,90); //Set 3 month cookie
    }
}

function closeMyPopup(strArticleID) {
  // Closes popup
  document.getElementById("mypopup").style.display = "none";
  //document.getElementById("modalBackground").style.display = "none";
  
  // Set cookie if user checks box to prevent popup from appearing
  DHTMLCookie(strArticleID,30); //Set 1 month cookie
}

function SubmitEmailValidationWDYHAU(strArticleID,iExpiry) {
    // Form validation + submit
    if (document.getElementById("DHTMLemail").value != "") {
        if (EmailCheck( trimString(document.getElementById("DHTMLemail").value) ) == false){
            alert('Please enter a valid email address.');
		    return false;
		}else{
		    document.DHTMLReg.submit();
            // Set cookie once user registers to prevent popup from appearing
            DHTMLCookie(strArticleID,iExpiry); //Set 3 month cookie
		}
    }else{
        document.DHTMLReg.submit();
        // Set cookie once user registers to prevent popup from appearing
        DHTMLCookie(strArticleID,iExpiry); //Set 3 month cookie
    }
}

function SubmitValidationWDYHAU(strArticleID,iExpiry) {
    // submit
    document.DHTMLReg.submit();
    // Set cookie once user registers to prevent popup from appearing
    DHTMLCookie(strArticleID,iExpiry); //Set 3 month cookie
}

function closeMyPopupWDYHAU(strArticleID,iExpiry) {
  // Closes popup
  document.getElementById("mypopup").style.display = "none";
  //document.getElementById("modalBackground").style.display = "none";
  
  // Set cookie to prevent popup from appearing
  DHTMLCookie(strArticleID,iExpiry); //Set cookie with dynamic expiry
}

function myPopupRelocate() {
  // Positions popup
  var scrolledX, scrolledY;
  if( self.pageYOffset ) {
    scrolledX = self.pageXOffset;
    scrolledY = self.pageYOffset;
  } else if( document.documentElement && document.documentElement.scrollTop ) {
    scrolledX = document.documentElement.scrollLeft;
    scrolledY = document.documentElement.scrollTop;
  } else if( document.body ) {
    scrolledX = document.body.scrollLeft;
    scrolledY = document.body.scrollTop;
  }

  var centerX, centerY;
  if( self.innerHeight ) {
    centerX = self.innerWidth;
    centerY = self.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) {
    centerX = document.documentElement.clientWidth;
    centerY = document.documentElement.clientHeight;
  } else if( document.body ) {
    centerX = document.body.clientWidth;
    centerY = document.body.clientHeight;
  }

  var leftOffset = scrolledX + (centerX - 750) / 2;
  var topOffset = scrolledY + (centerY - 500) / 2;
  //var topOffset = 75;

  document.getElementById("mypopup").style.top = topOffset + "px";
  document.getElementById("mypopup").style.left = leftOffset + "px";
}

function fireMyPopup() {

  // Activates popup
  myPopupRelocate();
  document.getElementById("mypopup").style.display = "block";
  //document.getElementById("modalBackground").style.display = "block";
  document.body.onscroll = myPopupRelocate;
  window.onscroll = myPopupRelocate;
}