

/* BE AFFILIATE PROGRAM FUNCTIONALITY  --------------------------------------------------------------- 

These scripts work in conjunction with users who are being redirected form the shareasale.com website 
(affiliate program). When coming from shareasale.com, there  will be a "?SSAID=" prefix along with a 
unique number sequence at the end of the url:

http://www.boudoiressentials.com/?SSAID=174539

This is the affiliate id and allows us to understand whether a user came in from an affiliate link.

If the special URL is present, we need to supress the "links" button in the top nav (sitewide). We also 
need to hide the links page in the sitemap. We also need to display the 'Promotion Code = <>" under the help box at the bottom of the category tree 
footer in the Products pages. This is a phone tracking code so that if an affiliate sale comes in but the user calls the 877 number to place the order that the proper affiliate is credited for the sale.



When home page loads, we will need to do the following:

1. Check to see if the url contains the "?SSAID=" prefix or if the cookie exists.
	A. If the cookie already exists, compare the promo code and hide/show the various page elements.
		a. If the promo code doesn't match the code stored in the cookie, update the cookie for the new variable, 
		b. If the promo code matches the promo code stored in the cookie, do nothing.
	B. If the cookie doesn't already exist, create the cookie (6 months expiration)

2. If the cookie exists, hide the "links" button in the main nav bar(sitewide). ("links" button with unique ID, then use display: none? or add another stylesheet?)

3. If the cookie exists and the user is on the "Sitemap" page, hide the "links" link in the sitemap. ("links" text with unique ID, then use display: none? or add another stylesheet?)

4. In the products section, if the user is an affiliate, display the user code beneath the toll-free area.



NOTES:

1. Cookies/Javascript need to be activated for this functionality to work

2. Once the cookie is set in that browser, the special string in the code does not need to be there for functionality to take place.


------------------------------------------------------------------------------------------------ */




//  ********************* General Cookie handling *********************

function getCookieVal (offset) {  
   var endstr = document.cookie.indexOf (";", offset);  
   if (endstr == -1)    
      endstr = document.cookie.length;  
   return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {  
   var arg = name + "=";  
   var alen = arg.length;  
   var clen = document.cookie.length;  
   var i = 0;  
   while (i < clen) {    
      var j = i + alen;    
      if (document.cookie.substring(i, j) == arg)      
         return getCookieVal (j);    
      i = document.cookie.indexOf(" ", i) + 1;    
      if (i == 0) break;   
   }  
   return null;
}

function SetCookie (name, value) {  
   var argv = SetCookie.arguments;  
   var argc = SetCookie.arguments.length;
   // get current date and time
   now= new Date();  
   // Set expiration date for 90 days...
   var expires = new Date();
   expires.setTime(now.getTime() + 24*60*60*90*1000);
   //var path = (argc > 3) ? argv[3] : null;  
   var path = "/";
   //var domain = (argc > 4) ? argv[4] : null; 
   var domain = ".boudoiressentials.com"; 
   var secure = (argc > 5) ? argv[5] : false;  
   document.cookie = name + "=" + escape (value) + 
      ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
      ((path == null) ? "" : ("; path=" + path)) +  
      ((domain == null) ? "" : ("; domain=" + domain)) +    
      ((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {  
   var exp = new Date();  
   exp.setTime (exp.getTime() - 1);  
   // This cookie is history  
   var cval = GetCookie (name);  
   document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}





//---- DETERMINE IF THERE"S A PROMO CODE AND WHAT IT IS ----------------

var promoCode= null;

// Check to see if the cookie exists. If it does, extract the value. If not, returns null.
var cookie_promoCode = GetCookie('affiliate_code');

// if the cookie (and a promo code in the cookie) exists, assign the value to the "promoCode" variable.
if (cookie_promoCode != null) {
	promoCode = cookie_promoCode;
}
	
var pageURL = location.href;

// Check to see if there is a promocode appended to the url.
// If so, returns the numbered position of the first character in the code prefix
// If not, returns a "-1"
var promoCode_startPos = pageURL.indexOf('?SSAID=');
	
// If the promo code does exist as part of the url, extract it into this variable
var url_promoCode = null;
if (promoCode_startPos != -1) {
	// when extracting, account for "?SSAID=" (7 characters)
	url_promoCode = pageURL.substring(promoCode_startPos + 7);
}

// if the promo code exists as part of the URL, assign the value to the "promoCode" variable.
if (url_promoCode != null) {
	promoCode = url_promoCode;
}

// If either the code exists as part of the url or in a cookie...
if (promoCode != null) {		
	
	// If the cookie doesn't exist or if it doesn't match the promoCode delivered as part of the url (and if there is a promoCode delivers as part of the URL), set the cookie to the value of the new promoCode
	if ((cookie_promoCode == null) || ((cookie_promoCode != url_promoCode) && (cookie_promoCode != null))) {
		SetCookie ('affiliate_code', promoCode);
	}

}



// Called in the <head> of each html doc

function insertAffiliateStyles() {
	
	// If either the code exists as part of the url or in a cookie...
	if (promoCode != null) {		
		// insert additional CSS overriding certain styles in "global.css"
		document.write('<link href="/css/global_affiliateAdjust.css" rel="stylesheet" type="text/css" />');
	}
}



// Called on MIVA store pages and FAQ Pages (wherever the LiveHelp box exists)

function displayPromoCode() {
	
	// If either the code exists as part of the url or in a cookie...
	if (promoCode != null) {		
		document.getElementById('promoCode').innerHTML= ("<span class=\"affiliateCode\">Promo Code: " + promoCode + "</h3>");
	}	
}

