<?php

/**
 * Gravity forms
 * @category Functions
 * @author Krik
 */

// Remove scripts
add_filter('gform_init_scripts_footer', 'krik_gravity_footer_scripts');

// Wait for jQuery to be loaded
add_filter('gform_cdata_open', 'krik_gravity_script_open');
add_filter('gform_cdata_close', 'krik_gravity_script_close');

// Change textarea output HTML
add_filter('gform_field_input', 'krik_gravity_textarea_input', 10, 5);

function krik_gravity_footer_scripts()
{
	return true;
}

function krik_gravity_script_open($content)
{
	$content = 'document.addEventListener("DOMContentLoaded", function() { ';
	return $content;
}

function krik_gravity_script_close($content)
{
	$content = ' }, false );';
	return $content;
}

function krik_gravity_textarea_input($input, $field, $value, $lead_id, $form_id)
{
	if ($field->type == 'textarea') {
		$input = '<div class="ginput_container ginput_container_textarea"><textarea name="input_' . $field->id . '" id="input_' . $form_id . '_' . $field->id . '" tabindex="' . $field->id . '" aria-required="true" aria-invalid="false" rows="1"></textarea></div>';
	}

	return $input;
}
