﻿//Base validator object
function BaseValidator(senderId,  messageContainer, messageText)
{
    this.senderId = senderId;
    this.ContainerId = messageContainer;
    this.messageText = messageText;
    this.messageCss = "";
    this.IValid = true;
    this.sender = new Object();
    this.container = new Object();
    //private [for all derivibled objects]
    var initiated = function()
    {
        this.sender = document.getElementById(this.senderId);
        this.container = document.getElementById(this.ContainerId);    
        this.container.setAttribute("className", this.messageCss);
    }
    this.validate = function()
    {
        initiated.call(this); 
    }
    this.addEventTo = function(obj, evt, handler)
    {
        if(obj.addEventListener)
        {
            obj.addEventListener(evt, handler, true);
        }
        if(obj.attachEvent)
        {
            obj.attachEvent('on' + evt, handler);
        }
    }
}

//Confirm Validation Object
function ConfirmValidationObject(firstObjectId, confirmObjectid, messageContainer, messageText)
{
    //specify of this validator
    var conf = confirmObjectid;
    this.confirmer = document.getElementById(confirmObjectid);
    //inheritance
    this.constructor = BaseValidator;    
    this.constructor.call(this, firstObjectId, messageContainer, messageText);
    var bValidate = this.validate;
    //override
    this.validate = function()
    {
        bValidate.call(this);//exec base validate;
        if(this.sender.value != this.confirmer.value)
        {
            this.container.innerHTML = this.messageText;
            this.IValid = false;
        }
        else
        {
            var msg = this.container.innerHTML;
            this.container.innerHTML = (msg == this.messageText) ? "" : msg;
            this.IValid = true;
        }   
    }
}

//Length Validation Object
function LengthValidationObject(objectId, min, max, messageContainer, messageText)
{
    this.max = max;
    this.min = min;
    this.constructor = BaseValidator;
    this.constructor.call(this, objectId, messageContainer, messageText);
    var bValidate = this.validate;
    this.validate = function()
    {
        bValidate.call(this);
        var val = this.sender.value.length;
        if(val < this.min || val > this.max)
        {
            this.container.innerHTML = this.messageText;
            this.IValid = false;
        }
        else
        {
            var msg = this.container.innerHTML;
            this.container.innerHTML = (msg == this.messageText) ? "" : msg;
            this.IValid = true;
        }    
    }
}

//Required Field Validation Object class
function RequiredValidationObject(objectId, objectType, messageContainer, messageText)
{
    this.type = objectType;                     // must be only {txt, ddl, chb}
    this.constructor = BaseValidator;
    this.constructor.call(this, objectId, messageContainer, messageText);
    var bValidate = this.validate;
    this.validate = function()
    {
        bValidate.call(this);
        switch(this.type)
        {
            case "txt":
                if(this.sender.value == "")
                {
                    this.container.innerHTML = this.messageText;
                    this.IValid = false;
                }
                else
                {
                    var msg = this.container.innerHTML;
                    this.container.innerHTML = (msg == this.messageText) ? "" : msg;
                    this.IValid = true;
                }
                break;
            case "ddl":
                if(this.sender.options[this.sender.selectedIndex].value < 0)
                {
                    this.container.innerHTML = this.messageText;
                    this.IValid = false;
                }
                else
                {
                    var msg = this.container.innerHTML;
                    this.container.innerHTML = (msg == this.messageText) ? "" : msg;
                    this.IValid = true;
                }
                break;
            case "chb":
                if(this.sender.checked == false)
                {
                    this.container.innerHTML = this.messageText;
                    this.IValid = false;
                }
                else
                {
                     var msg = this.container.innerHTML;
                     this.container.innerHTML = (msg == this.messageText) ? "" : msg;
                     this.IValid = true;
                }
        }
    }
}

//Email Validation Object class
function EmailValidationObject(objectId, messageContainer, messageText)
{
    //Email pattern
    this.EmailRegExp = new RegExp(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/);
    this.constructor = BaseValidator;
    this.constructor.call(this, objectId, messageContainer, messageText);
    var bValidate = this.validate;
    this.validate = function()
    {
        bValidate.call(this);
        if(!this.EmailRegExp.test(this.sender.value))
        {
            this.container.innerHTML = this.messageText;
            this.IValid = false;
        }
        else
        {
            var msg = this.container.innerHTML;
            this.container.innerHTML = (msg == this.messageText) ? "" : msg;
            this.IValid = true;
        }
    }
}

//Text Validation Object class
function TextValidationObject (objectId, regExp, messageContainer, messageText)
{
    this.regExp = new RegExp(regExp);
    this.constructor = BaseValidator;
    this.constructor.call(this, objectId, messageContainer, messageText);
    var bValidate = this.validate;
    this.validate = function()
    {
        bValidate.call(this);
        if(this.sender.value.match(this.regExp) != null)
        {
            this.container.innerHTML = this.messageText;
            this.IValid = false;
        }
        else
        {
            var msg = this.container.innerHTML;
            this.container.innerHTML = (msg == this.messageText) ? "" : msg;
            this.IValid = true;
        }
    }
}

// FormValidator class
function FormValidator(msgCss) 
{
    this.MessageCSS = msgCss;
    this.TextCollection = [];
    this.EmailCollection = [];
    this.RequiredCollection = [];
    this.LengthCollection = [];
    this.ConfirmCollection = [];
    this.getValueOf = getValueOf; 
}

function getValueOf(objectId, type)
{
    var sender = document.getElementById(objectId);
    var value = "";
    
    switch(type)
    {
        case "txt":
            return sender.value;
        case "ddl":
            return sender.options[sender.selectedIndex].value;
        case "chb":
            return sender.checked; 
    }
}

FormValidator.prototype.AddTextToValidate = function(objectId, regExp, messageContainer, messageText)
{
    var tObject = new TextValidationObject(objectId, regExp, messageContainer, messageText);
    tObject.messageCss = this.MessageCSS;
    this.TextCollection.push(tObject);
}

FormValidator.prototype.AddEmailToValidate = function(objectId, messageContainer, messageText)
{
    var eObject = new EmailValidationObject(objectId, messageContainer, messageText);
    eObject.messageCss = this.MessageCSS;
    this.EmailCollection.push(eObject);
}

FormValidator.prototype.AddRequiredFieldToValidate = function(objectId, objectType, messageContainer, messageText)
{
    var rObject = new RequiredValidationObject(objectId, objectType, messageContainer, messageText);
    rObject.messageCss = this.MessageCSS;
    this.RequiredCollection.push(rObject);
}
    
FormValidator.prototype.AddLengthToValidate = function(objectId, min, max, messageContainer, messageText)
{
    var lObject = new LengthValidationObject(objectId, min, max, messageContainer, messageText);
    lObject.messageCss = this.MessageCSS;
    this.LengthCollection.push(lObject);
} 

FormValidator.prototype.AddConfirmPairToValidate = function(firstObjectId, secondObjectid, messageContainer, messageText)
{
    var cObject = new ConfirmValidationObject(firstObjectId, secondObjectid, messageContainer, messageText);
    cObject.messageCss = this.MessageCSS;
    this.ConfirmCollection.push(cObject);
}
        
FormValidator.prototype.validate = function()
{
    for(i=0; i < this.TextCollection.length; i++)
    {
        this.TextCollection[i].validate();
    } 
    for(i=0; i < this.EmailCollection.length; i++)
    {
        this.EmailCollection[i].validate();
    }
    for(i=0; i < this.RequiredCollection.length; i++)
    {   
        this.RequiredCollection[i].validate();
    }
    for(i=0; i < this.LengthCollection.length; i++)
    {
        this.LengthCollection[i].validate();
    }
    for(i=0; i < this.ConfirmCollection.length; i++)
    {
        this.ConfirmCollection[i].validate();
    }
    
    if(CheckCollection(this.TextCollection) == true 
    && CheckCollection(this.EmailCollection) == true 
    && CheckCollection(this.RequiredCollection) == true 
    && CheckCollection(this.LengthCollection) == true
    && CheckCollection(this.ConfirmCollection) == true)
    {
        return true;
    }
    return false;  
}

FormValidator.prototype.dispose = function()
{
    this.TextCollection = [];
    this.EmailCollection = [];
    this.RequiredCollection = [];
    this.LengthCollection = [];
    this.ConfirmCollection = [];
}


//*************************PRIVATE FUNCTIONS******************************
function CheckCollection(collection)
{
    for(i=0; i < collection.length; i++)
    {
        if(!collection[i].IValid)
            return false;
    }
    return true;
}
//*******************************END**************************************