<?php

require_once("shared.php");

function nameTaken($charname){
  $nameUnavailable = '{ "nameTaken": true }';
  $nameAvailable   = '{ "nameTaken": false }';
  global $dbconn;  
  $strSQL = "SELECT charname FROM chars WHERE charname = :charname";

  $statement = $dbconn->prepare($strSQL);  
  $statement->bindValue(':charname',$charname);

  if (!$statement->execute()) {
    watchdog($statement->errorInfo(),'SQL');
    return $nameUnavailable;
  }
  else {
    $arrReturn = $statement->fetchAll();
    
    if (empty($arrReturn)) {
      return $nameAvailable;
    }
    else {
      return $nameUnavailable;
    }
  }
}

if (isset($_POST['name'])) {
  $name = $_POST['name'];
  echo nameTaken($name);
}
else
{
  echo '{ "nameTaken": true }';
}

?>
