var editorHeight;
var toolbarHeight = 230; // height of browsertoolbars + toolbars of editor

function determineClientHeight()
{
	// determine page height
	var minHeight = 100;
	if(document.body.clientHeight)
	{
		editorHeight = document.body.clientHeight - toolbarHeight;
	
		if(editorHeight < minHeight)
			editorHeight = minHeight;
	}else
	{
		editorHeight = minHeight;	
	}
}

function submitForm(name)
{
	var theform = document.forms[name];

	if(typeof theform == 'object' &&  validateForm(theform,false,false,false,false,8))
	{
		if(theform.submit())
			return true;
		else
			return false;
	}else
	{
		return false;
	}
}

// Returns true or false based on whether the specified string is found
// in the array. This is based on the PHP function of the same name.
// Written by Beau Lebens <beau@dentedreality.com.au>
function in_array(stringToSearch, arrayToSearch)
{
	for (s = 0; s < arrayToSearch.length; s++)
	{
		thisEntry = arrayToSearch[s].toString();
		if (thisEntry == stringToSearch)
			return true;
	}
	return false;
}

function checkAll(form, checkall, c_name){
	for (var i=0;i<form.elements.length;i++) {
		var e = form.elements[i];
		if ((e.name == c_name) && (e.type=='checkbox')) {
			e.checked = checkall;
		}
	}
}

function showHelpDialog(width,height) {
	OpenWindow = window.showModalDialog("help.php","","dialogWidth:"+(width+20)+"px;dialogHeight:"+(height+70)+"px;center:yes;help:no;status:no");
}

var image_action = true
function openImageWindow() {
	var return_function = "setImage" // must be the name of the function you want to call when an item is chosen. This should be string containing only letters and numbers.
	wp_openDialog('http://www.whitetree.com/beheer/include/editor/dialog_frame.php?window=image.php&return_function='+return_function, 'modal',730,466)
}

function openDocWindow() {
	var return_function = "setDocument" // must be the name of the function you want to call when an item is chosen. This should be string containing only letters and numbers.
	wp_openDialog('http://www.whitetree.com/beheer/include/editor/dialog_frame.php?window=document.php&return_function='+return_function, 'modal',730,466)
}

function openColorWindow(elem) {
	wp_openDialog('http://www.whitetree.com/beheer/include/editor/dialog_frame.php?action=fill&elem='+elem+'&window=selcolor.php', 'modal',296,352)
}

var _formatDateFrom;
var _formatDateTo;
var _formatDateOldValue;

function formatDate(from, to, oldValue)
{
	_formatDateFrom = from;
	_formatDateTo = to;
	_formatDateOldValue = oldValue;

	if (from.value == oldValue)
	{
		window.setTimeout('formatDate(_formatDateFrom, _formatDateTo, _formatDateOldValue)', 100);
		return;
	}
	else
	{
		if (from.value == '' || from.value == 0)
		{
			from.value = 0;
			to.value = '';
		}
		else
		{
			var datestr = from.value;
			var year = datestr.substr(0, 4);
			var month = datestr.substr(4, 2);
			var day = datestr.substr(6, 2);
	
			var date = new Date(year, month-1, day);
			var newdatestr = date.toLocaleString();
			newdatestr = newdatestr.substr(0, newdatestr.lastIndexOf(" "));
			to.value = newdatestr;
			return;
		}
	}
}

// Wraperfunction to set a value of a formelement
function setValue(object, value)
{
	if (!object)
		return;

	object.value = value;
}

/* Scripts for selecting keywords */

var oldSelect = null;
function switchSelect(ch)
{
	if (!document.getElementById)
		return;

 	if (ch && ch.length > 0)
		var select = document.getElementById("select_" + ch);

	var submit = document.getElementById("submit_button");

	if (oldSelect)
		oldSelect.style.display = "none";

	submit.style.display = "none";
	if (select)
	{
		select.style.display = "inline";
		select.selectedIndex = 0;
		oldSelect = select;
	}
	else
		oldSelect = null;
}

function showSubmit()
{
	if (!document.getElementById)
		return;

	var submit = document.getElementById("submit_button");
	if (!submit)
		return;

	submit.style.display = "inline";
}

function selectKeyword()
{
	if (oldSelect && oldSelect.selectedIndex == 0)
		return false;

	var rdata = new Array(2);
	var option = oldSelect.options[oldSelect.selectedIndex];
	if (!option)
		return false;

	rdata[0] = option.value;
	rdata[1] = option.innerHTML;
	
	charselect = document.getElementById("charselect");
	if (charselect)
	{
		charselect.selectedIndex = 0;
		switchSelect("");
	}
	
	addKeyword(rdata);
	checkKeywordEmpty(); // do not call until row is added !!
	return false;
}

function checkKeywordEmpty()
{
	if( !document.getElementById )
		return( false );
	
	var body = document.getElementById('keyword_table_body');

	if( body.childNodes.length <= 1 )
		document.getElementById('nokeyword').style.display = 'block';
	else
		document.getElementById('nokeyword').style.display = 'none';
}

function addKeyword(data)
{
	if (!document.getElementById || !data)
		return false;

	var ids = getRowIDs('keyword_table');

	// data[0] = keyword ID
	// data[1] = keyword Title

	if (in_array("keyword_" + data[0], ids))
	{
		alert('Dit trefwoord is al toegekend.');		return false;
	}

	// hidden input field
	var id_field = document.createElement('input');
	id_field.type = 'hidden';
	id_field.name = 'keywords[]';
	id_field.value = data[0];

	// text-td
	var td_title = document.createElement('td');
	td_title.className = 'left noborder';
	td_title.appendChild(document.createTextNode(data[1]));
	td_title.appendChild(id_field);

	// delete button
	var a_delete = document.createElement('a');
	var i_delete = document.createElement('img');
	i_delete.src = 'http://www.whitetree.com/beheer/include/images/icons/cms.icon.delete.gif';
	a_delete.href = 'javascript:removeKeyword(' + data[0] + ');';
	a_delete.appendChild(i_delete);

	// button-td
	var td_button = document.createElement('td');
	td_button.appendChild(a_delete);
	td_button.style.width = '20px';
	td_button.style.textAlign = 'right';
	td_button.className = 'noborder';

	addRow('keyword_table', new Array(td_title, td_button), 'keyword_'+data[0]);
	
	return false;
}

function removeKeyword(id)
{
	removeRow('keyword_table', 'keyword_'+id);
	checkKeywordEmpty(); // do not call until row is removed!!
}

// ------------------------------- Common functions ------------------------------- \\
// Adds a row to a table
function addRow(tableID, rowElems, id)
{
	if (!document.getElementById)
		return false;

	var table = document.getElementById(tableID);
	if (!table || !rowElems)
		return false;

	var tbody = table.firstChild;
	if (tbody.nodeName == "#text") tbody = tbody.nextSibling;
	var row = document.createElement('tr');

	if (tbody.lastChild && tbody.lastChild.className == 'odd')
		var className = 'even';
	else
		var className = 'odd';

	row.className = className;
	if (id)
		row.id = id;

	for (i = 0; i < rowElems.length; i++)
		row.appendChild(rowElems[i]);

	tbody.appendChild(row);

	return false;
}

// Removes a row
function removeRow(tableID, rowID)
{
	if (!document.getElementById)
		return;

	var table = document.getElementById(tableID);
	var row = document.getElementById(rowID);
	if (!table || !row)
		return;

	var tbody = table.firstChild;
	if (tbody.nodeName == "#text") tbody = tbody.nextSibling;
	tbody.removeChild(row);

	var oldClass = 'even';
	for (i = 0; i < tbody.childNodes.length; i++)
	{
		if (tbody.childNodes[i].className == oldClass)
		{
			if (oldClass == 'even')
				tbody.childNodes[i].className = 'odd';
			else
				tbody.childNodes[i].className = 'even';
		}
		if (tbody.nodeName != "#text")
			oldClass = tbody.childNodes[i].className;
	}
}

// Get rowID's
function getRowIDs(tableID)
{
	if (!document.getElementById)
		return false;

	var table = document.getElementById(tableID);
	if (!table)
		return false;

	var tbody = table.firstChild;
	if (tbody.nodeName == "#text")
		tbody = tbody.nextSibling;

	var list = tbody.childNodes;
	if (!list || list.length == 0)
		return false;

	var ids = new Array();

	for (x = 0; x < list.length; x++)
	{
		if (list.item(x).nodeName != "#text")
		{
			ids[ids.length] = list.item(x).id;
		}
	}

	return ids;
}