function placeImg(id, layout)
{
	// Check that image doesn't already exist
	e = tinyMCE.getInstanceById("mce_editor_0").getDoc().getElementsByName('img' + id);
	var msg = '';

	for (i in e)
	{
		if (undefined != e[i].src)
		{
			tinyMCE.execCommand("mceSelectNode",false,e[i]);
			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'; }
		
		// mediaPath is set in the header of the editing page
		tinyMCE.execCommand('mceInsertContent',false, '<img name="img' + id + '" ' + dimen + '="150" border="0" class="floatimgleft" src="' + mediaPath + 'imgdisplay.php?id=' + id + '&view=1" />');
	}
}

function deleteElement(id, record_type, assoc_id, type)
{
	e = tinyMCE.getInstanceById("mce_editor_0").getDoc().getElementsByTagName('img');
	var RegC = 'id=' + id + '&view=';
	var myRegC = new RegExp(RegC, 'gi');

	for (i in e)
	{
		if (e[i].tagName != undefined)
		{
			if (e[i].src.match(myRegC))
			{
				var agree = confirm('Remove instance of ' + e[i].name + ' from textbox?');
				
				if (agree == true)
				{
					tinyMCE.execCommand("mceSelectNode",false,e[i]);
					tinyMCE.execCommand("mceRemoveNode", false, e[i]);
					
					break;
				}
			}
		}
	}
	
	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 modifyImg(id, layout, newVal)
{
	tinyMCE.triggerSave();
	e = tinyMCE.getInstanceById("mce_editor_0").getDoc().getElementsByName('img' + id);

	var sizes = new Array();
	sizes[0] = '';
	sizes[1] = '150';
	sizes[2] = '250';
	sizes[3] = '350';
	
	for (i in e)
	{
		if (undefined != e[i].src)
		{
			if (layout == 'land')
			{
				e[i].width = sizes[newVal];
				// mediaPath is set in the header of the editing page
				e[i].src = mediaPath + 'imgdisplay.php?id=' + id + '&view=' + newVal;
			}
			else if (layout == 'port')
			{
				e[i].height = sizes[newVal];
				// mediaPath is set in the header of the editing page
				e[i].src = mediaPath + 'imgdisplay.php?id=' + id + '&view=' + newVal;
			}
			else{
				e[i].className = newVal;
			}
		}
	}
	
	//tinyMCE.execCommand('mceSetAttribute',false,{name:'align',value:'right'});
	//tinyMCE.getInstanceById(textarea_name).getBody().innerHTML = new_content;
}

function getLinkTargets()
{
	if (document.getElementById('doc_ids').innerHTML != '')
	{
		// Get an array of all links
		e = tinyMCE.getInstanceById("mce_editor_0").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("mce_editor_0").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("mce_editor_0").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("mce_editor_0").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("mce_editor_0").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');
}
