var flagSucces = false;
var formText = '<form action="" method="post" id="formFlag" name="formFlag" onsubmit="sendFlag(); return false;">'+
'<fieldset>'+
'<legend>Signaler une annonce terminée</legend>'+
'<p style="margin:25px 5px;">entrez votre email : <input size="25" type="text" id="auth_email" name="auth_email" /></p>'+
'Cet email vous servira à terminer l\'annonce et doit être le même que celui utilisé pour l\'annonce.<br /><br />'+
'<input type="button" style="float:left; width:75px; margin:4px;" value="OK" id="" name="" onclick="sendFlag();"/>'+
'<input type="button" style="float:right; width:75px; margin:4px;" value="Annuler" id="" name="" onclick="cancelFlag();"/>'+
'<br style="clear:both;"/>'+
'</fieldset>'+
'</form>';

function reply(url){
	document.form3.action = url+"#bcontact";
	document.form3.submit();
}
function mailFocus(){
	if (document.getElementById("formulaire_contact").style.display!="none"){
		document.getElementById('email_contact').focus();
		}
}
function openAnnonce(url){
	document.location = url;
}

function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = (20+document.body.scrollWidth)+'px';
        var pageHeight = (30+document.body.scrollHeight)+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}

function getScrollWidth()
{
   var w = window.pageXOffset ||
           document.body.scrollLeft ||
           document.documentElement.scrollLeft;
           
   return w ? w : 0;
}

function getScrollHeight()
{
   var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;
           
   return h ? h : 0;
}

function centerDiv2(objDiv){
	//get Window size
	pageWidth = 0;pageHeight = 0;
	pageWidth =  window.innerWidth;
	pageWidth = (pageWidth)? pageWidth : document.documentElement.clientWidth;
	pageWidth = (pageWidth)? pageWidth: document.body.clientWidth;
	pageHeight =  window.innerHeight;
	pageHeight = (pageHeight)? pageHeight: document.documentElement.clientHeight;
	pageHeight = (pageHeight)? pageHeight: document.body.clientHeight;
	//center div
	var obw = objDiv.style.width;
	obw = obw.substr(0, obw.indexOf("px"));
	obw = parseInt(obw);
	var posLeft = (pageWidth-obw)/2;
	objDiv.style.left = (posLeft<0)?0+"px":posLeft+"px";
	var posTop = getScrollHeight() + ((pageHeight-150)/2);
  //var posTop = document.body.scrollHeight - ((pageHeight+150)/2);
  	objDiv.style.top = (posTop<0)?0+"px":posTop+"px";
}

function terminer_annonce(){
	grayOut(true);
	centerDiv2(document.getElementById('flag_p'));
	document.getElementById('flag_p').style.display = "block";
	flagSucces = false;
}
function cancelFlag(){
	document.getElementById('flag_p').style.display = "none";
	grayOut(false);
	document.getElementById('flag_p').innerHTML = formText;
	if(flagSucces){window.location.reload(true);}
}
function sendFlag(){
	var email = document.getElementById('auth_email').value;
	var id = document.getElementById('annonce_id').value;
	var sendingText = '<img align="absmiddle" src="/annonces/img2/sending.gif"/>' + ' Votre demande est en cours de traitement...';
	document.getElementById('flag_p').innerHTML = sendingText;
	var data = "auth_email="+email +"&id="+id;
	ajaxSendForm(data);
}

function displayMsg(id){
	var img = "";
	var msg = "";
	var errorImg = '<img style="float:left;" src="/annonces/img2/alert.png" align="absmiddle" />';
	var okImg = '<img style="float:left;" src="/annonces/img2/ok.png" align="absmiddle" />';
	buttonOK = '<p align="center"><input type = "button" value = "OK" style="width:75px;" onclick="cancelFlag();"/></p>';
	if(id=="0")
	{
	img = okImg;
	msg = "L'annonce est désormais marquée comme terminée";
	flagSucces= true;
	}
	
	if(id=="1")
	{
		img = errorImg;
		msg = 'Erreur : l\'email est vide';
	}
	
	if(id=="2")
	{
		img = errorImg;
		msg = 'Erreur : l\'email est invalide';
	}
	
	if(id=="4")
	{
		img = errorImg;
		msg = 'Erreur : echec de la connexion à la base de donnée';
	}
	
	if(id=="5")
	{
		img = errorImg;
		msg = 'Erreur : l\'email saisi ne correspond pas à l\'email de l\'auteur de l\'annonce';
	}
	
	if(id=="6")
	{
		img = errorImg;
		msg = 'Erreur : echec à l\'écriture dans la base de donnée';
	}
	
	document.getElementById('flag_p').innerHTML = '<p align="center">'+ img + msg +'</p>' + buttonOK;
	
}

function ajaxSendForm(data){

	var xhr;
	if (window.XMLHttpRequest) xhr = new XMLHttpRequest();
	else if (window.ActiveXObject) xhr = new ActiveXObject('Microsoft.XMLHTTP'); // Internet Explorer
	else alert('JavaScript : votre navigateur ne supporte pas les objets XMLHttpRequest...'); // Navigateur ancien
	xhr.open('POST','/annonces/flag_p.php',true);
	xhr.onreadystatechange = function() // attribution de la fonction
        {
        if (xhr.readyState == 4)
			{
			if (xhr.status == "200" || xhr.status == "0")
                {
				var reponse = xhr.responseText;
				displayMsg (reponse);
                }
			}
        }
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send(data);
}
