<?php
/***************************************** for complex fields like address and name *************************************/

if ( in_array( $field->type, $email_confirmation ) ) {
	//var_dump($content);

	$is_complex_field = false;
	$contains_error = false;

	//check if error is present
	$all_divs = $dom->getElementsByTagName( 'div' );
	foreach ( $all_divs as $current_div ) {
		$current_div_classes = $current_div->getAttribute( 'class' );
		if ( strpos( $current_div_classes, 'validation_message' ) !== false ) {
			$contains_error = true;
			$current_div->setAttribute( 'class', $current_div->getAttribute( 'class' ) . ' invalid-feedback' );
		}

		if ( strpos( $current_div_classes, 'ginput_complex' ) !== false ) {
			$is_complex_field = true;
		}
	}
	// if its complex field only then process it.. ( added because of confirm email )
	if ( $is_complex_field  ) {
		$get_span = $dom->getElementsByTagName( 'span' );

		foreach ( $get_span as $input_element ) {

			$span_classes = $input_element->getAttribute( 'class' );
            $input_element->setAttribute( 'class', $input_element->getAttribute( 'class' ) . ' form-group' );
			//check if the current span is not 'required' * span
			if ( strpos( $span_classes, 'gfield_required' ) === false ) {
				

				$inputs = $input_element->getElementsByTagName( 'input' );
				foreach ( $inputs as $input ) {
					$input->setAttribute( 'class', $input->getAttribute( 'class' ) . ' form-control' );
					if( $contains_error ){
						$input->setAttribute( 'class', $input->getAttribute( 'class' ) . ' is-invalid' );
					}
				}

			}
		}
	}
	$content = utf8_decode( $dom->saveHTML( $dom->documentElement ) );
}
