

function insertTag(startTag, endTag, textareaId, tagType) {
	var field = document.getElementById(textareaId);
	var scroll = field.scrollTop;
	field.focus();
	
	
        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);               
        }

	
	if (tagType) {
		switch (tagType) {
			case "lien":
					endTag = "</lien>";
					if (currentSelection) {
							if (currentSelection.indexOf("http://") == 0 || currentSelection.indexOf("https://") == 0 || currentSelection.indexOf("ftp://") == 0 || currentSelection.indexOf("www.") == 0) {
									var label = prompt("Quel est le libellé du lien ?") || "";
									startTag = "<lien url=\"" + currentSelection + "\">";
									currentSelection = label;
							} else {
									var URL = prompt("Quelle est l'url ? (Avec http://)");
									startTag = "<lien url=\"" + URL + "\">";
							}
					} else {
							var URL = prompt("Quelle est l'url ? (Avec http://)") || "";
							var label = prompt("Quel est le libellé du lien ?") || "";
							startTag = "<lien url=\"" + URL + "\">";
							currentSelection = label;                     
					}
			break;
			case "citation":
					endTag = "</citation>";
					if (currentSelection) {
							if (currentSelection.length > 30) {
									var auteur = prompt("Quel est l'auteur de la citation ?") || "";
									startTag = "<citation nom=\"" + auteur + "\">";
							} else {
									var citation = prompt("Quelle est la citation ?") || "";
									startTag = "<citation nom=\"" + currentSelection + "\">";
									currentSelection = citation;    
							}
					} else {
							var auteur = prompt("Quel est l'auteur de la citation ?") || "";
							var citation = prompt("Quelle est la citation ?") || "";
							startTag = "<citation nom=\"" + auteur + "\">";
							currentSelection = citation;    
					}
			break;
			case "video":
					endTag = "</video>";
					if (currentSelection) {
							if (currentSelection.length > 30) {
									var titre_video = prompt("Quel est le titre de la vidéo ?") || "";
									startTag = "<video=\"" + titre_video + "\">";
							} else {
									var url_video = prompt("Quelle est l'Url de la vidéo ?") || "";
									startTag = "<video=\"" + currentSelection + "\">";
									currentSelection = url_video;    
							}
					} else {
							var titre_video = prompt("Quel est le titre de la vidéo ?") || "";
							var url_video = prompt("Quelle est l'Url de la vidéo ?") || "";
							startTag = "<video=\"" + titre_video + "\">";
							currentSelection = url_video;    
					}
			break;				
		}
	}
	
	if (window.ActiveXObject) {
		textRange.text = startTag + currentSelection + endTag;
		textRange.moveStart('character', -endTag.length-currentSelection.length);
		textRange.moveEnd('character', -endTag.length);
		textRange.select();  
	} else { // Ce n'est pas IE
		field.value = startSelection + startTag + currentSelection + endTag + endSelection;
		field.focus();
		field.setSelectionRange(startSelection.length + startTag.length, startSelection.length + startTag.length + currentSelection.length);
	}  
	
	field.scrollTop = scroll; 

        field.scrollTop = scroll; // et on redéfinit le scroll
		document.getElementById('taille_form').options[0].selected = true; 
		document.getElementById('couleur_form').options[0].selected = true; 
		document.getElementById('titre_form').options[0].selected = true; 
		document.getElementById('position_form').options[0].selected = true; 
}







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 getElementsByClass_(classe)
{
     var divs = document.getElementsByTagName('div');
     var resultats = new Array();
     for(var i=0; i<divs.length; i++)
          if(divs[i].className == classe)
               resultats.push(divs[i]);
     return resultats;
}



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 cour...</div>";
		}
	}
	
	xhr.open("POST", "inc/view_bbcode.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send("string=" + content);
	
}

document.getElementsByClassName = function(className, elmt)
{
   var selection = new Array();
   var regex = new RegExp("\\b" + className + "\\b");

   // le second argument, facultatif
   if(!elmt)
      elmt = document;
   else if(typeof document == "string")
      elmt = document.getElementById(elmt);
   
   // on sélectionne les éléments ayant la bonne classe
   var elmts = elmt.getElementsByTagName("*");
   for(var i=0; i<elmts.length; i++)
      if(regex.test(elmts[i].className))
         selection.push(elmts[i]);

   return selection;
}


function resize(imgs,maxWidth,maxHeight) {

	for (var i = 0, a = imgs.length; i < a; i++) {
		imgs[i].onload = function() {
			var width = this.offsetWidth;
			var height = this.offsetHeight;
			
			if (width > maxWidth || height > maxHeight) {
				if ((width / maxWidth) > (height / maxHeight)) {
					var nHeight = (height * maxWidth) / width;
					var nWidth = maxWidth;
				}
				else {
					var nWidth = (width * maxHeight) / height;
					var nHeight = maxHeight;
				}
				
				this.style.width = nWidth + 'px';
				this.style.height = nHeight + 'px';
				this.nWidth = width;
				this.nHeight = height;
				this.sWidth = nWidth;
				this.sHeight = nHeight;
			}
		};
		
		imgs[i].onclick = function() {
			if (this.nWidth) {
				if (this.offsetWidth == this.nWidth) {
					this.style.width = this.sWidth + 'px';
					this.style.height = this.sHeight + 'px';
				}
				else {
					this.style.width = this.nWidth + 'px';
					this.style.height = this.nHeight + 'px';
				}
			}
		};
		imgs[i].src = imgs[i].src;
	}
 }


