(function($) {
	var onDocumentReady = function (event) {
		var frm = $('form#frm-contact');
		if (frm.length > 0) {
			var emailRE = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/,
				maxMessageLength = 2048,
				fields = frm.find(':input:visible:enabled'),
				showMessage = function (elm, message) {
					var container = elm.parent('div.field').nextAll('div.message');
					if (container.length > 0) {
						container.text(message).show();
					}
					else {
						elm.parent('div.field').after('<div class="message">' + message + '</div>');
					}
					elm.addClass('error');
					var ev;
					if (elm.is('select')) {
						ev = 'change';
					}
					else {
						ev = 'keyup';							
					}
					elm.one(ev + '.contactForm', function (event) {
						hideMessage(elm);
					});
				},
				hideMessage = function (elm) {
					elm.parent('div.field').nextAll('div.message').hide();
					elm.removeClass('error');
				},
				onFieldFocus = function (event) {
					$(this).addClass('focusjs');
				},
				onFieldBlur = function (event) {
					$(this).removeClass('focusjs');
				},
				onFormSubmit = function (event) {
					var errors = 0,
						subj = fields.filter('#frm-contact-subj'),
						subjValue = $.trim(subj.val()),
						//org = fields.filter('#frm-contact-org'),
						//orgValue = $.trim(org.val()),
						fname =	fields.filter('#frm-contact-fname'),
						fnameValue = $.trim(fname.val()),
						lname =	fields.filter('#frm-contact-lname'),
						lnameValue = $.trim(lname.val()),
						addr = fields.filter('#frm-contact-addr'),
						addrValue = $.trim(addr.val()),
						zipcity = fields.filter('#frm-contact-zipcity'),
						zipcityValue = $.trim(zipcity.val()),
						phone = fields.filter('#frm-contact-phone'),
						phoneValue = $.trim(phone.val()),
						email = fields.filter('#frm-contact-email'),
						emailValue = $.trim(email.val()),
						msg = fields.filter('#frm-contact-msg'),
						msgValue = $.trim(msg.val());
					if (subjValue.length == 0 || subjValue == subj.metadata().defaultValue) {
						showMessage(subj, $.l18n('CONTACT_FRM_ERR_SUBJ_EMPTY'));
						errors++;
					}
					else {
						hideMessage(subj);
					}
					/*
					if (orgValue.length == 0 || orgValue == org.data('defaultValue')) {
						showMessage(org, $.l18n('CONTACT_FRM_ERR_ORG_EMPTY'));
						errors++;
					}
					else {
						hideMessage(org);
					}
					*/
					if (fnameValue.length == 0 || fnameValue == fname.data('defaultValue')) {
						showMessage(fname, $.l18n('CONTACT_FRM_ERR_FNAME_EMPTY'));
						errors++;
					}
					else {
						hideMessage(fname);
					}
					if (lnameValue.length == 0 || lnameValue == lname.data('defaultValue')) {
						showMessage(lname, $.l18n('CONTACT_FRM_ERR_LNAME_EMPTY'));
						errors++;
					}
					else {
						hideMessage(lname);
					}
					/*
					if (addrValue.length == 0 || addrValue == addr.data('defaultValue')) {
						showMessage(addr, $.l18n('CONTACT_FRM_ERR_ADDR_EMPTY'));
						errors++;
					}
					else {
						hideMessage(addr);
					}
					if (zipcityValue.length == 0 || zipcityValue == zipcity.data('defaultValue')) {
						showMessage(zipcity, $.l18n('CONTACT_FRM_ERR_ZIPCITY_EMPTY'));
						errors++;
					}
					else {
						hideMessage(zipcity);
					}
					*/
					if (phoneValue.length == 0 || phoneValue == phone.data('defaultValue')) {
						showMessage(phone, $.l18n('CONTACT_FRM_ERR_PHONE_EMPTY'));
						errors++;
					}
					else {
						hideMessage(phone);
					}
					if (emailValue.length == 0 || emailValue == email.data('defaultValue')) {
						showMessage(email, $.l18n('CONTACT_FRM_ERR_EMAIL_EMPTY'));
						errors++;
					}
					else if (emailValue.length > 0 && !emailRE.test(emailValue)) {
						showMessage(email, $.l18n('CONTACT_FRM_ERR_EMAIL_INVALID'));
						errors++;
					}
					else {
						hideMessage(email);
					}
					if (msgValue.length == 0 || msgValue == msg.data('defaultValue')) {
						showMessage(msg, $.l18n('CONTACT_FRM_ERR_MSG_EMPTY'));
						errors++;
					}
					else if (msgValue.length > maxMessageLength) {
						showMessage(msg, $.l18n('CONTACT_FRM_ERR_MSG_LONG', {max: maxMessageLength}));
						errors++;
					}
					else {
						hideMessage(msg);
					}
					return errors == 0;
				};
			frm.bind('submit.contactForm', onFormSubmit);
			fields.bind('focus', onFieldFocus).bind('blur', onFieldBlur).filter('.defaultValue').defaultValue();
		}
	};
	$(document).bind('ready', onDocumentReady);
})(jQuery);
