﻿     
      var userLoginMask = "^[0-9a-z_\\.\\-]+$";
      var userNameMask = "^[a-zа-я]+$";
      var userPasswordMask = "^[a-z0-9]+$";
      var userEmailMask = "^[0-9a-z_\\.\\-]+@[0-9a-z_^.\\-]+\\.[a-z]{2,3}$";
      var langNameMask = "^[a-z]+$";
      var xslFileMask = "\\.[x][s][l]$";
      var pathMask = "^[/][a-z0-9_\\/\\.\\-]+[/]$";
      
      var commonErrorMessage = "*";
            
      function ValidateObj(controlToValidate, controlToCompare, mask, errorMessage, displayContainer){
        this.controlToValidate = controlToValidate;
        this.controlToCompare = controlToCompare;
        this.mask = mask;
        this.errorMessage = errorMessage;
        this.displayContainer = displayContainer;
      }
      
      function FormValidate(validateObjects, errorsDisplayContainer){
        
        errors = [];
        
        for (i = 0; i < validateObjects.length; i++){
            controlToValidate = document.getElementById(validateObjects[i].controlToValidate);
            mask = validateObjects[i].mask;
            errorMessage = validateObjects[i].errorMessage;
            displayContainer = document.getElementById(validateObjects[i].displayContainer);

            if (validateObjects[i].controlToCompare != null){ 
                controlToCompare = document.getElementById(validateObjects[i].controlToCompare);
                if (!CompareValidate(controlToValidate, controlToCompare, displayContainer, commonErrorMessage)) errors.push(errorMessage);
            }
            else{
                if (!RegularExpressionValidate(controlToValidate, mask, displayContainer, commonErrorMessage)) errors.push(errorMessage);
            }    
        }
        
        if (errorsDisplayContainer != null){
        
            if (errors.length == 0){ errorsDisplayContainer.innerHTML = ""; validateObjects = [];return true;}
        
            errorMessage = "";
            for (i = 0; i < errors.length; i++){
                errorMessage += errors[i] + "<br />";
            }
            errorsDisplayContainer.innerHTML = errorMessage;  validateObjects = [];
            return false;
        }
      }
      
      function RegularExpressionValidate(controlToValidate, regularExpression, displayContainer, errorMessage){
        if (regularExpression=='') regularExpression='^[A-Za-zа-яА-Я_0-9]{3,255}$';//применяется для проверки значение имени поля или группы шаблона
        reg = new RegExp(regularExpression, "i");
        if (reg.test(controlToValidate.value))
        {
            displayContainer.innerHTML = "";
       //     alert('true');
            return true;
            
        }
        else
        {
            displayContainer.innerHTML = errorMessage;
      //      alert('false');
            
            return false;
        }
      }
      
      function CompareValidate(controlToValidate, controlToCompare, displayContainer, errorMessage){
        
        if (controlToValidate.value == ""){
            displayContainer.innerHTML = errorMessage; 
            return false; 
        }
        if (controlToValidate.value == controlToCompare.value)
        {
            displayContainer.innerHTML = "";
            return true;
        }
        else
        {
            displayContainer.innerHTML = errorMessage;
            return false;
        }
      }

      function CheckChar(e, mask){
        var keynum;
        var keychar;
        
        if(window.event) // IE
        {
            keynum = e.keyCode;
        }
        else if(e.which) // Netscape/Firefox/Opera
        {
            keynum = e.which;
            if (e.which == 0)
                keynum = e.keyCode;
//            alert(keynum);
        }
        keychar = String.fromCharCode(keynum);
//        alert(keychar);

        if (e.keyCode == 0){
            reg = new RegExp(mask, "i");
            if (reg.test(keychar)){
                return true;
            }
        }
        
        //     Backspace        delete              left                top               right               down           tab
        if (e.keyCode == 8 || e.keyCode == 46 || e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 39 || e.keyCode == 40 || e.keyCode == 9)
        {
            return true;
        }
        
        return false;
      }
      
      function CheckPressedKey(sender, mask, displayContainer){
        i = 0;
        while (i < sender.value.length){
            if (!CheckChar(sender.value.charAt(i), mask)){
                sender.value = sender.value.substring(0, i) + sender.value.substring(i, sender.value.length - 1);
                i=-1;
            }
            i++;
        }
        return;
      }