
// sets a new cookie for PLCode if one is specified in the querystring
function SetPLCodeCookie() {

	// Parse the current page's querystring
	var qs = new Querystring();
	
	var PLCode;


	if (qs.contains("plcode"))
	{
		PLCode = qs.get("plcode");
	
		// From the full hostname, we need to determine the domain (without the subdomain)
		// because our cookie needs to work across multiple subdomains of the domain
		var hostname = location.hostname;
		var domain = "";
		
		if (hostname.indexOf(".") > -1)
		{
			// in this case and at this point, we know that our URLs will ALWAYS be <subdomain>.<domain>
			// so the domain without the subdomain is simply the remainder of the hostname after the first "."
			domain = hostname.substring(hostname.indexOf(".") + 1);
		}
		
		if (GetCookie("plcode") != PLCode)
		{
			if (domain != "")
			{
				document.cookie = "plcode=" + PLCode + ";path=/;domain=" + domain;
			}
			else
			{
				document.cookie = "plcode=" + PLCode + ";path=/";
			}
		}
	}
	
	// on every page, check for the existance of a PLCode cookie
	// If found, need to update ANY url on the page which has PLCode as a parameter
	if ((GetCookie("plcode") != null) && (GetCookie("plcode") != ''))
	{
		PLCode = GetCookie("plcode");
		
		$("a[href*=PLCode]").each(function() {
			$(this).attr("href", ReplaceQueryString($(this).attr("href"), 'PLCode', PLCode));
		});
	}

}


// returns the value of a specified cookie
function GetCookie (cookieName)
{
  var myCookie = document.cookie.toLowerCase().match ( '(^|;) ?' + cookieName + '=([^;]*)(;|$)' );

  if ( myCookie )
    return (unescape (myCookie[2]));
  else
    return null;
}

/*
 * (c)2006 Jesse Skinner/Dean Edwards/Matthias Miller/John Resig
 * Special thanks to Dan Webb's domready.js Prototype extension
 * and Simon Willison's addLoadEvent
 *
 * For more info, see:
 * http://www.thefutureoftheweb.com/blog/adddomloadevent
 * http://dean.edwards.name/weblog/2006/06/again/
 * http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 * 
 *
 * To use: call addDOMLoadEvent one or more times with functions, ie:
 *
 *    function something() {
 *       // do something
 *    }
 *    addDOMLoadEvent(something);
 *
 *    addDOMLoadEvent(function() {
 *        // do other stuff
 *    });
 *
 */
 
addDOMLoadEvent = (function(){
    // create event function stack
    var load_events = [],
        load_timer,
        script,
        done,
        exec,
        old_onload,
        init = function () {
            done = true;

            // kill the timer
            clearInterval(load_timer);

            // execute each function in the stack in the order they were added
            while (exec = load_events.shift())
                exec();

            if (script) script.onreadystatechange = '';
        };

    return function (func) {
        // if the init function was already ran, just run this function now and stop
        if (done) return func();

        if (!load_events[0]) {
            // for Mozilla/Opera9
            if (document.addEventListener)
                document.addEventListener("DOMContentLoaded", init, false);

            // for Internet Explorer
            /*@cc_on @*/
            /*@if (@_win32)
                document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
                script = document.getElementById("__ie_onload");
                script.onreadystatechange = function() {
                    if (this.readyState == "complete")
                        init(); // call the onload handler
                };
            /*@end @*/

            // for Safari
            if (/WebKit/i.test(navigator.userAgent)) { // sniff
                load_timer = setInterval(function() {
                    if (/loaded|complete/.test(document.readyState))
                        init(); // call the onload handler
                }, 10);
            }

            // for other browsers set the window.onload, but also execute the old window.onload
            old_onload = window.onload;
            window.onload = function() {
                init();
                if (old_onload) old_onload();
            };
        }

        load_events.push(func);
    }
})();


/*
*  set the organic flag cookie if this parameter is set in the query string, or the user arrived with no PLCode.
*  this function should get called after the SetPLCodeCookie() function.
*/
function SetOrganicFlagCookie() {
 // set this flag only once upon first entering the marketing site
if (typeof(url) == 'undefined'){
	url =  location.pathname.toLowerCase();
 }
 if (GetCookie("organic") == null) {
  var isOrganic = false;
  var qs = new Querystring();
  // check if the PLCode cookie has been set, if not then this user arrived without a PLCode and is organic
  // Also, certain pages they want to mark as NOT organic even without a PLCode
  if (
  url.indexOf("american-kennel-club") < 0 && 
  ((GetCookie("plcode") == null) || (GetCookie("plcode").length == 0) || (!qs.contains("plcode"))) 
  )
  {
   isOrganic = true;
  } else {
   // the user has arrived with a PLCode, in this scenario check if the organic flag was passed as well
   if (qs.contains("organic")) {
    if (qs.get("organic").indexOf("true") != -1) {
     isOrganic = true;
    }
   }
  }
  // set organic cookie flag if this user is organic
  if (isOrganic) {
   SetCookie("organic", "true", null, "/");
  } else {
   SetCookie("organic", "false", null, "/");
  }
 }
}

