// Creates a hash of the data entered and compares it to the database. If there is a match, then assume that this is a duplicate record
// Adapted from code found at http://crosstec.de/support/index.php?pid=knowledgebase&cmd=viewentclient&id=227&PHPSESSID=5d6277f32e88af01aa2c4ecd20df6ce3

// Load the standard Facile Forms library
$this->execPieceByName('ff_InitLib');

// Get the fields that create the hash (userID,company,first,last)
$user =& JFactory::getUser();
$frm_userID = $user->id;
$frm_company = ff_getSubmit('txtCompany');
$frm_first = ff_getSubmit('txtFirstName');
$frm_last = ff_getSubmit('txtLastName');

// Create a hash of the values by appending them together and the running md5
$hash = $frm_userID .$frm_company .$frm_first .$frm_last;
$hash = md5(strtolower($hash));

// Check the database to see if this hash exists
$sql = "SELECT count(user_id) AS count FROM joomla_sales_reps WHERE unique_id = '" .$hash ."'";
$dupe_check = ff_select($sql);

// If the SQL count comes back greater than one, assume that this is a duplicate
if ($dupe_check[0]->count > 0) {  

    // set a custom error code
    $this->status = 1001;

    // set message for submitted script
    $this->message = 'This company has already been entered into the directory. Please enter a new company, or edit the company that already exists to make changes.';

    // stop processing begin submit piece
    return;
}