/**
 * 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, php.lang.trans.cfmBoxYourName);
		});
		$('#frmContactBox-email').live('focus', function() {
			self.hideValue(this, php.lang.trans.cfmBoxContact);
		});
		$('#frmContactBox-message').live('focus', function() {
			self.hideValue(this, php.lang.trans.cfmBoxMessage);
		});
		$('#frmContactBox-code').live('focus', function() {
			self.hideValue(this, php.lang.trans.cptCodeInfoShort);
		});

		// blur fields
		$('#frmContactBox-name').live('blur', function() {
			self.showValue(this, php.lang.trans.cfmBoxYourName);
		});
		$('#frmContactBox-email').live('blur', function() {
			self.showValue(this, php.lang.trans.cfmBoxContact);
		});
		$('#frmContactBox-message').live('blur', function() {
			self.showValue(this, php.lang.trans.cfmBoxMessage);
		});
		$('#frmContactBox-code').live('blur', function() {
			self.showValue(this, php.lang.trans.cptCodeInfoShort);
		});
	};


	/**
	 * 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 == php.lang.trans.cfmBoxYourName) {
			error += php.lang.trans.cfmNameError + '\n';
			$('#frmContactBox-name').focus();
		} else if (email.length == 0 ||  email == php.lang.trans.sysEmail) {
			error += php.lang.trans.cfmEmailError + '\n';
			$('#frmContactBox-email').focus();
		} else if (message.length == 0 || message == php.lang.trans.cfmBoxMessage) {
			error += php.lang.trans.cfmMessageError + '\n';
			$('#frmContactBox-message').focus();
		} else if (code.length == 0 || code == php.lang.trans.cptCodeInfoShort) {
			error += php.lang.trans.cptCodeInfoShort + '.\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);
	};

}


var contactFormBoxFe = new ContactFormBoxFe();

// call class init
$(document).ready(function(){
	contactFormBoxFe.jq_init();
});

