// Init variables
	var current = "block1form";

// Block switch for opening/closing DIVs
	function SwitchBlock(id) {
		if (current != id && current != "") {
			document.getElementById(current).style.display = "none";
		}
		document.getElementById(id).style.display == "block" ? document.getElementById(id).style.display = "none" : document.getElementById(id).style.display = "block";
		
	current = id;
	}

// NAW gegevens
function CheckBlock1(formName) {
	Block1_OK = "0";
	var geboortedatumcheck_d = "OK";
	var geboortedatumcheck_m = "OK";
	var geboortedatumcheck_j = "OK";

	for (i=0; i < document[formName].length; i++) {
		switch (document[formName].elements[i].name) {
			case "aanhef":
				if (document[formName].elements[i].value == "") {
					document.getElementById("req_aanhef").className = "incorrect";
					Block1_OK = "1";
				}
				else {
					document.getElementById("req_aanhef").className = "required";
				}
				break;

			case "voornaam": 
				var regexp = /[a-zA-Z ]{2,}/i;
				if (!regexp.exec(document[formName].elements[i].value)) {
					document.getElementById("req_voornaam").className = "incorrect";
					Block1_OK = "1";
				}
				else {
					document.getElementById("req_voornaam").className = "required";
				}
				break;

			case "voorletters": 
				var regexp = /[a-zA-Z ]{1,}/i;
				if (!regexp.exec(document[formName].elements[i].value)) {
					document.getElementById("req_voorletters").className = "incorrect";
					Block1_OK = "1";
				}
				else {
					document.getElementById("req_voorletters").className = "required";
				}
				break;

			case "roepnaam": 
				var regexp = /[a-zA-Z ]{2,}/i;
				if (!regexp.exec(document[formName].elements[i].value)) {
					document.getElementById("req_roepnaam").className = "incorrect";
					Block1_OK = "1";
				}
				else {
					document.getElementById("req_roepnaam").className = "required";
				}
				break;

			case "achternaam": 
				var regexp = /[a-zA-Z ]{2,}/i;
				if (!regexp.exec(document[formName].elements[i].value)) {
					document.getElementById("req_achternaam").className = "incorrect";
					Block1_OK = "1";
				}
				else {
					document.getElementById("req_achternaam").className = "required";
				}
				break;

			case "adres": 
				var regexp = /[a-zA-Z ]{3,}/i;
				if (!regexp.exec(document[formName].elements[i].value)) {
					document.getElementById("req_adres").className = "incorrect";
					Block1_OK = "1";
				}
				else {
					document.getElementById("req_adres").className = "required";
				}
				break;

			case "huisnummer": 
				var regexp = /[0-9a-zA-Z ]{1,}/i;
				if (!regexp.exec(document[formName].elements[i].value)) {
					document.getElementById("req_huisnummer").className = "incorrect";
					Block1_OK = "1";
				}
				else {
					document.getElementById("req_huisnummer").className = "required";
				}
				break;

			case "postcode":
				var regexp = /[0-9]{4}[ ]{0,1}[a-zA-Z]{2}/i;
				if (!regexp.exec(document[formName].elements[i].value)) {
					document.getElementById("req_postcode").className = "incorrect";
					Block1_OK = "1";
				}
				else {
					document.getElementById("req_postcode").className = "required";
				}
				break;
		
			case "woonplaats": 
				var regexp = /[a-zA-Z ]{2,}/i;
				if (!regexp.exec(document[formName].elements[i].value)) {
					document.getElementById("req_woonplaats").className = "incorrect";
					Block1_OK = "1";
				}
				else {
					document.getElementById("req_woonplaats").className = "required";
				}
				break;

			case "telefoon": 
				var regexp = /[0-9- ()]{10,}/i;
				if (!regexp.exec(document[formName].elements[i].value)) {
					document.getElementById("req_telefoon").className = "incorrect";
					Block1_OK = "1";
				}
				else {
					document.getElementById("req_telefoon").className = "required";
				}
				break;

			case "emailadres": 
				var regexp = /[0-9a-zA-Z.-]{2,}@[0-9a-zA-Z.-]{2,}.[a-z]{2,4}/i;
				if (!regexp.exec(document[formName].elements[i].value) || document[formName].elements[i].value == "") 				{
				document.getElementById("req_emailadres").className = "incorrect";
					Block1_OK = "1";
				}
				else {
					document.getElementById("req_emailadres").className = "required";
				}
				break;

			case "geboortedatum_d":
				var dag = document[formName].elements[i].value;
				if (parseFloat(dag) < 1 || parseFloat(dag) > 31 || dag == "" || dag == "DD") {
					geboortedatumcheck_d = "notOK";
				}
				break;

			case "geboortedatum_m":
				var maand = document[formName].elements[i].value;
				if (parseFloat(maand) < 1 || parseFloat(maand) > 12 || maand == "" || maand == "MM") {
				geboortedatumcheck_m = "notOK";
				}
				break;

			case "geboortedatum_j":
				var jaar = document[formName].elements[i].value;
				if (parseFloat(jaar) < 1900 && parseFloat(jaar) > 2100 || jaar == "JJJJ") {
					geboortedatumcheck_j = "notOK";
				}
				break;
		}
	}

	// Geboortedatum
	if (geboortedatumcheck_d == "notOK" || geboortedatumcheck_m == "notOK" || geboortedatumcheck_j == "notOK") {
		document.getElementById("req_geboortedatum").className = "incorrect";
		Block1_OK = "1";
	}
	else {
		document.getElementById("req_geboortedatum").className = "required";
	}

	if (Block1_OK == "1") {
		document.getElementById("block1validate").className = "vbad";
	}
	else {
		document.getElementById("block1validate").className = "vgood";
		SwitchBlock("block2form");
	}
}


// Motivatie + brief
function CheckBlock2(formName) {
	Block2_OK = "0";

	if (document[formName].motivatie.value == "" && document[formName].brief.value == "") {
		document.getElementById("req_motivatie").className = "incorrect";
		document.getElementById("req_brief").className = "incorrect";
		Block2_OK = "1";
	}
	else {
		if (document[formName].brief.value != "") {
			var start = document[formName].brief.value.lastIndexOf(".") + 1;
			var length = (document[formName].brief.value.length - 1) - document[formName].brief.value.lastIndexOf(".");
			var filetype = document[formName].brief.value.substr(start,length);
			
			if (filetype == "doc" || filetype == "pdf" || filetype == "htm" || filetype == "html" || filetype == "txt") {
				document.getElementById("req_brief").className = "required";
				document.getElementById("req_motivatie").className = "required";
			}
			else {
				document.getElementById("req_brief").className = "incorrect";
				document.getElementById("req_motivatie").className = "incorrect";
				Block2_OK = "1";
			}
		}
		
		if (document[formName].motivatie.value != "") {
			document.getElementById("req_motivatie").className = "required";
			document.getElementById("req_brief").className = "required";
		}
	}

	if (Block2_OK == "1") {
		document.getElementById("block2validate").className = "vbad";
	}
	else {
		document.getElementById("block2validate").className = "vgood";
		SwitchBlock("block3form");
	}
}

// CV
function CheckBlock3(formName) {
	Block3_OK = "0";
	if (document[formName].cv.value != "") {
		var start = document[formName].cv.value.lastIndexOf(".") + 1;
		var length = (document[formName].cv.value.length - 1) - document[formName].cv.value.lastIndexOf(".");
		var filetype = document[formName].cv.value.substr(start,length);
					
		if (filetype == "doc" || filetype == "pdf" || filetype == "htm" || filetype == "html" || filetype == "txt") {
			document.getElementById("req_cv").className = "required";
		}
		else {
			document.getElementById("req_cv").className = "incorrect";
		}
	}else {
		document.getElementById("req_cv").className = "incorrect";
		Block3_OK = "1";
	}
	
	if (document[formName].cijferlijst.value == "") {
		var start = document[formName].cijferlijst.value.lastIndexOf(".") + 1;
		var length = (document[formName].cijferlijst.value.length - 1) - document[formName].cijferlijst.value.lastIndexOf(".");
		var filetype = document[formName].cijferlijst.value.substr(start,length);
					
		if (filetype == "doc" || filetype == "pdf" || filetype == "htm" || filetype == "html" || filetype == "txt") {
			document.getElementById("req_cijferlijst").className = "required";
		}
		else {
			document.getElementById("req_cijferlijst").className = "incorrect";
		}
	}else {
		document.getElementById("req_cijferlijst").className = "incorrect";
		Block3_OK = "1";
	}

	if (Block3_OK == "1") {
		document.getElementById("block3validate").className = "vbad";
	}else {
		Block3_OK = "0";
		document.getElementById("block3validate").className = "vgood";
		SwitchBlock("block4form");
	}
}

// Bron
function CheckBlock6(formName) {
	Block6_OK = "0";
	if (document[formName].bron.value == "") {
		document.getElementById("req_bron").className = "incorrect";
		Block6_OK = "1";
	}
	else {
		if (document[formName].bron.value == "anders" && document[formName].anders.value == "") {
			document.getElementById("req_bron").className = "incorrect";
			Block6_OK = "1";		
		}
		else {
			document.getElementById("req_bron").className = "required";
		}
	}

	if (Block6_OK == "1") {
		document.getElementById("block6validate").className = "vbad";
	}
	else {
		document.getElementById("block6validate").className = "vgood";
		SwitchBlock("block7form");
	}
}

function CheckAll(formName,send) {
	if (send == "0") {
		CheckBlock1(formName);
		CheckBlock2(formName);
		CheckBlock3(formName);
		CheckBlock6(formName);
	}
	else if (send == "1") {
		if (Block1_OK == "0" && Block2_OK == "0" && Block3_OK == "0" && Block6_OK == "0" && send == "1") {
			document[formName].submit();
		}
	}
}

// values
var d = "00";
var m = "00";
var j = "0000";
var adres = "";
var huisnummer = "";
var postcode = "";
var woonplaats = "";

function GetValue(field) {
// Exception adres + huisnummer
	if (field.name == "adres") {
		adres = field.value;
	}
	if (field.name == "huisnummer") {
		huisnummer = field.value;
	}

// Exception postcode + plaats
	if (field.name == "postcode") {
		postcode = field.value;
	}
	if (field.name == "woonplaats") {
		woonplaats = field.value;
	}


// Exception geboortedatum
	if (field.name == "geboortedatum_d") {
		d = field.value;
	}
	if (field.name == "geboortedatum_m") {
		m = field.value;
	}
	if (field.name == "geboortedatum_j") {
		j = field.value;
	}

// Exception dienstverband

	if (field.name == "dienstverband") {
		var dienstverband = "";
		for (i=0; i < field.options.length ; i++) {
			if (field.options[i].selected == true) {
				dienstverband += ","+field.options[i].value+" ";
			}
		}
		dienstverband = dienstverband.substr(1, dienstverband.length-1);

	}

// Write in result
	if (field.name == "adres" || field.name == "huisnummer") {
		document.opensollicitatie.result_adres.value = adres+" "+huisnummer;
	}

	else if (field.name == "postcode" || field.name == "woonplaats") {
		document.opensollicitatie.result_postcodeplaats.value = postcode+"  "+woonplaats;
	}
	
	else if (field.name == "opleidingsniveau") {
		document.opensollicitatie.result_opleidingsniveau.value = opleidingsniveau;
	}

	else if (field.name == "geboortedatum_d" || field.name == "geboortedatum_m" || field.name == "geboortedatum_j") {
		document.opensollicitatie.result_geboortedatum.value = d+"-"+m+"-"+j;
	}
	else if (field.name == "dienstverband") {
		document.opensollicitatie.result_dienstverband.value = dienstverband;
	}else {
		result = eval("document.opensollicitatie.result_"+field.name);
		var tmp_length = field.value.length;
		if(tmp_length > 50) {
			result.value = field.value.slice((tmp_length - 50));
		}else {
			result.value = field.value;
		}
	}
}