
function Validation(obj, title, required, min_length, max_length, reg_exp, example, twin_obj)
{
   this.obj = obj;
   this.title = title;
   this.reg_exp = reg_exp;
   this.example = example;
   this.required = required;
   this.min_length = min_length;
   this.max_length = max_length;
   this.twin_obj = twin_obj;

   this.validate = Validation_validate;
}

function Validation_validate()
{
   var valid = true;

   if ( typeof(this.obj.length) != "undefined" )
   {
      if ( this.required )
      {
         if ( typeof(this.obj.selectedIndex) != "undefined" )
         {
            if ( (this.obj.selectedIndex == -1) || 
                 (this.obj[this.obj.selectedIndex].value == '') )
            {
               valid = false;

               this.obj.focus();
            }
         }
         else
         {
            var checked_found = false;
            var i;

            for (i = 0; i < this.obj.length; i++)
            {
               if ( this.obj[i].checked )
               {
                  checked_found = true;

                  break;
               }
            }

            if ( !checked_found )
            {
               valid = false;

               this.obj[0].select();
               this.obj[0].focus();
            }
         }

         if ( !valid )
         {
            alert("В поле \'" + this.title + "\' должен быть выбран хотя бы один пункт!" );
         }
      }
   }
   else if ( typeof(this.obj.value) != "undefined" )
   {
      if ( this.obj.value == '' )
      {
         if ( this.required )
         {
            alert("Поле \'" + this.title + "\' должно быть заполнено!" );
            valid = false;
         }
      }
      else
      {
         if ( this.min_length && (this.obj.value.length < this.min_length) )
         {
            alert("Неверная длина поля \'" + this.title + "\'.\n\nПоле должно содержать не менее " + this.min_length + " символов!");
            valid = false;
         }
         else if ( this.max_length && (this.obj.value.length > this.max_length) )
         {
            alert("Неверная длина поля \'" + this.title + "\'.\n\nПоле должно содержать не более " + this.max_length + " символов!");
            valid = false;
         }
         else if ( (this.reg_exp != null) && (this.obj.value.match(this.reg_exp) == null) )
         {
            alert("Неверный формат поля \'" + this.title + "\'.\n\nПример правильного формата:\n\n" + this.example);
            valid = false;
         }   
      }

      if ( this.twin_obj && (this.obj.value != this.twin_obj.value) )
      {
         alert("Содержимое поля \'" + this.title + "\' не совпадает со своим подтверждением. ");
         valid = false;
      }

      if ( !valid )
      {
         this.obj.select();
         this.obj.focus();
      }
   }

   return valid;
}

function InputForm(obj)
{
   this.obj = obj;
   this.stop_focus = null;
   this.vlist = new Array();
   this.v_index = 0;

   this.addValidation = InputForm_addValidation;
   this.stopFocus = InputForm_stopFocus;
   this.initialize = InputForm_initialize;
   this.onSubmit = InputForm_onSubmit;
   this.submit = InputForm_submit;
}

function InputForm_addValidation(key, title, required, min_length, max_length, reg_exp, example, twin_key)
{
   this.vlist[this.v_index] = new Validation(this.obj.elements[key], title, required, min_length, max_length, reg_exp, example, twin_key ? this.obj.elements[twin_key] : null);
   this.v_index++;
}

function InputForm_stopFocus(key)
{
   this.stop_focus = key;
}

function InputForm_initialize()
{
   var i;
   var elem;
   var submit;
   var button;

   for (i = 0; i < this.obj.elements.length; i++)
   {
      elem = this.obj.elements[i];

      if ( typeof(elem.type) != "undefined" )
      {
         if ( elem.type == "submit" )
         {
            submit = elem;
         }
         else if ( elem.type == "button" )
         {
            button = elem;
         }
      }

      if ( ((typeof(elem.name) != "undefined") &&
            (elem.name == this.stop_focus)) ||
           ((typeof(elem.type) != "undefined") &&
            (elem.type != "hidden") &&
            (typeof(elem.value) != "undefined") &&
            (elem.value == "")) )
      {
         elem.focus();
         break;
      }
   }

   if ( i == this.obj.elements.length )
   {
      if ( submit )
      {
         submit.focus();
      }
      else if ( button )
      {
         button.focus();
      }
   }
}

function InputForm_onSubmit()
{
   var i;
   var valid = true;

   for (i = 0; i < this.vlist.length; i++)
   {
      if ( !this.vlist[i].validate() )
      {
         valid = false;
         break;
      }
   }

   return valid;
}

function InputForm_submit()
{
   if ( this.onSubmit() )
   {
      this.obj.submit();
   }
}

