function preg_replace (array_pattern, array_pattern_replace, my_string)  {
	var new_string = String (my_string);
		for (i=0; i<array_pattern.length; i++) {
			var reg_exp= RegExp(array_pattern[i], "gi");
			var val_to_replace = array_pattern_replace[i];
			new_string = new_string.replace (reg_exp, val_to_replace);
		}
		return new_string;
	}
function Clean(string)
{
patterns = array('/\./', '/\//');
preg_replace($patterns, '', string);
return string;
}
function getXMLHttpRequest() {
        var xhr = null;
 
        if(window.XMLHttpRequest || window.ActiveXObject) {
                if(window.ActiveXObject) {
                        try {
                                xhr = new ActiveXObject("Msxml2.XMLHTTP");
                        } catch(e) {
                                xhr = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                } else {
                        xhr = new XMLHttpRequest();
                }
        } else {
                alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
                return null;
        }
 
        return xhr;
}
function view(textareaId, viewDiv){
	var content = encodeURIComponent(document.getElementById(textareaId).value);
	var xhr = getXMLHttpRequest();
	
	if (xhr && xhr.readyState != 0) {
		xhr.abort();
		delete xhr;
	}
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200){
			document.getElementById(viewDiv).innerHTML = xhr.responseText;
		} else if (xhr.readyState == 3){
			document.getElementById(viewDiv).innerHTML = "<div style=\"text-align: center;\">Chargement en cours...</div>";
		}
	}
	
	var pseudo = encodeURIComponent(document.getElementById('pseudo').value);
	var os = encodeURIComponent(document.getElementById('OS').value);
	var titre = encodeURIComponent(document.getElementById('titre').value);
	
	xhr.open("POST", "Admin/preview.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send("contenu=" + content +"&pseudo="+pseudo+"&os="+os+"&titre="+titre);
}
function Load_Page(aff,page,method,send)	{
        var xhr = getXMLHttpRequest();
 
        if(xhr && xhr.readyState != 0) {
                xhr.abort(); 
        }
 
        xhr.onreadystatechange = function() { 
                if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                        document.getElementById('loader').style.display = 'none';
						document.getElementById(aff).innerHTML = xhr.responseText;
                } else if(xhr.readyState == 2 || xhr.readyState == 3) {
                        document.getElementById('loader').style.display = '';
                }
        }
		//Clean(page);
		
        xhr.open(method, page+send, true);
        xhr.setRequestHeader("Content-type", "charset=iso-8859-1");
        xhr.send(null);
}
function ConfirmMessage(url) {
       if (confirm("Voulez-vous vraiment supprimer ?")) { // Clic sur OK
           document.location.href=url;
       }
}
function ConfirmTicketClose(aff,page,method,send) {
       if (confirm("Voulez-vous vraiment fermer le ticket ?")) { // Clic sur OK
           Load_Page(aff,page,method,send);
       }
}
function switsh(id)
{
    if(document.getElementById(id).style.display == 'none')
	{
    document.getElementById(id).style.display = '';
    }
	else
	{
    document.getElementById(id).style.display = 'none';
	}
}
function close(id)
{
    if(document.getElementById(id).style.display == '')
	{
    document.getElementById(id).style.display = 'none';
    }
}
function refresh(aff,page,method,send,timeout)
{
Load_Page(aff,page,method,send);
setTimeout("refresh('"+aff+"','"+page+"','"+method+"','"+send+"',"+timeout+")", timeout);
//setTimeout("refresh('info','Pages/info.php','get','',3000)", 3000);
}
document.getElementById.value = function(id)
{
	var DocumentId = document.getElementById(id).options[document.getElementById(id).selectedIndex].text;
	return DocumentId;
}
function OuvrirPopup(page,nom,option) {
window.open(page,nom,option);
} //onclick="OuvrirPopup(\'upload.php\', \'\', \'resizable=no, location=no, width=500, height=500, menubar=no, status=no, scrollbars=no, menubar=no\')', '1'),




function insertTag(startTag, endTag, textareaId, tagType) {
        var field = document.getElementById(textareaId); 
		var scroll = field.scrollTop;
        field.focus();
        
        /* === Partie 1 : on récupère la sélection === */
        if (window.ActiveXObject) {
                var textRange = document.selection.createRange();            
                var currentSelection = textRange.text;
        } else {
                var startSelection   = field.value.substring(0, field.selectionStart);
                var currentSelection = field.value.substring(field.selectionStart, field.selectionEnd);
                var endSelection     = field.value.substring(field.selectionEnd);               
        }
        
        /* === Partie 2 : on analyse le tagType === */
        if (tagType) {
                switch (tagType) {
case "lien":
        endTag = "</lien>";
        if (currentSelection) { // Il y a une sélection
                if (currentSelection.indexOf("http://") == 0 || currentSelection.indexOf("https://") == 0 || currentSelection.indexOf("ftp://") == 0 || currentSelection.indexOf("www.") == 0) {
                        // La sélection semble être un lien. On demande alors le libellé
                        var label = prompt("Quel est le libellé du lien ?") || "";
                        startTag = "<lien url=\"" + currentSelection + "\">";
                        currentSelection = label;
                } else {
                        // La sélection n'est pas un lien, donc c'est le libelle. On demande alors l'URL
                        var URL = prompt("Quelle est l'url ?");
                        startTag = "<lien url=\"" + URL + "\">";
                }
        } else { // Pas de sélection, donc on demande l'URL et le libelle
                var URL = prompt("Quelle est l'url ?") || "";
                var label = prompt("Quel est le libellé du lien ?") || "";
                startTag = "<lien url=\"" + URL + "\">";
                currentSelection = label;                     
        }
break;

case "citation":
        endTag = "</citation>";
        if (currentSelection) { // Il y a une sélection
                if (currentSelection.length > 30) { // La longueur de la sélection est plus grande que 30. C'est certainement la citation, le pseudo fait rarement 20 caractères
                        var auteur = prompt("Quel est l'auteur de la citation ?") || "";
                        startTag = "<citation nom=\"" + auteur + "\">";
                } else { // On a l'Auteur, on demande la citation
                        var citation = prompt("Quelle est la citation ?") || "";
                        startTag = "<citation nom=\"" + currentSelection + "\">";
                        currentSelection = citation;    
                }
        } else { // Pas de selection, donc on demande l'Auteur et la Citation
                var auteur = prompt("Quel est l'auteur de la citation ?") || "";
                var citation = prompt("Quelle est la citation ?") || "";
                startTag = "<citation nom=\"" + auteur + "\">";
                currentSelection = citation;    
        }
break;

                }
        }
        
        /* === Partie 3 : on insère le tout === */
        if (window.ActiveXObject) {
                textRange.text = startTag + currentSelection + endTag;
                textRange.moveStart("character", -endTag.length - currentSelection.length);
                textRange.moveEnd("character", -endTag.length);
                textRange.select();
				field.scrollTop = scroll;
				
        } else {
                field.value = startSelection + startTag + currentSelection + endTag + endSelection;
                field.focus();
                field.setSelectionRange(startSelection.length + startTag.length, startSelection.length + startTag.length + currentSelection.length);
				field.scrollTop = scroll;
		}      
		 
}
