
var colors = new Array("#ddffdd", "#fa8080", "#ffffff");
var plaatjesurl = 'plaatjes/';
var confidential = '6'; 
var secret = '7';


function checkForm(formName) {
	var i = 0;
	var doc = formName;
	var error = '<font class="errormelding">Errors found, check marked field</font>';

	for (i; i<doc.elements.length; i++) {
		if ((doc.elements[i].id.indexOf("free") == -1) || doc.elements[i].style.background == colors[1]) {		  
		 	if (doc.elements[i].type == 'text' || doc.elements[i].type == 'password' || doc.elements[i].type == 'file' || doc.elements[i].type == 'textarea') {
		 		if (doc.elements[i].value == "" || doc.elements[i].style.background == colors[1]) {
		 			doc.elements[i].style.background = colors[1];
					
					$('melding').innerHTML = error
					return false;
				}
			} else if (doc.elements[i].value == '-1') {
				doc.elements[i].style.background = colors[1];
				//doc.elements[i].focus();
				$('melding').innerHTML = error;
				return false;
			}
		}
		
		// No error found, reset background color in case it was already marked
		doc.elements[i].style.background = colors[2];
	}
	return true;
}

function controleerWachtwoord(field1,field2,formulier)
{
	if(field1.value == field2.value)
	{		
		return checkForm(formulier);
	}
	else
	{
		$('melding').innerHTML = '<font class="errormelding">Passwords do not match</font>'
		return false;
	}
	
}

function controleerNaam(field3,field1,field2,formulier)
{
	if(field3.value.indexOf(" ") > 0)
	{
		return controleerWachtwoord(field1,field2,formulier);
	}
	else
	{
		alert("PLease fill in both your first and last name");
		return false;
	}
}

function checkVeld(field) {
	if (field.value == "") {
		field.style.background = colors[1];
	} else {
		field.style.background = colors[2];
	}
}

function checkEmail(field) 
{		
	var emailFilter=/^.+@.+\..{2,4}$/;
	if (!(emailFilter.test(field.value))) 
	{		
		field.style.background = colors[1];
		field.focus();
		return false;

	}
	else
	{

		var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
		if (field.value.match(illegalChars)) 
		{			
			field.style.background = colors[1];
			field.focus();
			return false;
		}
		else
		{
			field.style.background = colors[2];
			return true;
		}
	}
}

function checkDatum(field)
{
	if (field.value == "")
	{
		field.style.background = colors[1];
	}
	else
	{
		if (field.value== field.value.match(/[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}/))
		{
			values =  field.value.split('-');
			if (values[0]>0 && values[0]<=31 && values[1]>0 & values[1]<=12)
			{
				field.style.background = colors[2];
			}
			else
			{
				field.style.background = colors[1];
			}
		}
		else
		{
			field.style.background = colors[1];
		}
	}
}

function checkSelect(field) {
	if (field.value == "-1") {
		field.style.background = colors[1];
	} else {
		field.style.background = colors[2];
	}
}

function checkNummer(field) {
	if(field.value != '')
	{
		if (field.value!= field.value.match(/-?[0-9]*\.?[0-9]+/)) {
			field.style.background = colors[1];
		} else {
			field.style.background = colors[2];
		}
	}
	else
	{
		field.style.background = colors[2];
	}
}

function controleerCheckbox(checkbox1,checkbox2)
{
	if($(checkbox1).checked || $(checkbox2).checked)
	{
		return true;
	}
	else
	{
		alert('parameter has to be autorised or raw or both.');
		return false;
	}
}


function hide(divid,cellid,height)
{

	document.getElementById(divid).style.visibility='hidden';
	document.getElementById(divid).style.height='1px';
	document.getElementById(cellid).innerHTML = '<img src="' +plaatjesurl+ 'plus.gif" onmouseover="this.style.cursor=\'hand\';this.src=\'' +plaatjesurl+ 'plus_o.gif\'" onmouseout="this.src=\'' +plaatjesurl+ 'plus.gif\'" onclick="show(\''+divid+'\',\''+cellid+'\','+height+')">';
}

function show(divid,cellid,height)
{
	document.getElementById(divid).style.visibility = 'visible';
	document.getElementById(divid).style.height= height+'px';
	document.getElementById(cellid).innerHTML = '<img src="' +plaatjesurl+ 'min.gif" onmouseover="this.style.cursor=\'hand\';this.src=\'' +plaatjesurl+ 'min_o.gif\'" onmouseout="this.src=\'' +plaatjesurl+ 'min.gif\'" onclick="hide(\''+divid+'\',\''+cellid+'\','+height+')">';
}

function hide_klein(divid,cellid,height)
{

	document.getElementById(divid).style.visibility='hidden';
	document.getElementById(divid).style.height='1px';
	document.getElementById(cellid).innerHTML = '<img src="' +plaatjesurl+ 'plus_klein.gif" onmouseover="this.style.cursor=\'hand\';this.src=\'' +plaatjesurl+ 'plus_klein_o.gif\'" onmouseout="this.src=\'' +plaatjesurl+ 'plus_klein.gif\'" onclick="show_klein(\''+divid+'\',\''+cellid+'\','+height+')">';
}

function show_klein(divid,cellid,height)
{
	document.getElementById(divid).style.visibility = 'visible';
	document.getElementById(divid).style.height= height+'px';
	document.getElementById(cellid).innerHTML = '<img src="' +plaatjesurl+ 'min_klein.gif" onmouseover="this.style.cursor=\'hand\';this.src=\'' +plaatjesurl+ 'min_klein_o.gif\'" onmouseout="this.src=\'' +plaatjesurl+ 'min_klein.gif\'" onclick="hide_klein(\''+divid+'\',\''+cellid+'\','+height+')">';
}

function hideverticaal(divid,cellid,cellid2,width)
{
	veranderZoekbalkEvent('verborgen')
	document.getElementById(divid).style.visibility='hidden';
	document.getElementById(divid).style.width='1px';
	document.getElementById(cellid2).style.width='1px';
	document.getElementById(cellid).innerHTML = '<img src="' +plaatjesurl+ 'knop_balk_open.gif" onmouseover="this.src=\'' +plaatjesurl+ 'knop_balk_open_o.gif\';this.style.cursor=\'hand\';" onmouseout="this.src=\'' +plaatjesurl+ 'knop_balk_open.gif\'" onClick="this.src=\'' +plaatjesurl+ 'knop_balk_open_c.gif\';showverticaal(\''+divid+'\',\''+cellid+'\',\''+cellid2+'\','+width+')">';
}

function showverticaal(divid,cellid,cellid2,width)
{
	veranderZoekbalkEvent('getoond')
	document.getElementById(divid).style.visibility = 'visible';
	document.getElementById(divid).style.width= width+'px';
	document.getElementById(cellid).innerHTML = '<img src="' +plaatjesurl+ 'knop_balk_sluiten.gif" onmouseover="this.src=\'' +plaatjesurl+ 'knop_balk_sluiten_o.gif\';this.style.cursor=\'hand\';" onmouseout="this.src=\'' +plaatjesurl+ 'knop_balk_sluiten.gif\'" onClick="this.src=\'' +plaatjesurl+ 'knop_balk_sluiten_c.gif\';hideverticaal(\''+divid+'\',\''+cellid+'\',\''+cellid2+'\','+width+')">';
}

function veranderInputMogelijkheid(cellId,classificatie,type,inputId,waarde)
{
	if(classificatie != confidential && classificatie != secret)
	{
		switch(type)
		{
			case('nummer'):
				$(cellId).innerHTML = '<input type="text" name="' + inputId + '" id="free" value="' + waarde + '" style="width:100%;" onblur="checkNummer(this);"/>';
			break;
			case('tekst'):				
				$(cellId).innerHTML = '<input type="text" name="' + inputId + '" id="free" value="' + waarde + '" style="width:100%;"/>';
			break;
			case('array'):
				$(cellId).innerHTML = '<textarea name="' + inputId + '" id="free" style="width:100%;" rows="3">' + waarde + '</textarea>';
			break;
		}	
	}
	else
	{
		$(cellId).innerHTML = '&nbsp;';
	}
}

function checkVoorwaarde(form,voorwaarde)
{
	if(($('niveau').value >= voorwaarde) || (voorwaarde == 0))
	{
		$('niveau').value = '0';
		return checkForm(form);
	}
	else
	{
		if(voorwaarde == 1)
		{
			alert('you are not logged in.');	
		}
		if(voorwaarde == 2 || voorwaarde == 3)
		{
			alert('your ANR number is not valid.');	
		}
		
		return false;
	}
}

var stopGrowing = [];
var stopShrinking = [];

function toggleGrow(containerId, containedId)
{
	if(stopGrowing[containerId])
	{
		grow(containerId, containedId, false);
	} 
	else if(stopShrinking[containerId])
	{
		shrink(containerId, containedId, false);
	}
	else
	{
		grow(containerId, containedId, false);
	}
}

function grow(containerId, containedId, recurseCall)
{
	var container = document.getElementById(containerId);
	var contained = document.getElementById(containedId);
	
	if(stopGrowing[containerId] && recurseCall)
	{
		stopGrowing[containerId] = false;	
		return;
	}
	
	if(container.style.display == 'none')
	{
		container.style.height = '1px';
		container.style.display = 'block';
	}
	
	stopGrowing[containerId] = false;
	stopShrinking[containerId] = true;
	
	if(container.offsetHeight < contained.offsetHeight)
	{
		var curHeight = parseInt(container.style.height);
		var newHeight = curHeight + 2;

		container.style.height = newHeight.toString() + 'px';
		contained.style.top = newHeight - contained.offsetHeight + 'px';

		setTimeout('grow(\''+containerId+'\',\''+containedId+'\', true);', 10);
	}
}

function shrink(containerId, containedId, recurseCall)
{
	var container = document.getElementById(containerId);
	var contained = document.getElementById(containedId);
	
	if(stopShrinking[containerId] && recurseCall)
	{
		stopShrinking[containerId] = false;	
		return;
	}
	
	stopShrinking[containerId] = false;	
	stopGrowing[containerId] = true;

	if(container.offsetHeight > 1)
	{
		var curHeight = parseInt(container.style.height);
		var newHeight = curHeight - 2;
		
		if(newHeight < 1)
		{
			newHeight = 1;
		}		

		container.style.height = newHeight.toString() + 'px';
		contained.style.top = -(contained.offsetHeight - newHeight) + 'px';

		setTimeout('shrink(\''+containerId+'\',\''+containedId+'\', true);', 10);
	}
	else
	{
		container.style.display = 'none';
	}
}