$(document).ready(function(){
	$('#tblMijnAccount').hide();
	$('#tblLevering').hide();
	$('#tblMijnAccount').removeClass('tblHidden');
	$('#tblLevering').removeClass('tblHidden');
	
	$('#spnAdresEnFacturatie').addClass('clickable').click(function() {
		if ($(this).children().attr('src') == "images/plus_orange.png") {
			$(this).children().attr('src', 'images/minus_orange.png');
			$('#tblMijnAccount').show('slow');
		} else {
			$(this).children().attr('src', 'images/plus_orange.png');
			$('#tblMijnAccount').hide('slow');
		}
	});
	
	$('#spnLevering').addClass('clickable').click(function() {
		if ($(this).children().attr('src') == "images/plus_orange.png") {
			$(this).children().attr('src', 'images/minus_orange.png');
			$('#tblLevering').show('slow');
		} else {
			$(this).children().attr('src', 'images/plus_orange.png');
			$('#tblLevering').hide('slow');
		}
	});

	var submitForm = false;
	//Check if a required field has a value or an email adres is valid
	var checkField = function(object, text, type) {
		switch (type) {
			case 'required':
				if (object.val() == "") {
					object.next().next().text('Het is verplicht je ' + text + ' in te vullen');
					submitForm = false;
					return false;
				} else {
					object.next().next().empty();
					return true;
				}
				break;
			case 'email':
				var reg = /^\w+[\w-\.]*\@\w+((-\w+)|(\w*))\.[a-z]{2,3}$/;
				if (reg.test(object.val()) == false) {
					object.next().next().text('Het e-mail adres is ongeldig');
					submitForm = false;
					return false;
				} else {
					object.next().next().empty();
					return true;
				}
				break;
		}
	}	
	
	var sbmtForm = function () {
		if (submitForm) {
			$.post('ajax.asp', {
				'a': 'showConfirm'
			}, function(data) {
				$('#maBevestiging').addClass('mailexists').html(data);
				$('#popupConfirmSluit').addClass('clickable').click(function() {
					$("#frmMijnAccount").submit();											 
				});
			});
		}
	}		
	
	$('#btnVerzoek').addClass('clickable').click(function() {
		submitForm = true;
		
		//checkField checks if a required field has a value or an email adres is valid
		if ($('#txtBedrijf').val() == null) {
			checkField($('#txtAchternaam'), 'achternaam', 'required');
			checkField($('#txtVoornaam'), 'voornaam', 'required');			
		} else {
			checkField($('#txtBedrijf'), 'bedrijf', 'required');
			checkField($('#txtBTWNummer'), 'BTW-nummer', 'required');
		}

		checkField($('#txtStraat'), 'straat', 'required');
		
		if ($('#txtNummer').val() == "") {
			$('#txtNummer').next().next().next().text('Het is verplicht je huisnummer in te vullen');
			submitForm = false;
		} else {
			$('#txtNummer').next().next().next().empty();
		}
		
		checkField($('#txtPostcode'), 'postcode', 'required');
		checkField($('#txtPlaats'), 'woonplaats', 'required');
		if (checkField($('#txtEmail'), 'email adres', 'required')) {
			checkField($('#txtEmail'), 'email adres', 'email');
		}

		//Only perform some checks when the new password has a value
		if ($('#txtNieuwPaswoord').val() != "") {
			var fieldRequired = true;
			//Check if the old and if the confirm password has a value, when not their label will turn red
			if ($('#txtOudPaswoord').val() == "") {
				$('#txtOudPaswoord').next().next().text(' Vereist bij het veranderen van het wachtwoord');
				fieldRequired = false;
				submitForm = false;
			} else {
				$('#txtOudPaswoord').next().next().empty();
			}
			if ($('#txtBevPaswoord').val() == "") {
				$('#txtBevPaswoord').next().next().text(' Vereist bij het veranderen van het wachtwoord');
				fieldRequired = false;
				submitForm = false;
			} else {
				 $('#txtBevPaswoord').next().next().empty();
			}
			
			//If all required password fields has a value then ...
			if (fieldRequired) {
				if ($('#txtNieuwPaswoord').val() == $('#txtBevPaswoord').val()) {
					//Check if the old password is the password of the user
					$.post('ajax.asp', {
						'a': 'checkPassw',
						'passw': $('#txtOudPaswoord').val()
					}, function(data) {
						if (data == "0") {
							$('#txtOudPaswoord').next().next().text(' Paswoord komt niet overeen met paswoord van de gebruiker');
							$('#txtNieuwPaswoord').next().next().empty();
							$('#txtBevPaswoord').next().next().empty();
							submitForm = false;
						} else {
							sbmtForm();
						}
					});
				} else {
					$('#txtBevPaswoord').next().next().text(' Het nieuw en bevestig paswoord komen niet overeen');
					$('#txtNieuwPaswoord').next().next().text(' Het nieuw en bevestig paswoord komen niet overeen');
					submitForm = false;
				}
			}
		} else {
			sbmtForm();
		}
	});
	
	$('#btnVerzoekLev').addClass('clickable').click(function() {
		submitForm = true;
		
		checkField($('#txtStraatLev'), 'straat', 'required');

		if ($('#txtNummerLev').val() == "") {
			$('#txtNummerLev').next().next().next().text('Het is verplicht je huisnummer in te vullen');
			submitForm = false;
		} else {
			$('#txtNummerLev').next().next().next().empty();
		}
		
		checkField($('#txtPostcodeLev'), 'postcode', 'required');
		checkField($('#txtPlaatsLev'), 'woonplaats', 'required');
		
		sbmtForm();
	});
});




























