
var ZipAjaxCities=new Class({mFranceIdCountry:null,mAjax:null,mDataReceived:{},initialize:function(){this.mFranceIdCountry=global_france_id_country;this.mAjax=new Json.Remote('/ajax/espActions/GetCitiesByZipCode',{onComplete:this.loadNewCities.bind(this),onRequest:this.loadingDataFromAjax.bind(this)});editform_zip.addEvent('keyup',this.eZipKeyDown.bind(this));editform_zip.getDomElement().setProperty('autocomplete','off');editform_id_country.addEvent('onChangeOption',this.eSwitchCountry.bind(this));editform_city2.addEvent('onChangeOption',this.eSwitchCity.bind(this));editform_id_country.fireEvent('onChangeOption');this.initializeAfterLoad();},initializeAfterLoad:function(){if(editform_zip.getValue()!=''&&this.mFranceIdCountry==editform_id_country.getValue()){editform_city.hide();editform_city2.show();if(editform_city.isInvalid()){editform_city2.setInvalid(true,'Veuillez remplir ce champ');}
this.invokeAJAXCitiesLoad();}},eZipKeyDown:function(){$clear(this.mAjaxDelayId);if(editform_id_country.getValue()!=this.mFranceIdCountry){return;}
if(''==editform_zip.getValue()||editform_zip.getValue().length<2){editform_city2.removeAllOptions();editform_city2.addOption('','Veuillez saisir le code postal');if(editform_city2.isVisible()){editform_city2.closePopup();}}
else if(editform_zip.getValue().length>=2){editform_city.hide();editform_city2.show();editform_city2.setInvalid(false);this.invokeAJAXCitiesLoad();}},invokeAJAXCitiesLoad:function(){this.mAjaxDelayId=this.mAjax.send.delay(500,this.mAjax,[{'zip':editform_zip.getValue()}]);},eSwitchCountry:function(pPrevIdCountry,pIdCountry){if(pIdCountry==this.mFranceIdCountry){if(pPrevIdCountry!=this.mFranceIdCountry){editform_city.setValue('');editform_city2.setValue('');editform_zip.setValue('');editform_zip.fireEvent('keyup');editform_city2.fireEvent('onChangeOption');}
editform_city.hide();editform_city2.show();}
else{if(pPrevIdCountry==this.mFranceIdCountry){editform_city.setValue('');editform_city2.setValue('');editform_zip.setValue('');}
editform_city.show();editform_city2.hide();}},eSwitchCity:function(pPrevCity,pCity){editform_city.setValue(editform_city2.getValue());if(pCity&&this.mDataReceived[pCity]){editform_zip.setValue(this.mDataReceived[pCity]);}},loadNewCities:function(pData){editform_city2.removeAllOptions();if(editform_city2.isVisible()){editform_city2.closePopup();}
this.mDataReceived={};if(null==pData.data){if(editform_city2.isVisible()){editform_city2.closePopup();}
editform_city2.addOption('','Aucun résultat');return;}
editform_city2.addOption('','--- choix ---');for(city in pData.data){editform_city2.addOption(city,city);}
for(city in pData.data){this.mDataReceived[city]=pData.data[city];}
if(editform_city2.isVisible()){if(editform_city.getValue()!=''){editform_city2.setValue(editform_city.getValue());}
else{editform_city.setValue('');editform_city2.openPopup();}}},loadingDataFromAjax:function(){editform_city2.removeAllOptions();if(editform_city2.isVisible()){editform_city2.closePopup();}
editform_city2.addOption('','En cours de chargement...');}});window.addEvent('domready',function(){new ZipAjaxCities();});