--- unique_field.module	2009-03-07 09:50:14.000000000 +0800
+++ ../unique_field/unique_field.module	2009-03-31 16:33:17.000000000 +0800
@@ -36,6 +36,19 @@
 define('UNIQUE_FIELD_SHOW_MATCHES', 'show_matches');
 
 /**
+ * Implementation of hook_menu()
+ */
+function unique_field_menu() {
+  $items['uniquefield'] = array(
+    'title' => 'Check unique title field ',
+    'page callback' => 'unique_field_ahah',
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+  return $items;
+}
+
+/**
  * Implementation of hook_perm().
  */
 function unique_field_perm() {
@@ -49,6 +62,81 @@
   if ($form_id == 'node_type_form' && user_access(UNIQUE_FIELD_PERM) && isset($form['#node_type'])) {
     unique_field_node_settings_form($form);
   }
+  if (preg_match('/^(.*)_node_form$/', $form_id, $bits)) {
+    $nodetype = $bits[1];
+    $fields = variable_get('unique_field_fields_'. $nodetype, array());
+
+    //let's check if the node-type need to check the title field
+    if (in_array('title', $fields)) {
+      drupal_add_css(drupal_get_path('module', 'unique_field_realtime') ."/unique_field_realtime.css");
+      $form['warning'] = array(
+        '#type' => 'hidden',
+        '#prefix' => '<div id="warning-uniquefield">',
+        '#suffix' => '</div>',
+        '#weight' => -10,
+      );
+      //we add AHAH in the form title
+      $form['title'] += array(
+        '#ahah' => array(
+          'wrapper' => 'warning-uniquefield',
+          'event' => 'change',
+          'path' => 'uniquefield',
+          'effect' => 'fade',
+        ),
+      );
+    }
+  }
+}
+
+/**
+ * Form AHAH callback
+ */
+function unique_field_ahah() {
+  $content_type = $_POST['form_id'];
+
+  if (preg_match('/^(.*)_node_form$/', $content_type, $bits)) {
+    $content_type = $bits[1];
+    $fields = variable_get('unique_field_fields_'. $content_type, array());
+
+    if (in_array('title', $fields)) {
+      // Get the form from the cache.
+      $form_state = array('storage' => NULL, 'submitted' => FALSE);
+      $form_build_id = $_POST['form_build_id'];
+      $form = form_get_cache($form_build_id, $form_state);
+
+      $scope = variable_get('unique_field_scope_'. $content_type, UNIQUE_FIELD_SCOPE_TYPE);
+      $title = $_POST['title']; //current inputted title
+      $msg = '';
+
+      if ($scope == UNIQUE_FIELD_SCOPE_TYPE) {
+        $result = db_query("SELECT COUNT(*) FROM {node} WHERE title = '%s' AND type = '%s'", $title, $content_type);
+      }
+      else {
+        $result = db_query("SELECT COUNT(*) FROM {node} WHERE title = '%s'", $title);
+      }
+
+      $num = db_result($result);
+
+      if ($num > 0) {
+        $node = strcmp($scope, UNIQUE_FIELD_SCOPE_TYPE) ? node_load(array('title' => $title)) : node_load(array('title' => $title, 'type' => $content_type));
+        //we check if we're in the edit page
+        if (isset($form['nid']['#value'])) {
+          //let's check if the current node title is the same with the new inputted title
+          // if TRUE, just do nothing
+          if (strtoupper($form['title']['#default_value']) == strtoupper($title)) {
+            $msg = '';
+          }
+          else {
+            $msg = t('*The Title field has a value that is already used. !link', array('!link' => l('view the '. $content_type, "node/". $node->nid)));
+          }
+        }
+        else {
+          $msg = t('*The Title field has a value that is already used. !link', array('!link' => l('view the '. $content_type, "node/". $node->nid)));
+        }
+      }
+      drupal_json(array('status' => TRUE, 'data' => $msg));
+    }
+  }
 }
 
 /**
