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

if ( in_array( $field->type, $this->complex_fields ) ) {
	$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' ) . ' mdc-text-field-helper-text mdc-text-field-helper-text--persistent mdc-text-field-helper-text--validation-msg' );
		// }

		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_spans = $dom->getElementsByTagName( 'span' );
		foreach ( $get_spans as $span ) {
			$span_classes = $span->getAttribute( 'class' );
			
			//only modify fields if first, middle , last name or address field except for country selection
			// if ( strpos( $span_classes, 'name_' ) !== false || strpos( $span_classes, 'address_' ) !== false  ) {
			if ( strpos( $span_classes, 'address_country' ) === false ) {
				$inputs = $span->getElementsByTagName( 'input' );

				foreach ( $inputs as $input ) {
					$input_id = $input->getAttribute( 'id' );
					$saved_field_id = str_replace('input_'.$form_id.'_','',$input_id);
					$saved_field_id = str_replace('_','.',$saved_field_id);
					//$input_id_param =  filter_var( $input_id, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION );
					//var_dump( $field->type);
					// var_dump( $get_field_icons['fontawesome'][ $field->type ]);
						if ( isset( $get_field_icons['fontawesome'][ $field->type ][$saved_field_id ] ) ) {
							$fontawesome_icon = $get_field_icons['fontawesome'][ $field->type ][$saved_field_id ];
							$icon_element = $dom->createElement( 'i' );

							$fontawesome_icon = str_replace( '_', ' ', $fontawesome_icon );
							$fontawesome_icon .= ' stla-fontawesome-icon';
							$icon_element->setAttribute( 'class', $fontawesome_icon );
							$span->setAttribute( 'style', 'position:relative' );
							$span->setAttribute( 'class', $span_classes . ' stla-fontawesome' );
							$span->insertBefore( $icon_element, $input );
					}
				}
			}
		}
	}


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