// +---------+---------+---------+---------+---------+---------+
// Transfer query string parameters to cookie.
// Check cookie central for co-branding.
//
// Written by: Greg Traub
// Last modified: Greg Traub (8/5/04)
//
// 2006-10-09 - Gregg Obst - Removed the call to the cookie engine since the MSN deal is now over. 
// 2008-03-25 - Steve Yeakel - Added smartcode for MDP project. 
// +---------+---------+---------+---------+---------+---------+

var glob_msn_header = false;

function munch() {

	var TheParameters, TheParametersArray, param, k;
	var crumbs, crumb;
	var loc, protocol;
	var uof_keycode, uof_email, uof_smartcode, uof_referurl, cm_mmc = "", uof_source = "", uid = "";
	
	// Get document location reference.
	loc = document.location.href;
	
	// Derive protocol.
	if (loc.substring(0, 5).toUpperCase() == "HTTPS") {
		protocol = "https";
	} else {
		protocol = "http";
	}
	
	// Check query string parameters.
	TheParameters = document.location.search.substr(1);
	TheParametersArray = TheParameters.split("&");
	k = TheParametersArray.length;
	
	for (i=0; i<k; i++) {
	
		param = unescape(TheParametersArray[i]);
		
		// Keycode parameter passed, set crumb.
		if (param.substring(0, 7).toUpperCase() == "KEYCODE") {
			uof_keycode = param.substr(8);
			document.cookie = "keycode=" + escape(uof_keycode) + ";path=/";
		}

		// Keycode parameter passed, set crumb.
		if (param.substring(0, 6).toUpperCase() == "EMAIL1") {
			uof_email = param.substr(7);
			document.cookie = "email1=" + escape(uof_email) + ";path=/";
		}		
		// Source parameter passed, set crumb.
		if (param.substring(0, 6).toUpperCase() == "SOURCE") {
			uof_source = param.substr(7);
			document.cookie = "source=" + escape(uof_source) + ";path=/";
		}
		if (param.substring(0, 9).toUpperCase() == "MSNSOURCE") {
			uof_source = param.substr(10);
		}
		if (uof_source != "") {
			document.cookie = "source=" + escape(uof_source) + ";path=/";
		}
		
		// Referer URL parameter passed, set crumb.
		if (param.substring(0, 8).toUpperCase() == "REFERURL") {
			uof_referurl = param.substr(9);
			document.cookie = "referurl=" + escape(uof_referurl) + ";path=/";
		}
		
		// UID parameter passed, set crumb.
		if (param.substring(0, 3).toUpperCase() == "UID") {
			uid = param.substr(4);
			document.cookie = "uid=" + escape(uid) + ";path=/";
		}
		
		// MDP Smartcode parameter passed, set crumb.
		if (param.substring(0, 9).toUpperCase() == "SMARTCODE") {
			uof_smartcode = param.substr(10);
			document.cookie = "smartcode=" + escape(uof_smartcode) + ";path=/";
		}
		
		if (param.substring(0, 6).toUpperCase() == "CM_MMC") {
		cm_mmc  = param.substr(7);
		document.cookie = "cm_mmc=" + escape(cm_mmc) + ";path=/";
		}
		
		
	}
	
	// Referer URL parameter not passed.
	// Check if cookie crumb already exists.
	if (typeof(uof_referurl) == "undefined") {
		crumbs = document.cookie.split(";");
		k = crumbs.length;
		for (i=0; i<k; i++) {
			crumb = unescape(crumbs[i]);
			crumb = crumb.split("=");
			if (crumb[0].toUpperCase().indexOf("REFERURL") != -1) {  // Referer URL crumb found in cookie.
				uof_referurl = unescape(crumb[1]);
			}
		}
		if (typeof(uof_referurl) == "undefined") {
			// Still couldn't find a referer URL.
			// Try to get it from DOM.
			if (document.referrer != "") {
				uof_referurl = document.referrer;
				uof_referurl = uof_referurl.split("?");
				uof_referurl = uof_referurl[0];
				document.cookie ="REFERURL=" + escape(uof_referurl) + ";path=/";
			}
		}
	}

 	// Source parameter not passed.
	// Check if cookie crumb already exists.
	if (uof_source == "") {
		crumbs = document.cookie.split(";");
		k = crumbs.length;
		for (i=0; i<k; i++) {
			crumb = unescape(crumbs[i]);
			crumb = crumb.split("=");
			if (crumb[0].toUpperCase().indexOf("SOURCE") != -1) {  // Source crumb found in cookie.
				uof_source = unescape(crumb[1]);
			}
		}
		
		/* 2006-10-09 gobst - Removed the call to the cookie engine since the MSN deal is now over. 
		if (uof_source == "") {
			// Still couldn't find a source.
			// Check with cookie central.
			window.location = protocol + "://msn.rodale.com/msnrodale.html?action=checkmsn&url=" + loc;
			return;
		}
		*/
		
	}
	
	if (uof_source.toUpperCase() == "MSNDIRECT") { glob_msn_header = true; }
	
	return;
	
}

munch();
