//Elements global
var listBlocActionsChat = new Array('alerte','prevenir','documents');

//Switch des blocs actions du bloc chat
function switchOngletActions(indexToShow)
{
	for (var i=0; i<listBlocActionsChat.length; i++)
	{
		element = $('#chat_'+listBlocActionsChat[i]);
		if (element.length >= 1)
		{
			element.css('display',(indexToShow==i?'block':'none'));
			elementO = $('#onglet_'+listBlocActionsChat[i])
			if (elementO.length >= 1)
				elementO.attr('src',switchImgSrc(elementO.attr('src'),(indexToShow==i)));
		}
	}
}

function switchImgSrc(srcVal,isSelec)
{
	var reg=new RegExp("(-selec)?\.png", "g");
	return srcVal.replace(reg,(isSelec==1?'-selec':'')+".png");
}


/******************* FONCTIONS CHATS  ****************/
function valide_sendContrib()
{
	if(trim(document.getElementById("input-pseudo").value) == "")
	{
		showMsg("Vous devez indiquer un pseudo pour envoyer une question.",1,1);
	 	return false;
	}
	if(trim(document.getElementById("input-contribution").value) == "")
	{
		showMsg("N'oubliez pas de remplir la question.",1,1);
		return false;
	}
	if (navigator.appName=="Microsoft Internet Explorer")
	{
		document.getElementById("input-pseudo").value = encode_utf8(document.getElementById("input-pseudo").value);
		document.getElementById("input-contribution").value = encode_utf8(document.getElementById("input-contribution").value);
	}

	sendFormJs('contribution');
	return false;
}

/** Envoie des formulaires pas JS **/
function sendFormJs(idForm)
{
	//Récupération de la frame
	var myf = document.getElementById('iframe_form');
	myf = myf.contentWindow.document || myf.contentDocument;
	//Contribution
	if (idForm == 'contribution')
	{
		myf.form_contribution.pseudo.value = document.getElementById("input-pseudo").value;
		myf.form_contribution.contribution.value = document.getElementById("input-contribution").value;
		myf.form_contribution.submit();
	}
	else if (idForm == 'alerte_email')
	{
		myf.form_alerte.alerte_email.value = document.getElementById("alerte_email_tbx").value;
		myf.form_alerte.submit();
	}
	else if (idForm == 'alerte_sms')
	{
		myf.form_alerte.alerte_sms.value = document.getElementById("alerte_sms_tbx").value;
		myf.form_alerte.submit();
	}
	else if (idForm == 'prevenir')
	{
		myf.form_prevenir.email_from.value = document.getElementById("prevenir_email_tbx").value;
		myf.form_prevenir.email_to.value = document.getElementById("prevenir_email_ami_tbx").value;
		myf.form_prevenir.submit();
	}
	return false;
}

/** Affichage-Masquage des messages de retour (alerte ok...) **/
function showMsg(msg,isError,keepValue)
{
	var idElement = '#msgAlerte'+(isError?'Erreur':'Ok');
	var element = $(idElement, window.parent.document);
	if (element.length)
	{
		element.text(msg);
		if (keepValue)
			element.slideDown("fast");
		else
			element.slideDown("fast",function(){videChamps()});
		//element.fadeIn("slow",function(){videChamps()});
		setTimeout('HideMsg("'+idElement+'")',2000)
	}
}
function HideMsg(idElement)
{
	var element = $(idElement, window.parent.document);
	if (element.length==0) element = $(idElement);
	if (element.length >= 1)
		$(idElement, window.parent.document).fadeOut('slow');
}

function videChamps()
{
	$('#prevenir_email_tbx', window.parent.document).attr('value','');
	$('#prevenir_email_ami_tbx', window.parent.document).attr('value','');
	$('#alerte_sms_tbx', window.parent.document).attr('value','');
	$('#alerte_email_tbx', window.parent.document).attr('value','');
	$('#input-pseudo', window.parent.document).attr('value','');
	$('#input-contribution', window.parent.document).attr('value','');
}

function videTextboxInit()
{
	var listTbx = new Array('forum_nom_tbx','forum_prenom_tbx','forum_pass_tbx','forum_pass2_tbx','forum_email_tbx','forum_fonction_tbx','forum_login_tbx','pass_login_tbx','new_sujet_tbx','captcha_tbx');
	//var valToClear = new Array('Votre nom','Votre prénom','Votre mot de passe...','Confirmer le','Votre email','Votre fonction','Votre login','Votre mot de passe','Titre du sujet');
	for (var i=0; i<listTbx.length; i++)
	{
		var element = $('#'+listTbx[i]);
		if (element.length >= 1) // && element.attr('value')==valToClear[i])
		{
			element.focus(function(){
				if ($(this).attr('value') == $(this).attr('title'))
					$(this).attr('value','');
			});
		}
	}
}

function trim(s){
		return s.replace(/^\s+/g,'').replace(/\s+$/g,'')
  }

function encode_utf8(contribution) 
{
	string = contribution.replace(/\r\n/g,"\n");
	var utftext = "";
	for (var n = 0; n < string.length; n++) 
	{
 		var c = string.charCodeAt(n);
 		if (c < 128) utftext += String.fromCharCode(c);
		else if((c > 127) && (c < 2048)) 
		{
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else 
		{
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}
	}
 	return utftext;
}

function valideArchive()
{
	var valSelect = $('#select_archive').attr('value')
	if (valSelect>0) $('#form_archive').attr('action','index.php?nrChat='+valSelect)
	else  $('#form_archive').attr('action','index.php');
	$('#form_archive').submit();
}

/** FUNCTION ALERTES CHATS **/
//Initialisation des fonctions javascript
$(document).ready(function() {
	//Si page d'accueil, on initialise lesmsgErreur
	if ($('#msgAlerte').length) 
	{
		$('#msgAlerte').fadeIn('slow');
		setTimeout('HideMsg("#msgAlerte")',2000);
	}
	videTextboxInit();
});


