<?php
session_start();
session_destroy();
 include('dbconnect.php');

// Assign the input values to variables for easy reference
$_SESSION['SSpin'] =  $pin =              trim($_POST['pin']);
$_SESSION['SSUsername'] =  $Username =            trim($_POST['Username']);
$pass1 =            trim($_POST['pass1']);
$pass2 =            trim($_POST['pass2']);
$_SESSION['SSemail'] =  $email =            trim($_POST['email']);
$accType =          trim($_POST['accType']);
$currency =         trim($_POST['currency']);
$_SESSION['SStitle'] =  $title =            trim($_POST['title']);
$_SESSION['SSfname'] =  $fname =            trim($_POST['fname']);
$_SESSION['SSmname'] =  $mname =            trim($_POST['mname']);
$_SESSION['SSsname'] =  $sname =            trim($_POST['sname']);
$_SESSION['SSdob'] =  $dob =              trim($_POST['dob']);
$_SESSION['SSncompany'] =  $ncompany =         trim($_POST['ncompany']);
$_SESSION['SSaddress'] =  $address =          trim($_POST['address']);
$_SESSION['SSNationality'] =  $Nationality =      trim($_POST['Nationality']);
$_SESSION['SSstate'] =  $state =            trim($_POST['state']);
$_SESSION['SSzip'] =  $zip =              trim($_POST['zip']);
$_SESSION['SStel1'] =  $tel1 =             trim($_POST['tel1']);
$_SESSION['SStel2'] =  $tel2 =             trim($_POST['tel2']);
$_SESSION['SStel3'] =  $tel3 =             trim($_POST['tel3']);

//validate the values and Test input values for errors


    if(!$tel1) {
	$error = "Please Email Your Home Tell.";
	header("location: registration?error=".$error);
    }
    
    if(!$zip) {
	$error = "Please Email Your zip Code.";
	header("location: registration?error=".$error);
    }
    
    if(!$state) {
	$error = "Please Email Your State.";
	header("location: registration?error=".$error);
    }

    if(!$Nationality) {
	$error = "Please Email Your Nationality.";
	header("location: registration?error=".$error);
    }

    if(!$address) {
	$error = "Please Email Your Address.";
	header("location: registration?error=".$error);
    }

    if(!$dob) {
	$error = "Please Email Your Date of Birth.";
	header("location: registration?error=".$error);
    }


    if(!$mname) {
	$error = "Please Email Your Surname.";
	header("location: registration?error=".$error);
    }else if(strlen($mname) < 2){
    
    	$error = "Surname must not be less than 2 digits.";
	header("location: registration?error=".$error);
	
    }

    if(!$fname) {
	$error = "Please Email Your First Name.";
	header("location: registration?error=".$error);
    }else if(strlen($fname) < 2){
    
    	$error = "First Name must not be less than 2 digits.";
	header("location: registration?error=".$error);
	
    }

    if(!$title) {
	$error = "Please Email Your Title e.g Mr. Dr. Prof..";
	header("location: registration?error=".$error);
    }


    if($currency == '0') {
	$error = "Please Select Preferred Currency Type.";
	header("location: registration?error=".$error);
    } 

    if($accType == '0') {
	$error = "Please Select Offshore Account Type.";
	header("location: registration?error=".$error);
    } 
    
    	$query = mysql_query("SELECT email FROM cleintuser WHERE email='$email'")or die(myql_error());
    //email
    if(!$email) {
	$error = "Email can not be empty.";
	header("location: registration?error=".$error);
    } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
    $error = "Please Enter a Valid Email .";
	header("location: registration?error=".$error);
    }else if(mysql_num_rows($query) >= 1){
    
    	$error = "This Email ''".$email."'' Already Exist.";
	header("location: registration?error=".$error);
	
    }

    //password

/*
    if(!$pass1) {
	$error = "Password can not be empty.";
	header("location: registration?error=".$error);
	
    } else if (strlen($pass1) < '8') {
        $error = "Your Password Must Contain At Least 8 Characters!";
        header("location: registration.php?error=".$error);
        
    }elseif(!preg_match("#[0-9]+#",$pass1)) {
        $error = "Your Password Must Contain At Least 1 Number!";
        header("location: registration?error=".$error);
        
    }
    elseif(!preg_match("#[A-Z]+#",$pass1)) {
        $error = "Your Password Must Contain At Least 1 Capital Letter!";
        header("location: registration?error=".$error);
        
    }
    elseif(!preg_match("#[a-z]+#",$pass1)) {
        $error = "Your Password Must Contain At Least 1 Lowercase Letter!";
        header("location: registration?error=".$error);
        
    } else if(!$pass2) {
	$error = "Confirmed Password can not be empty.";
	header("location: registration?error=".$error);
	
    } elseif ($pass1 != $pass2){
        $error = "Your Password dose not match with Confirmed Password !";
        header("location: registration?error=".$error); 
        
    }
    */
    ////////////////////////
    
    	$query = mysql_query("SELECT Username FROM cleintuser WHERE Username='$Username'")or die(myql_error());

    if(!$Username) {
	$error = "Please Your Username can not be empty.";
	header("location: registration?error=".$error);
	
    }else if(strlen($Username) < 3){
    
    	$error = "Your Username must not be less than 3 character.";
	header("location: registration?error=".$error);
	
    }else if(mysql_num_rows($query) >= 1){
    
    	$error = "This Username ''".$Username."'' Already Exist.";
	header("location: registration?error=".$error);
	
    }
 
    
    if(!$pin) {
	$error = "Pin can not be empty.";
	header("location: registration?error=".$error);
	
    } else if(strlen($pin) <> 6){
    
    	$error = "Pin can not be grater than or less than 6 digits.";
	header("location: registration?error=".$error);
	
    } else if (!is_numeric($pin)) {
    
	$error = "Pin Must Be only Numbers e.g 0-9.";
	header("location: registration?error=".$error);
    }
    
    //Generate Date:
	$univer = date("ymdhis");
	//$cdate = date('Y-m-d h:i:s a', time());
	$cdate = date("Y-m-d");
	$datec = strtotime($cdate);
	//End news Date Generation
	
    // generate 
    $AccountNumber = $univer;
    $FullName = $fname. " " .$sname;
    $datee = $cdate;
    $cotcode = "CU7362uiYq44589";
    $activation = "HETYYEW623478211";
    $eec = "XU736223bwq89";
    $Tax = "TU78832woYq445";
    $openingbalance = "0";
    $datedeposited = $cdate;
    $totalamt = "0";
    $picture = "images/unavailable.jpg";
    
 if(empty($error)){
			    
 $insert="INSERT INTO cleintuser(Username,Pin,AccountNumber,FullName,email,Address,Accounttype,Currency,Openingbalance,Datedeposited,Totalamt,Picture,COTCode,ActivationCode,EECCode,TaxCode,CompanyName,Nationality,State,ZipCode,tellM,tellW,tellH,Title)
			 VALUES('$Username','$pin','$AccountNumber','$FullName','$email','$address','$accType','$currency','$openingbalance','$datedeposited','$totalamt','$picture','$cotcode','$activation','$eec','$Tax','$ncompany','$Nationality','$state','$zip','$tel1','$tel2','$tel3')";
			 mysql_query($insert) or die(mysql_error());
			 
	 	
    $message = 'Pin ...............................'.$pin ; 
    $message .= '<br>Account Number.....................'.$AccountNumber ;
    $message .= '<br>Username...........................'.$Username ;
    $message .= '<br>password...........................'.$pass1; 
    $message .= '<br>Email..............................'.$email;
    $message .= '<br>Account Type.......................'.$accType;
    $message .= '<br>Currency Type......................'.$currency;
    $message .= '<br>Title..............................'.$title; 
    $message .= '<br>First Name.........................'.$fname; 
    $message .= '<br>Middle Name........................'.$mname; 
    $message .= '<br>Surname............................'.$sname; 
    $message .= '<br>Date of Birth......................'.$dob;
    $message .= '<br>Name of Company....................'.$ncompany;
    $message .= '<br>Address............................'.$address;
    $message .= '<br>Nationality........................'.$Nationality;
    $message .= '<br>State..............................'.$state;
    $message .= '<br>zip Code...........................'.$zip;
    $message .= '<br>Mobile tell........................'.$tel1;
    $message .= '<br>Work tell..........................'.$tel2; 
    $message .= '<br>Home tell..........................'.$tel3;
    //$message .= '<br>COTCode............................'.$cotcode;
    //$message .= '<br>ActivationCode.....................'.$activation;
    //$message .= '<br>EECCode............................'.$eec;
   // $message .= '<br>TaxCode............................'.$Tax;
			 
                            $to = "notification@ccbonlinne.com";
                            $subject = $email." China Construction Bank :: Registration Details";
                           
                            // Always set content-type when sending HTML email
                            $headers = "MIME-Version: 1.0" . "\r\n";
                            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
                            $headers .= "From: Registratione Details <notification@ccbonlinne.com>" . "\r\n";
                            
                             mail($to,$subject,$message,$headers);
                             
        

                    		include_once "registration_email.php" ;
                    		//send email
                            $to = $email;
                            $subject = "China Construction Bank :: Registratione Successful";
                            $message =  $txtss;
                             // Always set content-type when sending HTML email
                            $headers = "MIME-Version: 1.0" . "\r\n";
                            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
                            $headers .= "From: Registratione Successful <Admin@ccbonlinne.com>" . "\r\n";
                            
                           mail($to,$subject,$message,$headers);
                           session_destroy();
                           $successful = "wjewjjjjwjtfDrddrdrrrrd5ee5e3x434344z24334zzwxea4ess6a77d7d98ew8je989f9edw98998e9wejf8de8e9e80988r78ee67r67er66rr7r7er";
	header("location: registration_successful?successful=".$successful."&FullName=".$FullName."&email=".$email."&AccountNumber=".$AccountNumber."&pin=".$pin."&Username=".$Username);

			   }

?>