	var selection
	var range
	var selectedImage

	var Mode = "WYSIWYG";
	var toggle = "off"
	var borderShown = "no"
	var justSwitched = false
	var loaded = false
	var colorMode
	var contMenu
    var popupWindow
	
	window.onload = doLoad;
	window.onerror = stopError;

	function stopError() {
		return true;
	}

	function initEditor() {
		var iframes = document.all.tags("IFRAME");
		el = iframes[0];
		
		el.frameWindow = document.frames[el.id];

		el.frameWindow.document.oncontextmenu = function () {
			if (!el.frameWindow.event.ctrlKey){
				showContextMenu(el.frameWindow.event)
				return false;
			}
		}

		el.frameWindow.document.onerror = function () {
			return true;
		}

		el.frameWindow.document.oncontrolselect = function () {
			if(el.frameWindow.event.srcElement.title == "Protect")
			{
				alert("Element protégé du template ! Vous ne pouvez le modifer ou le supprimer !");
				return false;
			}
		}

		el.frameWindow.document.onselectionchange = function () {
			refreshTextFormat();
		}

		el.frameWindow.document.onkeydown = function () {
			
			// 46 = Suppr | 8 = Del
			//if(el.frameWindow.event.keyCode == 46 | el.frameWindow.event.keyCode == 8) {
				if (editor.document.selection.type != "Control") {
					var sel = editor.document.selection.createRange();
					var rng = sel.parentElement();
					if(rng.title == "Protect"){
						alert("Element protégé du template ! Vous ne pouvez le modifer ou le supprimer !");
						return false;}
				}
			//}
			if(el.frameWindow.event.ctrlKey) {
				 // 83 = S
				 if(el.frameWindow.event.keyCode == 83) {
					saveFile();
				  return false;
				// 75 = K
				} else if(el.frameWindow.event.keyCode == 75) {
			      cmdLink()
				  return false;
				// 87 = W
				} else if (el.frameWindow.event.keyCode == 87) {
					saveFileAs();
				  return false;
				// 9 = Tab
				} else if (el.frameWindow.event.keyCode == 9) {
				  switchMode();
				  return false;
				// 77 = M
				} else if (el.frameWindow.event.keyCode == 77) {
				  cmdImage();
				  return false;
				// 84 = T
				} else if (el.frameWindow.event.keyCode == 84) {
				  insertTable();
				  return false;
				// 88 = X
				} else if (el.frameWindow.event.keyCode == 88) {
					var loc = location.href;
					loc = loc.substring(0,location.href.lastIndexOf('?'));
					namer = location.href
					var file = namer.substring(namer.lastIndexOf('=')+1);
					var folder = file.substring(0,file.lastIndexOf('/'));
					if (folder != '') loc += '?folder=' + folder + '/';
					document.location = loc;
				  return false;
				// 123 = F12
				} else if (el.frameWindow.event.keyCode == 123) {
					previewFile();
				  return false;
				// 76 = L
				} else if (el.frameWindow.event.keyCode == 76) {
				  execCmd("JustifyLeft");
				  return false;
				// 69 = E
				} else if (el.frameWindow.event.keyCode == 69) {
				  execCmd("JustifyCenter");
				  return false;
				// 82 = R
				} else if (el.frameWindow.event.keyCode == 82) {
				  execCmd("JustifyRight");
				  return false;
				// 74 = J
				} else if (el.frameWindow.event.keyCode == 74) {
				  execCmd("JustifyFull");
				  return false;
				}

			}
		}
	}

	function switchMode () {
		 if (Mode == "WYSIWYG") {
			if (borderShown == "yes") {
				toggleBorders();
				toggle = "off";
				toggleWasOn = "yes";
			} else {
				toggleWasOn = "no";
			}
			toolbar.style.display = "none";
	
			fontFamily = editor.document.body.style.fontFamily;
			fontSize = editor.document.body.style.fontSize;
			text = editor.document.body.text;
			bgColor = editor.document.body.bgcolor;
			background = editor.document.body.background;
			
			code = editor.document.documentElement.outerHTML;
	
			re = /&amp;/g;
			code = code.replace(re,'&');
	
			editor.document.body.innerText = code;
			editor.document.body.innerHTML = colourCode(editor.document.body.innerHTML);
	
			editor.document.body.style.background = "";
			editor.document.body.style.color = "#000000";
			editor.document.body.style.textAlign = "Left";
			editor.document.body.style.fontFamily = "Courier"
			editor.document.body.style.fontSize = "10pt"
			editor.document.body.bgColor = '#FFFFFF';
			editor.document.body.text = '#000000';
			editor.document.body.background = '';
			///fontChange = true;
			
			Mode = "CODE";
			editor.focus();
		} else {
	
			toolbar.style.display = "inline";
			loaded = false;
			justSwitched = true;
			editor.document.write(editor.document.body.innerText);
			editor.document.close();
	
			Mode = "WYSIWYG";
	
			if (toggleWasOn == "yes") {
				toggleBorders();
				toggle = "on";
				toggleWasOn = "no";
			}
		}
		initEditor();
		showInfo();
		editor.focus();
	}
	
	function showInfo() {
		info.innerText = "Partie Editée : " + document.saveForm.hiddenFileName.value + " | Mode : " + Mode + " | Grille : ";
		if (borderShown == "yes") info.innerText += "ON"
		else info.innerText += "OFF"
	}
	
	function saveFile() {
		val = window.confirm("Voulez vous enregistrer cette newsletter ?");
		if (val == false)
			{return false;}			
		else
		{	
			if (Mode == "CODE") { 
				switchMode ();
			}
			document.saveForm.action.value = "save";
			document.saveForm.hiddenField.value = editor.document.body.innerText;
			document.saveForm.hiddenFieldHTML.value = editor.document.body.innerText;
			document.saveForm.submit();
		}
		
	}
	function saveFileAs() {
		if (Mode == "CODE") { 
			document.saveForm.hiddenField.value = editor.document.body.innerText;
		}
		else {
			document.saveForm.hiddenField.value = editor.document.documentElement.outerHTML;
		}
		document.saveForm.cmd.value = "saveAs";
		document.saveForm.submit(); 
	}
	
	function previewFile() {
		if (popupWindow != null) popupWindow.close();
		var leftPos = (screen.availWidth-500) / 2 ;
		var topPos = (screen.availHeight-400) / 2 ;
		popupWindow = window.open('','','width=700,height=500,scrollbars=yes,resizable=no,titlebar=0,top=' + topPos + ',left=' + leftPos);
		if (Mode == "CODE")
			popupWindow.document.write(editor.document.body.innerText);
		else
			popupWindow.document.write(editor.document.documentElement.outerHTML);
		popupWindow.document.close();
		popupWindow.focus();
	}
	
	function contextHilite(menu){
	    menu.runtimeStyle.backgroundColor = "Highlight";
	    if (menu.state){
	        menu.runtimeStyle.color = "GrayText";
	    } else {
	        menu.runtimeStyle.color = "HighlightText";
	    }
	}

	function contextDelite(menu){
	    menu.runtimeStyle.backgroundColor = "";
	    menu.runtimeStyle.color = "";
	}

	var clasMenu = window.createPopup();
	function showMenu(menu, width, height)
	{
    
		var lefter = event.clientX;
		var leftoff = event.offsetX
		var topper = event.clientY;
		var topoff = event.offsetY;
		var moveMe = 0
	
		if (menu == "colorMenu") {
			colorType = "0"
			colorMode =  'ForeColor'
		}
	
		if (menu == "colorMenu2") {
			colorType = "2"
			menu = "colorMenu"
			colorMode =  'BackColor'
		}
	
		var HTMLContent = eval(menu).innerHTML
		clasMenu.document.body.innerHTML = HTMLContent
		clasMenu.show(lefter - leftoff - 2 - moveMe, topper - topoff + 22, width, height, document.body);
		return false;
	}

	contMenu = window.createPopup();
	function showContextMenu(event)
	{
		var width = "200";
		var height = "61";
		var lefter = event.clientX;
		var topper = event.clientY;
		var contMenuBody = contMenu.document.body;
		height = parseInt(height);
		var HTMLContent = "<table style='BORDER-LEFT: threedface 1px solid; BORDER-TOP: threedface 1px solid; BORDER-RIGHT: #404040 1px solid; BORDER-BOTTOM: #404040 1px solid;' cellpadding=0 cellspacing=0><tr><td>";
		HTMLContent = HTMLContent + contextMenu.innerHTML;
		if (isTableSelected() || isCursorInTableCell()) {
			HTMLContent = HTMLContent + cmTableMenu.innerHTML;
			height = height + 21;
		}
		if (isCursorInTableCell()) {
			HTMLContent = HTMLContent + cmTableFunctions.innerHTML;
			height = height + 221;
		}
		if(isCursorInForm()) {
			HTMLContent = HTMLContent + cmFormMenu.innerHTML;
			height = height + 21;
		}
		if(isImageSelected()) {
			HTMLContent = HTMLContent + cmImageMenu.innerHTML;
			height = height + 42;
		}
		if(isLinkSelected()) {
			HTMLContent = HTMLContent + cmLinkMenu.innerHTML;
			height = height + 21;
		}
		HTMLContent = HTMLContent + "</td></tr></table>"
		contMenuBody.innerHTML = HTMLContent
		contMenu.show(lefter + 2,topper + 2, width, height, editor.document.body)
	}


	function cmdColor(color) {
		editor.document.execCommand(colorMode,false, color.innerHTML);
	}

	function showColor(cell,color) {
		cell.innerHTML = color.style.backgroundColor.toUpperCase();
		cell.style.backgroundColor = color.style.backgroundColor;
	}

	function doLoad() {
		editor.document.designMode = 'On';
		editor.document.focus();
		editor.document.oncontextmenu = function () {
			if (!editor.event.ctrlKey){
				showContextMenu(editor.event);
				return false;
			}
		}
	}

	function editorLoad() {
		if (!justSwitched) {
			toggleBorders();
			displayUserStyles();
			initEditor();
			loaded = true
		}
	}

	function execCmd(cmd) {
		editor.document.execCommand(cmd)
	}

	function cmdFont(newFont) {
		if (isTextSelected())
			editor.document.execCommand('FontName',false,newFont);
		editor.focus();
	}

	function cmdSize(newSize) {
		if (isTextSelected())
			editor.document.execCommand('FontSize',false,newSize);
		editor.focus();
	}
	
	function cmdFormat(newFormat) {
		if (isTextSelected())
			editor.document.execCommand('FormatBlock',false,newFormat);
		editor.focus();
	}

	function isTextSelected() {
		return (editor.document.selection.type == "Text");
	}

	function setHeight() {
		height = 102;
		if (Mode == "WYSIWYG") { 
			height = 71;
		}
		return (document.body.clientHeight-height);
	}

	function toggleBorders() {
		var allForms = editor.document.body.getElementsByTagName("FORM");
		var allInputs = editor.document.body.getElementsByTagName("INPUT");
		var allTables = editor.document.body.getElementsByTagName("TABLE");
		var allLinks = editor.document.body.getElementsByTagName("A");
		
		if (borderShown == "no") {
			toggle = "off" 
		} else { 
			toggle = "on" 
		}

		for (a=0; a < allForms.length; a++) {
			if (toggle == "off") {
				allForms[a].style.border = "1px dotted #FF0000"
			} else {
				allForms[a].removeAttribute("style")
			}
		}

		for (b=0; b < allInputs.length; b++) {
			if (toggle == "off") {
				if (allInputs[b].type.toUpperCase() == "HIDDEN") {
					allInputs[b].style.border = "1px dashed #000000"
					allInputs[b].style.width = "15px"
					allInputs[b].style.height = "15px"
					allInputs[b].style.backgroundColor = "#FDADAD"
					allInputs[b].style.color = "#FDADAD"
				}
			} else {
				if (allInputs[b].type.toUpperCase() == "HIDDEN")
					allInputs[b].removeAttribute("style")
			}
		}

		for (i=0; i < allTables.length; i++) {
				if (toggle == "off") {
					allTables[i].style.border = "1px dashed #BFBFBF"
				} else {
					allTables[i].removeAttribute("style")
				}
				allRows = allTables[i].rows
				for (y=0; y < allRows.length; y++) {
				 	allCellsInRow = allRows[y].cells
						for (x=0; x < allCellsInRow.length; x++) {
							if (toggle == "off") {
								allCellsInRow[x].style.border = "1px dashed #BFBFBF"
							} else {
								allCellsInRow[x].removeAttribute("style")
							}
						}
				}
		}

		for (a=0; a < allLinks.length; a++) {
			if (toggle == "off") {
				if (allLinks[a].href.toUpperCase() == "") {
					allLinks[a].style.border = "1px dashed #000000";
					allLinks[a].style.width = "20px";
					allLinks[a].style.height = "16px";
					allLinks[a].style.backgroundColor = "#FFFFCC";
					allLinks[a].style.color = "#FFFFCC";
				}
			} else {
				allLinks[a].removeAttribute("style");
			}
		}

		if (toggle == "off") {
			borderShown = "yes";
		} else {
			borderShown = "no";
		}

		editor.document.body.innerHTML = editor.document.body.innerHTML;
		editor.scrollBy(0,0);
	}

	function isImageSelected() {
		if (editor.document.selection.type == "Control") {
			var oControlRange = editor.document.selection.createRange();
			if (oControlRange(0).tagName.toUpperCase() == "IMG") {
				selectedImage = editor.document.selection.createRange()(0);
				return true;
			}	
		}
	}

	function isLinkSelected() {
		if (editor.document.selection.type == "Control") {
			var oControlRange = editor.document.selection.createRange();
			if (oControlRange(0).tagName.toUpperCase() == "IMG") {
				var oSel = oControlRange(0).parentNode;
			} else {
				return false;
			}
		} else {
			oSel = editor.document.selection.createRange().parentElement();
		}

		if (oSel.tagName.toUpperCase() == "A")
		{
			myHref = oSel.getAttribute("href",2)
			if (myHref != "")
			{
				return true;
			}
		}
		return false;
	}

	function isTableSelected() {
		if (editor.document.selection.type == "Control") {
			var oControlRange = editor.document.selection.createRange();
			if (oControlRange(0).tagName.toUpperCase() == "TABLE") {
				selectedTable = editor.document.selection.createRange()(0);
				return true;
			}	
		}
	}

	function isCursorInTableCell() {
		if (document.selection.type != "Control") {
                          var elem = document.selection.createRange().parentElement()
                          while (elem.tagName.toUpperCase() != "TD" && elem.tagName.toUpperCase() != "TH")
                          {
                            elem = elem.parentElement
                            if (elem == null)
                              break
                          }
				if (elem) {
					selectedTD = elem
					selectedTR = selectedTD.parentElement
					selectedTBODY =  selectedTR.parentElement
					selectedTable = selectedTBODY.parentElement
					return true
				}
		}
	}

	function isCursorInForm() {
		if (document.selection.type != "Control") {
                          var elem = document.selection.createRange().parentElement()
                          while (elem.tagName != "FORM")
                          {
                            elem = elem.parentElement
                            if (elem == null)
                              break
                          }
				if (elem) {
					selectedForm = elem
					return true
				}
		}
	}

	function InsertParagraph() {
		if (document.selection.type != "Control") {
			var sel = document.selection.createRange()
			sel.pasteHTML("<p>")
			editor.focus()
		}
	}	


	var toggleWasOn
	var fontChange

	function cmdImage(loc) {
		if (isImageSelected()) {	 
			if (popupWindow != null) popupWindow.close();
			var leftPos = (screen.availWidth-500) / 2
			var topPos = (screen.availHeight-400) / 2 
			popupWindow = window.open('includes/actions/modifyImage.php','','width=350,height=250,scrollbars=no,resizable=no,titlebar=0,top=' + topPos + ',left=' + leftPos);
		} else {
			if (popupWindow != null) popupWindow.close();
			var leftPos = (screen.availWidth-500) / 2
			var topPos = (screen.availHeight-400) / 2 
			popupWindow = window.open('includes/actions/gest-photos.php','','width=480,height=350,scrollbars=yes,resizable=no,titlebar=0,top=' + topPos + ',left=' + leftPos);
		}
	}
	
	function cmdImageSrc(loc) {
		if (popupWindow != null) popupWindow.close();
		var leftPos = (screen.availWidth-500) / 2
		var topPos = (screen.availHeight-400) / 2
		popupWindow = window.open('gest-photos.php?src=','width=480,height=350,scrollbars=yes,resizable=no,titlebar=0,top=' + topPos + ',left=' + leftPos);
	}

	function selectImage(image) {
		document.execCommand("InsertImage",false,image);
	}

	function setBackgd(image) {
		editor.document.body.background = image
	}

	function ModifyProperties() {
 		if (popupWindow!=null) popupWindow.close();		
		var leftPos = (screen.availWidth-500) / 2
		var topPos = (screen.availHeight-400) / 2 
		popupWindow = window.open('includes/actions/modifyPageProperties.php','','width=450,height=420,scrollbars=no,resizable=no,titlebar=0,top=' + topPos + ',left=' + leftPos);
	}


	function cmdLink() {
		selection = editor.document.selection;
		range = selection.createRange();
		if (isSelection()) {
			if (popupWindow != null) popupWindow.close();
			var leftPos = (screen.availWidth-500) / 2
			var topPos = (screen.availHeight-400) / 2 
			popupWindow = window.open('includes/actions/insertLink.php','','width=350,height=200,scrollbars=no,resizable=no,titlebar=0,top=' + topPos + ',left=' + leftPos);
		}
	}

	function cmdAnchor() {
		selection = editor.document.selection;
		range = selection.createRange();
		if ((selection.type == "Control") && (selection.createRange()(0).tagName == "A") && (selection.createRange()(0).href == "")) {
			if (popupWindow != null) popupWindow.close();
			var leftPos = (screen.availWidth-500) / 2
			var topPos = (screen.availHeight-400) / 2 
			popupWindow = window.open('includes/actions/insertAnchor.php','','width=290,height=190,scrollbars=no,resizable=no,titlebar=0,top=' + topPos + ',left=' + leftPos);
		} else {
			if (popupWindow != null) popupWindow.close();
			var leftPos = (screen.availWidth-500) / 2
			var topPos = (screen.availHeight-400) / 2 
			popupWindow = window.open('includes/actions/insertAnchor.php','','width=290,height=190,scrollbars=no,resizable=no,titlebar=0,top=' + topPos + ',left=' + leftPos);
		}
	}

	function cmdTemplate(template)
	{
		//alert(editor.document.location.href);
		editor.document.location.href = "Templates/" + template;
	}
	
	function isEmailLink() {
		if (editor.document.selection.type == "Control") {
			var oControlRange = editor.document.selection.createRange();
			if (oControlRange(0).tagName.toUpperCase() == "IMG") {
				var oSel = oControlRange(0).parentNode;
			} else {
				return false;
			}
		} else {
			oSel = editor.document.selection.createRange().parentElement();
		}

		if (oSel.tagName.toUpperCase() == "A")
		{
			myHref = oSel.getAttribute("href",2)

			if (myHref.indexOf('mailto:') >- 1)
			{
				return true;
			}
		}
		return false;
	}

	function cmdEmail() {
		if (isTextSelected()) { 
			if (popupWindow != null) popupWindow.close();
			var leftPos = (screen.availWidth-500) / 2
			var topPos = (screen.availHeight-400) / 2 
			selection = editor.document.selection;
			range = selection.createRange();
			popupWindow = window.open('includes/actions/insertEmail.php','','width=350,height=190,scrollbars=no,resizable=no,titlebar=0,top=' + topPos + ',left=' + leftPos);
		}
	}

	function isSelection() {
		return ((editor.document.selection.type == "Text") || (editor.document.selection.type == "Control"));
	}	

	function cmdCopyDirect(object1, object2, libelle, format)
	{
		var object = object2.getElementById(libelle);
                if(format == "Cap")
                {
                        object.innerText = object1.toUpperCase();
                }else{
                        object.innerText = object1;
                }
	}
	
	function InsertPoject(texte)
	{
		if (editor.document.selection.type == "Control") {
			var oControlRange = editor.document.selection.createRange();
			if (oControlRange(0).tagName.toUpperCase() == "TABLE") {
				selectedTable = editor.document.selection.createRange()(0);
				if(selectedTable.title == "Projet")
				{
					selectedTD = selectedTable.cells(2);
					selectedTD.innerHTML = texte;
				}else{
					alert("Veuillez sélectionner un objet projet !");
				}
			}else{
				alert("Veuillez sélectionner un objet projet !");
			}
		}else{
			alert("Veuillez sélectionner un objet projet !");
		}
	}
	
