/*
	These functions must come from the blocks of code at the bottom of the page
*/
var manual_cm_mmc = null;

function generateMmcFromNats(txt) {
	firstColon = txt.indexOf(":");
	semicolon = txt.indexOf(";");
	secondColon = txt.indexOf(":", firstColon + 1);
	
	if (semicolon < firstColon) {
		campaign = txt.substring(semicolon + 1, firstColon);
		affiliate = txt.substring(0, semicolon);
	} else {
		campaign = null;
		affiliate = txt.substring(0, firstColon);
	}
	
	program = txt.substring(firstColon + 1, secondColon);
	return program + "-_-" + affiliate + (campaign ? ("-_-" + campaign + "-_-na") : "-_-na-_-na");
}
			
function getCookie (name) {
	var dc = document.cookie;
    var cname = name + "=";

    if (dc.length > 0) {
    	begin = dc.indexOf(cname);
      	if (begin != -1) {
        	begin += cname.length;
        	end = dc.indexOf(" ", begin);
        	if (end == -1) end = dc.length;
        	return unescape(dc.substring(begin, end));
        }
	}
    
    return null;
}

// sso redirection - non javascript clients will not benefit from this - session cookie insures they don't get sent back over and over again
ssoCookie = getCookie('sso_initial');
if (!ssoCookie) {
	pieces = document.location.hostname.split(".").reverse();
	newLocation = "http://sso.kink." +  pieces[0] + (document.location.port != 80 ? ":" + document.location.port : "") + "/sso/check?ret=" + escape(document.location.href);
	document.cookie = "sso_initial=check; path=/";
	document.location = newLocation;
}

//	nats cookie -> mmc management
natsCookie = getCookie('nats');
if (natsCookie) {
	try {
		manual_cm_mmc = generateMmcFromNats(natsCookie);
	} catch(error) {
		manual_cm_mmc = "error-_-" + natsCookie + "-_-na-_-na";
	}
}
