/*
 * Project     : Building Blocks
 * Block       : WebForms
 * Created on  : 12/06/2003
 * CVS Id      : $Id: iHandler.js,v 1.1 2004/11/18 10:40:46 miki Exp $
 *
 * (C) 2003 Polymita Technologies, S.L
 *          All rights reserved.
 */

/**
 * Library javascript for handlers utililty.
 * A collection of methods to create a common handlers.
 *
 * @author <a href="mailto:javier.alperte@polymita.com">Javier Alperte Pérez-Rejón</a>
 * @version $Revision: 1.1 $, $Date: 2004/11/18 10:40:46 $
 */

/** Indicate if the event key is associated with a tab.
 * @param evt Event key.
 */
function isTab(evt) {
     var keyCode = 
        document.layers ? evt.which :
        document.all ? event.keyCode :
        document.getElementById ? evt.keyCode : 0;

    return keyCode  ==  9;               
}

/**
 * Common utility for handlers.
 *
 * @author <a href="mailto:javier.alperte@polymita.com">Javier Alperte Pérez-Rejón</a>
 */

/** Remove all children of an element.
 */
function Common_removeChildren() {    

    toDelete    =   this.element.childNodes;
    size        =   toDelete.length;        

    while (size>0) {
        child       =   toDelete.item(0);            
        this.element.removeChild(child);
        toDelete    =   this.element.childNodes;
        size        =   toDelete.length;
    }        
}

/** Change the visibility of the component
 */
function Common_setVisible(visibility) {

    if (!visibility) {
        this.element.style.visibility = "hidden";
    } else {
        this.element.style.visibility = "visible";
    }
}

/** Change the focus to a component.
 */
function Common_setFocus() {

    this.element.focus();    
}

/**
 * Handler to work with a combo.
 *
 * @author <a href="mailto:javier.alperte@polymita.com">Javier Alperte Pérez-Rejón</a>
 */

/** Create a new instance.
 * @param element HTML SELECT element associated with the combo.
 */
function ComboHandler(element) {
    this.element    =   element;

    this.setVisible     =   Common_setVisible;
    this.setOptions     =   ComboHandler_setOptions;
    this.setValue       =   ComboHandler_setValue;
    this.getValue       =   ComboHandler_getValue;
    this.getText        =   ComboHandler_getText;
    this.removeChildren =   Common_removeChildren;
    this.setFocus       =   Common_setFocus;
}

/** Change the OPTIONS elements of the combo.
 * @param data Div structure with the data of options.
 *             The format of the data are:
 *        
 *             <DIV id='MainData'>
 *               ...
 *               <DIV>  --> Associated with a OPTION i
 *                 <DIV id='value of option"/>
 *                 <DIV id='label of option"/>
 *               </DIV>
 *               ...
 *               <DIV>  --> Associated with a OPTION i+1
 *                 <DIV id='value of option"/>
 *                 <DIV id='label of option"/>
 *               </DIV>
 *             </DIV>
 */
function ComboHandler_setOptions(data) {
    
    var children1    = data.getElementsByTagName("DIV");
    var size         = children1.length;

    this.removeChildren();
    
    for (var i=0;i<size;i+=3) {        
                
        var child   =   children1.item(i);

        var value   =   children1.item(i+1).id;
        var label   =   children1.item(i+2).id;

        option = document.createElement("OPTION");
        option.appendChild(document.createTextNode(label));
        option.setAttribute('value',value);
        this.element.appendChild(option);

    }
}

/** Change the value of combo.
 * @param v New value.
 */
function ComboHandler_setValue(v) {
    this.element.value  =   v;
}

/** Obtain the value of combo.
 * @return Value.
 */
function ComboHandler_getValue() {
    return this.element.value;
}

/** Obtain the text of combo.
 * @return Text.
 */
function ComboHandler_getText() {
    return this.element.options[this.element.selectedIndex].text;
}

/**
 * Handler to work with a table.
 *
 * @author <a href="mailto:javier.alperte@polymita.com">Javier Alperte Pérez-Rejón</a>
 */

/** Create a new instance.
 * @param element HTML TABLE element associated with the table.
 */
function TableHandler(element) {

    var tbody =   element.getElementsByTagName("TBODY");

    if (tbody==null) {
        tbody   =   document.createElement("TBODY");
        element.appendChild(tbody);
    } else {
        tbody   =   tbody[0];
        
    }
    
    this.element        =   tbody;
    
    this.setTR          =   TableHandler_setTR;
    this.addTR          =   TableHandler_addTR;
    this.removeChildren =   Common_removeChildren;
}

/** Add rows to a table.
 * @param data Div structure with the data of options.
 *             The format of the data are:
 *        
 *             <DIV id='MainData'>
 *               ...
 *               <DIV>  --> Associated with a TR i
 *                 <DIV id='value of TD i,j"/>
 *                 <DIV id='value of TD i,j+1"/>
 *               </DIV>
 *               ...
 *               <DIV>  --> Associated with a TR i+1
 *                 <DIV id='value of TD i+1,j"/>
 *                 <DIV id='value of TD i+1,j+1"/>
 *               </DIV> 
 *             </DIV>
 */
function TableHandler_addTR(data) {
    var children1    = data.getElementsByTagName("DIV");
    var size         = children1.length;
    
    for (var i=0;i<size;) {        
        
        var child   =   children1.item(i);
                
        if (child.getAttribute("id")==null || child.getAttribute("id")=='') {
        
            var row =   document.createElement("TR");
            this.element.appendChild(row);

            j   =   1;

            child       =   children1.item(i+j);
            
            while ((i+j)<size &&
                   child.getAttributeNode("id")!=null && 
                   child.getAttribute("id")!='') {

                var td  =   document.createElement("TD");
                td.appendChild(document.createTextNode(child.id));
                row.appendChild(td);
                j++;
                child       =   children1.item(i+j);
            }

            i+=j;

        } else {
            i++;
        }

    }
    
}

/** Change the rows of a table.
 * @param data Div structure with the data of options.
 *             The format of the data are:
 *        
 *             <DIV id='MainData'>
 *               ...
 *               <DIV>  --> Associated with a TR i
 *                 <DIV id='value of TD i,j"/>
 *                 <DIV id='value of TD i,j+1"/>
 *               </DIV>
 *               ...
 *               <DIV>  --> Associated with a TR i+1
 *                 <DIV id='value of TD i+1,j"/>
 *                 <DIV id='value of TD i+1,j+1"/>
 *               </DIV> 
 *             </DIV>
 */
function TableHandler_setTR(data) {
    this.removeChildren();
    this.addTR(data);
}

/**
 * Handler to work with a field.
 *
 * @author <a href="mailto:javier.alperte@polymita.com">Javier Alperte Pérez-Rejón</a>
 */

/** Create a new instance.
 * @param element HTML TABLE element associated with the table.
 */
function FieldHandler(element) {
    
    this.element        =   element;

    this.type           =   this.element.getAttribute("type");
    this.setVisible     =   Common_setVisible;
    this.setData        =   FieldHandler_setData;
    this.setValue       =   FieldHandler_setValue;
    this.getValue       =   FieldHandler_getValue;
    this.removeChildren =   Common_removeChildren;
    this.setFocus       =   Common_setFocus;
}

/** Set value to a field from a data.
 * @param data Div structure with the data of field. 
 *        
 *             <DIV id='MainData'>
 *               <DIV id='value of field"/>
 *             </DIV>
 */
function FieldHandler_setData(data) {
    var children1    = data.getElementsByTagName("DIV");
    var size         = children1.length;
        
        
    var child   =   children1.item(0);
                
    if (child.getAttribute("id")!=null && child.getAttribute("id")!='') {        
        this.element.setAttribute("value",child.getAttribute("id"));
    }                
}

/** Set value to a field.
 * @param v Value. 
 */
function FieldHandler_setValue(v) {
    this.element.value  =   v;
}

/** Get value to a field.
 * @return Value. 
 */
function FieldHandler_getValue() {
    return this.element.value;
}