var cpCount = 0;

function createCPdivs() {

	var cpDiv1 = document.createElement("div");
	cpDiv1.setAttribute("id", "cpDiv1");
	cpDiv1.style.width = "0px";
	cpDiv1.style.height = "0px";
	cpDiv1.style.overflow = "hidden";
	document.documentElement.appendChild(cpDiv1);

	var cpDiv2 = document.createElement("div");
	cpDiv2.setAttribute("id", "cpDiv2");
	cpDiv2.style.width = "0px";
	cpDiv2.style.height = "0px";
	cpDiv2.style.overflow = "hidden";
	document.documentElement.appendChild(cpDiv2);

}

// open new window, don't have to wait for cps to load, use: cp(passedCP)
// stay in the same window, need to wait for cps to load, use: cp(passedCP, destination)
// form submission, stay in same window, need to wait for cps to load, use: cp()
function cp(passedCP, destination) {
	if (passedCP) {loadCP(passedCP);}
	else if (passedCP && destination) {loadCP(passedCP, destination); return false;}
	else {loadCP(); return false;}
}

// LOADS CPs ONLY
function loadCP(passedCP, destination) {
	var base1 = "http://counterpixel.niveausa.com/cgi-bin/PageImp.exe?/nivea/us/ext/highlights/specials/";
	var base2 = "http://counterpixel1.niveausa.com/cgi-bin/PageImp.exe?/nivea/us/ext/highlights/specials/";
	// var promoName = "nbd_fashionrocksduanereade_registration";
	var onloadHandler;
	if (destination) {onloadHandler = "onload='cpReady(\""+destination+"\")'";}
	if (!passedCP) {passedCP = "main_submit.nopv"; onloadHandler = "onload='cpReady()'";}
	else {onloadHandler = "";}
var kk = "<img src='"+base1+promoName+"/"+passedCP+"' height='0' width='0' "+onloadHandler+">";
if (!document.getElementById("cpDiv1")) {alert("hello")};
	document.getElementById("cpDiv1").innerHTML = "<img src='"+base1+promoName+"/"+passedCP+"' height='0' width='0' "+onloadHandler+" />";
	document.getElementById("cpDiv2").innerHTML = "<img src='"+base2+promoName+"/"+passedCP+"' height='0' width='0' "+onloadHandler+" />";
	
}

function cpReady(destination) {
	cpCount++;
	if (cpCount == 2) {
		cpCount = 0; // safeguard just in case there is more than one nopv click on the page
		if (destination) {parent.location.href = destination;}
		else {document.forms[0].submit();}
	}
}

window.load = createCPdivs();