--- uc_aac.orig	2008-01-13 01:26:44.000000000 -0600
+++ uc_aac.module	2008-01-13 02:58:29.000000000 -0600
@@ -49,7 +49,7 @@ function uc_aac_form_alter($form_id, &$f
 
 function _uc_aac_alter_form($field_id, $nid) {
   $correct_token = drupal_get_token($field_id);
-  $result = db_query('select sell_price from {uc_products} where nid=%d', $nid);
+  $result = db_query('SELECT sell_price FROM {uc_products} WHERE nid=%d AND vid=(SELECT vid FROM {node_revisions} WHERE nid=%d ORDER BY timestamp DESC LIMIT 1)', $nid, $nid);
   $price = 0;
   if ($product = db_fetch_object($result)) {
     $price = $product->sell_price;
@@ -111,6 +111,44 @@ function _uc_aac_alter_form($field_id, $
   $content .= '
     $(fakeForm).ajaxSubmit( {
       success: function(html_result) {
+        while (html_result.indexOf("[")>-1) {
+          var spos = html_result.indexOf("[");
+          var epos = html_result.indexOf("]");
+          var attr = html_result.substring(spos+1, epos-spos);
+          html_result = html_result.substring(epos+1);
+          var opts = attr.split("|");
+          var el = false;
+          for (i=0; i<opts.length; i++) {
+            var pos = opts[i].indexOf(":");
+            if (pos==-1) {
+              break;
+            }
+            var key = opts[i].substring(0, pos);
+            var val = opts[i].substring(pos+1);
+            if (key=="attr" && val) {
+              el = document.getElementById("edit-attributes-"+val);
+              var defval = el.options[el.selectedIndex].value;
+              while (el.length) {
+                el.remove(0);
+              }
+            }
+            else if (el && key && val) {
+              var newel = document.createElement("option");
+              newel.text = val;
+              newel.value = key;
+              if (key==defval) {
+                newel.selected = true;
+                newel.defaultSelected = true;
+              }
+              if ($.browser.msie) {
+                el.add(newel);
+              }
+              else {
+                el.add(newel, null);
+              }
+            }
+          }
+        }
         var currentsellHTML= $("#node-'.$nid.' .sell_price").html();
         var currentdisplayHTML = $("#node-'.$nid.' .display_price").html();
         if (currentdisplayHTML) {
@@ -142,20 +180,45 @@ function _calculate_price() {
   $field_values = $_POST;
   $correct_token = drupal_get_token($field_values['uccp']['field_id']);
   if ($correct_token == $field_values['uccp']['form_token']) {
-    $result = db_query('select sell_price from {uc_products} where nid=%d', $field_values['uccp']['nid']);
+    $nid = $field_values['uccp']['nid'];
+    $result = db_query('SELECT sell_price FROM {uc_products} WHERE nid=%d AND vid=(SELECT vid FROM {node_revisions} WHERE nid=%d ORDER BY timestamp DESC LIMIT 1)', $nid, $nid);
     if ($product = db_fetch_object($result)) {
       $price = $product->sell_price;
     }
     $attributes = $field_values['attributes'];
     if ($attributes) {
       foreach ($attributes as $attr) {
-        $result = db_query('select price from {uc_product_options} WHERE nid = %d AND oid = %d', $field_values['uccp']['nid'], $attr);
-        if ($adjustment = db_fetch_object($result)) {
-          $price += $adjustment->price;
+        $res = db_query('SELECT aid FROM {uc_attribute_options} WHERE oid = %d', $attr);
+        if ($aid = db_result($res)) {
+          $res = db_query('SELECT DISTINCT upo.oid, uao.name, upo.cost, upo.price FROM {uc_attribute_options} AS uao JOIN {uc_product_options} AS upo ON uao.oid = upo.oid WHERE uao.aid = %d AND upo.nid = %d', $aid, $nid);
+          $names = array();
+          $prices = array();
+          while ($obj = db_fetch_object($res)) {
+            $names[$obj->oid] = $obj->name;
+            $prices[$obj->oid] = $obj->price;
+            if ($obj->oid==$attr) {
+              $price += $obj->price;
+            }
+          }
+          $baseprice = $prices[$attr];
+          print "[attr:$aid|";
+          foreach ($prices as $oid=>$aprice) {
+            $adjprice = $aprice - $baseprice;
+            $text = $names[$oid];
+            if ($adjprice!=0) {
+              $text .= ", ";
+              if ($adjprice>0) {
+                $text .= "+";
+              }
+              $text .= uc_currency_format($adjprice);
+            }
+            print "{$oid}:{$text}|";
+          }
+          print "]";
         }
       }
     }
     $ret = uc_currency_format($price);
-    print( $ret );
+    print $ret;
   }
 }

