//Copyright 2005 BGT Partners (www.bgtpartners.com)
//v1.4 - USE ONLY FOR IPT READINESS

function checkCookie(cn) {

	var cidEM = getQueryVariable("cid");
	var emPin = getQueryVariable("em");
	var cmp = getQueryVariable("cmp");

	if (emPin != "undefined"){
	getPrepopData();		
	}
	else{
		var formPage="avaya_newsletter_registration.html";
		var prepop;
		prepop = getQueryVariable('prepop');
		if (prepop == 1) {
			document.location.href = formPage + "?prepop=" + getQueryVariable('prepop') + "&cid=" + getQueryVariable('cid') +"&cmp="+getQueryVariable('cmp') + "&first_name=" + getQueryVariable('first_name') + 
			"&last_name=" + getQueryVariable('last_name') + "&middle_name=" + getQueryVariable('middle_name') + "&company=" + getQueryVariable('company') + "&country=" + getQueryVariable('country') + 
			"&phone=" + getQueryVariable('phone') + "&address1=" + getQueryVariable('address1') + "&address2=" + getQueryVariable('address2') + "&city=" + getQueryVariable('city') + "&area=" + getQueryVariable('area') + 
			"&zip=" + getQueryVariable('zip') + "&pin=" + getQueryVariable('pin') + "&email=" + getQueryVariable('email') + "&contsysid=" + getQueryVariable('contsysid') + "&compsysid=" + getQueryVariable('compsysid') +
			"&campaigncode=" + getQueryVariable('campaigncode') + "&promocode=" + getQueryVariable('promocode') + "&table=" + getQueryVariable('table');}
			
		else if ( readCookie(cn)) {
			prepop = 1;
			
			if ( eval("readCookie('newslet_short_exp')") ) {
				var exp = eval("readCookie('newslet_short_exp')");
				var exp = Date.parse(exp);
				var timenow = new Date();
				var timenow = Date.parse(timenow);
				if ( timenow <= exp ) {
					//populate_MDB();
					document.location.href = formPage + "?prepop=" + prepop + "&cid=" + getQueryVariable('cid') +"&cmp="+getQueryVariable('cmp'); 
				}
				else { document.location.href = formPage + "?prepop=" + prepop + "&cid=" + getQueryVariable('cid') +"&cmp="+getQueryVariable('cmp'); }
			}
			else { document.location.href = formPage + "?prepop=" + prepop + "&cid=" + getQueryVariable('cid') +"&cmp="+getQueryVariable('cmp'); }
		}
		
		else { prepop = 0; document.location.href = formPage + "?prepop=" + prepop +  "&cid=" + getQueryVariable('cid') +"&cmp="+getQueryVariable('cmp') +"&cmp="+getQueryVariable('cmp'); }
	}
}

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("&");
		var ok = false;
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) {
				ok = true; return pair[1];
			}
		}
		if (!(ok)) { return "undefined"; }
	}
	else { return "undefined"; }
}

function getPrepopData() {
	//Accomodate staging environment
	var staging = unescape(self.location).indexOf('intact.avaya.com');
	if (staging == -1)
                {var server = "http://www1.avaya.com";}
	else    {var server = "http://intact.avaya.com:9094";}

	var ScriptPath = server + "/cgi-bin/bgt/mdb_ppbizcon.pl";
	var cid = getQueryVariable('cid');
	var emValue = getQueryVariable("em");
//		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='bk' value='1'>");
	document.writeln("   <input type='hidden' name='em' value='" + emValue + "'>");
	document.writeln("   <input type='hidden' name='campaignID' value='" + cid + "'>");
	document.writeln("  </form>");
	document.writeln(" </body>");
	document.writeln("</html>");
	document.tempForm.submit();
	}


function populate_MDB() {
	
	var unk = "newslt";

	//Accomodate staging environment
	var staging = unescape(self.location).indexOf('intact.avaya.com');
	if (staging == -1)
                {var server = "http://www1.avaya.com";}
	else    {var server = "http://intact.avaya.com:9094";}

	var ScriptPath = server + "/cgi-bin/bgt/mdb_process.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='bk' value='1'>");
		document.writeln("   <input type='hidden' name='prepop' value='1'>");
		document.writeln("   <input type='hidden' name='session' value='1'>");
		document.writeln("   <input type='hidden' name='unk' value='" + unk + "'>");
		document.writeln("   <input type='hidden' name='campaignID' value='" + cid + "'>");

		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] == "campaignID")	{ }
			else if (pair[0] == "promocode") { }
			else if (pair[0] == "compsysid") { } //bf 4.28.06
            else if (pair[0] == "unk")		{ }
			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;
}
