jQuery(document).ready(function() {
	jQuery('div.mmcf > form').ajaxForm({
		beforeSubmit: mmcfBeforeSubmit,
		dataType: 'json',
		success: mmcfProcessJson
	});

  jQuery('div.mmcf > form').each(function(i, n) {
    mmcfToggleSubmit(jQuery(n));
  });
});

// Exclusive checkbox
function mmcfExclusiveCheckbox(elem) {
  jQuery(elem.form).find('input:checkbox[@name="' + elem.name + '"]').not(elem).removeAttr('checked');
}

// Toggle submit button
function mmcfToggleSubmit(form) {
  var submit = jQuery(form).find('input:submit');
  if (! submit.length) return;
  
  var acceptances = jQuery(form).find('input:checkbox.mmcf-acceptance');
  if (! acceptances.length) return;
  
  submit.removeAttr('disabled');
  acceptances.each(function(i, n) {
    n = jQuery(n);
    if (n.hasClass('mmcf-invert') && n.is(':checked') || ! n.hasClass('mmcf-invert') && ! n.is(':checked'))
      submit.attr('disabled', 'disabled');
  });
}

function mmcfBeforeSubmit(formData, jqForm, options) {
	mmcfClearResponseOutput();
	jQuery('img.ajax-loader', jqForm[0]).css({ visibility: 'visible' });
  
  formData.push({name: '_mmcf_is_ajax_call', value: 1});
  
	return true;
}

function mmcfNotValidTip(into, message) {
  jQuery(into).append('<span class="mmcf-not-valid-tip">' + message + '</span>');
	jQuery('span.mmcf-not-valid-tip').mouseover(function() {
		jQuery(this).fadeOut('fast');
	});
	jQuery(into).find(':input').mouseover(function() {
		jQuery(into).find('.mmcf-not-valid-tip').not(':hidden').fadeOut('fast');
	});
	jQuery(into).find(':input').focus(function() {
		jQuery(into).find('.mmcf-not-valid-tip').not(':hidden').fadeOut('fast');
	});
}

function mmcfProcessJson(data) {
	var mmcfResponseOutput = jQuery(data.into).find('div.mmcf-response-output');
	mmcfClearResponseOutput();
	if (data.invalids) {
		jQuery.each(data.invalids, function(i, n) {
			mmcfNotValidTip(jQuery(data.into).find(n.into), n.message);
		});
		mmcfResponseOutput.addClass('mmcf-validation-errors');
	}
	if (data.captcha) {
		jQuery.each(data.captcha, function(i, n) {
			jQuery(data.into).find(':input[@name="' + i + '"]').clearFields();
			jQuery(data.into).find('img.mmcf-captcha-' + i).attr('src', n);
			var match = /([0-9]+)\.(png|gif|jpeg)$/.exec(n);
			jQuery(data.into).find('input:hidden[@name="_mmcf_captcha_challenge_' + i + '"]').attr('value', match[1]);
		});
	}
	if (1 == data.spam) {
		mmcfResponseOutput.addClass('mmcf-spam-blocked');
	}
	if (1 == data.mailSent) {
		jQuery(data.into).find('form').resetForm().clearForm();
		mmcfResponseOutput.addClass('mmcf-mail-sent-ok');
	} else {
		mmcfResponseOutput.addClass('mmcf-mail-sent-ng');
	}
	mmcfResponseOutput.append(data.message).fadeIn('fast');
}

function mmcfClearResponseOutput() {
	jQuery('div.mmcf-response-output').hide().empty().removeClass('mmcf-mail-sent-ok mmcf-mail-sent-ng mmcf-validation-errors mmcf-spam-blocked');
	jQuery('span.mmcf-not-valid-tip').remove();
	jQuery('img.ajax-loader').css({ visibility: 'hidden' });
}