//
//  Class:
//      VdfError
//
//  Represents an error that occured. Contains functionality to present the 
//  error. Is created when error occured, cleared when the user has performed 
//  an action that dissolved the problem. Should be customized for custom 
//  interfaces.
//
//  Since:
//      05-03-06
//  Changed:
//      --
//  Version:
//      0.9
//  Creator:
//      Data Access Europe (Harm Wibier)
//

//
//  Constructor
//
//  Params:
//      oHandler    The errorhandler object that created the error.
//      sTable      Name of the table on wich the error occured (null if global 
//                  error)
//      sField      Name of the field (null if not an field error)
//      sMessage    Description of the error
//      sId         Id that makes the error unique for the field
//
function VdfError(oHandler, sTable, sField, sMessage, sId){
    this.oHandler       = oHandler;
    this.sTable         = sTable;
    this.sField         = sField;
    this.sMessage       = sMessage;
    this.sId            = sId;
    
    this.oDomObject     = null;
    this.sOrrigionalCSS = "";
}

//
//  Called when when the error should be displayed.
//
VdfError.prototype.display = function(){
    if(this.sTable != null && this.sField != null){
        this.displayField();
    }else if(this.sTable != null){
        this.displayTable();
    }else{
        this.displayGlobal();
    }
}

//
//  Displays global errors.
//
VdfError.prototype.displayGlobal = function(){
    alert(this.sMessage);    
}

//
//  Displays field errors.
//
VdfError.prototype.displayTable = function(){
    this.sMessage = this.sMessage + " (Table: " + this.sTable + ")";
    this.displayGlobal();
}

//
//  Displayes field errors (by changing the fields css class and generating a 
//  span with error text in the fields error dom object). Calls the global 
//  error method if the field has no error dom object.
//
VdfError.prototype.displayField = function(){
    var oField, oElement, sName, oErrorField, sOrigCSS, oTabMenu;
    
    oErrorField = document.getElementById(this.sTable + "__" + this.sField + this.oHandler.sErrorExtension);
    
    if(oErrorField != null){
        this.oDomObject = document.createElement("span");
        this.oDomObject.setAttribute("id", this.sTable + "__" + this.sField + '__' + this.sId);
        browser.dom.setCellText(this.oDomObject, this.sMessage.replace('\n' + '<br>') + ";");
     	oErrorField.appendChild(this.oDomObject);
        
        oField = this.oHandler.oVdfForm.aFields[this.sTable + "__" + this.sField];
        if(oField != null){
            sOrigCSS = oField.getCSSClass();
            
            if(sOrigCSS.indexOf((" " + this.oHandler.sCSSErrorClass)) == -1){
                this.sOrrigionalCSS = sOrigCSS;
                
                if(sOrigCSS != ""){
                    oField.setCSSClass((sOrigCSS + " " + this.oHandler.sCSSErrorClass));
                }else{
                    oField.setCSSClass(this.oHandler.sCSSErrorClass);
                }
            }else{
                this.sOrrigionalCSS = null;
                this.oHandler.sCSSErrorClass;
            }
            
            if(this.oHandler.oVdfForm.oLastFocus != oField){
                oField.focusSelect();
            }
        }
        
        //  If tabmenu in the page call it to make sure the error is visible
        oTabMenu = document.getElementById("JTabMenu");
        if(document.getElementById("JTabMenu") != null){
            oTabMenu.oJTabMenu.displayTabWithElement(oErrorField);
        }
        
    }else{
        this.sMessage = this.sMessage + " (Table: " + this.sTable + " Column: " + this.sField + ")";
        this.displayGlobal();
    }    
}

//
//  Called when the error is disolved
//
VdfError.prototype.clear = function(){
    if(this.sField == null || this.sField == ""){
        this.clearGlobal();
    }else{
        this.clearField();
    }
    
    if(this.sTable != null && this.sField != null){
        this.clearField();
    }else if(this.sTable != null){
        this.clearTable();
    }else{
        this.clearGlobal();
    }
}

//
//  Clears global errors (nothing has to be done)
//
VdfError.prototype.clearGlobal = function(){

}

//
//  Clears table errors (nothing has to be done)
//
VdfError.prototype.clearTable = function(){

}


//
//  Clears field errors by removing the generated span and resetting the 
//  orrigional CSS class.
//
VdfError.prototype.clearField = function(){
    var oField;
    
    if(this.oDomObject != null){
        this.oDomObject.parentNode.removeChild(this.oDomObject);
        this.oDomObject = null;
        
        oField = this.oHandler.oVdfForm.aFields[this.sTable + "__" + this.sField];
        if(oField != null && this.sOrrigionalCSS != null){
            oField.setCSSClass(this.sOrrigionalCSS);
        }
    }else{
        this.clearGlobal();
    }
}