var divSuperContainer, divFilter, divFormNL;
var heightSuperContainer, leftSuperContainer, widthSuperContainer, widthFormNL;
var increaseTimeOut;
var cookieNameOneDay = "formNL";
var cookieNameEmail = "formNLEmailRegistered";
var currentUrl, currentHost, domainForCookie;

function mainHomeNL(toShow){
	currentUrl = document.location.href;
	currentHost = window.location.host;
	domainForCookie = currentHost.substring(currentHost.indexOf("."),currentHost.length);
	//alert("domainForCookie : " + domainForCookie);
	if(toShow){
		//alert("case toShow true");
		if(currentUrl.indexOf("myportal")==-1) {
			//alert("no myportal in url");
			var alreadyDisplayForToday = readCookie(cookieNameOneDay);
			var alreadyRegistered = readCookie(cookieNameEmail);
			//alert("cookieValue alreadyDisplayForToday = " + alreadyDisplayForToday);
			//alert("cookieValue alreadyRegistered = " + alreadyRegistered);
			if(alreadyRegistered!=null)
				return;
			if(alreadyDisplayForToday==null){
				prepareFilterDiv();
				displayDiv(divFilter,'visible');
				prepareFormDiv();
				increaseDivFilter();
				writeCookie(cookieNameOneDay,"yes",1,domainForCookie);
			}
		}
	} else {
		//alert("case toShow false");
		displayDiv(divFilter,'hidden');
		displayDiv(divFormNL,'hidden');
	}
}

function prepareFilterDiv(){
	divSuperContainer = document.getElementById('super_container');
	divFilter = document.getElementById('filter');
	divFormNL = document.getElementById('formNL');
	divFilter.style.height = "1px";
	heightSuperContainer = divSuperContainer.offsetHeight;
	//alert("heightSuperContainer : " + heightSuperContainer);
	leftSuperContainer = divSuperContainer.offsetLeft;
	//alert("leftSuperContainer : " + leftSuperContainer);
	widthSuperContainer = divSuperContainer.offsetWidth;
	//alert("widthSuperContainer : " + widthSuperContainer);
}

function prepareFormDiv(){
	widthFormNL = divFormNL.offsetWidth;
	var posX = widthSuperContainer/2 - widthFormNL/2;
	//posX+=leftSuperContainer ;
	var posY = 200;
	divFormNL.style.left = posX + "px";
	divFormNL.style.top = posY + "px";
}

function displayDiv(element,visibleValue){
	element.style.visibility = visibleValue;
}


function increaseDivFilter(){
	var filterHeight = divFilter.offsetHeight;
	if(filterHeight < heightSuperContainer){
		divFilter.style.height = filterHeight +50 + "px";
		increaseTimeOut = setTimeout('increaseDivFilter()',5);
	}
	else {
		clearTimeout(increaseTimeOut);
		displayDiv(divFormNL,'visible');
	}
}

function writeCookie(name, value, nbDays, domain) {
	var cookieString = name + "=" + escape(value);
	var domainString = "";
	if (nbDays) {
		var date = new Date();
		date.setTime(date.getTime()+(nbDays*24*60*60*1000));
		cookieString += "; expires="+date.toGMTString();
	} 
	if (domain){
		domainString = "; domain=" + escape(domain);
	}
	document.cookie = cookieString + "; path=/" + domainString;
}

function readCookie(name){
	var deb,fin;
    deb = document.cookie.indexOf(name + "=");
    if (deb >= 0) {
        deb += name.length + 1;
        fin = document.cookie.indexOf(";",deb);
        if (fin < 0) 
        	fin = document.cookie.length;
        return unescape(document.cookie.substring(deb,fin));
    }
	return null;
}

function eraseCookie(name) { 
	writeCookie(name,"",-1);
}

function sendFormNL(f){
	if(f.email.value=="" || f.email.value=="votre email")
		return false;
	var larg=820;
	var haut=750;
	var posXpop = ((screen.width)/2)-larg/2;
	var posYpop = 50;	
	param = "width="+larg+",height="+haut+",left="+posXpop+",top="+posYpop+",directories=no,menubar=no,status=no,location=no,toolbar=no,scrollbars=yes,resizable=no";
	popup = window.open("","fen",param);
	f.target="fen";

	var formBeginUrl, formEndUrl;
	var currentPath=window.location.pathname;
	//alert("currentPath :" + currentPath);

	if(currentPath.indexOf("Creargos") !=-1)
		formEndUrl = "/newsletterCreargosInscriptionViewPopupAction.do";
	else if(currentPath.indexOf("Batissor") !=-1)
		formEndUrl = "/newsletterBatissorInscriptionViewPopupAction.do";
	else
		formEndUrl = "/newsletterBatirenoverInscriptionViewPopupAction.do";
	
	if(currentHost.indexOf(".batirenover.com")!=-1 
		|| (currentUrl.indexOf("stagingpdm.lafarge.com")!=-1 && currentPath.indexOf("Batirenover") !=-1))
		formBeginUrl = "http://tools.batirenover.com";
	else if(currentHost.indexOf(".creargos.com")!=-1 
		|| (currentUrl.indexOf("stagingpdm.lafarge.com")!=-1 && currentPath.indexOf("Creargos") !=-1))
		formBeginUrl = "http://tools.creargos.com";
	else if(currentHost.indexOf(".batissor.com")!=-1 
		|| (currentUrl.indexOf("stagingpdm.lafarge.com")!=-1 && currentPath.indexOf("Batissor") !=-1))
		formBeginUrl = "http://tools.batissor.com";
	else if(currentHost.indexOf("pdm-ref.edifixio.fr") !=-1)
		formBeginUrl = "http://search-pdm.edifixio.fr";
	else
		return false;
	
	f.action = formBeginUrl + "/prwwForms" + formEndUrl;
	mainHomeNL(false);
	return true;
}
