/**
/*
 * TechDivision_RitterSportTheme
 *
 * NOTICE OF LICENSE
 *
 * TechDivision_RitterSportTheme is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TechDivision_RitterSportTheme is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TechDivision_RitterSportTheme. If not, see <http://www.gnu.org/licenses/>.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade TechDivision_RitterSportTheme to newer
 * versions in the future. If you wish to customize TechDivision_RitterSportTheme for your
 * needs please refer to http://www.techdivision.com for more information.
 *
 * @category   	TechDivision
 * @package    	TechDivision_RitterSportTheme
 * @copyright  	Copyright (c) 2011 <info@techdivision.com> TechDivision GmbH
 * @license    	<http://www.gnu.org/licenses/>
 *			  	GNU General Public License (GPL 3)
 * @author      Thomas Kalteis <tk@techdivision.com>
 **/
/**
 * js/addresscomplete.js
 *
 * Specific JS functions for the Addresscomplete extension 
 *
 * @category   	TechDivision
 * @package    	TechDivision_RitterSportTheme
 * @copyright  	Copyright (c) 2011 <info@techdivision.com> TechDivision GmbH
 * @license    	<http://www.gnu.org/licenses/>
 *			  	GNU General Public License (GPL 3)
 * @author      Thomas Kalteis <tk@techdivision.com>
 */

// 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, emptyText, urlPostCodeOne){
	
        this.form   = $(form);
        this.fPostcode  = $(fPostcode);
        this.fCity  = $(fCity);
        //this.fRegion  = $(fRegion);
        this.img  = $(img);
        this.alert  = $(alert);
        this.imgEnable = false;
        this.urlPostCodeOne = urlPostCodeOne;
        
        this.fCity.readOnly = true;
        //this.fRegion.disabled = true; 
        
        
        this.emptyText = emptyText;
        
        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.blur();
    },

    submit : function(event){
    	//this.alert.style.display = 'none';
    	 
    	this.fCity.readOnly = false;
		//this.fRegion.disabled = false;
        if (this.fPostcode.value == this.emptyText || isNaN(this.fPostcode.value) == true){
        	this.alert.style.display = 'inline';
            Event.stop(event);
            return false;
        } else {
        	//added by TD (Elias Erckens-Platsch)
        	this.alert.style.display = 'none';
        }
        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.fPostcode.value.length<=4){
			this.fCity.value ='';
			//this.fRegion.selectedIndex=0;
			this.fCity.readOnly = true;
    		//this.fRegion.disabled = true;
		}
    	if (this.fPostcode.value.length == 5) {  
    		new Ajax.Request(this.urlPostCodeOne, {
    			method: 'post',
    			postBody: 'postcode='+this.fPostcode.value,
    			onSuccess: this.setOneResult.bind(this)
    		});
    	}
    	if (this.fPostcode.value.length == 5 && this.fCity.value == '') {
    		this.fCity.readOnly = false;
    		//this.fRegion.disabled = false;
    	}
        /*if(this.imgEnable && this.fPostcode.value.length>=1 && this.fPostcode.value.length<=4){
            this.img.style.display='inline';
        }
        else{
        	this.img.style.display='none';
        }*/
    },
    
    setOneResult : function(transport) {
    	
    	if(transport.responseText){
        	var value_vecteur = transport.responseText.split("||");
        	if (!this.fCity.value)
        		this.fCity.value  = value_vecteur[1];
			//this.fRegion.value = value_vecteur[2];
			//this.fCity.readOnly = true;
	        //this.fRegion.disabled = true;
        }
    },
    
    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),
                onShow : function(element, update) { 
            		//update.setAttribute("style", '');
            		update.setAttribute("class", 'field-select-info');
                    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];
        }
    }
}
