
<?php
    
    session_start();
    date_default_timezone_set('Africa/Lagos');
    ini_set('display_errors', 0); 
    include $_SERVER['DOCUMENT_ROOT'] . "/dbase/dbaseconnection.php";
    include $_SERVER['DOCUMENT_ROOT'] . "/functions/functions.php";
    include $_SERVER['DOCUMENT_ROOT'] . "/functions/mlmfunctions.php";
    include $_SERVER['DOCUMENT_ROOT'] . "/scripts/arraylist.php";    //$userid=1;
    
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <title>MLMPro | Multi-Level Marketing</title>
        <link href="/css/styles.css" rel="stylesheet" type="text/css" />
        <link href="/icofont/css/icofont.css" rel="stylesheet" type="text/css"/>
        <style type="text/css">
            * {font-family: Arial;box-sizing: border-box;}
            .errors {font-weight: normal;font-size: 1em;margin-left: 5px;color: red;}

            @media screen and (max-width: 480px){
               .registrationform .control {float: left;width: 100%;padding: 0 1%;float: none;} 
            }
        </style>           
    </head>
    <body>       
        <div id="header"><h1>MLMPro MLM | Welcome</h1></div>
        <div id="topbackground" style="position: relative;">
            

            <!--<div id="pagetitle" style="font-size: 18px; color: #fff;border: 1px solid #333;margin-top: -48px;">
                <h2 style="">Join MLMPro MLM</h2>
            </div>-->
        </div>
        <div id="bodycontainer">
            <?php
                $usernameErr=$emailErr=$passwordErr=$phoneErr=$confirmpasswordErr=$firstnameErr=$lastnameErr=$sponsoridErr=$epincodeErr='';
                $errors=0;
                
                if(isset($_POST['submit']) && $_POST['submit']=='Create Account'){
                     //retrieve variables and validate
                    $sponsorid=isset($_POST['sponsorid'])?$_POST['sponsorid']:'0';
                    $sql="SELECT * FROM mlmmembers WHERE profileid='" . $sponsorid . "'";
                    $result=mysql_query($sql) or die(mysql_error());
                    $count=mysql_num_rows($result);

                    if($count <=0){
                        $sponsoridErr='Invalid Sponsor ID entered';
                        $errors=1;
                    }else{
                        $rows=mysql_fetch_array($result);
                        $sponsorid=$rows['profileid'];
                        $referralid=$rows['userid'];
                    }

                    $username=isset($_POST['username'])?($_POST['username']):'';
                    if(empty($username)){
                        $usernameErr='Username cannot be empty';
                        $errors=1;
                    }

                    $epincode=isset($_POST['epincode'])?($_POST['epincode']):'';
                    if(empty($epincode)){
                        $epincodeErr='e-Pin cannot be empty';
                        $errors=1;
                    }else{
                        $sql="SELECT * FROM mlmepins WHERE pincode='" . $epincode . "' AND status='NOT USED' AND blocked='NO'";
                        $result=mysql_query($sql) or die(mysql_error());

                        $count=mysql_num_rows($result);

                        if($count<=0){
                            $epincodeErr='e-Pin invalid/already used';
                            $errors=1;
                        }
                    }                    

                    $firstname=isset($_POST['firstname'])?($_POST['firstname']):'';
                    if(empty($firstname)){
                        $firstnameErr='First name cannot be empty';
                        $errors=1;
                    }

                    $lastname=isset($_POST['lastname'])?($_POST['lastname']):'';
                    if(empty($lastname)){
                        $lastnameErr='Last name cannot be empty';
                        $errors=1;
                    }


                    $sql="SELECT * FROM mlmmembers WHERE username='" . $username . "'";
                    $result=mysql_query($sql) or die(mysql_error());
                    $count=mysql_num_rows($result);

                    if($count >=1){
                        $usernameErr='Username already taken';
                        $errors=1;
                    }


                    $emailaddress=isset($_POST['emailaddress'])?$_POST['emailaddress']:'';
                    $emailaddress = filter_input(INPUT_POST, 'emailaddress', FILTER_VALIDATE_EMAIL);
                    if ($emailaddress === false) {
                        $emailErr= "Email address is invalid.";
                         $errors=1;
                    }

                    $phoneno=isset($_POST['phoneno'])?$_POST['phoneno']:'na';
                    /*if(empty($phoneno)){
                        $phoneErr='Mobile no is required!';
                        $errors=1;
                    }*/

                    $city=isset($_POST['city'])?$_POST['city']:'NA';
                    /*if(empty($city)){
                        $cityErr='Location/City required!';
                        $errors=1;
                    }*/

                    $accountname=isset($_POST['accountname'])?$_POST['accountname']:'na';
                    /*if(empty($accountname)){
                        $accountnameErr='Enter your Account Name!';
                        $errors=1;
                    }*/

                    $accountno=isset($_POST['accountno'])?$_POST['accountno']:'na';
                    /*if(empty($accountno)){
                        $accountnoErr='Enter your Account Number!';
                        $errors=1;
                    }*/

                    $bankname=isset($_POST['bankname'])?$_POST['bankname']:'na';
                    /*if(empty($bankname)){
                        $banknameErr='Enter your Bank Name';
                        $errors=1;
                    }*/

                    $bankswiftcode=isset($_POST['bankswiftcode'])?$_POST['bankswiftcode']:'NA';
                    /*if(empty($bankswiftcode)){
                        $bankswiftcodeErr='Enter your Bank Swift/Sort Code';
                        $errors=1;
                    }*/


                    $address=isset($_POST['address'])?$_POST['address']:'na';
                    /*if(empty($address)){
                        $addressErr='Enter your address!';
                        $errors=1;
                    }*/


                    
                    if(empty($_POST['password'])){
                        $passwordErr="Password required";
                        $errors=1;
                    }else{
                        $password=testinput(md5($_POST['password']));
                    }

                    if(empty($_POST['confirmpassword'])){
                        $confirmpasswordErr="Confirm password required!";
                        $errors=1;
                    }else{
                        $confirmpassword=testinput(md5($_POST['confirmpassword']));
                    }                               

                    if($password!=$confirmpassword){
                        $confirmpasswordErr='Password and Confirm Password not identical';
                        $errors=1;
                    }

                    $country=isset($_POST['country'])?$_POST['country']:'NA';

                    $terms=isset($_POST['terms'])?$_POST['terms']:'';
                    if(isset($_POST['terms']) && $_POST['terms']!=""){

                    }else{
                        $termsErr="You must accept in our Terms &amp; Condition to continue";
                        $errors=1;
                    }
                    if(empty($terms)){
                        $termsErr="You must accept in our Terms &amp; Condition to continue";
                        $errors=1;
                    }

                    if(empty($_POST['transpassword'])){
                        $transpasswordErr="Transaction Password required";
                        $errors=1;
                    }else{
                        $transpassword=testinput(md5($_POST['password']));
                    }

                    if(empty($_POST['confirmtranspassword'])){
                        $confirmtranspasswordErr="Confirm Transaction password required!";
                        $errors=1;
                    }else{
                        $confirmtranspassword=testinput(md5($_POST['confirmpassword']));
                    }                               

                    if($transpassword!=$confirmtranspassword){
                        $confirmtranspasswordErr='Transaction Password not identical';
                        $errors=1;
                    }

                    $gender=isset($_POST['gender'])?$_POST['gender']:'NA';
                    $dob=isset($_POST['dob'])?$_POST['dob']:'0000-00-00';
                    $zipcode=isset($_POST['zipcode'])?$_POST['zipcode']:'NA';
                    $nomineename=isset($_POST['nomineename'])?$_POST['nomineename']:'NA';
                    $nomineephoneno=isset($_POST['nomineephoneno'])?$_POST['nomineephoneno']:'NA';
                    $relationship=isset($_POST['relationship'])?$_POST['relationship']:'NA';
                    $nomineeaddress=isset($_POST['nomineeaddress'])?$_POST['nomineeaddress']:'NA';
                    $terms=isset($_POST['terms'])?$_POST['terms']:'NA';
                    $l_member=isset($_POST['l_member'])?$_POST['l_member']:'NA';
                    $r_member=isset($_POST['r_member'])?$_POST['r_member']:'NA';

                    //check error count
                    if($errors==1){
                        include $_SERVER['DOCUMENT_ROOT'] . '/includes/registrationform.inc.php';
                    }else{
                        $insertSQL="INSERT INTO mlmmembers(sponsorid,username,firstname, lastname, emailaddress, password, phoneno,datecreated, country, city, address, accountname, accountno, bankname, bankswiftcode,epinused,currentstage,gender, dob, zipcode, nomineename, nomineephoneno, relationship, nomineeaddress, terms, transpassword) VALUES ('$sponsorid','$username', '$firstname', '$lastname', '$emailaddress', '$password', '$phoneno',CURDATE(), '$country','$city','$address', '$accountname', '$accountno', '$bankname', '$bankswiftcode','$epincode','Pawn','$gender', '$dob', '$zipcode', '$nomineename', '$nomineephoneno', '$relationship', '$nomineeaddress', '$terms','$transpassword')";
                        $result=mysql_query($insertSQL) or die(mysql_error()); 

                        if(1==1){
                            //create session variables
                            $_SESSION['userid']=$userid=mysql_insert_id();
                            $_SESSION['username']=$username;
                            $_SESSION['logged']=1;

                            //generate profileid consists of YearMonthID
                            $profileID= 'RL'.date('Y').date('m'). str_pad($userid,5,'0',STR_PAD_LEFT);
                            $sql="UPDATE mlmmembers SET profileid='$profileID' WHERE userid=$userid";
                            $result=mysql_query($sql) or die(mysql_error());

                            //update pin code as used
                            mysql_query("UPDATE mlmepins SET status ='USED' WHERE pincode=$epincode");

                            
                           if($result){

                                $parentid=setparentid_new($sponsorid,$userid);
                                newmatrixbonus($parentid);
                                $count=0;
                                newrookmatrix();
                                $count=0;
                                newknightmatrix();
                                $count=0;
                                newbishopmatrix();
                                $count=0;
                                newkingmatrix();
                                $count=0;
                                newqueenmatrix();
                                $count=0;                                
                                registrationcalculation($userid);
                                $count=0;
                                referralbonus($sponsorid,$userid);
                                //setparentid($sponsorid,$userid);
                                //computematrixbonus($sponsorid); 
                                //stepoutbonus($sponsorid);  
                                $to=$emailaddress;
                                
                                $subject="Welcome to MLMPro";

                                $msg="Dear " . $username . "\n\n Welcome to MLMPro. You have successfully registered as a member. Now, your journey towards building a great future and that of those around you have begun. You are requested to log into your account and update your account information with valid details including a working phone number, email, physical address, bank details, and nominee details. \n\n Below are a few important pointers that may help you:\n\n -Do not reveal your Password or your Transaction Password to anyone;\n -MLMPro will never ask you for the above either;\n -The name on your account should match the bank details for withdrawals (except for few exceptions);\n -Give minimum of 24-48hours for your Withdrawal Requests to be processed (delays may be from the bank);\n -Always go through our website, blog, social media pages, and broadcasted messages to be in tune with updates;\n -Your business is about teamwork.\n\nIf you will require help, please contact the Admin using the Contact Us form on the website or Open a Ticket on your dashboard (recommended), or send an email to admin@MLMPro.com.\n\nWishing you success\n\n\nTeam MLMPro\nwww.MLMPro.com\n";

                                $mail=sendmail2($to,$msg,$subject);                              
                           }
                            
                            header('Location: /index.php');
                        }else{
                            echo "<p>Error inserting record, pls try again</p>";
                        }
                    }
                }else{
                    include $_SERVER['DOCUMENT_ROOT'] . '/includes/registrationform.inc.php';
                }
            ?>      
            
        <div id="footer">
            <h2>Footer</h2>
        </div><!--footer-->
        <script type="text/javascript" src="/js/jquery-1.7.1.min.js"></script>
        <!--<script src='/js/mlm.js'> </script>-->

        <script type="text/javascript">
            
            $(document).ready(function(){
                //alert("Hellow");

                /*$("#username").on("blur",function(){
                    var profileid=$('#sponsorid').val();
                    alert(profileid);
                    var data='profileid='+profileid;
                    $.ajax({
                        url:'/scripts/populateparent.php',
                        type: 'POST',
                        data : data,
                        //beforeSend
                        success: function(response){
                            $('#parentid').html(response);
                        }
                    })
                })*/

                $("#sponsorid").on("blur",function(){
                    var sponsorid=$(this).val();
                    
                    var data='sponsorid='+sponsorid;
                    $.ajax({
                        url:'/scripts/sponsorid.php',
                        type: 'POST',
                        data : data,
                        //beforeSend: function() {
                        //    $('#sponsoridErr').text('Searching... Pls wait...');
                        //},
                        success: function(response){
                            $('#sponsoridErr').html(response);
                        }
                    })
                })


                $(".registrationform #checksponsor").on('change',function(){
                    if(this.checked){
                        //$('#sponsorid').val('RL20180300001');
                        //$('input#sponsorid').prop({'readonly':true});
                        //var data='sponsorid='+sponsorid;
                        $.ajax({
                            url:'/scripts/adminsponsor.php',
                            //type: 'POST',
                            //data : data,
                            beforeSend: function() {
                                $('#sponsoridErr').text('Searching... Pls wait...');
                            },
                            success: function(response){
                                $('#sponsorid').val(response);
                                 $('#sponsoridErr').text('');
                            }
                        })
                    }else{
                        $('#sponsorid').val('');
                    }
                })
                
            });
        </script>
    </body>
</html>

