var teller = document.createElement('span');
teller.className = 'teller';

function insertTeller(x)
{
	for (var i = 0; i < x.length; i++)
	{
		if (!x[i].getAttribute('maxlen')) continue;
		x[i].onkeyup = checkLength;
		x[i].onchange = checkLength;
		var writeroot = teller.cloneNode(true);
		var insertPoint = x[i].nextSibling;
		if (insertPoint.nodeName == 'BR') insertPoint = insertPoint.nextSibling;
		x[i].parentNode.insertBefore(writeroot,insertPoint);
		x[i].relatedElement = writeroot;
		var currentLen = x[i].value.length;
		writeroot.appendChild(document.createTextNode(currentLen + '/' + x[i].getAttribute('maxlen')));
	}
}

function insertInvTeller(x)
{
	for (var i = 0; i < x.length; i++)
	{
		if (!x[i].getAttribute('maxlen')) continue;
		x[i].onkeyup = checkInvLength;
		x[i].onchange = checkInvLength;
		var currentLen = x[i].value.length;
	}
}

function checkLength(e)
{
	var max = this.getAttribute('maxlen');
	var current = this.value.length;
	var withinLimits = (current <= max);
	if (!withinLimits)
	{
		this.value = this.value.substring(0,max);
		current = this.value.length;
		alert('The number of characters you entered exceeds ' + max + '. Exceeding characters will be lost.');
	}
	this.relatedElement.innerHTML = current + '/' + max;
	return withinLimits;
}

function checkInvLength(e)
{
	var max = this.getAttribute('maxlen');
	var current = this.value.length;
	var withinLimits = (current <= max);
	if (!withinLimits)
	{
		this.value = this.value.substring(0,max);
		current = this.value.length;
		alert('The number of characters you entered exceeds ' + max + '. Exceeding characters will be lost.');
	}
	return withinLimits;
}

function validateForm(frm)
{
	var frms = document.forms;
	var errorFound = false;
	for (var n=1;n<frms.length;n++)
	{
		var x = frms[n].elements;
		for (var i=0;i<x.length;i++)
		{
			//alert(x[i]+' - required: '+x[i].getAttribute('required')+' - value: '+x[i].value);
			if (x[i].getAttribute('required') == 'true' && (!x[i].value || x[i].value == '' || x[i].errorMessage))
			{
				
				x[i].style.color = '#cc0000';
				x[i].value = 'Dit veld is verplicht';
				errorFound = true;
				x[i].errorMessage = true;
				x[i].onfocus = removeError;
			}
		}
		break;
	}
	if (errorFound)
		alert('U heeft enige verplichte velden niet ingevuld');
	return !errorFound;
}

function removeError()
{
	this.style.color = '';
	this.value = '';
	this.errorMessage = false;
}

var fakeFileUpload = document.createElement('div');
fakeFileUpload.className = 'fakefile';
fakeFileUpload.appendChild(document.createElement('input'));
var href = document.createElement("a");
href.innerHTML = "BROWSE";
fakeFileUpload.appendChild(href);
//var image = document.createElement('img');
//image.src='static/images/buttons/button_select.gif';
//fakeFileUpload.appendChild(image);

function insertFakeFileUpload()
{
	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++)
	{
		if (x[i].type != 'file') continue;
		if (x[i].parentNode.className.indexOf('fileinputs') == -1) continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];

		x[i].onmouseout = x[i].onkeyup = function () {
			this.relatedElement.value = this.value;
			if (this.value != '') {
				fileSelected();
			}
		}
		x[i].onclick = function() {
			this.relatedElement.click();
		}
		x[i].onselect = function () {
			this.relatedElement.select();
		}
	}
}
var fileSelectedValue = false;
function fileSelected() {
	fileSelectedValue = true;
}
function isSelected() {
	return fileSelectedValue;
}

/* Speciaal voor Levensloop */

function wisBody()
{
	document.forms[0].bodytext.value = '';
	document.forms[0].bodytext.onchange();
}
function SelectPhoto() {
	document.getElementById("selectphoto").style.display = "block";
}
function PhotoOK() {
	document.getElementById("selectphoto").style.display = "none";
	return true;
}
function PhotoClose() {
	document.getElementById("selectphoto").style.display = "none";
	return false;
}
function PhotoCancel() {
	document.getElementById("selectphoto").style.display = "none";
	return false;
}
function SelectMovie() {
	document.getElementById("selectmovie").style.display = "block";
}
function MovieOK() {
	document.getElementById("selectmovie").style.display = "none";
	return true;
}
function MovieClose() {
	document.getElementById("selectmovie").style.display = "none";
	return false;
}
function MovieCancel() {
	document.getElementById("selectmovie").style.display = "none";
	return false;
}

