/*
 * Constructor
 */
function FieldValidator(
        sName,
        sMode,
        pAfterValidation,
        pBeforeValidation,
        sRegExp,
        sExtra2
)
{
        var oSelf = this

        this.oFieldNode = document.getElementById(sName)
        if( this.oFieldNode == null ) {
            // check for the form (for Mozilla)
            this.oFieldNode = document.forms[sName];
            if( this.oFieldNode == null ) {
                alert("Field not found: "+sName);
                throw 1
            }
        }
        
        if (sMode == 'SubmitStart' || sMode == 'SubmitEnd')
            this.oFieldNode.form = this.oFieldNode;

        if( !this.oFieldNode.form ) throw 2

        this.sMode = sMode ? sMode : 'RequiredField'

        if( sRegExp && sMode == 'RegExp') 
            this.oRegExp = new RegExp( sRegExp )

        if( sRegExp && sMode == 'Counter')
        {
            this.oSize = sRegExp;
        }

        if(sExtra2)
        {
            var aNodes = document.getElementsByName(sExtra2)
            if( aNodes.length == 0 ) 
            {
                this.oExtra2 = document.getElementById(sExtra2)
                if( this.oExtra2 == null ) {
                    alert("Field not found: "+sExtra2);
                    throw 3
                }
            }
            else
                this.oExtra2 = aNodes.item(0);
            
            oSizeField = this.oExtra2;
            if (this.oSize)
                this.SetupEvent(document, 'load', function(e){ 
                    oSizeField.value = (sRegExp - oSelf.oFieldNode.value.length);
                
                } )
        }

        this.SetupEvent( this.oFieldNode.form, 'submit', function(e){ return oSelf.Validator(e) } )

        if (sMode == 'Counter')
        {
            this.SetupEvent( this.oFieldNode, 'change', function(e){ return oSelf.Validator(e) } )
            this.SetupEvent( this.oFieldNode, 'keyup',  function(e){ return oSelf.Validator(e) } )
        }

        this.bValidated = false

        this.BeforeValidation = pBeforeValidation ? pBeforeValidation : this.BeforeValidationDefault

        this.AfterValidation = pAfterValidation ? pAfterValidation : this.AfterValidationDefault

        return this
}

FieldValidator.prototype.Validator = function(e)
{
        this.BeforeValidation()

        switch( this.sMode )
        {
                case 'RequiredField' : this.bValidated = this.RequiredFieldValidator(); break;
                case 'RegExp'        : this.bValidated = this.RegExpValidator();        break;
                case 'Email'         : this.bValidated = this.EmailValidator();         break;
                case 'Int'           : this.bValidated = this.IntValidator();           break;
                case 'Float'         : this.bValidated = this.FloatValidator();         break;
                case 'Counter'       : this.bValidated = this.CounterValidator();       break;
                case 'SubmitStart'   : this.bValidated = this.SubmitValidator();        break;
                case 'SubmitEnd'     : this.bValidated = this.SubmitCleaner();          break;
                default              : this.bValidated = false
        }

        if( !this.bValidated ) 
        {
            this.KillEvent(e);
            this.oFieldNode.form.error = true;
        }

        this.AfterValidation()
}

// RequiredField
FieldValidator.prototype.RequiredFieldValidator = function()
{
        return (
                ( this.oFieldNode.type != 'checkbox' && this.oFieldNode.value != '' ) ||
                ( this.oFieldNode.type == 'checkbox' && this.oFieldNode.checked )
        )
}

// RegExp
FieldValidator.prototype.RegExpValidator = function()
{
        return ( this.oFieldNode.type != 'checkbox' && this.oRegExp.test( this.oFieldNode.value ) )
}

// Email
FieldValidator.prototype.EmailValidator = function()
{
        var sEmail = this.oFieldNode.value.replace( new RegExp('/\(.*?\)/'), '' )
        var oRegExp = /^[A-Za-z0-9][-\w]*(\.[A-Za-z0-9][-\w]*)*@[A-Za-z0-9][-\w]*(\.[A-Za-z0-9][-\w]*)*\.[a-zA-Z]{2,4}$/
        return (this.oFieldNode.value.length == 0) || oRegExp.test(sEmail)
}

// Int
FieldValidator.prototype.IntValidator = function()
{
        return ( parseInt(this.oFieldNode.value) == this.oFieldNode.value )
}

// Float
FieldValidator.prototype.FloatValidator = function()
{
        return ( parseFloat(this.oFieldNode.value) == this.oFieldNode.value )
}

// Float
FieldValidator.prototype.FloatValidator = function()
{
        return ( parseFloat(this.oFieldNode.value) == this.oFieldNode.value )
}

// Counter
FieldValidator.prototype.CounterValidator = function()
{
        str  = this.oFieldNode.value; 
        size = str.length;
        maxl = this.oSize;

        if ( size > maxl ) 
            this.oFieldNode.value = str.substring(0, maxl);
        if ( this.oExtra2 )
            this.oExtra2.value = (maxl - str.length);
        
        return true;
}

// Submit
FieldValidator.prototype.SubmitValidator = function()
{
        if (!this.oFieldNode.error)
        {
            var inSub = this.oFieldNode.getElementsByTagName('INPUT');
            for (var i = 0; i < inSub.length; i++) {
                if (inSub[i].type == 'submit') {
                    this.oFieldNode.oFieldSub = inSub[i];
                    this.oFieldNode.oFieldOnclick = inSub[i].onclick;
                    this.oFieldNode.oFieldStyleColor = inSub[i].style.color;
                    inSub[i].onclick = function () {return false;}
                    inSub[i].style.color = '#888888';
                }
            }


        }

        return true;
}

FieldValidator.prototype.SubmitCleaner = function()
{
        if (this.oFieldNode.error && this.oFieldNode && this.oFieldNode.oFieldOnclick)
        {
            this.oFieldNode.oFieldSub.onclick = this.oFieldNode.oFieldOnclick;
            this.oFieldNode.oFieldSub.style.color = this.oFieldNode.oFieldStyleColor;
            this.oFieldNode.error = false;
        }
        return true;
}


FieldValidator.prototype.BeforeValidationDefault = function()
{
    var sId = this.oFieldNode.getAttribute('id')
    var aLabels = this.oFieldNode.form.getElementsByTagName('label')
    if (aLabels != null) 
    {
        for( var i=0; i<aLabels.length; i++ )
        {
            if( aLabels[i].htmlFor == sId )
            {
                 aLabels[i].style.color = '';
                 break;
            }
        }
    }
}

FieldValidator.prototype.AfterValidationDefault = function()
{
    if( !this.bValidated )
    {
        var sId = this.oFieldNode.getAttribute('id')
        var aLabels = this.oFieldNode.form.getElementsByTagName('label')
        if (aLabels != null) 
        {
            for( var i=0; i<aLabels.length; i++ )
            {
                if( aLabels[i].htmlFor == sId )
                {
                     aLabels[i].style.color = this.bValidated ? '' : 'red'
                     return;
                }
            }
        }

        alert(
            'ATTENTION!\n' +
            'One of fields contains invalid data\!\n\n' +
            '---\n' +
            'Field ' + this.oFieldNode.name + ' has validated as ' + this.sMode
        )
    }
}

FieldValidator.prototype.SetupEvent = function( oElement, sEventType, pHandler )
{
        if( oElement.attachEvent ) oElement.attachEvent('on' + sEventType, pHandler)
        if( oElement.addEventListener ) oElement.addEventListener(sEventType, pHandler, false)
}


FieldValidator.prototype.KillEvent = function(e)
{
        var oEvent = e ? e : window.event
        if( oEvent.preventDefault )
        {
                oEvent.preventDefault()
        }
        else
        {
                oEvent.returnValue = false
        }
}
