/**
 *  Select Object
 *
 *  Copyright: (c)2008 CK Web Technologies - http://www.ckweb.com.au/
 *  Author:    Chris Knowles <chris.knowles@ckweb.com.au>
 *  Version:   $Id: Select.js 2 2008-07-23 08:02:22Z Chris $
 */

CKW.Form = function(){};
$Form = CKW.Form;

CKW.Form.Select = function(id, val, key){
    this.field = $D.id(id);
    if (!this.field) {
        this.field = $D.create('select', {'id': id});
    }
    this.value = "";
    this.index = 0;
    if (val != undefined) {
        if (key) {
            this.setBlank(val, key);
        } else {
            this.setBlank(val);
        }
    }
}

CKW.Form.Select.prototype = {

    setBlank: function(val, key){
        this.blankVal = val;
        if (key) {
            this.blankKey = key;
        }
    },

    disable: function(){
	    this.field.disabled = 'disabled';
    },
    
    clear: function(){
        for (var i = this.field.options.length; i > -1; i--) {
            this.field.remove(i);
        }
    },
    
    loading: function(){
        this.clear();
        this.populate({'false':'Loading...'}, true);
        this.disable();
    },
    
    populate: function(data, ignoreBlank){
        if (!ignoreBlank && this.blankVal != undefined && this.blankKey != undefined) {
            this.field.options[0] = new Option(this.blankVal, this.blankKey);
            var i = 1;
        } else if (!ignoreBlank && this.blankVal != undefined) {
            this.field.options[0] = new Option(this.blankVal);
            var i = 1;
        } else {
            var i = 0;
        }
        for (key in data) {
            this.field.options[i] = new Option(data[key], key);
            i++;
        }
    },
    
    populateKeys: function(data, ignoreBlank){
        if (!ignoreBlank && this.blankVal != undefined && this.blankKey != undefined) {
            this.field.options[0] = new Option(this.blankVal, this.blankKey);
            var i = 1;
        } else if (!ignoreBlank && this.blankVal != undefined) {
            this.field.options[0] = new Option(this.blankVal);
            var i = 1;
        } else {
            var i = 0;
        }        
        for (var j = 0; j < data.length; j++) {
            var bits = data[j].split(":");
            this.field.options[i] = new Option(bits[1], bits[0]);
            i++;
        }
    },
    
    enable: function(){
        this.field.disabled = '';
    },
    
    getValue: function(){
        this.value = false;
        if (this.field.selectedIndex > -1) {
            this.value = this.field.options[this.field.selectedIndex].value;
        }
        return this.value;
    },
    
    setValue: function(key){
        for (var i = 0, len = this.field.options.length; i < len; i++) {
	        if (this.field.options[i].value == key) {
                this.field.selectedIndex = i;
                return true;
            }
        };
        return false;
    },
    
    getIndex: function(){
        this.index = this.field.selectedIndex;
        return this.index;
    },
    
    listen: function(func, action){
        if (!action) {
            action = 'change';
        }
        $E.listen(this.field, action, function(e){func(e)});
    },
    
    appendTo: function(id){
        $D.id(id).appendChild(this.field);
    },
    
    setStyle: function(style, value){
        eval("this.field.style." + style + " = '" + value + "'");
    },
    
    reset: function(){
        this.field.selectedIndex = 0;
    },
    
    formatAndPopulate: function(data, separator1, separator2){
        if (!separator1) {
            separator1 = "||";
        }
        if (!separator2) {
            separator2 = "|";
        }
        var options = data.split(separator1);
        var tmp = {};
		for (var i = 1, len = options.length + 1; i < len; i++) {
            var parts = options[i - 1].split(separator2);
            tmp[parts[0]] = parts[1];
		};
        this.populate(tmp);
    },
    
    show: function(){
        this.setStyle('display', 'default');
    },
    
    hide: function(){
        this.setStyle('display', 'none');
    }
}

