function placeImg(id, layout, x, y)
{
	// Check that image doesn't already exist
	e = tinyMCE.getInstanceById('text_entry_' + lang).getDoc().getElementById('fig' + id);
	var msg = '';

	printOut('test', 'langEntries: ' + langEntries.toJSON());
	
	if (e)
	{
		tinyMCE.execCommand("mceSelectNode",false,e);
		msg += 'An instance of img' + id + ' already exists in the text.';
		alert(msg);
	}

	if (msg == '')
	{
		if (layout == 'port') { var dimen = 'height'; } else { var dimen = 'width'; }
		var caption_x = (layout == 'port') ? getCapWidthFromImg(x, y, 150) : 150;
		
		var node = '<div id="fig' + id + '" class="floatimgleft">';
		node += '<p id="p' + id + '"><a href="[image_link]id=' + id + '" title="' + langKeyPrint(langEntries, 'click_image_to_enlarge') + '"><img class="content_image" name="img' + id + '" ' + dimen + '="150" border="0" src="' + mediaPath + 'imgdisplay.php?id=' + id + '&view=1" /></a></p>';
		node += '<p id="caption' + id + '" style="width: ' + caption_x + 'px;" class="caption">' + langKeyPrint(langEntries, 'enter_caption_here') + '</p>';
		node += '</div>';
		
		printOut('test', node.escapeHTML());
		
		// mediaPath is set in the header of the editing page
		tinyMCE.execCommand('mceInsertContent',false, node);
	}
}

function modifyImg(id, layout, x, y, newVal)
{
	tinyMCE.triggerSave();
	var div = tinyMCE.getInstanceById('text_entry_' + lang).getDoc().getElementById('fig' + id);
	var p = tinyMCE.getInstanceById('text_entry_' + lang).getDoc().getElementById('p' + id);
	var caption = tinyMCE.getInstanceById('text_entry_' + lang).getDoc().getElementById('caption' + id);

	var sizes = new Array();
	sizes[0] = '';
	sizes[1] = '150';
	sizes[2] = '250';
	sizes[3] = '350';
	
	if (layout == '')
	{
		div.className = newVal;
	}
	else{
		var dimen = sizes[newVal]; 
		var axis = (layout == 'land') ? 'width' : 'height';
		
		var node = '<a href="[image_link]id=' + id + '" title="' + langKeyPrint(langEntries, 'click_image_to_enlarge') + '"><img class="content_image" name="img' + id + '" ' + axis + '="' + dimen + '" border="0" src="' + mediaPath + 'imgdisplay.php?id=' + id + '&view=' + newVal + '" /></a>';
 		p.innerHTML = node;

		var caption_x = (layout == 'port') ? getCapWidthFromImg(x, y, sizes[newVal]) : sizes[newVal];
 		caption.style.width = caption_x;
		printOut('test', node.escapeHTML());
	}
}

// Find the width of a portrait layout image
function getCapWidthFromImg(x, y, destHeight)
{
	var proportion = (x / y);
	var destWidth = Math.round(proportion * destHeight);
	
	return destWidth;
}

function deleteElement(id, record_type, assoc_id, type)
{
	e = tinyMCE.getInstanceById('text_entry_' + lang).getDoc().getElementById('fig' + id);

	if (e)
	{
		var agree = confirm('Remove instance of ' + e.id + ' from textbox?');
		
		if (agree == true)
		{
			tinyMCE.execCommand("mceSelectNode",false, e);
			e.innerHTML = '';
			tinyMCE.execCommand("mceRemoveNode", false, e);
		}
	}
	else{
		var agree = true;
	}

	if (!agree || agree == true)
	{
		var agree2 = confirm('Delete uploaded image img' + id + ' from database?\n\nIf you need the image again, you will have to upload it.');
		
		if (agree2 == true)
		{
			// Delete record from DB
			deleteRecord(id, record_type, assoc_id, type);
		}
	}
}

function getLinkTargets()
{
	if (document.getElementById('doc_ids').innerHTML != '')
	{
		// Get an array of all links
		e = tinyMCE.getInstanceById('text_entry_' + lang).getDoc().getElementsByTagName('a');
		id_arr = document.getElementById('doc_ids').innerHTML.split(',');
	
		for (x in id_arr)
		{
			document.getElementById('doctarget' + id_arr[x]).innerHTML = '';
		}
		
		for (i in e)
		{
			if (e[i] != undefined)
			{
				if (e[i].tagName != undefined)
				{
					for (x in id_arr)
					{
						RegC = 'docdownload.php\\?id=' + id_arr[x];
						myRegC = new RegExp(RegC, 'gi');
					
						if (e[i].href.match(myRegC))
						{
							text = e[i].innerHTML;
							content = '<font color="#000">Linked to:</font> ' + text;
							document.getElementById('doctarget' + id_arr[x]).innerHTML = content;
						}
					}
				}
			}
		}
	}
}

function doDoc(id, filename, action, script_name, assoc_id, type)
{
	// Check that document doesn't already exist
	e = tinyMCE.getInstanceById('text_entry_' + lang).getDoc().getElementsByTagName('a');
	var searchstr = 'docdownload.php\\?id=' + id;
	var myRegC = new RegExp(searchstr, 'gi');
	var msg = '';

	for (i in e)
	{
		if (e[i] != undefined)
		{
			if (e[i].tagName != undefined)
			{
				if (e[i].href.search(myRegC) > -1)
				{
					tinyMCE.execCommand("mceSelectNode",false,e[i]);
					text = e[i].innerHTML;
					
					if (action == 'unlink')
					{
						var agree = confirm('Remove link to ' + filename + ' from textbox?');
						
						if (agree == true)
						{
							tinyMCE.execCommand("mceRemoveNode", false, e[i]);
							msg += 'The link to ' + filename + ' with link text ' + text + ' has been removed.';
							
							// Remove deleted id from doc_ids div
							id_arr = document.getElementById('doc_ids').innerHTML.split(',');
							new_doc_ids = '';
							z = 0;
							for (q in id_arr)
							{
								if (id_arr[q] != id)
								{
									if (z != 0) { com = ','; } else { com = ''; }
									new_doc_ids += com + id_arr[q];
									
									z++;
								}
							}
							document.getElementById('doc_ids').innerHTML = new_doc_ids;

							break;
						}
					}
					else if (action == 'insert')
					{
						msg += 'A link to ' + filename + ' already exists in the text.';
						msg += '\n\nLink Text: '+ text;
						alert(msg);
						break;
					}
				}
			}
		}
	}
	
	if (action == 'insert' && msg == '')
	{
		// mediaPath is set in the header of the editing page
		href = mediaPath + 'docdownload.php?id=' + id + '&doc_action=view';
		text = tinyMCE.getInstanceById('text_entry_' + lang).getSel().toString();
		
		tinyMCE.execCommand('mceReplaceContent', false, '<a href="' + href + '" title="' + filename + '">' + text + '</a>');
		tinyMCE.triggerSave();
		show('doc_box');
	}
	else if (action == 'unlink')
	{
		tinyMCE.triggerSave();
		getLinkTargets();
		var agree2 = confirm('Delete uploaded document ' + filename + ' from database?\n\nIf you need the document again, you will have to upload it.');
		
		if (agree2 == true)
		{
			// Delete record from DB
			deleteRecord(id, script_name, assoc_id, type);
		}
	}
	else{
		//alert(msg);
		show('doc_box');
	}
}

function doExternalLink(href)
{
	if (!href.match('http:\/\/') && !href.match('#')) { href = 'http://' + href; }
	
	tinyMCE.execCommand('createlink', false, href);

	tinyMCE.triggerSave();
	e = tinyMCE.getInstanceById('text_entry_' + lang).getDoc().getElementsByTagName('a');
	for (i in e)
	{
		if (e[i] != undefined)
		{
			if (e[i].tagName != undefined)
			{
				if (e[i].href.match(href))
				{
					e[i].setAttribute('title', href);
					break;
				}
			}
		}
	}
	show('ext_links_box');
}

function doExternalLinkFromURL()
{
	href = tinyMCE.getInstanceById('text_entry_' + lang).getSel().toString();
	href = href.replace(/^ *|\ *$/g,'');
	text = href;
	if (!href.match('http:\/\/')) { href = 'http://' + href; }

	tinyMCE.execCommand('mceReplaceContent', false, '<a href="' + href + '" title="' + text + '">' + text + '</a>');
	tinyMCE.triggerSave();
	show('ext_links_box');
}

function doEmail()
{
	tinyMCE.execCommand('mceReplaceContent', false, '<a href="mailto:{$selection}" title="Email {$selection}">{$selection}</a>');
	tinyMCE.triggerSave();
	show('ext_links_box');
}

function fnSelect(objId) {
	fnDeSelect();
	if (document.selection) {
	var range = document.body.createTextRange();
		range.moveToElementText(document.getElementById(objId));
	range.select();
	}
	else if (window.getSelection) {
	var range = document.createRange();
	range.selectNode(document.getElementById(objId));
	window.getSelection().addRange(range);
	}
}
	
function fnDeSelect() {
	if (document.selection) document.selection.empty(); 
	else if (window.getSelection)
			window.getSelection().removeAllRanges();
}
