/*
Script: InputTransform.js
	Replaces Input Fields with TextArea fields for easier editing of multilang content in Moodle 1.5

Author: 
	Sandro Ducceschi [Liip AG, Switzerland]

*/

var InputTransform = {
	
	/*
	   Public Variables
	---------------------------------------------------------------------------------
	*/
	sInputType: 		"text", 				// Tag name for input elements to replace
	sInputReplace: 		"textarea", 			// Replace the sInputType with this
	sInputReplaceCols:	65,		// Width of the sInputType
	sSubmitType: 		"submit", 				// Tag to look for where to add the injection of the new fullContent
	sSubmitReplace: 	"input",				// Replace the sSubmitType with this
	sSubmitReplaceType: "button",				// If sSubmitReplace is an input element specify it's type
	_parent:			null,	// public value for the element.parentNode
	_incremental:		0,		// for the freaking element id's
	_currentLanguage:	"de",	// current Language : defaults to german
	
	// elements that should NOT be transformed
	_excludes:			["penalty", "defaultgrade", "generalfeedback", "freelink[0]", , "freelink[1]", "freelink[2]", "freelink[3]", , "freelink[4]", "freelink[5]",
        "backgroundpath[value]", "filepath[0][value]", "filepath[1][value]",  "filepath[2][value]",   "filepath[3][value]", "filepath[4][value]", "filepath[5][value]"],		
	
	// Language Settings
	requiredLangs:		["de","fr","it"],			// Array with the Required Languages
	langLong:			["deutsch","franais","italiano"],	// Languages written out.
	editSource:			new Boolean(true),	// Comment this out if you don't want to allow src editing
	
	/*
	   Init
	---------------------------------------------------------------------------------
	*/
	init:function() {
		
		if(this.editSource){
			this.requiredLangs.push("src"); 		
		}
		
		var textAreas = document.getElementsByTagName( 'textarea');		// get all the TextAreas
		
		for( var i=textAreas.length-1; i>=0; i-- ) {
			var oldTextArea = textAreas[i];
			
			if(oldTextArea.name == "questiontext"){
				var tempSRC = oldTextArea.value
			}
			else if(this.isExcluded(oldTextArea.name)){
			}
			else{
				var recreatedElement = this.createTextArea( oldTextArea );				// create a new TextArea
				this.createBreak();			// add a break
				this.createLinks( recreatedElement );		// remove the old one
				this.removeOldElement( oldTextArea );	
				recreatedElement.currentLang = this._currentLanguage;	// switch the current language
				recreatedElement.value = recreatedElement[ this._currentLanguage ];
			}
			
		}
				
		var formInputs = document.getElementsByTagName( 'input' );		// get all the Input Tags
		
		
		for(var i=formInputs.length-1; i>=0; i-- ) {		// weeeeh! i like loops
			var inputField = formInputs[ i ];
			
			if(this.isExcluded(inputField.name)){
			}
			else {	
				if ( inputField.type == this.sInputType ) {	// if we found a text input time to replace
			
	var createdElement = this.createTextArea( inputField );				// create a new TextArea
	this.createBreak();			// add a break
	this.createLinks( createdElement );	
	this.setInputLang(createdElement.id,this._currentLanguage);	// create the Language Links
	this.removeOldElement( inputField );		// remove the old input field
				}
				
				else if (inputField.type == "submit" && inputField.name != "cancel" ) {	// if we found a SPECIFIC submit button 
			
	this.createSubmitter( inputField );			// create a replacement
	this.removeOldElement( inputField );		// remove the old one
				}
			}
		}
		
		/*
		var editorArea = document.getElementById('edit-questiontext');
		editorArea.src = tempSRC;
		editorArea.value = tempSRC;
		var newEditorContent = this.parseContent(editorArea,true);
		edit_questiontext.setHTML(newEditorContent);
		*/
	},
	
	/*
	   Methods
	---------------------------------------------------------------------------------
	*/
	 createSubmitter: function ( element ) {
		this._parent = element.parentNode;				// let's save it's _parent ...
		var buttonValue = element.value; 
		var buttonName = element.name; // ...and name attribute for later use
	
		var newButton = document.createElement( this.sSubmitReplace );				// create the replacement for the previous element
		
		if( this.sSubmitReplace == "input" ) {			// in case it's an input element specify a type
			newButton.type = this.sSubmitReplaceType;
		}
		
		newButton.value = buttonValue; //assign the value to the element	
		newButton.name = buttonName;
		newButton.type = "submit";
		newButton.onclick = new Function( "F", "InputTransform.buildContent(1)" );	// event handler
		this._parent.appendChild( newButton );			//insert the sucker!
		
	 },
	
	
	createTextArea: function( element ) {
		this._parent = element.parentNode;				// let's save it's _parent ..
		var content = element.value;					// ..current content ...
		var elementID = element.name;					// ..and name attribute for later use
			
		var newTextArea = document.createElement( this.sInputReplace ); 			// ...and replace with a brand-spanking new textarea field
		
		newTextArea.name = elementID;					// also add the name ..
		newTextArea.id = this.trim(elementID,"[]")+this._incremental++;			// .. and id attribute with the name attribute from the text field
		newTextArea.currentLang = null;					// also add an empty var for the language
		
		// create an onkeyup event
		newTextArea.onkeyup = new Function( 'F', 'InputTransform.proccessTextChange( this )' );
 
		this._parent.appendChild( newTextArea );		// let's insert the new textarea at the spot the input field was

		newTextArea.value = content;					// and let's give it the old content back
		newTextArea.src = content;
		
		newTextArea.cols = this.sInputReplaceCols;
		newTextArea.rows = 4;
		
		return newTextArea;
	},
	
	
	createLinks: function( element ) {
		var langs = this.parseContent( element );			// parse the Content and generate an Language Array
		
		var linkDiv = this.createDiv( 'linkdiv' );			// create a div that houses the links
		//linkDiv.style.textAlign = "right";	// for prettyness align to the right
		this._parent.appendChild( linkDiv );	// add the div to the parent element
		
		for( a=0; a<langs.length; a++ ) {
			var lang = langs[a];			 	//save the lang temporary			
		
			//create the link
			var link = document.createElement( "a" );
			link.setAttribute( 'href', 'javascript:InputTransform.setInputLang("' + element.id + '","' + lang + '" )' );
			link.innerHTML = lang;
			linkDiv.appendChild( document.createTextNode( "\u00a0" ) ); 	// creates a space in between
			linkDiv.appendChild( link );
		}
	},
	
	hasLangSpan: function( element , tempContent ) {
		if(element.src != ""){			// check for ANY content
			if(tempContent.childNodes[0].hasChildNodes()){			// check for childnodes 
				if(tempContent.childNodes[0].hasAttributes()){		// check for attributes
	if(tempContent.childNodes[0].getAttribute( 'lang' ) != null){ 	// check for lang attribute
		return true;					// found some!
	}	
				}	
			}
			else {
			}	
		}
		return false;
	},
	
	parseContent: function( element , returnstring) {
		var tempContent = this.addTempContainer( 'parseDiv' );		// create temporary div to juggle the content
		
		
		//tempContent.innerHTML = element.src;		 	// inject content to tempDiv so we can access it through DOM
		
		
		// rewrite content in order to show "sort of" valid html
		
		var rewritenContent = this.str_replace("<lang lang", "<span lang",element.src);
		rewritenContent = this.str_replace("/lang>", "/span>",rewritenContent);
		tempContent.innerHTML = rewritenContent;
	
		var langArray = this.requiredLangs; 			// fill a language Array
				
		
		if(!this.hasLangSpan( element, tempContent ) ){	// incase it didn't exist...
			
			var brandNewContent = "";					// .. let's create the default lang entries
			for(i=0; i<langArray.length;i++){			// .. with the languages specified in requiredLangs
				switch( langArray[i] ) {
	case "de":		
			// since we expect german to be default in this case append the content to this span ..
		brandNewContent += "<span lang=\""+langArray[i]+"\" class=\"multilang\">"+((element.name == "questiontext" && element.src == "") ? this.langLong[i] : element.src)+"</span>"+((element.name == "questiontext") ? "" : "");
		element[langArray[i]] = element.src;		// .. add the content to the lang span also
		break;
	case "src":
		// do nothing
		break;
	default:
			var tmp = "<span lang=\""+langArray[i]+"\" class=\"multilang\">"+((element.name == "questiontext") ? this.langLong[i] : "")+"</span>"+((element.name == "questiontext") ? "" : "");	// create the default lang span
			brandNewContent += tmp;			// add it to the src
			element[langArray[i]] = "";		// and to the lang var
		break;
				}
			}
			element.src = brandNewContent;					// assign the whole content to the src
			element.value = brandNewContent;				// and the screen ;)				
		}
		else {					// incase we did find langspans
			if(element.name == "questiontext"){
				brandNewContent = this.trim(rewritenContent);
			}
			else {
				/* reworked to switch to divs instead of langs */
				for( t=0; t<tempContent.childNodes.length; t++ ) {
				

				if(tempContent.childNodes[ t ].getAttribute){
	var lang = tempContent.childNodes[ t ].getAttribute( 'lang' ); 		//save the lang temporary
	var langContent = tempContent.childNodes[ t ].innerHTML; 			// save the language specific content temporarily
	element[lang] = langContent;			//set the languageContent into the lang variable of the element
	}
				}
			}
		}
		
		this.removeTempContainer( tempContent );			// kill the temporary div
		return ((returnstring) ? brandNewContent : langArray);
	},
	
	
	buildContent: function( flag ) {
		if(document.getElementById('formTemp')){	// check if the formTemp div still exists
			var formTemp = document.getElementById('formTemp');	// and reuse it if it's still there
		}
		else {
			var formTemp = this.addTempContainer( 'formTemp' );				// create temporary div to juggle the content
		}
		
		var formAreas = document.getElementsByTagName( this.sInputReplace );	// get all the textareas
		
		// loop through the textareas
		for ( i=0; i<formAreas.length; i++ ) {
			if(this.isExcluded(formAreas[i].name)){
			}
			else{
				var textarea = formAreas[ i ];			// set the current textarea to a var
				var newContent = "";					// define a new empty content var
				
				if(textarea.name == "id_questiontext"){
	var txt = this.linebreakRemove(edit_questiontext.getHTML());
	var tmptxt = this.trim(txt);
	textarea.value = this.html_entity_decode(tmptxt);
	/*
	if(flag){
		var rewritenContent = this.str_replace("<span lang", "<lang lang",textarea.value);
		rewritenContent = this.str_replace("/span>", "/lang>",rewritenContent);
		textarea.value = rewritenContent;
	}*/
	
				}
				else {
	formTemp.innerHTML = textarea.src;	// set the current textarea content into the div so we can access through DOM
	
	var noContent = 0;
  var langs = new Array();
  var c = 0;

	for( j=0; j<formTemp.childNodes.length; j++ ) {			//loop through the childNodes of the content
	

	
	if(formTemp.childNodes[ j ].getAttribute){
	
		var lang = formTemp.childNodes[ j ].getAttribute( 'lang' ); 	// get the lang and save it temporary
			langs[c] = lang;
			c++;
		
		if(flag && (textarea[ lang ] == "" || textarea[ lang ] == " ")){
			noContent++;
			textarea[ lang ] = " ";
		}
		newContent += "<span lang=\"" + lang + "\" class=\"multilang\">" + textarea[ lang ] + "</span>";	// generate the content			

		}
	}
	
	// are all langs available?
	if(this.requiredLangs.length-1 != langs.length){
    var c = 0;
    var missingLangs = new Array();
    
      for( j=0; j<this.requiredLangs.length; j++ ) {	
        var l = this.requiredLangs[j];
        if(l != "src"){
         if(!this.contains(l, langs) && l != ""){
          missingLangs[c] = l;
         } 
          
        }
      }
      
       for( j=0; j<missingLangs.length; j++ ) {	
        newContent += '<span lang="' + missingLangs[j] + '" class="multilang> </span>"'
       }
	
	}
	
	
 
				
				}
				newContent = this.linebreakRemove(newContent);		// check for possible linebreaks and remove them to not break the spans
				textarea.src = newContent;				// assign the new content to the old src var
				
				if(flag && textarea.name != "questiontext") {		// if flag is true paste the newcontent to the textarea ..
	var langAmnt = (this.editSource) ? this.requiredLangs.length-1 : this.requiredLangs.length;
	textarea.value = (noContent < langAmnt) ? this.html_entity_decode(newContent) : "";
	
    /*
	var rewritenContent = this.str_replace("<span lang", "<lang lang",textarea.value);
	rewritenContent = this.str_replace("/span>", "/lang>",rewritenContent);
	textarea.value = rewritenContent;
    */
				}
			}
		}
		this.removeTempContainer( formTemp );		// kill the temporary div
		return true; 
	},
	
	isExcluded: function( element_name ) {			// check if an element is supposed to be 
		var bool = false;			// excluded from transformation
		for(key in this._excludes){
			if ( element_name == this._excludes[key] ) {
				return true;		// is excluded
				break;
			} 
		}
		
		return bool;				// transform it
	},
	
	setLanguage: function ( newlang ) {
		this._currentLanguage = newlang;
	},
	
	
	/*
	   Event Handlers
	---------------------------------------------------------------------------------
	*/
	proccessTextChange:function( e ) { 
		e[ e.currentLang ] = e.value;					// assign the new content to the lang variable of the element
	},
	
	
	setInputLang:function( id,lang ) {
		var e = document.getElementById( id );			// get the element
		if(e.currentLang == "src"){		// if the currentLang is the source
			if(lang != "src" && e[lang] != ""){
				this.parseContent( e );					// reparse the content
			}
		}
		
		if(this.buildContent()) {		// if the content has been updated continue
			e.currentLang = lang;		// switch the current language
			e.value = e[ lang ];		// assign the content to the element
		}
	},
	
	
	submitChanges:function() {
		
		if(this.buildContent(1)){					// if the content has been updated continue
			// document.forms[0].submit();				// and finally submit the form
		}
	},

		
	/*
	   HTML Building Blocks
	---------------------------------------------------------------------------------
	*/
	createBreak: function(){
		var breaker = document.createElement( "br" );	//create a break element
		this._parent.appendChild( breaker );			// add the element
	},
	

	createDiv: function( name ) {
		var newDiv = document.createElement( 'div' );			// create a new div element
		newDiv.id = name;			// give it an id
		return newDiv;				// return the object
	},
	

	removeOldElement: function ( element ) {
		element.parentNode.removeChild( element );				// kill the given element, DESTROY IT!
	},

	
	addTempContainer: function( name ) {
		var tContainer = this.createDiv( name );				// create a temporary div element
		tContainer.style.display = "none";		// set it to invisible, so that nobody can see it's content
		document.body.appendChild( tContainer );				// add it the the end of the body of the html
		return tContainer;			// return the object to whoever called it
	},

	
	removeTempContainer: function( e ) {
		document.body.removeChild( e );				// remove that damn div ;)
	},
	
	html_entity_encode: function ( texte ) {
		texte = texte.replace(/"/g,'&quot;'); // 34 22
		texte = texte.replace(/&/g,'&amp;'); // 38 26
		texte = texte.replace(/\'/g,'&#39;'); // 39 27
		texte = texte.replace(/</g,'&lt;'); // 60 3C
		texte = texte.replace(/>/g,'&gt;'); // 62 3E
		texte = texte.replace(/\^/g,'&circ;'); // 94 5E
		texte = texte.replace(//g,'&lsquo;'); // 145 91
		texte = texte.replace(//g,'&rsquo;'); // 146 92
		texte = texte.replace(//g,'&ldquo;'); // 147 93
		texte = texte.replace(//g,'&rdquo;'); // 148 94
		texte = texte.replace(//g,'&bull;'); // 149 95
		texte = texte.replace(//g,'&ndash;'); // 150 96
		texte = texte.replace(//g,'&mdash;'); // 151 97
		texte = texte.replace(//g,'&tilde;'); // 152 98
		texte = texte.replace(//g,'&trade;'); // 153 99
		texte = texte.replace(//g,'&scaron;'); // 154 9A
		texte = texte.replace(//g,'&rsaquo;'); // 155 9B
		texte = texte.replace(//g,'&oelig;'); // 156 9C
		texte = texte.replace(//g,'&#357;'); // 157 9D
		texte = texte.replace(//g,'&#382;'); // 158 9E
		texte = texte.replace(//g,'&Yuml;'); // 159 9F
		texte = texte.replace(/ /g,'&nbsp;'); // 160 A0
		texte = texte.replace(//g,'&iexcl;'); // 161 A1
		texte = texte.replace(//g,'&cent;'); // 162 A2
		texte = texte.replace(//g,'&pound;'); // 163 A3
		texte = texte.replace(/ /g,'&curren;'); // 164 A4
		texte = texte.replace(//g,'&yen;'); // 165 A5
		texte = texte.replace(//g,'&brvbar;'); // 166 A6
		texte = texte.replace(//g,'&sect;'); // 167 A7
		texte = texte.replace(//g,'&uml;'); // 168 A8
		texte = texte.replace(//g,'&copy;'); // 169 A9
		texte = texte.replace(//g,'&ordf;'); // 170 AA
		texte = texte.replace(//g,'&laquo;'); // 171 AB
		texte = texte.replace(//g,'&not;'); // 172 AC
		texte = texte.replace(//g,'&shy;'); // 173 AD
		texte = texte.replace(//g,'&reg;'); // 174 AE
		texte = texte.replace(//g,'&macr;'); // 175 AF
		texte = texte.replace(//g,'&deg;'); // 176 B0
		texte = texte.replace(//g,'&plusmn;'); // 177 B1
		texte = texte.replace(//g,'&sup2;'); // 178 B2
		texte = texte.replace(//g,'&sup3;'); // 179 B3
		texte = texte.replace(//g,'&acute;'); // 180 B4
		texte = texte.replace(//g,'&micro;'); // 181 B5
		texte = texte.replace(//g,'&para'); // 182 B6
		texte = texte.replace(//g,'&middot;'); // 183 B7
		texte = texte.replace(//g,'&cedil;'); // 184 B8
		texte = texte.replace(//g,'&sup1;'); // 185 B9
		texte = texte.replace(//g,'&ordm;'); // 186 BA
		texte = texte.replace(//g,'&raquo;'); // 187 BB
		texte = texte.replace(//g,'&frac14;'); // 188 BC
		texte = texte.replace(//g,'&frac12;'); // 189 BD
		texte = texte.replace(//g,'&frac34;'); // 190 BE
		texte = texte.replace(//g,'&iquest;'); // 191 BF
		texte = texte.replace(//g,'&Agrave;'); // 192 C0
		texte = texte.replace(//g,'&Aacute;'); // 193 C1
		texte = texte.replace(//g,'&Acirc;'); // 194 C2
		texte = texte.replace(//g,'&Atilde;'); // 195 C3
		texte = texte.replace(//g,'&Auml;'); // 196 C4
		texte = texte.replace(//g,'&Aring;'); // 197 C5
		texte = texte.replace(//g,'&AElig;'); // 198 C6
		texte = texte.replace(//g,'&Ccedil;'); // 199 C7
		texte = texte.replace(//g,'&Egrave;'); // 200 C8
		texte = texte.replace(//g,'&Eacute;'); // 201 C9
		texte = texte.replace(//g,'&Ecirc;'); // 202 CA
		texte = texte.replace(//g,'&Euml;'); // 203 CB
		texte = texte.replace(//g,'&Igrave;'); // 204 CC
		texte = texte.replace(//g,'&Iacute;'); // 205 CD
		texte = texte.replace(//g,'&Icirc;'); // 206 CE
		texte = texte.replace(//g,'&Iuml;'); // 207 CF
		texte = texte.replace(//g,'&ETH;'); // 208 D0
		texte = texte.replace(//g,'&Ntilde;'); // 209 D1
		texte = texte.replace(//g,'&Ograve;'); // 210 D2
		texte = texte.replace(//g,'&Oacute;'); // 211 D3
		texte = texte.replace(//g,'&Ocirc;'); // 212 D4
		texte = texte.replace(//g,'&Otilde;'); // 213 D5
		texte = texte.replace(//g,'&Ouml;'); // 214 D6
		texte = texte.replace(//g,'&times;'); // 215 D7
		texte = texte.replace(//g,'&Oslash;'); // 216 D8
		texte = texte.replace(//g,'&Ugrave;'); // 217 D9
		texte = texte.replace(//g,'&Uacute;'); // 218 DA
		texte = texte.replace(//g,'&Ucirc;'); // 219 DB
		texte = texte.replace(//g,'&Uuml;'); // 220 DC
		texte = texte.replace(//g,'&Yacute;'); // 221 DD
		texte = texte.replace(//g,'&THORN;'); // 222 DE
		texte = texte.replace(//g,'&szlig;'); // 223 DF
		texte = texte.replace(//g,'&agrave;'); // 224 E0
		texte = texte.replace(//g,'&aacute;'); // 225 E1
		texte = texte.replace(//g,'&acirc;'); // 226 E2
		texte = texte.replace(//g,'&atilde;'); // 227 E3
		texte = texte.replace(//g,'&auml;'); // 228 E4
		texte = texte.replace(//g,'&aring;'); // 229 E5
		texte = texte.replace(//g,'&aelig;'); // 230 E6
		texte = texte.replace(//g,'&ccedil;'); // 231 E7
		texte = texte.replace(//g,'&egrave;'); // 232 E8
		texte = texte.replace(//g,'&eacute;'); // 233 E9
		texte = texte.replace(//g,'&ecirc;'); // 234 EA
		texte = texte.replace(//g,'&euml;'); // 235 EB
		texte = texte.replace(//g,'&igrave;'); // 236 EC
		texte = texte.replace(//g,'&iacute;'); // 237 ED
		texte = texte.replace(//g,'&icirc;'); // 238 EE
		texte = texte.replace(//g,'&iuml;'); // 239 EF
		texte = texte.replace(//g,'&eth;'); // 240 F0
		texte = texte.replace(//g,'&ntilde;'); // 241 F1
		texte = texte.replace(//g,'&ograve;'); // 242 F2
		texte = texte.replace(//g,'&oacute;'); // 243 F3
		texte = texte.replace(//g,'&ocirc;'); // 244 F4
		texte = texte.replace(//g,'&otilde;'); // 245 F5
		texte = texte.replace(//g,'&ouml;'); // 246 F6
		texte = texte.replace(//g,'&divide;'); // 247 F7
		texte = texte.replace(//g,'&oslash;'); // 248 F8
		texte = texte.replace(//g,'&ugrave;'); // 249 F9
		texte = texte.replace(//g,'&uacute;'); // 250 FA
		texte = texte.replace(//g,'&ucirc;'); // 251 FB
		texte = texte.replace(//g,'&uuml;'); // 252 FC
		texte = texte.replace(//g,'&yacute;'); // 253 FD
		texte = texte.replace(//g,'&thorn;'); // 254 FE
		texte = texte.replace(//g,'&yuml;'); // 255 FF
		return texte;
	},

	html_entity_decode: function( texte ) {
		texte = texte.replace(/&quot;/g,'"'); // 34 22
		texte = texte.replace(/&amp;/g,'&'); // 38 26	
		texte = texte.replace(/&#39;/g,"'"); // 39 27
		texte = texte.replace(/&lt;/g,'<'); // 60 3C
		texte = texte.replace(/&gt;/g,'>'); // 62 3E
		texte = texte.replace(/&circ;/g,'^'); // 94 5E
		texte = texte.replace(/&lsquo;/g,''); // 145 91
		texte = texte.replace(/&rsquo;/g,''); // 146 92
		texte = texte.replace(/&ldquo;/g,''); // 147 93
		texte = texte.replace(/&rdquo;/g,''); // 148 94
		texte = texte.replace(/&bull;/g,''); // 149 95
		texte = texte.replace(/&ndash;/g,''); // 150 96
		texte = texte.replace(/&mdash;/g,''); // 151 97
		texte = texte.replace(/&tilde;/g,''); // 152 98
		texte = texte.replace(/&trade;/g,''); // 153 99
		texte = texte.replace(/&scaron;/g,''); // 154 9A
		texte = texte.replace(/&rsaquo;/g,''); // 155 9B
		texte = texte.replace(/&oelig;/g,''); // 156 9C
		texte = texte.replace(/&#357;/g,''); // 157 9D
		texte = texte.replace(/&#382;/g,''); // 158 9E
		texte = texte.replace(/&Yuml;/g,''); // 159 9F
		texte = texte.replace(/&nbsp;/g,' '); // 160 A0
		texte = texte.replace(/&iexcl;/g,''); // 161 A1
		texte = texte.replace(/&cent;/g,''); // 162 A2
		texte = texte.replace(/&pound;/g,''); // 163 A3
		texte = texte.replace(/&curren;/g,' '); // 164 A4
		texte = texte.replace(/&yen;/g,''); // 165 A5
		texte = texte.replace(/&brvbar;/g,''); // 166 A6
		texte = texte.replace(/&sect;/g,''); // 167 A7
		texte = texte.replace(/&uml;/g,''); // 168 A8
		texte = texte.replace(/&copy;/g,''); // 169 A9
		texte = texte.replace(/&ordf;/g,''); // 170 AA
		texte = texte.replace(/&laquo;/g,''); // 171 AB
		texte = texte.replace(/&not;/g,''); // 172 AC
		texte = texte.replace(/&shy;/g,''); // 173 AD
		texte = texte.replace(/&reg;/g,''); // 174 AE
		texte = texte.replace(/&macr;/g,''); // 175 AF
		texte = texte.replace(/&deg;/g,''); // 176 B0
		texte = texte.replace(/&plusmn;/g,''); // 177 B1
		texte = texte.replace(/&sup2;/g,''); // 178 B2
		texte = texte.replace(/&sup3;/g,''); // 179 B3
		texte = texte.replace(/&acute;/g,''); // 180 B4
		texte = texte.replace(/&micro;/g,''); // 181 B5
		texte = texte.replace(/&para/g,''); // 182 B6
		texte = texte.replace(/&middot;/g,''); // 183 B7
		texte = texte.replace(/&cedil;/g,''); // 184 B8
		texte = texte.replace(/&sup1;/g,''); // 185 B9
		texte = texte.replace(/&ordm;/g,''); // 186 BA
		texte = texte.replace(/&raquo;/g,''); // 187 BB
		texte = texte.replace(/&frac14;/g,''); // 188 BC
		texte = texte.replace(/&frac12;/g,''); // 189 BD
		texte = texte.replace(/&frac34;/g,''); // 190 BE
		texte = texte.replace(/&iquest;/g,''); // 191 BF
		texte = texte.replace(/&Agrave;/g,''); // 192 C0
		texte = texte.replace(/&Aacute;/g,''); // 193 C1
		texte = texte.replace(/&Acirc;/g,''); // 194 C2
		texte = texte.replace(/&Atilde;/g,''); // 195 C3
		texte = texte.replace(/&Auml;/g,''); // 196 C4
		texte = texte.replace(/&Aring;/g,''); // 197 C5
		texte = texte.replace(/&AElig;/g,''); // 198 C6
		texte = texte.replace(/&Ccedil;/g,''); // 199 C7
		texte = texte.replace(/&Egrave;/g,''); // 200 C8
		texte = texte.replace(/&Eacute;/g,''); // 201 C9
		texte = texte.replace(/&Ecirc;/g,''); // 202 CA
		texte = texte.replace(/&Euml;/g,''); // 203 CB
		texte = texte.replace(/&Igrave;/g,''); // 204 CC
		texte = texte.replace(/&Iacute;/g,''); // 205 CD
		texte = texte.replace(/&Icirc;/g,''); // 206 CE
		texte = texte.replace(/&Iuml;/g,''); // 207 CF
		texte = texte.replace(/&ETH;/g,''); // 208 D0
		texte = texte.replace(/&Ntilde;/g,''); // 209 D1
		texte = texte.replace(/&Ograve;/g,''); // 210 D2
		texte = texte.replace(/&Oacute;/g,''); // 211 D3
		texte = texte.replace(/&Ocirc;/g,''); // 212 D4
		texte = texte.replace(/&Otilde;/g,''); // 213 D5
		texte = texte.replace(/&Ouml;/g,''); // 214 D6
		texte = texte.replace(/&times;/g,''); // 215 D7
		texte = texte.replace(/&Oslash;/g,''); // 216 D8
		texte = texte.replace(/&Ugrave;/g,''); // 217 D9
		texte = texte.replace(/&Uacute;/g,''); // 218 DA
		texte = texte.replace(/&Ucirc;/g,''); // 219 DB
		texte = texte.replace(/&Uuml;/g,''); // 220 DC
		texte = texte.replace(/&Yacute;/g,''); // 221 DD
		texte = texte.replace(/&THORN;/g,''); // 222 DE
		texte = texte.replace(/&szlig;/g,''); // 223 DF
		texte = texte.replace(/&agrave;/g,''); // 224 E0
		texte = texte.replace(/&aacute;/g,''); // 225 E1
		texte = texte.replace(/&acirc;/g,''); // 226 E2
		texte = texte.replace(/&atilde;/g,''); // 227 E3
		texte = texte.replace(/&auml;/g,''); // 228 E4
		texte = texte.replace(/&aring;/g,''); // 229 E5
		texte = texte.replace(/&aelig;/g,''); // 230 E6
		texte = texte.replace(/&ccedil;/g,''); // 231 E7
		texte = texte.replace(/&egrave;/g,''); // 232 E8
		texte = texte.replace(/&eacute;/g,''); // 233 E9
		texte = texte.replace(/&ecirc;/g,''); // 234 EA
		texte = texte.replace(/&euml;/g,''); // 235 EB
		texte = texte.replace(/&igrave;/g,''); // 236 EC
		texte = texte.replace(/&iacute;/g,''); // 237 ED
		texte = texte.replace(/&icirc;/g,''); // 238 EE
		texte = texte.replace(/&iuml;/g,''); // 239 EF
		texte = texte.replace(/&eth;/g,''); // 240 F0
		texte = texte.replace(/&ntilde;/g,''); // 241 F1
		texte = texte.replace(/&ograve;/g,''); // 242 F2
		texte = texte.replace(/&oacute;/g,''); // 243 F3
		texte = texte.replace(/&ocirc;/g,''); // 244 F4
		texte = texte.replace(/&otilde;/g,''); // 245 F5
		texte = texte.replace(/&ouml;/g,''); // 246 F6
		texte = texte.replace(/&divide;/g,''); // 247 F7
		texte = texte.replace(/&oslash;/g,''); // 248 F8
		texte = texte.replace(/&ugrave;/g,''); // 249 F9
		texte = texte.replace(/&uacute;/g,''); // 250 FA
		texte = texte.replace(/&ucirc;/g,''); // 251 FB
		texte = texte.replace(/&uuml;/g,''); // 252 FC
		texte = texte.replace(/&yacute;/g,''); // 253 FD
		texte = texte.replace(/&thorn;/g,''); // 254 FE
		texte = texte.replace(/&yuml;/g,''); // 255 FF
		return texte;
	},
	
	/*
	   String Functions
	---------------------------------------------------------------------------------
	*/
	
	trim: function( str, chars ) {
		return this.ltrim(this.rtrim(str, chars), chars);
	},

	ltrim: function( str, chars ) {
		chars = chars || "\\s";
		return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
	},

	rtrim: function( str, chars ) {
		chars = chars || "\\s";
		return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	},
	
	linebreakRemove: function ( str ) {
		return str.replace(/\r\n|\r|\n/g, ' ');
	},
	
	str_replace: function ( search_for , replace_with, str ) {
		var index = str.indexOf( search_for );
 
		while (index != -1){
			// Relace out the current instance.
			str = str.replace( search_for, replace_with );
			 
			// Get the index of any next matching substring.
			index = str.indexOf( search_for );
		}
		 
		return str;
	},
	

contains: function (element, array)
{
    for (var i = 0; i < array.length; ++i) {
        if (array[i] == element) {
            return true;
        }
    }
    return false;
}

};

