function ajax($terminate = true)
 {
     $x = @ob_get_clean();
     $x = @ob_get_clean();
     $x = @ob_get_clean();
     $x = @ob_get_clean();
     $x = @ob_get_clean();
     $x = @ob_get_clean();
     $cmd = JRequest::getVar('cmd', '');
     if (strtolower($cmd) == 'showfile') {
         $this->showFile();
     }
     if ($terminate) {
         echo 'Running AJAX...<br />';
     }
     $mainframe = JFactory::getApplication();
     echo 'Command: ' . $cmd . '<br />';
     if (strtolower($cmd) == 'sendxml') {
         $this->sendXml();
     }
     if (strtolower($cmd) == 'checkfile') {
         require_once JPATH_COMPONENT . DS . 'assets' . DS . 'export_helper.php';
         $ehelper = new OnepageTemplateHelper();
         $ehelper->checkFile();
     }
     if (strtolower($cmd) == 'sendxmlmulti') {
         $this->sendXmlMulti();
     }
     if (strtolower($cmd) == 'sendemail') {
         $this->sendMail();
     }
     if (strtolower($cmd) == 'updatejoomla') {
         $msg = $this->updateJoomla();
         echo $msg . '<br />';
         if (empty($msg)) {
             echo 'OK!<br/>';
         }
         return;
     }
     $mainframe = JFactory::getApplication();
     // echo JPATH_ADMINISTRATOR.DS.'components'.DS.'com_onepage'.DS.'views'.DS.'order_details'.DS.'tmpl'.DS.'ajax'.DS.'onepage_ajax.php';
     //echo '<br />'.$cmd;
     $fieldid = JRequest::getVar('fieldid', '');
     if ($terminate) {
         $mainframe->close();
         die;
     }
     if (strpos($fieldid, 'track_num') !== false) {
         $this->changeTrackNum($fieldid);
     }
     $order_id = JRequest::getVar('orderid', '');
     if (empty($order_id)) {
         echo 'Empty Order Id';
         if ($terminate) {
             $mainframe->close();
         }
     }
     $cmd = JRequest::getVar('cmd', '');
     if (!empty($cmd)) {
         $d = JRequest::get('post');
         $d['order_id'] = $order_id;
         $msg = '';
         ob_start();
         if (strtolower($cmd) == 'orderstatusset') {
             echo $this->orderstatusset();
             return;
         }
         if (strtolower($cmd) == 'orderitemstatusset') {
             $ps_order = new ps_order();
             $ret = $ps_order->order_status_update($d);
             if ($ret === true) {
                 $msg = '<br />Order Status Updated';
             } else {
                 $msg = '<br />Error Updating Order Status';
             }
             //var_dump($d);
             echo $msg;
             //die();
             return;
         }
         if (strtolower($cmd) == 'resendconfirm') {
             $this->resendconfirm();
             if ($terminate) {
                 $mainframe->close();
             }
         } else {
             //echo 'cmd:'.$cmd.'endcmd';
             //$cmd = '$this->'.$cmd.'()';
             $msg = '<br />Function: ' . $cmd . '<br />';
             $cmd = htmlspecialchars($cmd);
             if (!@eval('$ps_order_change->' . $cmd . '($d);')) {
                 $msg .= '<br />Error Calling Function !';
             }
         }
         if (method_exists($this, $cmd)) {
             $this->{$cmd}();
         }
         $xx = ob_get_clean();
         echo $msg . '<br />';
         $mainframe = JFactory::getApplication();
         if ($terminate) {
             $mainframe->close();
         }
     }
     //$t = @ob_get_clean();$t = @ob_get_clean(); $t = @ob_get_clean();$t = @ob_get_clean();$t = @ob_get_clean();
     //while (!@ob_get_clen()) {;}
     // unset($t);
     //echo 'ajax initialized';
     //  var q = '&id='+id+'&orig_val='+val+'&new_val='+element.value;
     $new_value = JRequest::getVar('newval', '');
     $orig_value = JRequest::getVar('origval', '');
     $orig_value = urldecode($orig_value);
     if ($orig_value == ' ') {
         $orig_value = '';
     }
     $id = JRequest::getVar('fieldid', '');
     $onlyOrder = JRequest::getVar('onlyorder', false);
     if (empty($id) || empty($order_id)) {
         echo 'Empty field id or order_id ' . $id . ' ' . $order_id . ' <br />';
         $mainframe = JFactory::getApplication();
         if ($terminate) {
             $mainframe->close();
         }
     }
     $db = JFactory::getDBO();
     $new_value = $db->getEscaped($new_value);
     //$orig_value = trim($db->getEscaped($orig_value));
     $id = $db->getEscaped($id);
     $order_id = urlencode($order_id);
     if (strpos($id, 'bt_') === 0 || strpos($id, 'st_') === 0) {
         if (substr($id, 0, 3) == 'st_') {
             $address_type = 'ST';
         } else {
             $address_type = 'BT';
         }
         //$id = str_replace('bt_', '', $id);
         $id = substr($id, 3);
         // lets get the right line in order_user_info
         $q = "select * from #__vm_order_user_info where order_id = '" . $order_id . "' and address_type = '" . $address_type . "' ";
         $db->setQuery($q);
         $res = $db->loadAssoc();
         if (!isset($res) && $address_type == 'ST') {
             // we don't have a shipping address created yet
             // let us create it
             $q = "select * from #__vm_order_user_info where order_id = '" . $order_id . "' and address_type='BT' ";
             $db->setQuery($q);
             $d1 = $db->loadAssoc();
             if (isset($d1)) {
                 $col1 = 'order_info_id, order_id';
                 // follows user_id
                 $col2 = 'user_info_id';
                 // follows user_id
                 $val1 = "NULL, '" . $order_id . "'";
                 $newid = md5(uniqid('VirtueMartIsCool'));
                 $val2 = "'" . $newid . "'";
                 foreach ($d1 as $key => $val) {
                     if ($key != 'order_info_id' && $key != 'order_id') {
                         if ($key == $id) {
                             $val = $new_value;
                         }
                         if ($key == 'address_type') {
                             $val = 'ST';
                         }
                         $col1 .= "," . $key . " ";
                         $col2 .= "," . $key . " ";
                         $val1 .= ",'" . $val . "' ";
                         $val2 .= ",'" . $val . "' ";
                     }
                 }
                 $q = 'insert into #__vm_order_user_info (' . $col1 . ') values (' . $val1 . ') ';
                 $db->setQuery($q);
                 $db->query();
                 echo 'Creating new shipping address<br />';
                 $msg = $db->getErrorMsg();
                 if (!empty($msg)) {
                     echo $msg;
                 }
                 $q = 'insert into #__vm_user_info (' . $col2 . ') values (' . $val2 . ') ';
                 $db->setQuery($q);
                 $db->query();
                 $msg = $db->getErrorMsg();
                 if (!empty($msg)) {
                     echo $msg;
                 }
                 echo 'New shipping address created<br />';
             }
         }
         $msg = $db->getErrorMsg();
         // echo 'right here<br />db:'.var_dump($res[$id]).'<br />orig:'.var_dump($orig_value);die();
         if (!empty($msg)) {
             echo $msg;
             die;
         }
         //echo $orig_value.'='.$res[$id];
         if (isset($res)) {
             if (isset($res[$id])) {
                 if (!($res[$id] == $orig_value)) {
                     echo 'Original value does not match new value!<br />';
                 }
                 $q = "update #__vm_order_user_info set " . $id . " = '" . $new_value . "' where order_info_id = '" . $res['order_info_id'] . "' limit 1";
                 $db->setQuery($q);
                 $db->query();
                 $msg = $db->getErrorMsg();
                 if (!empty($msg)) {
                     echo $msg;
                     die;
                 }
                 echo 'Order Info updated <br />';
             }
         }
         // we will not update other fields if we have onlyOrder here
         if ($onlyOrder === true) {
             $mainframe = JFactory::getApplication();
             if ($terminate) {
                 $mainframe->close();
             }
         }
         // lets get the right line in user_info table
         $q = "select * from #__vm_user_info where user_id = '" . $res['user_id'] . "' and address_type = '" . $address_type . "' and " . $id . " = '" . $db->getEscaped($orig_value) . "' limit 0,10";
         $db->setQuery($q);
         $res3 = $db->loadAssocList();
         $msg = $db->getErrorMsg();
         echo $msg;
         if (!isset($res3)) {
             echo 'Oginal value not found. Will not update VM User Data<br />';
         }
         if (isset($res3)) {
             foreach ($res3 as $res2) {
                 $q = "update #__vm_user_info set " . $id . " = '" . $new_value . "' where user_id = '" . $res['user_id'] . "' and address_type = '" . $address_type . "' limit 1";
                 $db->setQuery($q);
                 $db->query();
                 echo 'User Info updated <br />';
                 $msg = $db->getErrorMsg();
                 if (!empty($msg)) {
                     echo $msg;
                     die;
                 }
             }
         }
         // lets update jomla info, only if address type is BT
         if ($address_type == 'BT') {
             $q = 'select * from #__users where id = "' . $res['user_id'] . '" ';
             $db->setQuery($q);
             $data = $db->loadAssoc();
             if (!empty($data)) {
                 switch ($id) {
                     case 'user_email':
                         $email = $data['email'];
                         if ($email == $orig_value) {
                             if ($data['username'] == $orig_value) {
                                 $ins = ", username = '******' ";
                                 echo 'Joomla Username updated <br />';
                             } else {
                                 $ins = "";
                             }
                             $q = "update #__users set email = '" . $new_value . "' " . $ins . " where id = '" . $res['user_id'] . "' limit 1";
                             echo 'Joomla Email updated <br />';
                             $db->setQuery($q);
                             $db->query();
                             //echo 'jos_users updated <br />';
                             $msg = $db->getErrorMsg();
                             if (!empty($msg)) {
                                 echo $msg;
                                 die;
                             }
                         } else {
                             echo 'emails do not match<br />';
                         }
                         break;
                     case 'first_name':
                         $full_name = $data['name'];
                         if (strpos($full_name, $orig_value) !== false) {
                             $full_name = str_replace($orig_value, $new_value, $full_name);
                             $q = "update #__users set name = '" . $full_name . "' where id = '" . $res['user_id'] . "' limit 1";
                             echo 'Joomla Name field updated <br />';
                             $db->setQuery($q);
                             $db->query();
                             $msg = $db->getErrorMsg();
                             if (!empty($msg)) {
                                 echo $msg;
                                 die;
                             }
                         }
                         break;
                     case 'last_name':
                         $full_name = $data['name'];
                         if (strpos($full_name, $orig_value) !== false) {
                             $full_name = str_replace($orig_value, $new_value, $full_name);
                             $q = "update #__users set name = '" . $full_name . "' where id = '" . $res['user_id'] . "' limit 1";
                             echo 'Joomla Name field updated <br />';
                             $db->setQuery($q);
                             $db->query();
                             $msg = $db->getErrorMsg();
                             if (!empty($msg)) {
                                 echo $msg;
                                 die;
                             }
                         }
                         break;
                     default:
                         break;
                 }
             }
         }
         $q = "";
     }
     $mainframe = JFactory::getApplication();
     if ($terminate) {
         $mainframe->close();
     }
 }