jQuery(document).ready(function() {
    /* added by sjokki */
    try {
        jQuery('div.wpcf7 br').each(function () {
            this.parentNode.removeChild(this);
        });
        jQuery('div.wpcf7-response-output').each(function () {
            var parentNode = this.parentNode;
            jQuery(parentNode.parentNode).css({position: 'relative'});

            parentNode.removeChild(this);
            parentNode.parentNode.appendChild(this);
            jQuery(this).hide().css({position: 'absolute', top: 0, left: 0 })
        })
        jQuery('input').each(function () {
            if (jQuery(this).attr('type') == 'submit') {
                this.parentNode.replaceChild(jQuery('<button type="submit"></button>')
                    .attr('id', this.id)
                    .attr('class', jQuery(this).attr('class'))
                    .html('<span>' + jQuery(this).val() + '</span>').get(0), this
                );
            }
            else {
                jQuery(this).addClass('input-' + jQuery(this).attr('type'));
            }
        });
        jQuery('img.ajax-loader').replaceWith('<div class="ajax-loader" style="visibility: hidden"></div>');

    }  catch (e) {
        alert(e);
    }
    try {
        jQuery('div.wpcf7 label.swopper').each(function () {
            if (jQuery(this).attr('for') != '') {
                var label = this;
                jQuery('#' + jQuery(this).attr('for')).each(function () {
                    if ((this.nodeName == 'INPUT' && this.type == 'text') || this.nodeName == 'TEXTAREA') {
                        this.setDefaultValue = function (value) {
                            this.labelValue = value;
                            this.showDefaultValue();
                            jQuery(this).focus(function () {
                                this.hideDefaultValue();
                            })
                            jQuery(this).blur(function () {
                                this.showDefaultValue();
                            })
                        }
                        this.showDefaultValue = function () {
                            if (jQuery(this).val() == '') {
                                jQuery(this).val(this.labelValue).addClass('default-value');
                            }
                        }
                        this.hideDefaultValue = function () {
                            if (jQuery(this).val() == this.labelValue) {
                                jQuery(this).val('').removeClass('default-value');
                            }
                        }
                        this.setDefaultValue(label.innerHTML);
                        jQuery(label).css({display: 'none'});
                    }
                })
           }
        })
    	jQuery('div.wpcf7 > form').submit(function () {
            jQuery(this).find('input, textarea').each (function () {
                this.hideDefaultValue && this.hideDefaultValue();
           })
       })
    } catch (e) {
    }

    /* end added */
	try {
		jQuery('div.wpcf7 > form').ajaxForm({
			beforeSubmit: wpcf7BeforeSubmit,
			dataType: 'json',
			success: wpcf7ProcessJson
		});
	} catch (e) {
	}

	try {
		jQuery('div.wpcf7 > form').each(function(i, n) {
			wpcf7ToggleSubmit(jQuery(n));
		});
	} catch (e) {
	}

	try {
		if (_wpcf7.cached) {
			jQuery('div.wpcf7 > form').each(function(i, n) {
				wpcf7OnloadRefill(n);
			});
		}
	} catch (e) {
	}
});

// Exclusive checkbox
function wpcf7ExclusiveCheckbox(elem) {
	jQuery(elem.form).find('input:checkbox[name="' + elem.name + '"]').not(elem).removeAttr('checked');
}

// Toggle submit button
function wpcf7ToggleSubmit(form) {
	var submit = jQuery(form).find('input:submit');
	if (! submit.length) return;

	var acceptances = jQuery(form).find('input:checkbox.wpcf7-acceptance');
	if (! acceptances.length) return;

	submit.removeAttr('disabled');
	acceptances.each(function(i, n) {
		n = jQuery(n);
		if (n.hasClass('wpcf7-invert') && n.is(':checked') || ! n.hasClass('wpcf7-invert') && ! n.is(':checked'))
		submit.attr('disabled', 'disabled');
	});
}

function wpcf7BeforeSubmit(formData, jqForm, options) {
    /* added by sjokki */
	wpcf7ClearResponseOutput();
	jQuery('div.ajax-loader', jqForm[0]).css({ visibility: 'visible' });

	formData.push({name: '_wpcf7_is_ajax_call', value: 1});
	jQuery(jqForm[0]).append('<input type="hidden" name="_wpcf7_is_ajax_call" value="1" />');

	return true;
}

function wpcf7NotValidTip(into, message) {
    jQuery(into).addClass('validation-error');
    /* removed by sjokki */
	jQuery(into).append('<span class="wpcf7-not-valid-tip"><span>' + message + '</span></span>');
	jQuery('span.wpcf7-not-valid-tip').mouseover(function() {
		jQuery(this).fadeOut('fast');
	});
	jQuery(into).find(':input').mouseover(function() {
		jQuery(into).find('.wpcf7-not-valid-tip').not(':hidden').fadeOut('fast');
	});
	jQuery(into).find(':input').focus(function() {
    		jQuery(into).find('.wpcf7-not-valid-tip').not(':hidden').fadeOut('fast');
	});
    /* end removed */
}

function wpcf7OnloadRefill(form) {
	var url = jQuery(form).attr('action');
	if (0 < url.indexOf('#'))
		url = url.substr(0, url.indexOf('#'));

	var id = jQuery(form).find('input[name="_wpcf7"]').val();
	var unitTag = jQuery(form).find('input[name="_wpcf7_unit_tag"]').val();

	jQuery.getJSON(url,
		{ _wpcf7_is_ajax_call: 1, _wpcf7: id },
		function(data) {
			if (data && data.captcha) {
				wpcf7RefillCaptcha('#' + unitTag, data.captcha);
			}
			if (data && data.quiz) {
				wpcf7RefillQuiz('#' + unitTag, data.quiz);
			}
		}
	);
}

function wpcf7ProcessJson(data) {
    
    /* added by sjokki */
    var showResponseOutput = false;
    /* end added */

	var wpcf7ResponseOutput = jQuery(data.into).find('div.wpcf7-response-output');
	wpcf7ClearResponseOutput();
    jQuery(data.into).find('.validation-error').removeClass('validation-error');

	if (data.invalids) {
		jQuery.each(data.invalids, function(i, n) {
			wpcf7NotValidTip(jQuery(data.into).find(n.into), n.message);
		});
        /* added by sjokki */
        jQuery(data.into).find('input, textarea').each (function () {
            this.showDefaultValue && this.showDefaultValue();
        });

        /* end added */
		wpcf7ResponseOutput.addClass('wpcf7-validation-errors');
	}

	if (data.captcha) {
		wpcf7RefillCaptcha(data.into, data.captcha);
	}

	if (data.quiz) {
		wpcf7RefillQuiz(data.into, data.quiz);
	}

	if (1 == data.spam) {
		wpcf7ResponseOutput.addClass('wpcf7-spam-blocked');
	}

	if (1 == data.mailSent) {
		jQuery(data.into).find('form').resetForm().clearForm();

        /* added by sjokki */
        jQuery(data.into).find('input, textarea').each (function () {
            this.showDefaultValue && this.showDefaultValue();
        });
        /* end added */

		wpcf7ResponseOutput.addClass('wpcf7-mail-sent-ok');
        /* added by sjokki */
        showResponseOutput = true;
        /* end added */

		if (data.onSentOk)
			jQuery.each(data.onSentOk, function(i, n) { eval(n) });
	} else {
		wpcf7ResponseOutput.addClass('wpcf7-mail-sent-ng');
	}

	if (data.onSubmit)
		jQuery.each(data.onSubmit, function(i, n) { eval(n) });
    
    /* removed by sjokki */
    //wpcf7ResponseOutput.append(data.message).show();
    /* end removed */

    /* added by sjokki */
    if (showResponseOutput) {
        wpcf7ResponseOutput.append(data.message).fadeIn();
        jQuery(data.into).find('form').fadeOut('fast'); // fadeTo('fast', 0);
        setTimeout( function(){
                jQuery(data.into).find('form').fadeIn(); // fadeTo('normal', 1);
                wpcf7ResponseOutput.fadeOut();
            },
            5000
        );
    }
    else {
        jQuery(data.into).find('form').css({ visibility: 'visible' });
    }
    /* end added */
}

function wpcf7RefillCaptcha(form, captcha) {
	jQuery.each(captcha, function(i, n) {
		jQuery(form).find(':input[name="' + i + '"]').clearFields();
		jQuery(form).find('img.wpcf7-captcha-' + i).attr('src', n);
		var match = /([0-9]+)\.(png|gif|jpeg)$/.exec(n);
		jQuery(form).find('input:hidden[name="_wpcf7_captcha_challenge_' + i + '"]').attr('value', match[1]);
	});
}

function wpcf7RefillQuiz(form, quiz) {
	jQuery.each(quiz, function(i, n) {
		jQuery(form).find(':input[name="' + i + '"]').clearFields();
		jQuery(form).find(':input[name="' + i + '"]').siblings('span.wpcf7-quiz-label').text(n[0]);
		jQuery(form).find('input:hidden[name="_wpcf7_quiz_answer_' + i + '"]').attr('value', n[1]);
	});
}

function wpcf7ClearResponseOutput() {
	jQuery('div.wpcf7-response-output').hide().empty().removeClass('wpcf7-mail-sent-ok wpcf7-mail-sent-ng wpcf7-validation-errors wpcf7-spam-blocked');
	jQuery('.wpcf7-form span.wpcf7-not-valid-tip').remove();
    
	jQuery('div.ajax-loader').css({ visibility: 'hidden' });
}
function loadInBackground (url)
{
    jQuery('<iframe src="' + URL + '/' + url + '" style="visibility: hidden; width: 0px; height: 1px; position: absolute; top: 0; left: 0"></iframe>')
        .appendTo(document.body);
}
