// JavaScript Document
var isMozilla = (navigator.userAgent.toLowerCase().indexOf('gecko')!=-1) ? true : false;
var regexp = new RegExp("[\r]","gi");

function storeCaret(div,as_mf){
	ao_txtfield = document.getElementById(div);
	
	var isIE = (document.all);
	if(!isIE){
	//mozilla
	//recuperation du txt selectionné 
	oField = ao_txtfield;
	oFieldValue = oField.value;
	deb = oField.selectionStart;
	fin = oField.selectionEnd;
	Deb = oFieldValue.substring( 0 , oField.selectionStart );
	Fin = oFieldValue.substring( oField.selectionEnd , oField.textLength );
	Sel = oFieldValue.substring( oField.selectionStart ,oField.selectionEnd ); 
	oField.value = Deb + '[' + as_mf + "]" + Sel + "[/" + as_mf + ']' + Fin;
	oField.selectionStart = Deb.length;
	tmps =Deb + '[' + as_mf+']' + Sel +"[/" + as_mf + ']'
	oField.selectionEnd = tmps.length+2;
	oField.setSelectionRange(Deb.length+as_mf.length+2,tmps.length-as_mf.length-3);
	oField.focus();
	}else{
	//IE
	var str = document.selection.createRange().text;
	if (str.length>0){
	//recuperation du txt selectionné
	var select = document.selection.createRange();
	select.text = '[' + as_mf + ']' + str + "[/" + as_mf + ']';
	select.collapse();
	select.select();
	}else{
	ao_txtfield.focus(ao_txtfield.caretPos);
	ao_txtfield.focus(ao_txtfield.value.length);
	ao_txtfield.caretPos = document.selection.createRange().duplicate(); 
	var bidon = "%~%";
	var original = ao_txtfield.value;
	ao_txtfield.caretPos.text = bidon;
	var i = ao_txtfield.value.search(bidon);
	ao_txtfield.value = original.substr(0,i) + '[' + as_mf + "][/" + as_mf + ']' + original.substr(i, ao_txtfield.value.length);
	var temp = original.substr(0,i);
	var nbretour =0;
	for (var cpt=0;cpt<temp.length;cpt=cpt+1){
	if(temp.charAt(cpt)=="\n"){
	nbretour=nbretour+1;
	}
	}
	pos = i + 2 + as_mf.length - nbretour;
	var r = ao_txtfield.createTextRange();
	r.moveStart('character', pos);
	r.collapse();
	r.select();
	}
	}
}

function storeCaretValue(div,as_mf,as_url){
	ao_txtfield = document.getElementById(div);


	var isIE = (document.all);
	if(!isIE){
		//mozilla
		//recuperation du txt selectionné
		oField = ao_txtfield;
		oFieldValue = oField.value;
		deb = oField.selectionStart;
		fin = oField.selectionEnd;
		Deb = oFieldValue.substring( 0 , oField.selectionStart );
		Fin = oFieldValue.substring( oField.selectionEnd , oField.textLength );
		Sel = oFieldValue.substring( oField.selectionStart ,oField.selectionEnd ); 
		oField.value = Deb + '[' + as_mf+'='+as_url+ '*'+as_mf+']' + Sel + '[/' + as_mf + ']' + Fin;
		oField.selectionStart = Deb.length;
		tmps =Deb + '[' + as_mf+'=' +as_url+ '*'+as_mf+']' + Sel +'[/' + as_mf + ']';
		oField.selectionEnd = tmps.length+3;
		oField.setSelectionRange(Deb.length+(as_mf.length*2)+as_url.length+4,tmps.length-as_mf.length-3);
		oField.focus();
	}else{
		//IE
		var str = document.selection.createRange().text;
		if (str.length>0){
			//recuperation du txt selectionné
			var select = document.selection.createRange();
			select.text = '[' + as_mf +'='+as_url+ '*'+as_mf+']' + str + '[/' + as_mf + ']';
			select.collapse();
			select.select();
		}else{
			ao_txtfield.focus(ao_txtfield.caretPos);
			ao_txtfield.focus(ao_txtfield.value.length);
			ao_txtfield.caretPos = document.selection.createRange().duplicate(); 
			var bidon = "%~%";
			var original = ao_txtfield.value;
			ao_txtfield.caretPos.text = bidon;
			var i = ao_txtfield.value.search(bidon);
			ao_txtfield.value = original.substr(0,i) + '[' + as_mf +'='+as_url+ '*'+as_mf+'][/' + as_mf + ']' + original.substr(i, ao_txtfield.value.length);
			var temp = original.substr(0,i);
			var nbretour =0;
			for (var cpt=0;cpt<temp.length;cpt=cpt+1){
				if(temp.charAt(cpt)=="\n"){
				nbretour=nbretour+1;
				}
			}
			var chaine = '[' + as_mf +'='+as_url+ '*'+as_mf+']';
			var pos = i + chaine.length - nbretour;
			var r = ao_txtfield.createTextRange();
			r.moveStart('character', pos);
			r.collapse();
			r.select();
		}
	}
}
 
function storeCaretSimple(div,as_mf,as_url){
	ao_txtfield = document.getElementById(div);


	var isIE = (document.all);
	if(!isIE){
		//mozilla
		//recuperation du txt selectionné
		oField = ao_txtfield;
		oFieldValue = oField.value;
		deb = oField.selectionStart;
		fin = oField.selectionEnd;
		Deb = oFieldValue.substring( 0 , oField.selectionStart );
		Fin = oFieldValue.substring( oField.selectionEnd , oField.textLength );
		Sel = oFieldValue.substring( oField.selectionStart ,oField.selectionEnd ); 
		oField.value = Deb + '[' + as_mf+'='+as_url+ '*'+as_mf+']' + Fin;
		oField.focus();
	}else{
		//IE
		var str = document.selection.createRange().text;
		
			ao_txtfield.focus(ao_txtfield.caretPos);
			ao_txtfield.focus(ao_txtfield.value.length);
			ao_txtfield.caretPos = document.selection.createRange().duplicate(); 
			var bidon = "%~%";
			var original = ao_txtfield.value;
			ao_txtfield.caretPos.text = bidon;
			var i = ao_txtfield.value.search(bidon);
			ao_txtfield.value = original.substr(0,i) + '[' + as_mf +'='+as_url+ '*'+as_mf+']' + original.substr(i, ao_txtfield.value.length);
	}
}
 
/*
function resetTxtField(ao_input){
	var ls_texte = ao_input.value;
	var la_ar = ls_texte.split("[u]");
	var tamp="";
	for(var i=0;i<la_ar.length;i++){
	tamp+=la_ar;
	}
	ls_texte = tamp;
	tamp="";
	la_ar = ls_texte.split("[/u]");
	for(var i=0;i<la_ar.length;i++){
	tamp+=la_ar[i];
	}
	ls_texte = tamp;
	tamp="";
	la_ar = ls_texte.split("[i]");
	for(var i=0;i<la_ar.length;i++){
	tamp+=la_ar[i];
	}
	ls_texte = tamp;
	tamp="";
	la_ar = ls_texte.split("[/i]");
	for(var i=0;i<la_ar.length;i++){
	tamp+=la_ar[i];
	}
	ls_texte = tamp;
	tamp="";
	la_ar = ls_texte.split("[b]");
	for(var i=0;i<la_ar.length;i++){
	tamp+=la_ar[i];
	}
	ls_texte = tamp;
	tamp="";
	la_ar = ls_texte.split("[/b]");
	for(var i=0;i<la_ar.length;i++){
	tamp+=la_ar[i];
	}
	ls_texte = tamp;
	tamp="";
	la_ar = ls_texte.split("[/url]");
	for(var i=0;i<la_ar.length;i++){
	tamp+=la_ar[i];
	}
	ls_texte = tamp;
	var tamp="";
	la_ar = ls_texte.split("[/color]");
	for(var i=0;i<la_ar.length;i++){
	tamp+=la_ar[i];
	}
	ls_texte = tamp;
	var tamp="";
	var bfind=false;
	for(var i=0;i<ls_texte.length;i++){
	if(ls_texte.charAt(i)=="["&&ls_texte.charAt(i+1)=="u"&&ls_texte.charAt(i+2)=="r"&&ls_texte.charAt(i+3)=="l"){
	bfind=true;
	}
	if(bfind){
	if(ls_texte.charAt(i)=="]"){
	bfind=false;
	}
	}else{
	tamp+=ls_texte.charAt(i);
	}
	}
	ls_texte = tamp;
	var tamp="";
	var bfind=false;
	for(var i=0;i<ls_texte.length;i++){
	if(ls_texte.charAt(i)=="["&&ls_texte.charAt(i+1)=="i"&&ls_texte.charAt(i+2)=="m"&&ls_texte.charAt(i+3)=="g"){
	bfind=true;
	}
	if(bfind){
	if(ls_texte.charAt(i)=="]"){
	bfind=false;
	}
	}else{
	tamp+=ls_texte.charAt(i);
	}
	}
	ls_texte = tamp;
	tamp="";
	var bfind=false;
	for(var i=0;i<ls_texte.length;i++){
	if(ls_texte.charAt(i)=="["&&ls_texte.charAt(i+1)=="c"&&ls_texte.charAt(i+2)=="o"&&ls_texte.charAt(i+3)=="l"&&ls_texte.charAt(i+4)=="o"&&ls_texte.charAt(i+5)=="r"){
	bfind=true;
	}
	if(bfind){
	if(ls_texte.charAt(i)=="]"){
	bfind=false;
	}
	}else{
	tamp+=ls_texte.charAt(i);
	}
	}
	ls_texte = tamp;
	ao_input.value=ls_texte; 
} 
 
*/ 
 /* code HTML 
 
<form action="test.php" method="POST">
<div>
<script language="javascript" src="_js/bbcode.js"></script>
<input type="button" class="form_bouton" value="b" style="font-weight:bold" onClick="storeCaret(this.form.newst,'b');" />
<input type="button" class="form_bouton" value="i" style="font-style:italic" onClick="storeCaret(this.form.newst,'i');" />
<input type="button" class="form_bouton" value="u" style="text-decoration:underline" onClick="storeCaret(this.form.newst,'u');" />
<input type="button" class="form_bouton" value="url" onClick="saisie = prompt('url du site','Adresse');if(saisie!=null){storeCaretValue(this.form.newst,'url',saisie);}" />
<input type="button" class="form_bouton" value=" " style="background-color:#000000;" onClick="storeCaretValue(this.form.newst,'color','#000000');" />
<input type="button" class="form_bouton" value=" " style="background-color:#820000;" onClick="storeCaretValue(this.form.newst,'color','#820000');" />
<input type="button" class="form_bouton" value=" " style="background-color:#9e7023;" onClick="storeCaretValue(this.form.newst,'color','#9e7023');" />
<input type="button" class="form_bouton" value=" " style="background-color:#D9AE4A;" onClick="storeCaretValue(this.form.newst,'color','#D9AE4A');" />
<input type="button" class="form_bouton" value=" " style="background-color:#E2C490;" onClick="storeCaretValue(this.form.newst,'color','#E2C490');" />
<input type="button" class="form_bouton" value=" " style="background-color:#3366FF;" onClick="storeCaretValue(this.form.newst,'color','#3366FF');" />
</div>
<div>
<textarea name="newst" id="newst" rows="10" wrap="virtual" cols="45"><?php echo $newss; ?></textarea>
</div>
<div>
<input type="submit" value="Mettre en forme" /><input type="button" value="Annuler mise en forme" onClick="resetTxtField(this.form.newst);" />
</div>
</form>

 */

/* code PHP

<?php
function replaceBBCode($as_texte){
	$texte = '<p>'.$as_texte.'</p>';
	$texte = str_replace($search1,"<span class='italictext'>",str_replace($search2,"</span>",$texte));
	$search1 = array("[u]");
	$search2 = array("[/u]");
	$texte = str_replace($search1,"<u>",str_replace($search2,"</u>",$texte));
	$search1 = array("[i]");
	$search2 = array("[/i]");
	$texte = str_replace($search1,"<i>",str_replace($search2,"</i>",$texte));
	$search1 = array("[b]");
	$search2 = array("[/b]");
	$texte = str_replace($search1,"<b>",str_replace($search2,"</b>",$texte));
	$search1 = array("[url=");
	$search2 = array("*url]");
	$search3 = array("[/url]");
	$texte = str_replace($search1,"<a href='",str_replace($search2,"' target='_blank'>",str_replace($search3,"</a>",$texte)));
	$search1 = array("[color=");
	$search2 = array("*color]");
	$search3 = array("[/color]");
	$texte = str_replace($search1,"<span style='color:",str_replace($search2,"'>",str_replace($search3,"</span>",$texte)));
	return $texte;
}
if(isset($_POST["newst"])){
	$newss = $_POST["newst"];
	echo replaceBBCode($newss, ENT_QUOTES);
}
?>


*/
