box = function(divMask, divContainer){
	
	this.emailDestEtNomManifCrypte = "";
	this.idEvt = 0;
	
	this.open = function () {
		this.positionneDiv();
		this.addListener(divMask, "click", this.close);
		this.addListener(window, "resize", adapteOnResize);
		divMask.style.display = 'block';
		divContainer.style.display = 'block';
	},
	
	this.close = function () {
		divMask.style.display = 'none';
		divContainer.style.display = 'none';
		delete this;
	},
	
	this.sendMail = function (nom, prenom, email, message, root) {
		if (nom == "") return "Vous n'avez pas renseigné votre nom";
		if (prenom == "") return "Vous n'avez pas renseigné votre prénom";
		if (email == "") return "Vous n'avez pas renseigné votre email";
		if (message == "") return "Vous n'avez pas renseigné votre message";
		if (!this.checkEmail(email)){ return "Votre email est incorrect"; }
		if (this.emailDestEtNomManifCrypte == "") return "Erreur, l'email du destinataire est inconnu. Merci de réessayer plus tard";
		
		var xhr_object = null;
		var xhr_url=root+"ajax/sendMail.php";

		var chaine_get="";
		chaine_get += "nom="+escape(nom);
		chaine_get += "&prenom="+escape(prenom);		
		chaine_get += "&email="+escape(email);
		chaine_get += "&message="+escape(message);
		chaine_get += "&emailDestEtNomManifCrypte="+escape(this.emailDestEtNomManifCrypte);
		chaine_get += "&idEvt="+escape(this.idEvt);

		xhr_url+="?"+chaine_get;

		if ( window.XMLHttpRequest ) {xhr_object = new XMLHttpRequest();} 
		if ( window.ActiveXObject ) {xhr_object = new ActiveXObject( 'Microsoft.XMLHTTP' );}
		xhr_object.open( 'GET', xhr_url, false );
		xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		xhr_object.send(null);
		
		var contenu = xhr_object.responseText;
//		alert(contenu );
		contanu = contenu.substring(contenu.length - 2, contenu.length);
		if (contenu.substring(contenu.length - 2, contenu.length) == "OK") return "Message envoyé";
		return "Le message n'a pas pu être envoyé. Merci de réessayer plus tard";
	},
	
	this.checkEmail = function(email){
		var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/
		return (reg.exec(email)!=null)
	},
	
	this.positionneDiv = function() {
		var arrayPageSize = this.getPageSize();
	    var width = arrayPageSize[0] + 'px';
		var height = arrayPageSize[1] + 'px';
//		alert("width="+width+" | height="+height);
		if (divMask){
			divMask.style.width = width;
			divMask.style.height = height;//IL faut utilisser l'evt onResize sur le body....
		}
		else{
			alert("le Div mask est manquant");
			return false;
		}

		// calculate top and left offset for the box 
        var arrayPageScroll = new Array(window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft, window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop);
		
		var hauteurContenu = this.getDimensions(divContainer).height;
		var largeurContenu = this.getDimensions(divContainer).width;
        var boxTop = arrayPageScroll[1] + (document.documentElement.clientHeight / 10) - (hauteurContenu/2) + 200;
        var boxLeft = document.documentElement.clientWidth / 2 - (largeurContenu/2);//arrayPageScroll[0];
        var top = boxTop + 'px';
		var left = boxLeft + 'px';
		//alert("top="+top+" | left="+left);
		if (divContainer){
			divContainer.style.top = top;
			divContainer.style.left = left;//IL faut utilisser l'evt onResize sur le body....
		}
		else{
			alert("le Div container est manquant");
			return false;
		}
    },
	
	this.getDimensions = function(element) {
		// All *Width and *Height properties give 0 on elements with display none,
		// so enable the element temporarily
		var els = element.style;
		var originalVisibility = els.visibility;
		var originalPosition = els.position;
		var originalDisplay = els.display;
		els.visibility = 'hidden';
		els.position = 'absolute';
		els.display = 'block';
		var originalWidth = element.clientWidth;
		var originalHeight = element.clientHeight;
		els.display = originalDisplay;
		els.position = originalPosition;
		els.visibility = originalVisibility;
		return {width: originalWidth, height: originalHeight};
	},
	
	this.addListener = function(element, evt, functionName) {
		if (element.addEventListener) {
			element.addEventListener(evt, functionName, false);
		} else {
			element.attachEvent("on"+evt, functionName);
		}
	},


	this.getPageSize=function() {     
	     var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}
}


