<?php

// define variables and set to empty values
$nameerrors = $emailerrors = $phoneerrors = $companyerrors = $designationerrors = $subjecterrors = $messageerrors = "";


$name = $phone = $emailFrom = $designation = $company = $subj = $msg =  $success = "";


if(isset($_POST['submit'])){
  if($_SERVER["REQUEST_METHOD"] == "POST"){
    if (empty($_POST["your-name"])){
      $nameerrors ="Please enter your name";
    }else{
      $name = test_input($_POST["your-name"]);
      if(!preg_match("/^[a-zA-Z0-9 ]*$/",$name)){
        $nameerrors="No Special Characters Allowed";
      }
    }
  
  
  if (empty($_POST["your-phone"])){
    $phoneerrors ="Please enter your number";
  }else{
    $phone = test_input($_POST["your-phone"]);
    if(!preg_match("/^[0-9]*$/",$phone)){
      $phoneerrors="Only Numbers Allowed";
    }
   
  }
  
  if (empty($_POST["your-email"])){
    $emailerrors ="Please enter your email";
  }else{
    $emailFrom = test_input($_POST["your-email"]);
    if(!filter_var($emailFrom , FILTER_VALIDATE_EMAIL)){
      $emailerrors="Enter Valid Email";
    }
  }
  
  if (empty($_POST["your-designation"])){
    $designationerrors ="Please enter your designation";
  }else{
    $designation = test_input($_POST["your-designation"]);
    if(!preg_match("/^[a-zA-Z0-9 ]*$/",$designation)){
      $designationerrors="No Special Characters Allowed";
    }
    }
  
    if (empty($_POST["your-company"])){
      $companyerrors ="Please enter your company";
    }else{
      $company = test_input($_POST["your-company"]);
      if(!preg_match("/^[a-zA-Z0-9 ]*$/",$company)){
        $companyerrors="No Special Characters Allowed";
      }
      }
  
      if (empty($_POST["your-subject"])){
          $subjecterrors ="Please enter your subject";
        }else{
          $subj = test_input($_POST["your-subject"]);
          if(!preg_match("/^[a-zA-Z0-9 ]*$/",$subj)){
            $subjecterrors="No Special Characters Allowed";
          }
          }
      
  if (empty($_POST["your-message"])){
    $messageerrors ="Write your message";
  }else{
    $msg = test_input($_POST["your-message"]);
  /*  if(!preg_match("/^[a-zA-Z0-9\_]{2,20}/",$msg)){
      $messageerrors="Enter Valid Message";
    }*/
  }
  
  if ($nameerrors == '' and $phoneerrors=='' and $emailerrors=='' and $designationerrors=='' and $companyerrors=='' and $subjecterrors==''and $messageerrors==''){
        $message_body='';
        unset($_POST['submit']);
        foreach ($_POST as $key => $value){
          $message_body .= "$key: $value\n";
        }
        $from = "From:Enquiry from COSM Work with Us Page!"; //Site name
        // Change this to your email address you want to form sent to
        $to = "info@co-sm.co"; 
        $subject = "Enquiry from " . $name . " for COSM ";
        $message = "Following are the details :
        \n
        Name: $name
        Company: $company
        Designation: $designation 
        Phone: $phone 
        Email:  $emailFrom
        Subject: $subj
        Message: $msg 
        \n";
        if ( mail($to,$subject,$message,$from)){
          $success ="Thank you for sharing your details. Our team will get back to you shortly!";
          $name = $phone = $emailFrom = $designation = $company = $subj = $msg = '';
        }
      }
  
  }
  }

  function test_input($data){
    $data =trim($data);
    $data =stripslashes($data);
    $data =htmlspecialchars($data);
    return $data;
  }

  
  ?>