function init ()
{
}

var largeur;
var hauteur;
var cookName = 'MBFcookOptin';
var bDACshow = true;
var tempoClose;
var ie;

window.onresize = askPos;	
function askMail (vHome)
{
	sHome=(vHome!=undefined)?true:false;
	ie = document.all?true:false;
	//ie = true;

	NavName 												= navigator.appName;
	NavVers 												= navigator.appVersion;
	Nav 													= navigator.userAgent;

	if(document.cookie.indexOf(cookName+'=')!=-1) return false;
	
	var DAC 						= document.createElement('div');
	DAC.id 							= 'DAC';
	DAC.style.height				= '140px';
	
	var DACtext 					= document.createElement('div');
	DACtext.id = 'DACtext';
	DACtext.innerHTML 				= 'Pour recevoir tous nos bons plans, inscris toi vite !';

	var DACpastille					= document.createElement('div');
	DACpastille.id = 'DACpastille';
	
	var outForm = document.createElement("form");
	outForm.setAttribute("method", "post");
	outForm.setAttribute("action", "http://www.example.com/test.php");
	
	var formTxt = document.createElement("input");
	formTxt.id = 'mailInput';
	formTxt.setAttribute("type", "text");
	formTxt.setAttribute("name", "mailOptin");
	formTxt.setAttribute("size", "60");
	formTxt.setAttribute("value", "Entre ton adresse email ici ! ");
	
	formTxt.onclick = function () {
		this.value = '';
		this.focus();
	}
	
	outForm.appendChild(formTxt);
	formTxt.style.top = "-19px";
	
	if (!ie) formTxt.style.paddingTop = '0px';

	var formValid = document.createElement("input");
	formValid.id = 'mailValid';
	formValid.setAttribute("type", "image");
	formValid.setAttribute("name", "mailOptinValid");
	formValid.setAttribute("src", path_prefix+"images/MBF_confirmation_mailValid.png");
	formValid.setAttribute("border",0);
	
	outForm.onsubmit= function () {
		if(verifMail()){
			collecteEmail(formTxt.value);
		}
		return false;
	}
	//outForm.submit();
	var retourValid = document.createElement("div");
	retourValid.id = 'retour';
	
	var retourText 					= document.createElement('div');
	retourText.id 					= 'retourText';
	retourText.innerHTML 			= ' <br/>Merci <br/> Votre email a bien &eacute;t&eacute; enregistr&eacute;';

	
	var contentModal = document.createElement("div");
	contentModal.id = 'contentModal';
	
	outForm.appendChild(formValid);
		
	document.body.appendChild(DAC);
	document.body.appendChild(contentModal);
	document.body.appendChild(retourValid);
	retourValid.appendChild(retourText);
	
	var retourClose = document.createElement('div');	
	retourClose.id = 'retourClose';
	retourValid.appendChild(retourClose);
	retourClose.onclick = function () {selfClose ();}


	if (ie) document.getElementById("contentModal").style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=85)";
	else document.getElementById("contentModal").style.opacity = 0.85;
	
	DAC.appendChild(DACtext);
	DACtext.appendChild(outForm);
	DACtext.appendChild(DACpastille);

	if (sHome)
	{
		var DACclose = document.createElement('div');	
		DACclose.id = 'DACclose';
		DAC.appendChild(DACclose);
		DACclose.onclick = function ()
		{
			DACmoving ();
			document.getElementById('DACclose').style.backgroundPosition = (bDACshow)?'0px -32px':'0px 0px';
		}
	}

	askPos ();
}	
function askPos ()
{
	largeur					= document.documentElement.clientWidth;
	hauteur					= document.documentElement.clientHeight;
	
	var divFound 			= document.getElementById('DAC');
	divFound.style.top		= hauteur -140 + 'px';
	divFound.style.left 	= '0px';
	divFound.style.width 	= largeur+'px';

	var divRetour 			= document.getElementById('retour');
	divRetour.style.top		= (hauteur -130)/2 + 'px';
	divRetour.style.left 	= (largeur-460)/2+'px';
	
	var divModal 			= document.getElementById('contentModal');
	divModal.style.height	= hauteur + 'px';
	divModal.style.width 	= largeur +'px';
	
	//retourMail ();
}
function verifMail() {
	var email=document.getElementById('mailInput').value;
	if(!re_email.exec(email)){
		alert('E-mail invalide');
		return false;
	}
	else {createCookie(cookName,'optinOK',30);}
	return true;
}
var re_email = /^[a-z0-9\-_\.]+@[a-z0-9\-_\.]+$/i;
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
var tempoDiv;
function DACmoving ()
{
	var div2move 	= document.getElementById('DAC');
	var divHeight 	= parseInt (div2move.style.height);
	var divPos 		= parseInt (div2move.style.top);
	if (bDACshow)
	{
		if (divHeight > 42)
		{
			var divDecal 			= Math.max(42,((divHeight-42)*0.5));
			div2move.style.top 		= divPos + divDecal + 'px';
			div2move.style.height 	= divDecal + 'px';
			tempoDiv 				= setTimeout('DACmoving()', 64);
		}
		else
		{
			div2move.style.top 		= hauteur - 42 + 'px';
			clearTimeout(tempoDiv);
			document.getElementById('DACpastille').style.display = 'none';
			document.getElementById('DACtext').style.display = 'none';
			bDACshow = false;
		}
	}
	else
	{
		if (divHeight < 140)
		{
			var divDecal 			= Math.min(140,Math.round((140 - divHeight)*0.5));

			div2move.style.top 		= divPos - divDecal + 'px';
			div2move.style.height 	= divHeight + divDecal + 'px';
			tempoDiv 				= setTimeout('DACmoving()', 64);
		}
		else
		{
			div2move.style.top 		= hauteur - 140 + 'px';
			clearTimeout(tempoDiv);
			document.getElementById('DACpastille').style.display = 'block';
			document.getElementById('DACtext').style.display = 'block';
			bDACshow = true;
		}
	}
}
function retourMail ()
{
	if (Nav.indexOf ('Chrome') >0) var position = document.body.scrollTop;
	else var position = document.documentElement.scrollTop;
		
	document.body.style.overflow		= 'hidden';

	var divRetour 			= document.getElementById('retour');
	divRetour.style.top		= position + parseInt(divRetour.style.top) + 'px';
	divRetour.style.display	= 'block';
	
	var divModal 			= document.getElementById('contentModal');
	divModal.style.top		= position + 'px';
	divModal.style.display	= 'block';

	var divFound 			= document.getElementById('DAC');
	divFound.style.display	= 'none';
	
	tempoClose 				= setTimeout(' selfClose()', 3000);
}
function selfClose ()
{
	clearTimeout(tempoClose);
	if (ie) crosoftFadeDown();
	else otherFadeDown();
}
var temps = 32;
var nOpacity = 85;
var tempo_fade;
function crosoftFadeDown(){
if (nOpacity > 0) 
	{
	tempo_fade = setTimeout('crosoftFadeDown()',temps); // On passe cette fonction en boucle tant que le cache n'a pas disparu
	nOpacity -= 5; 
	document.getElementById("contentModal").style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + nOpacity + ")";
	document.getElementById("retour").style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + nOpacity + ")";
	}
else 
	{
	window.clearTimeout(tempo_fade);
	document.getElementById("contentModal").style.display = 'none';
	document.getElementById("retour").style.display = 'none';
	document.body.style.overflow	 = 'auto';
	}
}
function otherFadeDown(){
if (nOpacity > 0) 
	{
	tempo_fade = setTimeout('otherFadeDown()',temps);
	nOpacity -= 5; 
	document.getElementById("contentModal").style.opacity = nOpacity/100;
	document.getElementById("retour").style.opacity = nOpacity/100;
	}
else 
	{
	window.clearTimeout(tempo_fade);
	document.getElementById("contentModal").style.display = 'none';
	document.getElementById("retour").style.display = 'none';
	document.body.style.overflow	 = 'auto';
	}
}

function collecteEmail(mailOptin){
	var xmlhttp = false;
	
	/*@cc_on
	@if (@_jscript_version >= 5)
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				xmlhttp = false;
			}
		}
	@else
	xmlhttp = false;
	@end @*/

	if(!xmlhttp && typeof XMLHttpRequest!='undefined'){
			try{
				xmlhttp=new XMLHttpRequest();
     	}catch(e){
				xmlhttp=false;
  		}
 	}

  if(xmlhttp){
    	xmlhttp.onreadystatechange=function(){
        	if(xmlhttp.readyState==4){
							if(xmlhttp.status==200) {
           				retourMail();
							}
					}
			}
			xmlhttp.open("POST",path_prefix+'minisite/collecte_email.php',true);
			xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xmlhttp.send('mailOptin='+mailOptin+'&domaine='+collecte_domaine+'&page='+collecte_page);
	}
}
