<?php
if (!defined('_VALID_MOS') && !defined('_JEXEC')) die('Direct Access to ' . basename(__FILE__) . ' is not allowed.');
/**
*
* @package VehicleManager
* @copyright by Ordasoft
* @author Andrey Kvasnevskiy - OrdaSoft (akbet@mail.ru); Rob de Cleen (rob@decleen.com);
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* Homepage: https://ordasoft.com/
*
* */
$mosConfig_absolute_path = $GLOBALS['mosConfig_absolute_path'] = JPATH_SITE;
require_once($mosConfig_absolute_path . "/components/com_vehiclemanager/compat.joomla1.5.php");
require_once($mosConfig_absolute_path . "/components/com_vehiclemanager/vehiclemanager.class.sefurl.php");

$GLOBALS['pu_vm_debug'] = false;
function pu_log_vm($msg, $request=false ) {

    global $pu_vm_debug;

    if($pu_vm_debug){
              // Write out new initialization file
        $logFilePath =  "./log_debug.log" ;
        $fd = fopen($logFilePath, "a+")
                or die("Cannot create log_debug.log file.");
        if(!empty($msg) ){
          fwrite($fd, "---------------".date( "d M Y G:i:s" ) ."\n");
          fwrite($fd, $msg ."\n" );
        } 
        if($request){
          if(is_string( $request ) || is_int( $request ) )  fwrite($fd,  $request ."\n" );
          else if(is_array( $request ) || is_object( $request ) ) 
            foreach($request as $key=>$value){
                if(is_string( $value ) || is_int( $value ) ) 
                    fwrite($fd,  "[".$key."] = ".$value ."\n" );
                else if(is_array( $value ) || is_object( $value ) ) {
                    fwrite($fd,  "[".$key."] = ".pu_log_vm("",$value) ."\n" );
                }
            }
        }
        fclose($fd);    
      }
}



class VehicleManagerRouter extends JComponentRouterView
{
    public function build(&$query)
    {


        pu_log_vm("VehicleManagerRouter:build start:",$query);
// echo ":111111111111112:";
// print_r($url_alias);
// exit;  


        $segments = array();
        $segments_with_id = array(); //need if same $segments exist for other URL(same vehicle name.. )
        if (isset($query['view']))   $view =$query['view'] ;
        if (isset($query['task']))   $task =$query['task'] ;
        else if(isset($query['view']))  $task =$query['view'] ;


        if (isset($query['Itemid']) ) { 
            //for menu items - not use router

            pu_log_vm("VehicleManagerRouter:build 00101:");        
            if(!isset($task)) return $segments;
            pu_log_vm("VehicleManagerRouter:build 00102:");        

            if (isset($query['view']) ) { 
            pu_log_vm("VehicleManagerRouter:build 00103:");        

                $url_alias = get_menu_url_alias_vm($query['Itemid'],$query['view'],$task) ;
            pu_log_vm("VehicleManagerRouter:build 0010:",$url_alias);        
                if(!empty($url_alias) ) {
                    //we in some exectly menu
            pu_log_vm("VehicleManagerRouter:build 001:",$query);        
                    if ( !isset($query['letindex']) 
                        && !isset($query['limitstart'])
                        && !isset($query['limit'])
                        )  {
                        // foreach ($query as $key => $value) {
                        //     if( $key == 'option' ) continue ;
                        //     if( $key == 'Itemid' ) continue ;
                        //     unset($query[$key]);
                        // }
                        unset($query['view']);
                        unset($query['layout']);
             pu_log_vm("VehicleManagerRouter:build 002:",$query);        
                        return $segments;
                    }

                }
            }
        }

            pu_log_vm("VehicleManagerRouter:build 00104:");        
        $db = JFactory::getDBO();

        // remove 'Itemid' from "vars query" for DB, because start menu items may be many
        $query_tmp = array();
        foreach ($query as $key => $value) {
pu_log_vm("VehicleManagerRouter:build 00105:");                    
            if( $key == 'Itemid' ) continue ;
            $query_tmp[$key] = $query[$key];
        }
pu_log_vm("VehicleManagerRouter:build 00106:");        
        $sefurl = new mosVmSefurl($db);
pu_log_vm("VehicleManagerRouter:build 00107:");        

        $ser_vars = serialize($query_tmp) ;
    pu_log_vm("VehicleManagerRouter:build 01:",$query);        
        $hash_vars = md5( $ser_vars );
        $sefurl = $sefurl->loadByHashVars($hash_vars);
       
        if($sefurl != null ) {
          
            //if this url aready created
            foreach ($query as $key => $value) {
                if( $key == 'option' ) continue ;
                if( $key == 'Itemid' ) continue ;
                if( $key == 'task' || $key == 'id' 
                    || $key == 'letindex' || $key == 'limitstart' 
                    || $key == 'limit' || $key == 'printItem' 
                    || ( $key == 'catid' && ($task != "search_vehicle" || $task != "search") ) 
                    || ( $key == 'name' && 
                         ( $task == 'owner_vehicles' || $task == 'view_user_vehicles' 
                          || $task == 'show_my_cars' || $task == 'owner_vehicles' ) 
                     ) 
                    || ( $key == 'id' && $task == 'mdownload') 
                ) unset($query[$key] );
            }
            $segments = unserialize( $sefurl->ser_url_segments ) ;
    pu_log_vm("VehicleManagerRouter:build 1:",$segments);
        //exit;

            return $segments;
        } else{
   
            //if this url not exist
        pu_log_vm("VehicleManagerRouter:build 2:");
            $sefurl = new mosVmSefurl($db);
            $sefurl->ser_vars = $ser_vars;
            $sefurl->hash_vars = $hash_vars ;

          
            if(isset($task)) {
                switch($task) {
                    case 'show_search_vehicle':
                        $segments[] = 'show-search';
                    break;
                    case 'search_vehicle':
                    case 'search':
                        $segments[] = 'search';
                    break;
                    case 'view':
                    case 'view_vehicle':
                        //$segments[] = 'view'; //comment because we no need "this task name" URL part
                    break;
                    case 'alone_category':
                    case 'showCategory':
                    // $segments[] = "showCategory"; //comment because we no need "this task name" URL part
                    break;
                    case 'review':
                        $segments[] = 'review';
                    break;
                    case 'owners_list':
                        $segments[] = 'owners-list';
                    break;    
                    case 'secret_image':
                     //   $segments[] = 'secret_image'; //comment because we no need "this task name" URL part
                    break;    
                    case 'view_user_vehicles':
                        //$segments[] = 'user-vehicles';//comment because we no need "this task name" URL part
                    break;    


                    default:
                        $segments[] = JFilterOutput::stringURLUnicodeSLug($task);
                    break;
                }
            } 

            if (isset($query['catid']) && ( $task != "search_vehicle" ||  $task != "search" )  ) {
                if( $query['catid'] > 0) {
                    $sql_query = "SELECT c.id, c.name, c.parent_id 
                            FROM #__vehiclemanager_main_categories AS c 
                            WHERE c.id = ".intval($query['catid']);
                    $db->setQuery($sql_query);
                    $row = null;
                    $row = $db->loadObject();
                    
                    if(isset($row)) {
                      $cattitle = array();
                      $cattitle_with_id = array();
                      $cattitle[] = JFilterOutput::stringURLUnicodeSLug($row->name);
                      $cattitle_with_id[] = JFilterOutput::stringURLUnicodeSLug($row->id. '_' .$row->name);
                   
                      while(isset($row) && $row->parent_id>0) {
                        $sql_query = "SELECT name, c.id , parent_id 
                                  FROM #__vehiclemanager_main_categories AS c
                                  WHERE c.id = ".intval($row->parent_id);
                        $db->setQuery($sql_query);
                        $row = $db->loadObject();
                        if(isset($row) && $row->name!=''){
                          $cattitle[] = JFilterOutput::stringURLUnicodeSLug($row->name);
                          $cattitle_with_id[] = JFilterOutput::stringURLUnicodeSLug($row->name);
                        }
                      }
                      $segments_with_id = array_merge($segments,array_reverse($cattitle_with_id));
                      $segments = array_merge($segments,array_reverse($cattitle));
                    }
                }
                unset($query['catid']);
            }

            if(isset($query['id']) && ( $task == 'view' || $task == 'view_vehicle' ) ) {
                //if exist vehicle, - this not end segment, so clean segments_with_id
                $segments_with_id = $segments;
            }


pu_log_vm("VehicleManagerRouter:build 22:".$task."::",$query);             
            if(isset($query['id']) && ( $task == 'view' || $task == 'view_vehicle') ) {
                $sql_query = "SELECT v.vtitle, v.id 
                        FROM #__vehiclemanager_vehicles AS v
                        LEFT JOIN #__vehiclemanager_categories AS vc ON v.id=vc.iditem
                        LEFT JOIN #__vehiclemanager_main_categories AS c ON c.id=vc.idcat
                        WHERE v.id=" . intval($query['id']) ;

pu_log_vm("VehicleManagerRouter:build 221:".$sql_query);             

                 $db->setQuery($sql_query);
                 $row = null;
                 $row = $db->loadObject();
                 if(isset($row)) {
                   $segments[] = JFilterOutput::stringURLUnicodeSLug($row->vtitle);
                   $segments_with_id[] = JFilterOutput::stringURLUnicodeSLug($row->id. '_' .$row->vtitle);
                 }
            } 

            if(isset($query['id']) && $task == 'edit_vehicle') {
                $segments[] = JFilterOutput::stringURLUnicodeSLug($query['id']);
                $segments_with_id[] = JFilterOutput::stringURLUnicodeSLug($query['id']);
            }

            if(isset($query['id']) && $task == 'mdownload') {
                $segments[] = JFilterOutput::stringURLUnicodeSLug($query['id']);
                $segments_with_id[] = JFilterOutput::stringURLUnicodeSLug($query['id']);
            }      

            if(isset($query['name']) && 
                ( $task == 'owner_vehicles' || $task == 'view_user_vehicles' || 
                  $task == 'show_my_cars' || $task == 'owner_vehicles' ) ) {
                $segments[] = JFilterOutput::stringURLUnicodeSLug($query['name'] );
                $segments_with_id[] = JFilterOutput::stringURLUnicodeSLug($query['name']);
                unset($query['name']);
            }

            if(isset($query['printItem']) ) {
                $segments[] = JFilterOutput::stringURLUnicodeSLug($query['printItem'] );
                $segments_with_id[] = JFilterOutput::stringURLUnicodeSLug($query['printItem']);
                unset($query['printItem']);
            }


            if ( isset($query['letindex']) ) {
                //if(count($segments) == 0 ) $segments[] = $task;
                $segments[] = JFilterOutput::stringURLUnicodeSLug('letindex_'.$query['letindex']);
                unset($query['letindex']);
            }            
            if (isset($query['limitstart'])) {
                //if(count($segments) == 0 ) $segments[] = $task;
                $segments[] = JFilterOutput::stringURLUnicodeSLug("ls_".$query['limitstart']);
                unset($query['limitstart']);
            }

            if (isset($query['limit'])) {
                //if(count($segments) == 0 ) $segments[] = $task;
                $segments[] = JFilterOutput::stringURLUnicodeSLug("l_".$query['limit']);
                unset($query['limit']);
            }

            unset($query['id']);
            unset($query['task']);
            unset($query['view']);
            unset($query['layout']);
            unset($query['is_show_data']);
            unset($query['tab']);


            $sefurl->ser_url_segments = serialize($segments);
            $sefurl->hash_url_segments = md5( $sefurl->ser_url_segments ) ;

            $sefurl_new = new mosVmSefurl($db);
            $sefurl_new = $sefurl_new->loadByHashUrlSegments($sefurl->hash_url_segments);

            if($sefurl_new != null 
                && count($segments_with_id) > 0 
                && $sefurl->hash_vars == $sefurl_new->hash_vars ) {
                //$sefurl_new != null - this mean exist some duplecate with same url, 
                //$sefurl->hash_vars == $sefurl_new->hash_vars - mean all vars 
                // in url same(not only in segment)
                //so add to URL the ID
                $sefurl->ser_url_segments = serialize($segments_with_id);
                $sefurl->hash_url_segments = md5( $sefurl->ser_url_segments ) ;
                $segments = $segments_with_id;
            }else if($sefurl_new != null 
                && count($segments_with_id) == 0 
                && $sefurl->hash_vars == $sefurl_new->hash_vars ) {
                //this mean exist some duplecate with same url, 
                //$sefurl->hash_vars == $sefurl_new->hash_vars - mean all vars 
                // in url same(not only in segment)
                //so add to URL the some hash code
                //need additional check for SEaRCH tasks, 
                //so task may check with date and hint, and remove all old 1 month
                //this condition must not happened, other some error  !!!!
                $hash = md5( serialize($query) ) ;
                $segments[] = $hash;
                $sefurl->ser_url_segments = serialize($segments);
                $sefurl->hash_url_segments = md5( $sefurl->ser_url_segments ) ;
            }else if($sefurl_new != null 
                && count($segments_with_id) > 0 
                && $sefurl->hash_vars != $sefurl_new->hash_vars ) {
                //this mean exist some duplecate with same url, 
                //$sefurl->hash_vars != $sefurl_new->hash_vars - mean all vars 
                // in url NOT same(not only in segment)(may be exist other item with same name)
                $sefurl->ser_url_segments = serialize($segments_with_id);
                $sefurl->hash_url_segments = md5( $sefurl->ser_url_segments ) ;
                $segments = $segments_with_id;
            }

            $sefurl->store();
pu_log_vm("VehicleManagerRouter:build 3:",$segments);
            return $segments;

        }
pu_log_vm("VehicleManagerRouter:build 4:");



//old code - if some error :
        // $segments = array();
        // $db = JFactory::getDBO();

        // if (isset($query['Itemid'])  && $query['Itemid'] !== 0  )
        // {
        //     $app = JFactory::getApplication();
        //     $menu = $app->getMenu();
        //     $component = isset($menu->getItem($query['Itemid'])->component) ? $menu->getItem($query['Itemid'])->component : '';

        //     if (!isset($query['view']) && ($component == 'com_vehiclemanager') && !isset($query['task']))
        //     {

        //         if (version_compare(JVERSION, "1.6.0", "lt"))
        //         {
        //             $query['view'] = $menu->getItem($query['Itemid'])->query['task'];
        //         } else if (version_compare(JVERSION, "1.6.0", "ge"))
        //         {

        //             $query['view'] = $menu->getItem($query['Itemid'])->query['view'];
        //         }
        //     }

        //     if( isset($query['view']) && $query['view'] == "my_vehicles" ) {

        //       $user_name = isset($menu->getItem($query['Itemid'])->query['name']) ? $menu->getItem($query['Itemid'])->query['name'] : '';
        //       if(isset($name) && $name != "" ) $query['name'] = $user_name;
        //     }

        //     if( isset($query['view']) && $query['view'] == "alone_category" ) {
        //       if (version_compare(JVERSION, '3.0', 'ge'))
        //       {
        //           $menu = new JTableMenu($db);
        //           $menu->load($query['Itemid']);
        //           $params = new JRegistry;
        //           $params->loadString($menu->params);
        //       }
        //       $single_category = $params->get('single_category', '');
        //       if($single_category != "" ) $query['catid'] = $single_category;
        //     }

        // }

        // if (isset($query['option']) && $query['option'] == 'com_vehiclemanager')
        // { //check component
        //      if (isset($query['view']) && ($query['view'] == 'all_categories'
        //     || $query['view'] == 'my_vehicles'
        //     /*|| $query['view'] == 'owner_vehicles'*/
        //     || $query['view'] == 'show_search_vehicle'
        //     || $query['view'] == 'view_vehicle'
        //     || $query['view'] == 'owners_list')) {

        //     }else{
        //     $segments[0] = (isset($query['Itemid'])) && ($query['Itemid'] != 0) ? $query['Itemid'] : '0';
        //         if ((isset($query['view'])) && (!isset($query['task'])))
        //         {
        //             $query['task'] = $query['view'];
        //         }
        //     }

        //     if (isset($query['task']))
        //     {
        //         switch ($query['task']) {
        //             case 'all_vehicle':
        //                 $segments[1] = 'all_vehicle';
        //             break;
        //             case 'my_vehicles':
        //                 $segments[1] = 'my_vehicles';
        //             break;
        //             case 'new_url':
        //                 $segments[1] = 'buy_now';
        //                 break;
        //             case 'new_url_for_vm':
        //                 $segments[1] = 'buy_now_for_vm';
        //                 break;
        //             case 'view_user_vehicles':
        //             case 'owner_vehicles':
        //                 $segments[1] = $query['task'];
        //                 if (isset($query['name']))
        //                 {
        //                     //$segments[] = JFilterOutput::stringURLSafe($query['name']);
        //                     $segments[] = $query['name'];
        //                     unset($query['name']);
        //                 }
        //                 break;
        //             case 'rent_requests_vehicle':
        //                 $segments[1] = $query['task'];
        //                 break;
        //             case 'buying_requests_vehicle':
        //                 $segments[1] = $query['task'];
        //                 break;
        //             case 'rent_history_vehicle':
        //                 $segments[1] = $query['task'];
        //                 break;
        //             default:
        //                 $segments[1] = $query['task'];
        //                 break;
        //         }
        //     } else if ( !isset($query['task']) && !isset($query['view']) ){

        //         $segments[1] = "all_vehicle";
        //     }

        //     unset($query['task']);
        //     unset($query['view']);

        //     if (isset($query['catid']) && $segments[1] !== "search" && $segments[1] !== "search_vehicle"  )
        //     {
        //         if ($query['catid'] > 0)
        //         {
        //             $sql_query = "SELECT vmc.id, vmc.name, vmc.title, vc.idcat, vmc.parent_id " .
        //                     " FROM #__vehiclemanager_main_categories AS vmc" .
        //                     " LEFT JOIN #__vehiclemanager_categories AS vc ON vc.idcat=vmc.id " .
        //                     " LEFT JOIN #__vehiclemanager_vehicles AS v ON vc.iditem = v.id " .
        //                     " WHERE vmc.section = 'com_vehiclemanager' AND vmc.id = " . intval($query['catid']);
        //             $db->setQuery($sql_query);
        //             $row = null;
        //             $row = $db->loadObject();
        //             if (isset($row))
        //             {
        //                 $cattitle = array();
        //                 $segments[] = $query['catid'];
        //                 $segments[] = JFilterOutput::stringURLUnicodeSLug($row->title);
        //                 unset($query['catid']);
        //             }
        //         } else
        //         {
        //             $temp = $query['catid']; //is catid set??
        //             unset($query['catid']);
        //         }
        //     }

        //     if (!empty($query['lang']))
        //     {
        //         unset($query['lang']);
        //     }

        //     if (isset($query['name']))
        //     {
        //         $segments[] = JFilterOutput::stringURLUnicodeSLug($query['name']);
        //         unset($query['name']);
        //     }

        //     if (isset($query['user']))
        //     {
        //         $segments[] = $query['user'];
        //         unset($query['user']);
        //     }



        //     if (isset($query['id']))
        //     {
        //         $sql_query = "SELECT vc.idcat AS catid, v.vtitle"
        //                 . "\n FROM #__vehiclemanager_vehicles AS v"
        //                 . "\n LEFT JOIN #__vehiclemanager_categories AS vc ON vc.iditem=v.id"
        //                 . "\n LEFT JOIN #__vehiclemanager_main_categories AS vmc ON vmc.id=vc.idcat"
        //                 . "\n WHERE v.id = " . intval($query['id']);
        //         $db->setQuery($sql_query);
        //         $row = null;
        //         $row = $db->loadObject();
        //         if (isset($row))
        //         {
        //             $temp_title = JFilterOutput::stringURLUnicodeSLug($row->vtitle);
        //             if (isset($temp))
        //             {
        //                 $sql_query = "SELECT name, title FROM #__vehiclemanager_main_categories WHERE id = " . $row->catid;
        //                 $db->setQuery($sql_query);
        //                 $row = $db->loadObject();
        //                 if (isset($row))
        //                 {
        //                     $segments[] = $row->title;
        //                 }
        //             }
        //             $segments[] = $query['id']; //for back up in parseroute
        //             $segments[] = $temp_title;
        //             unset($query['id']);
        //         }
        //     }

        //     if (isset($query['start']))
        //     {
        //         $segments[] = $query['start'];
        //         if (isset($query['limitstart']))
        //         {
        //             $segments[] = $query['limitstart'];
        //             unset($query['limitstart']);
        //         }
        //         else
        //         {
        //              $segments[] = $query['start'];
        //         }
        //         unset($query['start']);
        //     }
        //     elseif(isset($query['limitstart']))
        //     {
        //         $segments[] = $query['limitstart'];
        //         unset($query['limitstart']);
        //     }

        //     if (isset($query['searchtext']))
        //     {
        //         $segments[] = $query['searchtext'];
        //         unset($query['searchtext']);
        //     }

        //     if (isset($query['searchtype']))
        //     {
        //         $segments[] = $query['searchtype'];
        //         unset($query['searchtype']);
        //     }
        // }

         // print_r("VehicleManagerBuildRoute end") ;
         // print_r($segments) ;


        return $segments;
    }

/**
 * Parse the segments of a URL.
 * */
    public function parse(&$segments)
    {
        pu_log_vm("VehicleManagerRouter:parse 1:",$segments);
        // exit;

        $vars = array();
        $db = JFactory::getDBO();

        $sefurl = new mosVmSefurl($db);
        $ser_segments = serialize($segments) ;
        $hash_segments = md5( $ser_segments );
        $sefurl = $sefurl->loadByHashUrlSegments($hash_segments);
        if($sefurl != null ) {
            foreach ($segments as $key => $value) {
                unset($segments[$key]);
            }

            $vars = unserialize( $sefurl->ser_vars ) ;
pu_log_vm("VehicleManagerRouter:parse 2:",$vars);            
            return $vars;
        }


//old code - if some error :
        // $db = JFactory::getDBO();
        // $vars = array();

        // $count = count($segments);
        // $vars['option'] = 'com_vehiclemanager';

        // $app = JFactory::getApplication();
        // $menu = $app->getMenu();    
        // if( !is_numeric($segments[0] ) ){
        //     array_unshift($segments, "0" );
        // }


        // if( !is_numeric($segments[0] ) ){
        //     $vars['Itemid'] = $menu->getDefault()->id;
        // } else 
        // {
        //     $menu->setActive($segments[0]);
        //     $vars['Itemid'] = $segments[0];

        // }

        // if ((@$segments[1] == "alone_category" || @$segments[1] == "showCategory") && isset($segments[2]))
        // {
        //     $vars['task'] = "alone_category";
        //     $vars['catid'] = @$segments[2];

        //     if (isset($segments[4]))
        //     {
        //         $vars['start'] = $segments[4];
        //     } else
        //     {
        //         unset($vars['start']);
        //     }

        //     if (isset($segments[5]) )
        //     {
        //         $vars['limitstart'] = $segments[5];
        //     } else
        //     {
        //         unset($vars['limistart']);
        //     }

        // } elseif (@$segments[1] == "all_categories"){

        //     $vars['task'] = 'all_categories';
        //     $vars['lang'] = 'en';

        // }
        // elseif (@$segments[1] == "all_vehicle") {
        //     $vars['task'] = @$segments[1];
        //       if(isset($segments[2]))   $vars['limitstart'] = $segments[2];
        //      }
        // elseif (@$segments[1] == "my_vehicles") {
        //     $vars['task'] = @$segments[1];

        //     if (isset($segments[2]))
        //     {
        //         $vars['start'] = $segments[2];
        //     } else
        //     {
        //         unset($vars['start']);
        //     }
        //     if (isset($segments[3]))
        //     {
        //         $vars['limitstart'] = $segments[3];
        //     } else
        //     {
        //         unset($vars['limitstart']);
        //     }
        // }
        // elseif (@$segments[1] == "view_vehicle" && isset($segments[4]))
        // {
        //     $vars['task'] = "view_vehicle";
        //     $vars['id'] = (int) $segments[4];
        //     $vars['catid'] =  (int) $segments[2];
        // } elseif (@$segments[1] == "Search")
        // {
        //     $vars['task'] = "search";
        //     if (isset($segments[4]))
        //     {
        //         $vars['searchtext'] = $segments[4];
        //     }

        //     if (isset($segments[5]))
        //     {
        //         $vars['searchtype'] = $segments[5];
        //     }
        //     if (isset($segments[2]))
        //     {
        //         $vars['start'] = $segments[2];
        //     } else
        //     {
        //         unset($vars['start']);
        //     }
        //     if (isset($segments[3]))
        //     {
        //         $vars['limitstart'] = $segments[3];
        //     } else
        //     {
        //         unset($vars['limistart']);
        //     }
        // } elseif (@$segments[1] == "show_search_vehicle")
        // {
        //     $vars['task'] = "show_search_vehicle";
        //     if (isset($segments[4]))
        //     {
        //         $vars['searchtext'] = $segments[4];
        //     }
        //     if (isset($segments[5]))
        //     {
        //         $vars['searchtype'] = $segments[5];
        //     }
        //     if (isset($segments[2]))
        //     {
        //         $vars['start'] = $segments[2];
        //     } else
        //     {
        //         unset($vars['start']);
        //     }
        //     if (isset($segments[3]))
        //     {
        //         $vars['limitstart'] = $segments[3];
        //     } else
        //     {
        //         unset($vars['limistart']);
        //     }
        // } elseif (@$segments[1] == "search" || @$segments[1] == "search_vehicle")
        // {
        //     $vars['task'] = @$segments[1];
        //     if (isset($segments[4]))
        //     {
        //         $vars['searchtext'] = $segments[4];
        //     }
        //     if (isset($segments[5]))
        //     {
        //         $vars['searchtype'] = $segments[5];
        //     }
        //     if (isset($segments[2]))
        //     {
        //         $vars['start'] = $segments[2];
        //     } else
        //     {
        //         unset($vars['start']);
        //     }
        //     if (isset($segments[3]))
        //     {
        //         $vars['limitstart'] = $segments[3];
        //     } else
        //     {
        //         unset($vars['limistart']);
        //     }
        // } elseif (@$segments[1] == "show_rss_categories")
        // {
        //     $vars['task'] = "show_rss_categories";
        // } elseif (@$segments[1] == "buy_now")
        // {
        //     $vars['task'] = "new_url";
        //     if (isset($segments[2]))
        //     {
        //         $vars['id'] = $segments[2];
        //     }
        // } elseif (@$segments[1] == "buy_now_for_vm")
        // {
        //     $vars['task'] = "new_url_for_vm";
        //     if (isset($segments[2]))
        //     {
        //         $vars['id'] = $segments[2];
        //     }
        // } elseif (@$segments[1] == "view_user_vehicles")
        // {
        //     $vars['task'] = "view_user_vehicles";
        //     $vars['limit'] = isset($segments[3]) ? $segments[3] : null;
        //     $vars['limitstart'] = isset($segments[4]) ? $segments[4] : null;
        //     if (isset($segments[2]))
        //     {
        //         $vars['name'] = $segments[2];
        //     }
        // } elseif (@$segments[1] == "owner_vehicles")
        // {
        //     $vars['task'] = "owner_vehicles";

        //     if (isset($segments[2]) and ( (int)$segments[2] == 0 )   )
        //     {
        //         $vars['name'] = $segments[2];
        //         if (isset($segments[3]))
        //         {
        //             $vars['start'] = $segments[3];
        //         } else
        //         {
        //             unset($vars['start']);
        //         }
        //         if (isset($segments[4]))
        //         {
        //             $vars['limitstart'] = $segments[4];
        //         } else
        //         {
        //             unset($vars['limitstart']);
        //         }

        //     } else{
        //         if (isset($segments[2]))
        //         {
        //             $vars['start'] = $segments[2];
        //         } else
        //         {
        //             unset($vars['start']);
        //         }
        //         if (isset($segments[3]))
        //         {
        //             $vars['limitstart'] = $segments[3];
        //         } else
        //         {
        //             unset($vars['limitstart']);
        //         }
        //     }
        // }
        // elseif(@$segments[1] == "rent_requests_vehicle"){
        //     $vars['task'] = "rent_requests_vehicle";
        //     if (isset($segments[2])) {
        //         $vars['start'] = $segments[2];
        //     } else {
        //         unset($vars['start']);
        //     }
        //     if (isset($segments[3])) {
        //         $vars['limitstart'] = $segments[3];
        //     } else {
        //         unset($vars['limistart']);
        //     }
        // }elseif(@$segments[1] == "buying_requests_vehicle"){
        //     $vars['task'] = "buying_requests_vehicle";
        //     if (isset($segments[2])) {
        //         $vars['start'] = $segments[2];
        //     } else {
        //         unset($vars['start']);
        //     }
        //     if (isset($segments[3])) {
        //         $vars['limitstart'] = $segments[3];
        //     } else {
        //         unset($vars['limistart']);
        //     }
        // }elseif(@$segments[1] == "rent_history_vehicle"){
        //     $vars['task'] = "rent_history_vehicle";
        //     if (isset($segments[2])) {
        //         $vars['start'] = $segments[2];
        //     } else {
        //         unset($vars['start']);
        //     }
        //     if (isset($segments[3])) {
        //         $vars['limitstart'] = $segments[3];
        //     } else {
        //         unset($vars['limistart']);
        //     }
        // }
        // elseif (@$segments[1] == "show_my_cars")
        // {
        //     $vars['task'] = "show_my_cars";
        //     $vars['limit'] = isset($segments[3]) ? $segments[3] : null;
        //     $vars['limitstart'] = isset($segments[4]) ? $segments[4] : null;
        //     if (isset($segments[2]))
        //     {
        //         $vars['name'] = str_replace(":"," ", $segments[2]);
        //     }
        // }

        // elseif (@$segments[1] == "lend_history")
        // {
        //     $vars['task'] = "lend_history";
        //     $vars['name'] = $segments[2];
        //     $vars['user'] = $segments[3];
        // } elseif (@$segments[1] == "edit_my_cars"  )
        // {
        //     $vars['task'] = "edit_my_cars";
        //     if (isset($segments[2]))
        //     {
        //         $vars['start'] = $segments[2];
        //     } else
        //     {
        //         unset($vars['start']);
        //     }
        //     if (isset($segments[3]))
        //     {
        //         $vars['limitstart'] = $segments[3];
        //     } else
        //     {
        //         unset($vars['limistart']);
        //     }
        // } elseif (@$segments[1] == "lend_requests")
        // {
        //     $vars['task'] = "lend_requests";
        //     if (isset($segments[2]))
        //     {
        //         $vars['start'] = $segments[2];
        //     } else
        //     {
        //         unset($vars['start']);
        //     }
        //     if (isset($segments[3]))
        //     {
        //         $vars['limitstart'] = $segments[3];
        //     } else
        //     {
        //         unset($vars['limistart']);
        //     }
        // } elseif (@$segments[1] == "mdownload")
        // {
        //     $vars['task'] = "mdownload";
        //     if (isset($segments[2]))
        //     {
        //         $vars['id'] = $segments[2];
        //     }
        // } elseif (@$segments[1] == "edit_vehicle")
        // {
        //         $vars['task'] = "edit_vehicle";
        //         if (isset($segments[2]))
        //         $vars['id'] = $segments[2];
        //         $vars['Itemid'] = $segments[0];

        // } else
        // {
        //     $vars['task'] = @$segments[1];
        // }


        // foreach ($segments as $key => $value) {
        //     unset($segments[$key]);
        // }

                // ECHO "   ++++end VehicleManagerParseRoute    ----  ";
                // print_r($vars);
             //exit;
        return $vars;
    }
}




function VehicleManagerBuildRoute(&$query)
{
    $app = JFactory::getApplication();
    $router = new VehicleManagerRouter($app, $app->getMenu('site'));

    return $router->build($query);
}

function VehicleManagerParseRoute($segments)
{
    $app = JFactory::getApplication();
    $router = new VehicleManagerRouter($app, $app->getMenu('site'));

    return $router->parse($segments);
}
