/**
 * checkForm
 * version: 0.9
 * author: Nedik <tomas.nedela@religis.cz>
 * 
 * description:
 * - testuje vsechny formulare, ktere jsou na webu. pokud nějaké políčko formuláře má definovanou třídu class="required", je testován.
 * - další doplňující podporované třídy: check_email, check_phone
 *
 * updates:
 */

function CheckForm()
{
  var own = this;
  this.aError = new Array();
  this.aElementsForProcess = new Array('input', 'select', 'textarea');
  this.aElementsRadioAlreadyChecked = new Array();
  this.oFocusElement = false;

  this.init = function ()
  {
    if (!$)
    {
      alert('CheckForm needs JQUERY for operate.');
      return true;
    }
    
    // vezmu si vsechny formulare pod kontrolu
    $('form').bind('submit', function () {return own.checkFormOnSubmit(this);});

    return true;
  }

  this.checkFormOnSubmit = function (oForm)
  {
    own.aError = new Array();
    own.aElementsRadioAlreadyChecked = new Array();
    own.oFocusElement = false;
    
    // projdu veskere elementy FORMu
    for (i in own.aElementsForProcess)
    {
      
      $(oForm).find(own.aElementsForProcess[i]).each(function () {
        if ($(this).attr('type') == 'radio')
          own.processElementRadio(this);
        else
          own.processElement(this);
      });
    }

    if (own.aError.length > 0)
    {
      own.focusElement();
      own.throwError();
      return false;
    }
    else
    {
      return true;
    }
  }


  this.focusElement = function ()
  {
    if (own.oFocusElement)
    {
      offset = $(own.oFocusElement).offset();
      window.scrollTo(offset.top, offset.left);
      $(own.oFocusElement).focus();
    }
  }


  this.processElementRadio = function (oElement)
  {
    // zjistim, jestli uz jsem tento element jednou nezkoumal
    sElementName = $(oElement).attr('name');
    for (i in own.aElementsRadioAlreadyChecked)
    {
      if (own.aElementsRadioAlreadyChecked[i] == sElementName)
        return true;
    }
    
    bError = false;

    if ($(oElement).attr('rel'))
      sErrorMessage = $(oElement).attr('rel');
    else
      sErrorMessage = $(oElement).attr('name')+' musí být zvolen';

    if ($(oElement).hasClass('required'))
    {
      // najdu vsechy tyto elementy dle jejich NAME a zjistim, jestli je nejaky z nich je checked
      var bChecked = false;
      $('input[name="'+sElementName+'"]').each( function () {
        if (this.checked == true) bChecked = true;
      });

      if (bChecked === false)
      {
        own.aError[own.aError.length] = sErrorMessage;
      }
    }
    own.aElementsRadioAlreadyChecked[own.aElementsRadioAlreadyChecked.length] = sElementName;
  }


  this.processElement = function (oElement)
  {
    bError = false;
    
    if ($(oElement).attr('rel'))
      sErrorMessage = $(oElement).attr('rel');
    else if ($(oElement).attr('title'))
      sErrorMessage = $(oElement).attr('title');
    else
      sErrorMessage = $(oElement).attr('name')+' musí být vypln\u011bn';


    // required
    if ($(oElement).hasClass('required') && $(oElement).val() == '')
    {
      own.aError[own.aError.length] = sErrorMessage;
      bError = true;
    }

    // required AND check_email
    if (!bError && $(oElement).hasClass('check_email') && $(oElement).val().length)
    {
      if (!own.validEmail($(oElement).val()))
      {
        own.aError[own.aError.length] = sErrorMessage;
        bError = true;
      }
    }

    // required AND check_phone
    if (!bError && $(oElement).hasClass('check_phone') && $(oElement).val().length)
    {
      if (!own.check_phone($(oElement).val()))
      {
        own.aError[own.aError.length] = sErrorMessage;
        bError = true;
      }
    }



    // oznacim element warningem
    own.markElement(oElement, bError);
  }


  this.check_phone = function ( pnumber, length ) {     
     if (!length) length = 9;
     pnumber = pnumber.replace(/^\s+|\s+$/g,""); // TRIM funkce
     pnumber = pnumber.replace(/\s+/g,""); // odstranim dalsi nepotrebne znaky
     pnumber = pnumber.replace(/\+420+/g,""); // odstranim dalsi nepotrebne znaky
     pnumber = pnumber.replace(/00420+/g,""); // odstranim dalsi nepotrebne znaky
     pnumber = pnumber.replace(/\++/g,""); // odstranim dalsi nepotrebne znaky

     if( pnumber.length != length ) {
         return false;
     }

     return true;
  }


  this.markElement = function(oElement, bError)
  {
    if (bError)
    {
      $(oElement).addClass('checkFormWarning');
      if (!own.oFocusElement) own.oFocusElement = oElement;
    }
    else
      $(oElement).removeClass('checkFormWarning');
  }


  this.throwError = function()
  {
    sMessage = 'Formulá\u0159 nemohl být odeslán, neboť:'+"\n\n";
    for (i in own.aError)
    {
      sMessage += '   -'+own.aError[i]+"\n";
    }
    sMessage += "\n"+'Opravte chyby a ode\u0161lete formulá\u0159 znovu';
    alert(sMessage);
  }


  this.validEmail = function(sEmail)
  {
    reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (reg.test(sEmail) == false) return false;
    else return true
  }
}

var oCheckForm = new CheckForm();
$(document).ready(oCheckForm.init);