/**
 * Class geoname date check
 *
 */
GeonameDateController = Class.create();
Object.extend(GeonameDateController.prototype, {

    /**
     * Initializes this class
     *
     */
    initialize: function(from, until, trigger) {
        // save basic elements
        this.from = $(from); 
        this.until = $(until); 
        this.trigger = $(trigger);
        
   		this.oldDtFrom = '';
   		this.oldDtUntil = '';
   		
   		$('disableDate').onchange = this.changeState.bindAsEventListener(this);
        
        // add onchange events to the input fields
        this.from.onchange = this.onChange.bindAsEventListener(this);
        this.until.onchange = this.onChange.bindAsEventListener(this);

        // call onChange to initialize the trigger
        this.releaseTrigger();
    },
    
    /**
     * Parses a user-given String to a useable Date
     */
     
    parseToTimestamp: function(theString, asDate){
    	var now = new Date();
    	theString = theString.replace(/^(\d\d?)\D(\d\d?)\D?$/, '$1.$2.' + now.getFullYear());
    	theString = theString.replace(/^(\d\d?)\D(\d\d?)\D(\d\d)$/, '$1.$2.20$3');
    	theString = theString.replace(/^(\d\d?)\D(\d\d?)\D(\d)$/, '$1.$2.200$3');
    	theString = theString.replace(/^(\d\d?)\D(\d\d?)\D(\d\d\d\d)$/, '$1.$2.$3');
		var parts = theString.split('.');
		if (! asDate)
			return (parts[2] + '-' + this.padLeft(parts[1]) + '-' + this.padLeft(parts[0]));
		else
			return new Date(parts[2], parts[1], parts[0]);
    },
    
    
    padLeft: function (theString){
    	if (theString.length == 2)
    		return theString;
    	else if (theString.length == 1)
    		return '0' + theString;
    	else
    		return '00';
    },

    /**
     * Gets a date representing string like 1978-12-31
     *
     * @return string
     */    
    getFromDateString: function(){
        var result = $F(this.from);
		return this.parseToTimestamp(result, false);
    },

    /**
     * Gets a date representing string like 1978-12-31
     *
     * @return string
     */    
    getUntilDateString: function(){
        var result = $F(this.until);
		return this.parseToTimestamp(result, false);
    },

    /**
     * Checks the date range
     *
     * @return bool true if date range is valid
     */
    validDate: function(){
        var valid = true;
        // run some tests here...
        
        var from = $F(this.from);
        if (from.length < 3)
        	return false;
        var dt_from = this.parseToTimestamp(from);
        
        var until = $F(this.until);
        if (until.length < 3)
        	return false;
        var dt_until = this.parseToTimestamp(until);
        
        valid = valid && dt_until >= dt_from;

        // more tests like from < until
        // [...]
        return valid;
    },
    
    /**
     * Releases or disables the trigger button
     *
     */                                
    onChange: function(event){
        this.releaseTrigger();
    },
    
    /**
     * Releases or disables the trigger button
     *
     * @return bool true if trigger is released, false otherwise
     */
    releaseTrigger: function(){
        if( this.validDate() || $('disableDate').checked){
            // date is valid: enable the trigger
            this.trigger.disabled = false;
            
        } else if (! $('disableDate').checked) {
            // date is NOT valid: disenable the trigger
            this.trigger.disabled = true;
            
        }
    },
    
    /**
     * Disables the date-fields
     */
    changeState: function(){
    	if (! $('disableDate').checked) {	// => Datum ist aktiviert
    		this.from.disabled = false;
    		this.until.disabled = false;
    		this.from.value = this.oldDtFrom;
    		this.until.value = this.oldDtUntil;
    	} else {							// Datum nicht aktiv
    		this.oldDtFrom = $F(this.from);
    		this.oldDtUntil = $F(this.until);
    		this.from.value = '';
    		this.until.value = '';
    		this.from.disabled = true;
    		this.until.disabled = true;
    	}
    	this.releaseTrigger();
    },

    /**
     * Resets the date selecting elements
     *
     */    
    reset: function(){
        this.releaseTrigger();
    }
    
});
