var tempo = 0;

function showPopupErreur(idElement,idConteneur,message,margin_left,margin_top,type,default_value,nbElement,show)
{
	var pos = findPos(document.getElementById(idElement));
	var largeur = document.getElementById(idElement).offsetWidth;
	var container=document.getElementById(idConteneur);
	var ok = true;
	var popup = false;
	var radio = false;
	var bDate = false;

	if ( document.getElementById(idElement + "_divErreur") )
	{
		//setTimeout(function(){container.removeChild(document.getElementById(idElement + "_divErreur"))});
		container.removeChild(document.getElementById(idElement + "_divErreur"));
	}


	//Vérification si radio button
	if (type != undefined && type == "Radio" )
	{
		for ( i = 0 ; i < nbElement ; i++ ) 
		{
			if ( document.getElementById(idElement + '_' + i).checked )
			{
				radio = true;
			}
		}
	}
	
	//Verification si date
	if (type != undefined && type == "date" )
	{
		if (isDateValidCalendar(document.getElementById(idElement).value))
		{
		bDate = true;
		}
	}

	if  ( type != undefined && type == "email" && !verifEmail(document.getElementById(idElement).value)  )
	{
		message = "Email invalide";
	}

	if ( (type != undefined && type == "date" && !bDate) ||  (type != undefined && type == "Radio" && !radio) ||  ( default_value != undefined && default_value != "" && document.getElementById(idElement).value == default_value    ) || ( document.getElementById(idElement).value == "" && type == undefined ) || ( type != undefined && type == "email" && !verifEmail(document.getElementById(idElement).value)  ) )    { 
	ok = false;
	var newElt=document.createElement("div");
	//newElt.style.border="solid 1px red";
	newElt.style.height="20px";
	newElt.id=idElement + "_divErreur";
	
	var contenu = "";
contenu +="	<table border='0' cellpadding='0' cellspacing='0' width='200'>";
contenu +="	<tr style='height: 100%;' valign='top'>";
contenu +="		<td style='height: 100%; vertical-align: top;' align='right' width='20'>";
contenu +="			<table style='height: 100%;' border='0' cellpadding='0' cellspacing='0'>";
contenu +="			<tr>";
contenu +="			<td style='font-size: 1px; padding-top: 5px;' align='right' valign='top'>";
contenu +="			<div style='border-top: 1px solid black; font-size: 1px; position: relative; left: 1px; width: 15px;'>";
contenu +="			<div style='border-left: 1px solid black; overflow: hidden; width: 14px; height: 1px; background-color: #F9CAB0;'></div>";
contenu +="			<div style='border-left: 1px solid black; overflow: hidden; width: 13px; height: 1px; background-color: #F9CAB0;'></div>";
contenu +="			<div style='border-left: 1px solid black; overflow: hidden; width: 12px; height: 1px; background-color: #F9CAB0;'></div>";
contenu +="			<div style='border-left: 1px solid black; overflow: hidden; width: 11px; height: 1px; background-color: #F9CAB0;'></div>";
contenu +="			<div style='border-left: 1px solid black; overflow: hidden; width: 10px; height: 1px; background-color: #F9CAB0;'></div>";
contenu +="			<div style='border-left: 1px solid black; overflow: hidden; width: 9px; height: 1px; background-color: #F9CAB0;'></div>";
contenu +="			<div style='border-left: 1px solid black; overflow: hidden; width: 8px; height: 1px; background-color: #F9CAB0;'></div>";
contenu +="			<div style='border-left: 1px solid black; overflow: hidden; width: 7px; height: 1px; background-color: #F9CAB0;'></div>";
contenu +="			<div style='border-left: 1px solid black; overflow: hidden; width: 6px; height: 1px; background-color: #F9CAB0;'></div>";
contenu +="			<div style='border-left: 1px solid black; overflow: hidden; width: 5px; height: 1px; background-color: #F9CAB0;'></div>";
contenu +="			<div style='border-left: 1px solid black; overflow: hidden; width: 4px; height: 1px; background-color: #F9CAB0;'></div>";
contenu +="			<div style='border-left: 1px solid black; overflow: hidden; width: 3px; height: 1px; background-color: #F9CAB0;'></div>";
contenu +="			<div style='border-left: 1px solid black; overflow: hidden; width: 2px; height: 1px; background-color: #F9CAB0;'></div>";
contenu +="			<div style='border-left: 1px solid black; overflow: hidden; width: 1px; height: 1px; background-color: #F9CAB0;'></div>";
contenu +="			</div>";
contenu +="			</td>";
contenu +="			</tr>";
contenu +="			</table>";
		contenu +="</td>";
contenu +="		<td style='border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black; padding: 1px; background-color: #F9CAB0;' width='20'>";
contenu +="			<img src='/img/sondage/attention.png' border='0' style='BEHAVIOR: url(/iepngfix.htc);'>";
contenu +="			</td>";
contenu +="			<td style='vertical-align:middle;border-top: 1px solid black; border-bottom: 1px solid black; padding: 1px; background-color: #F9CAB0; font-family: verdana; font-size: 10px;' width='100%'>";
contenu +="			<b>" + message  +  "</b>";
contenu +="			</td>";
contenu +="			<td style='border-top: 1px solid black; border-right: 1px solid black; border-bottom: 1px solid black; padding: 1px; background-color: #F9CAB0; vertical-align: top; text-align: right;'>";
contenu +="			&nbsp;";
		contenu +="</td>";
	contenu +="</tr>";
contenu +="</table>";
	newElt.innerHTML = contenu;

	//Placement du div si besoin
	if ( margin_left != undefined  && margin_left != "" )	
	{
	
	newElt.style.marginLeft = margin_left + "px";
	newElt.style.marginTop = margin_top + "px";	
	}
	
	var correctionX = "";
	var correctionY = "";
	
	if (type != undefined && type == "Radio" )
	{
	newElt.style.marginLeft = document.getElementById(idElement).offsetWidth + "px";
	newElt.style.marginTop = "-" + document.getElementById(idElement).offsetHeight + "px";
	}


	
	
		
	if (navigator.userAgent.indexOf("Firefox")!=-1 || navigator.userAgent.indexOf("Opera")!=-1 )
	{
		correctionX = document.getElementById(idElement).offsetWidth;
		correctionY= document.getElementById(idElement).offsetHeight;
		
		if ( margin_top != undefined  && margin_top != "" )	
		{
				var correctionF = Number(margin_top) + Number(correctionY);
				newElt.style.marginTop =  "-" + correctionF + "px";
		}
		else{
				newElt.style.marginTop =  "-" + correctionY + "px";
		}
		if ( margin_left != undefined  && margin_left != "" )	
		{
			newElt.style.marginLeft = Number(margin_left) + correctionX + "px";
		}
		else
		{
			newElt.style.marginLeft =  correctionX + "px";
		}
		
	}
	
		
	newElt.style.width="200px";
	newElt.style.position="absolute";
	newElt.style.zindex="1000";
	

	//newElt.style.left= pos[0] + largeur + "px";
	//newElt.style.top= newElt.style.top -5 "px";
	
	//setTimeout(function(){container.appendChild(newElt)});
	if ( show == undefined )
	{
		container.appendChild(newElt);
		tempo = tempo + 500
		setTimeout("test2('" + idElement + "')",tempo);
	}

}

	return ok;
}


function test2(idElement)
{
if ( document.getElementById(idElement + "_divErreur") ) 
{
document.getElementById(idElement + "_divErreur").style.display = "none";
}

}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
} 

function verifEmail(email)
{
      if ( email == "" ) {
      return true;
      }
var aroba = email.indexOf("@");
if (aroba == -1) {
return false;
}
var point = email.indexOf(".", aroba);
if ((point == -1) || (point == (aroba + 1))) {
return false;
}
var point = email.lastIndexOf(".");
if ((point + 1) == email.length) {
return false;
}
var point = email.indexOf("..")
if (point != -1) {
return false;
}
return true;
}

//Vérification de la date
function isDateValidCalendar(chaineDate) {

   if (chaineDate == "") return false

   var ladate = (chaineDate).split("/")
   if ((ladate.length != 3) || isNaN(parseInt(ladate[0])) || isNaN(parseInt(ladate[1])) || isNaN(parseInt(ladate[2]))) return false
   var unedate = new Date(eval(ladate[2]),eval(ladate[1])-1,eval(ladate[0]))
   var annee = unedate.getYear()
   if ((Math.abs(annee)+"").length < 4) annee = annee + 1900
   return ((unedate.getDate() == eval(ladate[0])) && (unedate.getMonth() == eval(ladate[1])-1) && (annee == eval(ladate[2])))
}


