//Copyright 2005 BGT Partners (www.bgtpartners.com)
//v1.3

function checkCookie (name,urlcode) {

    var url = getURL(urlcode);

	if ( readCookie(name) ) {
		if ( eval("readCookie('" + name + "_exp')") ) {
			var exp = eval("readCookie('" + name + "_exp')");
			var exp = Date.parse(exp);
			var timenow = new Date();
			var timenow = Date.parse(timenow);
			if ( timenow <= exp ) {
				populate_MDB(url);
			}
			else { document.location.href = formPage + "?revisit=1&name=" + name + "&urlcode=" + urlcode + "&cid=" + getQueryVariable('cid'); }
		}
		else { document.location.href = formPage + "?revisit=1&name=" + name + "&urlcode=" + urlcode + "&cid=" + getQueryVariable('cid'); }
	}

	else { document.location.href = formPage + "?revisit=0&name=" + name + "&urlcode=" + urlcode + "&cid=" + getQueryVariable('cid'); }

}

function readCookie(name) {

    var CookieString = document.cookie;
    var CookieSet = CookieString.split (';');
    var SetSize = CookieSet.length;
    var CookiePieces;
    var ReturnValue = "";
    var x = 0;

	for (x = 0; ((x < SetSize) && (ReturnValue == "")); x++) {
		CookiePieces = CookieSet[x].split ('=');
		if (CookiePieces[0].substring (0,1) == ' ')
			{ CookiePieces[0] = CookiePieces[0].substring (1, CookiePieces[0].length); }
		if (CookiePieces[0] == name)
			{ ReturnValue = CookiePieces[1]; }
	}

	return ReturnValue;
}

function getQueryVariable(variable) {
	if (!(unescape(location.href).indexOf("?") == -1)) { 
		var query = location.href.split("?");
		var vars = query[1].split("&");
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) {
				return pair[1];
			}
		}
	}
	else { return "undefined"; }
}

function populate_MDB(url) {

	var ScriptPath = "/cgi-bin/bgt/mdb_premcontent.pl";
	var userdata = unescape(readCookie('userdata'));
	var cid = getQueryVariable('cid');

    var CookieString = userdata;
    var CookieSet = CookieString.split ('|');
    var SetSize = CookieSet.length;
    var pair;
	if (CookieString) {
		var visitorID = readCookie('contsysid');
		document.writeln("<html>");
		document.writeln(" <head>");
		document.writeln("  <script>history.forward();</script>");
		document.writeln(" </head>");
		document.writeln(" <body>");
		document.writeln("  <form name='tempForm' method='post' action='" + ScriptPath + "'>");
		document.writeln("   <input type='hidden' name='session' value='1'>");
		document.writeln("   <input type='hidden' name='revisit' value='1'>");
		for (var x=0; x < SetSize; x++) {
			pair = CookieSet[x].split ('&');
			if (pair[0].substring (0,1) == ' ')
				{ pair[0] = pair[0].substring (1, pair[0].length); }  //Remove any prefixing space from field name
			pair[1] = pair[1].replace(/\+/g, ' ');	//Replace + signs with a space
			if (pair[0] == "visitorID")			{ document.writeln("   <input type='hidden' name='" + pair[0] + "' value='" + visitorID + "'>"); }
			else if (pair[0] == "campaignID") 	{ document.writeln("   <input type='hidden' name='" + pair[0] + "' value='" + cid + "'>"); }
			else if (pair[0] == "url")			{ document.writeln("   <input type='hidden' name='" + pair[0] + "' value='" + url + "'>"); }
			else { document.writeln("   <input type='hidden' name='" + pair[0] + "' value='" + pair[1] + "'>"); }
		}							
		document.writeln("  </form>");
		document.writeln(" </body>");
		document.writeln("</html>");
		document.tempForm.submit();
	}
	return true;
}