function licence_send() {

		_gaq.push(['_trackEvent', "Zoek Onderdelen", 'click', $('.kenteken').val()])


		if($('.kenteken').val() > '') {

				$.modal('<center><img src="/images/loading.gif" /></center> <br />' +
						'<p id="load_licence_msg" style="text-align: center">De gegevens van uw kenteken worden verzameld.. een moment geduld a.u.b. <br /> <small style="font-size: 11px; color: #666">Het opvragen van uw kenteken kan enkele seconden duren, afhanklijk van uw internetverbinding & de snelheid van de externe kenteken-service</small></p>', {
								containerCss:{
										backgroundColor:"#fff",
										height:150
								}
						});

				// Na 15 seconden alert dat het te lang duurt
				setTimeout(delayHandler, 15000);
				
				window.location = '/kenteken/' + document.licence_form.licence.value;
        
		} else {

				if($('.error-notice').length > 0) {
						$('.error-notice').remove();
				}

				$('#licence_form').before('<p style="width: 92%; background: #FFF;" class="error-notice"><span class="notice-icon"></span> U bent vergeten een kenteken in te vullen, om de juiste onderdelen te zoeken hebben wij uw kenteken nodig. <a style="display: inline" class="weet_ik_niet" href="/kenteken/">Zonder kenteken winkelen?</a></p>');

		}


}

function delayHandler() {
		$('#load_licence_msg').html('Het laden van uw kenteken duurt te lang, druk op de onderstaande knop op het nogmaal te proberen');
		$('#load_licence_msg').after('<br /> <center><a href="javascript:void(0)" onclick="javascript:close_loading_msg()" class="button-blue-input" style="float: none; margin: 0px;">Nogmaals proberen</a></center>');
}

function close_loading_msg() {
		close_modal();
		setTimeout(licence_send, 1000)
}

function is_int(s)
{
		if ((s == '0') ||
				(s == '1') ||
				(s == '2') ||
				(s == '3') ||
				(s == '4') ||
				(s == '5') ||
				(s == '6') ||
				(s == '7') ||
				(s == '8') ||
				(s == '9')) {
				return 1;
		} else {
				return 0;
		}
}

function check_new_licence_format(licence)
{

		var s = '';
		for (i = 0; i <= licence.length-1; i++)
		{
				var n = licence.charCodeAt(i);
				if (((n >= 65) && (n <= 90)) || ((n >= 48) && (n <= 57)))
				{
						s = s + licence.charAt(i);
				}
		}
		if (s.length != 6) {
				return licence;
		}

		if ((is_int(s.charAt(4)) == 0) && (is_int(s.charAt(5)) == 1))
		{
				licence = s.charAt(0) + s.charAt(1) + '-' + s.charAt(2) + s.charAt(3) + s.charAt(4) + '-' + s.charAt(5);
		}

		return licence;
}


function check_licencex(e)
{
		var key = window.event ? e.keyCode : e.which;
		var keychar = String.fromCharCode(key);
		if (key == 8) {
				return;
		}
		if (key == 13) {
				licence_send()
				}

		var licence = document.licence_form.licence.value;
		licence = licence.toUpperCase();

		s = '';
		m = 0;
		for (i = 0; i <= licence.length-1; i++)
		{
				n = licence.charCodeAt(i);
				if (((n >= 65) && (n <= 90)) || ((n >= 48) && (n <= 57)))
				{
						s = s + licence.charAt(i);
						m = m + 1;
						if (m == 2) {
								s = s + '-';
						}
						if (m == 4) {
								s = s + '-';
						}
				}
		}

		s = check_new_licence_format(s);
		document.licence_form.licence.value = s;
}

//***
function insert_char(s, behind_char_pos, ch)
{
		var s1 = s.substr(0, behind_char_pos)
		var s2 = s.substr(behind_char_pos);

		return s1 + ch + s2;
}


function check_licence(e)
{

		if( navigator.userAgent.match(/Android/i) ||  navigator.userAgent.match(/webOS/i) ||   navigator.userAgent.match(/iPad/i) ||  navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/BlackBerry/)) {
				// Niks doen
		} else {

				var key = window.event ? e.keyCode : e.which;
				if (key == 8) {
						return;
				}
				if (key == 13) {
						licence_send()
						}

				var licence = $('.kenteken').val();

				//IE: 189, FF: 109
				if ((key == 189) || (key == 109))
				{
						var dash_count = 0;
						for (i = 0; i <= licence.length-1; i++)
						{
								n = licence.charCodeAt(i);
								if (n == 45)
								{
										dash_count++;
								}
						}
						if (dash_count <= 2)
						{
								return;
						}
				}

				licence = licence.toUpperCase();

				licence_format = '';
				licence2 = '';
				for (i = 0; i <= licence.length-1; i++)
				{
						n = licence.charCodeAt(i);

						// A-Z
						if ((n >= 65) && (n <= 90))
						{
								licence_format = licence_format + 'A';
								licence2 = licence2 + licence.charAt(i);
						}

						// 0-9
						if ((n >= 48) && (n <= 57))
						{
								licence_format = licence_format + '0';
								licence2 = licence2 + licence.charAt(i);
						}
				}

				//alert(licence_format);

				licence = licence2.substr(0, 6);

				if (licence_format.length <= 2)
				{
						$('.kenteken').val(licence);
						return;
				}

				if (licence_format.length <= 4)
				{
						licence = insert_char(licence, 2, '-');
						$('.kenteken').val(licence);
						return;
				}

				if (
						(licence_format == '0AAA00')
						)
						{
						licence = insert_char(licence, 1, '-');
						licence = insert_char(licence, 5, '-');
						$('.kenteken').val(licence);
						return;
				}

				if (
						(licence_format == '00AAA0')
						)
						{
						licence = insert_char(licence, 2, '-');
						licence = insert_char(licence, 6, '-');
						$('.kenteken').val(licence);
						return;
				}

				licence = insert_char(licence, 2, '-');
				licence = insert_char(licence, 5, '-');

				$('.kenteken').val(licence);
				return;

		}

}
 
