/*
	script.js
	----------

	This file contains every javascript function used in the application.
*/


function addText(formName, elemName, startTag, endTag) {
    startTag.replace("&quot;", '"');
	var input = window.opener.document.forms[formName].elements[elemName];
	//alert('formName=' + formName + '\n elemName=' + elemName + '\n startTag=' + startTag + '\n endTag=' + endTag);
	input.focus();
	/* For IE */
	if(typeof window.opener.document.selection != 'undefined') {
		/* Insertion of the code */
		var range = window.opener.document.selection.createRange();
		var text = range.text;
		range.text = startTag + text + endTag;
		/* Cursor position */
		range = window.opener.document.selection.createRange();
		if (text.length == 0) {
			range.move('character', -endTag.length);
		}
		else {
			range.moveStart('character', startTag.length + text.length + endTag.length);
		}
		range.select();
	}
	/* For more recent browser */
	else if(typeof input.selectionStart != 'undefined')
	{
		/* Insertion of the code */
		var start = input.selectionStart;
		var end = input.selectionEnd;
		var text = input.value.substring(start, end);
		input.value = input.value.substr(0, start) + startTag + text + endTag + input.value.substr(end);
		/* Cursor position */
		var pos;
		if (text.length == 0) {
			pos = start + startTag.length;
		}
		else {
			pos = start + startTag.length + text.length + endTag.length;
		}
		input.selectionStart = pos;
		input.selectionEnd = pos;
	}
	window.close();
}
function addTextOnpage(formName, elemName, startTag, endTag) {
	//alert('it works');   
    startTag.replace("&quot;", '"');
	var input = document.forms[formName].elements[elemName];
	
//	alert('formName=' + formName + '\n elemName=' + elemName + '\n startTag=' + startTag + '\n endTag=' + endTag);
	input.focus();
	/* For IE */
	if(typeof document.selection != 'undefined') {
		/* Insertion of the code */
		var range = document.selection.createRange();
		var text = range.text;
		range.text = startTag + text + endTag;
		/* Cursor position */
		range = document.selection.createRange();
		if (text.length == 0) {
			range.move('character', -endTag.length);
		}
		else {
			range.moveStart('character', startTag.length + text.length + endTag.length);
		}
		range.select();
	}
	/* For more recent browser */
	else if(typeof input.selectionStart != 'undefined')
	{
		/* Insertion of the code */
		var start = input.selectionStart;
		var end = input.selectionEnd;
		var text = input.value.substring(start, end);
		input.value = input.value.substr(0, start) + startTag + text + endTag + input.value.substr(end);
		/* Cursor position */
		var pos;
		if (text.length == 0) {
			pos = start + startTag.length;
		}
		else {
			pos = start + startTag.length + text.length + endTag.length;
		}
		input.selectionStart = pos;
		input.selectionEnd = pos;
	}
}



