/*
Copyright (c) 2006 Author
Author: Anil Sharma for the book "Real-World AJAX: Secrets of the Masters"

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

Object.extend = function(destination, source) {
   for (property in source) {
      destination[property] = source[property];
   }
   return destination;
}

// base class of all other component types
function Component(name, displayName) {
   // the associated element
   this.element = null;
   // name of the component
   this.name = name;
   // label of the component
   this.displayName = displayName;
   // the data, this compoennt is bound to
   this.data = null;
   // list of value changed listener
   // 'valueChanged' method of each listener will
   // be called after successful update.
   this.valueChangedListeners = new Array();
}

// This method is used to associate an HTML element with
// the component
Component.prototype.setHtmlElement = function(e) {
    this.element = e;
    var self = this;
    // add onchange listener to the HTML element
    e.onchange = function() {self.changed();};
} 

// This method is used to bound an object with
// the component.
Component.prototype.setData = function(data) {
    // set the given data to its attribute
    this.data = data;
    if (this.element == null) {
        return;
    }
    // retrieve attribute value
    // assumes that the component name is
    // same as the attribute name
    var v = data[this.name];
    if (v == null) {
        v = '';
        } else {
        v = v.toString();
    }
    // set the value of element
    this.element.value = v;
}

// This method is triggered by onchange of the HTML element.
Component.prototype.changed = function() {
    var v = this.element.value;
    // validate new value
    if (!this.validate(v)) {
        return;
    }
    // set the value of object's attribute
    if (this.data != null) {
        this.data[name] = v;
    }
    // inform value changed listeners
    for (var i=0; i<this.valueChangedListeners.length; i++) {
        this.valueChangedListeners[i].valueChanged(this.data, this.name, v);
    }
}

// validates the value
// might be overridden by its subclass.
Component.prototype.validate = function(v) {
    return true;
}

// adds a value changed listener
// assumes that the listener implements 'valueChanged' listener
Component.prototype.addValueChangedListener = function(l) {
    if (l.valueChanged == null) {
        alert("The given listener should implement valueChanged(data, name, newValue) method");
        return;
    }
    this.valueChangedListeners.push(l);
}

Component.prototype.render = function() {
    var elem;
    elem = document.createElement('input');
    elem.type = 'text';
    this.element = elem;
    return elem;
}

/*-------------  TextComp -------------------*/

function TextComp(name, displayName) {
   Object.extend(this, new Component(name, displayName));
   this.type = "TextComp";
}

/*-------------  DateComp -------------------*/

function DateComp(name, displayName) {
   Object.extend(this, new Component(name, displayName));
   this.type = "DateComp";
}

 // validates if the value is in mm/dd/yy format
DateComp.prototype.validate = function(v) {
    return true;
}

/*------------  SelectComp ------------------*/

function SelectComp(name, displayName) {
   
   this.options = new Array();
   Object.extend(this, new Component(name, displayName));
   this.type = "SelectComp";
   
}

SelectComp.prototype.clearOptions = function() {
}

SelectComp.prototype.addOption = function(label, value) {
    var op = new Object();
    op.label = label;
    op.value = value;
    this.options.push(op);
}

SelectComp.prototype.render = function() {
    var elem;
    // create select element
    elem = document.createElement('select');
    // for each option, add option element
    if (this.options) {
        for (var i=0; i<this.options.length; i++) {
        var option = this.options[i];
        var oe = document.createElement("option");
        oe.value=option.value;
        oe.label = option.label;
        oe.appendChild(document.createTextNode(option.label));
        elem.appendChild(oe);
        }
    }
    this.element = elem;
    return elem;
}

/*------------  TextAreaComp ------------------*/

function TextAreaComp(name, displayName) {
   Object.extend(this, new Component(name, displayName));
   this.type = "TextAreaComp";
}

TextAreaComp.prototype.render = function() {
    var elem = document.createElement("fieldset");
    if (this.displayName != null) {
        var l = document.createElement("legend");
        l.innerText = this.displayName;
        elem.appendChild(l);
    }
    var textArea = document.createElement('textarea');
    elem.appendChild(textArea);
    textArea.className = "TextArea";
    textArea.rows = "5";
    return elem;
}