$(function(){
	if($.browser.msie && $.browser.version >= 6)
	{
		// $(".ui-corner-all").prepend('<div class="tl"></div><div class="tr"></div>').append('<div class="bl"></div><div class="br"></div>');
	}
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		keyboard_shortcuts: false,
		theme: 'light_square',
		show_title: true,
		modal: false
	});
	// $('td.productListing-data').each(function(){
		// //console.info($(this).find('0.00'));
		// if($(this).text()== '&nbsp;$0.00&nbsp;'){
			// $(this).text('op aanvraag');
		// }
	// });
	var w = $(document).width();
	var h = $(document).height();

	$('#loadingLayer').width(w).height(h);
	
	$('ul.categories a').click(function(){
		$('#loadingLayer').show().fadeTo(0, 0.8);		
	});
	
	$('.kenteken-invoer').focus();
	
	$('input:submit').click(function(){
		if($('#kenteken').val()!=''){
			return true;
		}else{
			return false;
		}
	});
	
	$("#nav li").each(function(){
	  	if($(this).text() == ""){
			$(this).remove();
		}
	});
	
	$("#model .year li a").click(function(){
		var val =  $(this).attr("name");
		$("#modeldata tbody tr").each(function(){
			var year = $(this).attr("name");
			if (val == "2010 en later") val = new Date().getFullYear();
			$(this).show();
			if (val != "Alle"){
				if (year != undefined){
					var split = year.split(" ");
					if (!between ( val, split ) ){
						$(this).hide();
					}
				}
			}
		});
	});
	
	$("#type .year li a").click(function(){
		var val =  $(this).attr("name");
		$("#typedata tbody tr").each(function(){
			var year = $(this).attr("name");
			if (val == "2010 en later") val = new Date().getFullYear();
			$(this).show();
			if (val != "Alle"){
				if (year != undefined){
					var split = year.split(" ");
					if (!between ( val, split ) ){
						$(this).hide();
					}
				}
			}
		});
	});
	
	$('.tr-over tbody tr').hover(
	  function () {
	    $(this).addClass("hover");
	  },
	  function () {
	    $(this).removeClass("hover");
	  }
	);
	
	$('.tr-over tbody tr').click(function(){
		var url = $(this).find("td a").attr("href");
		window.open(url, "_self");
	});
	
	$('ul.year li, ul.make li, ul.menu li').click(function(){
		var url = $(this).find("a").attr("href");
		window.open(url, "_self");
	});
	
	$('#type ul li, #model ul li').hover(
	  function () {
	    $(this).addClass("hover");
	  },
	  function () {
	    $(this).removeClass("hover");
	  }
	);

	
});

function between ( year, data ){
	var curr;
	var start = Number(data[0]);
	var end = Number(data[1]);
	var date = new Date();
	if (end == '') end = date.getFullYear();
	
	if (year == "Voor 1986"){
		curr = 1986;
		if ( end <= curr ){
			return true;
		}
		else{
			return false;}
	}else{
		curr = Number(year);
		if ( curr >= start && curr <= end )
			{return true;}
		else
			{return false;	}	
	}
}


function isNum(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 checkNewLicenseFormat(license){
	var s = '';
	for (i = 0; i <= license.length-1; i++)	{
	  	var n = license.charCodeAt(i);
	  	if (((n >= 65) && (n <= 90)) || ((n >= 48) && (n <= 57))){
			s = s + license.charAt(i);
	  	}
	}
	if (s.length != 6) {
		return license;
	}
	if ((isNum(s.charAt(4)) == 0) && (isNum(s.charAt(5)) == 1))	{
		license = s.charAt(0) + s.charAt(1) + '-' + s.charAt(2) + s.charAt(3) + s.charAt(4) + '-' + s.charAt(5);
	}
	return license;
}

function checkLicense(e) {
	//console.log('e');
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);      
	if (key == 8) {
		return;
	}
	if (key == 13) {
	//	getLicenseData();
	}

	var license = document.licenseForm.kenteken.value;
	license = license.toUpperCase();

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

	s = checkNewLicenseFormat(s);
	document.licenseForm.kenteken.value = s;
}

