// Voorwaarden
function openWindow() {
	window.open("http://www.testjegezondheid.nl/algemene-voorwaarden/","Algemene_voorwaarden","width=1024,height=600,toolbar=no,scrollbars=yes");
	return false;
}

function findNearestLocation(lat,lng) {
	jQuery.post(""+document.location.href ,{'nearestlocation':1,'lat':lat,'lng':lng},function(data) {
		if (data != '') {
			selectedlocation = jQuery("#loc_option_"+data).attr("selected","selected").text();
			
			jQuery('#kies_locatie').html("<strong>Locatie het dichtst bij u in de buurt:<br /><br />"+selectedlocation+"</strong><br /><br />Wilt u op een andere locatie de test laten uitvoeren, kies een locatie uit de volgende lijst:");
		}
		//jQuery("#testplaats").html(data)
	});	
}

document.write('<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>');
function geocodeAddress(address,callback) {		
	var geocoder = new google.maps.Geocoder();
	geocoder.geocode( {
		'address': postcode
	}, function(results, status) {
		callback(results[0].geometry.location.lat(),results[0].geometry.location.lng())		
	});	
}

var geocodeTimer = null;
function checkAddress() {
	try {
		clearTimeout(geocodeTimer)
	} catch (e) {}

	straat = jQuery("#straatnaam").val()
	huisnr = jQuery("#huisnummer").val()
	plaats = jQuery("#plaats").val()
	postcode = jQuery("#postcode").val()
	address = '';
	if (straat != '' && huisnr != '' && plaats != '') {
		address = straat+" "+huisnr+" "+plaats;			
	}
	else if (postcode != '' && postcode.length > 5) {
		address = postcode+" nederland";			
	}			

	if (address != '') {
		geocodeTimer = setTimeout(function() {
			geocodeTimer = geocodeAddress(address, findNearestLocation)
		},2000);
	}
}


$().ready(function(){
	jQuery("#straatnaam,#huisnummer,#postcode,#plaats").live("click",function() {
		checkAddress();
	})
	jQuery("#straatnaam,#huisnummer,#postcode,#plaats").live("keyup",function() {
		checkAddress();
	})
	

	$("#aanvraag").validate({
		rules: {
			nuchter: "required",
			voorletters: "required",
			achternaam: {
				required: true,
				minlength: 2
			},
			straatnaam: "required",
			huisnummer: "required",
			postcode: {
				required: true,
				minlength: 6
			},
			geboortedag: "required",
			geboortemaand: "required",
			geboortejaar: "required",
			plaats: {
				required: true,
				minlength: 2
			},
			telefoon: {
				required: true,
				minlength: 10
			},
			email: {
				required: true,
				email: true
			},
			voorwaarden: "required",
			testplaats: "required"
		},
		messages: {
			nuchter: "Geef aan of u de test wel of niet nuchter wilt nemen",
			voorletters: "Verplicht",
			achternaam: {
				required: "Vul alstublieft uw achternaam in",
				minlength: "Uw achternaam dient uit minimaal 2 karakters te bestaan"
			},
			straatnaam: "Vul alstublieft uw straatnaam in",
			huisnummer: "Verplicht",
			postcode: {
				required: "Verplicht",
				minlength: "####AA"
			},
			geboortedag: "Vul alstublieft een geboortedatum in.",
			geboortemaand: "",
			geboortejaar: "",
			plaats: {
				required: "Vul alstublieft uw woonplaats in",
				minlength: "Uw woonplaats dient uit minimaal 2 karakters te bestaan"
			},
			telefoon: {
				required: "Vul alstublieft uw telefoonnummer in",
				minlength: "Uw telefoonnummer dient uit minimaal 10 karakters te bestaan"
			},
			email: "Vul alstublieft een geldig e-mailadres in",
			voorwaarden: "&nbsp;U dient akkoord te gaan met onze algemene voorwaarden",
			testplaats: "Kies een locatie waar u geprikt wilt worden"
		}
	});
});

