﻿// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.


/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference path="../ExtenderBase/BaseScripts.js" />
/// <reference path="../Common/Common.js" />


Type.registerNamespace('FutureElectronics.Web.UI.Controls');
Type.registerNamespace('AjaxControlToolKit');
Type.registerNamespace('FutureElectronics');

FutureElectronics.Web.UI.Controls.ValidationDisplayerBehavior = function AjaxControlToolKit$ValidationDisplayerBehavior(element) {
    FutureElectronics.Web.UI.Controls.ValidationDisplayerBehavior.initializeBase(this, [element]);
    
    this._inValidImageUrl = null;
    this._validImageUrl = null;
    this._cssClass = "ajax__ValidationDisplayer";
    this._helpMessage = null;
    this._imageControlID = null;
    this._imageControl = null;
    this._textContainerID = null;
    this._textContainer = null;
    this._groupContainerID = null;
    this._groupContainer = null;    
    this._width = "200px";
    this._isvalid = true;
    this._originalValidationMethod = null;
    this._validationMethodOverride = null;
    this._elementToValidate = null;
    this._focusAttached = false;
    this._isOpen = false;
    this._isBuilt = false;
    this._validators = null;
    this._isValidated = false;
    this._val = null;
    this._isValidateOnBlur = false;
    this._setFocusOnErrorOnSubmit = true;
    this._errormessage = "";
    this._ispostback = null;
    this._enabledValidation = true;
    this._ischanged = false;
    this._originalValue = "";
    this._alwaysDisplayErrorMessage = false;
    this._useAutoPosition = false;
    this._isvalidServer = true;
    this._errorMessageServer = "";
}
FutureElectronics.Web.UI.Controls.ValidationDisplayerBehavior.prototype = {
    initialize : function() {
        FutureElectronics.Web.UI.Controls.ValidationDisplayerBehavior.callBaseMethod(this, 'initialize');
        
        var elt = this.get_element();
        
        this._imageControl = $get(this._imageControlID);
        this._textContainer = $get(this._textContainerID);
        
        //this._imageControl.src = '';
        //this._textContainer.innerHTML = '';
        $common.setVisible(this._imageControl, false);
        $common.setVisible(this._textContainer, false);
        
        this._focusHandler = Function.createDelegate(this, this._onfocus);
        $addHandler(elt, 'focus', this._focusHandler);
        
        this._blurHandler = Function.createDelegate(this, this._onblur);        
        $addHandler(elt, 'blur', this._blurHandler);   
        
        this._changeHandler = Function.createDelegate(this, this._onchange);        
        $addHandler(elt, 'change', this._changeHandler);   
         
        
        var value = ValidatorGetValue(elt.id); 
        this._originalValue = ValidatorTrim(value);
        
//        this._groupContainer = $get(this._groupContainerID); 
//        if (this._groupContainer != null) {                                   
//            this._focusHandlerGroupContainer = Function.createDelegate(this, this._onfocusGroupContainer);
//            $addHandler(this._groupContainer, 'focus', this._focusHandlerGroupContainer);            
//            this._blurHandlerGroupContainer = Function.createDelegate(this, this._onblurGroupContainer);        
//            $addHandler(this._groupContainer, 'blur', this._blurHandlerGroupContainer);  
//        }
              
       
        
        //               
        // Override the evaluation method of the current validator
        //
        //Get the last one that is validated
//        for(var i=(this._validators.length-1); i==0; i--) {
//            var val = this._validators[i];
//            if ((val.controltovalidate == elt.id) && val.enabled == true) {
//                this._val = this._validators[i];
//                break;
//            }        
//        } 
        //this._val = this._validators[this._validators.length-1];   
        this.setValidator();       
        if (this._val) {  
            this.attachToValidationMethod(this._val);          
//            if(this._val.evaluationfunction) {                       
//                this._originalValidationMethod = Function.createDelegate(this, this._val.evaluationfunction);
//                this._validationMethodOverride = Function.createDelegate(this, this._onvalidate);
//                this._val.evaluationfunction = this._validationMethodOverride;
//            }
            if (this._ispostback) 
                this.dovalidation();
        }
        this.setFocusOnError();
        
        if (this._alwaysDisplayErrorMessage == true)
        {
            this.setmessage();
        }
        
        // Check for failed server-side validation (indicated by non-empty ClientState)
        var clientState = this.get_ClientState();
        if ((null != clientState) && ("" != clientState)) {
            
            //this._ensureCallout();
            //this.show();
            var canDisplay = true;
            for(var i=0; i < Page_Validators.length; i++) {
                var val = Page_Validators[i];
                if (val.isvalid == false) {
                    canDisplay = false;
                    break;
                }
            }
            if( canDisplay == true ) {
                this._imageControl.title = 'invalid'; 
                this._imageControl.src = this.get_inValidImageUrl();
                this._textContainer.innerHTML = clientState;
                this._isvalid = false;
                
                this.show();
            }
        } else {
            if (this._isvalid == true && this._isvalidServer == true) {
                $common.setVisible(this._imageControl, false);
                $common.setVisible(this._textContainer, false);
            }
        }
        this._ispostback = true;
    },

    
    _detectChange : function() {
        var currentValue = "";
        var elt = this.get_element();
        var value = ValidatorGetValue(elt.id); 
        currentValue = ValidatorTrim(value);
        if (this._originalValue != currentValue)
        {
            this._ischanged = true;
            this._isvalidServer = true;
        }
    },
    
    _ensureCallout : function() {
        if (!this._isBuilt) {
              
            var elt = this.get_element();
            //
            // create the DOM elements
            //
            //var elementToValidate = this._elementToValidate = $get(elt.controltovalidate);            
            //this._imageControl.src = '';
            //this._textContainer.innerHTML = '';
           
            //
            // initialize behaviors
            //
            
            this._isBuilt = true;
        }
    },
    
    dispose : function() {
        
        if (this._isBuilt) {
            this.hide();
            
            if (this._focusHandler) {
                $removeHandler(this.get_element(), "focus", this._focusHandler);
                this._focusAttached = false;
            }
            if (this._blurHandler) {
                $removeHandler(this.get_element(), 'blur', this._blurHandler);                
                this._blurHandler = null;
            }  
            if (this._changeHandler) {
                $removeHandler(this.get_element(), 'change', this._changeHandler);                
                this._changeHandler = null;
            }           
//            if (this._focusHandlerGroupContainer) {                                    
//                $removeHandler(this._groupContainer, 'focus', this._focusHandlerGroupContainer);            
//            }
//            if (this._blurHandlerGroupContainer) {   
//                $removeHandler(this._groupContainer, 'blur', this._blurHandlerGroupContainer); 
//            }
            
            this._isBuilt = false;
        }
        FutureElectronics.Web.UI.Controls.ValidationDisplayerBehavior.callBaseMethod(this, 'dispose');
    },    
    
    setValidator : function() {
        this._val = null;
        var elt = this.get_element();        
        this._validators = elt.Validators;
        if (this._validators != null) {
            for(var i=(this._validators.length-1); i>=0; i--) {
                var val = this._validators[i];
                //if ((val.controltovalidate == elt.id) && val.enabled != false) {
                if ((val.controltovalidate == elt.id)) {
                    var isEnabled = true;
                    if (val.enabled != null) {
                        isEnabled = val.enabled;                        
                    }
                    if (isEnabled != false) {
                        this._val = this._validators[i];
                        break;
                    }
                }        
            }
        }
    },
    
    
        
    show : function(force) {        
          $common.setVisible(this._textContainer, true);
    },
    
    hide : function() {
          $common.setVisible(this._textContainer, false);
    },
    
    reinitialize : function() {
        $common.setVisible(this._imageControl, false);
        $common.setVisible(this._textContainer, false);
        this._imageControl.src = '';
        this._textContainer.innerHTML = '';
        this._isvalid = true;
        this._isValidated = false;
        this._isvalidServer = true;
        this._errorMessageServer = '';
        this._ischanged = false;
        this._errormessage = '';
        this.reinitvalidator();
    },
    
    reinitvalidator : function() {
        var elt = this.get_element();
        this._validators = elt.Validators;
        for (var i = 0; i < this._validators.length; i++){
            var val = this._validators[i]; 
            if (val != null) {
                if ((val.controltovalidate == elt.id)) {
                    val.isvalid = true;
                }
            }  
        }
    
    },
    
    attachToValidationMethod : function(val) {        
        if(val.evaluationfunction) {                       
            this._originalValidationMethod = Function.createDelegate(this, val.evaluationfunction);
            this._validationMethodOverride = Function.createDelegate(this, this._onvalidate);
            val.evaluationfunction = this._validationMethodOverride;
        }    
    },
    
    _onchange : function(e) {
        if (this._ischanged != true)
            this._ischanged = true;       
    },

    _onfocus : function(e) {
        this._focusAttached = true;
        this.setmessage(); 
    },
    
        
    _onblur : function(e) {
        this._detectChange();
        this._textContainer.innerHTML = '';        
        this._focusAttached = false;
        //the error message is always invisible when lost the focus
        $common.setVisible(this._textContainer, false);    
        this.setValidator();       
        if (this._val) { 
            if (this._originalValidationMethod == null) {
                this.attachToValidationMethod(this._val);  
            }
            var val = this._val;
            this._isValidateOnBlur = true;
            val.isvalid = this._originalValidationMethod(val); 
            this._isValidateOnBlur = false;
            this.dovalidation(); 
            return val.isvalid;   
        } else {            
            return true;
        }
    },
    
    _onfocusGroupContainer : function(e) {
        this.setmessage();        
    },
    
    _onblurGroupContainer : function(e) {
        $common.setVisible(this._textContainer, false);            
    },
    
    setmessage: function() {
    
        $common.setVisible(this._textContainer, false);
        if(!this._isvalid) {
          this._textContainer.innerHTML = this._errormessage;          
          $common.setVisible(this._textContainer, true);  
          if (this._useAutoPosition == true){
            var elt = this.get_element();
            this._textContainer.style.position = 'absolute';
            this._textContainer.style.top = elt.offsetParent.offsetTop + 'px'
            this._textContainer.style.left = (elt.offsetParent.offsetLeft + elt.offsetParent.offsetWidth) + 'px';          
          }          
        } else { 
            if(this._helpMessage != null) {                
                if(this._helpMessage.length > 0) {
                    $common.setVisible(this._textContainer, true);  
                    this._textContainer.innerHTML = this._helpMessage;
                }
            }    
        }
    
    },
    
    dovalidation : function() {        
        var elt = this.get_element();
        this._validators = elt.Validators;
        //var errormessage = '';
        var isvalid = -2;
        for (var i = 0; i < this._validators.length; i++){
            var val = this._validators[i]; 
            if (val != null) {
                //if ((val.controltovalidate == elt.id) && val.enabled != false) {
                if ((val.controltovalidate == elt.id)) {
                    var isEnabled = true;
                    if (val.enabled != null) {
                        isEnabled = val.enabled;                        
                    }
                    if (isEnabled == false) {
                        isvalid = -2; 
                    } else {                    
                        if (val.isvalid == false) { 
                            isvalid = 0;
                            this._errormessage = val.errormessage;
                            break; 
                        } else {  
                            isvalid = -1;  
                            var value = ValidatorGetValue(elt.id); 
                            if (ValidatorTrim(value).length == 0)  
                                isvalid = -2; 
                        }  
                    }
                }
            }  
        }
        //Consider the state on the server
        if (this._isvalidServer == false) {
            isvalid = 0;
            this._errormessage = this._errorMessageServer;
        }
        //ByPass in the case of an onblur event and in the case that a change is made
        var bypass = false;
        if ((this._ischanged == false) && (this._isValidateOnBlur == true))
            bypass = true;
            
        if ((isvalid > -2) && (bypass == false)) {
           $common.setVisible(this._imageControl, true);   
                
            if (isvalid == -1) {
                this._imageControl.title = 'valid'; 
                this._imageControl.src = this.get_validImageUrl();
                this._textContainer.innerHTML = '';
                this._isvalid = true;
            } else { 
               this._imageControl.title = 'invalid'; 
               this._imageControl.src = this.get_inValidImageUrl();
               this._textContainer.innerHTML = this._errormessage;
               this._isvalid = false;
            }  
            this._isValidated = true;   
        } else { 
            $common.setVisible(this._imageControl, false);   
            this._errormessage = '';         
            this._isvalid = true;
            this._isValidated = false;             
        }
        if (this._focusAttached == true)
            this.setmessage();
       
    },
    
    
    _onvalidate : function(val) {                
        val.isvalid = this._originalValidationMethod(val);
        this.dovalidation();
        if (this._isvalid == false) {
            if (this._isValidateOnBlur == false) {
                if (this._setFocusOnErrorOnSubmit == true)
                {
                    this.setFocusOnError();
                }
            }
        }
        //Because we overriding the method we must return the same type of value
        //and return a value of course...       
        return val.isvalid;
        
    },
    
    setFocusOnError : function () {
        
        //get the first element invalid
        if (this._setFocusOnErrorOnSubmit == true)
        {
            var elt = this.get_element();        
            for(var i=0; i < Page_Validators.length; i++) {
                var val = Page_Validators[i];
                if (val.isvalid == false) {
                    if (val.controltovalidate == elt.id) {
                        //If the first validator is related to our control
                        try {
                            elt.focus(); 
                        } catch(ex) {
                             
                        }
                        this.setmessage();                 
                    }
                    break;
                }
                       
            }
        }
    },
    
    get_inValidImageUrl : function() {
        return this._inValidImageUrl;
    },
    set_inValidImageUrl : function(value) {
        
        if (this._inValidImageUrl != value) {
            this._inValidImageUrl = value;
            this.raisePropertyChanged("inValidImageUrl");
        }
    },
    
    get_validImageUrl : function() {
        return this._validImageUrl;
    },
    set_validImageUrl : function(value) {
        
        if (this._validImageUrl != value) {
            this._validImageUrl = value;
            this.raisePropertyChanged("validImageUrl");
        }
    },
    
    get_imageControlID : function() {
        return this._imageControlID;
    },
    set_imageControlID : function(value) {
        
        if (this._imageControlID != value) {
            this._imageControlID = value;
            this.raisePropertyChanged("imageControlID");
        }
    },
    
    get_textContainerID : function() {
        return this._textContainerID;
    },
    set_textContainerID : function(value) {
        
        if (this._textContainerID != value) {
            this._textContainerID = value;
            this.raisePropertyChanged("textContainerID");
        }
    },
    
     get_groupContainerID : function() {
        return this._groupContainerID;
    },
    set_groupContainerID : function(value) {
        
        if (this._groupContainerID != value) {
            this._groupContainerID = value;
            this.raisePropertyChanged("groupContainerID");
        }
    },
    
        
    get_width : function() {
        return this._width;
    },
    set_width : function(value) {

        if (this._width != value) { 
            this._width = value;
            this.raisePropertyChanged("width");
        }
    },
    
    get_isvalid : function() {
        return this._isvalid;
    },
    set_isvalid : function(value) {

        if (this._isvalid != value) { 
            this._isvalid = value;
            this.raisePropertyChanged("isvalid");
        }
    },
    
    get_isvalidServer : function() {
        return this._isvalid;
    },
    set_isvalidServer : function(value) {

        if (this._isvalidServer != value) { 
            this._isvalidServer = value;
            this.raisePropertyChanged("isvalidServer");
        }
    },
    
    get_errorMessageServer : function() {
        return this._isvalid;
    },
    set_errorMessageServer : function(value) {

        if (this._errorMessageServer != value) { 
            this._errorMessageServer = value;
            this.raisePropertyChanged("errorMessageServer");
        }
    },
        
    
    get_alwaysDisplayErrorMessage : function() {
        return this._alwaysDisplayErrorMessage;
    },
    set_alwaysDisplayErrorMessage : function(value) {

        if (this._alwaysDisplayErrorMessage!= value) { 
            this._alwaysDisplayErrorMessage = value;
            this.raisePropertyChanged("alwaysDisplayErrorMessage");
        }
    },
    
    get_ispostback : function() {
        return this._ispostback;
    },
    set_ispostback : function(value) {

        if (this._ispostback != value) { 
            this._ispostback = value;
            this.raisePropertyChanged("ispostback");
        }
    },

    get_cssClass : function() {
        return this._cssClass;
    },
    set_cssClass : function(value) {

        if (this._cssClass != value) {
            this._cssClass = value;
            this.raisePropertyChanged("cssClass");
        }
    },
    
    get_helpMessage : function() {
        return this._helpMessage;
    },
    set_helpMessage : function(value) {

        if (this._helpMessage != value) {
            this._helpMessage = value;
            this.raisePropertyChanged("helpMessage");
        }
    },
    
    get_setFocusOnErrorOnSubmit : function() {
        return this._setFocusOnErrorOnSubmit;
    },
    set_setFocusOnErrorOnSubmit : function(value) {

        if (this._setFocusOnErrorOnSubmit != value) {
            this._setFocusOnErrorOnSubmit = value;
            this.raisePropertyChanged("setFocusOnErrorOnSubmit");
        }
    },
        
    get_isOpen : function() {
        return this._isOpen;
    },
    
    set_validators : function(value) {

        if (this._validators != value) {
            this._validators = value;
            this.raisePropertyChanged("validators");
        }
    },
    get_validators : function() {
        return this._validators;
    },
    
    set_useAutoPosition : function(value) {

        if (this._useAutoPosition != value) {
            this._useAutoPosition = value;
            this.raisePropertyChanged("useAutoPosition");
        }
    },
    get_useAutoPosition : function() {
        return this._useAutoPosition
    }
    
    
    
}
FutureElectronics.Web.UI.Controls.ValidationDisplayerBehavior.registerClass('FutureElectronics.Web.UI.Controls.ValidationDisplayerBehavior', AjaxControlToolkit.BehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();