// $Id: bjs.js 830 2008-12-15 18:18:08Z mike007.devpwsphp $
/**
*
*fichier bjs.js
* bbcode en javascript pour faire des previsus
*par zeta (et tout ptit bout de TofClock,)
*idee originale du siteduzero et code perso
*
*/


inter_prev=setInterval(bjss,500);
ok_pr=true;
zed='';

function bjss()
{
	bjs(document.getElementById('postzone').value,'apercu');
}

function pr_act(t)
{
	to=document.getElementById('apercu');
	actu=document.getElementById('prev');

	if(t.checked)
	{
		/* Pris par le css du theme (style.css) */
		/* to.style.cssText='border:1px solid #000;text-align:left;width:580px;height:150px;overflow:auto; padding :2px;'; */
		to.style.display='block';
		actu.style.visibility='visible';
		ok_pr=true;
		bjs(document.getElementById('postzone').value,'apercu');
		inter_prev=setInterval(bjss,500);
	}
	else
	{
		clearInterval(inter_prev);
		to.style.display='none';
		actu.style.visibility='hidden';
		ok_pr=false;
	}
}

function bjs(txt,t)
{
	if(ok_pr==true)
	{
		post=document.getElementById('postzone');
		to=document.getElementById(t);
		txt=txt.replace("<","&lt;","gi");
		txt=txt.replace(">","&gt;","gi");
		//rein a faire il a fallut l'emprunter au sdz cette astuce :(
		while(txt.indexOf("\n")!=-1)
		{
			p=txt.indexOf("\n");
			txt=txt.substring(0, p)+"<br />"+txt.substring(p+1, txt.length);
		}

		if(typeof(code_smil)!='undefined')
		{
			for(a in code_smil)
			{
				txt=txt.replace(code_smil[a],'<img src="'+image_smil[a]+'" alt="smiley '+code_smil[a]+'" />','g');

			}
		}
		balises=new Array(
 					/\[(taille|size)=(([0-3])[0-9])\](.+?)\[\/(taille|size)\]/gi,
					/\[center\](.*?)\[panneau\](.+?)\[\/panneau\](.*?)\[\/center\]/gi,
					/\[align=center\](.*?)\[panneau\](.+?)\[\/panneau\](.*?)\[\/align\]/gi,
					/\[panneau\](.+?)\[\/panneau\]/gi,
					/\[puce\](.+?)\[\/puce\]/gi,
					/\[surlign\](.+?)\[\/surlign\]/gi,
					/\[surlign=(\#[0-9A-F]{6}|[a-z]+)\](.+?)\[\/surlign\]/gi,
					/\[(center|justify|left|right)\](.+?)\[\/(center|justify|left|right)\]/gi,
					/\[align=(center|justify|left|right)+\](.+?)\[\/align\]/gi,
					/\[float=(left|right)\](.+?)\[\/float\]/gi,
					/\[u\](.+?)\[\/u\]/gi,
					/\[glow\](.+?)\[\/glow\]/gi,
					/\[b\](.+?)\[\/b\]/gi,
					/\[i\](.+?)\[\/i\]/gi,
					/\[barre\](.+?)\[\/barre\]/gi,
					/([^-\]^=^/";.])(www.)([\w./=&~?;\-_\#]+)/gi,
					/([^-\]^=";.])(http:\/\/|ftp:\/\/)([\w./=&~?;\-_\#]+)/gi,
					/\[url\](http:\/\/|ftp:\/\/)([^"'()<>\]\[]+?)\[\/url\]/gi,
					/\[url\]www(.[^"'()\<\>\]\[]+?)\[\/url\]/gi,
					/\[url=(www.)(.[^"'()<>\]\[]+?)\](.*?)\[\/url\]/gi,
					/\[url=(.[^"'()<>\]\[]+?)\](.+?)\[\/url\]/gi,
					/\[(color|couleur)=(\#[0-9A-F]{6}|[a-z]+)\](.+?)\[\/(color|couleur)\]/gi,
					/\[img\]www(.[^"'()<>\]\[]+?)\[\/img\]/gi,
					/\[img\](.[^"'()<>]+?)\[\/img\]/gi,
					/\[hr\]/gi,
					/<!--smil s(.*?).[a-z]{0,4}-->/gi

				);
		rempl=new Array(
					'<span style="font-size:$2px">$4</span>',
					'[center]$1 [/center]<table cellspacing="0" cellpadding="0" style="border:0px;text-align:center;margin:auto;"><tbody><tr><td><img src="images/pancarte_haut.png" alt="Panneau haut" /></td></tr><tr><td class="panneau">$2</td></tr><tr><td><img alt="Pancarte" src="./images/pancarte.png" /></td></tr></tbody></table>[center]$3 [/center]',
					'[center]$1 [/center]<table cellspacing="0" cellpadding="0" style="border:0px;text-align:center;margin:auto;"><tbody><tr><td><img src="images/pancarte_haut.png" alt="Panneau haut" /></td></tr><tr><td class="panneau">$2</td></tr><tr><td><img alt="Pancarte" src="./images/pancarte.png" /></td></tr></tbody></table>[center]$3 [/center]',
					'<table cellspacing="0" cellpadding="0" style="border:0px;text-align:center"><tbody><tr><td><img src="images/pancarte_haut.png" alt="Panneau haut" /></td></tr><tr><td class="panneau">$1</td></tr><tr><td><img alt="Pancarte" src="./images/pancarte.png" /></td></tr></tbody></table>',
					'<ul><li style="margin-left:12px;">$1</li></ul>',
					'<span class="surlign">$1</span>',
					'<span style="background-color:$1">$2</span>',
					'<span style="float:left;width:100%;text-align:$1">$2</span><br style="clear:both" />',
					'<span style="float:left;width:100%;text-align:$1">$2</span><br style="clear:both" />',
					'<span style="margin:5px;float:$1">$2</span>',
					'<span style="text-decoration: underline;">$1</span>',
					'<div style="width:100%;filter:glow(color=#000000,strength=1);">$1</div>',
					'<span style="font-weight:bold">$1</span>',
					'<span style="font-style:italic">$1</span>',
					'<strike>$1</strike>',
					'$1<a href="http://$2$3" target="_blank">$2$3</a>',
					'$1<a href="$2$3" target="_blank">$2$3</a>',
					'<a href="$1$2" target="_blank">$1$2</a>',
					'<a href="http://www$1" target="_blank">www$1</a>',
					'<a href="http://$1$2" target="_blank">$3</a>',
					'<a href="$1" target="_blank">$2</a>',
					'<span style="color:$2">$3</span>',
					'<img style=" max-width:640px;max-height:400px;border:0px;overflow:auto;" alt="image" src="http://www$1" />',
					'<img style=" max-width:640px;max-height:400px;border:0px;overflow:auto;" alt="image" src="$1" />',
					'<hr style="border:1px solid" />',
					' '
				);
		for(b in balises)
		{
			txt=txt.replace(balises[b],rempl[b]);
		}

		testquo=txt.indexOf("\[/quote\]");
		while ( testquo != -1 )
		{
			txtemp=txt.substring(0,testquo+8);
			if ( txt.length > testquo+8 )
			{
				txtfin=txt.substring(testquo+8,txt.length);
			}
			else
			{
				txtfin="";
			}
			debcit=txtemp.lastIndexOf("\[quote");
			txtdeb=txtemp.substring(0,debcit);
			txtemp=txtemp.substring(debcit,testquo);
			if ( txtemp.charAt(6) == "=" )
			{
				finciteur=txtemp.indexOf("\]");
				leciteur=txtemp.substring(7,finciteur);
				txtemp="<br /><b>Citation de "+leciteur+" :</b><div class='quote'>"+txtemp.substring(finciteur+1,txtemp.length)+"</div>";
			}
			else
			{
				txtemp="<br /><b>Citation :</b><div class='quote'>"+txtemp.substring(7,txtemp.length)+"</div>";
			}
			txt=txtdeb+txtemp+txtfin;
			testquo=txt.indexOf("\[/quote\]");
		}

		to.innerHTML=txt;
		to.scrollTop=post.scrollTop;
	}
}

function finalprev(url,txt,whe)
{
	clearInterval(inter_prev);
        var http_request = false;
        if (window.XMLHttpRequest) 
	{
		// Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType)
		{
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	}
	else if (window.ActiveXObject)
	{
		// IE
		try
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
			}
		}
	}
        if (!http_request)
	{
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = function() { aff_prev(http_request,whe); };
	http_request.open('POST', url, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send("txt="+escape(txt));
}

function aff_prev(http_request,whe)
{
	if (http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
			if(ok_pr==false)
			{
				var t=new Object();
				t.checked=true;
				pr_act(t);
			}
			document.getElementById(whe).innerHTML=http_request.responseText;
		}
		else
		{
			alert('There was a problem with the request.');
		}
	}
}

