function mf_is_payment_has_value($dbh, $form_id, $entry_id)
{
    $payment_has_value = false;
    $props = array('payment_enable_merchant', 'payment_merchant_type', 'payment_price_amount', 'payment_price_type', 'payment_delay_notifications', 'form_review', 'form_page_total');
    $form_properties = mf_get_form_properties($dbh, $form_id, $props);
    if ($form_properties['payment_enable_merchant'] == 1 && $form_properties['payment_merchant_type'] == 'stripe') {
        if ($form_properties['payment_price_type'] == 'variable') {
            $total_payment_amount = (double) mf_get_payment_total($dbh, $form_id, $entry_id, 0, 'live');
            if (!empty($total_payment_amount)) {
                $payment_has_value = true;
            }
        } else {
            if ($form_properties['payment_price_type'] == 'fixed') {
                $total_payment_amount = (double) $form_properties['payment_price_amount'];
                if (!empty($total_payment_amount)) {
                    $payment_has_value = true;
                }
            }
        }
    }
    return $payment_has_value;
}
Example #2
0
 $row = mf_do_fetch_result($sth);
 $payment_paypal_email = strtolower($row['payment_paypal_email']);
 $payment_currency = $row['payment_currency'];
 $payment_price_type = $row['payment_price_type'];
 $payment_price_amount = (double) $row['payment_price_amount'];
 //make sure seller email address match
 if (strtolower($_POST['business']) != $payment_paypal_email) {
     $error_message .= "PayPal Email does not match. Current: {$payment_paypal_email}: - business: {$_POST['business']}";
 }
 //make sure the currency match
 if (strtolower($payment_currency) != strtolower($_POST['mc_currency'])) {
     $error_message .= "PayPal currency does not match. Current: {$payment_currency}: - mc_currency: {$_POST['mc_currency']}";
 }
 //make sure the amount paid match or larger
 if ($payment_price_type == 'variable') {
     $payment_amount = (double) mf_get_payment_total($dbh, $form_id, $entry_id, 0, 'live');
 } else {
     if ($payment_price_type == 'fixed') {
         $payment_amount = (double) $payment_price_amount;
     }
 }
 $gross_payment = (double) $_POST['mc_gross'];
 if (abs($gross_payment - $payment_amount) > 0.001) {
     $error_message .= "Gross amount does not match. Amount: {$payment_amount} - mc_gross: {$gross_payment}";
 }
 //if there is any error, log and exit
 if (!empty($error_message)) {
     error_log($error_message);
     error_log($listener->getTextReport());
     exit;
 } else {
Example #3
0
function mf_display_form_review($dbh, $form_id, $record_id, $from_page_num, $form_params = array())
{
    global $mf_lang;
    if (!empty($form_params['integration_method'])) {
        $integration_method = $form_params['integration_method'];
    } else {
        $integration_method = '';
    }
    if (!empty($form_params['machform_path'])) {
        $machform_path = $form_params['machform_path'];
    } else {
        $machform_path = '';
    }
    if (!empty($form_params['machform_data_path'])) {
        $machform_data_path = $form_params['machform_data_path'];
    } else {
        $machform_data_path = '';
    }
    $mf_settings = mf_get_settings($dbh);
    //get form properties data
    $query = "select \r\n\t\t\t\t\t\t  form_name,\r\n\t\t\t\t\t\t  form_has_css,\r\n\t\t\t\t\t\t  form_redirect,\r\n\t\t\t\t\t\t  form_review_primary_text,\r\n\t\t\t\t\t\t  form_review_secondary_text,\r\n\t\t\t\t\t\t  form_review_primary_img,\r\n\t\t\t\t\t\t  form_review_secondary_img,\r\n\t\t\t\t\t\t  form_review_use_image,\r\n\t\t\t\t\t\t  form_review_title,\r\n\t\t\t\t\t\t  form_review_description,\r\n\t\t\t\t\t\t  form_page_total,\r\n\t\t\t\t\t\t  form_lastpage_title,\r\n\t\t\t\t\t\t  form_pagination_type,\r\n\t\t\t\t\t\t  form_theme_id,\r\n\t\t\t\t\t\t  payment_show_total,\r\n\t\t\t\t\t\t  payment_total_location,\r\n\t\t\t\t\t\t  payment_enable_merchant,\r\n\t\t\t\t\t\t  payment_currency,\r\n\t\t\t\t\t\t  payment_price_type,\r\n\t\t\t\t\t\t  payment_price_amount\r\n\t\t\t\t     from \r\n\t\t\t\t     \t " . MF_TABLE_PREFIX . "forms \r\n\t\t\t\t    where \r\n\t\t\t\t    \t form_id=?";
    $params = array($form_id);
    $sth = mf_do_query($query, $params, $dbh);
    $row = mf_do_fetch_result($sth);
    $form_has_css = $row['form_has_css'];
    $form_redirect = $row['form_redirect'];
    $form_review_primary_text = $row['form_review_primary_text'];
    $form_review_secondary_text = $row['form_review_secondary_text'];
    $form_review_primary_img = $row['form_review_primary_img'];
    $form_review_secondary_img = $row['form_review_secondary_img'];
    $form_review_use_image = (int) $row['form_review_use_image'];
    $form_review_title = $row['form_review_title'];
    $form_review_description = $row['form_review_description'];
    $form_page_total = $row['form_page_total'];
    $form_lastpage_title = $row['form_lastpage_title'];
    $form_pagination_type = $row['form_pagination_type'];
    $form_name = htmlspecialchars($row['form_name'], ENT_QUOTES);
    $form_theme_id = $row['form_theme_id'];
    $payment_show_total = (int) $row['payment_show_total'];
    $payment_total_location = $row['payment_total_location'];
    $payment_enable_merchant = (int) $row['payment_enable_merchant'];
    if ($payment_enable_merchant < 1) {
        $payment_enable_merchant = 0;
    }
    $payment_currency = $row['payment_currency'];
    $payment_price_type = $row['payment_price_type'];
    $payment_price_amount = $row['payment_price_amount'];
    //prepare entry data for previewing
    $param['strip_download_link'] = true;
    $param['review_mode'] = true;
    $param['show_attach_image'] = true;
    $param['machform_data_path'] = $machform_data_path;
    $entry_details = mf_get_entry_details($dbh, $form_id, $record_id, $param);
    $entry_data = '<table id="machform_review_table" width="100%" border="0" cellspacing="0" cellpadding="0"><tbody>' . "\n";
    $toggle = false;
    foreach ($entry_details as $data) {
        if ($toggle) {
            $toggle = false;
            $row_style = 'class="alt"';
        } else {
            $toggle = true;
            $row_style = '';
        }
        if ($data['label'] == 'mf_page_break' && $data['value'] == 'mf_page_break') {
            $data['label'] = '&nbsp;';
            $data['value'] = '&nbsp;';
            $row_style = '';
        }
        $entry_data .= "<tr {$row_style}>\n";
        $entry_data .= "<td class=\"mf_review_label\" width=\"40%\">{$data['label']}</td>\n";
        $entry_data .= "<td class=\"mf_review_value\" width=\"60%\">" . nl2br($data['value']) . "</td>\n";
        $entry_data .= "</tr>\n";
    }
    $entry_data .= '</tbody></table>';
    //check for specific form css, if any, use it instead
    if ($form_has_css) {
        $css_dir = $mf_settings['data_dir'] . "/form_{$form_id}/css/";
    }
    if ($integration_method == 'iframe') {
        $embed_class = 'class="embed"';
    }
    //if the form has multiple pages
    //display the pagination header
    if ($form_page_total > 1) {
        //build pagination header based on the selected type. possible values:
        //steps - display multi steps progress
        //percentage - display progress bar with percentage
        //disabled - disabled
        $page_breaks_data = array();
        $page_title_array = array();
        //get page titles
        $query = "SELECT \r\n\t\t\t\t\t\t\telement_page_title\r\n\t\t\t\t\t\tFROM \r\n\t\t\t\t\t\t\t" . MF_TABLE_PREFIX . "form_elements\r\n\t\t\t\t\t   WHERE\r\n\t\t\t\t\t\t\tform_id = ? and element_status = 1 and element_type = 'page_break'\r\n\t\t\t\t\tORDER BY \r\n\t\t\t\t\t   \t\telement_page_number asc";
        $params = array($form_id);
        $sth = mf_do_query($query, $params, $dbh);
        while ($row = mf_do_fetch_result($sth)) {
            $page_title_array[] = $row['element_page_title'];
        }
        if ($form_pagination_type == 'steps') {
            $page_titles_markup = '';
            $i = 1;
            foreach ($page_title_array as $page_title) {
                $page_titles_markup .= '<td align="center"><span id="page_num_' . $i . '" class="ap_tp_num">' . $i . '</span><span id="page_title_' . $i . '" class="ap_tp_text">' . $page_title . '</span></td><td align="center" class="ap_tp_arrow">&gt;</td>' . "\n";
                $i++;
            }
            //add the last page title into the pagination header markup
            $page_titles_markup .= '<td align="center"><span id="page_num_' . $i . '" class="ap_tp_num">' . $i . '</span><span id="page_title_' . $i . '" class="ap_tp_text">' . $form_lastpage_title . '</span></td>';
            $i++;
            $page_titles_markup .= '<td align="center" class="ap_tp_arrow">&gt;</td><td align="center"><span id="page_num_' . $i . '" class="ap_tp_num ap_tp_num_active">' . $i . '</span><span id="page_title_' . $i . '" class="ap_tp_text ap_tp_text_active">' . $form_review_title . '</span></td>';
            $pagination_header = <<<EOT
\t\t\t<ul>
\t\t\t<li id="pagination_header" class="li_pagination">
\t\t\t <table class="ap_table_pagination" width="100%" border="0" cellspacing="0" cellpadding="0">
\t\t\t  <tr> 
\t\t\t  \t{$page_titles_markup}
\t\t\t  </tr>
\t\t\t</table>
\t\t\t</li>
\t\t\t</ul>
EOT;
        } else {
            if ($form_pagination_type == 'percentage') {
                $page_total = count($page_title_array) + 2;
                $percent_value = 99;
                $page_number_title = sprintf($mf_lang['page_title'], $page_total, $page_total);
                $pagination_header = <<<EOT
\t\t\t<ul>
\t\t\t\t<li id="pagination_header" class="li_pagination" title="Click to edit">
\t\t\t    <h3 id="page_title_{$page_total}">{$page_number_title}</h3>
\t\t\t\t<div class="mf_progress_container">          
\t\t\t    \t<div id="mf_progress_percentage" class="mf_progress_value" style="width: {$percent_value}%"><span>{$percent_value}%</span></div>
\t\t\t\t</div>
\t\t\t\t</li>
\t\t\t</ul>
EOT;
            } else {
                $pagination_header = '';
            }
        }
    }
    //build the button markup (image or text)
    if (!empty($form_review_use_image)) {
        $button_markup = <<<EOT
<input id="review_submit" class="submit_img_primary" type="image" name="review_submit" alt="{$form_review_primary_text}" src="{$form_review_primary_img}" />
<input id="review_back" class="submit_img_secondary" type="image" name="review_back" alt="{$form_review_secondary_text}" src="{$form_review_secondary_img}" />
EOT;
    } else {
        $button_markup = <<<EOT
<input id="review_submit" class="button_text btn_primary" type="submit" name="review_submit" value="{$form_review_primary_text}" />
<input id="review_back" class="button_text btn_secondary" type="submit" name="review_back" value="{$form_review_secondary_text}" />
EOT;
    }
    //if this form is using custom theme
    if (!empty($form_theme_id)) {
        //get the field highlight color for the particular theme
        $query = "SELECT \r\n\t\t\t\t\t\t\thighlight_bg_type,\r\n\t\t\t\t\t\t\thighlight_bg_color,\r\n\t\t\t\t\t\t\tform_shadow_style,\r\n\t\t\t\t\t\t\tform_shadow_size,\r\n\t\t\t\t\t\t\tform_shadow_brightness,\r\n\t\t\t\t\t\t\tform_button_type,\r\n\t\t\t\t\t\t\tform_button_text,\r\n\t\t\t\t\t\t\tform_button_image,\r\n\t\t\t\t\t\t\ttheme_has_css  \r\n\t\t\t\t\t\tFROM \r\n\t\t\t\t\t\t\t" . MF_TABLE_PREFIX . "form_themes \r\n\t\t\t\t\t   WHERE \r\n\t\t\t\t\t   \t\ttheme_id = ?";
        $params = array($form_theme_id);
        $sth = mf_do_query($query, $params, $dbh);
        $row = mf_do_fetch_result($sth);
        $form_shadow_style = $row['form_shadow_style'];
        $form_shadow_size = $row['form_shadow_size'];
        $form_shadow_brightness = $row['form_shadow_brightness'];
        $theme_has_css = (int) $row['theme_has_css'];
        //if the theme has css file, make sure to refer to that file
        //otherwise, generate the css dynamically
        if (!empty($theme_has_css)) {
            $theme_css_link = '<link rel="stylesheet" type="text/css" href="' . $machform_path . $mf_settings['data_dir'] . '/themes/theme_' . $form_theme_id . '.css" media="all" />';
        } else {
            $theme_css_link = '<link rel="stylesheet" type="text/css" href="' . $machform_path . 'css_theme.php?theme_id=' . $form_theme_id . '" media="all" />';
        }
        if ($row['highlight_bg_type'] == 'color') {
            $field_highlight_color = $row['highlight_bg_color'];
        } else {
            //if the field highlight is using pattern instead of color, set the color to empty string
            $field_highlight_color = '';
        }
        //get the css link for the fonts
        $font_css_markup = mf_theme_get_fonts_link($dbh, $form_theme_id);
        //get the form shadow classes
        if (!empty($form_shadow_style) && $form_shadow_style != 'disabled') {
            preg_match_all("/[A-Z]/", $form_shadow_style, $prefix_matches);
            //this regex simply get the capital characters of the shadow style name
            //example: RightPerspectiveShadow result to RPS and then being sliced to RP
            $form_shadow_prefix_code = substr(implode("", $prefix_matches[0]), 0, -1);
            $form_shadow_size_class = $form_shadow_prefix_code . ucfirst($form_shadow_size);
            $form_shadow_brightness_class = $form_shadow_prefix_code . ucfirst($form_shadow_brightness);
            if (empty($integration_method)) {
                //only display shadow if the form is not being embedded using any method
                $form_container_class = $form_shadow_style . ' ' . $form_shadow_size_class . ' ' . $form_shadow_brightness_class;
            }
        }
    } else {
        //if the form doesn't have any theme being applied
        $field_highlight_color = '#FFF7C0';
        if (empty($integration_method)) {
            $form_container_class = 'WarpShadow WLarge WNormal';
            //default shadow
        } else {
            $form_container_class = '';
            //dont show any shadow when the form being embedded
        }
    }
    //if the form has enabled merchant support and set the total payment to be displayed
    if (!empty($payment_enable_merchant) && !empty($payment_show_total)) {
        $currency_symbol = '&#36;';
        switch ($payment_currency) {
            case 'USD':
                $currency_symbol = '&#36;';
                break;
            case 'EUR':
                $currency_symbol = '&#8364;';
                break;
            case 'GBP':
                $currency_symbol = '&#163;';
                break;
            case 'AUD':
                $currency_symbol = 'A&#36;';
                break;
            case 'CAD':
                $currency_symbol = 'C&#36;';
                break;
            case 'JPY':
                $currency_symbol = '&#165;';
                break;
            case 'THB':
                $currency_symbol = '&#3647;';
                break;
            case 'HUF':
                $currency_symbol = '&#70;&#116;';
                break;
            case 'CHF':
                $currency_symbol = 'CHF';
                break;
            case 'CZK':
                $currency_symbol = '&#75;&#269;';
                break;
            case 'SEK':
                $currency_symbol = 'kr';
                break;
            case 'DKK':
                $currency_symbol = 'kr';
                break;
            case 'PHP':
                $currency_symbol = '&#36;';
                break;
            case 'MYR':
                $currency_symbol = 'RM';
                break;
            case 'PLN':
                $currency_symbol = '&#122;&#322;';
                break;
            case 'BRL':
                $currency_symbol = 'R&#36;';
                break;
            case 'HKD':
                $currency_symbol = 'HK&#36;';
                break;
            case 'MXN':
                $currency_symbol = 'Mex&#36;';
                break;
            case 'TWD':
                $currency_symbol = 'NT&#36;';
                break;
            case 'TRY':
                $currency_symbol = 'TL';
                break;
        }
        if ($payment_total_location == 'review' || $payment_total_location == 'all') {
            $session_id = session_id();
            if ($payment_price_type == 'variable') {
                $total_payment = (double) mf_get_payment_total($dbh, $form_id, $session_id, 0);
            } elseif ($payment_price_type == 'fixed') {
                $total_payment = $payment_price_amount;
            }
            $payment_total_markup = <<<EOT
\t\t\t\t<li class="total_payment mf_review">
\t\t\t\t\t<span>
\t\t\t\t\t\t<h3>{$currency_symbol}<var>{$total_payment}</var></h3>
\t\t\t\t\t\t<h5>{$mf_lang['payment_total']}</h5>
\t\t\t\t\t</span>
\t\t\t\t</li>
EOT;
        }
    }
    if (empty($mf_settings['disable_machform_link'])) {
        $powered_by_markup = 'Powered by MachForm';
    } else {
        $powered_by_markup = '';
    }
    $self_address = htmlentities($_SERVER['PHP_SELF']);
    //prevent XSS
    if ($integration_method == 'php') {
        $form_markup = <<<EOT
<link rel="stylesheet" type="text/css" href="data/form_default/css/view.css" media="all" />
{$theme_css_link}
{$font_css_markup}
<style>
html{
\tbackground: none repeat scroll 0 0 transparent;
}
</style>

<div id="main_body" class="integrated">
\t<div id="form_container">
\t\t<form id="form_{$form->id}" class="appnitro" method="post" action="{$self_address}">
\t\t    <div class="form_description">
\t\t\t\t<h2>{$form_review_title}</h2>
\t\t\t\t<p>{$form_review_description}</p>
\t\t\t</div>
\t\t\t{$pagination_header}
\t\t\t{$entry_data}
\t\t\t<ul>
\t\t\t{$payment_total_markup}
\t\t\t<li id="li_buttons" class="buttons">
\t\t\t    <input type="hidden" name="id" value="{$form_id}" />
\t\t\t    <input type="hidden" name="mf_page_from" value="{$from_page_num}" />
\t\t\t    {$button_markup}
\t\t\t</li>
\t\t\t</ul>
\t\t</form>\t\t
\t</div>
</div>
EOT;
    } else {
        if ($integration_method == 'iframe') {
            $auto_height_js = <<<EOT
<script type="text/javascript" src="{$machform_path}js/jquery.min.js"></script>
<script type="text/javascript" src="{$machform_path}js/jquery.ba-postmessage.min.js"></script>
<script type="text/javascript">
    \$(function(){
    \t\$.postMessage({mf_iframe_height: \$('body').outerHeight(true)}, '*', parent );
    });
</script>
EOT;
        }
        $form_markup = <<<EOT
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html {$embed_class} xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>{$form_name}</title>
<link rel="stylesheet" type="text/css" href="data/form_default/css/view.css" media="all" />
{$theme_css_link}
{$font_css_markup}
{$auto_height_js}
</head>
<body id="main_body">
\t
\t<img id="top" src="{$machform_path}images/top.png" alt="" />
\t<div id="form_container" class="{$form_container_class}">
\t
\t\t<h1><a>MachForm</a></h1>
\t\t<form id="form_{$form_id}" class="appnitro" method="post" action="{$self_address}">
\t\t    <div class="form_description">
\t\t\t\t<h2>{$form_review_title}</h2>
\t\t\t\t<p>{$form_review_description}</p>
\t\t\t</div>
\t\t\t{$pagination_header}
\t\t\t{$entry_data}
\t\t\t<ul>
\t\t\t{$payment_total_markup}
\t\t\t<li id="li_buttons" class="buttons">
\t\t\t    <input type="hidden" name="id" value="{$form_id}" />
\t\t\t    <input type="hidden" name="mf_page_from" value="{$from_page_num}" />
\t\t\t    {$button_markup}
\t\t\t</li>
\t\t\t</ul>
\t\t</form>\t\t
\t\t\t
\t</div>
\t<img id="bottom" src="{$machform_path}images/bottom.png" alt="" />
\t</body>
</html>
EOT;
    }
    return $form_markup;
}
$payment_trial_unit = $row['payment_trial_unit'];
$payment_trial_amount = (double) $row['payment_trial_amount'];
$payment_delay_notifications = (int) $row['payment_delay_notifications'];
if (!empty($payment_enable_merchant) && $payment_merchant_type == 'stripe') {
    if (!empty($payment_stripe_enable_test_mode)) {
        $stripe_secret_key = $payment_stripe_test_secret_key;
    } else {
        $stripe_secret_key = $payment_stripe_live_secret_key;
    }
    //calculate payment amount
    if ($payment_price_type == 'fixed') {
        $charge_amount = $payment_price_amount * 100;
        //charge in cents
    } else {
        if ($payment_price_type == 'variable') {
            $charge_amount = (double) mf_get_payment_total($dbh, $form_id, $payment_record_id, 0, 'live');
            $charge_amount = $charge_amount * 100;
        }
    }
    //set private key
    Stripe::setApiKey($stripe_secret_key);
    //create Customer object
    $customer_desc = "Customer for (Form #{$form_id} - Entry #{$payment_record_id})";
    $customer_name = trim($payment_data['first_name'] . ' ' . $payment_data['last_name']);
    if (!empty($customer_name)) {
        $customer_desc .= " - {$customer_name}";
    }
    $customer_obj = Stripe_Customer::create(array("card" => $token, "description" => $customer_desc));
    if (!empty($payment_enable_recurring)) {
        //this is recurring payments
        $trial_period_days = 0;
function mf_display_form_payment($dbh, $form_id, $record_id, $form_params = array())
{
    global $mf_lang;
    if (!empty($form_params['integration_method'])) {
        $integration_method = $form_params['integration_method'];
    } else {
        $integration_method = '';
    }
    if (!empty($form_params['machform_path'])) {
        $machform_path = $form_params['machform_path'];
    } else {
        $machform_path = '';
    }
    if (!empty($form_params['machform_data_path'])) {
        $machform_data_path = $form_params['machform_data_path'];
    } else {
        $machform_data_path = '';
    }
    //check permission to access this page
    if ($_SESSION['mf_form_payment_access'][$form_id] !== true) {
        return "Your session has been expired. Please <a href='view.php?id={$form_id}'>click here</a> to start again.";
    }
    $mf_settings = mf_get_settings($dbh);
    //get form properties data
    $query = "select \r\n\t\t\t\t\t\t  form_name,\r\n\t\t\t\t\t\t  form_has_css,\r\n\t\t\t\t\t\t  form_redirect,\r\n\t\t\t\t\t\t  form_language,\r\n\t\t\t\t\t\t  form_review,\r\n\t\t\t\t\t\t  form_review_primary_text,\r\n\t\t\t\t\t\t  form_review_secondary_text,\r\n\t\t\t\t\t\t  form_review_primary_img,\r\n\t\t\t\t\t\t  form_review_secondary_img,\r\n\t\t\t\t\t\t  form_review_use_image,\r\n\t\t\t\t\t\t  form_review_title,\r\n\t\t\t\t\t\t  form_review_description,\r\n\t\t\t\t\t\t  form_resume_enable,\r\n\t\t\t\t\t\t  form_page_total,\r\n\t\t\t\t\t\t  form_lastpage_title,\r\n\t\t\t\t\t\t  form_pagination_type,\r\n\t\t\t\t\t\t  form_theme_id,\r\n\t\t\t\t\t\t  payment_show_total,\r\n\t\t\t\t\t\t  payment_total_location,\r\n\t\t\t\t\t\t  payment_enable_merchant,\r\n\t\t\t\t\t\t  payment_merchant_type,\r\n\t\t\t\t\t\t  payment_currency,\r\n\t\t\t\t\t\t  payment_price_type,\r\n\t\t\t\t\t\t  payment_price_name,\r\n\t\t\t\t\t\t  payment_price_amount,\r\n\t\t\t\t\t\t  payment_ask_billing,\r\n\t\t\t\t\t\t  payment_ask_shipping,\r\n\t\t\t\t\t\t  payment_stripe_live_public_key,\r\n\t\t\t\t\t\t  payment_stripe_test_public_key,\r\n\t\t\t\t\t\t  payment_stripe_enable_test_mode,\r\n\t\t\t\t\t\t  payment_enable_recurring,\r\n\t\t\t\t\t\t  payment_recurring_cycle,\r\n\t\t\t\t\t\t  payment_recurring_unit,\r\n\t\t\t\t\t\t  payment_enable_trial,\r\n\t\t\t\t\t\t  payment_trial_period,\r\n\t\t\t\t\t\t  payment_trial_unit,\r\n\t\t\t\t\t\t  payment_trial_amount,\r\n\t\t\t\t\t\t  payment_delay_notifications\r\n\t\t\t\t     from \r\n\t\t\t\t     \t " . MF_TABLE_PREFIX . "forms \r\n\t\t\t\t    where \r\n\t\t\t\t    \t form_id=?";
    $params = array($form_id);
    $sth = mf_do_query($query, $params, $dbh);
    $row = mf_do_fetch_result($sth);
    $form_language = $row['form_language'];
    if (!empty($form_language)) {
        mf_set_language($form_language);
    }
    $form_payment_title = $mf_lang['form_payment_title'];
    $form_payment_description = $mf_lang['form_payment_description'];
    $form_has_css = $row['form_has_css'];
    $form_redirect = $row['form_redirect'];
    $form_review = (int) $row['form_review'];
    $form_review_primary_text = $row['form_review_primary_text'];
    $form_review_secondary_text = $row['form_review_secondary_text'];
    $form_review_primary_img = $row['form_review_primary_img'];
    $form_review_secondary_img = $row['form_review_secondary_img'];
    $form_review_use_image = (int) $row['form_review_use_image'];
    $form_review_title = $row['form_review_title'];
    $form_review_description = $row['form_review_description'];
    $form_page_total = (int) $row['form_page_total'];
    $form_lastpage_title = $row['form_lastpage_title'];
    $form_pagination_type = $row['form_pagination_type'];
    $form_name = htmlspecialchars($row['form_name'], ENT_QUOTES);
    $form_theme_id = $row['form_theme_id'];
    $form_resume_enable = (int) $row['form_resume_enable'];
    $payment_show_total = (int) $row['payment_show_total'];
    $payment_total_location = $row['payment_total_location'];
    $payment_enable_merchant = (int) $row['payment_enable_merchant'];
    if ($payment_enable_merchant < 1) {
        $payment_enable_merchant = 0;
    }
    $payment_currency = $row['payment_currency'];
    $payment_price_type = $row['payment_price_type'];
    $payment_price_amount = $row['payment_price_amount'];
    $payment_price_name = htmlspecialchars($row['payment_price_name'], ENT_QUOTES);
    $payment_ask_billing = (int) $row['payment_ask_billing'];
    $payment_ask_shipping = (int) $row['payment_ask_shipping'];
    $payment_merchant_type = $row['payment_merchant_type'];
    $payment_stripe_enable_test_mode = (int) $row['payment_stripe_enable_test_mode'];
    $payment_stripe_live_public_key = trim($row['payment_stripe_live_public_key']);
    $payment_stripe_test_public_key = trim($row['payment_stripe_test_public_key']);
    $payment_enable_recurring = (int) $row['payment_enable_recurring'];
    $payment_recurring_cycle = (int) $row['payment_recurring_cycle'];
    $payment_recurring_unit = $row['payment_recurring_unit'];
    $payment_enable_trial = (int) $row['payment_enable_trial'];
    $payment_trial_period = (int) $row['payment_trial_period'];
    $payment_trial_unit = $row['payment_trial_unit'];
    $payment_trial_amount = (double) $row['payment_trial_amount'];
    $payment_delay_notifications = (int) $row['payment_delay_notifications'];
    //check for specific form css, if any, use it instead
    if ($form_has_css) {
        $css_dir = $mf_settings['data_dir'] . "/form_{$form_id}/css/";
    }
    if ($integration_method == 'iframe') {
        $embed_class = 'class="embed"';
    }
    //get total payment
    $currency_symbol = '&#36;';
    if ($payment_price_type == 'variable') {
        $total_payment_amount = (double) mf_get_payment_total($dbh, $form_id, $record_id, 0, 'live');
        $payment_items = mf_get_payment_items($dbh, $form_id, $record_id, 'live');
        //build the payment list markup
        $payment_list_items_markup = '';
        if (!empty($payment_items)) {
            foreach ($payment_items as $item) {
                if ($item['type'] == 'money') {
                    $payment_list_items_markup .= "<li>{$item['title']} <span>{$currency_symbol}{$item['amount']}</span></li>" . "\n";
                } else {
                    if ($item['type'] == 'checkbox') {
                        $payment_list_items_markup .= "<li>{$item['sub_title']} <span>{$currency_symbol}{$item['amount']}</span></li>" . "\n";
                    } else {
                        if ($item['type'] == 'select' || $item['type'] == 'radio') {
                            $payment_list_items_markup .= "<li>{$item['title']} <em>({$item['sub_title']})</em> <span>{$currency_symbol}{$item['amount']}</span></li>" . "\n";
                        }
                    }
                }
            }
        }
    } else {
        if ($payment_price_type == 'fixed') {
            $total_payment_amount = $payment_price_amount;
            $payment_list_items_markup = "<li>{$payment_price_name}</li>";
        }
    }
    //construct payment terms
    if (!empty($payment_enable_recurring)) {
        $payment_plurals = '';
        if ($payment_recurring_cycle > 1) {
            $payment_plurals = 's';
            $payment_recurring_cycle_markup = $payment_recurring_cycle . ' ';
        }
        if (!empty($payment_enable_trial)) {
            //recurring with trial period
            $payment_trial_price = $currency_symbol . $payment_trial_amount;
            if (empty($payment_trial_amount)) {
                $payment_trial_price = 'free';
            }
            $payment_trial_plurals = '';
            if ($payment_trial_period > 1) {
                $payment_trial_plurals = 's';
            }
            $payment_term_markup = <<<EOT
\t\t\t\t\t<li class="payment_summary_term">
\t\t\t\t\t\t<em>Trial period: {$payment_trial_period} {$payment_trial_unit}{$payment_trial_plurals} ({$payment_trial_price})</em><br>
\t\t\t\t\t\t<em>Then you will be charged {$currency_symbol}{$total_payment_amount} every {$payment_recurring_cycle_markup}{$payment_recurring_unit}{$payment_plurals}</em>
\t\t\t\t\t</li>
EOT;
        } else {
            $payment_term_markup = "<li class=\"payment_summary_term\"><em>You will be charged {$currency_symbol}{$total_payment_amount} every {$payment_recurring_cycle_markup}{$payment_recurring_unit}{$payment_plurals}</em></li>";
        }
    }
    //if the form has multiple pages
    //display the pagination header
    if ($form_page_total > 1) {
        //build pagination header based on the selected type. possible values:
        //steps - display multi steps progress
        //percentage - display progress bar with percentage
        //disabled - disabled
        $page_breaks_data = array();
        $page_title_array = array();
        //get page titles
        $query = "SELECT \r\n\t\t\t\t\t\t\telement_page_title\r\n\t\t\t\t\t\tFROM \r\n\t\t\t\t\t\t\t" . MF_TABLE_PREFIX . "form_elements\r\n\t\t\t\t\t   WHERE\r\n\t\t\t\t\t\t\tform_id = ? and element_status = 1 and element_type = 'page_break'\r\n\t\t\t\t\tORDER BY \r\n\t\t\t\t\t   \t\telement_page_number asc";
        $params = array($form_id);
        $sth = mf_do_query($query, $params, $dbh);
        while ($row = mf_do_fetch_result($sth)) {
            $page_title_array[] = $row['element_page_title'];
        }
        if ($form_pagination_type == 'steps') {
            $page_titles_markup = '';
            $i = 1;
            foreach ($page_title_array as $page_title) {
                $page_titles_markup .= '<td align="center"><span id="page_num_' . $i . '" class="ap_tp_num">' . $i . '</span><span id="page_title_' . $i . '" class="ap_tp_text">' . $page_title . '</span></td><td align="center" class="ap_tp_arrow">&gt;</td>' . "\n";
                $i++;
            }
            //add the last page title into the pagination header markup
            $page_titles_markup .= '<td align="center"><span id="page_num_' . $i . '" class="ap_tp_num">' . $i . '</span><span id="page_title_' . $i . '" class="ap_tp_text">' . $form_lastpage_title . '</span></td>';
            if (!empty($form_review)) {
                $i++;
                $page_titles_markup .= '<td align="center" class="ap_tp_arrow">&gt;</td><td align="center"><span id="page_num_' . $i . '" class="ap_tp_num">' . $i . '</span><span id="page_title_' . $i . '" class="ap_tp_text">' . $form_review_title . '</span></td>';
            }
            $i++;
            $page_titles_markup .= '<td align="center" class="ap_tp_arrow">&gt;</td><td align="center"><span id="page_num_' . $i . '" class="ap_tp_num ap_tp_num_active">' . $i . '</span><span id="page_title_' . $i . '" class="ap_tp_text ap_tp_text_active">' . $mf_lang['form_payment_header_title'] . '</span></td>';
            $pagination_header = <<<EOT
\t\t\t<ul>
\t\t\t<li id="pagination_header" class="li_pagination">
\t\t\t <table class="ap_table_pagination" width="100%" border="0" cellspacing="0" cellpadding="0">
\t\t\t  <tr> 
\t\t\t  \t{$page_titles_markup}
\t\t\t  </tr>
\t\t\t</table>
\t\t\t</li>
\t\t\t</ul>
EOT;
        } else {
            if ($form_pagination_type == 'percentage') {
                $page_total = count($page_title_array) + 2;
                if (!empty($form_review)) {
                    $page_total++;
                }
                $percent_value = 99;
                $page_number_title = sprintf($mf_lang['page_title'], $page_total, $page_total);
                $pagination_header = <<<EOT
\t\t\t<ul>
\t\t\t\t<li id="pagination_header" class="li_pagination" title="Click to edit">
\t\t\t    <h3 id="page_title_{$page_total}">{$page_number_title}</h3>
\t\t\t\t<div class="mf_progress_container">          
\t\t\t    \t<div id="mf_progress_percentage" class="mf_progress_value" style="width: {$percent_value}%"><span>{$percent_value}%</span></div>
\t\t\t\t</div>
\t\t\t\t</li>
\t\t\t</ul>
EOT;
            } else {
                $pagination_header = '';
            }
        }
    }
    //build the button markup
    $button_markup = <<<EOT
<input id="btn_submit_payment" class="button_text btn_primary" type="submit" data-originallabel="{$mf_lang['payment_submit_button']}" value="{$mf_lang['payment_submit_button']}" />
EOT;
    //if this form is using custom theme
    if (!empty($form_theme_id)) {
        //get the field highlight color for the particular theme
        $query = "SELECT \r\n\t\t\t\t\t\t\thighlight_bg_type,\r\n\t\t\t\t\t\t\thighlight_bg_color,\r\n\t\t\t\t\t\t\tform_shadow_style,\r\n\t\t\t\t\t\t\tform_shadow_size,\r\n\t\t\t\t\t\t\tform_shadow_brightness,\r\n\t\t\t\t\t\t\tform_button_type,\r\n\t\t\t\t\t\t\tform_button_text,\r\n\t\t\t\t\t\t\tform_button_image,\r\n\t\t\t\t\t\t\ttheme_has_css  \r\n\t\t\t\t\t\tFROM \r\n\t\t\t\t\t\t\t" . MF_TABLE_PREFIX . "form_themes \r\n\t\t\t\t\t   WHERE \r\n\t\t\t\t\t   \t\ttheme_id = ?";
        $params = array($form_theme_id);
        $sth = mf_do_query($query, $params, $dbh);
        $row = mf_do_fetch_result($sth);
        $form_shadow_style = $row['form_shadow_style'];
        $form_shadow_size = $row['form_shadow_size'];
        $form_shadow_brightness = $row['form_shadow_brightness'];
        $theme_has_css = (int) $row['theme_has_css'];
        //if the theme has css file, make sure to refer to that file
        //otherwise, generate the css dynamically
        if (!empty($theme_has_css)) {
            $theme_css_link = '<link rel="stylesheet" type="text/css" href="' . $machform_path . $mf_settings['data_dir'] . '/themes/theme_' . $form_theme_id . '.css" media="all" />';
        } else {
            $theme_css_link = '<link rel="stylesheet" type="text/css" href="' . $machform_path . 'css_theme.php?theme_id=' . $form_theme_id . '" media="all" />';
        }
        if ($row['highlight_bg_type'] == 'color') {
            $field_highlight_color = $row['highlight_bg_color'];
        } else {
            //if the field highlight is using pattern instead of color, set the color to empty string
            $field_highlight_color = '';
        }
        //get the css link for the fonts
        $font_css_markup = mf_theme_get_fonts_link($dbh, $form_theme_id);
        //get the form shadow classes
        if (!empty($form_shadow_style) && $form_shadow_style != 'disabled') {
            preg_match_all("/[A-Z]/", $form_shadow_style, $prefix_matches);
            //this regex simply get the capital characters of the shadow style name
            //example: RightPerspectiveShadow result to RPS and then being sliced to RP
            $form_shadow_prefix_code = substr(implode("", $prefix_matches[0]), 0, -1);
            $form_shadow_size_class = $form_shadow_prefix_code . ucfirst($form_shadow_size);
            $form_shadow_brightness_class = $form_shadow_prefix_code . ucfirst($form_shadow_brightness);
            if (empty($integration_method)) {
                //only display shadow if the form is not being embedded using any method
                $form_container_class = $form_shadow_style . ' ' . $form_shadow_size_class . ' ' . $form_shadow_brightness_class;
            }
        }
    } else {
        //if the form doesn't have any theme being applied
        $field_highlight_color = '#FFF7C0';
        if (empty($integration_method)) {
            $form_container_class = 'WarpShadow WLarge WNormal';
            //default shadow
        } else {
            $form_container_class = '';
            //dont show any shadow when the form being embedded
        }
    }
    if (empty($mf_settings['disable_machform_link'])) {
        $powered_by_markup = 'Powered by <a href="http://www.appnitro.com" target="_blank">MachForm</a>';
    } else {
        $powered_by_markup = '';
    }
    $self_address = htmlentities($_SERVER['PHP_SELF']);
    //prevent XSS
    $country = mf_get_country_list();
    $country_markup = '<option value="" selected="selected"></option>' . "\n";
    foreach ($country as $data) {
        $country_markup .= "<option value=\"{$data['value']}\">{$data['label']}</option>\n";
    }
    $billing_address_markup = '';
    if (!empty($payment_ask_billing)) {
        $billing_address_markup = <<<EOT
\t\t\t\t<li id="li_billing_address" class="address">
\t\t\t\t\t<label class="description">Billing Address <span class="required">*</span></label>
\t\t\t\t\t<div>
\t\t\t\t\t\t<span id="li_billing_span_1">
\t\t\t\t\t\t\t<input id="billing_street" class="element text large" value="" type="text" />
\t\t\t\t\t\t\t<label for="billing_street">{$mf_lang['address_street']}</label>
\t\t\t\t\t\t</span>
\t\t\t\t\t
\t\t\t\t\t\t<span id="li_billing_span_2" class="left state_list">
\t\t\t\t\t\t\t<input id="billing_city" class="element text large" value="" type="text" />
\t\t\t\t\t\t\t<label for="billing_city">{$mf_lang['address_city']}</label>
\t\t\t\t\t\t</span>
\t\t\t\t\t
\t\t\t\t\t\t<span id="li_billing_span_3" class="right state_list">
\t\t\t\t\t\t\t<input id="billing_state" class="element text large" value="" type="text" />
\t\t\t\t\t\t\t<label for="billing_state">{$mf_lang['address_state']}</label>
\t\t\t\t\t\t</span>
\t\t\t\t\t
\t\t\t\t\t\t<span id="li_billing_span_4" class="left">
\t\t\t\t\t\t\t<input id="billing_zipcode" class="element text large" maxlength="15" value="{$default_value_5}" type="text" />
\t\t\t\t\t\t\t<label for="billing_zipcode">{$mf_lang['address_zip']}</label>
\t\t\t\t\t\t</span>
\t\t\t\t\t\t
\t\t\t\t\t\t<span id="li_billing_span_5" class="right">
\t\t\t\t\t\t\t<select class="element select large" id="billing_country"> 
\t\t\t\t\t\t\t\t{$country_markup}\t
\t\t\t\t\t\t\t</select>
\t\t\t\t\t\t<label for="billing_country">{$mf_lang['address_country']}</label>
\t\t\t\t\t    </span>
\t\t\t\t    </div><p id="billing_error_message" class="error" style="display: none"></p>
\t\t\t\t</li>
EOT;
    }
    $shipping_address_markup = '';
    if (!empty($payment_ask_shipping)) {
        $shipping_address_markup = <<<EOT
\t\t\t\t<li id="li_shipping_address" class="address">
\t\t\t\t\t<label class="description shipping_address_detail" style="display: none">Shipping Address <span class="required">*</span></label>
\t\t\t\t\t<div class="shipping_address_detail" style="display: none">
\t\t\t\t\t\t<span id="li_shipping_span_1">
\t\t\t\t\t\t\t<input id="shipping_street" class="element text large" value="" type="text" />
\t\t\t\t\t\t\t<label for="shipping_street">{$mf_lang['address_street']}</label>
\t\t\t\t\t\t</span>
\t\t\t\t\t
\t\t\t\t\t\t<span id="li_shipping_span_2" class="left state_list">
\t\t\t\t\t\t\t<input id="shipping_city" class="element text large" value="" type="text" />
\t\t\t\t\t\t\t<label for="shipping_city">{$mf_lang['address_city']}</label>
\t\t\t\t\t\t</span>
\t\t\t\t\t
\t\t\t\t\t\t<span id="li_shipping_span_3" class="right state_list">
\t\t\t\t\t\t\t<input id="shipping_state" class="element text large" value="" type="text" />
\t\t\t\t\t\t\t<label for="shipping_state">{$mf_lang['address_state']}</label>
\t\t\t\t\t\t</span>
\t\t\t\t\t
\t\t\t\t\t\t<span id="li_shipping_span_4" class="left">
\t\t\t\t\t\t\t<input id="shipping_zipcode" class="element text large" maxlength="15" value="{$default_value_5}" type="text" />
\t\t\t\t\t\t\t<label for="shipping_zipcode">{$mf_lang['address_zip']}</label>
\t\t\t\t\t\t</span>
\t\t\t\t\t\t
\t\t\t\t\t\t<span id="li_shipping_span_5" class="right">
\t\t\t\t\t\t\t<select class="element select large" id="shipping_country"> 
\t\t\t\t\t\t\t\t{$country_markup}\t
\t\t\t\t\t\t\t</select>
\t\t\t\t\t\t<label for="shipping_country">{$mf_lang['address_country']}</label>
\t\t\t\t\t    </span>
\t\t\t\t\t    <p id="shipping_error_message" class="error" style="display: none"></p>
\t\t\t\t    </div>
\t\t\t\t    <div>
\t\t\t\t\t    <input type="checkbox" value="1" checked="checked" class="checkbox" id="mf_same_shipping_address">
\t\t\t\t\t\t<label for="mf_same_shipping_address" class="choice">My shipping address is the same as my billing address</label>
\t\t\t\t\t</div>
\t\t\t\t</li>
EOT;
    }
    if ($payment_merchant_type == 'stripe') {
        if (!empty($payment_stripe_enable_test_mode)) {
            $stripe_public_key = $payment_stripe_test_public_key;
        } else {
            $stripe_public_key = $payment_stripe_live_public_key;
        }
        $stripe_js = <<<EOT
<script type="text/javascript" src="https://js.stripe.com/v1/"></script>
<script type="text/javascript">
\tStripe.setPublishableKey('{$stripe_public_key}');
</script>
<script type="text/javascript" src="{$machform_path}js/payment_stripe.js"></script>
EOT;
    }
    $ssl_suffix = mf_get_ssl_suffix();
    $jquery_url = 'http' . $ssl_suffix . '://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js';
    $current_year = date("Y");
    $year_dropdown_markup = '';
    foreach (range($current_year, $current_year + 15) as $year) {
        $year_dropdown_markup .= "<option value=\"{$year}\">{$year}</option>" . "\n";
    }
    if ($integration_method == 'php') {
        $form_markup = <<<EOT
<link rel="stylesheet" type="text/css" href="{$machform_path}{$css_dir}view.css" media="all" />
<link rel="stylesheet" type="text/css" href="{$machform_path}view.mobile.css" media="all" />
{$theme_css_link}
{$font_css_markup}
<script type="text/javascript" src="{$jquery_url}"></script>
<script type="text/javascript" src="{$machform_path}js/jquery-ui/ui/jquery.effects.core.js"></script>
<script type="text/javascript" src="{$machform_path}view.js"></script>
{$stripe_js}
<style>
html{
\tbackground: none repeat scroll 0 0 transparent;
}
</style>

<div id="main_body" class="integrated no_guidelines" data-machformpath="{$machform_path}">
\t<div id="form_container">
\t\t<form id="form_{$form_id}" class="appnitro" method="post" action="javascript:" data-highlightcolor="{$field_highlight_color}">
\t\t    <div class="form_description">
\t\t\t\t<h2>{$form_payment_title}</h2>
\t\t\t\t<p>{$form_payment_description}</p>
\t\t\t</div>
\t\t\t{$pagination_header}
\t\t\t
\t\t\t<ul class="payment_summary">
\t\t\t\t<li class="payment_summary_amount total_payment" data-basetotal="{$total_payment_amount}">
\t\t\t\t\t<span>
\t\t\t\t\t\t<h3>{$currency_symbol}<var>0</var></h3>
\t\t\t\t\t\t<h5>{$mf_lang['payment_total']}</h5>
\t\t\t\t\t</span>
\t\t\t\t</li>
\t\t\t\t<li class="payment_summary_list">
\t\t\t\t\t<ul class="payment_list_items">
\t\t\t\t\t\t{$payment_list_items_markup}
\t\t\t\t\t</ul>
\t\t\t\t</li>
\t\t\t\t{$payment_term_markup}
\t\t\t</ul>
\t\t\t<ul class="payment_detail_form">
\t\t\t\t<li id="error_message" style="display: none">
\t\t\t\t\t\t<h3 id="error_message_title">{$mf_lang['error_title']}</h3>
\t\t\t\t\t\t<p id="error_message_desc">{$mf_lang['error_desc']}</p>
\t\t\t\t</li>\t
\t\t\t\t<li id="li_accepted_cards">
\t\t\t\t\t<img src="{$machform_path}images/cards/visa.png" alt="Visa" title="Visa" />
\t\t\t\t\t<img src="{$machform_path}images/cards/mastercard.png" alt="MasterCard" title="MasterCard" />
\t\t\t\t\t<img src="{$machform_path}images/cards/amex.png" alt="American Express" title="American Express" />
\t\t\t\t\t<img src="{$machform_path}images/cards/jcb.png" alt="JCB" title="JCB" />
\t\t\t\t\t<img src="{$machform_path}images/cards/discover.png" alt="Discover" title="Discover" />
\t\t\t\t\t<img src="{$machform_path}images/cards/diners.png" alt="Diners Club" title="Diners Club" />
\t\t\t\t</li>
\t\t\t\t<li id="li_credit_card" class="credit_card">
\t\t\t\t\t<label class="description">Credit Card <span class="required">*</span></label>
\t\t\t\t\t<div>
\t\t\t\t\t\t<span id="li_cc_span_1" class="left">
\t\t\t\t\t\t\t<input id="cc_first_name" class="element text large" value="" type="text" />
\t\t\t\t\t\t\t<label for="cc_first_name">First Name</label>
\t\t\t\t\t\t</span>
\t\t\t\t\t
\t\t\t\t\t\t<span id="li_cc_span_2" class="right">
\t\t\t\t\t\t\t<input id="cc_last_name" class="element text large" value="" type="text" />
\t\t\t\t\t\t\t<label for="cc_last_name">Last Name</label>
\t\t\t\t\t\t</span>

\t\t\t\t\t\t<span id="li_cc_span_3" class="left">
\t\t\t\t\t\t\t<input id="cc_number" class="element text large" value="" type="text" />
\t\t\t\t\t\t\t<label for="cc_number">Credit Card Number</label>
\t\t\t\t\t\t</span>
\t\t\t\t\t
\t\t\t\t\t\t<span id="li_cc_span_4" class="right">
\t\t\t\t\t\t\t<input id="cc_cvv" class="element text large" value="" type="text" />
\t\t\t\t\t\t\t<label for="cc_cvv">CVV</label>
\t\t\t\t\t\t</span>

\t\t\t\t\t\t<span id="li_cc_span_5" style="text-align: right">
\t\t\t\t\t\t\t<img id="cc_secure_icon" src="{$machform_path}images/icons/lock.png" alt="Secure" title="Secure" /> 
\t\t\t\t\t\t\t<label for="cc_expiry_month" style="display: inline">Expiration: </label>
\t\t\t\t\t\t\t<select class="element select" id="cc_expiry_month">
\t\t\t\t\t\t\t\t<option value="01">01 - January</option>
\t\t\t\t\t\t\t\t<option value="02">02 - February</option>
\t\t\t\t\t\t\t\t<option value="03">03 - March</option>
\t\t\t\t\t\t\t\t<option value="04">04 - April</option>
\t\t\t\t\t\t\t\t<option value="05">05 - May</option>
\t\t\t\t\t\t\t\t<option value="06">06 - June</option>
\t\t\t\t\t\t\t\t<option value="07">07 - July</option>
\t\t\t\t\t\t\t\t<option value="08">08 - August</option>
\t\t\t\t\t\t\t\t<option value="09">09 - September</option>
\t\t\t\t\t\t\t\t<option value="10">10 - October</option>
\t\t\t\t\t\t\t\t<option value="11">11 - November</option>
\t\t\t\t\t\t\t\t<option value="12">12 - December</option>
\t\t\t\t\t\t\t</select>
\t\t\t\t\t\t\t<select class="element select" id="cc_expiry_year">
\t\t\t\t\t\t\t\t{$year_dropdown_markup}
\t\t\t\t\t\t\t</select>
\t\t\t\t\t\t</span>
\t\t\t\t\t</div><p id="credit_card_error_message" class="error" style="display: none"></p>
\t\t\t\t</li>
\t\t\t\t<li id="li_2" class="section_break">
\t\t\t\t</li>
\t\t\t\t{$billing_address_markup}
\t\t\t\t{$shipping_address_markup}
\t\t\t\t<li id="li_buttons" class="buttons">
\t\t\t\t\t<input type="hidden" id="form_id" value="{$form_id}" />
\t\t\t\t    {$button_markup}
\t\t\t\t    <img id="mf_payment_loader_img" style="display: none" src="{$machform_path}images/loader_small_grey.gif" />
\t\t\t\t</li>
\t\t\t</ul>
\t\t</form>\t\t
\t\t<form id="form_payment_redirect" method="post" action="{$self_address}">
\t\t\t<input type="hidden" id="form_id_redirect" name="form_id_redirect" value="{$form_id}" />
\t\t</form>\t\t
\t</div>
</div>
EOT;
    } else {
        if ($integration_method == 'iframe') {
            $auto_height_js = <<<EOT
<script type="text/javascript" src="{$machform_path}js/jquery.ba-postmessage.min.js"></script>
<script type="text/javascript">
    \$(function(){
    \t\$.postMessage({mf_iframe_height: \$('body').outerHeight(true)}, '*', parent );
    });
</script>
EOT;
        }
        $form_markup = <<<EOT
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html {$embed_class} xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>{$form_name}</title>
<link rel="stylesheet" type="text/css" href="{$machform_path}{$css_dir}view.css" media="all" />
<link rel="stylesheet" type="text/css" href="{$machform_path}view.mobile.css" media="all" />
{$theme_css_link}
{$font_css_markup}
<script type="text/javascript" src="{$jquery_url}"></script>
<script type="text/javascript" src="{$machform_path}js/jquery-ui/ui/jquery.effects.core.js"></script>
<script type="text/javascript" src="{$machform_path}view.js"></script>
{$stripe_js}
{$auto_height_js}
</head>
<body id="main_body" class="no_guidelines" data-machformpath="{$machform_path}">
\t
\t<img id="top" src="{$machform_path}images/top.png" alt="" />
\t<div id="form_container" class="{$form_container_class}">
\t
\t\t<h1><a>MachForm</a></h1>
\t\t<form id="form_{$form_id}" class="appnitro" method="post" action="javascript:" data-highlightcolor="{$field_highlight_color}">
\t\t    <div class="form_description">
\t\t\t\t<h2>{$form_payment_title}</h2>
\t\t\t\t<p>{$form_payment_description}</p>
\t\t\t</div>
\t\t\t{$pagination_header}
\t\t\t
\t\t\t<ul class="payment_summary">
\t\t\t\t<li class="payment_summary_amount total_payment" data-basetotal="{$total_payment_amount}">
\t\t\t\t\t<span>
\t\t\t\t\t\t<h3>{$currency_symbol}<var>0</var></h3>
\t\t\t\t\t\t<h5>{$mf_lang['payment_total']}</h5>
\t\t\t\t\t</span>
\t\t\t\t</li>
\t\t\t\t<li class="payment_summary_list">
\t\t\t\t\t<ul class="payment_list_items">
\t\t\t\t\t\t{$payment_list_items_markup}
\t\t\t\t\t</ul>
\t\t\t\t</li>
\t\t\t\t{$payment_term_markup}
\t\t\t</ul>
\t\t\t<ul class="payment_detail_form">
\t\t\t\t<li id="error_message" style="display: none">
\t\t\t\t\t\t<h3 id="error_message_title">{$mf_lang['error_title']}</h3>
\t\t\t\t\t\t<p id="error_message_desc">{$mf_lang['error_desc']}</p>
\t\t\t\t</li>\t
\t\t\t\t<li id="li_accepted_cards">
\t\t\t\t\t<img src="{$machform_path}images/cards/visa.png" alt="Visa" title="Visa" />
\t\t\t\t\t<img src="{$machform_path}images/cards/mastercard.png" alt="MasterCard" title="MasterCard" />
\t\t\t\t\t<img src="{$machform_path}images/cards/amex.png" alt="American Express" title="American Express" />
\t\t\t\t\t<img src="{$machform_path}images/cards/jcb.png" alt="JCB" title="JCB" />
\t\t\t\t\t<img src="{$machform_path}images/cards/discover.png" alt="Discover" title="Discover" />
\t\t\t\t\t<img src="{$machform_path}images/cards/diners.png" alt="Diners Club" title="Diners Club" />
\t\t\t\t</li>
\t\t\t\t<li id="li_credit_card" class="credit_card">
\t\t\t\t\t<label class="description">Credit Card <span class="required">*</span></label>
\t\t\t\t\t<div>
\t\t\t\t\t\t<span id="li_cc_span_1" class="left">
\t\t\t\t\t\t\t<input id="cc_first_name" class="element text large" value="" type="text" />
\t\t\t\t\t\t\t<label for="cc_first_name">First Name</label>
\t\t\t\t\t\t</span>
\t\t\t\t\t
\t\t\t\t\t\t<span id="li_cc_span_2" class="right">
\t\t\t\t\t\t\t<input id="cc_last_name" class="element text large" value="" type="text" />
\t\t\t\t\t\t\t<label for="cc_last_name">Last Name</label>
\t\t\t\t\t\t</span>

\t\t\t\t\t\t<span id="li_cc_span_3" class="left">
\t\t\t\t\t\t\t<input id="cc_number" class="element text large" value="" type="text" />
\t\t\t\t\t\t\t<label for="cc_number">Credit Card Number</label>
\t\t\t\t\t\t</span>
\t\t\t\t\t
\t\t\t\t\t\t<span id="li_cc_span_4" class="right">
\t\t\t\t\t\t\t<input id="cc_cvv" class="element text large" value="" type="text" />
\t\t\t\t\t\t\t<label for="cc_cvv">CVV</label>
\t\t\t\t\t\t</span>

\t\t\t\t\t\t<span id="li_cc_span_5" style="text-align: right">
\t\t\t\t\t\t\t<img id="cc_secure_icon" src="{$machform_path}images/icons/lock.png" alt="Secure" title="Secure" /> 
\t\t\t\t\t\t\t<label for="cc_expiry_month" style="display: inline">Expiration: </label>
\t\t\t\t\t\t\t<select class="element select" id="cc_expiry_month">
\t\t\t\t\t\t\t\t<option value="01">01 - January</option>
\t\t\t\t\t\t\t\t<option value="02">02 - February</option>
\t\t\t\t\t\t\t\t<option value="03">03 - March</option>
\t\t\t\t\t\t\t\t<option value="04">04 - April</option>
\t\t\t\t\t\t\t\t<option value="05">05 - May</option>
\t\t\t\t\t\t\t\t<option value="06">06 - June</option>
\t\t\t\t\t\t\t\t<option value="07">07 - July</option>
\t\t\t\t\t\t\t\t<option value="08">08 - August</option>
\t\t\t\t\t\t\t\t<option value="09">09 - September</option>
\t\t\t\t\t\t\t\t<option value="10">10 - October</option>
\t\t\t\t\t\t\t\t<option value="11">11 - November</option>
\t\t\t\t\t\t\t\t<option value="12">12 - December</option>
\t\t\t\t\t\t\t</select>
\t\t\t\t\t\t\t<select class="element select" id="cc_expiry_year">
\t\t\t\t\t\t\t\t{$year_dropdown_markup}
\t\t\t\t\t\t\t</select>
\t\t\t\t\t\t</span>
\t\t\t\t\t</div><p id="credit_card_error_message" class="error" style="display: none"></p>
\t\t\t\t</li>
\t\t\t\t<li id="li_2" class="section_break">
\t\t\t\t</li>
\t\t\t\t{$billing_address_markup}
\t\t\t\t{$shipping_address_markup}
\t\t\t\t<li id="li_buttons" class="buttons">
\t\t\t\t\t<input type="hidden" id="form_id" value="{$form_id}" />
\t\t\t\t    {$button_markup}
\t\t\t\t    <img id="mf_payment_loader_img" style="display: none" src="{$machform_path}images/loader_small_grey.gif" />
\t\t\t\t</li>
\t\t\t</ul>
\t\t</form>\t\t
\t\t<form id="form_payment_redirect" method="post" action="{$self_address}">
\t\t\t<input type="hidden" id="form_id_redirect" name="form_id_redirect" value="{$form_id}" />
\t\t</form>\t
\t</div>
\t<img id="bottom" src="{$machform_path}images/bottom.png" alt="" />
\t</body>
</html>
EOT;
    }
    return $form_markup;
}
function mf_send_notification($dbh, $form_id, $entry_id, $to_emails, $email_param)
{
    global $mf_hook_emails;
    global $mf_lang;
    $from_name = $email_param['from_name'];
    $from_email = $email_param['from_email'];
    $subject = $email_param['subject'];
    $content = $email_param['content'];
    $as_plain_text = $email_param['as_plain_text'];
    //if set to 'true' the email content will be a simple plain text
    $target_is_admin = $email_param['target_is_admin'];
    //if set to 'false', the download link for uploaded file will be removed
    $check_hook_file = $email_param['check_hook_file'];
    //get settings first
    $mf_settings = mf_get_settings($dbh);
    //get data for the particular entry id
    if ($target_is_admin === false) {
        $options['strip_download_link'] = false;
        //as of v3, receipt email should display download link
    }
    $options['strip_checkbox_image'] = true;
    $options['machform_path'] = $email_param['machform_base_path'];
    //the path to machform
    $entry_details = mf_get_entry_details($dbh, $form_id, $entry_id, $options);
    //if the form has payment enabled, get the payment details
    //start getting payment details -----------------------
    $query = "select \r\n\t\t\t\t\t payment_enable_merchant,\r\n\t\t\t\t\t payment_merchant_type,\r\n\t\t\t\t\t payment_price_type,\r\n\t\t\t\t\t payment_price_amount,\r\n\t\t\t\t\t payment_currency,\r\n\t\t\t\t\t payment_ask_billing,\r\n\t\t\t\t\t payment_ask_shipping\r\n\t\t\t     from \r\n\t\t\t     \t " . MF_TABLE_PREFIX . "forms \r\n\t\t\t    where \r\n\t\t\t    \t form_id = ?";
    $params = array($form_id);
    $sth = mf_do_query($query, $params, $dbh);
    $row = mf_do_fetch_result($sth);
    if (!empty($row)) {
        $payment_enable_merchant = (int) $row['payment_enable_merchant'];
        if ($payment_enable_merchant < 1) {
            $payment_enable_merchant = 0;
        }
        $payment_price_amount = (double) $row['payment_price_amount'];
        $payment_merchant_type = $row['payment_merchant_type'];
        $payment_price_type = $row['payment_price_type'];
        $form_payment_currency = strtoupper($row['payment_currency']);
        $payment_ask_billing = (int) $row['payment_ask_billing'];
        $payment_ask_shipping = (int) $row['payment_ask_shipping'];
    }
    if (!empty($payment_enable_merchant)) {
        $query = "SELECT \r\n\t\t\t\t\t\t\t`payment_id`,\r\n\t\t\t\t\t\t\t date_format(payment_date,'%e %b %Y - %r') payment_date, \r\n\t\t\t\t\t\t\t`payment_status`, \r\n\t\t\t\t\t\t\t`payment_fullname`, \r\n\t\t\t\t\t\t\t`payment_amount`, \r\n\t\t\t\t\t\t\t`payment_currency`, \r\n\t\t\t\t\t\t\t`payment_test_mode`,\r\n\t\t\t\t\t\t\t`payment_merchant_type`, \r\n\t\t\t\t\t\t\t`status`, \r\n\t\t\t\t\t\t\t`billing_street`, \r\n\t\t\t\t\t\t\t`billing_city`, \r\n\t\t\t\t\t\t\t`billing_state`, \r\n\t\t\t\t\t\t\t`billing_zipcode`, \r\n\t\t\t\t\t\t\t`billing_country`, \r\n\t\t\t\t\t\t\t`same_shipping_address`, \r\n\t\t\t\t\t\t\t`shipping_street`, \r\n\t\t\t\t\t\t\t`shipping_city`, \r\n\t\t\t\t\t\t\t`shipping_state`, \r\n\t\t\t\t\t\t\t`shipping_zipcode`, \r\n\t\t\t\t\t\t\t`shipping_country`\r\n\t\t\t\t\t\tFROM\r\n\t\t\t\t\t\t\t" . MF_TABLE_PREFIX . "form_payments\r\n\t\t\t\t\t   WHERE\r\n\t\t\t\t\t   \t\tform_id = ? and record_id = ? and `status` = 1\r\n\t\t\t\t\tORDER BY\r\n\t\t\t\t\t\t\tpayment_date DESC\r\n\t\t\t\t\t   LIMIT 1";
        $params = array($form_id, $entry_id);
        $sth = mf_do_query($query, $params, $dbh);
        $row = mf_do_fetch_result($sth);
        $payment_id = $row['payment_id'];
        $payment_date = $row['payment_date'];
        $payment_status = $row['payment_status'];
        $payment_fullname = $row['payment_fullname'];
        $payment_amount = (double) $row['payment_amount'];
        $payment_currency = strtoupper($row['payment_currency']);
        $payment_test_mode = (int) $row['payment_test_mode'];
        $payment_merchant_type = $row['payment_merchant_type'];
        $billing_street = htmlspecialchars(trim($row['billing_street']));
        $billing_city = htmlspecialchars(trim($row['billing_city']));
        $billing_state = htmlspecialchars(trim($row['billing_state']));
        $billing_zipcode = htmlspecialchars(trim($row['billing_zipcode']));
        $billing_country = htmlspecialchars(trim($row['billing_country']));
        $same_shipping_address = (int) $row['same_shipping_address'];
        if (!empty($same_shipping_address)) {
            $shipping_street = $billing_street;
            $shipping_city = $billing_city;
            $shipping_state = $billing_state;
            $shipping_zipcode = $billing_zipcode;
            $shipping_country = $billing_country;
        } else {
            $shipping_street = htmlspecialchars(trim($row['shipping_street']));
            $shipping_city = htmlspecialchars(trim($row['shipping_city']));
            $shipping_state = htmlspecialchars(trim($row['shipping_state']));
            $shipping_zipcode = htmlspecialchars(trim($row['shipping_zipcode']));
            $shipping_country = htmlspecialchars(trim($row['shipping_country']));
        }
        if (!empty($billing_street) || !empty($billing_city) || !empty($billing_state) || !empty($billing_zipcode) || !empty($billing_country)) {
            $billing_address = "{$billing_street}<br />{$billing_city}, {$billing_state} {$billing_zipcode}<br />{$billing_country}";
        }
        if (!empty($shipping_street) || !empty($shipping_city) || !empty($shipping_state) || !empty($shipping_zipcode) || !empty($shipping_country)) {
            $shipping_address = "{$shipping_street}<br />{$shipping_city}, {$shipping_state} {$shipping_zipcode}<br />{$shipping_country}";
        }
        if (!empty($row)) {
            $payment_has_record = true;
        } else {
            //if the entry doesn't have any record within ap_form_payments table
            //we need to calculate the total amount
            $payment_has_record = false;
            $payment_status = "unpaid";
            if ($payment_price_type == 'variable') {
                $payment_amount = (double) mf_get_payment_total($dbh, $form_id, $entry_id, 0, 'live');
            } else {
                if ($payment_price_type == 'fixed') {
                    $payment_amount = $payment_price_amount;
                }
            }
            $payment_currency = $form_payment_currency;
        }
        if (!$as_plain_text) {
            switch ($payment_currency) {
                case 'USD':
                    $currency_symbol = '&#36;';
                    break;
                case 'EUR':
                    $currency_symbol = '&#8364;';
                    break;
                case 'GBP':
                    $currency_symbol = '&#163;';
                    break;
                case 'AUD':
                    $currency_symbol = '&#36;';
                    break;
                case 'CAD':
                    $currency_symbol = '&#36;';
                    break;
                case 'JPY':
                    $currency_symbol = '&#165;';
                    break;
                case 'THB':
                    $currency_symbol = '&#3647;';
                    break;
                case 'HUF':
                    $currency_symbol = '&#70;&#116;';
                    break;
                case 'CHF':
                    $currency_symbol = 'CHF';
                    break;
                case 'CZK':
                    $currency_symbol = '&#75;&#269;';
                    break;
                case 'SEK':
                    $currency_symbol = 'kr';
                    break;
                case 'DKK':
                    $currency_symbol = 'kr';
                    break;
                case 'NOK':
                    $currency_symbol = 'kr';
                    break;
                case 'PHP':
                    $currency_symbol = '&#36;';
                    break;
                case 'MYR':
                    $currency_symbol = 'RM';
                    break;
                case 'PLN':
                    $currency_symbol = '&#122;&#322;';
                    break;
                case 'BRL':
                    $currency_symbol = 'R&#36;';
                    break;
                case 'HKD':
                    $currency_symbol = '&#36;';
                    break;
                case 'MXN':
                    $currency_symbol = 'Mex&#36;';
                    break;
                case 'TWD':
                    $currency_symbol = 'NT&#36;';
                    break;
                case 'TRY':
                    $currency_symbol = 'TL';
                    break;
                case 'NZD':
                    $currency_symbol = '&#36;';
                    break;
                case 'SGD':
                    $currency_symbol = '&#36;';
                    break;
                default:
                    $currency_symbol = '';
                    break;
            }
        } else {
            switch ($payment_currency) {
                case 'USD':
                    $currency_symbol = '$';
                    break;
                case 'EUR':
                    $currency_symbol = '€';
                    break;
                case 'GBP':
                    $currency_symbol = '£';
                    break;
                case 'AUD':
                    $currency_symbol = '$';
                    break;
                case 'CAD':
                    $currency_symbol = '$';
                    break;
                case 'JPY':
                    $currency_symbol = '¥';
                    break;
                case 'THB':
                    $currency_symbol = '฿';
                    break;
                case 'HUF':
                    $currency_symbol = 'Ft';
                    break;
                case 'CHF':
                    $currency_symbol = 'CHF';
                    break;
                case 'CZK':
                    $currency_symbol = 'Kč';
                    break;
                case 'SEK':
                    $currency_symbol = 'kr';
                    break;
                case 'DKK':
                    $currency_symbol = 'kr';
                    break;
                case 'NOK':
                    $currency_symbol = 'kr';
                    break;
                case 'PHP':
                    $currency_symbol = '$';
                    break;
                case 'MYR':
                    $currency_symbol = 'RM';
                    break;
                case 'PLN':
                    $currency_symbol = 'zł';
                    break;
                case 'BRL':
                    $currency_symbol = 'R$';
                    break;
                case 'HKD':
                    $currency_symbol = '$';
                    break;
                case 'MXN':
                    $currency_symbol = '$';
                    break;
                case 'TWD':
                    $currency_symbol = '$';
                    break;
                case 'TRY':
                    $currency_symbol = 'TL';
                    break;
                case 'NZD':
                    $currency_symbol = '$';
                    break;
                case 'SGD':
                    $currency_symbol = '$';
                    break;
                default:
                    $currency_symbol = '';
                    break;
            }
        }
        $total_payment_amount = $currency_symbol . $payment_amount . ' ' . $payment_currency;
        $total_entry_details = count($entry_details);
        //blank row for separator
        if (!$as_plain_text) {
            $entry_details[$total_entry_details]['value'] = '&nbsp;&nbsp;';
            $entry_details[$total_entry_details]['label'] = '&nbsp;&nbsp;';
        } else {
            $entry_details[$total_entry_details]['value'] = '';
            $entry_details[$total_entry_details]['label'] = '';
        }
        //get total amount
        $total_entry_details++;
        $entry_details[$total_entry_details]['value'] = $total_payment_amount;
        $entry_details[$total_entry_details]['label'] = $mf_lang['payment_total'];
        //get payment status
        $total_entry_details++;
        if (!empty($payment_test_mode)) {
            $entry_details[$total_entry_details]['value'] = strtoupper($payment_status) . ' (TEST mode)';
        } else {
            $entry_details[$total_entry_details]['value'] = strtoupper($payment_status);
        }
        $entry_details[$total_entry_details]['label'] = $mf_lang['payment_status'];
        if ($payment_has_record) {
            //get payment id
            $total_entry_details++;
            $entry_details[$total_entry_details]['value'] = $payment_id;
            $entry_details[$total_entry_details]['label'] = $mf_lang['payment_id'];
            //get payment date
            $total_entry_details++;
            $entry_details[$total_entry_details]['value'] = $payment_date;
            $entry_details[$total_entry_details]['label'] = $mf_lang['payment_date'];
            //blank row for separator
            $total_entry_details++;
            if (!$as_plain_text) {
                $entry_details[$total_entry_details]['value'] = '&nbsp;&nbsp;';
                $entry_details[$total_entry_details]['label'] = '&nbsp;&nbsp;';
            } else {
                $entry_details[$total_entry_details]['value'] = '';
                $entry_details[$total_entry_details]['label'] = '';
            }
            //get full name
            $total_entry_details++;
            $entry_details[$total_entry_details]['value'] = htmlspecialchars($payment_fullname, ENT_QUOTES);
            $entry_details[$total_entry_details]['label'] = $mf_lang['payment_fullname'];
            //get billing address
            if (!empty($payment_ask_billing) && !empty($billing_address)) {
                $total_entry_details++;
                $entry_details[$total_entry_details]['value'] = $billing_address;
                $entry_details[$total_entry_details]['label'] = $mf_lang['payment_billing'];
            }
            //get shipping address
            if (!empty($payment_ask_shipping) && !empty($shipping_address)) {
                $total_entry_details++;
                $entry_details[$total_entry_details]['value'] = $shipping_address;
                $entry_details[$total_entry_details]['label'] = $mf_lang['payment_shipping'];
            }
        }
    }
    //end payment enable merchant
    //end getting payment details -----------------------
    //populate field values to template variables
    $i = 0;
    foreach ($entry_details as $data) {
        $template_variables[$i] = '{element_' . $data['element_id'] . '}';
        $template_values[$i] = $data['value'];
        if ($data['element_type'] == 'textarea' && !$as_plain_text) {
            $template_values[$i] = nl2br($data['value']);
        } elseif ($data['element_type'] == 'file') {
            if (!$as_plain_text) {
                $template_values[$i] = strip_tags($data['value'], '<a><br/>');
            } else {
                $template_values[$i] = strip_tags($data['value']);
                $template_values[$i] = str_replace('&nbsp;', "\n- ", $template_values[$i]);
            }
        } elseif ($data['element_type'] == 'signature') {
            //skip the signature, we will construct the value later below
            continue;
        } else {
            $template_values[$i] = $data['value'];
        }
        $i++;
    }
    $entry_values = mf_get_entry_values($dbh, $form_id, $entry_id);
    //get template variables for some complex fields (name and address)
    $query = "select \r\n\t\t\t\t\t\t element_id,\r\n\t\t\t\t\t\t element_type \r\n\t\t\t\t     from\r\n\t\t\t\t     \t `" . MF_TABLE_PREFIX . "form_elements` \r\n\t\t\t\t    where \r\n\t\t\t\t    \t form_id=? and \r\n\t\t\t\t    \t element_type != 'section' and \r\n\t\t\t\t    \t element_status=1 and\r\n\t\t\t\t    \t element_type in('simple_name','simple_name_wmiddle','name','name_wmiddle','address')\r\n\t\t\t\t order by \r\n\t\t\t\t \t\t element_position asc";
    $params = array($form_id);
    $sth = mf_do_query($query, $params, $dbh);
    while ($row = mf_do_fetch_result($sth)) {
        $element_id = $row['element_id'];
        $element_type = $row['element_type'];
        if ('simple_name' == $element_type) {
            $total_sub_field = 2;
        } else {
            if ('simple_name_wmiddle' == $element_type) {
                $total_sub_field = 3;
            } else {
                if ('name' == $element_type) {
                    $total_sub_field = 4;
                } else {
                    if ('name_wmiddle' == $element_type) {
                        $total_sub_field = 5;
                    } else {
                        if ('address' == $element_type) {
                            $total_sub_field = 6;
                        }
                    }
                }
            }
        }
        for ($j = 1; $j <= $total_sub_field; $j++) {
            $template_variables[$i] = '{element_' . $element_id . '_' . $j . '}';
            $template_values[$i] = $entry_values['element_' . $element_id . '_' . $j]['default_value'];
            $i++;
        }
    }
    //get entry timestamp
    $query = "select date_created,ip_address from `" . MF_TABLE_PREFIX . "form_{$form_id}` where id=?";
    $params = array($entry_id);
    $sth = mf_do_query($query, $params, $dbh);
    $row = mf_do_fetch_result($sth);
    $date_created = $row['date_created'];
    $ip_address = $row['ip_address'];
    //get form name
    $query = "select form_name\tfrom `" . MF_TABLE_PREFIX . "forms` where form_id=?";
    $params = array($form_id);
    $sth = mf_do_query($query, $params, $dbh);
    $row = mf_do_fetch_result($sth);
    $form_name = $row['form_name'];
    $template_variables[$i] = '{date_created}';
    $template_values[$i] = $date_created;
    $i++;
    $template_variables[$i] = '{ip_address}';
    $template_values[$i] = $ip_address;
    $i++;
    $template_variables[$i] = '{form_name}';
    $template_values[$i] = $form_name;
    $i++;
    $template_variables[$i] = '{entry_no}';
    $template_values[$i] = $entry_id;
    $i++;
    $template_variables[$i] = '{form_id}';
    $template_values[$i] = $form_id;
    //populate template variables for payment details
    if (!empty($total_payment_amount)) {
        $i++;
        $template_variables[$i] = '{total_amount}';
        $template_values[$i] = $total_payment_amount;
    }
    if (!empty($payment_status)) {
        $i++;
        $template_variables[$i] = '{payment_status}';
        if (!empty($payment_test_mode)) {
            $template_values[$i] = strtoupper($payment_status) . ' (TEST mode)';
        } else {
            $template_values[$i] = strtoupper($payment_status);
        }
    }
    if (!empty($payment_id)) {
        $i++;
        $template_variables[$i] = '{payment_id}';
        $template_values[$i] = $payment_id;
    }
    if (!empty($payment_date)) {
        $i++;
        $template_variables[$i] = '{payment_date}';
        $template_values[$i] = $payment_date;
    }
    if (!empty($payment_fullname)) {
        $i++;
        $template_variables[$i] = '{payment_fullname}';
        $template_values[$i] = $payment_fullname;
    }
    if (!empty($billing_address)) {
        if ($as_plain_text) {
            $billing_address = str_replace('<br />', "\n", $billing_address);
        }
        $i++;
        $template_variables[$i] = '{billing_address}';
        $template_values[$i] = $billing_address;
    }
    if (!empty($shipping_address)) {
        if ($as_plain_text) {
            $shipping_address = str_replace('<br />', "\n", $shipping_address);
        }
        $i++;
        $template_variables[$i] = '{shipping_address}';
        $template_values[$i] = $shipping_address;
    }
    //compose {entry_data} based on 'as_plain_text' preferences
    $email_body = '';
    $files_to_attach = array();
    if (!$as_plain_text) {
        //compose html format
        $email_body = '<table width="100%" border="0" cellspacing="0" cellpadding="0" style="font-family:Lucida Grande,Tahoma,Arial,Verdana,sans-serif;font-size:12px;text-align:left">' . "\n";
        $toggle = false;
        $j = 0;
        foreach ($entry_details as $data) {
            //0 should be displayed, empty string don't
            if ((empty($data['value']) || $data['value'] == '&nbsp;') && $data['value'] !== 0 && $data['value'] !== '0' && $data['element_type'] !== 'section') {
                continue;
            }
            //skip pagebreak
            if ($data['label'] == 'mf_page_break' && $data['value'] == 'mf_page_break') {
                continue;
            }
            if ($toggle) {
                $toggle = false;
                $row_style = 'style="background-color:#F3F7FB"';
            } else {
                $toggle = true;
                $row_style = '';
            }
            if ($data['element_type'] == 'textarea') {
                $data['value'] = nl2br($data['value']);
            } elseif ($data['element_type'] == 'file') {
                if ($target_is_admin === false) {
                    $data['value'] = strip_tags($data['value'], '<a><br/>');
                    $data['value'] = str_replace('&nbsp;', '', $data['value']);
                } else {
                    $data['value'] = strip_tags($data['value'], '<a><br/>');
                    $data['value'] = str_replace('&nbsp;', '', $data['value']);
                    //if there is file to be attached
                    if (!empty($data['filedata'])) {
                        foreach ($data['filedata'] as $file_info) {
                            $files_to_attach[$j]['filename_path'] = $file_info['filename_path'];
                            $files_to_attach[$j]['filename_value'] = $file_info['filename_value'];
                            $j++;
                        }
                    }
                }
            } elseif ($data['element_type'] == 'signature') {
                $element_id = $data['element_id'];
                $signature_hash = md5($data['value']);
                //encode the long query string for more readibility
                $q_string = base64_encode("form_id={$form_id}&id={$entry_id}&el=element_{$element_id}&hash={$signature_hash}");
                if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
                    $ssl_suffix = 's';
                } else {
                    $ssl_suffix = '';
                }
                if (!empty($email_param['machform_base_path'])) {
                    //if the form is called from advanced form code
                    $data['value'] = '<a href="' . $email_param['machform_base_path'] . 'signature.php?q=' . $q_string . '">View Signature</a>';
                } else {
                    $data['value'] = '<a href="http' . $ssl_suffix . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/signature.php?q=' . $q_string . '">View Signature</a>';
                }
                //construct template variables
                $template_variables[$i] = '{element_' . $data['element_id'] . '}';
                $template_values[$i] = $data['value'];
                $i++;
            }
            if ($data['element_type'] == 'section') {
                if (!empty($data['label']) && !empty($data['value']) && $data['value'] != '&nbsp;') {
                    $section_separator = '<br/>';
                } else {
                    $section_separator = '';
                }
                $section_break_content = '<span><strong>' . nl2br($data['label']) . '</strong></span>' . $section_separator . '<span>' . nl2br($data['value']) . '</span>';
                $email_body .= "<tr {$row_style}>\n";
                $email_body .= '<td width="100%" colspan="2" style="border-bottom:1px solid #DEDEDE;padding:5px 10px;">' . $section_break_content . '</td>' . "\n";
                $email_body .= '</tr>' . "\n";
            } else {
                $email_body .= "<tr {$row_style}>\n";
                $email_body .= '<td width="40%" style="border-bottom:1px solid #DEDEDE;padding:5px 10px;"><strong>' . $data['label'] . '</strong></td>' . "\n";
                $email_body .= '<td width="60%" style="border-bottom:1px solid #DEDEDE;padding:5px 10px;">' . $data['value'] . '</td>' . "\n";
                $email_body .= '</tr>' . "\n";
            }
            $i++;
        }
        $email_body .= "</table>\n";
    } else {
        $money_symbols = array('&#165;', '&#163;', '&#8364;', '&#3647;', '&#75;&#269;', '&#122;&#322;', '&#65020;');
        $money_plain = array('¥', '£', '€', '฿', 'Kč', 'zł', '﷼');
        //compose text format
        foreach ($entry_details as $data) {
            $data['value'] = htmlspecialchars_decode($data['value'], ENT_QUOTES);
            //0 should be displayed, empty string don't
            if ((empty($data['value']) || $data['value'] == '&nbsp;') && $data['value'] !== 0 && $data['value'] !== '0') {
                continue;
            }
            //skip pagebreak
            if ($data['label'] == 'mf_page_break' && $data['value'] == 'mf_page_break') {
                continue;
            }
            $data['value'] = str_replace('<br />', "\n", $data['value']);
            if ($data['element_type'] == 'textarea' || $data['element_type'] == 'matrix') {
                $data['value'] = trim($data['value'], "\n");
                $email_body .= "{$data['label']}: \n" . $data['value'] . "\n\n";
            } elseif ($data['element_type'] == 'section') {
                $data['value'] = trim($data['value'], "\n");
                $email_body .= "{$data['label']} \n" . $data['value'] . "\n\n";
            } elseif ($data['element_type'] == 'checkbox' || $data['element_type'] == 'address') {
                $email_body .= "{$data['label']}: \n" . $data['value'] . "\n\n";
            } elseif ($data['element_type'] == 'file') {
                $data['value'] = strip_tags($data['value']);
                $data['value'] = str_replace('&nbsp;', "\n- ", $data['value']);
                $email_body .= "{$data['label']}: {$data['value']}\n";
                //if there is file to be attached
                if (!empty($data['filedata'])) {
                    foreach ($data['filedata'] as $file_info) {
                        $files_to_attach[$j]['filename_path'] = $file_info['filename_path'];
                        $files_to_attach[$j]['filename_value'] = $file_info['filename_value'];
                        $j++;
                    }
                }
            } elseif ($data['element_type'] == 'money') {
                $data['value'] = str_replace($money_symbols, $money_plain, $data['value']);
                $email_body .= "{$data['label']}: {$data['value']} \n\n";
            } elseif ($data['element_type'] == 'url') {
                $data['value'] = strip_tags($data['value']);
                $email_body .= "{$data['label']}: {$data['value']} \n\n";
            } elseif ($data['element_type'] == 'signature') {
                $element_id = $data['element_id'];
                $signature_hash = md5($data['value']);
                //encode the long query string for more readibility
                $q_string = base64_encode("form_id={$form_id}&id={$entry_id}&el=element_{$element_id}&hash={$signature_hash}");
                if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
                    $ssl_suffix = 's';
                } else {
                    $ssl_suffix = '';
                }
                if (!empty($email_param['machform_base_path'])) {
                    //if the form is called from advanced form code
                    $data['value'] = $email_param['machform_base_path'] . 'signature.php?q=' . $q_string;
                } else {
                    $data['value'] = 'http' . $ssl_suffix . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/signature.php?q=' . $q_string;
                }
                $email_body .= "{$data['label']}: {$data['value']} \n\n";
                //construct template variables
                $template_variables[$i] = '{element_' . $data['element_id'] . '}';
                $template_values[$i] = $data['value'];
                $i++;
            } else {
                $email_body .= "{$data['label']}: {$data['value']} \n\n";
            }
        }
    }
    $i = count($template_variables);
    $template_variables[$i] = '{entry_data}';
    $template_values[$i] = $email_body;
    //create the mail transport
    if (!empty($mf_settings['smtp_enable'])) {
        $s_transport = Swift_SmtpTransport::newInstance($mf_settings['smtp_host'], $mf_settings['smtp_port']);
        if (!empty($mf_settings['smtp_secure'])) {
            $s_transport->setEncryption('tls');
        }
        if (!empty($mf_settings['smtp_auth'])) {
            $s_transport->setUsername($mf_settings['smtp_username']);
            $s_transport->setPassword($mf_settings['smtp_password']);
        }
    } else {
        $s_transport = Swift_MailTransport::newInstance();
        //use PHP mail() transport
    }
    //create mailer instance
    $s_mailer = Swift_Mailer::newInstance($s_transport);
    if (file_exists($mf_settings['upload_dir'] . "/form_{$form_id}/files") && is_writable($mf_settings['upload_dir'] . "/form_{$form_id}/files")) {
        Swift_Preferences::getInstance()->setCacheType('disk')->setTempDir($mf_settings['upload_dir'] . "/form_{$form_id}/files");
    } else {
        Swift_Preferences::getInstance()->setCacheType('array');
    }
    //create the message
    //parse from_name template
    if (!empty($from_name)) {
        $from_name = str_replace($template_variables, $template_values, $from_name);
        $from_name = str_replace('&nbsp;', '', $from_name);
        //decode any html entity
        $from_name = html_entity_decode($from_name, ENT_QUOTES);
        if (empty($from_name)) {
            if (!empty($mf_settings['default_from_name'])) {
                $from_name = $mf_settings['default_from_name'];
            } else {
                $from_name = 'MachForm';
            }
        }
    } else {
        if (!empty($mf_settings['default_from_name'])) {
            $from_name = $mf_settings['default_from_name'];
        } else {
            $from_name = 'MachForm';
        }
    }
    //parse from_email_address template
    if (!empty($from_email)) {
        $from_email = str_replace($template_variables, $template_values, $from_email);
        if (empty($from_email)) {
            if (!empty($mf_settings['default_from_email'])) {
                $from_email = $mf_settings['default_from_email'];
            } else {
                $domain = str_replace('www.', '', $_SERVER['SERVER_NAME']);
                $from_email = "no-reply@{$domain}";
            }
        }
    } else {
        if (!empty($mf_settings['default_from_email'])) {
            $from_email = $mf_settings['default_from_email'];
        } else {
            $domain = str_replace('www.', '', $_SERVER['SERVER_NAME']);
            $from_email = "no-reply@{$domain}";
        }
    }
    //parse subject template
    if (!empty($subject)) {
        $subject = str_replace($template_variables, $template_values, $subject);
        $subject = str_replace('&nbsp;', '', $subject);
    } else {
        if ($target_is_admin) {
            $subject = utf8_encode("{$form_name} [#{$entry_id}]");
        } else {
            $subject = utf8_encode("{$form_name} - Receipt");
        }
    }
    //decode any html entity
    $subject = html_entity_decode($subject, ENT_QUOTES);
    //parse content template
    $email_content = str_replace($template_variables, $template_values, $content);
    if (!$as_plain_text) {
        //html type
        //add footer
        if (empty($mf_settings['disable_machform_link'])) {
            $email_content .= "<br /><br /><br /><br /><br /><b style=\"font-family:Lucida Grande,Tahoma,Arial,Verdana,sans-serif;font-size:12px\">Powered by <a href=\"http://www.appnitro.com\">MachForm</a></b>";
        }
        //enclose with container div
        $email_content = '<div style="font-family:Lucida Grande,Tahoma,Arial,Verdana,sans-serif;font-size:12px">' . $email_content . '</div>';
    }
    $to_emails = str_replace('&nbsp;', '', str_replace($template_variables, $template_values, $to_emails));
    if (!empty($to_emails)) {
        $email_address = explode(',', $to_emails);
    }
    if (!empty($email_address)) {
        if (!$as_plain_text) {
            $email_content_type = 'text/html';
        } else {
            $email_content_type = 'text/plain';
        }
        //check for hook file (currently being used to set the destination email based on dropdown/radio button/checkboxes selection)
        if ($check_hook_file === true) {
            $hook_emails = $mf_hook_emails[$form_id];
            if (!empty($hook_emails)) {
                $hook_element_id = $hook_emails['element_id'];
                //get the field type of this element_id
                $query = "select element_type from " . MF_TABLE_PREFIX . "form_elements where form_id=? and element_id=? and element_status=1";
                $params = array($form_id, $hook_element_id);
                $sth = mf_do_query($query, $params, $dbh);
                $row = mf_do_fetch_result($sth);
                if ($row['element_type'] == 'checkbox') {
                    //get all selected checkboxes
                    $query = "select \r\n\t\t\t\t\t\t\t\t\t\toption_id,\r\n\t\t\t\t\t\t\t\t\t\t`option` option_title \r\n\t\t\t\t\t\t\t\t\tfrom \r\n\t\t\t\t\t\t\t\t\t\t" . MF_TABLE_PREFIX . "element_options \r\n\t\t\t\t\t\t\t\t   where \r\n\t\t\t\t\t\t\t\t   \t\tform_id=? and element_id=? and live=1 \r\n\t\t\t\t\t\t\t\torder by \r\n\t\t\t\t\t\t\t\t\t\toption_id asc";
                    $params = array($form_id, $hook_element_id);
                    $sth = mf_do_query($query, $params, $dbh);
                    $checkbox_element_names_array = array();
                    while ($row = mf_do_fetch_result($sth)) {
                        $checkbox_hook_lookup[$row['option_id']] = $row['option_title'];
                        $checkbox_element_names_array[] = 'element_' . $hook_element_id . '_' . $row['option_id'];
                    }
                    $checkbox_element_names_joined = implode(',', $checkbox_element_names_array);
                    $query = "select {$checkbox_element_names_joined} from " . MF_TABLE_PREFIX . "form_{$form_id} where `id`=?";
                    $params = array($entry_id);
                    $sth = mf_do_query($query, $params, $dbh);
                    $row = mf_do_fetch_result($sth);
                    $selected_checkbox_array = array();
                    foreach ($checkbox_hook_lookup as $option_id => $option_title) {
                        if (!empty($row['element_' . $hook_element_id . '_' . $option_id])) {
                            $selected_checkbox_array[] = $option_title;
                        }
                    }
                    if (!empty($selected_checkbox_array)) {
                        $email_address = array();
                        foreach ($selected_checkbox_array as $selected_option_title) {
                            $selected_hook_email = $mf_hook_emails[$form_id][$selected_option_title];
                            if (!empty($selected_hook_email)) {
                                $temp_email_address = explode(",", $selected_hook_email);
                            }
                            $email_address = array_merge($email_address, (array) $temp_email_address);
                        }
                    }
                } else {
                    $query = "select \r\n\t\t\t\t\t\t\t\t\t\tB.`option` selected_value \r\n\t\t\t\t\t\t\t\t\tfrom \r\n\t\t\t\t\t\t\t\t\t\t" . MF_TABLE_PREFIX . "form_{$form_id} A left join " . MF_TABLE_PREFIX . "element_options B \r\n\t\t\t\t\t\t\t\t\t  on \r\n\t\t\t\t\t\t\t\t\t    B.form_id=? and A.element_{$hook_element_id}=B.option_id and B.live=1 and B.element_id=?\r\n\t\t\t\t\t\t\t\t\twhere \r\n\t\t\t\t\t\t\t\t\t\tA.`id`=?";
                    $params = array($form_id, $hook_element_id, $entry_id);
                    $sth = mf_do_query($query, $params, $dbh);
                    $row = mf_do_fetch_result($sth);
                    $selected_value = $row['selected_value'];
                    $selected_hook_email = $mf_hook_emails[$form_id][$selected_value];
                    if (!empty($selected_hook_email)) {
                        $email_address = explode(",", $selected_hook_email);
                    }
                }
            }
        }
        array_walk($email_address, 'mf_trim_value');
        $s_message = Swift_Message::newInstance()->setCharset('utf-8')->setMaxLineLength(1000)->setSubject($subject)->setFrom(array($from_email => $from_name))->setSender($from_email)->setReturnPath($from_email)->setTo($email_address)->setBody($email_content, $email_content_type);
        //attach files, if any
        if (!empty($files_to_attach)) {
            foreach ($files_to_attach as $file_data) {
                $s_message->attach(Swift_Attachment::fromPath($file_data['filename_path'])->setFilename($file_data['filename_value']));
            }
        }
        //send the message
        $send_result = $s_mailer->send($s_message);
        if (empty($send_result)) {
            echo "Error sending email!";
        }
    }
}