function mf_get_required_elements_status($dbh, $form_id, $page_number, $user_input)
{
    //get all fields within current page which has "required" property and has conditions
    $query = "SELECT \r\n\t\t\t\t\t\tA.element_id \r\n\t\t\t\t\tFROM \r\n\t\t\t\t\t\t" . MF_TABLE_PREFIX . "form_elements A LEFT JOIN " . MF_TABLE_PREFIX . "field_logic_elements B \r\n\t\t\t\t\t  ON \r\n\t\t\t\t\t  \tA.form_id=B.form_id and A.element_id=B.element_id\r\n\t\t\t\t   WHERE \r\n\t\t\t\t   \t\tA.form_id = ? and \r\n\t\t\t\t   \t\tA.element_status = 1 and \r\n\t\t\t\t   \t\tA.element_page_number = ? and \r\n\t\t\t\t   \t\tA.element_is_required = 1 and \r\n\t\t\t\t   \t\tB.element_id is not null\r\n\t\t\t\tORDER BY \r\n\t\t\t\t\t\tA.element_position asc";
    $params = array($form_id, $page_number);
    $sth = mf_do_query($query, $params, $dbh);
    $required_fields_array = array();
    while ($row = mf_do_fetch_result($sth)) {
        $required_fields_array[] = $row['element_id'];
    }
    $required_elements_status = array();
    //loop through each field and check for the conditions
    if (!empty($required_fields_array)) {
        foreach ($required_fields_array as $element_id) {
            $current_element_conditions_status = array();
            $query = "select rule_show_hide,rule_all_any from " . MF_TABLE_PREFIX . "field_logic_elements where form_id = ? and element_id = ?";
            $params = array($form_id, $element_id);
            $sth = mf_do_query($query, $params, $dbh);
            $row = mf_do_fetch_result($sth);
            $rule_show_hide = $row['rule_show_hide'];
            $rule_all_any = $row['rule_all_any'];
            //get all conditions for current field
            $query = "SELECT \r\n\t\t\t\t\t\tA.target_element_id,\r\n\t\t\t\t\t\tA.element_name,\r\n\t\t\t\t\t\tA.rule_condition,\r\n\t\t\t\t\t\tA.rule_keyword,\r\n\t\t\t\t\t\ttrim(leading 'element_' from substring_index(A.element_name,'_',2)) as condition_element_id,\r\n\t\t\t\t\t\t(select \r\n\t\t\t\t\t\t\t   B.element_page_number \r\n\t\t\t\t\t\t   from \r\n\t\t\t\t\t\t   \t   " . MF_TABLE_PREFIX . "form_elements B \r\n\t\t\t\t\t\t  where \r\n\t\t\t\t\t\t  \t\tform_id=A.form_id and \r\n\t\t\t\t\t\t  \t\telement_id=condition_element_id\r\n\t\t\t\t\t\t) condition_element_page_number,\r\n\t\t\t\t\t\t(select \r\n\t\t\t\t\t\t\t   C.element_type \r\n\t\t\t\t\t\t   from \r\n\t\t\t\t\t\t   \t   " . MF_TABLE_PREFIX . "form_elements C \r\n\t\t\t\t\t\t  where \r\n\t\t\t\t\t\t  \t\tform_id=A.form_id and \r\n\t\t\t\t\t\t  \t\telement_id=condition_element_id\r\n\t\t\t\t\t\t) condition_element_type\r\n\t\t\t\t\tFROM \r\n\t\t\t\t\t\t" . MF_TABLE_PREFIX . "field_logic_conditions A \r\n\t\t\t\t   WHERE\r\n\t\t\t\t\t\tA.form_id = ? and A.target_element_id = ?";
            $params = array($form_id, $element_id);
            $sth = mf_do_query($query, $params, $dbh);
            $i = 0;
            $logic_conditions_array = array();
            while ($row = mf_do_fetch_result($sth)) {
                $logic_conditions_array[$i]['element_name'] = $row['element_name'];
                $logic_conditions_array[$i]['element_type'] = $row['condition_element_type'];
                $logic_conditions_array[$i]['rule_condition'] = $row['rule_condition'];
                $logic_conditions_array[$i]['rule_keyword'] = $row['rule_keyword'];
                $logic_conditions_array[$i]['element_page_number'] = (int) $row['condition_element_page_number'];
                $i++;
            }
            //loop through each condition which is not coming from the current page
            foreach ($logic_conditions_array as $value) {
                if ($value['element_page_number'] == $page_number) {
                    continue;
                }
                $condition_params = array();
                $condition_params['form_id'] = $form_id;
                $condition_params['element_name'] = $value['element_name'];
                $condition_params['rule_condition'] = $value['rule_condition'];
                $condition_params['rule_keyword'] = $value['rule_keyword'];
                $current_element_conditions_status[] = mf_get_condition_status_from_table($dbh, $condition_params);
            }
            //loop through each condition which is coming from the current page
            foreach ($logic_conditions_array as $value) {
                if ($value['element_page_number'] != $page_number) {
                    continue;
                }
                $condition_params = array();
                $condition_params['form_id'] = $form_id;
                $condition_params['element_name'] = $value['element_name'];
                $condition_params['rule_condition'] = $value['rule_condition'];
                $condition_params['rule_keyword'] = $value['rule_keyword'];
                $current_element_conditions_status[] = mf_get_condition_status_from_input($dbh, $condition_params, $user_input);
            }
            //decide the status of the current element_id based on all conditions
            //required field should only being applied to an element which is being shown into the form
            //any field which is hidden due to conditions, shouldn't have any required attribute
            if ($rule_all_any == 'all') {
                if (in_array(false, $current_element_conditions_status)) {
                    $all_conditions_status = false;
                } else {
                    $all_conditions_status = true;
                }
            } else {
                if ($rule_all_any == 'any') {
                    if (in_array(true, $current_element_conditions_status)) {
                        $all_conditions_status = true;
                    } else {
                        $all_conditions_status = false;
                    }
                }
            }
            if ($rule_show_hide == 'show') {
                if ($all_conditions_status === true) {
                    $element_status = true;
                } else {
                    $element_status = false;
                }
            } else {
                if ($rule_show_hide == 'hide') {
                    if ($all_conditions_status === true) {
                        $element_status = false;
                    } else {
                        $element_status = true;
                    }
                }
            }
            if ($element_status === true) {
                $required_elements_status[$element_id] = 1;
            } else {
                $required_elements_status[$element_id] = 0;
            }
        }
        //end foreach required fields
    }
    return $required_elements_status;
}
function mf_get_logic_javascript($dbh, $form_id, $page_number)
{
    //get the target elements for the current page
    $query = "SELECT \r\n\t\t\t\t\tA.element_id,\r\n\t\t\t\t\tA.rule_show_hide,\r\n\t\t\t\t\tA.rule_all_any,\r\n\t\t\t\t\tB.element_title,\r\n\t\t\t\t\tB.element_page_number \r\n\t\t\t\tFROM \r\n\t\t\t\t\t" . MF_TABLE_PREFIX . "field_logic_elements A LEFT JOIN " . MF_TABLE_PREFIX . "form_elements B\r\n\t\t\t\t  ON \r\n\t\t\t\t  \tA.form_id = B.form_id and A.element_id=B.element_id and B.element_status = 1\r\n\t\t\t   WHERE\r\n\t\t\t\t\tA.form_id = ? and B.element_page_number = ?\r\n\t\t\tORDER BY \r\n\t\t\t\t\tB.element_position asc";
    $params = array($form_id, $page_number);
    $sth = mf_do_query($query, $params, $dbh);
    $logic_elements_array = array();
    while ($row = mf_do_fetch_result($sth)) {
        $element_id = (int) $row['element_id'];
        $logic_elements_array[$element_id]['element_title'] = $row['element_title'];
        $logic_elements_array[$element_id]['rule_show_hide'] = $row['rule_show_hide'];
        $logic_elements_array[$element_id]['rule_all_any'] = $row['rule_all_any'];
    }
    //get the conditions array
    $query = "SELECT \r\n\t\t\t\t\t\tA.target_element_id,\r\n\t\t\t\t\t\tA.element_name,\r\n\t\t\t\t\t\tA.rule_condition,\r\n\t\t\t\t\t\tA.rule_keyword,\r\n\t\t\t\t\t\ttrim(leading 'element_' from substring_index(A.element_name,'_',2)) as condition_element_id,\r\n\t\t\t\t\t\t(select \r\n\t\t\t\t\t\t\t   B.element_page_number \r\n\t\t\t\t\t\t   from \r\n\t\t\t\t\t\t   \t   " . MF_TABLE_PREFIX . "form_elements B \r\n\t\t\t\t\t\t  where \r\n\t\t\t\t\t\t  \t\tform_id=A.form_id and \r\n\t\t\t\t\t\t  \t\telement_id=condition_element_id\r\n\t\t\t\t\t\t) condition_element_page_number,\r\n\t\t\t\t\t\t(select \r\n\t\t\t\t\t\t\t   C.element_type \r\n\t\t\t\t\t\t   from \r\n\t\t\t\t\t\t   \t   " . MF_TABLE_PREFIX . "form_elements C \r\n\t\t\t\t\t\t  where \r\n\t\t\t\t\t\t  \t\tform_id=A.form_id and \r\n\t\t\t\t\t\t  \t\telement_id=condition_element_id\r\n\t\t\t\t\t\t) condition_element_type\r\n\t\t\t\t\tFROM \r\n\t\t\t\t\t\t" . MF_TABLE_PREFIX . "field_logic_conditions A \r\n\t\t\t\t   WHERE\r\n\t\t\t\t\t\tA.form_id = ?\r\n\t\t\t\t   ORDER by A.alc_id";
    $params = array($form_id);
    $sth = mf_do_query($query, $params, $dbh);
    $logic_conditions_array = array();
    $prev_element_id = 0;
    $i = 0;
    while ($row = mf_do_fetch_result($sth)) {
        $target_element_id = (int) $row['target_element_id'];
        if ($target_element_id != $prev_element_id) {
            $i = 0;
        }
        $logic_conditions_array[$target_element_id][$i]['element_name'] = $row['element_name'];
        $logic_conditions_array[$target_element_id][$i]['element_type'] = $row['condition_element_type'];
        $logic_conditions_array[$target_element_id][$i]['element_page_number'] = (int) $row['condition_element_page_number'];
        $logic_conditions_array[$target_element_id][$i]['rule_condition'] = $row['rule_condition'];
        $logic_conditions_array[$target_element_id][$i]['rule_keyword'] = $row['rule_keyword'];
        $prev_element_id = $target_element_id;
        $i++;
    }
    //build mf_handler_xx() function for each element
    $mf_handler_code = '';
    $mf_bind_code = '';
    $mf_initialize_code = '';
    $matrix_bind_array = array();
    foreach ($logic_elements_array as $element_id => $value) {
        $rule_show_hide = $value['rule_show_hide'];
        $rule_all_any = $value['rule_all_any'];
        $current_handler_conditions_array = array();
        $mf_handler_code .= "\n" . "function mf_handler_{$element_id}(){" . "\n";
        //start mf_handler_xx
        /************************************************************************************/
        $target_element_conditions = $logic_conditions_array[$element_id];
        //initialize the condition status for any other elements which is not within this page
        //store the status into a variable
        foreach ($target_element_conditions as $value) {
            if ($value['element_page_number'] == $page_number) {
                continue;
            }
            $current_handler_conditions_array[] = 'condition_' . $value['element_name'];
            $condition_params = array();
            $condition_params['form_id'] = $form_id;
            $condition_params['element_name'] = $value['element_name'];
            $condition_params['rule_condition'] = $value['rule_condition'];
            $condition_params['rule_keyword'] = $value['rule_keyword'];
            $condition_status = mf_get_condition_status_from_table($dbh, $condition_params);
            if ($condition_status === true) {
                $condition_status_value = 'true';
            } else {
                $condition_status_value = 'false';
            }
            $mf_handler_code .= "\t" . "var condition_{$value['element_name']} = {$condition_status_value};" . "\n";
        }
        $mf_handler_code .= "\n";
        //build the conditions for the current element
        $radio_suffix_id = 0;
        foreach ($target_element_conditions as $value) {
            $radio_suffix_id++;
            //skip field which doesn't belong current page
            if ($value['element_page_number'] != $page_number) {
                continue;
            }
            //for checkbox with other, we need to replace the id
            if ($value['element_type'] == 'checkbox') {
                $checkbox_has_other = false;
                if (substr($value['element_name'], -5) == 'other') {
                    $value['element_name'] = str_replace('_other', '_0', $value['element_name']);
                    $checkbox_has_other = true;
                }
            }
            $condition_params = array();
            $condition_params['form_id'] = $form_id;
            $condition_params['element_name'] = $value['element_name'];
            $condition_params['rule_condition'] = $value['rule_condition'];
            $condition_params['rule_keyword'] = $value['rule_keyword'];
            if ($value['element_type'] == 'radio' || $value['element_type'] == 'select') {
                //we need to add unique suffix into the element name
                //so that we can use the same radio/dropdown field multiple times to build a rule
                $condition_params['element_name'] = $value['element_name'] . '_' . $radio_suffix_id;
                $current_handler_conditions_array[] = 'condition_' . $value['element_name'] . '_' . $radio_suffix_id;
            } else {
                $current_handler_conditions_array[] = 'condition_' . $value['element_name'];
            }
            $mf_handler_code .= mf_get_condition_javascript($dbh, $condition_params);
            //build the bind code
            if ($value['element_type'] == 'radio') {
                $mf_bind_code .= "\$('input[name={$value['element_name']}]').bind('change click', function() {\n";
            } else {
                if ($value['element_type'] == 'time') {
                    $mf_bind_code .= "\$('#{$value['element_name']}_1,#{$value['element_name']}_2,#{$value['element_name']}_3,#{$value['element_name']}_4').bind('keyup mouseout change click', function() {\n";
                } else {
                    if ($value['element_type'] == 'money') {
                        $mf_bind_code .= "\$('#{$value['element_name']},#{$value['element_name']}_1,#{$value['element_name']}_2').bind('keyup mouseout change click', function() {\n";
                    } else {
                        if ($value['element_type'] == 'matrix') {
                            $exploded = array();
                            $exploded = explode('_', $value['element_name']);
                            $matrix_element_id = (int) $exploded[1];
                            //we only need to bind the event to the parent element_id of the matrix
                            $query = "select element_matrix_parent_id from " . MF_TABLE_PREFIX . "form_elements where element_id = ? and form_id = ? and element_status = 1";
                            $params = array($matrix_element_id, $form_id);
                            $sth = mf_do_query($query, $params, $dbh);
                            $row = mf_do_fetch_result($sth);
                            if (!empty($row['element_matrix_parent_id'])) {
                                $matrix_element_id = $row['element_matrix_parent_id'];
                            }
                            if (in_array($matrix_element_id, $matrix_bind_array) === false) {
                                $mf_bind_code .= "\$('#li_{$matrix_element_id} :input').bind('change click', function() {\n";
                                $matrix_bind_array[] = $matrix_element_id;
                            } else {
                                continue;
                            }
                        } else {
                            if ($value['element_type'] == 'checkbox') {
                                if ($checkbox_has_other) {
                                    $exploded = array();
                                    $exploded = explode('_', $value['element_name']);
                                    $mf_bind_code .= "\$('#{$value['element_name']},#element_{$exploded[1]}_other').bind('keyup mouseout change click', function() {\n";
                                } else {
                                    $mf_bind_code .= "\$('#{$value['element_name']}').bind('keyup mouseout change click', function() {\n";
                                }
                            } else {
                                if ($value['element_type'] == 'select') {
                                    $mf_bind_code .= "\$('#{$value['element_name']}').bind('keyup change', function() {\n";
                                } else {
                                    if ($value['element_type'] == 'date' || $value['element_type'] == 'europe_date') {
                                        $mf_bind_code .= "\$('#{$value['element_name']}_1,#{$value['element_name']}_2,#{$value['element_name']}_3').bind('keyup mouseout change click', function() {\n";
                                    } else {
                                        if ($value['element_type'] == 'phone') {
                                            $mf_bind_code .= "\$('#{$value['element_name']}_1,#{$value['element_name']}_2,#{$value['element_name']}_3').bind('keyup mouseout change click', function() {\n";
                                        } else {
                                            $mf_bind_code .= "\$('#{$value['element_name']}').bind('keyup mouseout change click', function() {\n";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            $mf_bind_code .= "\tmf_handler_{$element_id}();\n";
            $mf_bind_code .= "});\n";
        }
        //evaluate all conditions
        if ($rule_all_any == 'all') {
            $logic_operator = ' && ';
        } else {
            $logic_operator = ' || ';
        }
        if ($rule_show_hide == 'show') {
            $action_code_primary = "\$('#li_{$element_id}').show();";
            $action_code_secondary = "\$('#li_{$element_id}').hide();";
        } else {
            if ($rule_show_hide == 'hide') {
                $action_code_primary = "\$('#li_{$element_id}').hide();";
                $action_code_secondary = "\$('#li_{$element_id}').show();";
            }
        }
        $current_handler_conditions_joined = implode($logic_operator, $current_handler_conditions_array);
        $mf_handler_code .= "\tif({$current_handler_conditions_joined}){\n";
        $mf_handler_code .= "\t\t{$action_code_primary}\n";
        $mf_handler_code .= "\t}else{\n";
        $mf_handler_code .= "\t\t{$action_code_secondary}\n";
        $mf_handler_code .= "\t}\n\n";
        //postMessage to adjust the height of the iframe
        $mf_handler_code .= "\tif(\$(\"html\").hasClass(\"embed\")){\n";
        $mf_handler_code .= "\t\t\$.postMessage({mf_iframe_height: \$('body').outerHeight(true)}, '*', parent );\n";
        $mf_handler_code .= "\t}\n";
        /************************************************************************************/
        $mf_handler_code .= "}" . "\n";
        //end mf_handler_xx
        $mf_initialize_code .= "mf_handler_{$element_id}();\n";
    }
    $javascript_code = <<<EOT
<script type="text/javascript">
\$(function(){

{$mf_handler_code}
{$mf_bind_code}
{$mf_initialize_code}

});
</script>
EOT;
    return $javascript_code;
}