function mail(host, user, linktext)
{
	document.write('<a class=\"mail_to\" href=\"mailto:' + user + '@' + host + '\">');

	if ((linktext == '') || (!linktext))
	{
		document.write(user + '@' + host + '<\/a>');
	}
	else
	{
	  document.write(linktext + '<\/a>');
	}
}

function insertWikiTitle(comboBox, title)
{
	if (!(title.indexOf("0") > -1))
	{
		insert(title + ' ', ' ' + title, document, 'wiki-editor');
	}

	comboBox.selectedIndex = 0;
}

function cmdUpdateContent()
{
	addHiddenInputElement('plugin-form', 'content_update', 'true');
	addHiddenInputElement('plugin-form', 'close', 'false');
	document.getElementById('plugin-form').submit();
}

function cmdUpdateContentClose()
{
	addHiddenInputElement('plugin-form', 'content_update', 'true');
	addHiddenInputElement('plugin-form', 'close', 'true');
	document.getElementById('plugin-form').submit();
}

function enableTinyMCE(enable)
{
  if (enable)
  {
  	addHiddenInputElement('plugin-form', 'use_tinymce', '1');
  }
  else
  {
  	addHiddenInputElement('plugin-form', 'use_tinymce', '0');
  }

	cmdUpdateContent();
}

function addHiddenInputElement(formId, name, value)
{
	var form = document.getElementById(formId);

	var inputElement = document.createElement("input");

	inputElement.type = "hidden";
	inputElement.name = name;
	inputElement.value = value;

	form.appendChild(inputElement);
}

function createCookie(name, value, days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else
	{
	  var expires = "";
	}

	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');

	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}

	return null;
}

function addSrcToDestList(srcListId, destListId)
{
	destList = document.getElementById(destListId);
	srcList = document.getElementById(srcListId);

	var len = destList.length;

	for (var i = 0; i < srcList.length; i++)
	{
		if ((srcList.options[i] != null) && (srcList.options[i].selected))
		{
			//Check if this value already exist in the destList or not
			//if not then add it otherwise do not add it.
			var found = false;

			for(var count = 0; count < len; count++)
			{
				if (destList.options[count] != null)
				{
					if (srcList.options[i].text == destList.options[count].text)
					{
						found = true;
						break;
      		}
				}
			}

			if (found != true)
			{
				destList.options[len] = new Option(srcList.options[i].text, srcList.options[i].value, false, false);
				len++;
			}
		}
	}
}

function deleteFromDestList(destListId)
{
	var destList = document.getElementById(destListId);
	var len = destList.options.length;

	for(var i = (len-1); i >= 0; i--)
	{
		if ((destList.options[i] != null) && (destList.options[i].selected == true))
		{
			destList.options[i] = null;
    }
	}
}

function selectDestListItems(destListId)
{
	var destList = document.getElementById(destListId);
	var len = destList.options.length;

	for(var i = (len-1); i >= 0; i--)
	{
		destList.options[i].selected = true;
	}
}

function confirmDialog(message, link)
{
	var input;
	input = confirm(message);

	if (input == true)
	{
		self.location.href = link;
	}
}

function updateGroup(groupName)
{
	var group = document.getElementById(groupName + "-group");
	var groupHeader = document.getElementById(groupName + "-group-header");

	if (group.style.display == 'none')
	{
		group.style.display ='';
		groupHeader.className = "group-header-down";
	}
	else
	{
		group.style.display ='none';
		groupHeader.className = "group-header-up";
	}
}

function setElementVisibility(elementId, isVisible)
{
	var element = document.getElementById(elementId);

	if (isVisible == true)
	{
		element.style.display = "";
	}
	else
	{
		element.style.display = "none";
	}
}

function showPopup(title, url, width, height)
{
  var top = screen.height / 2 - height / 2;
  var left = screen.width / 2 - width / 2;

  popup = window.open(url, title, "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left);
}

function setInputValue(inputValue, inputId)
{
	var input = document.getElementById(inputId);
	input.value = inputValue;
}

function insert(aTag, eTag, document, inputId)
{
	input = document.getElementById(inputId);
  input.focus();
  /* Für Internet Explorer */
  if(typeof document.selection != 'undefined')
  {
    /* Einfügen des Formatierungscodes */
    var range = document.selection.createRange();
    var insText = range.text;
    range.text = aTag + insText + eTag;
    /* Anpassen der Cursorposition */
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -eTag.length);
    } else {
      range.moveStart('character', aTag.length + insText.length + eTag.length);
    }
    range.select();
  }
  /* Für neuere auf Gecko basierende Browser */
  else if(typeof input.selectionStart != 'undefined')
  {
    /* Einfügen des Formatierungscodes */
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = input.value.substring(start, end);
    input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
    /* Anpassen der Cursorposition */
    var pos;
    if (insText.length == 0) {
      pos = start + aTag.length;
    } else {
      pos = start + aTag.length + insText.length + eTag.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
  /* Für die übrigen Browser */
  else
  {
    /* Abfrage der Einfügeposition */
    var pos;
    var re = new RegExp('^[0-9]{0,3}$');
    while(!re.test(pos)) {
      pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
    }
    if(pos > input.value.length) {
      pos = input.value.length;
    }
    /* Einfügen des Formatierungscodes */
    var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
    input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
  }
}


