﻿    
    var nvc;
    var FieldValidators = new FieldValidators();
    var ForbrugerlivValidationResult;
    var ValidationGroup = '';
    var Validation_CB = null;
    var EventTarget = '';
    var EventArgument = '';
    var theform;
    
    function FieldValidator(ctrlName,ruleName,vldParams,errLabelName,validationGroup,callbackFunc)
    {
        this.controlName = ctrlName;
        this.rule = ruleName;
        this.params = vldParams;
        this.errorLabel = errLabelName;
        this.callbackFunction = callbackFunc;
        this.validationGroup = validationGroup;
        this.toString = this.controlName + ' | ' + this.rule + ' | ' + this.params + ' | ' + this.validationGroup + ' | ' + this.callbackFunction;
    }
    
    function FieldValidators()
    {
        this.validators = new Array();
        this.getElementByFieldName = fn_getValidatorByFieldName;
        this.push = fn_addValidator;
        this.isValid = true;
    }
    
    function fn_getValidatorByFieldName(fieldName)
    {
        for(var i=0; i<this.validators.length; i++)
        {
            if (this.validators[i].controlName == fieldName) 
            {
                return this.validators[i];
            }
        }
        return null;
    }
    
    function fn_addValidator(validator)
    {
        this.validators.push(validator);
    }
    
    
    function RequestValidation(validationGroup, validation_CB, eventTarget, eventArgument)
    {
            FieldValidators.isValid = false;
        
            ValidationGroup = validationGroup;
            EventTarget = eventTarget;
            EventArgument = eventArgument;
            
            if (validation_CB)
            {
                Validation_CB = validation_CB;
            }

            nvc = Array();
            
            var rowId = 0;
            var validatorType = null;
            for(var i=0; i<FieldValidators.validators.length; i++) 
            {
                var validator = FieldValidators.validators[i];
                if (ValidationGroup)
                {
                    if (validator.validationGroup != ValidationGroup) 
                    {
                        continue;
                    }
                }
                var elem = document.getElementById(validator.controlName);
                if (elem) 
                {
                    if (validator.rule) 
                    {
						nvc[rowId] = new Ajax.Web.NameValueCollection([]);
                        nvc[rowId].add('field', validator.controlName);
                        nvc[rowId].add('value', elem.value);
                        nvc[rowId].add('rule', validator.rule);
                        nvc[rowId].add('vldParams', validator.params ? validator.params : '');
                        rowId++;
                    }
                }
            }
            
            Forbrugerliv.Common.Validation.MainValidator.Validate(nvc, ForbrugerlivValidate_callback);
            
    }
    
  
    function ForbrugerlivValidate_callback(result)
    {
        if (result) 
        {
            if (result.error == null)
            {
                var nameValueColl = result.value;
                var fieldName;
                var message;
                var errorCount = 0;
                for(var i=0; i<nameValueColl.keys.length; i++)
                {
                    fieldName = nameValueColl.keys[i];
                    message = nameValueColl.values[i];
                    var control = document.getElementById(fieldName);
                    theform = control.form;
                    var validator = FieldValidators.getElementByFieldName(fieldName);
                    if (ValidationGroup)
                    {
                        if (validator.validationGroup != ValidationGroup)
                        {
                            continue;
                        }
                    }
                    var errLabel = document.getElementById(validator.errorLabel);
                    if (control == null) continue;
                    if (message.length > 0)
                    {
                        if (typeof(validator.callbackFunction) == 'function')
                        {
                            validator.callbackFunction(result,validator,message);
                        }
                        else 
                        {
                            MarkInvalidControl(control, errLabel, message);
                        }
                        errorCount++;      
                    }
                    else 
                    {
                         MarkValidControl(control, errLabel);
                    }
                }
                
                if (errorCount == 0) 
                {
                    FieldValidators.isValid = true;
                }
                else 
                {
                    FieldValidators.isValid = false;
                }
            }
            else 
            {
                FieldValidators.isValid = false;
                alert('Error: ' + result.error.Message);
            }
        }
        
        if (typeof(Validation_CB) == 'function')
        {
            Validation_CB();
        }
  }
  
  function alertme(result,validator,message)
  {
        alert(message);
  }

  function MarkInvalidControl(control, errLabel, message)
  {
        if (errLabel) 
        {
            errLabel.innerHTML = message;
            errLabel.style.visibility = 'visible';
        }
  }
  
  function MarkValidControl(control, errLabel)
  {
        if (errLabel) 
        {
            errLabel.innerHTML = '';
            errLabel.style.visibility = 'hidden';
        }        
  }
  
  function Submitter()
  {
       if (FieldValidators.isValid)
       {
            if (!theform)
            {
                theform = document.forms[0];
            }
            
            if (EventTarget != null && EventTarget != '')
            {
                __doPostBack(EventTarget, EventArgument != null ? EventArgument : '');
            }
            else
            {
                theform.submit();
            }
            
       }
  }
  
  function ResetValidationResult()
  {
     FieldValidators.isValid = true;
  }
