

var offForm = false;
var mitonForm = Class.create();
mitonForm.prototype = { 
  initialize: function(autoAttach, autoTranslate, inputElements, inputTranslations, outputElement, offColorize, noteElements, noteTranslations) {
    if(offColorize) {
      this.colorize = false;
    } else {
      this.colorize = true;    
    }
    if(outputElement) {
      this.outputElement = outputElement;
    }
    this.autoRequire = true;
    this.requiredFields = new Array();
    this.translationFields = new Array();
    this.noteFields = new Array();
    this.translationNoteFields = new Array();
    this.requireCount = 0;
    this.noteCount = 0;
    this.inputs = document.getElementsByTagName('input');
    if(autoTranslate) {
      this.autoTranslate = autoTranslate;
    }
    if(autoAttach) {
      this.autoAttachFields();
    }
    if(inputTranslations) {
      this.inputTranslations = inputTranslations;
    }
    if(noteElements) {
      this.noteElements = noteElements;
    }
    //alert(this.noteElements);
    if(noteTranslations) {
      this.noteTranslations = noteTranslations;
    }
    if(inputElements) {
      this.inputElements = inputElements;
    }
    if(inputElements || noteElements) {
      this.prepareInputs();
    }
    if(0 != this.requireCount) {
      this.createRequired();
    }
  },

  autoAttachFields: function() {
    for(var i=0, length = this.inputs.length; i < length; i++) {
      if('required' == this.inputs[i].className) {
        this.requiredFields[this.requireCount] = this.inputs[i];
        this.requireCount++;
      } else {
        //testing quickform required element
        var parentEl = this.inputs[i].parentNode;
        if(parentEl) {
          var parentEl2 = parentEl.parentNode;
          try {
            var testRequired = false;
            var ttt = parentEl2.getElementsByClassName('required');
            if(ttt.length > 0) {
              var testRequired = true;
            }
	  } catch (err) {
            var iesucks = parentEl2.getElementsByTagName('*');
            var testRequired = false;
            for(var ie=0, lengthie = iesucks.length; ie < lengthie; ie++) {
              if('required' == iesucks[ie].className) {
                var testRequired = true;
              }
            }
          }
          if(testRequired) {
             if(this.autoTranslate) {
                var testTranslation = parentEl2.getElementsByTagName('label');
                if(testTranslation.length > 0) {
                   for(var i2=0, length2 = testTranslation.length; i2 < length2; i2++) {
                     var label = testTranslation[i2].innerHTML;
                     var tr = label.replace(/<b.*<\/b>/, "");
                     var tr = tr.replace(/:/, "");
                     this.translationFields[this.requireCount] = tr;
                   }
                }
             }   
             this.requiredFields[this.requireCount] = this.inputs[i];
             this.requireCount++;               
          }
        }
      }
    }
  },
  
  prepareInputs: function() {
    if(this.inputElements) {
       var iEls = this.inputElements.split(',');
       if(this.inputTranslations) {
         var iTra = this.inputTranslations.split(',');
       } else {
         var iTra = new Array();
       }
       for(var i=0, length = iEls.length; i < length; i++) {
         if(iTra[i]) {
           this.attachField(iEls[i], iTra[i]);
         } else {
           this.attachField(iEls[i], false);
         }
       }
    }   
    if(this.noteElements) {
       var iNels = this.noteElements.split(',');
       if(this.noteTranslations) {
         var iTraN = this.noteTranslations.split(',');
       } else {
         var iTraN = new Array();
       }
       for(var iN=0, lengthN = iNels.length; iN < lengthN; iN++) {
         if(iTraN[iN]) {
           this.attachNoteField(iNels[iN], iTraN[iN]);
         } else {
           this.attachNoteField(iNels[iN], false);
         }
       }
    }
  },
  
  attachField: function(inputName, inputTranslation) {
    for(var i=0, length = this.inputs.length; i < length; i++) {
      if(inputName == this.inputs[i].name) {
         if(inputTranslation) {
           this.translationFields[this.requireCount] = inputTranslation;
         }
         this.requiredFields[this.requireCount] = this.inputs[i];
         this.requireCount++;
      }
    }
  },
  
  attachNoteField: function(inputName, inputTranslation) {
    for(var i=0, length = this.inputs.length; i < length; i++) {
      if(inputName == this.inputs[i].name) {
         if(inputTranslation) {
           this.translationNoteFields[this.noteCount] = inputTranslation;
         }
         this.noteFields[this.noteCount] = this.inputs[i];
         this.noteCount++;
      }
    }
  },
  
  checkForm: function() {
    if(offForm) {
      return true;
    }
    for(var i=0, length = this.required.length; i < length; i++) {
            
      // zalozfirmu vyhybka pro cislo domu popisne/evidencni
      if ($(this.required[i].name+'E') && ((this.required[i].name.toLowerCase() == 'housenum') || (this.required[i].name.toLowerCase() == 'chousenum') || (this.required[i].name.toLowerCase() == 'housenumcr'))) {
         
         var elHouseNumE = $(this.required[i].name+'E');
         if (this.required[i].value.blank() && elHouseNumE.value.blank()) {
            if (this.colorize) {
               this.required[i].style.border = '1px solid red';
               this.required[i].style.background = '#ffafaf';
               elHouseNumE.style.border = '1px solid red';
               elHouseNumE.style.background = '#ffafaf';
            }
            var text = translate.mitonform.emptyHouseNum;
            if (!this.outputElement) {
               alert(text);
            } else {
               $(this.outputElement).innerHTML = text;
            }
            this.required[i].focus();
            return false;
         }
         
      } else {
         
         if(this.required[i].value.blank()) {
           if(this.colorize) {
             this.required[i].style.border = '1px solid red';
             this.required[i].style.background = '#ffafaf';
           }
           if(this.translations[i] && ('' != this.translations[i])) {
             var text = translate.mitonform.required + ' ' + this.translations[i] + '!';
           } else {
             var text = translate.mitonform.required_all;
           }
           if(!this.outputElement) {
             alert(text);
           } else {
             $(this.outputElement).innerHTML = text;
           }
           this.required[i].focus();
           return false;
         }
         
      }
    }
    
    for(var i2=0, length2 = this.notes.length; i2 < length2; i2++) {
      if(this.notes[i2].value.blank()) {
        if(this.colorize) {
          this.notes[i2].style.border = '1px solid green';
          this.notes[i2].style.background = '#afffaf';
        }
        if(this.noteTranslations[i2] && ('' != this.noteTranslations[i2])) {
          var text = translate.mitonform.recommend_start + ' ' + this.noteTranslations[i2] + translate.mitonform.recommend_end;
        } else {
          var text = translate.mitonform.recommend_all;
        }
        var con = confirm(text);
        if(!con) {
          this.notes[i2].focus();
          return false;
        }  
      }
    }
  },
  
  createRequired: function() {
    var forms = document.getElementsByTagName('form');
    for(var i=0, length = forms.length; i < length; i++) {
      var reqCount = 0;
      var noteCount = 0;
      forms[i].required = new Array();
      forms[i].translations = new Array();
      forms[i].notes = new Array();
      forms[i].noteTranslations = new Array();
      forms[i].colorize = this.colorize;
      forms[i].outputElement = this.outputElement;
      var fChilds = forms[i].getElementsByTagName('input');
      for(var i2=0, length2 = fChilds.length; i2 < length2; i2++) {
        for(var i4=0, length4 = this.noteFields.length; i4 < length4; i4++) { 
          if(this.noteFields[i4] == fChilds[i2]) {
            if(this.translationNoteFields[i4]) {
              forms[i].noteTranslations[noteCount] = this.translationNoteFields[i4];
            }
            forms[i].notes[noteCount] = this.noteFields[i4];
            noteCount++;
            forms[i].onsubmit = this.checkForm; 
          }
        }
        for(var i3=0, length3 = this.requiredFields.length; i3 < length3; i3++) {
          if(this.requiredFields[i3] == fChilds[i2]) {
            if(this.translationFields) {
              forms[i].translations[reqCount] = this.translationFields[i3];
            }
            forms[i].required[reqCount] = this.requiredFields[i3];
            reqCount++;
            forms[i].onsubmit = this.checkForm; 
          }
        }
      }
    }
  }
}

