var finConteneurMsg = '</span>';

function getMsgFEchecChamp(msg)
{
	var msg = msg || "";
	var symboleCSSEchec = '<span class="echec"></span>';
	var debConteneurMsgEchec = '<span class="msg_echec">';

	return debConteneurMsgEchec+symboleCSSEchec+msg+finConteneurMsg;
}

function getMsgFSuccesChamp(msg)
{
	var msg = msg || "";
	var symboleCSSSucces = '<span class="succes"></span>';
	var debConteneurMsgSucces = '<span class="msg_succes">';

	return debConteneurMsgSucces+symboleCSSSucces+msg+finConteneurMsg;
}	

function treatTaskMsg(msgs,getmsgerror)
{
	msgs = msgs.replace(/^\s*|\s*$/,"");
	var getmsgerror = getmsgerror || false;
	
	var tabmsg = msgs.split("|");
	var marqueurEchecTache = "{EXE_FAIL}";
	var marqueurReussiteTache = "{EXE_SUCCESS}";
	var marqueurRedirectionTache = "{URL}";
	var marqueurMsgErreurValidation = "ECHEC";
	var marqueurMsgReussiteValidation = "SUCCES";
	var chMsg ="";

	var tabunmsg = "";
	var champAffichage = "";
	var itab = 0;
	var cur = "";
	var nbr = tabmsg.length;

	for(itab = 0; itab < nbr; itab++)
	{
		//L'execution de la tache a retourné une URL
		if(tabmsg[itab].substr(0,marqueurRedirectionTache.length) == marqueurRedirectionTache)
		{
			window.location = tabmsg[itab].replace(marqueurRedirectionTache,"");
		}
		
		//L'execution de la tache a echouee
		if(tabmsg[itab].substr(0,marqueurEchecTache.length) == marqueurEchecTache)
		{
			if(getmsgerror == true)
			{
				return tabmsg[itab].replace(marqueurEchecTache,"");
			}else{
				return false;
			}
		}

		//L'execution de la tache a reussie apres avoir nettoyé les messages
		if(tabmsg[itab].substr(0,marqueurReussiteTache.length) == marqueurReussiteTache)
		{
			return true;
		}

		//Si l'execution n'a pas eu lieu, il faut exploiter les messages
		tabunmsg = tabmsg[itab].split(":");
		champAffichage = "msg:"+tabunmsg[1];
		var msg = tabunmsg[2]
		var oAffichage;
		
		if($(champAffichage))
		{
			oAffichage = $(champAffichage);

			if(msg != "")
			{
				if(tabunmsg[0] == marqueurMsgErreurValidation)
				{
					msg_formate = getMsgFEchecChamp(msg);
				}
		
				if(tabunmsg[0] == marqueurMsgReussiteValidation)
				{
					msg_formate = getMsgFSuccesChamp(msg);
				}

				oAffichage.update(msg_formate);
			}else{
				if(tabunmsg[0] == marqueurMsgReussiteValidation)
				{
					oAffichage.update("");
				}
			}
		}
	}

	return null;
}