<?php

/***************************************** for radio ****************************************************************************/

if ( in_array( $field->type, $radio_fields ) ) {

	$radio_classes_types = array( 'ginput_container_radio' );
	//get the first div
	$contains_error = false;
	$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 ( $contains_error ) {
	// 	foreach ( $all_divs as $current_div ) {
	// 		$current_div_classes = $current_div->getAttribute( 'class' );
	// 		if ( strpos( $current_div_classes, 'ginput_container_radio' ) !== false ) {
	// 			$contains_error = true;
	// 			$current_div->setAttribute( 'class', $current_div->getAttribute( 'class' ) . '  mdc-text-field--invalid' );
	// 		}
	// 	}
	// }
	foreach ( $all_divs as $div ) {
		$div_classes = $div->getAttribute( 'class' );
		$div_classes_array = explode( ' ', $div_classes );
		$is_valid_radio_field = false;
		foreach ( $div_classes_array as $div_class_value ) {
			if ( in_array( $div_class_value, $radio_classes_types ) ) {
				$is_valid_radio_field = true;
			}
		}

		if ( $is_valid_radio_field ) {
			// if custom checkboxes are enabled or not set( as they are set as true by default)
		//	if ( !isset( $bootstrap_options['custom-radio'] ) || $bootstrap_options['custom-radio'] === true ) {
				$get_container = $dom->getElementsByTagName( 'li' );
				foreach ( $get_container as $container_element ) {
					$container_classes = $container_element->getAttribute( 'class' );
					if ( strpos( $container_classes, 'gchoice_' ) !== false ) {
						$container_element->setAttribute( 'class', $container_element->getAttribute( 'class' ) . ' custom-control custom-radio' );
					}
				}

			$lis = $div->getElementsByTagName( 'li' );
			foreach ( $lis as $li ) {
				$input_elements = $li->getElementsByTagName( 'input' );

				//only use the first element .. if other choice is enable then it has 2 inputs
				if( isset( $input_elements[0] ) ) {
					$input_value = $input_elements[0]->getAttribute( 'value' );
					$input_elements[0]->setAttribute( 'class',
						$input_elements[0]->getAttribute( 'class' ) . ' custom-control-input' );
					if ( $contains_error ) {
						$input_elements[0]->setAttribute( 'class',
						$input_elements[0]->getAttribute( 'class' ) . ' is-invalid' );
					}

					// other radio button 
					if ( strpos( $input_value, 'gf_other_choice' ) !== false ) { 
						$input_elements[0]->setAttribute( 'onfocus', "jQuery(this).parent('div').parent('li').find('> input').focus();" );
						
						// Other input text field
						$defualt_onfocus = $input_elements[1]->getAttribute('onfocus') ;
						$new_onfocus = str_replace( "jQuery(this).prev(\"input\")[0].click();", "jQuery(this).parent('li').find('input[value=\"gf_other_choice\" ]').click();", $defualt_onfocus);

						$input_elements[1]->setAttribute( 'onfocus', $new_onfocus );

						// other option doesn't have a label for option.. create an empty label
						$new_label = $dom->createElement( 'label' );
						$li->appendChild( $new_label );

					}

				}

				$get_labels = $li->getElementsByTagName( 'label' );
				foreach ( $get_labels as $label ) {
					$label->setAttribute( 'class',
					$label->getAttribute( 'class' ) . ' custom-control-label' );
				}
			}
			$content = utf8_decode( $dom->saveHTML( $dom->documentElement ) );
		}

	}

}
