--- dependent.info	2008-05-31 14:10:05.000000000 -0400
+++ dependent.info	2009-04-09 10:25:35.000000000 -0400
@@ -1,12 +1,6 @@
-; $Id: dependent.info,v 1.1 2008/03/07 09:42:10 levyofi Exp $
+; $Id: $
 name = Dependent
 description = Make CCK fields dependent of the values of other fields.
-dependencies = content
+dependencies[] = content
 package = CCK
-
-
-; Information added by drupal.org packaging script on 2008-05-31
-version = "5.x-1.4-beta"
-project = "dependent"
-datestamp = "1212257405"
-
+core = 6.x
\ No newline at end of file
--- dependent.install	2008-05-31 14:02:25.000000000 -0400
+++ dependent.install	2009-04-09 10:25:35.000000000 -0400
@@ -1,27 +1,59 @@
 <?php
 // $Id: dependent.install,v 1.2 2008/05/31 18:02:25 levyofi Exp $
 
-function dependent_install() {
-  switch ($GLOBALS['db_type']) {
-    case 'mysqli':
-    case 'mysql':
-    case 'pgsql':
-      db_query("CREATE TABLE if not exists {field_dependencies} (
-        parent_field_name varchar(32) NOT NULL,
-        child_field_name varchar(32) NOT NULL,
-        parent_node_type_name varchar(32) NOT NULL,
-        child_node_type_name varchar(32) NOT NULL,
-        option_text varchar(255),
-        put_after_parent boolean default 0,
-        is_required boolean default 0,
-        PRIMARY KEY(parent_field_name,child_field_name,parent_node_type_name,child_node_type_name)                                         
-      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
-      break;
-       default:
-      break;
+/**
+ * Implementation of hook_schema().
+ */
+function dependent_schema() {
+  $schema['field_dependencies'] = array(
+      'fields' => array(
+           'parent_field_name' => array(
+             'type' => 'varchar',
+             'length' => '32',
+             'not null' => TRUE),
+           'child_field_name' => array(
+             'type' => 'varchar',
+             'length' => '32',
+             'not null' => TRUE),
+           'parent_node_type_name' => array(
+             'type' => 'varchar',
+             'length' => '32',
+             'not null' => TRUE),
+           'child_node_type_name' => array(
+             'type' => 'varchar',
+             'length' => '32',
+             'not null' => TRUE),
+           'option_text' => array(
+             'type' => 'varchar',
+             'length' => '255',
+             'not null' => FALSE),
+           'put_after_parent' => array(
+             'type' => 'int',
+             'size' => 'tiny',
+             'not null' => FALSE,
+             'default' => 0,
+             'disp-width' => '1'),
+           'is_required' => array(
+             'type' => 'int',
+             'size' => 'tiny',
+             'not null' => FALSE,
+             'default' => 0,
+             'disp-width' => '1')),
+      'primary key' => array('parent_field_name', 'child_field_name', 'parent_node_type_name', 'child_node_type_name'),
+  );
+  return $schema;
   }
+
+/**
+ * Implementation of hook_install().
+ */
+function dependent_install() {
+  drupal_install_schema('dependent');
 }  
 
+/**
+ * Implementation of hook_uninstall().
+ */
 function dependent_uninstall() {
-   db_query("DROP TABLE {field_dependencies}");
+  drupal_uninstall_schema('dependent');
 }
\ No newline at end of file
--- dependent.js	2008-05-31 14:02:25.000000000 -0400
+++ dependent.js	2009-04-09 12:30:57.000000000 -0400
@@ -1,235 +1,63 @@
-//if (Drupal.jsEnabled) {
-    var table= new Array();    
-    var i=0;
-    var parent_num=1;
-    var table_count=0;
-    var parent_forms=new Array(1);             
-    $(document).ready(function(){  
-    //debugger;    
-      $("div.this_is_a_parent", "#node-form").each(function(){                         
-          var field_hidden_text=$(this).html().split(' ')[0];     //the hidden text contains details needed to determine the children such as the field type name of the parent                                       
-          var the_parent_form=$(this).prev();          
-          $(the_parent_form).each(function(){                  
-            var parent_form_item=get_prev_form_item($(this));     //get the real parent form-item the '.this_is_a_parent' element is after the form-item element                                                                  
-            var has_attribute_parent_num=$(parent_form_item).attr('parent_num');
-            if ((($(parent_form_item).attr('parent_num'))===null)||(typeof ($(parent_form_item).attr('parent_num'))=="undefined")){ //check if this dom element has been inserted to the parent_forms array
-              $(parent_form_item).attr('parent_num',parent_num);  //set the parent number attribute                          
-              parent_forms.push(new Array());                          
-              parent_forms[parent_num]["children_arr"]=new Array();
-              parent_forms[parent_num]["parents_arr"]=new Array();
-              parent_forms[parent_num++]["parent_form"]=$(parent_form_item);
-            }
-            //the next code is for setting the checkboxes/radios hide/show options            
-            if ($(parent_form_item).find(':radio,.form-checkboxes').size()){ //the next code is for setting the radios/checkboxes groups hide/show options
-              $(parent_form_item).find(':radio,:checkbox').each(function (){//go through every radio/checkbox button in the parent form-item                    
-                 var parent_checked=$(this).attr('checked')?1:0;            //get the state of the radio/checkbox
-                 var _button=$(this);                 
-                 var txt='option_text='+$(_button).val();
-                 var parent_id=($(parent_form_item).attr('parent_num'));
-                 $(_button).attr('parent_num',parent_id);                     //set the parent number of this checkbox/radio button                 
-                 var $childs = $('div.child_of_'+field_hidden_text);          //find all child elements of this form-item
-                 $childs.each(function(){                                     //go through all the dependent elements and hide/show them according to the state of the radio/checkbox button                          
-                    if ($(this).html().split('option_text=')[1]==$(_button).val()){
-                    //this specific checkbox/radio button is the parent of this field
-                      var new_row={"form_item":$(parent_form_item),"parent":$(_button), "child":get_prev_form_item($(this))};
-                      var in_array=$(this).attr('in_array');
-                      if ((in_array===null)||(typeof in_array=="undefined")){//these 'if' makes sure that there are no duplicates in the table                        
-                        var parent_num_attr=$(parent_form_item).attr('parent_num');                                             
-                        parent_forms[parent_id]["children_arr"].push(get_prev_form_item($(this)));
-                        parent_forms[parent_id]["parents_arr"].push($(_button));
-                        parent_forms[parent_id]["parent_form"].attr('is_selection',0);
-                        table[table_count++]=new_row;
-                        $(this).attr('in_array',1);                        
-                        if ($(this).html().indexOf('put_after_parent')>0){
-                        //children that need to be put after their parents are only moved after the table is complete, so they are being signed for future recognition
-                           get_prev_form_item($(this)).attr('put_after_parent',1);
-                        }                              
-                      }
-                    }                    
-                 });                        
-                 $(this).click(function(){//whenever a click event occurs - go through all radio buttons, check their state and update the document 
-                                          //this is important because in radio buttons choosing one option sets the previously selected radio button as unselected
-                    var click_parent_num=$(this).attr('parent_num');                                                                  
-                    if ((click_parent_num!==null)&&(typeof click_parent_num!="undefined")) {
-                      var parentNum=parseInt($(this).attr('parent_num'));
-                      show_hide_element(parent_forms[parseInt($(this).attr('parent_num'))]["parent_form"]);                                            
-                    }
-                 });
-              });                     
-            }
-            else if ($(parent_form_item).find(':checkbox').size()){//the form item input is from type single on/off checkbox                                     
-              var parent_checked=$(parent_form_item).find(':checkbox').attr('checked')?1:0;//get the check state of the checkbox                                                
-              $('div.child_of_'+field_hidden_text).each(function(){              
-                var _button=$(parent_form_item).find(':checkbox')
-                var new_row={"form_item":$(parent_form_item),"parent":$(_button), "child":get_prev_form_item($(this))};
-                var in_array=$(this).attr('in_array');
-                if ((in_array===null)||(typeof in_array=="undefined")){                
-                  var parent_id=($(parent_form_item).attr('parent_num'));
-                  $(_button).attr('parent_num',parent_id);
-                  parent_forms[parent_id]["children_arr"].push(get_prev_form_item($(this)));
-                  parent_forms[parent_id]["parents_arr"].push($(_button));
-                  parent_forms[parent_id]["parent_form"].attr('is_selection',0);
-                  table[table_count++]=new_row;
-                  $(this).attr('in_array',1);                  
-                  if ($(this).html().indexOf('put_after_parent')>0){
-                    get_prev_form_item($(this)).attr('put_after_parent',1);
-                  }                             
-                }
-              });                    
-              $(parent_form_item).find(':checkbox').click(function(){//set the click event of the checkbox                                     
-                show_hide_element(parent_forms[parseInt($(this).attr('parent_num'))]["parent_form"]);                 
-            	});
-            } 
-            else if ($(parent_form_item).find('select').size()){
-              var selection_form_item=$(this);
-              var selection_field=$(parent_form_item).find('select');
-              var selectedIndex=selection_field[0].selectedIndex;
-              var options=selection_field[0].options;
-              var $childs = $('div.child_of_'+field_hidden_text);          //find all child elements of this form-item
-              $childs.each(function(){                                     //go through all the dependent elements and hide/show them according to the state of the radio/checkbox button                                          
-                  var new_row={"form_item":$(parent_form_item),"parent":$(selection_field), "child":get_prev_form_item($(this))};
-                  var in_array=$(this).attr('in_array');
-                  var parent_id=($(parent_form_item).attr('parent_num')); 
-                  if ((in_array===null)||(typeof in_array=="undefined")){//these 'if' makes sure that there are no duplicates in the table                        
-                    var parent_num_attr=$(parent_form_item).attr('parent_num'); 
-                    var child=get_prev_form_item($(this));                                            
-                    parent_forms[parent_id]["children_arr"].push($(child));
-                    parent_forms[parent_id]["parents_arr"].push($(selection_field));
-                    parent_forms[parent_id]["parent_form"].attr('is_selection',1);
-                    table[table_count++]=new_row;
-                    $(this).attr('in_array',1);
-                    var option_text=$(this).html().split('option_text=')[1];
-                    var option_index;
-                    for (var i=0; i<options.length; ++i){
-                      if (options[i].text==option_text)
-                        option_index=i;
-                    }
-                    $(child).attr('option_index',option_index);
-                    $(selection_field).attr('parent_num',parent_id);                        
-                    if ($(this).html().indexOf('put_after_parent')>0){
-                    //children that need to be put after their parents are only moved after the table is complete, so they are being signed for future recognition
-                       get_prev_form_item($(this)).attr('put_after_parent',1);
-                    }                              
+Drupal.behaviors.dependentFields = function(context) {
+  if(!Drupal.settings.dependentfield || !$(Drupal.settings.dependentfield).length) {
+    return;
                   }
+  jQuery.each(Drupal.settings.dependentfield, function(key, value) {
+    if(Drupal.settings.dependentvalue && Drupal.settings.dependentvalue[key]) {
+      // This is a select element
+      $('select[name^='+key+']').selCheckDependentFields();
+      $('select[name^='+key+']').bind('change', function() {
+        $(this).selCheckDependentFields();
                 });
-                $(selection_field).change(function(){
-                  show_hide_element(parent_forms[parseInt($(this).attr('parent_num'))]["parent_form"]);                 
+      
+    } else {
+      // This is a checkbox
+      $('input[name^='+key+']').cbCheckDependentFields();
+      $('input[name^='+key+']').bind('change', function() {
+        $(this).cbCheckDependentFields()
                 });                    
               } 
-              //debugger;  
-           // }                  
-          });
-      });
-      $('#autosave-status #view a').click(function() {
-        show_hide_elements();
       });   
-      arrange_children();      
-      show_hide_elements();                              	
-    });                        
-   // }); 
-     
-function hide_show_childs_by_element(childElement,checked, parent){    
-//this function gets a sign element, a button state and the parent of the dependent element
-//in order to get the dependent element get_prev_form_item is used
-//the parent element is used in order to hide/show the dependent element accroding to the visibility of the parent
-//if the parent is hidden the dependent elements should also be hidden.        
-      
-    if (!parent)
-      return;
-    
-    //get the visivility of the parent, if not set then set it as "true"
-    var parent_visible=$(parent).attr('_visible');
-    if ((parent_visible===null)||(typeof parent_visible=="undefined")){
-      $(parent).attr('_visible',"true");
-      parent_visible="true"; 
     }
     
-    //get the child element and check its visibility. if not set then set it and all 
-    //its children(important because radio-button are form-item children of a parent form-item, the latter is the element that actually being shown/hidden) 
-    //form-item elements as true
-    //var childElement=get_prev_form_item($(childSignElement));//maybe should be in each clause
-    var child_visible= $(childElement).attr('_visible');
-    var childElementFormItems=$(childElement).find('div.form-item');
-    if ((child_visible===null)||(typeof child_visible=="undefined")){
-      $(childElementFormItems).attr('_visible',"true");
-      $(childElement).attr('_visible',"true");
-      child_visible="true"; 
-    } 
-    var change=0;    
-    //show/hide the child element and update its visible state
-    //aftert the update call hide_show_elements in order to update its own dependent elements 
-    var firstChild=$(childElement).find(":first");    
-    var firstChildIsFieldset= firstChild.is("fieldset"); //this means these form item is a related subform element
-    
-    if ((checked)&&(child_visible=="false")&&(parent_visible=="true")){
-      $(childElement).show('slow');
-      if (!firstChildIsFieldset)
-        $(childElementFormItems).attr('_visible',"true");
-      $(childElement).attr('_visible',"true");      
-      change=1;
-    }
-    else if (((!checked)||(parent_visible=="false"))&&(child_visible=="true")){
-      $(childElement).hide('slow');
-      if (!firstChildIsFieldset)
-        $(childElementFormItems).attr('_visible',"false");
-      $(childElement).attr('_visible',"false");      
-      change=1;
-    }    
-    if ((change==1)&&(!firstChildIsFieldset)){//
-      show_hide_element($(childElement));
-    }
+jQuery.fn.cbCheckDependentFields = function() {
+  $(this).checkDependentFields($(this).attr('checked'));
 }
 
-function get_prev_form_item(element){
-    while(!(($(element).is("div.form-item"))||($(element).is("fieldset")))){
-      element=element.prev();      
-    }
-    return element;
+jQuery.fn.selCheckDependentFields = function() {
+  var onoff = $(this).val() == Drupal.settings.dependentvalue[$(this).getCleanElementName()];
+  $(this).checkDependentFields(onoff);
 } 
 
-
-function get_parent_form_item(element){
-    while(!(($(element).is("div.form-item"))||($(element).is("fieldset")))){
-      element=element.parent();      
-    }
-    return element;
+jQuery.fn.checkDependentFields = function(onoff) {
+  var children = $(this).getDependentChildren();
+  $(this).toggleDependentFields(children, onoff);
 } 
 
-function show_hide_elements(){
-//this function is being called whenever a radio button is clicked or shown/hidden
-//it iterates through all elements that depend on a radio button 
-  for(var parent_form=1; parent_form<parent_forms.length; ++parent_form){    
-    show_hide_element(parent_forms[parent_form]["parent_form"]);
+jQuery.fn.getDependentChildren = function() {
+  var name = $(this).getCleanElementName();
+  var children = new Array();
+  if(typeof Drupal.settings.dependentfield[name] == 'string') {
+    var child_el = $('#'+Drupal.settings.dependentfield[name]);
+    children[0] = child_el;
+  } else {
+    jQuery.each(Drupal.settings.dependentfield[name], function(i, child) {
+      children[i] = $('#'+child);
+    });
   }
+  return children;
 }
 
-function show_hide_element(parent){
-//this function is being called whenever a radio button is clicked or shown/hidden
-//it iterates through all elements that depend on a radio button
-  var parent_id=$(parent).attr('parent_num');
-    if ((parent_id===null)||(typeof parent_id=="undefined")) 
-      return;
-    var parent_form=parent_forms[parseInt(parent_id)];
-    var number_of_children=parent_form["children_arr"].length;
-    for (var child=0; child<number_of_children; child++){
-      var checked=0;
-      //var selection_field=$(parent)
-      var sdkjh=typeof ($(parent).attr('is_selection'));    
-      if ($(parent).attr('is_selection') == 1){
-        var selection_field=parent_form["parents_arr"][child];
-        if (parent_form["children_arr"][child].attr('option_index')== selection_field[0].selectedIndex){
-          checked=1;
-        }
-      }
-      hide_show_childs_by_element(parent_form["children_arr"][child], parent_form["parents_arr"][child].attr('checked')?1:checked, parent_form["parent_form"]);   
-    }  
+jQuery.fn.getCleanElementName = function() {
+  return $(this).attr('name').replace('[value]', '');
 }
 
-function arrange_children(){   
-  for (var index=0; index<table.length; index++){
-    if (table[index]["child"].attr('put_after_parent')){
-      table[index]["child"].appendTo(get_parent_form_item($(table[index]["parent"]).parent()));
-      table[index]["child"].addClass("child"); 
-    }    
+jQuery.fn.toggleDependentFields = function(children, onoff) {
+  jQuery.each(children, function() {
+    if(onoff) {
+      $(this).slideDown();
+    } else {
+      $(this).slideUp();
   } 
+  });
 }
--- dependent.module	2008-05-31 14:02:25.000000000 -0400
+++ dependent.module	2009-04-09 12:37:56.000000000 -0400
@@ -1,11 +1,10 @@
 <?php
-
+// $Id$
   /*
- * Implementation of hook_form_alter()
+ * Implementation of hook_form_alter().
  */
-function dependent_form_alter($form_id, &$form) {
-  
-  if ($form_id == '_content_admin_field') {
+function dependent_form_alter(&$form, &$form_state, $form_id) {
+  if ($form_id == 'content_field_edit_form' || $form_id == '_content_admin_field') {
       //need to add our form element to the form where field types are created/updated
       
       //get the names of the related content type and the field type
@@ -14,34 +13,48 @@ function dependent_form_alter($form_id, 
       
       //fill the options list in which the field type can be dependent on
       //for now - thr list is filled with single on/off checkboxed and 2+ radio choices from the same content type only      
-      $options=dependent_create_form_options(array('parent'=>$content_type['type'], 'child'=>$content_type['type']) ,$form, $field_name);
+      $options = dependent_create_form_options(array(
+        'parent' => $content_type['type'],
+        'child' => $content_type['type']),
+        $form,
+        $field_name
+        );
       if ($options===FALSE){
         return $form;
-      }else{        
-        $form['#submit']['dependent_submition'] = array($options); //this is how we tell drupal that during submition the 'dependent_submition' function should be called               
+      }
+      else{
+        //this is how we tell drupal that during submition the
+        // 'dependent_submition' function should be called
+        // $form['#submit']['dependent_submition'] = array($options);
+        $form['#submit'][] = 'dependent_submition';
+        $form['#dependant_submition_options'] = array($options);
       } 
   }
   else if (strpos($form_id,'_node_form')){//this is a node form    
     $content_type = $form['type']['#value'];
     $dependencies=db_query("SELECT * FROM {field_dependencies} WHERE parent_node_type_name='%s'", $content_type);
-    if (db_num_rows($dependencies)>0){
+    $dependencies_switch = FALSE;
+    $form_dependencies = array();
+    while ($data = db_fetch_array($dependencies)) {
+      $dependencies_switch = TRUE;
+      $form_dependencies[] = $data;
+    }
+    if ($dependencies_switch > 0) {
       dependent_unset_required_before_validation($form_id, $form);
-      $form['#validate']['dependent_validate_required_fields']=array();
+      $form['#validate'][] = 'dependent_validate_required_fields';
     }     
-    while ($field=db_fetch_array($dependencies)){      //the while loop is to support future development where a field can be dependent on many parents
-      if (isset($form[$field['child_field_name']])){ 
-        if (isset($form[$field['child_field_name']]['#theme'])){
-          $form[$field['child_field_name']]['#prefix']='<div class="form-item">'.$form[$field['child_field_name']]['#prefix'];
-          $form[$field['child_field_name']]['#suffix'].=$form[$field['child_field_name']]['#suffix'].'</div>';
-        }                                                                                                           
-        $suffix=$form[$field['child_field_name']]['#suffix'].'<div class="child_of_'.$field['parent_field_name'].'" style="display: none">child of '.$field['parent_field_name'].($field['put_after_parent']?' put_after_parent':'').(($field['option_text']=='no_value_needed')?'':(' option_text='.$field['option_text'])).'</div>';//this string will help the javascript to hide/show this field
-        $form[$field['child_field_name']]['#suffix']=$suffix;
-      }
-      if ((isset($form[$field['parent_field_name']]))&&(strpos($form[$field['parent_field_name']]['#suffix'],"this_is_a_parent")===FALSE)){        
-        $suffix=$form[$field['parent_field_name']]['#suffix'].'<div class="this_is_a_parent" style="display: none">'.$field['parent_field_name'].(($field['option_text']=='no_value_needed')?'':(' parent_text='.$field['option_text'])).'</div>';
-        //$suffix='<div class="this_is_a_parent" style="display: none">'.$field['parent_field_name'].(($field['option_text']=='no_value_needed')?'':(' parent_text='.$field['option_text'])).'</div>';
-        $form[$field['parent_field_name']]['#suffix']=$suffix;         
+    foreach ($form_dependencies as $field) {      //the while loop is to support future development where a field can be dependent on many parents
+      $child_wrapper = 'wrapper-'.rand().'-'.$field['child_field_name'];
+      drupal_add_js(array('dependentfield' => array(
+        $field['parent_field_name'] => $child_wrapper
+        )),  'setting');
+      if($field['option_text'] != 'no_value_needed') {
+        drupal_add_js(array('dependentvalue' => array(
+          $field['parent_field_name'] => $field['option_text']
+          )), 'setting');
       }            
+      $form[$field['child_field_name']]['#prefix'] = '<div id="'.$child_wrapper.'">';
+      $form[$field['child_field_name']]['#suffix'] = '</div>';
     } 
     foreach ($form as $key=>$value){
       if (strpos($key,'field')!==FALSE){//this is a field
@@ -62,7 +75,7 @@ function dependent_form_alter($form_id, 
     drupal_add_js(drupal_get_path('module', 'dependent') .'/dependent.js');
   }
   else if ($form_id == '_content_admin_field_remove') { //a field removal page - need to update the database
-    $form['#submit']['dependent_content_admin_field_remove_submit'] = array();
+    $form['#submit'][] = 'dependent_content_admin_field_remove_submit';
   }
 }
 
@@ -71,29 +84,37 @@ function dependent_form_alter($form_id, 
  * this function is being called when the admin saves field settings
  * $options - an array with all the options that this field can depend on.
  */  
-function dependent_submition($form_id, $form_values, $options) {
-  if ($form_id == '_content_admin_field') {
+function dependent_submition($form, &$form_state) {
+  $options = $form['#dependant_submition_options'];
+  if ($form['#id'] == 'content-field-edit-form') {
   //this is a submition of adding or editing a field type      
-    if (isset($form_values['dependent_options'])){
+    if (isset($options)) {
       //get the names of the content type and field type
-      $content_type = $form_values['type_name'];
-      $field_name=$form_values['field_name'];
-      
+      $content_type = $form_state['values']['type_name'];
+      $field_name = $form_state['values']['field_name'];
       //check if this is an update operation
-      $is_updatable=db_num_rows(db_query("SELECT * FROM {field_dependencies} WHERE child_field_name='%s' AND child_node_type_name='%s'", $field_name, $content_type));
-      
+      $is_updatable = FALSE;
+      $is_updatable = db_result(db_query("SELECT COUNT(*) FROM {field_dependencies} WHERE child_field_name='%s' AND child_node_type_name='%s'", $field_name, $content_type));
       //get the selected choice 
-      $choice=strval($form_values['dependent_options']);
-      $option=explode(" option from ",$options[$choice]);
-      ($choice>0)?($selected_value=(count($option)>1)?$option[0]:'no_value_needed'):'';                         
-      ($choice>0)?$selected_field = ($option[1]?$option[1]:$options[$choice]): '';
-      
+      $choice = $form_state['values']['dependent_options'];
       //if this is an update delete previous database entries
       if ($is_updatable){
         db_query("DELETE FROM {field_dependencies} WHERE child_field_name='%s' AND child_node_type_name='%s'", $field_name, $content_type);            
       }        
+      if(!$choice) {
+        return;
+      }
+      if(strpos($options[0][$choice], ' option from ')) {
+        $option = explode(" option from ", $options[0][$choice]);
+        $selected_value = $option[0];
+        $selected_field = $option[1];
+      } else {
+        $selected_value = 'no_value_needed';
+        $selected_field = $options[0][$choice];
+      }
+
       //enter the choice to the database 
-      ($choice>0)?db_query("INSERT INTO {field_dependencies} VALUES ('%s','%s','%s','%s','%s',%s, %s)",$selected_field, $field_name, $content_type, $content_type, $selected_value, $form_values['put_after_parent'], $form_values['required']):'';        
+      db_query("INSERT INTO {field_dependencies} VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')", $selected_field, $field_name, $content_type, $content_type, $selected_value, $form_state['values']['put_after_parent'], $form_state['values']['required']);
     }   
   }
 }
@@ -102,16 +123,16 @@ function dependent_submition($form_id, $
  * this function is being called when the admin removes a field
  * $options - an array with all the options that this field can depend on.
  */ 
-function dependent_content_admin_field_remove_submit($form_id, $form_values) {
-  $content_type = $form_values['type_name'];
-  $field_name=$form_values['field_name'];
-  if ( $content_type && $field_name && $form_values['confirm']) {//remove this field dependencies data frm database
+function dependent_content_admin_field_remove_submit($form, &$form_state) {
+  $content_type = $form_state['values']['type_name'];
+  $field_name=$form_state['values']['field_name'];
+  if ( $content_type && $field_name && $form_state['values']['confirm']) {//remove this field dependencies data frm database
     db_query("DELETE FROM {field_dependencies} WHERE (child_field_name='%s' OR parent_field_name='%s') AND child_node_type_name='%s'", $field_name, $field_name, $content_type);            
   }
 }
 
 /*
- * Implementation of hook_form_alter()
+ * Implementation of hook_form_alter().
  */
 function dependent_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
   switch($op) {
@@ -122,11 +143,11 @@ function dependent_nodeapi(&$node, $op, 
          $value_needed=($field->option_text=='no_value_needed')?"1":$field->option_text;
          $parent_field=$field->parent_field_name;
          //check if $value_needed is in the array of $parent_field
-         $found=false;
+        $found=FALSE;
          $parent_content=$node->$parent_field;
          for($index=0; $index<count($node->$parent_field); ++$index){           
            if ($parent_content[$index]['value']==$value_needed){
-              $found=true;
+            $found=TRUE;
            }
          }
          if ((!isset($node->content[$parent_field]))||((!$found)&&(isset($node->content[$field->child_field_name])))){
@@ -137,6 +158,7 @@ function dependent_nodeapi(&$node, $op, 
       break;
     case 'validate':
       $nodecopy=$node;
+      break;
   }
 }
 
@@ -149,7 +171,7 @@ function dependent_nodeapi(&$node, $op, 
  *  The form to add the possible options that the admin should choose from
  * 
  * $param $field_name
- *  The field name that this setting is for. Use null if this field is from another content tyoe
+ *  The field name that this setting is for. Use null if this field is from another content type
  * 
  * @param $new_for_name
  *  The name of the fieldset item to add to $form
@@ -163,56 +185,75 @@ function dependent_nodeapi(&$node, $op, 
 
 function dependent_create_form_options($content_types, &$form, $field_name=NULL, $new_form_name='dependent', $weight=-5, $form_value_name=""){
   if (isset($field_name)){
-    $onoff_field_instances=db_query('SELECT * FROM {node_field_instance} WHERE widget_type="options_onoff" and type_name="%s" and field_name<>"%s"', $content_types['parent'],$field_name );
-    $options_field_instances=db_query('SELECT nfi.field_name, nf.global_settings FROM {node_field_instance} nfi,{node_field} nf WHERE nfi.widget_type="options_buttons" and nfi.type_name="%s" and nfi.field_name=nf.field_name and nfi.field_name<>"%s"', $content_types['parent'], $field_name);      
-    $selection_field_instances=db_query('SELECT nfi.field_name, nf.global_settings FROM {node_field_instance} nfi,{node_field} nf WHERE nfi.widget_type="options_select" and nfi.type_name="%s" and nfi.field_name=nf.field_name and nfi.field_name<>"%s"', $content_types['parent'], $field_name);      
-  }else{
-    $onoff_field_instances=db_query('SELECT * FROM {node_field_instance} WHERE widget_type="options_onoff" and type_name="%s"', $content_types['parent']);
-    $options_field_instances=db_query('SELECT nfi.field_name, nf.global_settings FROM {node_field_instance} nfi,{node_field} nf WHERE nfi.widget_type="options_buttons" and nfi.type_name="%s" and nfi.field_name=nf.field_name', $content_types['parent']);       
-    $selection_field_instances=db_query('SELECT nfi.field_name, nf.global_settings FROM {node_field_instance} nfi,{node_field} nf WHERE nfi.widget_type="options_select" and nfi.type_name="%s" and nfi.field_name=nf.field_name', $content_types['parent']);       
+    $onoff_field_instances=db_query('SELECT * FROM {content_node_field_instance} WHERE widget_type="optionwidgets_onoff" and type_name="%s" and field_name<>"%s"', $content_types['parent'], $field_name );
+    $options_field_instances=db_query('SELECT cnfi.field_name, cnf.global_settings FROM {content_node_field_instance} cnfi,{content_node_field} cnf WHERE cnfi.widget_type="optionwidgets_buttons" and cnfi.type_name="%s" and cnfi.field_name=cnf.field_name and cnfi.field_name<>"%s"', $content_types['parent'], $field_name);
+    $selection_field_instances=db_query('SELECT cnfi.field_name, cnf.global_settings FROM {content_node_field_instance} cnfi,{content_node_field} cnf WHERE cnfi.widget_type="optionwidgets_select" and cnfi.type_name="%s" and cnfi.field_name=cnf.field_name and cnfi.field_name<>"%s"', $content_types['parent'], $field_name);
+  }
+  else{
+    $onoff_field_instances=db_query('SELECT * FROM {content_node_field_instance} WHERE widget_type="optionwidgets_onoff" and type_name="%s"', $content_types['parent']);
+    $options_field_instances=db_query('SELECT cnfi.field_name, cnf.global_settings FROM {content_node_field_instance}  cnfi,{content_node_field} cnf WHERE cnfi.widget_type="optionwidgets_buttons" and cnfi.type_name="%s" and cnfi.field_name=cnf.field_name', $content_types['parent']);
+    $selection_field_instances=db_query('SELECT cnfi.field_name, cnf.global_settings FROM {content_node_field_instance} cnfi,{content_node_field} cnf WHERE cnfi.widget_type="optionwidgets_select" and cnfi.type_name="%s" and cnfi.field_name=cnf.field_name', $content_types['parent']);
   }
   $row=1;
-  if ((db_num_rows($onoff_field_instances)==0)&&(db_num_rows($options_field_instances)==0)&& (db_num_rows($selection_field_instances)==0)) //no field type can be selected  
-    return FALSE;
+  $instance_switch = FALSE;
+  $onoff_instance = array();
+  $options_instance = array();
+  $selection_instance = array();
+  while ($data = db_fetch_object($onoff_field_instances)) {
+    $instance_switch = TRUE;
+    $onoff_instance[] = $data;
+  }
+  while ($data = db_fetch_object($options_field_instances)) {
+    $instance_switch = TRUE;
+    $options_instance[] = $data;
+  }
+  while ($data = db_fetch_object($selection_field_instances)) {
+    $instance_switch = TRUE;
+    $selection_instance[] = $data;
+  }
   
-  //the first oprion is "no field"  
+  if ($instance_switch == FALSE) { //no field type can be selected
+    return FALSE;
+  }
+  //the first option is "no field"
   $options[0]=t("no field");
   //add the single on/off checkboxes to the options array
-  while($onoff_field=db_fetch_object($onoff_field_instances)){
+  foreach ($onoff_instance as $onoff_field) {
       $options[$row++]="$onoff_field->field_name";// in $onoff_field->type_name";
   }
   //add the options on each radio buttons field type. every single choice will be added as a separate choice which includes the choice text and the field type name     
-  while($options_field=db_fetch_object($options_field_instances)){
+  foreach ($options_instance as $options_field) {
       $field_details=content_fields($options_field->field_name,$content_types['parent']);
       $field_options_array=explode("\r\n", $field_details['allowed_values']);
       //iterate through the array and put the options in $options together with the field name          
       foreach($field_options_array as $option){
          $options[$row++]=$option.' '.t("option from").' '.$options_field->field_name;
-         //print_r($options);
       }          
   }
   
   //add the options on each radio buttons field type. every single choice will be added as a separate choice which includes the choice text and the field type name     
-  while($options_field=db_fetch_object($selection_field_instances)){
+  foreach ($selection_instance as $options_field) {
       $field_details=content_fields($options_field->field_name,$content_types['parent']);
       $field_options_array=explode("\r\n", $field_details['allowed_values']);
       //iterate through the array and put the options in $options together with the field name          
       foreach($field_options_array as $option){
          $options[$row++]=$option.' '.t("option from").' '.$options_field->field_name;
-         //print_r($options);
       }          
   }
-  
   //if this is an update operation we need to load the previously selected valued to the options list
-  $prev_result=db_query('SELECT * FROM {field_dependencies} WHERE child_field_name="%s" AND parent_node_type_name="%s" AND child_node_type_name="%s"', isset($field_name)?$field_name:"related", $content_types['parent'],$content_types['child']);
-  $is_updatable=db_num_rows($prev_result);
+  $prev_result=db_query('SELECT * FROM {field_dependencies} WHERE child_field_name="%s" AND parent_node_type_name="%s" AND child_node_type_name="%s"', $field_name, $content_types['parent'], $content_types['child']);
+  $prev_result_count = 0;
+  $prev_result_array = array();
+  while ($data = db_fetch_object($prev_result)) {
+    $prev_result_count ++;
+    $prev_result_obj = $data;
+  }
+  $is_updatable=$prev_result_count;
   $prev_selected=array();
   $put_after_parent_flag=FALSE;
-  while ($parent = db_fetch_array($prev_result)){
-    $selected_item=(($parent['option_text']=='no_value_needed')?'':($parent['option_text'].' '.t("option from").' ')).$parent['parent_field_name'] ;
+  $selected_item=(($prev_result_obj->option_text=='no_value_needed')?'':($prev_result_obj->option_text .' '. t("option from") .' ')) . $prev_result_obj->parent_field_name;
     $prev_selected[]=array_search($selected_item,$options); 
-    $put_after_parent_flag=$parent['put_after_parent'];
-  }
+  $put_after_parent_flag=$prev_result_obj->put_after_parent;
   
   $form[$new_form_name] = array( 
     '#type' => 'fieldset',
@@ -255,16 +296,15 @@ function dependent_unset_required_before
  * This is done because the module validation functions are called after the default node_form_validate function
  */
 function dependent_find_and_destroy_required_flag(&$element, $field_name){
-  global $dependent_required_fields;  
-  if (!isset($dependent_required_fields)){
-    $dependent_required_fields=array();
+  global $_dependent_required_fields;
+  if (!isset($_dependent_required_fields)) {
+    $_dependent_required_fields=array();
   }
   foreach ($element as $key=>$value) {
     if (strval($key)=='#required') {
       unset($element[$key]);
-      $dependent_required_fields[]=$field_name;
+      $_dependent_required_fields[]=$field_name;
       $element['#title'].= '<span class="form-required" title="'. t('This field is required.') .'">*</span>';
-      
       return;
     }
     if (is_array($value)){
@@ -281,8 +321,10 @@ function dependent_find_and_destroy_requ
 function dependent_validate_required_fields($form_id, $form_values){
   $dependencies=db_query("SELECT * FROM {field_dependencies} WHERE parent_node_type_name='%s' AND is_required=1", $form_values['type']);    
   while ($field=db_fetch_array($dependencies)){      
-    if (($form_values[$field['parent_field_name']]['key']===$field['option_text'])||(($field['option_text']=='no_value_needed')&&($form_values[$field['paremt_field_name']]===1))){//check if the parent field has the right value for the child field to be filled
-      if (((isset($form_values[$field['child_field_name']]['key']))&&($form_values[$field['child_field_name']]['key']===""))||((isset($form_values[$field['child_field_name']][0]))&&($form_values[$field['child_field_name']][0]['value']===""))){//check if the child field has a value
+    if (($form_values[$field['parent_field_name']]['key']===$field['option_text'])||(($field['option_text']=='no_value_needed')&&($form_values[$field['paremt_field_name']]===1))) {
+      //check if the parent field has the right value for the child field to be filled
+      if (((isset($form_values[$field['child_field_name']]['key']))&&($form_values[$field['child_field_name']]['key']===""))||((isset($form_values[$field['child_field_name']][0]))&&($form_values[$field['child_field_name']][0]['value']===""))) {
+        //check if the child field has a value
       //the child field does not have a value
           $field_data=content_fields($field['child_field_name'],$form_values['type']);
           form_set_error('dependent',t('!name field is required.', array('!name' => $field_data['widget']['label'])));          
@@ -290,4 +332,3 @@ function dependent_validate_required_fie
     }
   }    
 }
-?>
