﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("TipsCommunity");

TipsCommunity.UniqueNameValidator = function(element) {
    TipsCommunity.UniqueNameValidator.initializeBase(this, [element]);
    this._lastValidationSuccess = true;
}

TipsCommunity.UniqueNameValidator.prototype = {
    initialize: function() {
        TipsCommunity.UniqueNameValidator.callBaseMethod(this, 'initialize');
        $addHandlers(this.get_controlToValidate(), { 'keyup' : this.onKeyUpValidate }, this);
    },
    dispose: function() {
        TipsCommunity.UniqueNameValidator.callBaseMethod(this, 'dispose');
    },
    onKeyUpValidate: function(e) {
        scheduleMethodInvocation(this, 'TipsCommunity.UniqueNameValidator.globalValidateOnKeyUp', 500, this.get_id());
    },
    validationCallback: function(validationSuccess)
    {
        this.set_lastValidationSuccess(validationSuccess);
        if (this.get_element().isvalid != validationSuccess)
        {
            ValidatorValidate(this.get_element()); // Invoke global ASP.NET Validation Framework behaviour
        }
    },
    get_validationMethod: function() {
        return this._validationMethod;
    },
    set_validationMethod: function(value) {
        this._validationMethod = value;
    },
    get_controlToValidate: function() {
        return this._controlToValidate;
    },
    set_controlToValidate: function(value) {
        this._controlToValidate = value;
    },
    get_lastValidationSuccess: function() {
        return this._lastValidationSuccess;
    },
    set_lastValidationSuccess: function(value) {
        this._lastValidationSuccess = value;
    }
}
TipsCommunity.UniqueNameValidator.registerClass('TipsCommunity.UniqueNameValidator', Sys.UI.Control);

// Invoked by the standard ASP.NET Validation Framework
TipsCommunity.UniqueNameValidator.globalValidateNormal = function(source, args)
{
    var thisRef = source.control;
    args.IsValid = thisRef.get_lastValidationSuccess();
}

// Invoked when the user pauses or stops typing in the control to validate
TipsCommunity.UniqueNameValidator.globalValidateOnKeyUp = function(clientID)
{
    var 
        thisRef = $get(clientID).control,
        validationMethodDelegate = eval(thisRef.get_validationMethod());
    validationMethodDelegate($.trim(thisRef.get_controlToValidate().value), Function.createDelegate(thisRef, thisRef.validationCallback));
}


if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

