/**
* ForwardView subscription popup user experiance
* author: adestefa@us.ibm.com
* lastupdate: 5:42 PM 11/11/2008
* more info see: /ReadMe.txt
* 
*v.1.2
* - 2:34 PM 12/1/2008: created DELAY_TIMER variable for delay control.
* - 8:41 PM 12/4/2008: added SEENSUBPOPUP to limit popup to a single instance
*
* Data members:
* ------------------
* [LOG]
*    - verbose validation when true using alert box  
* [DECLINE_TIMER]
*    - delay in milliseconds before taking user to previous article
*  [NEWSUBSCRIPTION]
*    -  hash of subscription form URLs per country
*  [PREVIOUSCONTENT] 
*    - hash of archive URLs by type for use with no thanks action handler
*
* Methods:
* ------------
* authenticate()
*    - show subscription overlay to users not subscribed yet.
*
* subscribe()
*    - accept new subscription request callback
*
* noThanks()
*    - user declines subscription request and is redirected to archive
*
* readForwardView()
*    - send user to read subscription url
*
*
*/

var LOG, DECLINE_TIMER, PREVIOUSCONTENT, NEWSUBSCRIPTION, SEENSUBPOPUP;



/** delay in milliseconds before taking user to previous article when user declines new subscription */
DECLINE_TIMER = 15000;

/** Has the user seen the subscription popup? */
SEENSUBPOPUP = 0;


/** verbose validation with alerts */
LOG = false;


/** subscription form urls by locale */
NEWSUBSCRIPTION = [];
/** en us */
NEWSUBSCRIPTION[1] = "http://ibm.com/vrm/subscriptions/10041/USEN/FV";
/** en ca */
NEWSUBSCRIPTION[2] = "http://ibm.com/vrm/subscriptions/10041/CAEN/FV";


/** previous issues */
PREVIOUSCONTENT = [];
/** en us */
PREVIOUSCONTENT[1] = "http://www.ibm.com/businesscenter/cpe/html0/160424.html?subkey=subscribe";
/** en ca */
PREVIOUSCONTENT[2] = "http://www.ibm.com/businesscenter/cpe/html0/169327.html?subkey=subscribe";





/** brute force performance improvement over  ibmCommon.Overlays.close()    */
function hideOverlay(overlay){
   /** find this overlay's standard close link and click it */
   jQuery('div#'+overlay+'  a.ibm-common-overlay-close').click();
}



/**
* authenticate subscription email url
*  - display subscription overlay when unauthorized user found (not from drive to web email)
*/
function authenticate(){
    var browser_url, subkey, key;
	
	/** get current loaded URL */
	browser_url = document.location.href;
	/** try to retreive subscription key from URL */
	subkey = getURLValue('subkey');
	/** real key */
	key = "subscribe";
	

	/** only show popup one time */
	if(SEENSUBPOPUP == 0 ){
	
	/** validate provided subscription key */
	if(subkey != null){
	  
	  if(subkey == key){
		/** authorized user */
	     LOG?alert("Correct Subscription key"):"";
		 jQuery('#msgwin').html('Subscription confirmation, welcome back.');
		 return true;
	   
	   }else{
		/** invalid subscription key */
	     LOG?alert("Invalid Subscription key found"):"";
	     ibmCommon.Overlays.show('overlay-nosub');
		 return false;
	   }
	
	 
	}else{
	/** missing subscription key */ 
     LOG?alert("Missing Subscription key"):""
	 ibmCommon.Overlays.show('overlay-nosub');
	 return false;
	}
   
    SEENSUBPOPUP = 1;
   
   
   }
	return true;
}


/**
* 'nosub' overlay button callback handlers
* -----------------------------------------------
*/



/**  accept new subscription button click callback */
function subscribe(){
  
	/** find locale in url and set new subcription form url */
    var locale = getURLValue('cid');
	/** here locale should be an integer index value we map to list */
	if(locale != null && locale != ""){
		location.href = NEWSUBSCRIPTION[locale];
	}else{
		/** can not find locale default to en ca */
	    location.href = NEWSUBSCRIPTION[2];
	}
	
}

/**  decline new subscription buton click callback */
function decline(){
	/** close the subscription overlay */
	hideOverlay('overlay-nosub');
	/** show second popup */
	ibmCommon.Overlays.show('overlay-decline');
	
	setTimeout(function(){
	        
			/** find locale in url and set new subcription form url */
			var locale = getURLValue('cid');
			/** here locale should be an integer index value we map to list */
			if(locale != null && locale != ""){
				location.href = PREVIOUSCONTENT[locale];
			}else{
				/** can not find locale default to en ca */
				location.href = PREVIOUSCONTENT[2];
			}
	
	
		},DECLINE_TIMER);
	
}


/**  landing on an article page and click decline new subscription buton callback 
 *    here we just hide the popup when they decline a new subscription
 */
function decline_article(){
	/** close the subscription overlay */
	hideOverlay('overlay-nosub');
	
	
}



/**
*
* 'decline' overlay callback handlers
* -----------------------------------------------
*/

/** Drive user to read subscription  (view now button click)*/
function readForwardView(){
    
	hideOverlay('overlay-decline');
	
		var locale = getURLValue('cid');
			/** here locale should be an integer index value we map to list */
			if(locale != null && locale != ""){
				location.href = PREVIOUSCONTENT[locale];
			}else{
				/** can not find locale default to en ca */
				location.href = PREVIOUSCONTENT[2];
			}
	
	
	
}


/**
* URL/cookie support functions 
* -----------------------------------------------
 */
function getURLValue(name)
{
  return getNVP(window.location.search, "&", "=", name);
}
function getNVP(nvps, pSeparator, nvSeparator, name)
{
  var value = null;
  var offset = -1;
  var end = -1;

  if (nvps != null)
  {
    name += nvSeparator;
    offset = nvps.indexOf(name);
    if (offset >= 0)
    {
      end = nvps.indexOf(pSeparator, offset);
      if (end < 0)
      {
        end = nvps.length;
      }
      value = nvps.substring(offset + name.length, end);
    }
  }

  return value;
}

