﻿$(document).ready(function() {

    $('.m48 .main-button').click(function(e) {
		var isValid = true;
		
        if ($('.m28-1').size() > 0) {
			$('.m28-1 input[name*=tboxPlace]').each(function() {
				var $t = $(this);
				if ( !$t[0].disabled && $t.val() != '' ) {
					var jsonStr = JSON.stringify({ placeName:$t.val() });
					var result = $.ajax({
						async: false,
						type: 'POST',
						url: '/_vti_bin/PortalSearchService.asmx/IsPlaceValid',
						data: jsonStr,
						contentType: "application/json; charset=utf-8",
						dataType: 'json',
						dataFilter: function(data, type) {
							data = JSON.parse(data);
							return data.d;
						},
						success: function(data, textStatus, XMLHttpRequest) {
							isValid = data;
						}
					});
					
					if ( !isValid ) {
						if ( !$t.parents('.place').hasClass('alert') ) {
							$t.parents('.place').addClass('alert').prepend('<em>De gemeente of woonplaats is niet gevonden</em>');
						}
					} else {
						if ( $t.parents('.place').hasClass('alert') ) {
							$t.parents('.place').removeClass('alert').find('em').remove();
						}
					}
					
				}
			});
        }
		
		if ( !isValid ) {
			e.preventDefault();
			return false;
		}
    });

});