/**
 * 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(form2, fPostcode, fCity, fRegion, fCountry, img, alert1, emptyText, url){
        this.form2   = $(form2);
        this.fPostcode  = $(fPostcode);
        this.fCity  = $(fCity);
        //this.fRegion  = $(fRegion);
        this.fCountry  = $(fCountry);
        this.img  = $(img);
        this.alert1  = $(alert1);
        this.imgEnable = false;
        this.url = url;
        
        this.emptyText = emptyText;
        
        Event.observe(this.form2, '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.blur();
    },
    
    submit : function(event){
    	this.alert1.style.display = 'none';
        if (this.fPostcode.value == this.emptyText || isNaN(this.fPostcode.value) == true){
        	//this.alert1.style.display = 'inline';
        	//this.fPostcode.removeClassName('validation-passed');
        	//this.fPostcode.addClassName('validation-failed');
            Event.stop(event);
            return false;
        }
        return true;
    },

    focus : function(event){
    	if(this.fPostcode.value==this.emptyText){
            this.fPostcode.value='';
        }
    },

    blur : function(event){
    	this.img.style.display='none';
        if(this.fPostcode.value=='' && this.imgEnable){
            this.fPostcode.value=this.emptyText;
        }
    },
    
    keyup : function(event){
    	if (this.fPostcode.value.length==0){
			this.fCity.value ='';
			//this.fRegion.selectedIndex=0;
		}
    	
    	this.imgEnable = true;
        if(this.imgEnable && this.fPostcode.value.length>=2 && this.fPostcode.value.length<=5){
            this.img.style.display='inline';
        }
        else{
        	this.img.style.display='none';
        }
        
//        this.majValue.delay(1, this.url, this.fCity, this.fPostcode);
        this.majValue(this.url, this.fCity, this.fPostcode, this.fCountry);
    },
    
    majValue : function(url, fCity, fPostcode, fCountry) {
    	var request = new Ajax.Request(
    		url,
            {
                method: 'post',
                onSuccess: function(data) {
    				// On traite la réponse en JS
    				eval(data.responseText);
    				// On vide la liste
    				fCity.options.length=0
    				// Si des villes sont retournées, on met à jour la liste
    				if (arrayCity.length > 0 ) {
        				
    					// Si plusieurs choix possible, on ajoute le message en 1er
    					if (arrayCity.length > 1 ) {
        					var newVille = new Option(
	        						'- Sélectionnez une ville -',
	        						''
	        		            ); 
        					fCity.options[0] = newVille;
    					}
        				
    					// Ajout des villes à la liste
        				for (var i = 0; i < arrayCity.length; i++) {
        					var splitVille = arrayCity[i].split('_');
        					var newVille = new Option(
        						splitVille[0]+', '+splitVille[1],
        						splitVille[1]
        		            ); 
        					fCity.options[fCity.options.length] = newVille;
        				}
    				}
    				// Si aucune ville n'est retournée
    				else {
    					var newVille = new Option(
    							'- Saisissez un code postal -',
    							''
        		            ); 
    					fCity.options[0] = newVille;
    				}
    			},
    			parameters: {postcode:fPostcode.value, country:fCountry.value}
            }
        );
    },
    
    reInitCountry : function(fCountry, urlCountry){
    	this.fCountry  = $(fCountry);
    	this.fPostcode.value=this.emptyText;
    	this.fCity.value ='';
    	//this.fRegion.selectedIndex=0;
    	this.img.disabled=true;
    	new Ajax.Request(urlCountry, {
            method: 'post', 
            postBody: 'country='+this.fCountry.value,
            onComplete: this.processResult.bind(this)
        });
    },
    
    processResult : function(transport){
    	if(transport.responseText){
    		this.imgEnable = true;
		}else{
			this.imgEnable = false;
			this.fPostcode.value='';
		}
    },
    
    initAutocomplete : function(urlPostCode, destinationElement){
//        new Ajax.Autocompleter(
//            this.fPostcode,
//            destinationElement,
//            urlPostCode,
//            {
//                paramName: this.fPostcode.name,
//                minChars: 2,
//                updateElement: this._selectAutocompleteItem.bind(this),
//                afterUpdateElement: this._selectAutocompleteItem(this),
//                onShow : function(element, update) { 
//                    
//            		//if(update.getElementsByTagName('li').length <= 2)
//
//            		if(!update.style.position || update.style.position=='absolute') {
//                        update.style.position = 'absolute';
//                        Position.clone(element, update, {
//                            setHeight: false, 
//                            offsetTop: element.offsetHeight
//                        });
//                    }
//                    Effect.Appear(update,{duration:0});
//                }
//            }
//        );
    },

    _selectAutocompleteItem : function(element){
    	if(element.title){
        	var value_vecteur = element.title.split("||");
            this.fPostcode.value = value_vecteur[0];
            this.fCity.value  = value_vecteur[1];
			//this.fRegion.value = value_vecteur[2];
        }
    }
}

