var gfx = "http://www.yaronet.com/v31/gfx";

var encN=1;

function escapeTxt(os){
var ns='';
var t;
var chr='';
var cc='';
var tn='';
for(i=0;i<256;i++){
tn=i.toString(16);
if(tn.length<2)tn="0"+tn;
cc+=tn;
chr+=unescape('%'+tn);
}
cc=cc.toUpperCase();
os.replace(String.fromCharCode(13)+'',"%13");
for(q=0;q<os.length;q++){
t=os.substr(q,1);
for(i=0;i<chr.length;i++){
if(t==chr.substr(i,1)){
t=t.replace(chr.substr(i,1),"%"+cc.substr(i*2,2));
i=chr.length;
}}
ns+=t;
}
return ns;
}

function XMLHttp()
{
	xmlhttpOK = false;
	var xmlhttp=null;
	
	if (window.XMLHttpRequest)
	{
		//firefox - mozilla - safari
		xmlhttp = new XMLHttpRequest();
		xmlhttpOK = true;
	}
	else
	{
		//IE et selon la version de msxml installée sur le systeme
		if (window.ActiveXObject)
		{
			try {
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				xmlhttpOK = true;
			}
			catch(e) {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				xmlhttpOK = true;
			}
		}
	}
	
	return xmlhttp;
}

var xmlhttpOK;
var xmlhttp=new XMLHttp();

function postURL(url,data,fonction)
{
	if(xmlhttp)
	{
		xmlhttp.open("POST",url,true);
		
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState == 4)
			{
				if (xmlhttp.status == "200")
				{
					if (xmlhttp.responseText.charAt(0) == ";")
						eval(fonction+"(xmlhttp.responseText)");
					else
					{
						alert("Probleme dans la recuperation des donnees."+xmlhttp.responseText);
						return false;
					}
				}
			}
						
		}
		xmlhttp.send(data);	
	}

	return true;
}

function postURLRet(url,data)
{
	if(xmlhttp)
	{
		xmlhttp.open("POST",url,false);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

		xmlhttp.send(data);
		return xmlhttp.responseText;
	}

	return "";
}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

function getSel()
{
	if (window.getSelection)
	{
		var selection = window.getSelection();

		if (selection.rangeCount > 0)
		{
			var range = selection.getRangeAt(0);
			var clonedSelection = range.cloneContents();
			var div = document.createElement('div');
			div.appendChild(clonedSelection);
			return trim(div.innerHTML);
		}
		else
			return '';
	}
	else if (document.selection && document.selection.createRange)
	{
		return trim(document.selection.createRange().htmlText);
	}
	else return '';
}

var selectedText;
var lastPost, lastSuj;
var debutSel;
var stopBubble = false;

function sc(element)
{
	debutSel = element.id;
}

var timeKill;

function ce(ev,element,suj, num, login)
{
	clearTimeout(timeKill);

	if (stopBubble) { stopBubble = false; return; }
	
	if (debutSel == null || debutSel != element.id) { kill(); return; };
	
	if (xmlhttpOK)
	{
		var text = String(getSel());
		//alert(encodeURI(text));
		//selectedText = text.replace(/&/g,"%26");
		selectedText = escapeTxt(text);
		if (text.length > 5)
		{
			var spanBt = "<b>Citation : </b> <a href='javascript:;' onmousedown='addCite(\""+suj+"\",\""+login+"\",\""+num+"\")'>Sauver </a> &nbsp;-&nbsp;";
			spanBt += "<a href='javascript:;' onmousedown='videCite(\""+suj+"\")'>Vider</a> <span id='xcTemp'></span>";
			spanBt += " <b>|| <a href='javascript:kill()'>X</a></b>";

			timeKill = setTimeout("kill()",5000);

			popS(ev,spanBt);
			return;
		}
		else
			kill();
	}
}
function addCite(suj,login,num)
{
	postURL("xCite.php","a=a&cite="+selectedText+"&idu="+login+"&s="+suj+"&num="+num,"add");
	stopBubble = true;
}

function videCite(suj)
{
	postURL("xCite.php","a=t&s="+suj,"del");
	stopBubble = true;
}

function copyCites(suj)
{
	postURL("xCite.php","a=c&s="+suj,"cpy");
}

function add(str) { document.getElementById("xcTemp").innerHTML =  "- "+str.substring(1); stopBubble = false;  }
function del(str)
{
	if(document.getElementById("xcTemp"))
		document.getElementById("xcTemp").innerHTML =  "- Citation(s) supprimée(s)";

	if(document.getElementById("xcTemp2"))
		document.getElementById("xcTemp2").innerHTML =  "- Citation(s) supprimée(s)";

	stopBubble = false;
}

function cpy(str) { document.getElementById('msg_id').value += str.substring(1); }

function addCiteBtns(suj, login)
{
	if (!xmlhttpOK) return;
	
	document.write("<a href='javascript:;' onmousedown='addCite(\""+suj+"\",\""+login+"\")' onmouseover=\"pop('Sauvegarder le texte selectionné de ce post comme citation')\" onmouseout='kill()'>:: Sauver</a> &nbsp;");
	document.write("<a href='javascript:;' onmousedown='videCite(\""+suj+"\")' onmouseover=\"pop('Vider les citations sauvegardées.')\" onmouseout='kill()'>:: Vider</a> ");
}

function addInsBtns(suj)
{
	if (!xmlhttpOK) return;
	
	document.write("<br /><br /><img src='"+gfx+"/bulle.gif' /> <a href='javascript:;' onmousedown='copyCites(\""+suj+"\")'><b>Insérer citations sauvées</b></a> - ");
	document.write("<a href='javascript:;' onmousedown='videCite(\""+suj+"\"); stopBubble=false;'> <b>Vider</b></a> <span id='xcTemp2'></span>");
	
}

var getPostBub = 0;
function getPost(e, suj, num)
{
	if (getPostBub == 0)
		popS(e,"<div id='getPostDiv'><a href='javascript:;' onclick='kill();'>X</a> - Chargement du post "+num+", veuillez patienter ...</div>");
	else if (getPostBub == 1)
		popMsg("<div id='getPostDiv'><a href='javascript:;' onclick='kill();'>X</a> - Chargement du post "+num+", veuillez patienter ...</div>");

	getPostBub  = 0;

	postURL("getPost.php","s="+suj+"&p="+num,"getPostClbk");
}

function getPostClbk(str)
{
	var postContent = str.substring(1);
	document.getElementById('getPostDiv').innerHTML = postContent;
}

var spanMS;
var prefixMS;

function getPostMS(e, suj, num, prefix)
{
	spanMS = suj;
	prefixMS = prefix;

	if (document.getElementById('r'+suj))
		document.getElementById('r'+suj).innerHTML = "<img src="+gfx+"/4-1.gif />";
	else
		document.getElementById(prefix+spanMS).innerHTML = "<blockquote class='cite'><a href='javascript:;' onclick='cleanMS("+suj+");'>X</a> - Chargement du post "+num+", veuillez patienter ...</blockquote>";

	postURL("getPost.php","dp="+prefix+"&s="+suj+"&p="+num,"getPostMSClbk");
}

function getPostMSClbk(str)
{
	var postContent = str.substring(1);
	document.getElementById(prefixMS+spanMS).innerHTML = "<blockquote class='cite'>"+postContent+"</blockquote>";
	if (SyntaxHighlighter) SyntaxHighlighter.highlight(); 
}

function cleanMS(str, prefix)
{
	document.getElementById(prefix+str).innerHTML = '';
}

function checkCross(e,suj, num)
{
	var avertCross = document.getElementById('avertCross');

	avertCross.style.display = '';
	avertCross.innerHTML = "Vérification cross post ...";

	var retour = postURLRet("getCross.php","s="+suj+"&num="+num);

	if (retour.substring(0,3) == ";ko")
	{
		var lesPosts = retour.substring(3);

		var txt = "<b style='color: #c30'>Attention, de nouveaux posts ont été ajoutés pendant que vous rédigiez le vôtre !</b><br /><br />";
		txt += "<b>Voici la liste des posts concernés :</b> " + lesPosts + "<br /><br />";
		txt += "<a href='javascript:document.fPost.submit()'><b>Cliquez ici pour poster quand même.</b></a> <div class='ligne'></div>";
		avertCross.innerHTML = txt;

		return false;
	}

	avertCross.innerHTML = "Vérification cross post ... <b style='color: green;'>OK</b> <div class='ligne'></div>";
	return true;
}

var tabMouseDown = new Array();
var tabMouseUp = new Array();


function delGetSel(num)
{
	tabMouseDown[num-1] = document.getElementById('pF'+(num-1)).onmousedown;
	tabMouseUp [num-1] = document.getElementById('pF'+(num-1)).onmouseup ;
 
	document.getElementById('pF'+(num-1)).onmousedown = null;
	document.getElementById('pF'+(num-1)).onmouseup = null;
}

function putGetSel(num)
{
	document.getElementById('pF'+(num-1)).onmousedown = tabMouseDown[num-1];
	document.getElementById('pF'+(num-1)).onmouseup = tabMouseUp[num-1];
}

function ePF(e,suj, num)
{
	var content = postURLRet("getPost.php","n=1&s="+suj+"&p="+(num-1));
	content = content.substring(1);

	delGetSel(num);

	document.getElementById('pC'+num).style.display = 'none';
	document.getElementById('pCe'+num).style.display = '';
	document.getElementById('pCe'+num).innerHTML = "<form method='action' onsubmit='return false'><textarea style='width: 100%' rows='"+(content.split('\n').length+1)+"' id='pFT"+num+"'>"+content+"</textarea><input type='button' class='fd' style='border: 0; background: transparent;' value=':: Valider' onclick=\"ePFS(this, "+suj+", "+num+");\" /> &nbsp; <a href='javascript:;' onclick=\"ePFA(this, "+suj+", "+num+");\">:: Annuler</a></form>";
	return false;
}

function ePFA(form, suj, num)
{
	document.getElementById('pC'+num).style.display = '';
	document.getElementById('pCe'+num).style.display = 'none';
	putGetSel(num);
}

function ePFS(form, suj, num)
{
	var toParse = escape(document.getElementById('pFT'+num).value).replace(/\+/g,"%2B");
	var isOk = postURLRet("addEn.php", "urldec=1&sujetID="+suj+"&numID="+num+"&texte="+toParse);

	var isOkBool = isOk.indexOf("editok");

	if (isOkBool == -1)
		alert("Le post n'a pas pu être edité, merci de passer par le popup d'édition de post.");
	else
	{
		var content = postURLRet("getYml.php","p="+toParse).substring(1);

		document.getElementById('pC'+num).innerHTML = content;
		document.getElementById('pC'+num).style.display = '';
		document.getElementById('pCe'+num).style.display = 'none';
	}
	putGetSel(num);
}

function box(el)
{
	var boxC = el.parentNode.childNodes[1];

	if(boxC.style.display == 'block')
	{
		boxC.style.display = 'none';
		el.childNodes[0].src = el.childNodes[0].src.replace(/get/,'put');
	}
	else
	{
		boxC.style.display = 'block';

		el.childNodes[0].src = el.childNodes[0].src.replace(/put/,'get');
	}	
}

var memoHead = "<div class='cite'><div style='padding: 2px'><b><img src='"+gfx+"/memo.png' />&nbsp; <a href='javascript:;' onClick='affRaccClose();return(false)'>Fermer le mémo</a></b></div><div class='ligne' clear: left;'></div>";
var memoFoot = "</div>";

function affMemo(e)
{
	if(navigator.appName.substring(0,3) != "Net") get_mouse();
	document.getElementById("mylinks").innerHTML = "<blockquote class='cite'><img src="+gfx+"/4-1.gif /> Chargement ...</blockquote>";

	var myl = document.getElementById("mylinks").style;
	myl.visibility = 'visible';
	with(myl) { left =  x - 65 + "px"; top = y+20 + "px"; }

	postURL("getMemo.php","", "getMemoClbk");
}

function getMemoClbk(str)
{
	var content = str.substring(1);

	content = "<div style='padding: 4px'>"+content+"</div><div class='ligne'></div><b><a href='javascript:;' onclick=\"affMemoEdit(event)\">:: Editer</a></b>";

 	document.getElementById("mylinks").innerHTML = memoHead + content + memoFoot ;
 	myl.visibility = 'visible';

	with(myl) { left =  x - 65 + "px"; top = y+20 + "px"; }
}

function affMemoEdit(e)
{
	document.getElementById("mylinks").innerHTML = "<blockquote class='cite'><img src="+gfx+"/4-1.gif /> Chargement ...</blockquote>";
	postURL("getMemo.php","np=1", "getMemoEditClbk");
}

function getMemoEditClbk(str)
{
	var content = str.substring(1);

	content = "<textarea cols='75' rows='10' id='txtMemo'>"+content+"</textarea><br /><b><a href='javascript:;' onclick='sauveMemo(event)'>:: Sauvegarder</a> &nbsp; <a href='javascript:;' onclick='affMemo(event)'>:: Annuler</a></b>";

 	document.getElementById("mylinks").innerHTML = memoHead  + content + memoFoot;
 	myl.visibility = 'visible';

	with(myl) { left =  x - 65 + "px"; top = y+20 + "px"; }
}

function sauveMemo(e)
{
	var toParse = escape(document.getElementById('txtMemo').value).replace(/\+/g,"%2B");

	document.getElementById("mylinks").innerHTML = "<blockquote class='cite'><img src="+gfx+"/4-1.gif /> Chargement ...</blockquote>";
	postURL("getMemo.php","txt="+toParse, "getMemoClbk");
}

function tf(ev, suj, num)
{
	postURL("pvotes.php","suj="+suj+"&num="+num,"tfClbk");
	popS(ev, "<div id='tf'>Chargement en cours ...</div>");
}

function tfv(ev, vt, suj, num)
{
	postURL("pvotes.php","vt="+vt+"&suj="+suj+"&num="+num,"tfClbk");
	popMsg("<div id='tf'>Vote en cours ...</div>");
}

function tfClbk(str)
{
	var postContent = str.substring(1);
	document.getElementById('tf').innerHTML = postContent;
}

/* ALERTE MODOS */

function aadminMenu(s, p)
{
	document.write("<a href='javascript:;' title='Signaler ce post' onclick=\"popS(event,'Voulez vous envoyer une alerte aux modérateurs sur le contenu de ce post ?<br /><i>(Attention : tout abus sera sanctionné)</i><br/><br/><b>Raison (obligatoire):</b><br /><textarea cols=\\'40\\' type=\\'text\\' id=\\'aaReason\\'></textarea><br /><br /><b><center><a href=\\'javascript:;\\' onclick=\\'aadmin(this, "+s+", "+p+")\\'>Envoyer</a> - <a href=\\'javascript:kill();\\'>Fermer la popup</a></center></b>')\"><img src='"+gfx+"/cl.png' alt='Signaler un abus' /></a>");
}

var aaEl;

function aadmin(el, s, p)
{
	var reason = document.getElementById("aaReason").value;
	
	if (!reason || reason.length == 0)
	{
		alert("Merci d'entrer une raison");
		return;
	}
	
	aaEl = el;
	aaEl.innerHTML = "<img src="+gfx+"/4-1.gif />";
	postURL("alertAdmin.php","s="+s+"&p="+p+"&r="+reason, "aadminOk");
}

function aadminOk(str)
{
	if (aaEl)
	{
		aaEl.onclick = kill;
		aaEl.innerHTML = "Alerte envoyée";
	}
}

function fork(s, n)
{
	document.write("<a href='javascript:;' title='Diviser le sujet' onclick='forkp("+s+", "+n+")'><img src='"+gfx+"/icones/21.gif' alt='Découper' /></a>");
}

var forkw;

function forkp(s, n)
{
	if (!window.forkw || window.forkw.closed)
	{
		forkw = window.open("fork.php?s="+s+"&amp;n="+n,'fpop','width=350,height=200,scrollbars=yes,resizable=1');
	}
	else
	{
		var fp = forkw.document.getElementById('fp');
		
		if (fp.value)	fp.value += ";"+n;
		else			fp.value += n;
		
		if(forkw.focus){forkw.focus();}
	}
}