///<reference path="../TESCO.js" />
///<reference path="../event.js" />
///<reference path="../eventManager.js" />
///<reference path="../exception.js" />
///<reference path="../validation.js" />
///<reference path="../node.js" />
///<reference path="inputs.js" />
///<reference path="dialogue.js" />

TESCO.$("UI.inputs").saveDefaults = (function() {

    function _constructor(targetInputName, targetValue) {
        var _self = this;
        this.targetValue = targetValue;

        var _targetInput = TESCO.system.DOM.node.getDescendantsByAttributeRegExp(document.body, "input", "id", new RegExp('^' + targetInputName + '-'));
        if (!_targetInput) {
            _targetInput = TESCO.system.DOM.node.getDescendantsByAttributeRegExp(document.body, "textarea", "id", new RegExp('^' + targetInputName + '-'));
        }

        if (_targetInput) {
            _targetInput = _targetInput[0];
            this.targetInput = _targetInput;
            this.attachInputFields(_targetInput);
            this.setDefaultText(_targetInput, targetValue);
        }

        return this;
    }

    _constructor.prototype.saveOldValue = function(n) {
        if (((n.tagName == "INPUT" && (n.type == "text" || n.type == "password")) || n.tagName == "TEXTAREA") && n.value != null) {
            if (this.targetValue == this.targetInput.value.stripCR()) {
                var oldvalue = document.createAttribute("oldvalue");
                oldvalue.nodeValue = n.value;
                n.setAttributeNode(oldvalue);
                n.value = '';
            }
        }
    }

    _constructor.prototype.restoreOldValue = function(n) {
        if (((n.tagName == "INPUT" && (n.type == "text" || n.type == "password")) || n.tagName == "TEXTAREA") && n.value != null) {
            if (n.getAttribute("oldvalue") != null && n.value.length <= 0) {
                n.value = n.getAttribute("oldvalue");
            }
        }
    }

    _constructor.prototype.setDefaultText = function(e, v) {
        var inputElement = document.getElementById(e.id);
        if (inputElement.value.trim() == "") {
            inputElement.value = v;
        }
    }

    function focusFunc(e) {
        var n = e.target;
        this.saveOldValue(n);
    }

    function blurFunc(e) {
        var n = e.target;
        this.restoreOldValue(n);
    }


    function clearOnSubmit(e) {
        if (this.targetValue == this.targetInput.value) {
            this.targetInput.value = '';
        }
    }

    _constructor.prototype.attachInputFields = function(e) {
        var _self = this;
        var _form = TESCO.system.DOM.node.getAncestorByAttributeRegExp(e, "method", new RegExp('^post'));
        TESCO.system.event.attach(_form, "submit", function(e) { clearOnSubmit.call(_self, e); }, false);
        TESCO.system.event.attach(e, "focus", function(e) { focusFunc.call(_self, e); }, false);
        TESCO.system.event.attach(e, "blur", function(e) { blurFunc.call(_self, e); }, false);
    }

    _constructor.prototype.NAME = "TESCO.UI.inputs.saveDefaults";

    return _constructor;
})();

TESCO.$("UI.inputs").SelectBox = (function() {

    function _constructor(id) {
        var _self = this;
        this.changed = false;
        this.recentlyChanged = false;
        this.isKeyDown = false;
        var _selectBox = TESCO.system.DOM.node.getDescendantsByAttributeRegExp(document.body, "select", "id", new RegExp('^' + id + '-([0-9]+)$'));
        if (_selectBox) {
            _selectBox = _selectBox[0];
            this.prevIndex = _selectBox.selectedIndex;
            this.UndoIndex = this.prevIndex;
            this.selectBox = _selectBox;
            TESCO.system.event.manager.call(this, "changed");
            TESCO.system.event.attach(_selectBox, "keydown",
				function(e) {
				    keyDown.call(_self, e);
				}
			);
            TESCO.system.event.attach(_selectBox, "change",
				function(e) {
				    itemChanged.call(_self, e);
				}
			);
        }
        return this;
    }

    function updateIndex() {
        this.currentIndex = this.selectBox.selectedIndex;
    }

    function keyDown(e) {
        this.isKeyDown = true;
        itemChanged.call(this, e);
    }

    function fireEvent(e) {
        updateIndex.call(this);
        if (this.prevIndex != this.currentIndex) {
            this.dispatchEvent("changed", e);
            this.undoIndex = this.prevIndex;
            this.prevIndex = this.currentIndex;
        }
    }

    function itemChanged(e) {
        var _self = this;
        if ((e.type == "change" && this.isKeyDown == false) || e.type == "keydown") {
            if (this.recentlyChanged == false) {
                //	if (e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40) {
                if ((e.type == "keydown" && e.keyCode == 13) || e.type == "change") {
                    this.recentlyChanged = true;
                    fireEvent.call(this, e);
                    setTimeout(
						function() {
						    _self.recentlyChanged = false;
						}, 500
					);
                }
            }
            setTimeout(
				function() {
				    _self.isKeyDown = false;
				    //   _self.recentlyChanged = false;
				}, 500
			);
        }
    }

    _constructor.prototype.createButtonClone = function(formId, name, value) {
        var _form = document.getElementById(formId);
        var _input = TESCO.system.DOM.node.create("input");

        var inputType = document.createAttribute("type");
        inputType.nodeValue = 'hidden';
        _input.setAttributeNode(inputType);

        var inputName = document.createAttribute("name");
        inputName.nodeValue = name;
        _input.setAttributeNode(inputName);

        var inputValue = document.createAttribute("value");
        inputValue.nodeValue = value;
        _input.setAttributeNode(inputValue);

        _form.appendChild(_input);
    }

    _constructor.prototype.undo = function() {
        this.prevIndex = this.undoIndex;
        this.currentIndex = this.prevIndex;
        this.selectBox.selectedIndex = this.undoIndex;
    }

    _constructor.prototype.NAME = "TESCO.UI.inputs.saveDefaults";

    return _constructor;
})();

TESCO.$("UI.inputs").RadioButtonGroup = function() {

    function _constructor(name) {
        // Have to do it this way because we use hyphens in the name attributes
        this.options = document.getElementsByName(name);
    }

    _constructor.prototype.selectedIndex = function() {
        for (var i in this.options) {
            if (this.options[i].checked) {
                return i;
            }
        }
    }

    _constructor.prototype.value = function() {
        for (var i in this.options) {
            if (this.options[i].checked) {
                return this.options[i].value;
            }
        }
    }

    return _constructor;

} ();