/*
* Infusor v3.0
* Copyright © Hiteule Créative
*/

function hideaftertime(id){
	var func = "hide('"+id+"')";;
	setTimeout(func,2500);
}

function hide(id){
	if ($(id)){new Effect.Fade(id, {duration: 0.5});}
}

function show_overlay(id){
	var tabID = $(id);
	if (tabID.style.display == 'none')	{
		new Effect.Appear(id, {duration: 0.5});
		hideaftertime(id);
	}
}

function cb_clic(){
  var url='./gestion/chatbox.php';
  var myAjax=new Ajax.Request(
    url,
      {
        method: 'post',
        parameters: 'cb_contenu='+$('cb_contenu').value,
        onComplete: cb_reponse
      });
}

function cb_reponse(xhr){
  if (xhr.status == 200) $('resultat').innerHTML = xhr.responseText;
  else $('resultat').innerHTML = xhr.status;
}

function limit_textarea(textarea, max){
  if(textarea.value.length>=max) textarea.value = textarea.value.substring(0,max);
}

// Navbar
function showTab(id_div) {
	numTab = document.getElementById('tab_num').value
	for(i=1; i<= numTab ; i++){
		if(document.getElementById('tab_'+i)){
			currentTab = document.getElementById('tab_'+i);
			currentTab.style.display = "none";
			currentBtnTab = document.getElementById('btn_tab_'+i);
			currentBtnTab.className = "tab_unselected";
		}
	}
	mySelectedTab = document.getElementById(id_div);
	myBtnTab = document.getElementById('btn_'+id_div);
	if (!mySelectedTab){alert('Paramètre incorrect. Cet onglet n\'existe pas !')}
	else if (mySelectedTab.style){
			mySelectedTab.style.display = "";
			mySelectedTab.visibility = "show";
			myBtnTab.className = "tab_selected";
	}
}

function getLastSelection(e){
	from = e.relatedTarget.id
	alert("FROM :"+from);
}

// Chatbox
function chatbox_add() {
  txt_shout = document.getElementById("txt_shout").value;
  document.getElementById("txt_shout").value="";
  if(txt_shout != ""){
    if(chatbox_verif(txt_shout)){
      xhr_object = createXHR();
      var fichier = "gestion/chatbox.php";
      xhr_object.open("post", fichier, true);
      xhr_object.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      xhr_object.send("chatbox_contenu=" + txt_shout);
      xhr_object.onreadystatechange = function(){
        if(xhr_object.readyState == 4){document.getElementById("shoutbox").innerHTML = xhr_object.responseText;}
      }
      chatbox_get();
    }
    else alert("Desolé, caractère interdit !");
  }
}

function createXHR(){
  var xhr;
  if(window.XMLHttpRequest) xhr = new XMLHttpRequest();
  else if(window.ActiveXObject){
    try{xhr = new ActiveXObject("Msxml2.XMLHTTP");}
    catch(e){xhr = new ActiveXObject("Microsoft.XMLHTTP");}
  }
  else xhr = false;
  return xhr;
}

function chatbox_verif(chaine){
  var non = new Array(">","<","&lt;","&gt;");
  for(i=0; i<=chaine.length; i++){
    for(y=0 ; y <= non.length ; y++){
      if((chaine.charAt(i) == non[y])) return false;
    }
  }
  return true;
}
 
function chatbox_get(){
  if(document.getElementById("shoutbox")==null) return false;
  document.getElementById("shoutbox").innerHTML = "Chargement...";
  xhr_object = createXHR();
  var fichier = "gestion/chatbox.php";
  xhr_object.open("get", fichier, true);
  xhr_object.send(null);
  xhr_object.onreadystatechange = function(){
    if(xhr_object.readyState == 4){document.getElementById("shoutbox").innerHTML = xhr_object.responseText;}
  }
}
