// ### COOKIE STUFF
function getCookie(NameOfCookie) {

   // alert(document.cookie);
   // First we check to see if there is a cookie stored.
   // Otherwise the length of document.cookie would be zero.  
   if (document.cookie.length > 0) { 

      // Second we check to see if the cookie's name is stored in the 
      // "document.cookie" object for the page.

      begin = document.cookie.indexOf(NameOfCookie+"="); 
      if (begin != -1) { 

         // Our cookie was set. 
         // The value stored in the cookie is returned from the function.

         begin += NameOfCookie.length+1; 
         end = document.cookie.indexOf(";", begin);
         if (end == -1) end = document.cookie.length;
         return unescape(document.cookie.substring(begin, end)); 

      } 

   }
   // Our cookie was not set. 
   // The value "null" is returned from the function.
   return null; 

}

function setCookie(NameOfCookie, value, expiredays) {

   // Three variables are used to set the new cookie. 
   // The name of the cookie, the value to be stored,
   // and finally the number of days until the cookie expires.
   // The first lines in the function convert 
   // the number of days to a valid date.

   var ExpireDate = new Date ();
   ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

   // The next line stores the cookie, simply by assigning 
   // the values to the "document.cookie" object.
   // Note the date is converted to Greenwich Mean time using
   // the "toGMTstring()" function.


   // alert( NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString()) );
   document.cookie = NameOfCookie + "=" + escape(value) + "; path=/" + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
   // alert( document.cookie );

}

function delCookie (NameOfCookie) {

   // The function simply checks to see if the cookie is set.
   // If so, the expiration date is set to Jan. 1st 1970.

   if (getCookie(NameOfCookie)) {

      document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";

   }

}

function ShowCookie()
{
   // alert( document.cookie ); 
   myCookie = getCookie('code');
   alert( myCookie ); 

}

function SetCookie(name)
{
   setCookie('code',name,90);
}

var cookieRedirect = false;
var myLocation = document.location.toString();
codeIndex = myLocation.indexOf("code=");

// ### WAS CODE PASSED
if (codeIndex != "-1")
   {
   // ### PARSE OUT CODE VALUE
   theRest = myLocation.substr(codeIndex, myLocation.length);
   amperIndex = theRest.indexOf("&");
   hashIndex = theRest.indexOf("#");
   if (amperIndex != "-1")
      {
      myCode = theRest.substr(5,amperIndex - 5)  
      }
   else
      {
		  if ( hashIndex != "-1" )
		  {
	          // found hash in URL
			  myCode = theRest.substr(5,hashIndex - 5)  
		  }
		  else
		  {
			// no hash in URL
		    myCode = theRest.substr(5,theRest.length)  
		  }
      }

   // #### SET CODE COOKIE AND REF COOKIE ONLY IF NONE EXISTS

   if (getCookie("code"))
   {
		//do nothing	
   }
   else
   {
	   cookieRedirect = true;
	   setCookie('code',myCode,90);
	   var myReferrer = escape(location.href);
	   setCookie('ref',myReferrer,90);
   }

}
   
// Check for Keyword in URL
keywordIndex = myLocation.indexOf("kw=");
if (keywordIndex != "-1")
   {
   // ### PARSE OUT CODE VALUE
   theRest = myLocation.substr(keywordIndex, myLocation.length);
   amperIndex = theRest.indexOf("&");
   hashIndex = theRest.indexOf("#");
   if (amperIndex != "-1")
      {
      myKeyword = theRest.substr(3,amperIndex - 3)  
      }
   else
      {
		  if ( hashIndex != "-1" )
		  {
	          // found hash in URL
			  myKeyword = theRest.substr(3,hashIndex - 3)  
		  }
		  else
		  {
			// no hash in URL
		    myKeyword = theRest.substr(3,theRest.length)  
		  }
      }

   // #### SET KEYWORD COOKIE ONLY IF NONE EXISTS
   if (getCookie("keyword"))
   {
   	//do nothing for now
   }
   else
   {
	   setCookie('keyword',myKeyword,90);
   }

}

   
// Check for AffiliateID ("aff") in URL
keywordIndex = myLocation.indexOf("aff=");
if (keywordIndex != "-1")
   {
   // ### PARSE OUT CODE VALUE
   theRest = myLocation.substr(keywordIndex, myLocation.length);
   amperIndex = theRest.indexOf("&");
   hashIndex = theRest.indexOf("#");
   if (amperIndex != "-1")
      {
      myKeyword = theRest.substr(4,amperIndex - 4)  
      }
   else
      {
		  if ( hashIndex != "-1" )
		  {
	          // found hash in URL
			  myKeyword = theRest.substr(4,hashIndex - 4)  
		  }
		  else
		  {
			// no hash in URL
		    myKeyword = theRest.substr(4,theRest.length)  
		  }
      }

   // #### SET AFF COOKIE ONLY IF NONE EXISTS
   if (getCookie("aff"))
   {
   	//do nothing for now
   }
   else
   {
	   setCookie('aff',myKeyword,90);
   }
   
   
}

function IsURLValuePresent(key) {
	urlKey = key  + "=";
	currentLocation = document.location.toString();
	keyIndex = currentLocation.indexOf(urlKey);
	
	if(keyIndex != "-1") {
		return true;
	} else {
		return false;
	}
}

function GetURLValue(key) {
	urlKey = key  + "=";
	currentLocation = document.location.toString();
	keyIndex = currentLocation.indexOf(urlKey);
	if(keyIndex != "-1") {
		remainingLocationString = currentLocation.substr(keyIndex, currentLocation.length);
		amperIndex = remainingLocationString.indexOf("&");
		hashIndex = remainingLocationString.indexOf("#");

		if(amperIndex != "-1") {
			value = remainingLocationString.substr(urlKey.length, (amperIndex - urlKey.length));
		} else {
			if(hashIndex != "-1") {
				value = remainingLocationString.substr(urlKey.length, (hashIndex - urlKey.length));
			} else {
				value = remainingLocationString.substr(urlKey.length, remainingLocationString.length);
			}
		}
		
	} else {
		return null;
	}
	
	//one last tweak decode a URL encoded value
	value = unescape(value.replace(/\+/g,  " "));
	return value;	
}

if (cookieRedirect == true) {
	var newLocation = "http://enroll.portal2learn.com/CookieRewrite.aspx?";
	if (IsURLValuePresent("code")) {
		newLocation += "code=" + GetURLValue("code") + "&";
	}
	if (IsURLValuePresent("kw")) {
		newLocation += "kw=" + GetURLValue("kw") + "\r\n"  + "&";
	}
	if (IsURLValuePresent("aff")) {
		newLocation += "aff=" + GetURLValue("aff") + "&";
	}
		newLocation +="ref=" + escape(document.location) + "&";
	newLocation += "refurl=" + window.location.toString().substr(0,window.location.toString().indexOf("?"));
	window.location = newLocation;
}
   
