var ValidatorTypes =
{
    Phone: 1,
    Email: 2,
    Name: 3,
    CompanyName: 4,
    Combobox: 5,
    Number: 6
}

function Validator(control, type, name)
{
    this.Name = name;
    this.control = control;
    this.Type = type;
    this.Validate = Validate;
    
    function Validate()
    {
        var re;
        var bOk = true;

        switch(this.Type)
        {
            case ValidatorTypes.Phone:
                re = /^(\+\d)?(\(\d+\))?(\s)*\d+((\s)*(-)?(\s)*\d+)*$/;
                break;
            case ValidatorTypes.Email:
                re = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*\s+<(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})>$|^(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})$/;
                break;
            case ValidatorTypes.Name:
                re = /^[^`~!@#$%\^&*_+=|\\/?<>";:'¹1234567890\[\]]+$/;
                break;
            case ValidatorTypes.CompanyName:
                re = /^[0-9a-zA-Z \-\.\'\,]+$/;
                break;
            case ValidatorTypes.Number:
                re = /^[1-9]\d*$/;
                break;
            case ValidatorTypes.Combobox:
                if (control.selectedIndex == 0)
                    bOk = false;
                break;
        }
        
        
        if (re)
        {
            var s = new String(this.control.value);
            bOk = re.test(s);
        }
            
        
        return bOk;
    }
}


function ValidatorsColletion(messageContainer, messagePrefix)
{ 
    this.MessagePrefix = messagePrefix;
    this.MessageContainer = messageContainer;
    this.Validators = new Array();
    this.ValidateAll = ValidateAll;
    this.Add = Add;
    
    function Add(control, type, name)
    {
        var val = new Validator(control, type, name);
        this.Validators.push(val);
    }
    
    function ValidateAll()
    {
        var sMessage = "";
        for(var i = 0; i < this.Validators.length; i++)
        {
            var val = this.Validators[i];
            if(!val.Validate())
            {
                if (sMessage.length > 0)
                    sMessage += ",";
                sMessage += " " + val.Name;
            }   
        }
        
        if (sMessage.length > 0)
        {
            sMessage = this.MessagePrefix + sMessage;
            messageContainer.innerHTML = sMessage;
            return false;
        }
        return true;
    }
    
}