//
//  Class:
//      VdfField
//
//  Wrapper for html form elements representing an Database field. With this 
//  wrapper all elements (radio, text, select) have the same interface to talk 
//  to.
//
//  Since:
//      20-01-2006
//  Changed:
//      --
//  Version:
//      0.9
//  Creator:
//      Data Access Europe (Harm Wibier)
//

//
//  Constructor of VdfField class.
//
//  Params:
//      oField      The form element to wrap
//      oVdfForm    The VdfForm form for fetching information
//  
function VdfField(oField, oVdfForm){
    if(typeof(oVdfForm) == "undefined"){
        this.oVdfForm           = null;
    }else{
        this.oVdfForm           = oVdfForm;
    }

    //  Determine names
    this.sName                  = oField.name.toLowerCase();
    this.sTable                 = this.sName.substr(0, this.sName.indexOf('__'));
    this.sField                 = this.sName.substr(this.sName.indexOf('__') + 2);
    this.sServerTable           = browser.dom.getVdfAttribute(oField, "sServerTable", null);
    if(this.sServerTable != null){
        this.sServerTable.toLowerCase();
    }
    
    //  Determine type
    if(oField.tagName == 'SELECT'){
        this.sType              = 'select';
    }else{
        this.sType              = oField.type.toLowerCase();
    }
    
    //  Fetch type specific values
    if(this.sType == 'radio'){
        this.aElements          = new Array();
        this.addElement(oField);
    }else if(this.sType == "checkbox"){
        this.oElement           = oField;
        //  Checkbox has checked and unchecked values, ask form if available
        this.sCheckedValue      = "1";
        this.sUncheckedValue    = "0";
        if(oVdfForm != null){
            if(oVdfForm.getVdfFieldAttribute(this, "sCheckboxCheckedValue") != null){
                this.sCheckedValue = oVdfForm.getVdfFieldAttribute(this, "sCheckboxCheckedValue");
            }
            if(oVdfForm.getVdfFieldAttribute(this, "sCheckboxUncheckedValue") != null){
                this.sUncheckedValue = oVdfForm.getVdfFieldAttribute(this, "sCheckboxUncheckedValue");
            }
        }
        
        this.sOrigDisplayValue  = (this.oElement.checked ? this.sCheckedValue : this.sUncheckedValue);
    }else{
        this.oElement           = oField;
        this.sOrigDisplayValue  = oField.value;
    }
    oField.oVdfField        = this;
    
    //  Sets the sOrigValue
    if(this.sType == "text"){
        this.sOrigValue = oField.value;
    }else{
        //  Workaround because special elements should always be saved with new record
        this.sOrigValue = "";
    }
     
}

//
//  Add elements for field of a type that contain more elements (radio..)
//
//  Param:
//      oField  Element to add
//
VdfField.prototype.addElement = function(oField){
    if(this.sType == 'radio'){
        this.aElements[oField.value] = oField;
        if(oField.checked){
            this.sOrigDisplayValue = oField.value;
        }
        
        oField.oVdfField = this;
    }
}

//
//  Returns:
//      String with the field type (select, radio, text, checkbox etc..)
//
VdfField.prototype.getType = function(){
    return this.sType;
}

//
//  Returns:
//      The html form element (or an array of elements)
//
VdfField.prototype.getElement = function(){
    var oResult = null;
    
    if(this.sType == 'radio'){
        oResult = this.aElements;
    }else{
        oResult = this.oElement;
    }
    
    return oResult;
}

//
//  Returns:
//      The servertable of the field
//
VdfField.prototype.getServerTable = function(){
    return this.sServerTable;
}

//
//  Sets the servertable if not set already.
//
//  Params:
//      sNewServerTable New server table for the field
//      bOverwriteOrig  If true the new server table is always set
//
VdfField.prototype.setServerTable = function(sNewServerTable, bOverwriteOrig){
    if(bOverwriteOrig || this.getVdfAttribute("sServerTable", null) == null){
        this.sServerTable = sNewServerTable;
    }
}

//
//  Returns:
//      The current (selected)value of the field
//
VdfField.prototype.getValue = function(){
    var sValue = '', sCurrentValue;
    
    if(this.sType == 'radio'){
        for(sCurrentValue in this.aElements){
            if(this.aElements[sCurrentValue].checked){
                sValue = sCurrentValue;
            }
        }
    }else if(this.sType == "checkbox"){
        sValue = (this.oElement.checked ? this.sCheckedValue : this.sUncheckedValue);
    }else{
        sValue = this.oElement.value;
    }
    
    return sValue;
}

//
//  Set the value of the field
//
//  Params:
//      sValue  The new field value
//      bReset  If true the startvalue and changedstate are resetted
//
VdfField.prototype.setValue = function(sValue, bReset){
    var sElement;
    
    //  If needed reset the origvalue used for the changedstate
    if(bReset){
        this.sOrigValue = sValue;
    }
    
    if(this.sType == 'radio'){
        //  If none given select first
        if(sValue == ""){
            for(sElement in this.aElements){
                this.aElements[sElement].checked = true;
                break;
            }
        }
        
        //  Try to select
        if(this.aElements[sValue] != null){
            this.aElements[sValue].checked = true;
        }
    }else if(this.sType == "checkbox"){
        //  Set checked only if new value matches checkedvalue..
        this.oElement.checked = (sValue == this.sCheckedValue);
    }else if(this.sType == "select"){
        //  Select first if no value given
        if(sValue == ""){
            this.oElement.selectedIndex = 0;
        }
        
        //  Try to set the value
        this.oElement.value = sValue;
    }else{
        //  Just set the value
        this.oElement.value = sValue;
    }
    
    
    if(bReset){
        this.sOrigDisplayValue = this.getValue();
    }
}

//
//  Returns:
//      True if the value is not the origional value so it should be saved
//
VdfField.prototype.getChangedState = function(){
    return (this.getValue() != this.sOrigValue);
}

//
//  Returns:
//      True if the user has changed the value since the last reset to determine
//      wheter save is nessacary
//
VdfField.prototype.getDisplayChangedState = function(){
    return (this.getValue() != this.sOrigDisplayValue);
}

//
//  Returns:
//      The name of the element
//
VdfField.prototype.getName = function(){
    return this.sName;
}

//
//  Returns:
//      Name of the table
//
VdfField.prototype.getTableName = function(){
    return this.sTable;
}

//
//  Returns:
//      Name of the field
//
VdfField.prototype.getFieldName = function(){
    return this.sField;
}

//
//  Params:
//      sName       Name of the attribute
//      sDefault    The value returned if no value found
//  Returns:
//      The vdf value of the field attribute (Use VdfForm.getVdfFieldAttribute 
//      for datadictionary properties!)
//
VdfField.prototype.getVdfAttribute = function(sName, sDefault){
    var sResult, sCurrentValue;

    if(typeof(sDefault) == "undefined"){
        var sDefault = null;
    }
    sResult = sDefault;
    
    if(this.sType == 'radio'){
        for(sCurrentValue in this.aElements){
            sResult = browser.dom.getVdfAttribute(this.aElements[sCurrentValue], sName, sDefault);
            if(sResult != sDefault){
                break;
            }
        }
    }else{
        sResult = browser.dom.getVdfAttribute(this.oElement, sName, sDefault);
    }
    
    return sResult;
}

//
//  Params:
//      sName       The name of the attribute
//      sDefault    The value returned if no value found
//  Returns:
//      Value of the element attribute (null / default if not set)
//
VdfField.prototype.getAttribute = function(sName, sDefault){
    var sResult, sCurrentValue;
    
    if(typeof(sDefault) == "undefined"){
        var sDefault = null;
    }
    
    //  Fetch value
    if(this.sType == "radio"){
        for(sCurrentValue in this.aElements){
            sResult = this.aElements[sCurrentValue].getAttribute(sName);
            if(sResult != null){
                break;
            }
        }
    }else{
        sResult = this.oElement.getAttribute(sName);
    }
    
    //  Set to default and parse boolean
    if(sResult == null){
        sResult = sDefault;
    }else if(typeof(sResult) == "string"){
        if(sResult.toLowerCase() == "true"){
            sResult = true;
        }else if(sResult.toLowerCase() == "false"){
            sResult = false;
        }
    }
    
    return sResult;
}

//
//  Sets the field elements vdf attribute
//
//  Params:
//      sName   Name of the attribute to set
//      sValue  New value of the attribute
//
VdfField.prototype.setVdfAttribute = function(sName, sValue){
    if(this.sType == 'radio'){
        var sCurrentValue;
        for(sCurrentValue in this.aElements){
            browser.dom.setVdfAttribute(this.aElements[sCurrentValue], sName, sValue);
            break;
        }
    }else{
        browser.dom.setVdfAttribute(this.oElement, sName, sValue);
    }
}

//
//  Set the field elements attributes
//
//  Params:
//      sName   Name of the attribute to set
//      sValue  New value of the attribute
//
VdfField.prototype.setAttribute = function(sName, sValue){
    if(this.sType == 'radio'){
        var sCurrentValue;
        for(sCurrentValue in this.aElements){
            this.aElements[sCurrentValue].setAttribute(sName, sValue);
            break;
        }
    }else{
        this.oElement.setAttribute(sName, sValue);
    }
}

//
//  Set the focus to the field and if possible select the text in the element.
//  (with radio elements the selected element will receive the focus)
//
VdfField.prototype.focusSelect = function(){
    var sCurrentValue;
    
    if(this.sType == 'radio'){
        for(sCurrentValue in this.aElements){
            if(this.aElements[sCurrentValue].checked){
                browser.dom.setFocus(this.aElements[sCurrentValue]);
            }
        }
    }else if(this.sType == 'select' || this.sType == "checkbox"){
        browser.dom.setFocus(this.oElement);
    }else{
        browser.dom.setFocus(this.oElement);
        this.oElement.select();
    }
}

//
//  Sets the width of the field element
//
//  Params:
//      iPixels     The new width in pixels
//
VdfField.prototype.setWidth = function(iPixels){
    if(this.sType == 'radio'){
    
    }else{
        this.oElement.style.width = iPixels + 'px';
    }
}

//
//  Params:
//      The new css class for the elemnt
//
VdfField.prototype.setCSSClass = function(sClassName){
    if(this.sType == 'radio'){
        var sCurrent;
        for(sCurrent in this.aElements){
            this.aElements[sCurrent].className = sClassName;
        }
    }else{
        this.oElement.className = sClassName;
    }
}

//
//  Returns:
//      The current css class of the (first) element
//
VdfField.prototype.getCSSClass = function(sClassName){
    if(this.sType == 'radio'){
        var sCurrent;
        for(sCurrent in this.aElements){
            return this.aElements[sCurrent].className;
        }
        return null;
    }else{
        return this.oElement.className;
    }
}

//
//  Attaches keylistener to the element
//
//  Params:
//      oListener   Method to fetch event
//
VdfField.prototype.addKeyListener = function(oListener){
    if(this.sType == 'radio'){
        var sCurrent;
        for(sCurrent in this.aElements){
            browser.events.addKeyListener(this.aElements[sCurrent], oListener);
        }
    }else{
        browser.events.addKeyListener(this.oElement, oListener);
    }
}

//
//  Attachers generic listener to the element
//
//  Params:
//      sEvent      Name of the event
//      oListener   Method to fetch the event
//
VdfField.prototype.addGenericListener = function(sEvent, oListener){
    if(this.sType == 'radio'){
        var sCurrent;
        for(sCurrent in this.aElements){
            browser.events.addGenericListener(sEvent, this.aElements[sCurrent], oListener);
        }
    }else{
        browser.events.addGenericListener(sEvent, this.oElement, oListener);
    }
}

//
//  Removes keylistener from the element
//
//  Params:
//      oListener   Method to fetch event
//
VdfField.prototype.removeKeyListener = function(oListener){
    if(this.sType == 'radio'){
        var sCurrent;
        for(sCurrent in this.aElements){
            browser.events.removeKeyListener(this.aElements[sCurrent], oListener);
        }
    }else{
        browser.events.removeKeyListener(this.oElement, oListener);
    }
}

//
//  Removes generic listener from the element
//
//  Params:
//      sEvent      Name of the event
//      oListener   Method to fetch the event
//
VdfField.prototype.removeGenericListener = function(sEvent, oListener){
    if(this.sType == 'radio'){
        var sCurrent;
        for(sCurrent in this.aElements){
            browser.events.removeGenericListener(sEvent, this.aElements[sCurrent], oListener);
        }
    }else{
        browser.events.removeGenericListener(sEvent, this.oElement, oListener);
    }
}
