<?php
//set default values
$name = '';
$email = '';
$phone = '';
$message = 'Enter some data and click on the Submit button.';

//process
$action = filter_input(INPUT_POST, 'action');

switch ($action) {
    case 'process_data':
        $name = filter_input(INPUT_POST, 'name');
        $email = filter_input(INPUT_POST, 'email');
        $phone = filter_input(INPUT_POST, 'phone');
        //remove white space from input//
        $name = trim($name);
        $name = strtolower($name);
        $email = trim($email);
        $phone = trim($phone);
        /*************************************************
         * validate and process the name
         ************************************************/
        // 1. make sure the user enters a name
        if (empty($name)){
            $message = "Please make sure you entered a name";
        break;
        }
        // 2. display the name with only the first letter capitalized
        $name = ucwords($name);
        //Separate names//
        $i =strpos($name,' ');
        if ($i === false){
            $Fname = $name;
        } else {
            $Fname = substr($name,0,$i);
            $Lname = substr($name,$i +1);
        }
        /*************************************************
         * validate and process the email address
         ************************************************/
        // 1. make sure the user enters an email
        if (empty($email)){
            $message = "Please enter an Email Address";
        break; 
        // 2. make sure the email address has at least one @ sign and one dot character
        } elseif (strpos($email,'@')===false OR strpos($email,'.')=== false) {
            $message ="Please make sure you entered a Valid Email";
        break;
        }
        /*************************************************
         * validate and process the phone number
         ************************************************/
        // 1. make sure the user enters at least seven digits, not including formatting characters
        // remove formatting of the number//
        $phone=str_replace('-','',$phone);
        $phone=str_replace(' ','',$phone);
        if (strlen($phone)<7 or strlen($phone)>10){
            $message =" Please make sure that the number is 7 numbers without area code or\n 10 numbers with area code";
        break;
        }
        if (strlen($phone)==7) {
            $fpart = substr($phone,0,3);
            $lpart = substr($phone,3);
            // 2. format the phone number like this 123-4567 
            $phone = $fpart.'-'.$lpart;
        }elseif (strlen($phone)==10) {
            $arcode = substr($phone,0,3);
            $fpart = substr($phone,3,3);
            $lpart =substr($phone,6);
            // 2. format the phone number like this  123-456-7890
            $phone = $arcode.'-'.$fpart.'-'.$lpart;
        }
        


        /*************************************************
         * Display the validation message
         ************************************************/
        $message = "Hello $Fname, \n
                    Thank you for entering this data \n
                    Name: $name \n
                    Email:$email \n
                    Phone Number:$phone";
        break;
}
include 'string_tester.php';
?>