/**
 * Autocomplete Input
 * depending on type an input element value is completed upon blur
 * 
 * (c)Gregor Kofler, info@gregorkofler.at
 * @version 0.3.1 2007-06-04
 * 
 * @param {object} existing input element
 * @param {string} type of completion
 * @param {bool} valid input required
 * 
 * @return {bool} success
 */

function AutocompleteInput(elem, type, required) {
	if(!elem)		{ return false; }
	if(!["date_de", "date_us", "date_iso", "time_hm", "time_hms"].inArray(type)) { return false; }

	this.elem			= elem;
	this.completionType	= type;
	this.required		= false || required;
	if(this.required) { this.origBgColor = this.elem.style.backgroundColor; }
	this.attachHandler();
}

AutocompleteInput.prototype = {
	attachHandler: function() {
		var oThis = this;
		addEvent( this.elem, "blur", function() { oThis.handleCompletion(); });
	},

	handleCompletion: function() {
		if(this.elem.value == "") { return; }
	
		var dat = this.elem.value.toDateTime(this.completionType);
	
		if(!dat) {
			if(this.required) { this.elem.style.backgroundColor = "#ffe0e0"; }
			return;
		}
		this.elem.value = dat;
		if(this.required) { this.elem.style.backgroundColor = this.origBgColor; }
	}
}