/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vacature toggler
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

//show or hide vacature elements
function toggle()
{	

	//then find the direct reageren button in the div
	sd = document.getElementById('stuurdoor_btn');
	
	if(sd) {
		//when someone clicks this one
		sd.onclick = function()
		{

			//find the matching form
			sd_form = document.getElementById('stuurdoor');

			//show or hide it
			sd_form.style.display = (sd_form.style.display == 'none') ? 'block' : 'none';

			//break the href
			return false;	

		}

		//find the close button
		cb = document.getElementById('stuurdoor_close');

		if(cb)
		{

			//click the close button
			cb.onclick = function()
			{

				//find the matching form
				sd_form = document.getElementById('stuurdoor');

				//show or hide it
				sd_form.style.display = (sd_form.style.display == 'none') ? 'block' : 'none';	


			}	

		}
	}
	
	//get all definition titles
	dt = document.getElementsByTagName('dt');
	
	//if there is at least one
	if(dt)
	{
	
		//find em all
		for(i=0; i<dt.length; i++)
		{
				
			//if you click one
			dt[i].onclick = function()
			{
				
					//arrow must rotate when menu collapses
					//so we need the link inside the dt
					
					//find the childnodes of the dt
					dtA = this.childNodes;

					//if there is a link
					if(dtA)
					{
						
						//loop through the childnodes
						for(i=0; i<dtA.length; i++)
						{
						
							//check if it's a link
							if(dtA[i].nodeName == 'A')
							{
														
								//has no background image			
								if(dtA[i].style.backgroundImage == '')
								{
									
									//set the bg
									dtA[i].style.backgroundImage = 'url(images/arrow.gif)';
									
								}
								
								//has a background image
								if(dtA[i].style.backgroundImage == 'url(images/arrow.gif)')
								{
									
									//set the bg
									dtA[i].style.backgroundImage = 'url(images/arrow_rotate.gif)'
										
								}else
								{
								
									//set the bg
									dtA[i].style.backgroundImage = 'url(images/arrow.gif)'
									
								}
							
							}
							
						}
					
					}
				
				//find the class name
				dt_class = this.className;
				
				//find the matching definition description
				dd = document.getElementById(dt_class);

				//show or hide it
				dd.style.display = (dd.style.display == 'none') ? 'block' : 'none';
				
				/*

				//then find the direct reageren button in the div
				sd = document.getElementById(dt_class +'_stuurdoor_btn');
				
				//when someone clicks this one
				sd.onclick = function()
				{
				
					//find the matching form
					sd_form = document.getElementById(this.id.substr(0,15));
					
					//show or hide it
					sd_form.style.display = (sd_form.style.display == 'none') ? 'block' : 'none';
					
					//break the href
					return false;	
					
				}
				
				//find the close button
				cb = document.getElementById(dt_class + '_stuurdoor_close');
				
				if(cb)
				{
				
					//click the close button
					cb.onclick = function()
					{
					
						//find the matching form
						sd_form = document.getElementById(this.id.substr(0,15));
					
						//show or hide it
						sd_form.style.display = (sd_form.style.display == 'none') ? 'block' : 'none';	
						
						
					}	
					
				}*/

				//here too
				return false;
			
			}
			
		}
		
	}
	
}


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Initiate forms
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

//initiate form elements
function initForms()
{
	//gather all inputs
	f = document.getElementsByTagName('input');
	
	//are there any?
	if(f)
	{
		//loop through the array
		for(i=0; i<f.length; i++)
		{
			
			//see to it it's not the button
			if(f[i].type != "submit" && f[i].type != "file")
			{
			
		
				//click one
				f[i].onclick = function()
				{
				
					//check the value
					if(this.value == this.defaultValue)
					{
					
						//set to blank
						this.value = '';	
						
					//if it is blank
					}else if(this.value == '')
					{
					
						//make it the original value
						this.value = this.defaultValue;	
						
					//if it's the error message
					}else if(this.value == error_message)
					{
					
						//make it the original value
						this.value = this.defaultValue;	
						
					}
					
				}
				
			}
			
		}
		
	}
	
	
	
	//gather all textareas
	t = document.getElementsByTagName('textarea');
	
	//are there any?
	if(t)
	{
		//loop through the array
		for(i=0; i<t.length; i++)
		{
			
			//click one
			t[i].onclick = function()
			{
			
				//check the value
				if(this.value == this.defaultValue)
				{
				
					//set to blank
					this.value = '';	
					
				//if it is blank
				}else if(this.value == '')
				{
				
					//make it the original value
					this.value = this.defaultValue;	
					
				//if it's the error message
				}else if(this.value == error_message)
				{
				
					//make it the original value
					this.value = '';	
					
				}
				
			}
			
		}
		
	}
	
}


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Validate forms
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/


//set error message
var error_message = "Dit is een verplicht veld";
var error_email_message = "Je e-mailadres klopt niet";

function checkform(formName)
{
	
	//get all form input fields
	d = formName.getElementsByTagName('input');

	//delete errors
	error = false;
	
	if(d)
	{
	
		//loop through the inputs
		for(i=0; i<d.length; i++)
		{		
			
			//see to it it's not the button
			if(d[i].type != "submit" && d[i].type!='hidden')
			{
				
				//and not the browse thing	
				if(d[i].type != "file" && d[i].type !='checkbox' && d[i].getAttribute('rel') != 'not_mandatory')
				{				
		
					//empty
					if(d[i].value == "")
					{
						
						//set error
						d[i].value = error_message;
						
						error = true;
						
					//default value
					}else if(d[i].value == d[i].defaultValue)
					{
					
						//set error
						d[i].value = error_message;
						
						error = true;
					
						
					//has already an error	
					}else if(d[i].value == error_message)
					{
						
						//set error
						d[i].value = error_message;
						
						error = true;
					
					}
					
					
					
					
					//check e-mail
					if(d[i].name.indexOf('email') != -1)
					{
						
						
						//get the contents
						e = checkEmail(d[i].value);
						
						//add error
						if(!e)
						{ 
							
							//set error
							d[i].value = error_message;
							
							error = true; 

						}
						
					}
					
										
				
				//if it's the file browse thing	
				}else if(d[i].type=="file")
				{
					
					if(d[i].value == '' && d[i].getAttribute('rel') != 'not_mandatory')
					{
					
						error = true;
							
					}
					
				} else {
					if(d[i].getAttribute('rel')!='not_mandatory' && !d[i].checked) {
						d[i].parentNode.style.backgroundColor='#f63';
						error = true;
					}
				}
				
			}
			//alert(d[i].name+" / "+error);
			
		}
		
	}
	
	
	//get all form input fields
	t = formName.getElementsByTagName('textarea');
	
	if(t)
	{
	
		for(i=0; i<t.length; t++)
		{
		
			if(t[i].getAttribute('rel') != 'not_mandatory') {
				if(t[i].value == "")
				{

					//set error
					t[i].value = error_message;

					error = true;

				}else if(t[i].value == t[i].defaultValue)
				{

					//set error
					t[i].value = error_message;

					error = true;

				}else if(t[i].value == error_message)
				{

					//set error
					t[i].value = error_message;

					error = true;

				}
			}
		}
	
	}
	
	
	
	
	if(error){ 	return false;	}

}

function checkEmail(emailAddress) {

        var foundAtSymbol = 0;
        var foundDot = 0;
        var md;

        for (var x=0; x<emailAddress.length - 1; x++) {
        md = emailAddress.substr(x, 1);

        if (md == '@') foundAtSymbol++;

        if (md == '.' && foundAtSymbol == 1) foundDot++;
        };

        if (foundDot > 0 && foundAtSymbol == 1) {
        return true;
        }
        else{
        return false;
        }
};


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on Load
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
window.onload = function()
{

	toggle();
	
	initForms();
		
}