/*
 *  Klasa Validate umozliwiajaca latwe zarzadzanie poprawnoscia wpisywanych danych w formularzu.
 *  @date 31. sierpnia 2007, 09:17:05
 *  @author lukasz@szakul.net
 */

var Validate = function(sForm, sMode) 
{
  this.sForm = sForm;
  this.aRules = new Array();
  this.aToTest = new Array(); 
  this.sMode = (sMode != 'ico') ? 'hint' : 'ico';
  
  
  if (typeof Validate.__initialized == "undefined") 
  {	
      Validate.prototype.addRule = function(sInput, sFunc, sError) {
        if (this.aRules.length) {
            for (var i = 0; i < this.aToTest.length; i++) {
                 var bSearch = (this.aToTest[ i ] == sInput) ? true : false;
            } 
            if (!bSearch) { this.aToTest.push(sInput); }
        } else { this.aToTest.push(sInput); }
        this.aRules.push([sInput, sFunc, sError]);
      }
     
      Validate.prototype.isEmpty = function(o) { return o.value.length == 0 ? false : true; }
     
      Validate.prototype.isEmail = function(o) { return /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/.test(o.value); }
      
      Validate.prototype.isCaptchaMail = function(o) { return Boolean(o.value == 'kusi'); }
      
      Validate.prototype.isCaptchaBook = function(o) { return Boolean(o.value == 'hktz'); }
     
      Validate.prototype.check = function(oInput) {
        if (oInput.name != '') {
            for (var i = 0; i < this.aRules.length; i++) {
                 if (this.aRules[ i ][0] == oInput.name) {
                     var sFuncName = this.aRules[ i ][1];
                     if (eval('this.' + sFuncName)) {
                         var bResult = eval('this.' + sFuncName + '(oInput)');
                         this.hint(oInput, bResult, this.aRules[ i ][2]);
                         if (!bResult) { return false; }
                     }
                 }
            }
            return true;
        }
      }
     
      Validate.prototype.hint = function(oInput, bResult, sError) {
        if (bResult == true) {
            if (this.sMode == 'hint') { oInput.previousSibling.style.display = 'none'; } else {
                oInput.nextSibling.style.display = 'inline';
                oInput.nextSibling.className = 'validate-passed';
                oInput.nextSibling.title = '';
            } } else {   
            if (this.sMode == 'hint') {
                oInput.previousSibling.style.display = 'block';
                oInput.previousSibling.innerHTML = sError;
            } else {
                oInput.nextSibling.style.display = 'block';
                oInput.nextSibling.className = 'validate-error';
                oInput.nextSibling.title = oInput.title;
            }
        }
      }
      
      Validate.prototype.setMode = function(oInput) {
        var sTag = (this.sMode == 'hint') ? 'div' : 'span';
        var oHint = document.createElement(sTag);
        oHint.style.display = 'none';
        if (this.sMode == 'hint') { oInput.parentNode.insertBefore(oHint, oInput); } else { oInput.parentNode.insertBefore(oHint, oInput.nextSibling); }    
      }
     
      Validate.prototype.validate = function() {
         if (this.aToTest.length) {
             for (var i = 0; i < this.aToTest.length; i++) {
                  var oInput = document.forms[ this.sForm ].elements[ this.aToTest[ i ] ];
                  if (!this.check(oInput)) {
                      alert(oInput.title);
                      oInput.focus();
                      return false;
                  }
             }
       }
       return true;
     }
 
     Validate.prototype.init = function() {
       var oInstance = this;
       this.addEvent(window, 'load', function() {
         if (oInstance.aToTest.length) {
             for (var i = 0; i < oInstance.aToTest.length; i++) {
                  var oInput = document.forms[ oInstance.sForm ].elements[ oInstance.aToTest[ i ] ];
                  oInstance.addEvent(oInput, 'blur', function() {
                    var o = browser.ie ? window.event.srcElement : this;
                    oInstance.check(o);
                  });
                  oInstance.setMode(oInput);
             }

             document.forms[ oInstance.sForm ].onsubmit = function() { return oInstance.validate(); } 
         } 
       });
       delete oInstance;
     }
     
     Validate.prototype.addEvent = function(oTarget, sEventType, fnHandler) {
       if (oTarget.addEventListener) {
           oTarget.addEventListener(sEventType, fnHandler, false);
       } else if (oTarget.attachEvent) {
           oTarget.attachEvent("on"+sEventType, fnHandler);
       } else {
           oTarget["on"+sEventType] = fnHandler;
       }
     }
     Validate.__initialized = true;
  }
}