
jQuery.fn.typeWatch=function(wait,fire){this.each(function(){var input=this;var timeout;$(input).keyup(function(event){clearTimeout(timeout);timeout=setTimeout(function(){fire(input);},wait);});});return this;}
jQuery.fn.typeWatch2=function(wait,fire){this.each(function(){var input=this;var timeout;$(input).blur(function(event){clearTimeout(timeout);timeout=setTimeout(function(){fire(input);},wait);});$(input).change(function(event){clearTimeout(timeout);timeout=setTimeout(function(){fire(input);},wait);});});return this;}
function addressPrefill(postcodeField,housenumberField,addressField,cityField,countryField){postcodeField=$(postcodeField);housenumberField=$(housenumberField);addressField=$(addressField);cityField=$(cityField);countryField=$(countryField);var usePrefill;countryField.change(function(){if(countryField.val()=="NL"){usePrefill=true;addressField.attr("readonly","readonly");addressField.css("background-color","#BBBBBB");cityField.attr("readonly","readonly");cityField.css("background-color","#BBBBBB");doPrefill();}else{usePrefill=false;addressField.removeAttr("readonly");addressField.css("background-color","#FFFFFF");cityField.removeAttr("readonly");cityField.css("background-color","#FFFFFF");}});countryField.change();function doPrefill(){if(postcodeField.val()&&housenumberField.val()){$.getJSON("/php/address_verify.php?zip="+postcodeField.val()+"&hnr="+housenumberField.val()+"&r="+Math.ceil(99999999*Math.random()),function(data){if(data.NAK){addressField.val("");cityField.val("");}else if(data.street){addressField.val(data.street);cityField.val(data.city);}else{addressField.val("niet gevonden");cityField.val("niet gevonden");}});}}
postcodeField.typeWatch2(10,function(){if(usePrefill){doPrefill();}});housenumberField.typeWatch2(10,function(){if(usePrefill){doPrefill();}});}
function addressPrefill2(postcodeField,housenumberField,addressField,cityField,countryField){postcodeField=$(postcodeField);housenumberField=$(housenumberField);addressField=$(addressField);cityField=$(cityField);countryField=$(countryField);var usePrefill;countryField.change(function(){if(countryField.val()=="NL"){usePrefill=true;addressField.attr("readonly","readonly");addressField.css("background-color","#BBBBBB");cityField.attr("readonly","readonly");cityField.css("background-color","#BBBBBB");doPrefill();}else{usePrefill=false;addressField.removeAttr("readonly");addressField.css("background-color","#FFFFFF");cityField.removeAttr("readonly");cityField.css("background-color","#FFFFFF");}});countryField.change();function doPrefill(){if(postcodeField.val()&&housenumberField.val()&&housenumberField.val().match(/^\d+$/)){$.getJSON("/php/address_verify.php?zip="+postcodeField.val()+"&hnr="+housenumberField.val()+"&r="+Math.ceil(99999999*Math.random()),function(data){if(data.NAK){addressField.val("");cityField.val("");postcodeField.parent().children('span.inlineValidation_response').html('<img src="/pics/website_pluimen_2009/rood.jpg" />');housenumberField.parent().children('span.inlineValidation_response').html('<img src="/pics/website_pluimen_2009/rood.jpg" />');$('td#'+addressField.attr("id")+'_responsetxt').html("Deze combinatie van huisnummer & postcode leverde geen correct adres op.");$('td#'+addressField.attr("id")+'_responsetxt').css("display","table-cell");}else if(data.street){addressField.val(data.street);cityField.val(data.city);postcodeField.parent().children('span.inlineValidation_response').html('<img src="/pics/website_pluimen_2009/groen.jpg" />');housenumberField.parent().children('span.inlineValidation_response').html('<img src="/pics/website_pluimen_2009/groen.jpg" />');$('td#'+addressField.attr("id")+'_responsetxt').html('');$('td#'+addressField.attr("id")+'_responsetxt').css("display","none");}else{addressField.val("niet gevonden");cityField.val("niet gevonden");}});}}
postcodeField.typeWatch2(10,function(){if(usePrefill){doPrefill();}});housenumberField.typeWatch2(10,function(){if(usePrefill){doPrefill();}});}