/**
 * Clear an item if the source is a select item whose value
 * is set to false
 * @param id of the source
 * @param id of the target
 */
function clearFormItem( src, tgt ) {
	var source = document.getElementById( src );
	if ( source.options[source.selectedIndex].value == 'false' ) {
		document.getElementById( tgt ).value = '';
	}
}

/**
 * Insert a tag into a textarea
 * @param zoneId id of the text area
 * @param startTag opening tag
 * @param endTag ending tag
 */
function insertSimpleTag( zoneId, startTag, endTag ) {
	var myField = document.getElementById( zoneId );
	var myValue = '';

	//IE support
	if ( document.selection ) {
		myField.focus();
		var sel = document.selection.createRange();

		if ( sel.text == '' ) {
			var text = prompt( "Entrez le texte :", "" );
			myValue = startTag+text+endTag;
		}
		else {
			myValue = startTag+sel.text+endTag;
		}

		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if ( myField.selectionStart || myField.selectionStart == '0' ) {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;

		if ( startPos == endPos ) {
			var text = prompt( "Entrez le texte :", "" );
			myValue = startTag+text+endTag;
		}
		else
		{
			myValue =	startTag
			+ myField.value.substring( startPos, endPos )
			+ endTag;
		}

		myField.value =	myField.value.substring( 0, startPos )
		+ myValue
		+ myField.value.substring( endPos, myField.value.length );
	}
	else {
		myValue = prompt( "Entrez le texte :", "" );
		myField.value += startTag+myValue+endTag;
	}
	myField.focus();
}

/**
 * Insert something at the current cursor position
 * @param myField element where the cursor is active
 * @param myValue value to insert
 */
function insertAtCursor( myField, myValue ) {
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if ( myField.selectionStart || myField.selectionStart == '0' ) {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;

		myField.value = myField.value.substring( 0, startPos )
		+ myValue
		+ myField.value.substring(endPos, myField.value.length);
	}
	else {
		myField.value += myValue;
	}
	
	myField.focus();
}

/** 
 * Insert a bold tag
 */
function bold( zone ) {
	insertSimpleTag( zone, '<b>', '</b>' );
}

/** 
 * Insert a italic tag
 */
function italic( zone ) {
	insertSimpleTag( zone, '<i>', '</i>' );
}

/** 
 * Insert a link tag
 */
function link( zone ) {
	var url = prompt( "Entrez l'url", "http://" );
	insertSimpleTag( zone, '<a href="'+url+'" target="_blank">', '</a>' );
}

