﻿
Type.registerNamespace('ClubPlanet');


ClubPlanet.DynamicValidatorBehavior = function(element)
{
    ClubPlanet.DynamicValidatorBehavior.initializeBase(this, [element]);
    this._servicePath = null;
    this._serviceMethod = null;
    this._popupElementID = null;
    this._popupElement = null;
    this._popupBehavior = null;
    this._successMessage = null;
    this._failureMessage = null;
    this._invalidMessage = null;
    this._needMoreMessage = null;
    this._timer = null;
    this._successColor = null;
    this._failureColor = null;
    this._invalidColor = null;
    this._cache = null;
    this._minimumText = 4;
    this._checkInterval = 1000;
    this._currentUsername = null;
    this._focusHandler = null;
    this._blurHandler = null;
    this._keydownHandler = null;
    this._tickHandler = null;
    this._enableCaching = true;
    this._status = null;
    this._offsetX = null;
    this._offsetY = null;
    this._popupWidth = null;
    this._popupHeight = null;
}

ClubPlanet.DynamicValidatorBehavior.prototype = 
{
    initialize : function()
    {
        ClubPlanet.DynamicValidatorBehavior.callBaseMethod(this, 'initialize');
        this._tickHandler = Function.createDelegate(this, this._onTimerTick);
        this._blurHandler = Function.createDelegate(this, this._onLostFocus);
        this._focusHandler = Function.createDelegate(this, this._onGotFocus);
        this._keyDownHandler = Function.createDelegate(this, this._onKeyDown);
        
        this._timer = new Sys.Timer();
        this.initializeTimer(this._timer);
        
        var element = this.get_element();
        this.initializeTextBox(element);
        
        if(this._popupElementID !== null)
            this._popupElement = $get(this._popupElementID);
        if(this._popupElement == null)
        {
            this._popupElement = document.createElement('DIV');
            this._popupElement.id = this.get_id() + '_element';
            if(Sys.Browser.agent === Sys.Browser.Safari)
                document.body.appendChild(this._popupElement);
            else
                element.parentNode.appendChild(this._popupElement);
        }
        this.initializePopupElement(this._popupElement);
        this._popupBehavior = $create(AjaxControlToolkit.PopupBehavior,
            { 'id' : this.get_id() + 'PopupBehavior','parentElement': element, "positioningMode" : AjaxControlToolkit.PositioningMode.Absolute, y: this._offsetY, x: this._offsetX }, null, null, this._popupElement);
    },
    
    get_interval : function()               { return this._checkInterval; },
    set_interval : function(value)          { this._checkInterval = value; },
    
    get_invalidMessage : function()         { return this._invalidMessage; },
    set_invalidMessage : function(value)    { this._invalidMessage = value; },
    
    get_invalidColor : function()           { return this._invalidColor; },
    set_invalidColor : function(value)      { this._invalidColor = value; },
    
    get_successMessage : function()         { return this._successMessage; },
    set_successMessage : function(value)    { this._successMessage = value; },
    
    get_successColor : function()           { return this._successColor; },
    set_successColor : function(value)      { this._successColor = value; },
    
    get_failureMessage : function()         { return this._failureMessage; },
    set_failureMessage : function(value)    { this._failureMessage = value; },
    
    get_failureColor : function()           { return this._failureColor; },
    set_failureColor : function(value)      { this._failureColor = value; },
    
    get_needMoreMessage : function()         { return this._needMoreMessage; },
    set_needMoreMessage : function(value)    { this._needMoreMessage = value; },
    
    get_needMoreColor : function()           { return this._needMoreColor; },
    set_needMoreColor : function(value)      { this._needMoreColor = value; },
    
    get_popupElement : function()           { return this._popupElement; },
    set_popupElement : function(element)    { this._popupElement = element; },
    
    get_popupElementID : function()         { return this._popupElementID; },
    set_popupElementID : function(id)       { this._popupElementID = id; },
    
    get_minimumText : function()            { return this._minimumText; },
    set_minimumText : function(value)       { this._minimumText = value; },
    
    get_serviceMethod : function()          { return this._serviceMethod; },
    set_serviceMethod : function(method)    { this._serviceMethod = method; },
    
    get_servicePath : function()            { return this._servicePath; },
    set_servicePath : function(path)        { this._servicePath = path; },
    
    get_enableCaching : function()          { return this._enableCaching; },
    set_enableCaching : function(value)     { this._enableCaching = value; },
    
    get_offsetX : function()                { return this._offsetX; },
    set_offsetX : function(value)           { this._offsetX = value; },
    
    get_offsetY : function()                { return this._offsetY; },
    set_offsetY : function(value)           { this._offsetY = value; },
    
    get_popupHeight : function()            { return this._popupHeight; },
    set_popupHeight : function(value)       { this._popupHeight = value; },
    
    get_popupWidth : function()             { return this._popupWidth; },
    set_popupWidth: function(value)         { this._popupWidth = value; },
    
    
    dispose : function()
    {
        if(this._popupBehavior)
        {
            this._popupBehavior.dispose();
            this._popupBehavior = null;
        }
        if(this._timer)
        {
            this._timer.dispose();
            this._timer = null;
        }
        var element = this.get_element();
        if(element)
        {
            $removeHandler(element, "focus", this._focusHandler);
            $removeHandler(element, "blur", this._blurHandler);
            $removeHandler(element, "keydown", this._keyDownHandler);
        }
        this._tickHandler = null;
        this._focusHandler = null;
        this._blurHandler = null;
        this._keyDownHandler = null;
        ClubPlanet.DynamicValidatorBehavior.callBaseMethod(this, 'dispose');
    },
    
    initializeTimer : function(timer)
    {
        timer.set_interval(this._checkInterval);
        timer.add_tick(this._tickHandler);
    },
    
    initializeTextBox : function(element)
    {
        element.autocomplete = "off";
        $addHandler(element, "focus", this._focusHandler);
        $addHandler(element, "blur", this._blurHandler);
        $addHandler(element, "keydown", this._keyDownHandler);
    },
    
    initializePopupElement : function(element)
    {
        var popupStyle = element.style;
        popupStyle.visibility = 'hidden';
        popupStyle.backgroundColor = '#FFFFFF';
        if(this._popupWidth > 0)
            popupStyle.width = this._popupWidth + "px";
        if(this._popupHeight > 0)
            popupStyle.height = this._popupHeight + "px";
        popupStyle.fontSize = '10px';
        popupStyle.color = '#000000';
        popupStyle.cursor = 'default';
        popupStyle.unselectable = 'unselectable';
        popupStyle.overflow = 'hidden';
    },
    
    _hidePopupElement : function(item)
    {
        this._popupBehavior.hide();
        this._popupElement.innerHtml = '';
    },
    
    _onGotFocus : function(ev)
    {
        this._popupBehavior.show();
    },
    
    
    _onKeyDown : function(ev)
    {
        var k = ev.keyCode ? ev.keyCode : ev.rawEvent.keyCode;
        if(k === Sys.UI.Key.esc)
        {
            this._hidePopupElement();
            ev.preventDefault();
            this._timer.set_enabled(false);
        }
        else
        {
            this._timer.set_enabled(true);
        }
    },
    
    
    _onLostFocus : function() 
    {
        //this._hidePopupElement();
    },
    
    
    _onMethodComplete : function(status, context, methodName)
    {
        this._status = status;
        
        this._popupElement.innerHTML = '';
        
        if(this._status === 'Failure')
        {
            this._popupElement.style.color = this._failureColor;
            var message = this._failureMessage.replace('{context}',context);
            this._popupElement.innerHTML = message;
        }
        
        if(this._status === 'Invalid')
        {
            this._popupElement.style.color = this._invalidColor;
            var message = this._invalidMessage.replace('{context}',context);
            this._popupElement.innerHTML = message;
        }
        if(this._status === 'Success')
        {
            this._popupElement.style.color = this._successColor;
            var message = this._successMessage.replace('{context}',context);
            this._popupElement.innerHTML = message;
        }
        this._popupBehavior.show();
    },
    
    _onMethodFailed : function(err, response, context)
    {
        // ERROR HANDLING GOES HERE
        alert(err);
    },
    
    _showNeedMore : function(context)
    {
        var message = this._needMoreMessage;
        message = message.replace("{need}", this._minimumText);
        message = message.replace("{have}", context.length);
        message = message.replace("{context}", context);
    
        this._popupElement.innerHTML = '';
        this._popupElement.style.color = this._needMoreColor;
        this._popupElement.appendChild(document.createTextNode(message));
        this._popupBehavior.show();
    },
    
    _onTimerTick : function(sender, eventArgs)
    {
        this._timer.set_enabled(false);
        if(this._servicePath && this._serviceMethod)
        {
            var text = this.get_element().value;
            
            if(text.length >= this._minimumText)
            {
                if(this._currentUsername !== text)
                {
                    this._currentUsername = text;
                    Sys.Net.WebServiceProxy.invoke(this.get_servicePath(), this.get_serviceMethod(), false,
                                                    { value : this._currentUsername },
                                                    Function.createDelegate(this, this._onMethodComplete),
                                                    Function.createDelegate(this, this._onMethodFailed), text);
                }
            }
            else
            {
                this._showNeedMore(text);
            }
        }
    
    }
}

ClubPlanet.DynamicValidatorBehavior.descriptor = 
{
    properties: [   
                    { name: 'checkInterval', type: Number },
                    { name: 'minimumText', type: Number },
                    { name: 'popupElement', isDomElement: true},
                    { name: 'popupElementID', type: String },
                    { name: 'serviceMethod', type: String },
                    { name: 'servicePath', type: String },
                    { name: 'enableCaching', type: Boolean } ]
}

ClubPlanet.DynamicValidatorBehavior.registerClass('ClubPlanet.DynamicValidatorBehavior', AjaxControlToolkit.BehaviorBase);


if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();