function save_address()
 {
     global $db;
     $order = new order($this->params['orderid']);
     $billing = new billing($this->params['orderid']);
     $s = array_pop($order->shippingmethods);
     $shippingmethod = new shippingmethod($s->id);
     //eDebug($order);
     //eDebug($this->params,true);
     //eDebug($shippingmethod);
     $billingmethod = $billing->billingmethod;
     /*
            eDebug($order);
            eDebug($shippingmethod);
            eDebug($billingmethod);*/
     if ($this->params['save_option'] == 0) {
         //update existing
         //echo "Update";
         $addy = new address($this->params['addyid']);
     } else {
         if ($this->params['save_option'] == 1) {
             //create new
             //echo "New";
             $oldaddy = new address($this->params['addyid']);
             $addy = new address();
             $addy->user_id = $oldaddy->user_id;
         }
     }
     //eDebug($addy,true);
     foreach ($this->params['address'] as $key => $val) {
         $addy->{$key} = $val;
         if (isset($billingmethod->{$key})) {
             $billingmethod->{$key} = $val;
         }
         if (isset($shippingmethod->{$key})) {
             $shippingmethod->{$key} = $val;
         }
     }
     $addy->is_billing = 0;
     $addy->is_shipping = 0;
     $addy->save();
     $addy->refresh();
     if ($this->params['type'] == 'billing' || $this->params['same'] == true && $this->params['save_option'] == 0) {
         //echo "Billing";
         $billingmethod->addresses_id = $addy->id;
         $billingmethod->save();
         $addy->is_billing = 1;
     }
     if ($this->params['type'] == 'shipping' || $this->params['same'] == true && $this->params['save_option'] == 0) {
         //eDebug("Shipping",true);
         $shippingmethod->addresses_id = $addy->id;
         $shippingmethod->save();
         $addy->is_shipping = 1;
     }
     $addy->save();
     if ($addy->is_default) {
         $db->setUniqueFlag($addy, 'addresses', 'is_default', 'user_id=' . $addy->user_id);
     }
     //eDebug($shippingmethod,true);
     flashAndFlow('message', 'Address updated.');
     redirect_to(array('controller' => 'order', 'action' => 'show', 'id' => $this->params['id']));
 }