/**
 * Class for contact form box (frontend)
 *
 * @package ContactFormBoxFe
 * @category Module
 * @author Mirek Bily
 * @copyright e-invent s.r.o.
 */
function ContactFormBoxFe() {

  /** @var {object} */
  var self = this;


  /**
	 * Class init jQuery
	 * @return {void}
	 */
  self.jq_init = function() {
    // send form
    $('#frmContactBox-form').live('submit', function() {
      return self.validate();
    });

    // focus fields
    $('#frmContactBox-name').live('focus', function() {
      self.hideValue(this, 'Vaše jméno');
    });
    $('#frmContactBox-email').live('focus', function() {
      self.hideValue(this, 'Váš e-mail');
    });
    $('#frmContactBox-message').live('focus', function() {
      self.hideValue(this, 'text Vašeho dotazu...');
    });
    $('#frmContactBox-code').live('focus', function() {
      self.hideValue(this, 'Opište kód z obrázku');
    });

    // blur fields
    $('#frmContactBox-name').live('blur', function() {
      self.showValue(this, 'Vaše jméno');
    });
    $('#frmContactBox-email').live('blur', function() {
      self.showValue(this, 'Váš e-mail');
    });
    $('#frmContactBox-message').live('blur', function() {
      self.showValue(this, 'text Vašeho dotazu...');
    });
    $('#frmContactBox-code').live('blur', function() {
      self.hideValue(this, 'Opište kód z obrázku');
    });
  };


  /**
	 * Validate form data and send.
	 * @return {void}
	 */
  self.validate = function() {
    var error = new String();
    var emailRegxp = /^[^@\s]+@[^@\s]+\.[a-z]{2,10}$/i;
    var name = $('#frmContactBox-name').val();
    var email = $('#frmContactBox-email').val();
    var message = $('#frmContactBox-message').val();
    var code = $('#frmContactBox-code').val();

    if (name.length == 0 || name == 'Vaše jméno') {
      error += 'Jméno je povinná položka\n';
      $('#frmContactBox-name').focus();
    } else if (email.length == 0 ||  email == 'Váš e-mail') {
      error += 'E-mail je povinná položka\n';
      $('#frmContactBox-email').focus();
    } else if (!emailRegxp.test(email)) {
      error += 'E-mail je povinná položka\n';
      $('#frmContactBox-email').focus();
    } else if (message.length == 0 || message == 'text Vašeho dotazu') {
      error += 'Napište text Vašeho dotazu\n';
      $('#frmContactBox-message').focus();
    } else if (code.length == 0 || code == 'Opište kód z obrázku') {
      error += 'Opište kód z obrázku\n';
      $('#frmContactBox-code').focus();
    }


    if (error.length != 0) {
      alert(error);

      return false;
    } else {
      return true;
    }
  };


  /**
	 * Hides default value.
	 * @return {void}
	 */
  self.hideValue = function(e, value) {
    if ($(e).val() == value) {
      $(e).val('');
      $(e).focus();
    }
  };


  /**
	 * Shows default value.
	 * @return {void}
	 */
  self.showValue = function(e, value) {
    if ($(e).val() == '')
      $(e).val(value);
  };

}



// call class init
$(document).ready(function(){
  var contactFormBoxFe = new ContactFormBoxFe();
  contactFormBoxFe.jq_init();
});

