var emailRegExp = {re: "^[_a-z\\d\\-\\.]+@[_a-z\\d\\-]+(\\.[_a-z\\d\\-]+)+$", m: "ig"};
var urlRegExp = {re: "^([_a-z\\d\\-]+(\\.[_a-z\\d\\-]+)+)((/[ _a-z\\d\\-\\\\\\.]+)+)*$", m: "ig"};

var validators = new Array();

function checkAllValidators(showerror)
{
  var result = true;
  for (var i=0; i<validators.length; i++)
    if (!(result = validators[i].check()) && showerror) break;
  if (!result && showerror && validators[i].error != "")
    alert("Ошибка: "+validators[i].error);
  return result;
}

function checkAllFormValidators(f, showerror)
{
  var result = true;
  for (var i=0; i<validators.length; i++)
    if ((f == validators[i].element.form) && !(result = validators[i].check()) && showerror) break;
  if (!result && showerror && validators[i].error != "")
    alert("Ошибка: "+validators[i].error);
  return result;
}

function checkElementValidator(el, showerror, fs)
{
  var result = true;
  for (var i=0; i<validators.length; i++)
    if (el == validators[i].element)
    {
      result = validators[i].check(fs);
      if (!result && showerror && validators[i].error != "")
        alert("Ошибка: "+validators[i].error);
      break;
    }
  return result;
}


function Validator(a)
{
  validators[validators.length] = this;
  this.element = !a['element']?this.element:a['element'];
  this.regexp = !a['regexp']?this.regexp:a['regexp'];
  this.minlen = !a['minlen']?this.minlen:a['minlen'];
  this.maxlen = !a['maxlen']?this.maxlen:a['maxlen'];
  this.minvalue = !a['minvalue']?this.minvalue:a['minvalue'];
  this.maxvalue = !a['maxvalue']?this.maxvalue:a['maxvalue'];
  this.trim = a['trim']==undefined?this.trim:a['trim'];
  this.onlyint = a['onlyint']==undefined?this.onlyint:a['onlyint'];
  this.onlynum = a['onlynum']==undefined?this.onlynum:a['onlynum'];
  this.nonempty = a['nonempty']==undefined?this.nonempty:a['nonempty'];
  this.fs = !a['fs']?this.fs:a['fs'];
}

Validator.prototype =
{
  error: "",
  element: null,
  regexp: 
  {
    re: "",
    m: ""
  },
  minlen: -1,
  maxlen: -1,
  minvalue: -1.79E+308,
  maxvalue: 1.79E+308,
  trim: true,
  onlyint: false,
  onlynum: false,
  nonempty: false,
  fs:
  {
    setfocus: true,
    selectall: true,
    classname: ""
  },
  check: function(_fs)
  {
    oldfs = this.fs;
    if (_fs) this.fs = _fs;
    var v, result = true; 
    this.error = "";

    if (!this.element || ((v = this.element.value) == undefined)) return false;

    v = v.toString();
    if (this.trim) 
    {
      v = v.replace(/(^\s*)|(\s*$)/g, "");
      this.element.value = v;
    }
    if (!result || (this.minlen != -1 && v.length < this.minlen)) 
    {
      this.error = result?"Длина строки меньше минимального значения ("+this.minlen+")!":this.error;
      result = false;
    }
    if (!result || (this.maxlen != -1 && v.length > this.maxlen))
    {
      this.error = result?"Длина строки больше максимального значения ("+this.maxlen+")!":this.error;
      result = false;
    }
    var n = new Number(v.replace(/[,]/ig, "."));
    if (!result || (this.onlyint && (isNaN(n) || n != Math.round(n))))
    {
      this.error = result?"Введено неверное целое значение!":this.error;
      result = false;
    }
    if (!result || (this.onlynum && isNaN(n)))
    {
      this.error = result?"Введено неверное числовое значение!":this.error;
      result = false;
    }
    if (!result || ((this.onlynum || this.onlyint) && (n < this.minvalue))) 
    {
      this.error = result?"Число меньше минимального значения ("+this.minvalue+")!":this.error;
      result = false;
    }
    if (!result || ((this.onlynum || this.onlyint) && (n > this.maxvalue))) 
    {
      this.error = result?"Число больше максимального значения ("+this.maxvalue+")!":this.error;
      result = false;
    }
    if (!result || (this.nonempty && (v.replace(/\s+/ig, "") == "")))
    {
      this.error = result?"Строка не может быть пустой!":this.error;
      result = false;
    }
    if (result && this.regexp.re != "" && v.length != 0)
    {
      try
      {
        var re = new RegExp(this.regexp.re, this.regexp.m);
        result = re.test(v);
      }
      catch(e)
      { 
        result = false;
      }
      this.error = !result?"Введено неверное значение!":this.error;
    }

    if (!result)
    {
      if (this.fs.setfocus && this.element.focus) this.element.focus();
      if (this.fs.selectall && this.element.select) this.element.select();
      if (this.fs.classname != "")
      {
        if (this.element.oldclass == undefined) this.element.oldclass = this.element.className;
        this.element.className = this.fs.classname;
      }
    }
    else
    {
      if (this.element.oldclass != undefined) this.element.className = this.element.oldclass;
    }

    this.fs = oldfs;
    return result;
  } 

    
}

