/**
 * ASPerience Javascript
 */

// Version 1.0
var isIE = navigator.appVersion.match(/MSIE/) == "MSIE";

if (!window.Asperience){
var Asperience = new Object();
}

Asperience.showLoading = function(){
Element.show('loading-process');
}
Asperience.hideLoading = function(){
Element.hide('loading-process');
}
Asperience.GlobalHandlers = {
onCreate: function() {
Asperience.showLoading();
},
onComplete: function() {
if(Ajax.activeRequestCount == 0) {
Asperience.hideLoading();
}
}
};

Ajax.Responders.register(Asperience.GlobalHandlers);
Asperience.searchZipCode = Class.create();
Asperience.searchZipCode.prototype = {
initialize : function(form, fPostcode, fCity, fRegion, img, alert, fReg, emptyText, urlPostCode,urlCountry,fCountry)
{
this.form   = $(form);
this.fPostcode  = $(fPostcode);
this.fCity  = $(fCity);
this.fRegion  = $(fRegion);
this.img  = $(img);
this.alert  = $(alert);
this.fReg  = $(fReg);
this.imgEnable = false;
this.emptyText = emptyText;
this.urlPostCode = urlPostCode;
this.urlCountry = urlCountry;
this.fCountry = $(fCountry);
Event.observe(this.form,  'submit', this.submit.bind(this));
Event.observe(this.fPostcode, 'focus', this.focus.bind(this));
//Event.observe(this.fPostcode, 'blur', this.blur.bind(this));
Event.observe(this.fPostcode, 'keyup', this.keyup.bind(this));
this.keyup();
this.img.hide();
},
submit : function(event){
this.alert.style.display = 'none';
if (this.fPostcode.value == this.emptyText || isNaN(this.fPostcode.value) == true){
this.alert.style.display = 'inline';
Event.stop(event);
return false;
}
return true;
},
focus : function(event){
if(this.fPostcode.value==this.emptyText){
this.fPostcode.value='';
}
},
keyup : function(event){
	//	this.img.style.display='inline';
if (this.fPostcode.value.length==0){
this.fCity.value ='';
this.fRegion.selectedIndex=0;
}
if(this.fPostcode.value.length>4){
//alert(img);
post1=this.fPostcode.value.substr(0,2);
if(post1 == 20){
this.fRegion.selectedIndex=21;	
}
else{
this.fRegion.selectedIndex=1;	
}
if(this.fCountry.value != 'FR'){
this.fReg.value= "-";
}
//Requete qui m'envoie le pays
new Ajax.Request(this.urlCountry, {
method: 'post', 
postBody: 'country='+this.fCountry.value
});
//on recupere l'id du city, id des informations de facturation = billing:city, id des informations de livraison = shipping:city, id sur la carnet d'adresses est city. idem pour le name.
new Ajax.Updater(
this.fCity,
this.urlPostCode,
{
method: 'post',
parameters: 'postcode='+this.fPostcode.value,
onLoading : this.img.show(),
onSuccess: change.curry(this.fCity.id, this.img)
});
}
	}
};

function  change (id, img, transport){
	img.hide();
	var name 		= $(id).name;
	var onchange 	= $(id).onchange;
	var title		= $(id).title;
	//var classs		= $(id).class;
	var reponse = transport.responseText.replace('##id##',id);
	//alert(onchange);
	$(id).replace(reponse);
	$(id).name		= name ;
	$(id).onchange 	= onchange;
	$(id).title		= title;
	//$(id).class		= classs;
}

