function addBlank_from_liens() {
	if (document.getElementById("rightcol").offsetHeight - document.getElementById("presentation").offsetHeight > document.getElementById("middlecontent").offsetHeight) {
		var content = document.getElementById("blank").innerHTML;
		document.getElementById("blank").innerHTML = content + "<br />";
		timer = setTimeout ("addBlank_from_liens();", 0);
	}
}


function addBlank_from_menu() {
	if (document.getElementById("menu").offsetHeight > document.getElementById("middlecontent").offsetHeight) {
		var content = document.getElementById("blank").innerHTML;
		document.getElementById("blank").innerHTML = content + "<br />";
		timer = setTimeout ("addBlank_from_menu();", 0);
	}
}



function popup(file, type) {
	// on ouvre dans une fenêtre le fichier passé en paramètre.
	// cette ouverture peut être améliorée en passant d'autres
	// paramètres que la taille et la position de la fenêtre.
	if (type == '')
		popup = window.open(file,'ChooseIcon','scrollbars=yes,width=400,height=250,top=50,left=50'); 
	else 
		popup = window.open(file,'PopUp','scrollbars=yes, menubar=yes, toolbar=yes, width=800,height=600,top=5,left=5'); 
}

var oldCellColor = "#000";

function focusRow(row) {
	var cells = row.cells;
	if (!cells) return;
	oldCellColor = cells[0].style.backgroundColor;
	for (var i=0;i<cells.length;i++) {
		cells[i].style.backgroundColor='#eee';
	}
}
function blurRow(row) {
	var cells = row.cells;
	if (!cells) return;
	for (var i=0;i<cells.length;i++) {
		cells[i].style.backgroundColor=oldCellColor;
	}
}





// Helpline messages
default_help = "Astuce: Une mise en forme peut être appliquée au texte sélectionné.";
b_help = "Texte gras: <b>texte</b>";
i_help = "Texte italique: <i>texte</i>";
u_help = "Texte souligné: <u>texte</u>";
p_help = "Paragraphe: <p>texte</p>";
n_help = "Retour à la ligne: <br />";
s_help = "Saut de ligne: <br /><br />";
ul_help = "Liste: <ul>list</ul>";
li_help = "Liste: <li>texte</li>";
a_help = "Insérer un lien: <a href='http://url/'>http://url/</a>";
d_help = "Insérer une annotation: <div class='annotations'></div>";
t1_help = "Titre 1: <h4>texte</h4>";
t2_help = "Titre 2: <h5>texte</h5>";
t3_help = "Titre 3: <h6>texte</h6>";
t4_help = "Titre 4: <h7>texte</h7>";

// Shows the help messages in the helpline window
function helpline1(help, form_id) {
	document.post.helpbox1.value = eval(help + "_help");
}
function helpline2(help) {
	document.post.helpbox2.value = eval(help + "_help");
}
function helpline3(help, form_id) {
	document.post.helpbox3.value = eval(help + "_help");
}
function helpline4(help) {
	document.post.helpbox4.value = eval(help + "_help");
}


var nonie_FormType = "body";
var lastSelected;

// stores the caret
function storeCaret (textEl) {

	// store caret
	if (textEl.createTextRange) 
		textEl.caretPos = document.selection.createRange().duplicate();

	// also store lastselectedelement
	lastSelected = textEl;

	nonie_FormType = textEl.name;

}



function mozWrap(txtarea, lft, rgt) {
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + lft + s2 + rgt + s3;
}
function mozReplace(txtarea, newText) {
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + newText + s3;
}

// inserts text at caret (overwriting selection)
function insertAtCaret (text, form_id) {
	var textEl = lastSelected;
	if (textEl && textEl.createTextRange && textEl.caretPos) {
		var caretPos = textEl.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
	} else if (!document.all && document.getElementById) {
		mozReplace(document.getElementById(form_id), text);				
	} else if (textEl) {
		textEl.value  += text;
	} else {
		document.getElementById(form_id).value += text;		
	}
}


// inserts a tag around the selected text
function insertAroundCaret (textpre, textpost, form_id) {
	var textEl = lastSelected;
	if (textEl && textEl.createTextRange && textEl.caretPos) {
		var caretPos = textEl.caretPos;
		caretPos.text = textpre + caretPos.text + textpost;
	} else if (!document.all && document.getElementById) {
		mozWrap(document.getElementById(form_id), textpre, textpost);	
	} else {
		document.getElementById(form_id).value += textpre + textpost;
	}
}



function boldThis(form_id) { insertAroundCaret('<b>','</b>', form_id); }
function italicThis(form_id) { insertAroundCaret('<i>','</i>', form_id); }
function underlineThis(form_id) { insertAroundCaret('<u>','</u>', form_id); }
function paragraphThis(form_id) { insertAroundCaret('<p>','</p>', form_id); }
function title1This(form_id) { insertAroundCaret('<h4>','</h4>', form_id); }
function title2This(form_id) { insertAroundCaret('<h5>','</h5>', form_id); }
function title3This(form_id) { insertAroundCaret('<h6>','</h6>', form_id); }
function title4This(form_id) { insertAroundCaret('<h7>','</h7>', form_id); }
function beginlistThis(form_id) { insertAroundCaret('<ul>\n\n','</ul>', form_id); }
function annotateThis(form_id) { insertAroundCaret('<div class="annotations">','</div>', form_id); }
function listThis(form_id) { insertAroundCaret('<li>','</li>', form_id); }
function carriagereturnThis(form_id) { insertAtCaret('<br />', form_id); }
function jumplineThis(form_id) { insertAtCaret('<br /><br />', form_id); }

function ahrefThis(form_id) {
	if (document.selection)
		strSelection = document.selection.createRange().text;
	else
		strSelection = '';
		
	strHref = prompt("Create a link to:","http://");
	if (strHref == null) return;
	
	var textpre = "<a href=\"#\" onClick=\"return popup (" + strHref + ", \'PopUp\');\">";
	insertAroundCaret(textpre, "</a>", form_id);
}




function BtnHighlight(el) {
	el.style.backgroundColor = "#eee";
}

function BtnNormal(el) {
	el.style.backgroundColor = "#fff";
}
