/****************************/
/* BEGIN - Shared functions */
function checkLength(otext, min)
{
	if ( otext.val().length < min ) {
		otext.addClass('ui-state-error');
		return false;
	} else {
		return true;
	}
}

function updateTips(otips, text, clear)
{
	otips.show();
	if (clear)
		otips.html(text).effect("highlight", {}, 1500);
	else
		otips.html(otips.html() + "<br />" + text);
}

function checkRegExp(otext, regexp)
{
	if ( !(regexp.test(otext.val())) ) {
		otext.addClass('ui-state-error');
		return false;
	} else {
		return true;
	}
}

function dialogSetButtonState(button, state)
{
	button.show();
	if (state) {
		button.addClass('ui-state-default');
		button.removeClass('ui-state-disabled');
		button.removeClass('ui-state-hover');
		button.removeAttr('disabled');
	} else {
		button.removeClass('ui-state-default');
		button.addClass('ui-state-disabled');
		button.addClass('ui-state-hover');
		button.attr('disabled', 'disabled');
	}
}
/* END - Shared functions   */
/****************************/

var EmailPost;

jQuery(document).ready(function() {
	EmailPost.init();
});

/**********************/
/* BEGIN - Email post */
(function($) {
	EmailPost = {
		fields: null,
		tips: null,
		buttonSubmit: null,
		buttonCancel: null,
		labels: {},
		
		init: function() {
			this.fields = $("#ep-sender-name, #ep-destination-email, #ep-message, #ep-captcha-confirm");
			this.tips = $("#ep-validate-tips");
			
			var buttons = {};
			buttons[this.labels.buttonSubmit] = this.submit;
			buttons[this.labels.buttonCancel] = this.cancel;
			
			$("#ep-dialog").dialog({
				bgiframe: true,
				autoOpen: false,
				resizable: false,
				height: 'auto',
				width: 400,
				modal: true,
				buttons: buttons
			});
			
			this.buttonSubmit = $(":button:contains('"+this.labels.buttonSubmit+"')", $("#ep-dialog").parent());
			this.buttonCancel = $(":button:contains('"+this.labels.buttonCancel+"')", $("#ep-dialog").parent());
			
			$('#ep-get-popup-top').click(function() {
				$('#ep-dialog').dialog('open');
				EmailPost.refreshCaptchaClick();
			});
			
			$('#ep-get-popup-bottom').click(function() {
				$('#ep-dialog').dialog('open');
				EmailPost.refreshCaptchaClick();
			});
			
			$('#ep-captcha-refresh-image').click(function() {
				EmailPost.refreshCaptchaClick();
			});
		},
		
		processingState: function(state) {
			EmailPost.setButtonsState(!state);
			
			if (state) {
				$("#ep-form-zone").hide();
				$("#ep-results-zone").html("<img src=\"" + EmailPost.labels.loadingImgUrl + "\" style=\"margin-top: 2px; margin-left: 0px;\" /> " + EmailPost.labels.loadingValidationCode).show();
			} else {
				$("#ep-results-zone").html("").hide();
				$("#ep-form-zone").show();
			}
		},
		
		refreshCaptchaClick: function() {
			EmailPost.setButtonsState(false);
			$("#ep-captcha-confirm").attr("value", "");
			
			$("#ep-captcha-image").html("<img src=\"" + EmailPost.labels.loadingImgUrl + "\" style=\"margin-top: 2px; margin-left: 0px;\" /> " + EmailPost.labels.loadingValidationCode);
			
			$.post( equiAjaxUrl, {
				'class': 'EquiWp_Post_Tools_AView',
				'function': 'refreshCaptchaImage'
			}, function(response) {
				$("#ep-captcha-image").html(response);
				$("#ep-captcha-image").fadeIn(200);
				EmailPost.setButtonsState(true);
			});
		},
		
		submit: function() {
			var formValid = true;
			
			EmailPost.fields.removeClass('ui-state-error');
			
			if (!checkLength($('#ep-sender-name'), 1)) {
				updateTips(EmailPost.tips, EmailPost.labels.errorMissingName, formValid);
				formValid = false;
			}
			
			if (!checkLength($('#ep-destination-email'), 1)) {
				updateTips(EmailPost.tips, EmailPost.labels.errorMissingEmail, formValid);
				formValid = false;
			} else {
				if (!checkRegExp($('#ep-destination-email'), /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
					updateTips(EmailPost.tips, EmailPost.labels.errorInvalidEmail, formValid);
					formValid = false;
				}
			}
			
			if (!checkLength($('#ep-message'), 1)) {
				updateTips(EmailPost.tips, EmailPost.labels.errorMissingMessage, formValid);
				formValid = false;
			}
			
			if (!checkLength($('#ep-captcha-confirm'), 6)) {
				updateTips(EmailPost.tips, EmailPost.labels.errorLengthValidationCode, formValid);
				formValid = false;
			}
			
			if (formValid) {
				EmailPost.processingState(true);
				$('#ep-form').submit();
			}
		},
		
		cancel: function() {
			EmailPost.fields.val("").removeClass('ui-state-error');
			EmailPost.tips.html("").hide();
			EmailPost.setModeClose(false);
			$(this).dialog('close');
		},
		
		setButtonsState: function(state) {
			dialogSetButtonState(this.buttonSubmit, state);
			dialogSetButtonState(this.buttonCancel, state);
		},
		
		setModeClose: function(activateResult) {
			if (activateResult) {
				$("#ep-form-zone").hide();
				$("#ep-results-zone").show();
			} else {
				$("#ep-form-zone").show();
				$("#ep-results-zone").hide();
			}
			
			EmailPost.buttonSubmit.hide();
			dialogSetButtonState(EmailPost.buttonCancel, true);
		}
	}
})(jQuery);
/* END - Email post   */
/**********************/
