var Active_Id=0;
FocusEdtOut ('');

Fonts = new Array ( 
	"0"																	, "[ f o n t s ]"		, 
	"andale mono,times"									, "Andale Mono"			, 
	"arial,helvetica,sans-serif"				, "Arial"						, 
	"arial black,avant garde"						, "Arial Black"			, 
	"book antiqua,palatino"							, "Book Antiqua"		, 
	"comic sans ms,sand"								, "Comic Sans MS"		, 
	"courier new,courier"								, "Courier New"			, 
	"georgia,palatino"									, "Georgia"					, 
	"helvetica"													, "Helvetica"				, 
	"impact,chicago"										, "Impact"					, 
	"symbol"														, "Symbol"					, 
	"tahoma,arial,helvetica,sans-serif"	, "Tahoma"					, 
	"terminal,monaco"										, "Terminal"				, 
	"times new roman,times"							, "Times New Roman"	, 
	"trebuchet ms,geneva"								, "Trebuchet MS"		, 
	"verdana,geneva"										, "Verdana"					, 
	"webdings"													, "Webdings"				, 
	"wingdings,zapf dingbats"           , "Wingdings" 
);


Sizes = new Array ( 
	"0","[ t a i l l e s ]",
	"1", "1",
	"2", "2",
	"3", "3",
	"4", "4",
	"5", "5",
	"6", "6",
	"7", "7"
); 

Styles = new Array ( 
	"0"					,"[ s t y l e s ]",
	"<p>"				,"Paragraphe &lt;p&gt;",
	"<h1>"			,"Titre 1 &lt;h1&gt;",
	"<h2>"			,"Titre 2 &lt;h2&gt;",
	"<h3>"			,"Titre 3 &lt;h3&gt;",
	"<h4>"			,"Titre 4 &lt;h4&gt;",
	"<h5>"			,"Titre 5 &lt;h5&gt;",
	"<h6>"			,"Titre 6 &lt;h6&gt;",
	"<address>"	,"Adresse &lt;ADDR&gt;",
	"<pre>"			,"Formatter &lt;pre&gt;"
); 

Menus = new Array ( 
	new Array ( 
		new Array ( 'Cut'     		, 'Cut'     		, 'cut'   		),
		new Array ( 'Copy'     		, 'Copy'     		, 'copy'   		),
		new Array ( 'Paste' 			, 'Paste'   		, 'paste'		)
	),
	new Array ( 
		new Array ( 'Gras'     		, 'Bold'     		, 'bold'   		),
		new Array ( 'Italique' 		, 'Italic'   		, 'italic'		),
		new Array ( 'Underline'		, 'Underline'		, 'underline'	)
	),
	new Array ( 
		new Array ( 'Indent'			, 'Indent'			, 'indent'		),
		new Array ( 'Outdent'		, 'Outdent'			, 'outdent'		)
	),
	new Array ( 
		new Array ( 'JustifyCenter', 'JustifyCenter'	, 'justify'		),
		new Array ( 'JustifyLeft'	, 'JustifyLeft'	, 'left'			),
		new Array ( 'JustifyRight'	, 'JustifyRight'	, 'right'		)
	),
	new Array ( 
		new Array ( 'Couleur 1'		, 'ForeColor'		, 'fgcolor'		),
		new Array ( 'Couleur 2'		, 'BackColor'		, 'bgcolor'		),
		new Array ( 'RemoveFormat'	, 'RemoveFormat'	, 'rem_format'	)
	),
	new Array ( 
		new Array ( 'Lien'		   , 'CreateLink'		, 'lien'			),
		new Array ( 'Image'		   , 'InsertImage'	, 'image'		)
	),
	new Array ( 
		new Array ( 'FormatBlock1'	, 'FormatBlock1'	, 'formatblock1'),
		new Array ( 'FormatBlock2'	, 'FormatBlock2'	, 'formatblock2'),
		new Array ( 'FormatBlock3'	, 'FormatBlock3'	, 'formatblock3')
	)
	
//	new Array ( 'SelectAll','SelectAll','newfolder')
);

Menus2 = new Array ( 
	new Array ( 1, 'Cut'     		, 'Cut'     		, 'cut'   		),
	new Array ( 1, 'Copy'     	, 'Copy'     		, 'copy'   		),
	new Array ( 1, 'Paste' 			, 'Paste'   		, 'paste'			),
	new Array ( 4, '' ),
	new Array ( 1, 'Gras'     	, 'Bold'     		, 'bold'   			),
	new Array ( 1, 'Italique' 	, 'Italic'   		, 'italic'			),
	new Array ( 1, 'Underline'	, 'Underline'		, 'underline'		),
	new Array ( 3, '' ),
	new Array ( 1, 'Indent'			, 'Indent'			, 'indent'			),
	new Array ( 1, 'Outdent'		, 'Outdent'			, 'outdent'			),
	new Array ( 4, '' ),
	new Array ( 1, 'insertorderedlist'	, 'insertorderedlist'		, 'numlist'	),
	new Array ( 1, 'insertunorderedlist', 'insertunorderedlist'	, 'bullist'	),
	new Array ( 4, '' ),
	new Array ( 1, 'Couleur 1'	, 'ForeColor'	, 'fgcolor'		),
	new Array ( 1, 'Couleur 2'	, 'BackColor'	, 'bgcolor'		),
	new Array ( 3, '' ),
	new Array ( 1, 'JustifyCenter'	, 'JustifyCenter'	, 'center'	),
	new Array ( 1, 'JustifyLeft'		, 'JustifyLeft'		, 'left'		),
	new Array ( 1, 'JustifyRight'		, 'JustifyRight'	, 'right'		),
	new Array ( 1, 'Justify' 				, 'JustifyFull'		, 'justify'	),
	new Array ( 4, '' ),
	new Array ( 1, 'RemoveFormat'	, 'RemoveFormat', 'rem_format'	),
	new Array ( 4, '' ),
//	new Array ( 1, 'Lien'	, 'CreateLink'	, 'lien'			),
	new Array ( 1, 'Image', 'InsertImage'	, 'image'			),
//	new Array ( 4, '' ),
//	new Array ( 1, 'FormatBlock1'	, 'FormatBlock1'	, 'formatblock1'),
//	new Array ( 1, 'FormatBlock2'	, 'FormatBlock2'	, 'formatblock2'),
//	new Array ( 1, 'FormatBlock3'	, 'FormatBlock3'	, 'formatblock3'),
	new Array ( 3, '' ),
	new Array ( 2, 'Font'		   , 'FontName'		  , Fonts	),
	new Array ( 3, ''	),
	new Array ( 2, 'Taille'		 , 'FontSize'		  , Sizes	),
	new Array ( 3, ''	),
	new Array ( 2, 'Style'		 , 'FormatBlock'	, Styles)
);

function pick_color()
{
	return showModalDialog ( "forms/pick_color.php", "", "dialogWidth:445px; dialogHeight:140px; status:no; scroll:no; help:no");
}

function pick_link()
{
	return showModalDialog ( "forms/pick_link.php", "", "dialogWidth:445px; dialogHeight:140px; status:no; scroll:no; help:no");
}

function pick_image()
{
	var parameters = showModalDialog ( "forms/form_files.php5?withaction=2&withmenu=0", "", "dialogWidth:450px; dialogHeight:300px; status:no; scroll:yes; help:no");	
	return 'photo.php5?id='+parameters[3];
}

function ClickEdtChk(chk,qui)
{
	var div = document.getElementById('DIV_'+qui);
//	var inp = document.getElementById('DATA_DIV_'+qui);
//	var edt = document.getElementById('EDIT_DIV_'+qui);
	var txt = document.getElementById('TEXT_DIV_'+qui);
	eval ( 'var inp = document.forms.lxl.DATA_DIV_'+qui+';');
	eval ( 'var edt = document.forms.lxl.EDIT_DIV_'+qui+';');
	if (chk.checked)
	{
		edt.value=inp.value;

		div.style.display = 'none';
		txt.style.display = '';
	}
	else
	{
		inp.value = edt.value;
		
		div.style.display = '';
		txt.style.display = 'none';
		div.innerHTML=inp.value;
	}
}

function FocusEdtOut (qui)
{
	if (qui!='')
	{
		eval ( 'var inp = document.forms.lxl.DATA_DIV_'+qui+';');
		eval ( 'var edt = document.forms.lxl.EDIT_DIV_'+qui+';');
		inp.value = edt.value;
	}
	else if (Active_Id!=0)
	{
		var div = document.getElementById('DIV_'+Active_Id);
//		var inp = document.getElementById("DATA_DIV_'+Active_Id+'");
		eval ( 'var inp = document.forms.lxl.DATA_DIV_'+Active_Id+';');
		inp.value=div.innerHTML;
	}
	Active_Id=0;
}

function FocusEdtIn(qui)
{
	Active_Id=qui;
}

var popup_menu_open=false;
var popup_dialog_open=false;

var popup_menu = null;
if (window.createPopup)
	popup_menu = window.createPopup();

function noHTML (txt)
{
	a = txt.indexOf('<');
	b = txt.indexOf('>');
	len = txt.length;
	c = txt.substring ( 0, a);
	if (b==-1) b = a;
	d = txt.substring ( b+1, len);
	txt = c + d;
	cont = txt.indexOf('<');
	if (cont!=-1) txt = noHTML( txt);
	return txt;
}

function do_action(action)
{
	switch(action)
	{
		case 'ForeColor'	: return document.execCommand( action,false,pick_color());
		case 'BackColor'	: return document.execCommand( action,false,pick_color());
		case 'CreateLink'	: return document.execCommand( action,true);
		case 'InsertImage' : return document.execCommand( action,false,pick_image());
		case 'FormatBlock1': return document.execCommand( 'FormatBlock',false,'<H1>');
		case 'FormatBlock2': return document.execCommand( 'FormatBlock',false,'<H2>');
		case 'FormatBlock3': return document.execCommand( 'FormatBlock',false,'<H3>');
		case 'RemoveFormat':
			if (Active_Id!=0)
			{
				var div = document.getElementById('DIV_'+Active_Id);
				div.innerHTML = noHTML ( div.innerHTML);
			}
		return 1;
	}
	
	var sText = document.selection.createRange();
	if (!sText=="")
	{
     return document.execCommand(action);
  }
  return 0;
}

function popup_menu_over(obj, over)
{
	if(over)
	{
		obj.style.backgroundColor = 'Highlight';
		obj.style.color = 'HighlightText';
	}
	else
	{
		obj.style.backgroundColor = '';
		obj.style.color = '';
	}
}

function close_popup_menu ()
{
	if (popup_menu_open)
	{
		popup_menu_open=false;
	}
}

/****************************************************************************/

function make_popup_menu_body(innerHTML)
{
	var code = '';

	code += '<table cellspacing=1 cellpadding=0 border=0 bgcolor=black width=100% height=100%>';
	code += '<tr><td>';
	code += '<table cellspacing="0" cellpadding="0" border="0" bgcolor="buttonface" width="100%" height="100%">';
	code += '<tr bgcolor=#339933><td style="font-family:Verdana; font-size:10px; color=#FFFFFF; font-weight: bold" align=center>Menu</td></tr>';
	code += innerHTML;
	code += '</table>';
	code += '</td></tr>';
	code += '</table>';

	return code;
}

function make_popup_menu_button_item ( menus)
{
	var i;
	var code = '';

	code += '<tr><td><table align=left cellspacing=0 cellpadding=0 border=0><tr>';
	for (i=0; i<menus.length; i++)
	{
		code += '<td onclick="parent.do_action(\''+menus[i][1]+'\'';
		code += '); parent.popup_dialog_open=true; parent.popup_menu_open=false; parent.popup_menu.hide();';
		code += '" style="cursor:hand; font-family:Verdana; font-size:10px;"';
		code += ' onmouseover="parent.popup_menu_over(this, true);"';
		code += ' onmouseout="parent.popup_menu_over(this);">';
	
		code += '<img src="images/icones/'+menus[i][2]+'.gif" width="20" height="20" alt="'+menus[i][0]+'" align="absmiddle" hspace="2">';
		code += '</td>';
	}
	code += '</tr></table></td></tr>';

	return code;
}

function make_popup_menu_item ( menus)
{
	var code = '';

	name 		= menus[0];
	action 	= menus[1];
	img_name = menus[2];
	
	code += '<tr>';
	if(name != '<HR>')
	{
		code += '<td onclick="parent.do_action(\''+action+'\'';
		code += '); parent.popup_dialog_open=true; parent.popup_menu_open=false; parent.popup_menu.hide();';
		code += '" style="cursor:hand; font-family:Verdana; font-size:10px;"';
		code += ' onmouseover="parent.popup_menu_over(this, true);"';
		code += ' onmouseout="parent.popup_menu_over(this);">';
	}
	else
	{
		code += '<td>';
	}

	code += '<nobr>';
	if(img_name)
	{
		code += '<img src="images/icones/'+img_name+'.gif" width="20" height="20" alt="'+name+'" align="absmiddle" hspace="4">';
	}
	else
	{
		code += '<img src="images/icones/vide.gif" width="20" height="20" hspace="4">';
	}

	if(name != '<HR>')
	{
		code += name;
	}
	else
	{
		code += '<br><img src="images/icones/vide.gif" width="100%" height="1" style="BACKGROUND-COLOR: buttonshadow"><br>';
		code += '<img src="images/icones/vide.gif" width="100%" height="1" style="BACKGROUND-COLOR: buttonhighlight"><br>';
	}

	code += '<img src="images/icones/vide.gif" width="8" height="1"></nobr>';
	code += '</td></tr>';

	return code;
}

function make_popup_menu ()
{
	if(!popup_menu || !Active_Id || popup_menu_open) return false;
	popup_menu_open=true;

	var td_height = 20;
	var hr_height = 4;

	var width = 0;
	var height = 4;

	var char_width = 8;//13
	var max_chars = 0;

	var innerHTML = '';
	
	var i;
	
	for (i=0; i<Menus.length; i++)
	{
		if (Menus[i][0] instanceof Array)			// make_popup_menu_button_item
		{
			innerHTML += make_popup_menu_button_item ( Menus[i]); 
			height += td_height;
		}
		else 													// make_popup_menu_item
		{
			innerHTML += make_popup_menu_item ( Menus[i]); 
			height += td_height;
		}
	}
	
	max_chars = Math.max(max_chars, 10);
//	width     = max_chars*char_width+40;
	width     = 3*25;
		
	popup_menu.document.body.innerHTML = make_popup_menu_body ( innerHTML); height += td_height;
	popup_menu.document.body.onunload = close_popup_menu;
	popup_menu.show ( event.clientX, event.clientY, width, height, document.body);
	return false;
}

/****************************************************************************/
/****************************************************************************/
function setContent(action,blnInterfaceUtilisateur,valeur)
{
	ewin = window;
	edoc = ewin.document;

  edoc.execCommand(action, blnInterfaceUtilisateur, valeur);

	if (Active_Id!=0)
	{
		div = document.getElementById('DIV_'+Active_Id);
		div.focus();
	}
}

function make_popup_menu_body2(innerHTML)
{
	var code = '';

	code += '<table cellspacing=0 cellpadding=0 border=0 bgcolor="buttonface" width=100% height=100%>';
	code += '<tr><td><table cellspacing="0" cellpadding="0" style="border:1px solid #FFF; border-right-color:#000; border-bottom-color:#000; background:#999;"><tr>';
	code += innerHTML;
	code += '</tr></table></td></tr>';
	code += '</table>';

	return code;
}

function make_popup_menu_button_item2 ( menus)
{
	var code = '';

	if (menus[0]==1)
	{
		code += '<td onclick="parent.popup_dialog_open=true; parent.popup_menu_open=false; parent.popup_menu.hide(); parent.do_action(\''+menus[2]+'\');"';
		code += ' onmouseover="parent.popup_menu_over(this, true);"';
		code += ' onmouseout="parent.popup_menu_over(this);">';
		code += '<img src="images/icones/'+menus[3]+'.gif" width="20" height="20" alt="'+menus[1]+'" align="absmiddle" hspace="1">';
		code += '</td>';
	}

	if (menus[0]==2)
	{
		code += "<td><select style='font-family:Verdana; font-size:9px; height:22; width:132;' size=1 name='"+menus[1]+"' onchange='parent.setContent(\""+menus[2]+"\",false,this.value); this.selectedIndex=0; parent.popup_dialog_open=true; parent.popup_menu_open=false; parent.popup_menu.hide();'>";
		var i;
		for (i=0; i<menus[3].length; i++)
		{
			code += "<option value='"+menus[3][i++]+"'>"+menus[3][i]+"</option>";
		}
		code += "</select>";
		code += "</td>";
	}

	if (menus[0]==3)
	{
		code += '</tr></table></td></tr>';
		code += '<tr><td><table cellspacing="0" cellpadding="0" style="border:1px solid #FFF; border-right-color:#000; border-bottom-color:#000; background:#999;"><tr>';
	}

	return code;
}

function make_popup_menu2 ()
{
	if(!popup_menu || !Active_Id || popup_menu_open) return false;
	popup_menu_open=true;

	var innerHTML = '';
	
	var i;
	
	for (i=0; i<Menus2.length; i++)
	{
		innerHTML += make_popup_menu_button_item2 ( Menus2[i]); 
	}
	
	width  = 134;
	height = 126;
		
	popup_menu.document.body.innerHTML = make_popup_menu_body2 ( innerHTML);
	popup_menu.document.body.onunload = close_popup_menu;
	popup_menu.show ( event.clientX, event.clientY, width, height, document.body);
	return false;
}

/****************************************************************************/

document.oncontextmenu = make_popup_menu2;

