﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Cornerstone");

Cornerstone.WaterMarkBehavior = function(element) {
    Cornerstone.WaterMarkBehavior.initializeBase(this, [element]);
    this._stickieText;
    this._passwordOnBlur = false;
    this._passwordField;
}

Cornerstone.WaterMarkBehavior.prototype = {
    get_stickieText: function()
    {
        return this._stickieText;
    },
    set_stickieText: function(value)
    {
        this._stickieText = value;
    },
    get_passwordOnBlur: function()
    {
        return this._passwordOnBlur;
    },
    set_passwordOnBlur: function(value)
    {
        this._passwordOnBlur = value;
    },
    get_passwordField: function()
    {
        return this._passwordField;
    },
    set_passwordField: function(value)
    {
        this._passwordField = value;
    },
    
    initialize: function() {
        Cornerstone.WaterMarkBehavior.callBaseMethod(this, 'initialize');
        
        // Add custom initialization here
        
        var _onFocusHandler = Function.createDelegate(this, this.onFocus);
        var _onBlurHandler = Function.createDelegate(this, this.onBlur);
        var _onKeyUpHandler = Function.createDelegate(this, this.onKeyUp);
       
        this.onLoad();
        
        $addHandlers(this.get_element(), 
        {
            "focus": _onFocusHandler,
            "blur": _onBlurHandler,
            "keyup": _onKeyUpHandler
        }, this);
        
        if (this._passwordOnBlur)
        {
            if (this._passwordField != null)
            {
                $addHandler(this._passwordField, "blur", _onBlurHandler);
            }
        }
    },
    dispose: function() {        
        //Add custom dispose actions here
        $clearHandlers(this.get_element());
        
        Cornerstone.WaterMarkBehavior.callBaseMethod(this, 'dispose');
    },
    
    onFocus: function()
    {
        //alert("focus");
        
        if (this.get_element().value == this._stickieText)
        {
            this.get_element().value = "";
        }
        
        if (this._passwordOnBlur)
        {
            this._changeToPassword();
            this._passwordField.focus(); 
        }
    },
    onBlur: function()
    {
        //alert("blur");

        if (this._passwordOnBlur != true)
        {   
            if (this.get_element().value == "")
            {
                this._setStickieText(this.get_element());
            }
        }
    },
    onKeyUp: function(e)
    {
        
    },
    onLoad: function()
    {
         this._setStickieText(this.get_element());
    },
    _setStickieText: function(e)
    {
        e.value = this._stickieText;
    },
    _changeToPassword: function()
    {
        var element = this.get_element();

        element.style.display = 'none';
        this._passwordField.style.display = 'inline';
    },
    _changeToText: function()
    {
        var element = this.get_element();

        element.style.display = 'block';
        this._passwordField.style.display = 'inline';
    }
}
Cornerstone.WaterMarkBehavior.registerClass('Cornerstone.WaterMarkBehavior', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
