/**
 *	Displayes Validation Errors
 *	Each method implements different strategy on how to display
 *	error messages.  Selected startegy should be registered with lmbValidator
 * 	by calling lmbValidator.addErrorListener("", lmbValidationErrorDisplayer.<methodName>);
 */
 var lmbValidationErrorDisplayer = {
    /**
    *	Strategy that doesn't display any error messages
    */
    dontShowErrors : function(error){
    },
    /**
    *	Shows alert with error messages.
    *	Reports error to hitbox.
    */
    showFirstErrorAlertNoCSS : function(error){
        if(error.first){
            lmbValidationErrorDisplayer.reportError(error.errorCode);
            lmbValidationErrorDisplayer.setFocus(error.errorFocusField);
            alert(error.errorMessage);
            lmbValidationErrorDisplayer.enableSubmitButton();
        }
    },
    showFirstErrorAlertNoCSSLhpPage2 :  function(error){

        if(error.first){
            
            lmbValidationErrorDisplayer.reportError(error.errorCode);
            lmbValidationErrorDisplayer.setFocus(error.errorFocusField);
            if((error.errorCode) == ("MIN_DOWN_PAYMENT")) {
                error.errorMessage = error.errorMessage.replace("{0}", addCommasToNumString(document.application.minDownPaymentOther.value));
            }
            if((error.errorCode) == ("MAX_DOWN_PAYMENT")) {
                error.errorMessage = error.errorMessage.replace("{0}", addCommasToNumString(document.application.maxDownPaymentOther.value));
            }

            alert(error.errorMessage);
        }
    },
    /**
    *	Shows alert for first error and decorates the field
    *  with error styles.  The class name should be configured as
    *	lmbValidator.config.errorStyle
    */
    showFirstErrorAlertCSS : function(error){
        if(error.first){
            var elt = lmbValidationErrorDisplayer.decorateWithStyles(error.errorFieldId, lmbValidator.config.errorStyle);
            lmbValidationErrorDisplayer.installStyleRemover(elt, error.errorField);
            lmbValidationErrorDisplayer.reportError(error.errorCode);
            lmbValidationErrorDisplayer.setFocus(error.errorFocusField);
            alert(error.errorMessage);
            lmbValidationErrorDisplayer.enableSubmitButton();
        }
    },
    /**
    *	Reports validation errors to hitbox
    */
    reportError : function(errorCode){
        try{
            //logHbError(errorCode);
        }
        catch(ex){
            var e = new Object();
            e.errorType = "HitBox error";
            e.error = ex;
            lmbUtil.log(e);
        }
    },
    /**
    *	Sets focus to the field that failed validation
    */
    setFocus : function(field){
        if(field){
            try{
                field.focus();
            }
            catch(e){
                lmbUtil.log("Unfocusable Field " + field.name);
            }
        }
    },
    /**
    *	Sets errorStyle on either field or it's wrapper
    *	Wrappers are with class that contains field id.
    */
    decorateWithStyles : function(fldId, className){
        var field = document.getElementById(fldId);
        if(field){
            var elt = lmbValidationErrorDisplayer.findWrapper(field, field.id);
            if(elt){
                elt = field;
            }
            elt.className += " " + className;
        }
    },
    /**
    *	Finds wrapper for the field to decorate it with error class.
    *  Element <X> is wrapper of <Y id="y"/> if <X> is an ancesstor of <Y>
    *	and class attribute of <X> contains id attribute of <Y>
    */
    findWrapper : function(elt, name){
        if(elt){
            if(elt.className.indexOf(name) > -1){
                return elt;
            }
            lmbValidationErrorDisplayer.findWrapper(elt.parentNode, name);
        }
        return null;
    },
    /**
    *	Installs event listeners that remove error style.
    *	After recieving an error, user takes action to correct it,
    *	style that indicates that field is incorect should be removed.
    */
    installStyleRemover : function(elt, field){
        switch(field.type){
            case "text":
            case "password":
            case "textarea":
                lmbUtil.addEventListener(field, "change", function(){
                    lmbFormUtil.removeStyle(elt, lmbValidator.config.errorStyle);
                });
                if(window.attachEvent && field.onkeyup){
                    //IE and field has key filter
                    //Onchange wouldn't work, use onkeyup
                    lmbUtil.addEventListener(field, "keyup", function(){
                        lmbFormUtil.removeStyle(elt, lmbValidator.config.errorStyle);
                    });
                }
            case "select-one":
            case "select-multiple":
                lmbUtil.addEventListener(field, "change", function(){
                    lmbFormUtil.removeStyle(elt, lmbValidator.config.errorStyle);
                });
                break;
            case "radio":
            case "checkbox":
                lmbUtil.addEventListener(field, "click", function(){
                    lmbFormUtil.removeStyle(elt, lmbValidator.config.errorStyle);
                });
                break;
        }
    },

     enableSubmitButton : function() {
        var allInputs = document.getElementsByTagName("input");
        for(var i = 0; i < allInputs.length; i++) {
            if(allInputs.item(i).type.toLowerCase() == "submit") {
                allInputs.item(i).disabled = false;
            }
        }
    }
 }

