var idIntervalVoteMouseOut="";
var NOMBRE_ETOILES_TOTAL = 5;

// Effacer le textbox si la valeur est celle par défaut lorsqu'on clique dedans

function doClear(theText) 
{
  if (theText.value == theText.defaultValue)
  {
    theText.value = "";
  }
}

//myField accepts an object reference, myValue accepts the text strint to add
function insertatcursor(myField, myValue) {
	//IE support
	if (document.selection) {
		myField.focus();
		
		//in effect we are creating a text range with zero
		//length at the cursor location and replacing it
		//with myValue
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	
	//Mozilla/Firefox/Netscape 7+ support
	else if (myField.selectionStart || myField.selectionStart == '0') {
	
		//Here we get the start and end points of the
		//selection. Then we create substrings up to the
		//start of the selection and from the end point
		//of the selection to the end of the field value.
		//Then we concatenate the first substring, myValue,
		//and the second substring to get the new value.
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
} 

// Ajouter l'emoticon cliqué dans le textbox

function ajouterSmileys(smileys) {
  insertatcursor(obj_textarea_cible_smileys,smileys);
  obj_textarea_cible_smileys.focus();
  parent.hideLayer();
}

function getAjaxObject(){
  var xmlHttp = null
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch(e) {
    //Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      //other shit
      try {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e) {
        xmlHttp=null;
      }
    }
  }
  return xmlHttp;
}

function vote_submit(note)
{


  if (!userIsLogged)
  {
    cacherTousLesMessages();
    document.getElementById('vote_not_logged').style.display='block';
    return;
  }

  cacherTousLesMessages();
  document.getElementById('vote_in_progress').style.display='block';


  var xmlhttp = getAjaxObject();
  
  var post_params = "id_bazou="+id_bazou+"&note="+note;

  xmlhttp.open("POST", "voter.php",true);
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
       var donneesRecues = xmlhttp.responseText;
       
      //Dans le cas ou les données recues me signifie que j'ai déjà voté.
      
      if (donneesRecues == "DEJA_VOTE") {
        cacherTousLesMessages();
        document.getElementById('vote_deja_vote').style.display='block';
      }
      else if (donneesRecues == "VOTE_PAS_LOGGE")
      {
        cacherTousLesMessages();
        document.getElementById('vote_not_logged').style.display='block';
      }
      else if (donneesRecues == "VOTE_VOTRE_BAZOU")
      {
        cacherTousLesMessages();
        document.getElementById('vote_votre_bazou').style.display='block';
      }
      else if (donneesRecues == "VOTE_OK") {
        cacherTousLesMessages();
        document.getElementById('vote_done').style.display='block';
        
        from_php_note += note;
        from_php_nb_votes++;
        
        afficherNoteBazou();
      }
      else {
        cacherTousLesMessages();
        document.getElementById('vote_err').style.display='block';
      }     
      
     }
  }
  
  xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xmlhttp.send(post_params)
  
}

function cacherTousLesMessages()
{
    document.getElementById('vote_err').style.display='none';
    document.getElementById('vote_in_progress').style.display='none';
    document.getElementById('vote_done').style.display='none';
    document.getElementById('vote_not_logged').style.display='none';
}

function vote_etoile_over(id_img)
{
  if (idIntervalVoteMouseOut)
    clearInterval(idIntervalVoteMouseOut);

  for (var i = 1;i<=NOMBRE_ETOILES_TOTAL;i++)
  {
     if (i <= id_img)
       document.getElementById('vote_img_'+i).src='imgs/vote/etoile_over.gif';  
     else
       document.getElementById('vote_img_'+i).src='imgs/vote/etoile_empty.gif';  
      
  }  
}

function vote_etoile_out(id_img)
{
  if (idIntervalVoteMouseOut)
    clearInterval(idIntervalVoteMouseOut);

  idIntervalVoteMouseOut = self.setInterval("restorer_img_vote()",450);
}

function restorer_img_vote()
{
  if (idIntervalVoteMouseOut)
    clearInterval(idIntervalVoteMouseOut);
  
  afficherNoteBazou();
}

function afficherNoteBazou()
{
  var note_sur_5 = 0;
  
  //Avoid 0 division
  if (from_php_nb_votes > 0)
    note_sur_5 = from_php_note / from_php_nb_votes;
  
  var i;
  for (i = 1;i<=NOMBRE_ETOILES_TOTAL;i++)
  {
  
    if (note_sur_5 < ( (i-1) + 0.25) )
    {
       document.getElementById('vote_img_'+i).src='imgs/vote/etoile_empty.gif';  
    }
    else if ((note_sur_5 >= (i-1) + 0.25) && (note_sur_5 < (i-1) + 0.75))
    {
        document.getElementById('vote_img_'+i).src='imgs/vote/etoile_half.gif';
    }
    else if (note_sur_5 >= (i-1) + 0.75)
    {
        document.getElementById('vote_img_'+i).src='imgs/vote/etoile_full.gif';
    }
  
  }

 	if (from_php_nb_votes > 1) texte_vote = from_php_nb_votes + " votes";
 	else texte_vote = from_php_nb_votes + " vote";
 
  document.getElementById('vote_note_txt').innerHTML = texte_vote;

}

function showHideElement(elem)
{
  etat = document.getElementById(elem).style.display;
  
  if (etat == "none")
    document.getElementById(elem).style.display = "block";
  else 
    document.getElementById(elem).style.display = "none";
}

function showElement(elem)
{
    document.getElementById(elem).style.display = "block";
}

function hideElement(elem)
{
    document.getElementById(elem).style.display = "none";
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}


function confirmationDeleteReplyBazou(pathsite,nom_compte_bazou,index_bazou_membre,id_commentaire)
{
	var blnConfirm = confirm("Êtes-vous sûr de vouloir supprimer ce message ?");
	if (blnConfirm)
	{
		window.location = pathsite+"index.php?page=affichage_bazou&nom_membre="+nom_compte_bazou+"&index_bazou_membre="+index_bazou_membre+"&supprimer_commentaire="+id_commentaire;
	}

	return false;
}

function confirmationDeleteReplyMembre(pathsite,nom_compte,id_commentaire)
{
	var blnConfirm = confirm("Êtes-vous sûr de vouloir supprimer ce message ?");
	if (blnConfirm)
	{
		window.location = pathsite+"index.php?page=affichage_utilisateur&utilisateur="+nom_compte+"&supprimer_commentaire="+id_commentaire;
	}

	return false;

}

function confirmationDeleteReplyForum(pathsite,id_thread,id_commentaire)
{
	var blnConfirm = confirm("Êtes-vous sûr de vouloir supprimer ce message ?");
	if (blnConfirm)
	{
		window.location = pathsite+"index.php?page=forum_messages&id_thread="+id_thread+"&supprimer_message="+id_commentaire;
	}

	return false;
}

function confirmationDeleteReplyArticle(pathsite,id_article,id_commentaire)
{
	var blnConfirm = confirm("Êtes-vous sûr de vouloir supprimer ce message ?");
	if (blnConfirm)
	{
		window.location = pathsite+"index.php?page=articles&id="+id_article+"&supprimer_commentaire="+id_commentaire;
	}

	return false;
}

function previsualisationMessageForum(pathsite,username,message) {
	 window.open(pathsite+"previsualisation_message_forum.php?username="+username+"&message="+message,"previsualisation_message_forum","menubar=no, status=no, scrollbars=0, menubar=no, width=755, height=250");
}

function selectionner_valeur_select(select_name, value) {
  eval ('select_object = document.' + select_name + ';');
  for (index = 0; index < select_object.length; index++) {
   if (select_object[index].value == value)
     select_object.selectedIndex = index;
   }
}




