function myConsultantTotalSales()
{
    //get loged user's key
    $key = get_current_user_key();
    //Total Users on My left leg
    $leftLegUsers = totalLeftLegUsers($key);
    //paid users on my left leg
    $rightLegUsers = totalRightLegUsers($key);
    $totalConsultant = $leftLegUsers + $rightLegUsers;
    //show total users on left leg
    $totalUsersSales = totalSales($key);
    //_e("<pre>");print_r($totalUsersSales);exit;
    ?>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load('visualization', '1', {packages: ['table']});
    </script>
    <script type="text/javascript">
        var visualization;
        var data;
        var options = {'showRowNumber': true};
        function drawVisualization() {
            // Create and populate the data table.
            var dataAsJson =
                    {cols: [
                            {id: 'A', label: '<?php 
    echo _e("User Name", "binary-mlm-pro");
    ?>
', type: 'string'},
                            {id: 'B', label: '<?php 
    echo _e("User Key", "binary-mlm-pro");
    ?>
 ', type: 'string'},
                            {id: 'C', label: '<?php 
    echo _e("Sponsor", "binary-mlm-pro");
    ?>
', type: 'string'},
                            {id: 'D', label: '<?php 
    echo _e("Placement", "binary-mlm-pro");
    ?>
', type: 'string'},
                            {id: 'E', label: '<?php 
    echo _e("Status", "binary-mlm-pro");
    ?>
', type: 'string'}],
                        rows: [
    <?php 
    foreach ($totalUsersSales as $details) {
        foreach ($details as $row) {
            ?>
                                    {c: [{v: '<?php 
            echo $row['username'];
            ?>
'}, {v: '<?php 
            echo $row['user_key'];
            ?>
'}, {v: '<?php 
            echo $row['sponsor_key'];
            ?>
'}, {v: '<?php 
            echo $row['leg'];
            ?>
'}, {v: '<?php 
            echo $row['payment_status'];
            ?>
'}]},
            <?php 
        }
    }
    ?>
                        ]};
            data = new google.visualization.DataTable(dataAsJson);
            // Set paging configuration options
            // Note: these options are changed by the UI controls in the example.
            options['page'] = 'enable';
            options['pageSize'] = 10;
            options['pagingSymbols'] = {prev: 'prev', next: 'next'};
            options['pagingButtonsConfiguration'] = 'auto';
            //options['allowHtml'] = true;
            //data.sort({column:1, desc: false});
            // Create and draw the visualization.
            visualization = new google.visualization.Table(document.getElementById('table'));
            draw();
        }
        function draw() {
            visualization.draw(data, options);
        }
        google.setOnLoadCallback(drawVisualization);
        // sets the number of pages according to the user selection.
        function setNumberOfPages(value) {
            if (value) {
                options['pageSize'] = parseInt(value, 10);
                options['page'] = 'enable';
            } else {
                options['pageSize'] = null;
                options['page'] = null;
            }
            draw();
        }
        // Sets custom paging symbols "Prev"/"Next"
        function setCustomPagingButtons(toSet) {
            options['pagingSymbols'] = toSet ? {next: 'next', prev: 'prev'} : null;
            draw();
        }
        function setPagingButtonsConfiguration(value) {
            options['pagingButtonsConfiguration'] = value;
            draw();
        }
    </script>
    <!--va-matter-->
    <div class="va-matter">
        <!--va-matterbox-->
        <div class="va-matterbox">
            <!--va-headname-->
            <div class="va-headname"><?php 
    _e('My Consultants Details', 'binary-mlm-pro');
    ?>
</div>
            <!--/va-headname-->
            <div class="va-admin-leg-details">
                <!--va-admin-mid-->
                <div class="paging">
                    <form action="">
                        <div class="left-side">
                            <?php 
    _e('Display Number of Rows', 'binary-mlm-pro');
    ?>
 : &nbsp; 
                        </div>
                        <div class="right-side">
                            <select style="font-size: 12px" onchange="setNumberOfPages(this.value)">
                                <option value="5">5</option>
                                <option selected="selected" value="10">10</option>
                                <option value="20">20</option>
                                <option  value="50">50</option>
                                <option value="100">100</option>
                                <option value="500">500</option>
                                <option value="">All</option>
                            </select>
                        </div>	
                    </form>
                    <div class="right-members">
                        <?php 
    _e('Consultants', 'binary-mlm-pro');
    ?>
: &nbsp; <?php 
    _e('Left', 'binary-mlm-pro');
    ?>
: <strong><?php 
    echo $leftLegUsers;
    ?>
</strong>&nbsp;<?php 
    _e('Right', 'binary-mlm-pro');
    ?>
: <strong><?php 
    echo $rightLegUsers;
    ?>
</strong>
                        &nbsp; <?php 
    _e('Total', 'binary-mlm-pro');
    ?>
: <strong><?php 
    echo $totalConsultant;
    ?>
</strong>
                    </div>
                    <div class="va-clear"></div>
                </div>
                <div id="table"></div>
                <div class="va-clear"></div>
            </div>		
        </div>
    </div>	
    <?php 
}
function register_user_html_page()
{
    global $wpdb, $current_user;
    $user_id = $current_user->ID;
    $table_prefix = mlm_core_get_table_prefix();
    $error = '';
    $chk = 'error';
    include_once ABSPATH . '/wp-admin/includes/plugin.php';
    if (!empty($_GET['sp_name'])) {
        $sp_name = $wpdb->get_var("select username from {$table_prefix}mlm_users where username='******'sp_name'] . "'");
        if ($sp_name) {
            ?>
            <script type='text/javascript'>
                $.cookie('sp_name', '<?php 
            echo $sp_name;
            ?>
', {path: '/'});
            </script>
            <?php 
        }
    } else {
        if (!empty($_REQUEST['sp'])) {
            $sp_name = getusernamebykey($_REQUEST['sp']);
            if ($sp_name) {
                ?>
            <script type='text/javascript'>
                $.cookie('sp_name', '<?php 
                echo $sp_name;
                ?>
', {path: '/'});
            </script>
            <?php 
            }
        } else {
            $sp_name = empty($_COOKIE["sp_name"]) ? '' : $_COOKIE["sp_name"];
        }
    }
    get_currentuserinfo();
    $mlm_general_settings = get_option('wp_mlm_general_settings');
    if (is_user_logged_in()) {
        $sponsor_name = $current_user->user_login;
        $readonly_sponsor = 'readonly';
    } else {
        if (isset($_REQUEST['sp']) && $_REQUEST['sp'] != '') {
            $sponsorName = getusernamebykey($_REQUEST['sp']);
            if (isset($sponsorName) && $sponsorName != '') {
                $readonly_sponsor = 'readonly';
                $sponsor_name = $sponsorName;
            } else {
                redirectPage(home_url(), array());
                exit;
            }
        } else {
            if (!empty($_REQUEST['sp_name'])) {
                $sponsorName = $_REQUEST['sp_name'];
                if (!empty($sponsorName)) {
                    $readonly_sponsor = 'readonly';
                    $sponsor_name = $sponsorName;
                } else {
                    redirectPage(home_url(), array());
                    exit;
                }
            } else {
                $readonly_sponsor = '';
            }
        }
    }
    /* script for auto insert users================================================ */
    if ($_SERVER['HTTP_HOST'] == '192.168.100.100') {
        echo '<form name="form1"action="" method="post">
      <input type="number" min="0" max="99" name="id"/>
      <input type="number" min="0" max="1" name="epin"/>
      <input type="number" min="0" max="1" name="leg"/>
      <input type="submit"/></form>';
        $epinstatus = isset($_POST['epin']) ? $_POST['epin'] : '';
        if ($epinstatus != '') {
            $epin_no = $wpdb->get_var("select epin_no from {$table_prefix}mlm_epins where  point_status='{$epinstatus}' AND status=0 limit 1 ");
        }
        if (isset($_POST['id'])) {
            $z = $_POST['id'];
            $_POST = array('firstname' => 'binary' . $z, 'lastname' => 'binary' . $z, 'username' => 'binary' . $z, 'password' => 'binary' . $z, 'confirm_password' => 'binary' . $z, 'email' => 'binary' . $z . '@gmail.com', 'confirm_email' => 'binary' . $z . '@gmail.com', 'sponsor' => !empty($sponsor_name) ? $sponsor_name : '', 'submit' => 'submit', 'leg' => $_POST['leg'], 'epin' => $epin_no, 'paypal_id' => 'binary' . $z . '@gmail.com');
        }
        //'epin'=>!empty($epin_no)?$epin_no:'',
        //echo "<pre>"; print_r($_SERVER); echo "</pre>";
    }
    /* ===========================================================Close Auto Insert. */
    //most outer if condition
    if (isset($_POST['submit'])) {
        $firstname = sanitize_text_field($_POST['firstname']);
        $lastname = sanitize_text_field($_POST['lastname']);
        $username = sanitize_text_field($_POST['username']);
        $epin = sanitize_text_field(isset($_POST['epin']) ? $_POST['epin'] : '');
        $sponsor = sanitize_text_field($_POST['sponsor']);
        $password = sanitize_text_field($_POST['password']);
        $confirm_pass = sanitize_text_field($_POST['confirm_password']);
        $email = sanitize_text_field($_POST['email']);
        $confirm_email = sanitize_text_field($_POST['confirm_email']);
        $invalid_usernames = array('admin');
        $username = sanitize_user($username);
        if (!validate_username($username) || in_array($username, $invalid_usernames)) {
            $error .= "\n Username is invalid.";
        }
        if (username_exists($username)) {
            $error .= "\n Username already exists.";
        }
        if (empty($sponsor)) {
            $sponsor = $wpdb->get_var("select `username` FROM {$table_prefix}mlm_users order by id asc limit 1");
        }
        if (!empty($epin) && epin_exists($epin)) {
            $error .= "\n ePin already issued or wrong ePin.";
        }
        if (!empty($mlm_general_settings['sol_payment']) && empty($epin)) {
            $error .= "\n Please enter your ePin.";
        } else {
            if (empty($_POST['epin_value']) && empty($epin)) {
                $error .= "\n Please either enter the ePin or select the Product.";
            }
        }
        if (checkInputField($password)) {
            $error .= "\n Please enter your password.";
        }
        if (confirmPassword($password, $confirm_pass)) {
            $error .= "\n Please confirm your password.";
        }
        if (checkInputField($sponsor)) {
            $error .= "\n Please enter your sponsor name.";
        }
        if (checkInputField($firstname)) {
            $error .= "\n Please enter your first name.";
        }
        if (checkInputField($lastname)) {
            $error .= "\n Please enter your last name.";
        }
        if (!is_email($email)) {
            $error .= "\n E-mail address is invalid.";
        }
        if (email_exists($email)) {
            $error .= "\n E-mail address is already in use.";
        }
        if (confirmEmail($email, $confirm_email)) {
            $error .= "\n Please confirm your email address.";
        }
        include_once ABSPATH . '/wp-admin/includes/plugin.php';
        if (is_plugin_active('mlm-paypal-mass-pay/load-data.php')) {
            $paypalId = sanitize_text_field($_POST['paypal_id']);
            if (checkInputField($paypalId)) {
                $error .= "\n Please enter your Paypal id.";
            }
        }
        $sql = "SELECT COUNT(*) num, `user_key` FROM {$table_prefix}mlm_users WHERE `username` = '" . $sponsor . "'";
        $intro = $wpdb->get_row($sql);
        if (isset($_GET['l']) && $_GET['l'] != '') {
            $leg = $_GET['l'];
        } else {
            @($leg = $_POST['leg']);
        }
        if (isset($leg) && $leg != '0') {
            if ($leg != '1') {
                $error .= "\n You have enter a wrong placement.";
            }
        }
        //generate random numeric key for new user registration
        $user_key = generateKey();
        //if generated key is already exist in the DB then again re-generate key
        do {
            $check = $wpdb->get_var("SELECT COUNT(*) ck FROM {$table_prefix}mlm_users WHERE `user_key` = '" . $user_key . "'");
            $flag = 1;
            if ($check == 1) {
                $user_key = generateKey();
                $flag = 0;
            }
        } while ($flag == 0);
        //check parent key exist or not
        if (isset($_GET['k']) && $_GET['k'] != '') {
            if (!checkKey($_GET['k'])) {
                $error .= "\n Parent key does't exist.";
            }
            // check if the user can be added at the current position
            $checkallow = checkallowed($_GET['k'], $leg);
            if ($checkallow >= 1) {
                $error .= "\n You have enter a wrong placement.";
            }
        }
        if (!isset($leg)) {
            $key = $wpdb->get_var("SELECT user_key FROM {$table_prefix}mlm_users WHERE user_id = '{$user_id}'");
            $l = totalLeftLegUsers($key);
            $r = totalRightLegUsers($key);
            if ($l < $r) {
                $leg = '0';
            } else {
                $leg = '1';
            }
        }
        // outer if condition
        if (empty($error)) {
            // inner if condition
            if ($intro->num == 1) {
                $sponsor = $intro->user_key;
                $sponsor1 = $sponsor;
                //find parent key
                if (isset($_GET['k']) && $_GET['k'] != '') {
                    $parent_key = $_GET['k'];
                } else {
                    $readonly_sponsor = '';
                    do {
                        $sql = "SELECT `user_key` FROM {$table_prefix}mlm_users WHERE parent_key = '" . $sponsor1 . "' AND \n\t\t\t\tleg = '" . $leg . "' AND banned = '0'";
                        $spon = $wpdb->get_var($sql);
                        $num = $wpdb->num_rows;
                        if ($num) {
                            $sponsor1 = $spon;
                        }
                    } while ($num == 1);
                    $parent_key = $sponsor1;
                }
                $user = array('user_login' => $username, 'user_pass' => $password, 'first_name' => $firstname, 'last_name' => $lastname, 'user_email' => $email, 'user_registered' => current_time('mysql'), 'role' => 'mlm_user');
                // return the wp_users table inserted user's ID
                $user_id = wp_insert_user($user);
                /* Send e-mail to admin and new user  */
                wp_new_user_notification($user_id, $password);
                $pc = isset($mlm_general_settings['product_price']) ? $mlm_general_settings['product_price'] : '0';
                //insert the data into fa_user table
                if (!empty($epin)) {
                    $pointResult = $wpdb->get_row("select p_id,point_status from {$table_prefix}mlm_epins where epin_no = '{$epin}'");
                    $pointStatus = $pointResult->point_status;
                    $productPrice = $wpdb->get_var("SELECT product_price FROM {$table_prefix}mlm_product_price WHERE p_id = '" . $pointResult->p_id . "'");
                    // to epin point status 1
                    if ($pointStatus[0] == '1') {
                        $paymentStatus = '1';
                        $payment_date = current_time('mysql');
                    } else {
                        if ($pointStatus[0] == '0') {
                            $paymentStatus = '2';
                            $payment_date = current_time('mysql');
                        }
                    }
                } else {
                    if (!empty($_POST['epin_value'])) {
                        $productPrice = $wpdb->get_var("SELECT product_price FROM {$table_prefix}mlm_product_price WHERE p_id = '" . $_POST['epin_value'] . "'");
                        $paymentStatus = '0';
                        $payment_date = '0000-00-00 00:00:00';
                    } else {
                        // to non epin
                        $paymentStatus = '0';
                        $payment_date = '0000-00-00 00:00:00';
                    }
                }
                $insert = "INSERT INTO {$table_prefix}mlm_users\n\t\t\t\t\t\t   (\n\t\t\t\t\t\t\t\tuser_id, username, user_key, parent_key, sponsor_key, leg,payment_date,payment_status,product_price\n\t\t\t\t\t\t\t) \n\t\t\t\t\t\t\tVALUES\n\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t'" . $user_id . "','" . $username . "', '" . $user_key . "', '" . $parent_key . "', '" . $sponsor . "', '" . $leg . "','" . $payment_date . "','" . $paymentStatus . "','" . $productPrice . "'\n\t\t\t\t\t\t\t)";
                // if all data successfully inserted
                if ($wpdb->query($insert)) {
                    //begin most inner if condition
                    //entry on Left and Right Leg tables
                    if ($leg == 0) {
                        $insert = "INSERT INTO {$table_prefix}mlm_leftleg set  pkey='" . $parent_key . "',ukey='" . $user_key . "'";
                        $insert = $wpdb->query($insert);
                        if ($u = get_option('network_mail', true) == 1) {
                        }
                    } else {
                        if ($leg == 1) {
                            $insert = "INSERT INTO {$table_prefix}mlm_rightleg set pkey='" . $parent_key . "',ukey='" . $user_key . "'";
                            $insert = $wpdb->query($insert);
                            if ($u = get_option('network_mail', true) == 1) {
                            }
                        }
                    }
                    SendMailToAll($user_key, $parent_key, $sponsor);
                    //begin while loop
                    while ($parent_key != '0') {
                        $query = "SELECT COUNT(*) num, parent_key, leg \n\t\t\t\t\t\t\t\t  FROM {$table_prefix}mlm_users \n\t\t\t\t\t\t\t\t  WHERE user_key = '" . $parent_key . "'\n\t\t\t\t\t\t\t\t  AND banned = '0'";
                        $result = $wpdb->get_row($query);
                        if ($result->num == 1) {
                            if ($result->parent_key != '0') {
                                if ($result->leg == 1) {
                                    $tbright = "INSERT INTO {$table_prefix}mlm_rightleg set pkey='" . $result->parent_key . "',ukey='" . $user_key . "' ";
                                    $tbright = $wpdb->query($tbright);
                                    if ($u = get_option('network_mail', true) == 1) {
                                    }
                                } else {
                                    $tbleft = "INSERT INTO {$table_prefix}mlm_leftleg set pkey='" . $result->parent_key . "',ukey='" . $user_key . "' ";
                                    $tbleft = $wpdb->query($tbleft);
                                    if ($u = get_option('network_mail', true) == 1) {
                                    }
                                }
                            }
                            $parent_key = $result->parent_key;
                        } else {
                            $parent_key = '0';
                        }
                    }
                    //end while loop
                    if (isset($epin) && !empty($epin)) {
                        $sql = "update {$table_prefix}mlm_epins set user_key='{$user_key}', date_used='" . current_time('mysql') . "', status=1 where epin_no ='{$epin}' ";
                        $wpdb->query($sql);
                    }
                    if ($paymentStatus == 1) {
                        insert_refferal_commision($user_id);
                    }
                    if (is_plugin_active('mlm-paypal-mass-pay/load-data.php')) {
                        update_user_meta($user_id, 'mlm_user_paypalid', $paypalId, FALSE);
                    }
                    $chk = '';
                    $msg = "<span style='color:green;'>Congratulations! You have successfully registered in the system.</span>";
                }
                //end most inner if condition
            } else {
                $error = "\n Sponsor does not exist in the system.";
            }
        }
        //end outer if condition
    }
    //end most outer if condition
    //if any error occoured
    if (!empty($error)) {
        $error = nl2br($error);
    }
    if ($chk != '') {
        include 'js-validation-file.html';
        ?>

        <?php 
        $user_roles = $current_user->roles;
        $user_role = array_shift($user_roles);
        $general_setting = get_option('wp_mlm_general_settings');
        if (is_user_logged_in()) {
            if (!empty($general_setting['wp_reg']) && !empty($general_setting['reg_url']) && $user_role != 'mlm_user') {
                echo "<script>window.location ='" . site_url() . '/' . $general_setting['reg_url'] . "'</script>";
            }
        } else {
            if (!empty($general_setting['wp_reg']) && !empty($general_setting['reg_url'])) {
                echo "<script>window.location ='" . site_url() . '/' . $general_setting['reg_url'] . "'</script>";
            }
        }
        ?>

        <span style='color:red;'><?php 
        echo $error;
        ?>
</span>
        <table border="0" cellpadding="0" cellspacing="0" width="100%">
            <form name="frm" method="post" action="" onSubmit="return formValidation();">
                <tr>
                    <td><?php 
        _e('Create Username', 'binary-mlm-pro');
        ?>
<span style="color:red;">*</span> :</td>
                    <td><input type="text" name="username" id="username" value="<?php 
        if (!empty($_POST['username'])) {
            _e(htmlentities($_POST['username']));
        }
        ?>
" maxlength="20" size="37" onBlur="checkUserNameAvailability(this.value);"><br /><div id="check_user"></div></td>
                </tr>
                <?php 
        $mlm_general_settings = get_option('wp_mlm_general_settings');
        if (!empty($mlm_general_settings['ePin_activate']) && !empty($mlm_general_settings['sol_payment'])) {
            ?>
                    <tr><td colspan="2">&nbsp;</td></tr>
                    <tr>
                        <td><?php 
            _e('Enter ePin', 'binary-mlm-pro');
            ?>
<span style="color:red;">*</span> :</td>
                        <td><input type="text" name="epin" id="epin" value="<?php 
            if (!empty($_POST['epin'])) {
                _e(htmlentities($_POST['epin']));
            }
            ?>
" maxlength="20" size="37" onBlur="checkePinAvailability(this.value);"><br /><div id="check_epin"></div></td>
                    </tr>
                <?php 
        } else {
            if (!empty($mlm_general_settings['ePin_activate'])) {
                ?>
                    <tr><td colspan="2">&nbsp;</td></tr>
                    <tr>
                        <td><?php 
                _e('Enter ePin', 'binary-mlm-pro');
                ?>
 :</td>
                        <td><input type="text" name="epin" id="epin" value="<?php 
                if (!empty($_POST['epin'])) {
                    _e(htmlentities($_POST['epin']));
                }
                ?>
" maxlength="20" size="37" onBlur="checkePinAvailability(this.value);"><br /><div id="check_epin"></div></td>
                    </tr>
                    <?php 
            }
        }
        if (empty($mlm_general_settings['sol_payment'])) {
            ?>
                    <tr><td colspan="2">&nbsp;</td></tr>
                    <tr>
                        <td><?php 
            _e('Product', 'binary-mlm-pro');
            ?>
 :</td>
                        <td> <?php 
            $pro_price_settings = $wpdb->get_results("select * from {$table_prefix}mlm_product_price where p_id!='1'");
            ?>

                            <select name="epin_value" id="epin_value" >
                                <option value="">Select Product</option>
                                <?php 
            foreach ($pro_price_settings as $pricedetail) {
                ?>
       
                                    <option value="<?php 
                echo $pricedetail->p_id;
                ?>
" <?php 
                echo $epin_value == $pricedetail->p_id ? 'selected="selected"' : '';
                ?>
><?php 
                echo $pricedetail->product_name;
                ?>
</option>
                                <?php 
            }
            ?>
                            </select></td>
                    </tr>
                <?php 
        }
        ?>
                <tr><td colspan="2">&nbsp;</td></tr>
                <tr>
                    <td><?php 
        _e('Create Password', 'binary-mlm-pro');
        ?>
 <span style="color:red;">*</span> :</td>
                    <td>	<input type="password" name="password" id="password" maxlength="20" size="37" >
                        <br /><span style="font-size:12px; font-style:italic; color:#006633"><?php 
        _e('Password length atleast 6 character', 'binary-mlm-pro');
        ?>
</span>
                    </td>
                </tr>

                <tr><td colspan="2">&nbsp;</td></tr>

                <tr>
                    <td><?php 
        _e('Confirm Password', 'binary-mlm-pro');
        ?>
  <span style="color:red;">*</span> :</td>
                    <td><input type="password" name="confirm_password" id="confirm_password" maxlength="20" size="37" ></td>
                </tr>
                <tr><td colspan="2">&nbsp;</td></tr>
                <tr>
                    <td><?php 
        _e('Email Address', 'binary-mlm-pro');
        ?>
 <span style="color:red;">*</span> :</td>
                    <td><input type="text" name="email" id="email" value="<?php 
        if (!empty($_POST['email'])) {
            _e(htmlentities($_POST['email']));
        }
        ?>
"  size="37" ></td>
                </tr>

                <tr><td colspan="2">&nbsp;</td></tr><tr>

                <tr>
                    <td><?php 
        _e('Confirm Email Address', 'binary-mlm-pro');
        ?>
 <span style="color:red;">*</span> :</td>
                    <td><input type="text" name="confirm_email" id="confirm_email" value="<?php 
        if (!empty($_POST['confirm_email'])) {
            _e(htmlentities($_POST['confirm_email']));
        }
        ?>
" size="37" ></td>
                </tr>

                <tr><td colspan="2">&nbsp;</td></tr>
                <?php 
        if (is_plugin_active('mlm-paypal-mass-pay/load-data.php')) {
            ?>
                    <tr>
                        <td><?php 
            _e('Paypal ID', 'binary-mlm-pro');
            ?>
 <span style="color:red;">*</span> :</td>
                        <td><input type="text" name="paypal_id" id="paypal_id" value="<?php 
            if (!empty($_POST['paypal_id'])) {
                _e(htmlentities($_POST['paypal_id']));
            }
            ?>
" size="37" ></td>
                    </tr>

                    <tr><td colspan="2">&nbsp;</td></tr>
                <?php 
        }
        ?>
	
                <tr>
                    <td><?php 
        _e('First Name', 'binary-mlm-pro');
        ?>
 <span style="color:red;">*</span> :</td>
                    <td><input type="text" name="firstname" id="firstname" value="<?php 
        if (!empty($_POST['firstname'])) {
            _e(htmlentities($_POST['firstname']));
        }
        ?>
" maxlength="20" size="37" onBlur="return checkname(this.value, 'firstname');" ></td>
                </tr>

                <tr><td colspan="2">&nbsp;</td></tr>

                <tr>
                    <td><?php 
        _e('Last Name', 'binary-mlm-pro');
        ?>
 <span style="color:red;">*</span> :</td>
                    <td><input type="text" name="lastname" id="lastname" value="<?php 
        if (!empty($_POST['lastname'])) {
            _e(htmlentities($_POST['lastname']));
        }
        ?>
" maxlength="20" size="37" onBlur="return checkname(this.value, 'lastname');"></td>
                </tr>

                <tr><td colspan="2">&nbsp;</td></tr>

                <tr>
                    <?php 
        if (isset($sponsor_name) && $sponsor_name != '') {
            $spon = $sponsor_name;
        } else {
            if (isset($sp_name)) {
                $spon = $sp_name;
            } else {
                if (isset($_POST['sponsor'])) {
                    $spon = htmlentities($_POST['sponsor']);
                }
            }
        }
        ?>
                    <td><?php 
        _e('Sponsor Name', 'binary-mlm-pro');
        ?>
 <span style="color:red;">*</span> :</td>
                    <td>
                        <input type="text" name="sponsor" id="sponsor" value="<?php 
        if (!empty($spon)) {
            _e($spon);
        }
        ?>
" maxlength="20" size="37" onBlur="checkReferrerAvailability(this.value);" <?php 
        echo $readonly_sponsor;
        ?>
>
                        <br /><div id="check_referrer"></div>
                    </td>
                </tr>

                <tr><td colspan="2">&nbsp;</td></tr>

                <tr>
                    <td><?php 
        _e('Placement', 'binary-mlm-pro');
        ?>
 <span style="color:red;">*</span> :</td>
                    <?php 
        if (isset($_POST['leg']) && $_POST['leg'] == '0') {
            $checked = 'checked';
        } else {
            if (isset($_GET['l']) && $_GET['l'] == '0') {
                $checked = 'checked';
                $disable_leg = 'disabled';
            } else {
                $checked = '';
            }
        }
        if (isset($_POST['leg']) && $_POST['leg'] == '1') {
            $checked1 = 'checked';
        } else {
            if (isset($_GET['l']) && $_GET['l'] == '1') {
                $checked1 = 'checked';
                $disable_leg = 'disabled';
            } else {
                $checked1 = '';
            }
        }
        ?>

                    <td><?php 
        echo __('Left', 'binary-mlm-pro');
        ?>
 <input id="left" type="radio" name="leg" value="0" <?php 
        echo $checked;
        ?>
 <?php 
        if (!empty($disable_leg)) {
            _e($disable_leg);
        }
        ?>
/>
                        <?php 
        echo __('Right', 'binary-mlm-pro');
        ?>
<input id="right" type="radio" name="leg" value="1" <?php 
        echo $checked1;
        ?>
 <?php 
        if (!empty($disable_leg)) {
            _e($disable_leg);
        }
        ?>
/>



                    </td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" name="submit" id="submit" value="<?php 
        _e('Submit', 'binary-mlm-pro');
        ?>
" /></td>
                </tr>
            </form>
        </table>
        <?php 
    } else {
        _e($msg);
    }
}
function mlmNetworkDetailsPage()
{
    //get loged user's key
    $key = get_current_user_key();
    //Total Users on My left leg
    $leftLegUsers = totalLeftLegUsers($key);
    //Total users on my right leg
    $rightLegUsers = totalRightLegUsers($key);
    //paid users on my left leg
    $leftLegActiveUsers = activeUsersOnLeftLeg($key);
    //paid users on my right leg
    $rightLegActiveUsers = activeUsersOnRightLeg($key);
    //Total my personal sales
    $personalSales = totalMyPersonalSales($key);
    //Total my personal sales active users
    $activePersonalSales = activeUsersOnPersonalSales($key);
    //show five users on left leg
    $fiveLeftLegUsers = myFiveLeftLegUsers($key);
    //show five users on right leg
    $fiveRightLegUsers = myFiveRightLegUsers($key);
    //show five users on personal sales
    $fivePersonalUsers = myFivePersonalUsers($key);
    //get logged in user info
    global $current_user, $wpdb;
    get_currentuserinfo();
    $username = $current_user->ID;
    $user_info = get_userdata($current_user->ID);
    $_SESSION['ajax'] = 'ajax_check';
    $add_page_id = get_post_id('mlm_registration_page');
    $sponsor_name = $current_user->user_login;
    $affiliateURLold = site_url() . '?page_id=' . $add_page_id . '&sp=' . $key;
    $affiliateURLnew = site_url() . '/u/' . getusernamebykey($key);
    $permalink = get_permalink(empty($_GET['page_id']) ? '' : $_GET['page_id']);
    $postidparamalink = strstr($permalink, 'page_id');
    $affiliateURL = $postidparamalink ? $affiliateURLold : $affiliateURLnew;
    ?>
    <?php 
    if (function_exists('Update_Paypal_Notification')) {
        Update_Paypal_Notification();
    }
    ?>
    <p class="affiliate_url"><strong>Affiliate URL :</strong> <?php 
    echo $affiliateURL;
    ?>
 </p><br />

    <table width="100%" border="0" cellspacing="10" cellpadding="1">
        <tr>
            <td width="40%" valign="top">
                <table width="100%" border="0" cellspacing="10" cellpadding="1">
                    <tr>
                        <td colspan="2"><strong> <?php 
    _e('Personal Information', 'binary-mlm-pro');
    ?>
</strong></td>
                    </tr>
                    <tr>
                        <td scope="row"><?php 
    _e('Title', 'binary-mlm-pro');
    ?>
</td>
                        <td> <?php 
    _e('Details', 'binary-mlm-pro');
    ?>
</td>
                    </tr>
                    <tr>
                        <td scope="row"> <?php 
    _e('Name', 'binary-mlm-pro');
    ?>
</td>
                        <td><?php 
    echo $user_info->first_name . ' ' . $user_info->last_name;
    ?>
</td>
                    </tr>
                    <tr>
                        <td scope="row"> <?php 
    _e('Address', 'binary-mlm-pro');
    ?>
</td>
                        <td style="white-space:normal;"><?php 
    echo $user_info->user_address1 . "<br>" . $user_info->user_address2;
    ?>
</td>
                    </tr>
                    <tr>
                        <td scope="row"> <?php 
    _e('City', 'binary-mlm-pro');
    ?>
</td>
                        <td><?php 
    echo $user_info->user_city;
    ?>
</td>
                    </tr>
                    <tr>
                        <td scope="row"> <?php 
    _e('Contact No', 'binary-mlm-pro');
    ?>
.</td>
                        <td><?php 
    echo $user_info->user_telephone;
    ?>
</td>
                    </tr>
                    <tr>
                        <td scope="row"> <?php 
    _e('DOB', 'binary-mlm-pro');
    ?>
</td>
                        <td><?php 
    echo $user_info->user_dob;
    ?>
</td>
                    </tr>



                    <tr>
                        <td><a href="<?php 
    echo get_post_id_or_postname('mlm_update_profile_page', 'binary-mlm-pro');
    ?>
" style="text-decoration: none"><?php 
    _e('Edit', 'binary-mlm-pro');
    ?>
</a></td>
                        <td><a href="<?php 
    echo get_post_id_or_postname('mlm_network_genealogy_page', 'binary-mlm-pro');
    ?>
" style="text-decoration: none"><?php 
    _e('View Genealogy', 'binary-mlm-pro');
    ?>
</a></td>
                    </tr>
                </table>
                <table width="100%" border="0" cellspacing="10" cellpadding="1">
                    <tr>
                        <td colspan="2"><strong><?php 
    _e('My Payouts', 'binary-mlm-pro');
    ?>
</strong></td>
                    </tr>
                    <tr>
                        <td scope="row"><?php 
    _e('Date', 'binary-mlm-pro');
    ?>
</td>
                        <td><?php 
    _e('Amount', 'binary-mlm-pro');
    ?>
</td>
                        <td><?php 
    _e('Action', 'binary-mlm-pro');
    ?>
</td>
                    </tr>
                    <?php 
    $detailsArr = my_payout_function();
    //_e("<pre>");print_r($detailsArr); exit;
    //$page_id = get_post_id('mlm_my_payout_details_page');
    if (count($detailsArr) > 0) {
        $mlm_settings = get_option('wp_mlm_general_settings');
        ?>
                        <?php 
        foreach ($detailsArr as $row) {
            $amount = $row->commission_amount + $row->bonus_amount + $row->referral_commission_amount - $row->tax - $row->service_charge;
            ?>
                            <tr>
                                <td><?php 
            echo $row->payoutDate;
            ?>
</td>
                                <td><?php 
            echo $mlm_settings['currency'] . ' ' . $amount;
            ?>
</td>
                                <td><a href="<?php 
            echo get_post_id_or_postname_for_payout('mlm_my_payout_details_page', $row->payout_id);
            ?>
"><?php 
            echo __('View', 'binary-mlm-pro');
            ?>
</a></td>

                            </tr>

                        <?php 
        }
        ?>
                        <?php 
    } else {
        ?>
                        <div class="no-payout"><?php 
        _e('You have not earned any commisssions yet.', 'binary-mlm-pro');
        ?>
 </div>

                        <?php 
    }
    ?>
                </table>
            </td>
            <td width="40%">
                <table width="100%" border="0" cellspacing="10" cellpadding="1">
                    <tr>
                        <td><strong><?php 
    _e('Network Details', 'binary-mlm-pro');
    ?>
</strong></td>
                    </tr>
                    <tr>
                        <td>
                            <table width="100%" border="0" cellspacing="10" cellpadding="1">
                                <tr>
                                    <td colspan="2"><strong><?php 
    _e('Left Leg Sales', 'binary-mlm-pro');
    ?>
</strong></td>
                                </tr>

                                <tr>
                                    <td><?php 
    _e('Total on Left Leg', 'binary-mlm-pro');
    ?>
: <?php 
    echo $leftLegUsers;
    ?>
</td>
                                    <td><?php 
    _e('Active', 'binary-mlm-pro');
    ?>
: <?php 
    echo $leftLegActiveUsers;
    ?>
</td>
                                </tr>
                                <?php 
    foreach ($fiveLeftLegUsers as $key => $value) {
        _e("<tr>");
        foreach ($value as $k => $val) {
            _e("<td>" . $val . "</td>");
        }
        _e("</tr>");
    }
    ?>
                                <tr>
                                    <td colspan="2"><a href="<?php 
    echo get_post_id_or_postname('mlm_left_group_details_page', 'binary-mlm-pro');
    ?>
" style="text-decoration: none"><?php 
    _e('View All', 'binary-mlm-pro');
    ?>
</a></td>
                                </tr>

                            </table>
                        </td>
                    </tr>

                    <tr>
                        <td>
                            <table width="100%" border="0" cellspacing="10" cellpadding="1">
                                <tr>
                                    <td colspan="2"><strong><?php 
    _e('Right Leg Sales', 'binary-mlm-pro');
    ?>
</strong></td>
                                </tr>

                                <tr>
                                    <td><?php 
    _e('Total on Right Leg', 'binary-mlm-pro');
    ?>
: <?php 
    echo $rightLegUsers;
    ?>
</td>
                                    <td><?php 
    _e('Active', 'binary-mlm-pro');
    ?>
: <?php 
    echo $rightLegActiveUsers;
    ?>
</td>
                                </tr>
                                <?php 
    foreach ($fiveRightLegUsers as $key => $value) {
        _e("<tr>");
        foreach ($value as $k => $val) {
            _e("<td>" . $val . "</td>");
        }
        _e("</tr>");
    }
    ?>
                                <tr>
                                    <td colspan="2"><a href="<?php 
    echo get_post_id_or_postname('mlm_right_group_details_page', 'binary-mlm-pro');
    ?>
" style="text-decoration: none"><?php 
    _e('View All', 'binary-mlm-pro');
    ?>
</a></td>
                                </tr>

                            </table>
                        </td>
                    </tr>


                    <tr>
                        <td>
                            <table width="100%" border="0" cellspacing="10" cellpadding="1">
                                <tr>
                                    <td colspan="2"><strong><?php 
    _e('Personal Sales', 'binary-mlm-pro');
    ?>
</strong></td>
                                </tr>

                                <tr>
                                    <td><?php 
    _e('My Personal Sales', 'binary-mlm-pro');
    ?>
: <?php 
    echo $personalSales;
    ?>
</td>
                                    <td><?php 
    _e('Active', 'binary-mlm-pro');
    ?>
: <?php 
    echo $activePersonalSales;
    ?>
</td>
                                </tr>
                                <?php 
    foreach ($fivePersonalUsers as $key => $value) {
        _e("<tr>");
        foreach ($value as $k => $val) {
            _e("<td>" . $val . "</td>");
        }
        _e("</tr>");
    }
    ?>
                                <tr>
                                    <td colspan="2"><a href="<?php 
    echo get_post_id_or_postname('mlm_personal_group_details_page', 'binary-mlm-pro');
    ?>
" style="text-decoration: none"><?php 
    _e('View All', 'binary-mlm-pro');
    ?>
</a></td>
                                </tr>

                            </table>
                        </td>
                    </tr> 

                </table>
            </td>
        </tr>
    </table>

    <?php 
}
function adminMLMUserAccountInterface()
{
    //get database table prefix
    $table_prefix = mlm_core_get_table_prefix();
    global $wpdb;
    $msg = '';
    if (isset($_POST['mlm_user_account'])) {
        $search = $_POST['search_user'];
        $userId = $wpdb->get_var("SELECT ID FROM {$table_prefix}users WHERE user_login = '******' OR user_email = '{$search}'");
        if ($wpdb->num_rows > 0) {
            $_SESSION['search_user'] = $search;
            $_SESSION['session_set'] = 'sets';
            $_SESSION['userID'] = $userId;
        } else {
            $msg = __('You have entered a wrong username or email address', 'binary-mlm-pro');
            $_SESSION['search_user'] = $search;
            $_SESSION['session_set'] = '';
            $_SESSION['userID'] = '';
        }
    }
    include 'js-validation-file.html';
    ?>

    <div class='wrap'>
        <div id="icon-users" class="icon32"></div><h1><?php 
    _e('User Report', 'binary-mlm-pro');
    ?>
</h1><br />

        <div class="notibar msginfo" style="margin:10px;">
            <a class="close"></a>
            <p><?php 
    _e("Input a member's username or email address in the input box below to get complete information about the member's account. No more switching back and forth into different member accounts to check their account details.", 'binary-mlm-pro');
    ?>
</p>
        </div>	

    </div>

    <div class="forms-ui">

        <p><span style='color:red;'><?php 
    if (!empty($error)) {
        _e($error);
    }
    ?>
</span></p>

        <form name="open_user_account" method="post" action="">
            <table border="0" cellpadding="0" cellspacing="0" width="100%" class="form-table">
                <tr>
                    <th scope="row" class="admin-settings">
                        <a style="cursor:pointer;" title="Click for Help!" onclick="toggleVisibility('search-user');"><?php 
    _e('Search By username or email address', 'binary-mlm-pro');
    ?>
 :</a>
                    </th>
                    <td>
                        <input type="text" name="search_user" id="search_user" size="52" value="<?php 
    if (!empty($_SESSION['search_user'])) {
        _e(htmlentities($_SESSION['search_user']));
    }
    ?>
">
                        <a href="?page=mlm-user-account" style="cursor:pointer;text-decoration:none; margin-left:90px;"><?php 
    _e('Back to User Dashboard', 'binary-mlm-pro');
    ?>
</a>
                        <div class="toggle-visibility" id="search-user"><?php 
    _e('Please enter username or email address.', 'binary-mlm-pro');
    ?>
</div>
                        <div style="color:red;"><?php 
    echo $msg;
    ?>
</div>
                    </td>
                </tr>
            </table>
            <p class="submit">
                <input type="submit" name="mlm_user_account" id="mlm_user_account" value="<?php 
    _e('Search', 'binary-mlm-pro');
    ?>
 &raquo;" class='button-primary' onclick="needToConfirm = false;">
            </p>
        </form>

        <script language="JavaScript">
            populateArrays();
        </script>
        <!-----sdfgdf -->
        <?php 
    if (!empty($_GET['ac'])) {
        if ($_GET['ac'] == 'edit' && $_GET['page'] == 'mlm-user-account') {
            mlm_update_profile($_SESSION['userID']);
        } else {
            if ($_GET['ac'] == 'leftleg' && $_GET['page'] == 'mlm-user-account') {
                myLeftGroupDetails($_SESSION['userID']);
            } else {
                if ($_GET['ac'] == 'rightleg' && $_GET['page'] == 'mlm-user-account') {
                    myRightGroupDetails($_SESSION['userID']);
                } else {
                    if ($_GET['ac'] == 'personal' && $_GET['page'] == 'mlm-user-account') {
                        myPersonalGroupDetails($_SESSION['userID']);
                    } else {
                        if ($_GET['ac'] == 'payout' && $_GET['page'] == 'mlm-user-account') {
                            mlm_my_payout_page($_SESSION['userID']);
                        } else {
                            if ($_GET['ac'] == 'payout-details' && $_GET['page'] == 'mlm-user-account') {
                                mlm_my_payout_details_page($_SESSION['userID']);
                            } else {
                                if ($_GET['ac'] == 'network' && $_GET['page'] == 'mlm-user-account') {
                                    adminViewBinaryNetwork($_SESSION['userID']);
                                }
                            }
                        }
                    }
                }
            }
        }
    } else {
        if (!empty($_SESSION['session_set']) && $_SESSION['session_set'] == 'sets') {
            $key = $wpdb->get_var("SELECT user_key FROM {$table_prefix}mlm_users WHERE user_id = {$_SESSION['userID']}");
            //Total Users on My left leg
            $leftLegUsers = totalLeftLegUsers($key);
            //Total users on my right leg
            $rightLegUsers = totalRightLegUsers($key);
            //paid users on my left leg
            $leftLegActiveUsers = activeUsersOnLeftLeg($key);
            //paid users on my right leg
            $rightLegActiveUsers = activeUsersOnRightLeg($key);
            //Total my personal sales
            $personalSales = totalMyPersonalSales($key);
            //Total my personal sales active users
            $activePersonalSales = activeUsersOnPersonalSales($key);
            //show five users on left leg
            $fiveLeftLegUsers = myFiveLeftLegUsers($key);
            //show five users on right leg
            $fiveRightLegUsers = myFiveRightLegUsers($key);
            //show five users on personal sales
            $fivePersonalUsers = myFivePersonalUsers($key);
            $user_info = get_userdata($_SESSION['userID']);
            $add_page_id = get_post_id('mlm_registration_page');
            $sponsor_name = $user_info->user_login;
            $affiliateURL = site_url() . '?page_id=' . $add_page_id . '&sponsor=' . $sponsor_name;
            ?>

            <table width="100%" border="0" cellspacing="10" cellpadding="1">
                <tr>
                    <td width="40%" valign="top">
                        <table width="100%" border="0" cellspacing="10" cellpadding="1">
                            <tr>
                                <td colspan="2"><strong> <?php 
            _e('Personal Information', 'binary-mlm-pro');
            ?>
</strong></td>
                            </tr>
                            <tr>
                                <td scope="row"><?php 
            _e('Title', 'binary-mlm-pro');
            ?>
</td>
                                <td> <?php 
            _e('Details', 'binary-mlm-pro');
            ?>
</td>
                            </tr>
                            <tr>
                                <td scope="row"> <?php 
            _e('Name', 'binary-mlm-pro');
            ?>
</td>
                                <td><?php 
            echo $user_info->first_name . ' ' . $user_info->last_name;
            ?>
</td>
                            </tr>
                            <tr>
                                <td scope="row"> <?php 
            _e('Address', 'binary-mlm-pro');
            ?>
</td>
                                <td style="white-space:normal;"><?php 
            echo $user_info->user_address1 . "<br>" . $user_info->user_address2;
            ?>
</td>
                            </tr>
                            <tr>
                                <td scope="row"> <?php 
            _e('City', 'binary-mlm-pro');
            ?>
</td>
                                <td><?php 
            echo $user_info->user_city;
            ?>
</td>
                            </tr>
                            <tr>
                                <td scope="row"> <?php 
            _e('Contact No', 'binary-mlm-pro');
            ?>
.</td>
                                <td><?php 
            echo $user_info->user_telephone;
            ?>
</td>
                            </tr>
                            <tr>
                                <td scope="row"> <?php 
            _e('DOB', 'binary-mlm-pro');
            ?>
</td>
                                <td><?php 
            echo $user_info->user_dob;
            ?>
</td>
                            </tr>


                            <tr>
                                <td><a href="?page=mlm-user-account&ac=edit" style="text-decoration: none"> <?php 
            _e('Edit', 'binary-mlm-pro');
            ?>
</a></td>
                                <td><a href="?page=mlm-user-account&ac=network" style="text-decoration: none"> <?php 
            _e('View Genealogy', 'binary-mlm-pro');
            ?>
</a></td>
                            </tr>
                        </table>
                        <table width="100%" border="0" cellspacing="10" cellpadding="1">
                            <tr>
                                <td colspan="2"><strong> <?php 
            _e('My Payouts', 'binary-mlm-pro');
            ?>
</strong></td>
                            </tr>
                            <tr>
                                <td scope="row"> <?php 
            _e('Date', 'binary-mlm-pro');
            ?>
</td>
                                <td> <?php 
            _e('Amount', 'binary-mlm-pro');
            ?>
</td>
                                <td> <?php 
            _e('Action', 'binary-mlm-pro');
            ?>
</td>
                            </tr>
                            <?php 
            $detailsArr = my_payout_function($_SESSION['userID']);
            //_e("<pre>");print_r($detailsArr); exit;
            //$page_id = get_post_id('mlm_my_payout_details_page');
            if (count($detailsArr) > 0) {
                $mlm_settings = get_option('wp_mlm_general_settings');
                ?>
                                <?php 
                foreach ($detailsArr as $row) {
                    $amount = $row->commission_amount + $row->bonus_amount + $row->referral_commission_amount - $row->tax - $row->service_charge;
                    ?>
                                    <tr>
                                        <td><?php 
                    echo $row->payoutDate;
                    ?>
</td>
                                        <td><?php 
                    echo $mlm_settings['currency'] . ' ' . $amount;
                    ?>
</td>
                                        <td><a href="?page=mlm-user-account&ac=payout-details&pid=<?php 
                    echo $row->payout_id;
                    ?>
" style="text-decoration:none;"> <?php 
                    _e('View', 'binary-mlm-pro');
                    ?>
</a></td>	
                                    </tr>
                                    <tr><td colspan="2"><a href="?page=mlm-user-account&ac=payout" style="text-decoration:none;"> <?php 
                    _e('View All', 'binary-mlm-pro');
                    ?>
</a></td></tr>
                                <?php 
                }
                ?>
                                <?php 
            } else {
                ?>
                                <div class="no-payout">  <?php 
                _e('You have not earned any commisssions yet.', 'binary-mlm-pro');
                ?>
 </div>

                                <?php 
            }
            ?>
                        </table>
                    </td>
                    <td width="40%">
                        <table width="100%" border="0" cellspacing="10" cellpadding="1">
                            <tr>
                                <td><strong> <?php 
            _e('Network Details', 'binary-mlm-pro');
            ?>
</strong></td>
                            </tr>
                            <tr>
                                <td>
                                    <table width="100%" border="0" cellspacing="10" cellpadding="1">
                                        <tr>
                                            <td colspan="2"><strong> <?php 
            _e('Left Leg Sales', 'binary-mlm-pro');
            ?>
</strong></td>
                                        </tr>

                                        <tr>
                                            <td><?php 
            _e('Total on Left Leg', 'binary-mlm-pro');
            ?>
: <?php 
            echo $leftLegUsers;
            ?>
</td>
                                            <td>Active: <?php 
            echo $leftLegActiveUsers;
            ?>
</td>
                                        </tr>
                                        <?php 
            foreach ($fiveLeftLegUsers as $key => $value) {
                _e("<tr>");
                foreach ($value as $k => $val) {
                    _e("<td>" . $val . "</td>");
                }
                _e("</tr>");
            }
            ?>
                                        <tr>
                                            <td colspan="2"><a href="?page=mlm-user-account&ac=leftleg" style="text-decoration: none"> <?php 
            _e('View All', 'binary-mlm-pro');
            ?>
</a></td>
                                        </tr>

                                    </table>
                                </td>
                            </tr>

                            <tr>
                                <td>
                                    <table width="100%" border="0" cellspacing="10" cellpadding="1">
                                        <tr>
                                            <td colspan="2"><strong> <?php 
            _e('Right Leg Sales', 'binary-mlm-pro');
            ?>
</strong></td>
                                        </tr>

                                        <tr>
                                            <td><?php 
            _e('Total on Right Leg', 'binary-mlm-pro');
            ?>
: <?php 
            echo $rightLegUsers;
            ?>
</td>
                                            <td>Active: <?php 
            echo $rightLegActiveUsers;
            ?>
</td>
                                        </tr>
                                        <?php 
            foreach ($fiveRightLegUsers as $key => $value) {
                _e("<tr>");
                foreach ($value as $k => $val) {
                    _e("<td>" . $val . "</td>");
                }
                _e("</tr>");
            }
            ?>
                                        <tr>
                                            <td colspan="2"><a href="?page=mlm-user-account&ac=rightleg" style="text-decoration: none"> <?php 
            _e('View All', 'binary-mlm-pro');
            ?>
</a></td>
                                        </tr>

                                    </table>
                                </td>
                            </tr>


                            <tr>
                                <td>
                                    <table width="100%" border="0" cellspacing="10" cellpadding="1">
                                        <tr>
                                            <td colspan="2"><strong> <?php 
            _e('Personal Sales', 'binary-mlm-pro');
            ?>
</strong></td>
                                        </tr>

                                        <tr>
                                            <td> <?php 
            _e('My Personal Sales', 'binary-mlm-pro');
            ?>
: <?php 
            echo $personalSales;
            ?>
</td>
                                            <td>Active: <?php 
            echo $activePersonalSales;
            ?>
</td>
                                        </tr>
                                        <?php 
            foreach ($fivePersonalUsers as $key => $value) {
                _e("<tr>");
                foreach ($value as $k => $val) {
                    _e("<td>" . $val . "</td>");
                }
                _e("</tr>");
            }
            ?>
                                        <tr>
                                            <td colspan="2"><a href="?page=mlm-user-account&ac=personal" style="text-decoration: none"> <?php 
            _e('View All', 'binary-mlm-pro');
            ?>
</a></td>
                                        </tr>

                                    </table>
                                </td>
                            </tr> 

                        </table>
                    </td>
                </tr>
            </table>
        <?php 
        }
    }
    ?>
        <!---sdfdsfsd ->
        
        
                </div>
        <?php 
}
function join_network()
{
    global $wpdb, $current_user;
    $user_id = $current_user->ID;
    $table_prefix = mlm_core_get_table_prefix();
    $error = '';
    $chk = 'error';
    if (!empty($_GET['sp_name'])) {
        $sp_name = $wpdb->get_var("select username from {$table_prefix}mlm_users where username='******'sp_name'] . "'");
        if ($sp_name) {
            ?>
            <script type='text/javascript'>
                $.cookie('sp_name', '<?php 
            echo $sp_name;
            ?>
', {path: '/'});
            </script>
            <?php 
        }
    } else {
        if (!empty($_REQUEST['sp'])) {
            $sp_name = $wpdb->get_var("select username from {$table_prefix}mlm_users where user_key='" . $_REQUEST['sp'] . "'");
            if ($sp_name) {
                ?>
            <script type='text/javascript'>
                $.cookie('sp_name', '<?php 
                echo $sp_name;
                ?>
', {path: '/'});
            </script>
            <?php 
            }
        } else {
            $sp_name = empty($_COOKIE["sp_name"]) ? '' : $_COOKIE["sp_name"];
        }
    }
    /*     * ****date format ***** */
    $date_format = get_option('date_format');
    $time_format = get_option('time_format');
    /*     * ****** end******* */
    global $current_user;
    get_currentuserinfo();
    $mlm_general_settings = get_option('wp_mlm_general_settings');
    if (isset($_REQUEST['sp']) && $_REQUEST['sp'] != '') {
        $sponsorName = getusernamebykey($_REQUEST['sp']);
        if (isset($sponsorName) && $sponsorName != '') {
            $readonly_sponsor = 'readonly';
            $sponsor_name = $sponsorName;
        } else {
            redirectPage(home_url(), array());
            exit;
        }
    } else {
        if (!empty($_REQUEST['sp_name'])) {
            $sponsorName = $_REQUEST['sp_name'];
            if (!empty($sponsorName)) {
                $readonly_sponsor = 'readonly';
                $sponsor_name = $sponsorName;
            } else {
                redirectPage(home_url(), array());
                exit;
            }
        } else {
            $readonly_sponsor = '';
        }
    }
    //most outer if condition
    if (isset($_POST['submit'])) {
        $firstname = sanitize_text_field($_POST['firstname']);
        $lastname = sanitize_text_field($_POST['lastname']);
        $email = sanitize_text_field($_POST['email']);
        $sponsor = sanitize_text_field($_POST['sponsor']);
        if (empty($sponsor)) {
            $sponsor = $wpdb->get_var("select `username` FROM {$table_prefix}mlm_users order by id asc limit 1");
        }
        if (checkInputField($firstname)) {
            $error .= "\n Please enter your first name.";
        }
        if (checkInputField($lastname)) {
            $error .= "\n Please enter your last name.";
        }
        if (!is_email($email)) {
            $error .= "\n E-mail address is invalid.";
        }
        //Add usernames we don't want used
        $invalid_usernames = array('admin');
        //Do username validation
        $sql = "SELECT COUNT(*) num, `user_key` \n\t\t\t\tFROM {$table_prefix}mlm_users \n\t\t\t\tWHERE `username` = '" . $sponsor . "'";
        $intro = $wpdb->get_row($sql);
        if (isset($_GET['l']) && $_GET['l'] != '') {
            $leg = $_GET['l'];
        } else {
            $leg = $_POST['leg'];
        }
        if (isset($leg) && $leg != '0') {
            if ($leg != '1') {
                $error .= "\n You have enter a wrong placement.";
            }
        }
        if (!isset($leg)) {
            $key = $wpdb->get_var("SELECT user_key FROM {$table_prefix}mlm_users WHERE user_id = '{$user_id}'");
            $l = totalLeftLegUsers($key);
            $r = totalRightLegUsers($key);
            if ($l < $r) {
                $leg = '0';
            } else {
                $leg = '1';
            }
        }
        //generate random numeric key for new user registration
        $user_key = generateKey();
        //if generated key is already exist in the DB then again re-generate key
        do {
            $check = $wpdb->get_var("SELECT COUNT(*) ck FROM {$table_prefix}mlm_users \n                                     WHERE `user_key` = '" . $user_key . "'");
            $flag = 1;
            if ($check == 1) {
                $user_key = generateKey();
                $flag = 0;
            }
        } while ($flag == 0);
        //check parent key exist or not
        if (isset($_GET['k']) && $_GET['k'] != '') {
            if (!checkKey($_GET['k'])) {
                $error .= "\n Parent key does't exist.";
            }
            // check if the user can be added at the current position
            $checkallow = checkallowed($_GET['k'], $leg);
            if ($checkallow >= 1) {
                $error .= "\n You have enter a wrong placement.";
            }
        }
        // outer if condition
        if (empty($error)) {
            // inner if condition
            if ($intro->num == 1) {
                $sponsor = $intro->user_key;
                $sponsor1 = $sponsor;
                //find parent key
                if (isset($_GET['k']) && $_GET['k'] != '') {
                    $parent_key = $_GET['k'];
                } else {
                    $readonly_sponsor = '';
                    do {
                        $sql = "SELECT `user_key` FROM {$table_prefix}mlm_users \n                                WHERE parent_key = '" . $sponsor1 . "' AND \n                                leg = '" . $leg . "' AND banned = '0'";
                        $spon = $wpdb->get_var($sql);
                        $num = $wpdb->num_rows;
                        if ($num) {
                            $sponsor1 = $spon;
                        }
                    } while ($num == 1);
                    $parent_key = $sponsor1;
                }
                // return the wp_users table inserted user's ID
                $user = array('ID' => $user_id, 'first_name' => $firstname, 'last_name' => $lastname, 'user_email' => $email, 'role' => 'mlm_user');
                // return the wp_users table inserted user's ID
                $user_id = wp_update_user($user);
                $username = $current_user->user_login;
                //get the selected country name from the country table
                /* Send e-mail to admin and new user - 
                   You could create your own e-mail instead of using this function */
                /**                 * ****** product Price set *************** */
                if (!empty($mlm_general_settings['product_price'])) {
                    $pc = $mlm_general_settings['product_price'];
                } else {
                    $pc = '0';
                }
                //insert the data into fa_user table
                if (!empty($epin)) {
                    $pointStatus = $wpdb->get_row("select point_status from {$table_prefix}mlm_epins where epin_no = '{$epin}'", ARRAY_N);
                    // to epin point status 1
                    if ($pointStatus[0] == '1') {
                        $paymentStatus = '1';
                        $product_price = $pc;
                    } else {
                        if ($pointStatus[0] == '0') {
                            $paymentStatus = '2';
                            $product_price = '0';
                        }
                    }
                } else {
                    // to non epin
                    $paymentStatus = '0';
                    $product_price = '0';
                }
                $insert = "INSERT INTO {$table_prefix}mlm_users(\n\t\t\tuser_id, username, user_key, parent_key, sponsor_key, leg,payment_status,product_price) \n\t\t\tVALUES(\n\t\t\t'" . $user_id . "','" . $username . "', '" . $user_key . "', '" . $parent_key . "', '" . $sponsor . "', '" . $leg . "','" . $paymentStatus . "','" . $product_price . "')";
                // if all data successfully inserted
                if ($wpdb->query($insert)) {
                    //begin most inner if condition
                    //entry on Left and Right Leg tables
                    if ($leg == 0) {
                        $insert = "INSERT INTO {$table_prefix}mlm_leftleg  (pkey, ukey) \n\t\t\t\tVALUES ('" . $parent_key . "','" . $user_key . "')";
                        $insert = $wpdb->query($insert);
                    } else {
                        if ($leg == 1) {
                            $insert = "INSERT INTO {$table_prefix}mlm_rightleg(pkey, ukey) \n\t\t\t\tVALUES('" . $parent_key . "','" . $user_key . "')";
                            $insert = $wpdb->query($insert);
                        }
                    }
                    //begin while loop
                    while ($parent_key != '0') {
                        $query = "SELECT COUNT(*) num, parent_key, leg \n                                FROM {$table_prefix}mlm_users \n                                WHERE user_key = '" . $parent_key . "'\n                                AND banned = '0'";
                        $result = $wpdb->get_row($query);
                        if ($result->num == 1) {
                            if ($result->parent_key != '0') {
                                if ($result->leg == 1) {
                                    $tbright = "INSERT INTO {$table_prefix}mlm_rightleg (pkey,ukey) \n\t\t\t\t\t\tVALUES('" . $result->parent_key . "','" . $user_key . "')";
                                    $tbright = $wpdb->query($tbright);
                                } else {
                                    $tbleft = "INSERT INTO {$table_prefix}mlm_leftleg (pkey, ukey) \n\t\t\t\t\t\tVALUES('" . $result->parent_key . "','" . $user_key . "')";
                                    $tbleft = $wpdb->query($tbleft);
                                }
                            }
                            $parent_key = $result->parent_key;
                        } else {
                            $parent_key = '0';
                        }
                    }
                    //end while loop
                    if (isset($epin) && !empty($epin)) {
                        $sql = "update {$table_prefix}mlm_epins set user_key='{$user_key}', date_used=now(), status=1 where epin_no ='{$epin}' ";
                        // Update epin according user_key (19-07-2013)
                        $wpdb->query($sql);
                        if ($paymentStatus == 1) {
                            insert_refferal_commision($user_id, $sponsor, $user_key);
                        }
                    }
                    $chk = '';
                    $msg = "<span style='color:green;'>Congratulations! You have successfully Join MLM</span>";
                }
                //end most inner if condition
            } else {
                $error = "\n Sponsor does not exist in the system.";
            }
        }
        //end outer if condition
    }
    //end most outer if condition
    //if any error occoured
    if (!empty($error)) {
        $error = nl2br($error);
    }
    if ($chk != '') {
        include 'js-validation-file.html';
        ?>
        <?php 
        if ($current_user->roles[0] == 'mlm_user') {
            echo "Your are MLM user";
        } else {
            $_POST['firstname'] = get_user_meta($user_id, 'first_name', true);
            $_POST['lastname'] = get_user_meta($user_id, 'last_name', true);
            $_POST['email'] = $current_user->user_email;
            ?>
            <script>
                function checkspname() {
                    var spname = document.getElementById('sponsor').value;
                    if (spname == '') {
                        if (!confirm('Are you sure you do not know your Sponsor\'s username? Proceed without a Sponsor?')) {
                            return false;
                        }
                    }
                }
            </script>
            <span style='color:red;'><?php 
            echo $error;
            ?>
</span>
            <?php 
            if (isset($msg) && $msg != "") {
                echo $msg;
            }
            ?>
            <form name="frm" method="post" action="" onSubmit="checkspname()">			
                <table border="0" cellpadding="0" cellspacing="0" width="100%">
                    <tr><td colspan="2">&nbsp;</td></tr>

                    <tr>
                        <td><?php 
            _e('First Name', 'binary-mlm-pro');
            ?>
 <span style="color:red;">*</span> :</td>
                        <td><input type="text" name="firstname" id="firstname" value="<?php 
            if (!empty($_POST['firstname'])) {
                _e(htmlentities($_POST['firstname']));
            }
            ?>
" maxlength="20" size="37" onBlur="return checkname(this.value, 'firstname');" ></td>
                    </tr>

                    <tr><td colspan="2">&nbsp;</td></tr>

                    <tr>
                        <td><?php 
            _e('Last Name', 'binary-mlm-pro');
            ?>
 <span style="color:red;">*</span> :</td>
                        <td><input type="text" name="lastname" id="lastname" value="<?php 
            if (!empty($_POST['lastname'])) {
                _e(htmlentities($_POST['lastname']));
            }
            ?>
" maxlength="20" size="37" onBlur="return checkname(this.value, 'lastname');"></td>
                    </tr>
                    <tr><td colspan="2">&nbsp;</td></tr>
                    <tr>
                        <td><?php 
            _e('Email Address', 'binary-mlm-pro');
            ?>
 <span style="color:red;">*</span> :</td>
                        <td><input type="text" name="email" id="email" value="<?php 
            if (!empty($_POST['email'])) {
                _e(htmlentities($_POST['email']));
            }
            ?>
"  size="37" ></td>
                    </tr>
                    <tr><td colspan="2">&nbsp;</td></tr>
                    <tr>
                        <td><?php 
            _e('Sponsor Name', 'binary-mlm-pro');
            ?>
 <span style="color:red;">*</span> :</td>
                        <td>
                            <input type="text" name="sponsor" id="sponsor" value="<?php 
            if (!empty($_POST['sponsor'])) {
                _e(htmlentities($_POST['sponsor']));
            }
            ?>
" maxlength="20" size="37" onkeyup="checkReferrerAvailability12(this.value);">
                            <br /><div id="check_referrer"></div>
                        </td>
                    </tr>
                    <tr><td colspan="2">&nbsp;</td></tr>
                    <tr>
                        <td><?php 
            _e('Placement', 'binary-mlm-pro');
            ?>
 <span style="color:red;">*</span> :</td>
                        <td><?php 
            echo __('Left', 'binary-mlm-pro');
            ?>
 <input id="left" type="radio" name="leg" value="0" <?php 
            echo isset($led) && $leg == '0' ? 'checked="checked"' : '';
            ?>
 />
                            <?php 
            echo __('Right', 'binary-mlm-pro');
            ?>
<input id="right" type="radio" name="leg" value="1" <?php 
            echo isset($led) && $leg == '0' ? 'checked="checked"' : '';
            ?>
/>
                        </td>

                    <tr><td colspan="2">&nbsp;</td></tr>
                    <tr>
                        <td colspan="2"><input type="submit" name="submit" id="submit" value="<?php 
            _e('Submit', 'binary-mlm-pro');
            ?>
" /></td>
                    </tr>
                </table>
            </form>
            <?php 
        }
        ?>
	
        <?php 
    } else {
        _e($msg);
    }
}