<?php require_once("../../classes/DBConM.php");
	// Report all errors except E_NOTICE   
	error_reporting(E_ALL ^ E_NOTICE);
	//ini_set("error_reporting","E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED");

class Audit{
	public $dbcon 			= "";
	public $vinsrc 			= "";
	public $colname 		= "mpts_result";
	public $colname2 		= "mpts_result_2";
	private $formula 		= "";
	private $ko_convrate 	= 1.11;
	private $uid			= "";
	private $username		= "";
	private $uname			= "";
	private $uphone			= "";
	private $umail			= "";
	private $utype			= "";
	
	private $userbal		= 0;
	private $debitamt		= 1;
	private $lookfor		= array("O","I");
	private $replace		= array("0","1");
	
	private $region			= "";
	private $ops			= "";
	private $opstype		= "";
	
	private $vin			= 'N/A';
	private $coorigin		= "";
	private $refid			= "";
	private $transactdate	= "";
	private $baseurl		= 'http://www.decodethis.com/webservices/api.ashx?apikey=737faa81-d5ec-4abf-aa55-b704199137be&format=JSONP&vin=';

	private $director		= "";
	
	private $make			= "";
	private $makecode		= "";
	private $makeref		= "";
	private $model			= "";
	private $series			= "";
	private $modelseries 	= "";
	private $body 			= "";
	private $bodystyle		= "";
	private $trimcode		= "";
	
	private $seats			= "";
	private $fueltype		= "";
	private $cc 			= "";
	private $enginecapacity	= "";
	
	private $catcode		= "";
	private $category		= "";
	private $hscodecat		= "";
	
	private $measurement	= "";
	private $weight			= "";
	private $myear			= "";
	private $fyear			= "";
	
	private $doa			= "";
	private $aoa			= "";
	private $ageinmonths	= "";
	private $hscode			= "";
	private $msrp			= "";
	private $depa			= 0.0;
	private $depperc		= 0.0;
	private $fob			= 0.0;
	private $freight		= 0.0;
	private $freightUS		= 0.0;
	private $insurance		= 0.0;
	private $iduty			= 0.0;
	private $idutyperc		= 0.0;
	private $specialtax		= 0.0;
	private $specialtaxperc	= 0.0;
	private $vat			= 0.0;
	private $nhil			= 0.0;
	private $ecowas			= 0.0;
	private $exim			= 0.0;
	private $examfee		= 0.0;
	private $shippers		= 0.0;
	private $cert			= 0.0;
	private $irs			= 0.0;
	private $processfee		= 0.0;
	private $inspectfee		= 0.0;
	private $intcharges		= 0.0;
	private $netwcharges	= 0.0;
	private $vatnet			= 0.0;
	private $nhilnet		= 0.0;
	private $overagepen		= 0.0;
	private $penaltyperc	= "";
	private $cf				= 0.0;
	private $cif			= 0.0;
	private $fxrate			= 0.0;
	private $fxrateEU		= 0.0;
	private $fxrateYE 		= 0.0;
	private $cifingh		= 0.0;
	
	private $opsstat		= 0;
	private $opsstatinfo	= "";
	private $genstatinfo	= "";
	
	private $photo			= "";
	private $dtresult		= 0.0;
	private $dtresultusd	= 0.0;
	private $dtresulteur	= 0.0;
	private $dtresultyen	= 0.0;

	private $monthAI		= 0;
	private $yearDiff		= 0;
	private $html			= "";
	private $hhtml			= "";
	private $defCapacitySet	= "";
	private $defSeatSet		= "";
	
	private $vinsub			= "";
	private $capacity		= "";
	
	private $year			= "";
	private $seriescode		= "";
	private $bodycode		= "";
	
	private $ocapacity		= "";
	
	private $etpricekey		= "";//europe temp prices table key
	private $login			= array();
	
	private $etg_header		= array();
	private $settings		= array();
	
	private $dloc			= "http://webservices.eurotaxglass.com/wsdl/specification.wsdl";
	private $suprepHTML		= "";
	private $rid			= "";
	private $refcode		= "";
	
	private $dpath			= "";
	private $linktomodel	= "";
	private $leastyr		= 1985;
	
	private $module			= "";
		
	private $rref			= "";
	
	private $vin4			= "";
	private $vds			= "";
	private $vdsplus		= "";
	private $vmiref 		= "";
	private $vdsindic		= "";
	private $yearA			= "";
	private $yearB			= "";
	private $yearfrom		= "";
	private $yearto 		= "";

	private $platform		= "";
	private $modellevelone	= "";
	private $bodytype2		= "";
	private $bodytypecode	= "";
	
	private $modelcode		= "";
	
	private $production		= "";
	
	private $moa			= "";
	private $yoa			= "";
	
	private $fS				= array("holding"=>false);
		
	private $prefixinfo		= "";
	private $natcode		= "";
	private $natcodeArr		= array();
	private $pricesArr		= array();
	private $euVAT			= 21;
	private $nationalcode	= "";

	private $doors			= 0;
	private $transmission	= 0;
	
	private $insurancelevy	= 0;
	private $vatlevy		= 0;
	private $nhillevy		= 0;
	private $intchargeslevy	= 0;
	private $ecowaslevy		= 0;
	private $ediflevy		= 0;
	private $examfeelevy	= 0;
	private $netwchargeslevy= 0;
	private $vatnetlevy		= 0;
	private $nhilnetlevy	= 0;
	private $irslevy		= 0;
	private $processfeelevy	= 0;
	private $inspectfeelevy	= 0;
	private $shipperslevy	= 0;
	private $certlevy		= 0;

	private $cifincedis		= 0;
	
	private $ship			= "";
	private $sum			= "";
	private $amb			= "";

	private $st_trim		= "";
	private $st_msrp		= 0;
	private $st_weight		= 0;

	private $selregionname	= "";

	private $transtype_count = 0;
	private $unicount 		 = 0;
	
	public function __construct($t){
		$this->dbcon = new DBConM();
		$this->vinsrc = $t;
	}

	private function loadGlobals($region){
		$this->region	= $region;
		$this->formula 	= $_POST['formula'];
		$this->ops		= $_POST['ops'];
		$this->transactdate	= date('M d, Y');

		$this->st_trim		= $_POST['st_trim'];
		$this->st_msrp		= $_POST['st_msrp'];
		$this->st_weight	= $_POST['st_weight'];
		
		$this->userbal		= 0;
		$this->debitamt		= 1;
		$this->lookfor		= array("O","I");
		$this->replace		= array("0","1");
		
		$this->vin			= str_replace($this->lookfor,$this->replace,strtoupper($_POST['vin']));
		if($this->vin=="") $this->vin = 'N/A';
		
		$this->coorigin		= $_POST['coorigin'];
		$this->refid		= $_POST['refid'];
		
		$this->baseurl		= 'http://www.decodethis.com/webservices/api.ashx?apikey=737faa81-d5ec-4abf-aa55-b704199137be&format=JSONP&vin=';		
		
		$this->make			= $_POST['make'];
		$this->model		= $_POST['model'];
		$this->series		= $_POST['series'];
		$this->bodystyle	= $_POST['bodystyle'];
		
		$this->seats		= $_POST['seats']; if($this->seats=="above 31") $showseats="1"; else $showseats="0";
		$this->fueltype		= $_POST['fueltype'];
		$this->enginecapacity	= $_POST['enginecapacity'];
		
		$this->catcode		= $_POST['catcode'];
		$this->hscodecat	= $_POST['hscodecat'];
		
		$this->measurement	= $_POST['measurement'];
		$this->weight		= $_POST['weight'];
		$this->myear		= $_POST['myear'];
		$this->fyear		= $_POST['fyear'];
		
		$this->doa			= $_POST['doa'];
		$this->aoa			= $_POST['aoa'];
		$this->ageinmonths	= (int) $_POST['ageinmonths'];
		if($this->ageinmonths==0) { $this->ageinmonths=3; }
		
		$this->msrp			= $_POST['msrp'];
		$this->photo 		= $_POST['photo'];
	
		$this->monthAI = 0; $this->yearDiff = 0;
		$this->html = ""; $this->hhtml = "_"; $this->defCapacitySet = ""; $this->defSeatSet = "";
		$this->amb 			= $_POST['amb'];
		
		switch($this->region){
			case "us":
			$this->coorigin			= "U.S.A";
			$this->vinsub			= substr($this->vin, 0, 8)."*".substr($this->vin, 9, 1);
			$this->capacity			= ((double) $this->enginecapacity)*1000;
			
			$this->year				= $_POST['year'];
			$this->makecode			= $_POST['makecode'];
			$this->seriescode		= $_POST['seriescode'];
			$this->bodycode			= $_POST['bodycode'];
			break;
			
			case "eu":
			$this->coorigin			= "Europe";
			
			$this->etpricekey		= "";
			$this->ocapacity		= $_POST['enginecapacity'];
			$this->capacity			= $_POST['enginecapacity'];
			
			$login					= array('Name'=>'Automasters', 'Password'=>'342be991');
			$origin 				= array('LoginData'=>$login, 'Signature'=>'Automastersltd');
			$this->etg_header		= array('VersionRequest'=>'1.0.6', 'Originator'=>$origin, 'Request'=>'Test');
			$this->settings			= array("ISOcountryCode"=>"BE", "ISOlanguageCode"=>"FR", "ISOcurrencyCode"=>"EUR");
			$this->dloc				= "http://webservices.eurotaxglass.com/wsdl/specification.wsdl";
			
			$this->dpath			= $_POST['dpath'];
			$this->modellevelone	= $_POST['modellevelone'];
			$this->leastyr			= 1985;
			
			$this->makecode			= $_POST['makecode'];
			$this->modelcode		= $_POST['modelcode'];
			$this->bodycode			= $_POST['bodycode'];
			
			$this->production		= $_POST['production'];
			$this->bodystyle		= $_POST['bodystyle'];
			
			$this->moa				= $_POST['moa'];
			$this->yoa				= $_POST['yoa'];
			break;
			
			case "du":
			$this->coorigin			= "Middle East";
			$this->vinsub			= substr($this->vin, 0, 6)."**".substr($this->vin, 8, 1);
			$this->ocapacity		= ((double) $_POST['enginecapacity'])*1000;
			$this->capacity			= $_POST['enginecapacity'];
			$this->weight			= $_POST['weight'];
			break;
			
			case "ko":
			$this->coorigin			= "Korea";
			$this->vinsub			= substr($this->vin, 0, 6)."**".substr($this->vin, 8, 1);
			$this->capacity			= ((double) $_POST['enginecapacity'])*1000;
			$this->ocapacity		= ((double) $_POST['enginecapacity'])*1000;
			break;

			case "ja":
			$this->coorigin			= "Japan";
			$this->year				= $_POST['myear'];
			$this->makecode			= $_POST['makecode'];
			$this->modelcode		= $_POST['modelcode'];
			$this->bodycode			= $_POST['bodycode'];

			$this->vinsub			= substr($this->vin, 0, 6)."**".substr($this->vin, 8, 1);
			$this->ocapacity		= (double) $_POST['enginecapacity'];
			$this->capacity			= $_POST['enginecapacity'];
			$this->weight			= $_POST['weight'];
			break;

			case "ch":
			$this->coorigin			= "China";
			$this->year				= $_POST['myear'];
			$this->makecode			= $_POST['makecode'];
			$this->modelcode		= $_POST['modelcode'];
			$this->bodycode			= $_POST['bodycode'];
			$this->bodystyle		= $_POST['bodystyle'];
			$this->modellevelone	= $_POST['modellevelone'];

			$this->vinsub			= substr($this->vin, 0, 8);
			$this->ocapacity		= ((double) $_POST['enginecapacity'])*1000;
			$this->capacity			= $_POST['enginecapacity'];
			$this->weight			= $_POST['weight'];
			break;
		}
	}
	
	public function eustructureddecode(){
		//VIN input...
		$this->vin 	= $_POST["vin"];
		$vin = $this->vin;
		$refid=strtoupper("A".mt_rand(100,999).substr(uniqid(),10));
		$vmi = substr($this->vin,0,3);
	
		$modulefound = false;
		$vin15 = substr($vin,0,15); $vin15ast = "***".substr($vin,3,12);
		$vin14 = substr($vin,0,14); $vin14ast = "***".substr($vin,3,11);
		$vin13 = substr($vin,0,13); $vin13ast = "***".substr($vin,3,10);
		$vin12 = substr($vin,0,12); $vin12ast = "***".substr($vin,3,9);
		$vin11 = substr($vin,0,11); $vin11ast = "***".substr($vin,3,8);
		$vin10 = substr($vin,0,10); $vin10ast = "***".substr($vin,3,7);
		$vin9  = substr($vin,0,9);  $vin9ast  = "***".substr($vin,3,6);
		$vin8  = substr($vin,0,8);  $vin8ast  = "***".substr($vin,3,5);
		$vin7  = substr($vin,0,7);  $vin7ast  = "***".substr($vin,3,4);

		$res = $this->dbcon->execute("select * from module_director where
			(len=15 and (vdsplus='{$vin15}' or vdsplus='{$vin15ast}')) or
			(len=14 and (vdsplus='{$vin14}' or vdsplus='{$vin14ast}')) or
			(len=13 and (vdsplus='{$vin13}' or vdsplus='{$vin13ast}')) or
			(len=12 and (vdsplus='{$vin12}' or vdsplus='{$vin12ast}')) or
			(len=11 and (vdsplus='{$vin11}' or vdsplus='{$vin11ast}')) or
			(len=10 and (vdsplus='{$vin10}' or vdsplus='{$vin10ast}')) or
			(len=9 and (vdsplus='{$vin9}' or vdsplus='{$vin9ast}')) or
			(len=8 and (vdsplus='{$vin8}' or vdsplus='{$vin8ast}')) or
			(len=7 and (vdsplus='{$vin7}' or vdsplus='{$vin7ast}'))");

		if($res[2]){ $res = $res[0];
			$row=$this->dbcon->nextRow($res);
			$this->module = strtoupper($row['module']);
			$this->vdsplus = $row['vdsplus'];
			$this->makeref = $row['makecode'];
			$this->platform = $row['platform'];
			$this->vds = $row['vds'];

			$this->vin4	= substr($this->vin,($row['charpos']-1),$row['len']);
		}
		if($this->module!=""){ $modulefound = true; }
				
		$rs = "";
		if($modulefound){
			switch($this->module){ 
				case "MOYDCARS": /*echo*/ $this->proceed_MOYDCARS(); break;//done
				case "MAYDCARS": /*echo*/ $this->proceed_MAYDCARS(); break;//done
				case "MAYDBENZCARS": /*echo*/ $this->proceed_MAYDBENZCARS(); break;//done
				case "MOYDVANS": /*echo*/ $this->proceed_MOYDVANS(); break;
				case "MAYDVANS": /*echo*/ $this->proceed_MAYDVANS(); break;
				case "MAYDBENZVANS": /*echo*/ $this->proceed_MAYDBENZVANS(); break;//done
				case "MAYDDAF": /*echo*/ $this->proceed_MAYDDAF(); break;
			}
		}else{
			$this->log("Not in module_director");
			//$infoset = "No module found in module_director table for the submitted combination";
			//$rs = '{"opsstat":1,"opsstatinfo":"Oops! An error occurred (NI_MODDIR)", "infoset":"'.$infoset.'"}';
		}
		//echo $rs;
	}
	
	
	public function proceed_MOYDCARS(){
		proceed_MOYDCARS:
		$this->loadGlobals('eu');
		$this->vdsplus = substr($this->vin, 0,9);
		$this->vinprefix = substr($this->vin, 0,8)."*".substr($this->vin, 9, 1);
		$this->year = (int) $_POST['year'];

		//Proceed as having year
		if($this->year>1000){//reasonable check
			$arr = array("year"=>$this->year);
			$this->find_MOYDCAR($arr);
		}else{
			$arr = array();
			$this->find_MOYDCAR($arr);
			//Check if year was found
			if($this->year>1000){
				//yes, do nothing now
			}else{
				//no
				$rs='{';
				$rs.='"module":"'.$this->module.'",';
				$rs.='"opsstat":0,';
				$rs.='"opstype":"sel_yrs",';
				$rs.='"yearfrom":'.$this->yearfrom.',';
				$rs.='"yearto":'.$this->yearto.',';
				$rs.='"opsstatinfo":"Select year in range"';
				$rs.='}';
				$this->log2("Select year in range ".$this->yearfrom." - ".$this->yearto); 
				$_POST['year'] = $this->yearfrom; goto proceed_MOYDCARS;
				//return $rs;
			}
		}

		//FINAL DETAILS
		$qpart = ""; $qpart2 = "";
		if($this->trimcode!=""){ $qpart = " and trim_code='{$this->trimcode}'"; }
		if($this->transmission!=""){ $qpart2 = " and transmission='{$this->transmission}'"; }

		$qry = "select * from europe_car_trims where model_level_one='{$this->modellevelone}' and body_code='{$this->bodycode}' and ((capacity='{$this->capacity}') or 1) and capacity<='{$this->capacity}' and fuel_type='{$this->fueltype}' and doors='{$this->doors}' and ('{$this->year}'>=atm_import_start and '{$this->year}'<=atm_import_end){$qpart}{$qpart2} order by capacity DESC";
		
		$res=$this->dbcon->execute($qry);
		if($res[2]){ $row = $this->dbcon->nextRow($res[0]);
			$this->seats = $row['no_of_seats'];
			$this->weight = $row['total_weight_in_kg'];
			$capacityIR = $row['capacity'];

			$opstype = "p2"; if($this->seats=="" || $this->seats=="0" || $this->seats==0){ $opstyp = "p1_se"; }

			$this->log("Success");
			//echo $this->find_TRIMCAR("A", $capacityIR, $opstype);
		}else{
			$this->log("Not in europe_car_trims");
			//echo $this->NOMOCO();
		}
	}

	public function find_MOYDCAR($arr){
		$res = $this->dbcon->execute("select * from moyd_cars where vinprefix='{$this->vinprefix}' limit 1");
		if($res[2]){ $res = $res[0]; $row = $this->dbcon->nextRow($res);
			$this->make				= $row['make'];
			$this->model			= $row['model'];
			$this->modellevelone	= $row['model_level_one'];
			$this->modelcode		= $row['model_level_one'];
			$this->fueltype			= $row['fuel_type'];

			$this->bodycode			= $row['body_code'];
			$this->body 			= $row['body'];
			$this->bodystyle 		= $row['atm_body'];
			$this->doors			= $row['doors'];
			$this->transmission 	= trim($row['transmission']);

			$this->capacity 		= $row['capacity'];
			$this->category 		= $row['category'];
			$this->year 			= $row['year']; if(isset($arr['year'])){ $this->year = $arr['year']; }
			$this->yearfrom			= $row['vdsfrom'];
			$this->yearto			= $row['vdsto'];

			$this->production 		= $this->yearfrom." - ".$this->yearto;
			$this->trimcode 		= trim($row['trim_code']);

			$this->log("Success");
		}
	}

	function proceed_MAYDCARS(){
		proceed_MAYDCARS:
		$this->loadGlobals('eu'); $series = substr($this->vin,9,8); $this->year = (int) $_POST['year'];

		//Proceed as having year
		if($this->year>1000){//reasonable check
			$arr = array();
			$this->find_MAYDCAR($arr);
		}else{
			//Check if year is decodable
			$res = $this->dbcon->execute("select year from yeartable where make_code='{$this->makeref}' and vdsplus='{$this->vdsplus}' and '{$this->vin4}'>=vin4from and '{$this->vin4}'<=vin4to");
			if($res[2]){
				//yes
				$res = $res[0]; $row = $this->dbcon->nextRow($res);
				$this->year = $row['year'];
				$arr = array();
				$this->find_MAYDCAR($arr);
			}else{
				//no
				$res = $this->dbcon->execute("select vdsfrom, vdsto from mayd_cars where vdsplus='{$this->vdsplus}' limit 1");
				if($res[2]){ $res = $res[0]; $row = $this->dbcon->nextRow($res);
					$rs='{';
					$rs.='"module":"'.$this->module.'",';
					$rs.='"opsstat":0,';
					$rs.='"opstype":"sel_yrs",';
					$rs.='"yearfrom":'.$row['vdsfrom'].',';
					$rs.='"yearto":'.$row['vdsto'].',';
					$rs.='"opsstatinfo":"Select year in range"';
					$rs.='}';

					$this->log2("Select year in range ".$row['vdsfrom']." - ".$row['vdsto']);
					$_POST['year'] = $row['vdsfrom']; goto proceed_MAYDCARS;
					//return $rs;
				}
			}
		}

		//FINAL DETAILS
		$qpart = ""; $qpart2 = "";
		if($this->trimcode!=""){ $qpart = " and trim_code='{$this->trimcode}'"; }
		if($this->transmission!=""){ $qpart2 = " and transmission='{$this->transmission}'"; }

		$qry = "select * from europe_car_trims where model_level_one='{$this->modellevelone}' and body_code='{$this->bodycode}' and ((capacity='{$this->capacity}') or 1) and capacity<='{$this->capacity}' and fuel_type='{$this->fueltype}' and doors='{$this->doors}' and ('{$this->year}'>=atm_import_start and '{$this->year}'<=atm_import_end){$qpart}{$qpart2} order by capacity DESC";

		//$qry = "select * from europe_car_trims where 1 order by capacity DESC";
		//echo $qry;
		$res=$this->dbcon->execute($qry);
		if($res[2]){ $row = $this->dbcon->nextRow($res[0]);
			$this->seats = $row['no_of_seats'];
			$this->weight = $row['total_weight_in_kg'];
			$capacityIR = $row['capacity'];

			$opstype = "p2"; if($this->seats=="" || $this->seats=="0" || $this->seats==0){ $opstyp = "p1_se"; }

			$this->log("Success");
			//echo $this->find_TRIMCAR("A", $capacityIR, $opstype);
		}else{
			$this->log("Not in europe_car_trims");
			//echo $this->NOMOCO();
		}
	}

	public function find_MAYDCAR($arr){
		$res = $this->dbcon->execute("select * from mayd_cars where vdsplus='{$this->vdsplus}' limit 1");
		if($res[2]){ $res = $res[0]; $row = $this->dbcon->nextRow($res);
			$this->make				= $row['make'];
			$this->model			= $row['model'];
			$this->modellevelone	= $row['model_level_one'];
			$this->modelcode		= trim($row['model_level_one']);
			$this->fueltype			= $row['fuel_type'];

			$this->bodycode			= $row['body_code'];
			$this->body 			= $row['body'];
			$this->bodystyle 		= $row['atm_body'];
		
			$this->doors			= $row['doors'];
			$this->transmission		= trim($row['transmission']);

			$this->capacity 		= $row['capacity'];
			$this->category 		= $row['category'];

			$this->yearfrom			= $row['vdsfrom'];
			$this->yearto			= $row['vdsto'];

			$this->production 		= $this->yearfrom." - ".$this->yearto;
			$this->trimcode 		= trim($row['trim_code']);
		}
	}

	function proceed_MAYDBENZCARS(){
		proceed_MAYDBENZCARS:
		$this->loadGlobals('eu'); $series = substr($this->vin,9,8);
		$extras = trim($_POST['extras']);

		if($this->unicount == 1){
			//only one item found so just go on, everything already loaded
		}else if($extras!=''){//multiple item scenario
			//restore items
			$this->transtype_count = $_POST['nooftranstypes'];

			$load = json_decode($extras,true);
			$this->make 			= $load['make'];
			$this->model 			= $load['model'];
			$this->modellevelone 	= $load['modellevelone'];
			$this->modelcode 		= $load['modelcode'];
			$this->year 			= $load['year'];
			$this->fueltype 		= $load['fueltype'];
			$this->bodycode 		= $load['bodycode'];
			$this->body 			= $load['body'];
			$this->bodystyle 		= $load['bodystyle'];
			$this->doors 			= $load['doors'];
			$this->transmission 	= $load['transmission'];
			$this->platform 		= $load['platform'];
			$this->capacity 		= $load['capacity'];
			$this->category 		= $load['category'];
			$this->yearfrom 		= $load['yearfrom'];
			$this->yearto 			= $load['yearto'];
			$this->production 		= $load['production'];
			$this->trimcode 		= $load['trimcode'];

		}else{
			$this->year = (int) $_POST['year'];
			//Proceed as having year
			if($this->year>1000){//reasonable check
				$arr = array();
				$this->find_MAYDBENZCAR($arr);
				if($this->suprepHTML!=""){
					$rs='{';
					$rs.='"module":"'.$this->module.'",';
					$rs.='"opsstat":0,';
					$rs.='"opstype":"sel_rows",';
					$rs.='"opsstatinfo":"Select the Row",';
					$rs.='"html":"'.$this->suprepHTML.'",';
					$rs.='"transmissions":'.$this->transtype_count;
					$rs.='}';
					$this->log("Multiple rows");
					return $rs;
				}
			}else{
				//Check if year is decodable
				$res = $this->dbcon->execute("select year from yeartable_benz where platform='{$this->platform}' and '{$this->vin4}'>=vin4from and '{$this->vin4}'<=vin4to");
				//$res = $this->dbcon->execute("select year from yeartable_benz where id='2'");//NATH
				if($res[2]){
					//yes
					$res = $res[0]; $row = $this->dbcon->nextRow($res);
					$this->year = $row['year'];
					$arr = array();
					$this->find_MAYDBENZCAR($arr);
					if($this->unicount == 1){
						goto FINAL_DETAILS;
					}else{
						if($this->suprepHTML!=""){
							$rs='{';
							$rs.='"module":"'.$this->module.'",';
							$rs.='"opsstat":0,';
							$rs.='"opstype":"sel_rows",';
							$rs.='"opsstatinfo":"Select the Row",';
							$rs.='"html":"'.$this->suprepHTML.'",';
							$rs.='"transmissions":'.$this->transtype_count;
							$rs.='}';
							$this->log("Multiple rows");
							return $rs;
						}
					}
				}else{
					//no
					$res = $this->dbcon->execute("select platformfrom, platformto from mayd_benz_cars where platform='{$this->platform}' limit 1");
					if($res[2]){ $res = $res[0]; $row = $this->dbcon->nextRow($res);
						$rs='{';
						$rs.='"module":"'.$this->module.'",';
						$rs.='"opsstat":0,';
						$rs.='"opstype":"sel_yrs",';
						$rs.='"yearfrom":'.$row['platformfrom'].',';
						$rs.='"yearto":'.$row['platformto'].',';
						$rs.='"opsstatinfo":"Select year in range"';
						$rs.='}';
						$this->log2("Select year in range ".$row['platformfrom']." - ".$row['platformto']);
						$_POST['year'] = $row['platformfrom']; goto proceed_MAYDBENZCARS;
						//return $rs;
					}
				}
			}
		}

		FINAL_DETAILS:
		$qpart = ""; $qpart2 = "";
		if($this->trimcode!=""){ $qpart = " and trim_code='{$this->trimcode}'"; }
		if($this->transtype_count==1 && $this->transmission!=""){ $qpart2 = " and transmission='{$this->transmission}'"; }

		$qry = "select * from europe_car_trims where model_level_one='{$this->modellevelone}' and body_code='{$this->bodycode}' and ((capacity='{$this->capacity}') or 1) and capacity<='{$this->capacity}' and fuel_type='{$this->fueltype}' and doors='{$this->doors}' and ('{$this->year}'>=atm_import_start and '{$this->year}'<=atm_import_end){$qpart}{$qpart2} order by capacity DESC";
		//$qry = "select * from europe_car_trims where id='5' or id='6' order by capacity DESC";//NATH
		$res=$this->dbcon->execute($qry);
		if($res[2]){ $row = $this->dbcon->nextRow($res[0]);
			$this->seats = $row['no_of_seats'];
			$this->weight = $row['total_weight_in_kg'];
			$capacityIR = $row['capacity'];

			$opstype = "p2"; if($this->seats=="" || $this->seats=="0" || $this->seats==0){ $opstyp = "p1_se"; }

			$this->log("Success");
			//echo $this->find_TRIMCAR("A", $capacityIR, $opstype);
		}else{
			$this->log("Not in europe_car_trims");
			//echo $this->NOMOCO();
		}
	}

	public function find_MAYDBENZCAR($arr){
		$this->suprepHTML = ""; $transtypes_arr = array(); $k=0;
		$res = $this->dbcon->execute("select * from mayd_benz_cars where platform='{$this->platform}' and vds='{$this->vds}' group by model_level_one, body_code, capacity, fuel_type, doors, trim_code");
		$R = $this->dbcon->execute("select distinct transmission from mayd_benz_cars where platform='{$this->platform}' and vds='{$this->vds}'");
		//$res = $this->dbcon->execute("select * from mayd_benz_cars where id='5' or id='5'");//NATH
		if($res[2]){
			while($row = $this->dbcon->nextRow($res[0])){ $k++;
				//the constant items
				if($k==1){
					$this->make 			= $row['make'];
					$this->model			= $row['model'];
					$this->modellevelone	= $row['model_level_one'];
					$this->modelcode		= trim($row['model_level_one']);

					//convert to JSON
					$rr ='{';
					$rr.='\"make\":\"'.$this->make.'\",';
					$rr.='\"model\":\"'.$this->model.'\",';
					$rr.='\"modellevelone\":\"'.$this->modellevelone.'\",';
					$rr.='\"modelcode\":\"'.$this->modelcode.'\",';
					$rr.='\"year\":\"'.$this->year.'\",';

					//the HTML
					$html="<h2 class='vtitle'>{$this->year} {$this->make} {$this->model}</h2>";
					$html.="<table class='selveh' cellspacing='0px'>";
					$html.="<tr>";
					$html.="<th>&nbsp;</th>";
					$html.="<th style='text-align:left'>BODY</th>";
					$html.="<th style='text-align:left'>FUELTYPE</th>";
					$html.="<th style='text-align:right'>CAPACITY</th>";
					$html.="<th style='text-align:left'>DOORS</th>";
					$html.="</tr>";
				}
				//build the items to ship
				$rs ='\"fueltype\":\"'.$row['fuel_type'].'\",';
				$rs.='\"bodycode\":\"'.$row['body_code'].'\",';
				$rs.='\"body\":\"'.$row['body'].'\",';
				$rs.='\"bodystyle\":\"'.$row['atm_body'].'\",';
				$rs.='\"doors\":\"'.$row['doors'].'\",';
				$rs.='\"transmission\":\"'.$row['transmission'].'\",';
				$rs.='\"platform\":\"'.$row['platform'].'\",';
				$rs.='\"capacity\":\"'.$row['capacity'].'\",';
				$rs.='\"category\":\"'.$row['category'].'\",';
				$rs.='\"yearfrom\":\"'.$row['platformfrom'].'\",';
				$rs.='\"yearto\":\"'.$row['platformto'].'\",';
				$rs.='\"production\":\"'.$row['platformfrom'].' - '.$row['platformto'].'\",';
				$rs.='\"trimcode\":\"'.$row['trim_code'].'\"';
				$rs.='}';

				//build static items in case just one row
				$this->fueltype 	= $row['fuel_type'];
				$this->bodycode 	= $row['body_code'];
				$this->body 		= $row['body'];
				$this->bodystyle 	= $row['atm_body'];
				$this->doors 		= $row['doors'];
				$this->transmission = $row['transmission'];
				$this->platform 	= $row['platform'];
				$this->capacity 	= $row['capacity'];
				$this->category 	= $row['category'];
				$this->yearfrom 	= $row['platformfrom'];
				$this->yearto 		= $row['platformto'];
				$this->production 	= $row['platformfrom'].' - '.$row['platformto'];
				$this->trimcode 	= $row['trim_code'];

				$html.="<tr onclick='setSelected_Row({$rr}{$rs})'>";
				$html.="<td>{$k}</td>";
				$html.="<td align='left'>".$row['body']."</td>";
				$html.="<td align='left'>".$row['fuel_type']."</td>";

				$html.="<td align='right'>".$this->formatAD($row['capacity'])."</td>";
				$html.="<td align='left'>".$row['doors']."</td>";
				$html.="</tr>";

			}
			$html.="</table>";
			$this->suprepHTML = $html;
			$this->log("Multiple rows");
		}else{
			$this->log("Not in mayd_benz_cars");
		}
		//get distinct transmission types
		while($W = $this->dbcon->nextRow($R[0])){
			$tt	= $W['transmission']; if(!in_array($tt, $transtypes_arr)){ $transtypes_arr[] = $tt; }
		}
		$this->transtype_count = sizeof($transtypes_arr);
		if($k==1){ $this->unicount = 1; }
	}

	public function proceed_MAYDVANS(){
		proceed_MAYDVANS:
		$this->loadGlobals('eu'); $this->year = 0;
		$series = substr($this->vin,9,8);
		$refpage = "";
		//Get details level 1
		$res = $this->dbcon->execute("select * from mayd_vans where vdsplus='{$this->vdsplus}' and (series_b<='{$series}' and series_e>='{$series}') limit 1");
		if($res[2]){ $res = $res[0]; $row = $this->dbcon->nextRow($res);
			$make			= $row['make'];
			$makecode		= $row['makecode'];
			$model			= $row['model'];
			$modelcode		= $row['modelcode'];
			$body 			= $row['body'];
			$bodycode		= $row['bodycode'];
			$capacity  		= $row['capacity'];
			$this->category = $row['category'];
			$fueltype  		= $row['fueltype'];
			$platform		= $row['platform'];

			$this->year			 = $row['year'];
			$this->yearfrom		 = $row['modelfrom'];
			$this->yearto		 = $row['modelto'];
			$this->production = $this->yearfrom." - ".$this->yearto;
		}else{
			$this->log("Not in mayd_benz_cars");
			return '{"opsstat":1,"opsstatinfo":"An error occur: Code = MAVNDET001 '.$this->vmiref.' '.$this->vdsplus.'"}';
		}

		//Get year
		$yearfound=false;
		if($this->year>0){
			$yearfound=true;
		}else{
			$year = "".$_POST['year'];
			if($year!=''){ $yearfound=true; $this->year = $year; }
		}
		if(!$yearfound){
			$res = $this->dbcon->execute("select year from yeartable where platform='{$platform}' and '{$this->vin4}'>=vin4from and '{$this->vin4}'<=vin4to");
			if($res[2]){ $res = $res[0]; $row = $this->dbcon->nextRow($res); $this->year = $row['year']; }
			else{
				$rs='{';
				$rs.='"module":"'.$this->module.'",';
				$rs.='"opsstat":0,';
				$rs.='"opstype":"sel_yrs",';
				$rs.='"yearfrom":'.$this->yearfrom.',';
				$rs.='"yearto":'.$this->yearto.',';
				$rs.='"opsstatinfo":"Select year in range"';
				$rs.='}';

				$this->log2("Select year in range ".$this->yearfrom." - ".$this->yearto);
				$_POST['year'] = $this->yearfrom; goto proceed_MAYDVANS;
				//return $rs;
			}
		}
		//assume year is gotten by now
		//Get msrp and others
		$res=$this->dbcon->execute("select MIN(msrp) as msrp, capacity from europe_van_trims where makecode='{$makecode}' and modelcode='{$modelcode}' and bodycode='{$bodycode}' and year='{$this->year}' and fueltype='{$fueltype}' and capacity='{$capacity}'");
		if($res[2]){ $res = $res[0]; $row=$this->dbcon->nextRow($res); $this->msrp = $row["msrp"];
			//also get the other details there
			$res=$this->dbcon->execute("select * from europe_van_trims where makecode='{$makecode}' and modelcode='{$modelcode}' and bodycode='{$bodycode}' and year='{$this->year}' and fueltype='{$fueltype}' and capacity='{$capacity}' and msrp='{$this->msrp}'");
			if($res[2]){ $res = $res[0]; $row=$this->dbcon->nextRow($res);
				$this->trim 	= $row['trim'];
				$this->seats 	= $row['seats'];
				$this->photo 	= $row['photo'];
				$this->weight 	= $row['weight'];
				$refpage 	 	= $row['ref'];
			 }else{ $this->msrp = 0; }
			 $this->log("Success");
		}else{
			$this->log("No MSRP"); return;
			return '{"opsstat":1, "opsstatinfo":"Price not set !"}';
		}
		
		//Send output
		/*if(trim($this->photo)==""){
			$this->photo = base_url("nbase/vphotos/nophoto.jpg");
		}else{ $this->photo = base_url("nbase/vphotos/sprinters_n_vans/".$this->photo); }*/
		if(trim($_POST['seats'])==""){//should proceed to pick seats
			$this->opstype="p1_se";
		}else{//seats already picked
			$this->opstype="p2";
		}
		$rs='{';
		$rs.='"module":"'.$this->module.'",';
		$rs.='"formula":"B",';
		$rs.='"refpage":"'.$refpage.'",';
		$rs.='"dpath":1,';
		$rs.='"vin":"'.$this->vin.'",';
		$rs.='"make":"'.$make.'",';
		$rs.='"makecode":"'.$makecode.'",';
		$rs.='"model":"'.$model.'",';
		$rs.='"modelcode":"'.$modelcode.'",';
		$rs.='"msrp":"'.$this->msrp.'",';
		$rs.='"myear":"'.$this->year.'",';
		
		$rs.='"production":"'.$this->production.'",';
		$rs.='"body":"'.$body.'",';
		$rs.='"bodycode":"'.$bodycode.'",';
		$rs.='"bodystyle":"'.$body.'",';
		$rs.='"series":"'.$this->trim.'",';
		$rs.='"seatset":'.$this->getSeatSet($this->seats).',';
		
		$rs.='"cctodisp":"'.$capacity.'",';
		$rs.='"catcode":"'.$this->category.'",';
		$rs.='"enginecapacity":"'.$capacity.'",';
		$rs.='"fueltype":"'.$fueltype.'",';
		$rs.='"weight":"'.$this->weight.'",';
		
		$rs.='"photo":"'.$this->photo.'",';
		$rs.='"opsstat":0,';
		$rs.='"opstype":"'.$this->opstype.'",';
		$rs.='"opsstatinfo":""';
		$rs.='}';
		
		return $rs;
	}

	public function proceed_MAYDDAF(){
		proceed_MAYDDAF:
		$this->loadGlobals('eu');
		$refpage = "";
		$series = substr($this->vin,9,8);
		$extras = trim($_POST['extras']);
		$otherccs = true; $tcapacity = 0; $tmodel = "";

		//Get year
		$yearfound=false; $year = "".$_POST['year']; $yrreceived = false;
		if($year!=''){ $yrreceived = true; $yearfound=true; $this->year = $year; }
		else{
			$res = $this->dbcon->execute("select year, prod_from, prod_to from mayd_daf where vdsplus='{$this->vdsplus}' and (series_b<='{$series}' and series_e>='{$series}') limit 1");
			if($res[2]){ $res = $res[0]; $row = $this->dbcon->nextRow($res);
				$this->year  	= $row['year'];
				$this->yearfrom = $row['prod_from'];
				$this->yearto 	= $row['prod_to'];

				if($this->year>1){
					$yearfound = true;
				}else{
					$rs='{';
					$rs.='"module":"'.$this->module.'",';
					$rs.='"opsstat":0,';
					$rs.='"opstype":"sel_yrs",';
					$rs.='"yearfrom":'.$this->yearfrom.',';
					$rs.='"yearto":'.$this->yearto.',';
					$rs.='"opsstatinfo":"Select year in range"';
					$rs.='}';
					$this->log2("Select year in range ".$this->yearfrom." - ".$this->yearto);
					$_POST['year'] = $this->yearfrom; goto proceed_MAYDDAF;
					//return $rs;
				}
			}
		}
		$yearcond = " and year='{$this->year}'"; if($yrreceived){ $yearcond = ""; }
		if($yearfound){
			if($extras==""){
				$capacitycond = "";
				$qry = "select * from mayd_daf where vdsplus='{$this->vdsplus}'{$yearcond} group by model";
			}else{
				//selected model and capacity received
				$t = explode("|",$extras); $this->model = $t[0]; $this->capacity = $t[1];
				$capacitycond = " and capacity='{$this->capacity}'";
				$qry = "select * from mayd_daf where vdsplus='{$this->vdsplus}' and model='{$this->model}'{$yearcond} group by model";
			}
			//Get the details level 1
			$res = $this->dbcon->execute($qry);
			if($res[2]){ $res = $res[0]; $k=-1; $jsarr='[';
				while($row = $this->dbcon->nextRow($res)){
					if($k==-1){
						$this->yearfrom	= $row['prod_from'];
						$this->yearto	= $row['prod_to'];
						$this->production = $this->yearfrom." - ".$this->yearto;

						$cabin			= $row['cabin'];
						$atm_axel 		= $row['atm_axel'];
						$engine_power  	= $row['engine_power'];
					}
					$make 	 = $row["make"];
					$model 	 = $row["model"];
					$series  = $row["series"];
					$axel 	 = $row["atm_axel"];
					$type 	 = $row["type"];
					
					//Check if multiple (find out if other capacities and attach them)
					$r = $this->dbcon->execute("select * from daf_trims where make='{$make}' and type='{$type}' and model='{$model}' and year='{$this->year}' and axel='{$axel}'{$capacitycond} group by capacity order by capacity ASC");
					if($r[2]){
						$r = $r[0];
						while($w=$this->dbcon->nextRow($r)){ $tmodel = $model; $tcapacity = $w['capacity'];
							$k++;
							$rs='{';
							$rs.='"model":"'.$tmodel.'",';
							$rs.='"capacity":"'.$tcapacity.'"';
							$rs.='}';
							$jsarr.=$rs.',';
						}
					}//else{ return '{"opsstat":1,"opsstatinfo":"An error occur: Code = MADASUBDET001"}'; }
				}
				$jsarr=$this->removeLastComma($jsarr);
				$jsarr .=']';

				if($k>1){//user got to select one
					$this->opstype = "p1_dafbstyles";
					$rs='{';
					$rs.='"module":"'.$this->module.'",';
					$rs.='"dpath":"DAF",';
					$rs.='"vin":"'.$this->vin.'",';
					$rs.='"make":"'.$make.'",';
					$rs.='"type":"'.$type.'",';
					$rs.='"model":"'.$series.'",';
					$rs.='"myear":"'.$this->year.'",';
					$rs.='"axel":"'.$axel.'",';
					
					$rs.='"bstyles":'.$jsarr.',';
					$rs.='"opstype":"'.$this->opstype.'",';
					$rs.='"opsstat":0,';
					$rs.='"opsstatinfo":""';
					$rs.='}';
					
					$this->log("Success");
					return $rs;
				}else{
					$this->log("Success");
					$this->opstype = "p2";
				}
			}else{
				$this->log("Not in module_director");
				return '{"opsstat":1,"opsstatinfo":"An error occur: Code = MADADET001 '.$series.' '.$this->vdsplus.'"}';
			}
		}else{ return '{"opsstat":1,"opsstatinfo":"An error occur: Code = NOYR"}'; }

		$res=$this->dbcon->execute("select * from daf_trims where make='{$make}' and type='{$type}' and model='{$model}' and year='{$this->year}' and axel='{$axel}' and capacity='{$tcapacity}' order by msrp ASC limit 1");
		if($res[2]){
			$res = $res[0]; $row=$this->dbcon->nextRow($res);
			$this->trim 	= $row['axel']." ".$cabin;
			$axel 			= $row['axel'];
			$seats 			= $row['seats'];
			$capacity 		= $row['capacity'];
			$this->category = $row['category'];
			$this->photo 	= "";
			$this->weight 	= $row['atm_tonnes'];
			$this->msrp 	= $row['msrp'];
			$fueltype 		= $row['fueltype'];
			$refpage 	 	= $row['ref'];
		}else{ return '{"opsstat":1,"opsstatinfo":"An error occur: Code = TRIDAF001 '.$series.' '.$this->vdsplus.'"}'; }
		
		//Send output
		/*if(trim($this->photo)==""){
			$this->photo = base_url("nbase/vphotos/nophoto.jpg");
		}else{ $this->photo = base_url("nbase/vphotos/sprinters_n_vans/".$this->photo); }*/
		$this->opstype="p2";

		$rs='{';
		$rs.='"module":"'.$this->module.'",';
		$rs.='"formula":"C",';
		$rs.='"refpage":"'.$refpage.'",';
		$rs.='"dpath":"DAF",';
		$rs.='"vin":"'.$this->vin.'",';
		$rs.='"make":"'.$make.'",';
		$rs.='"type":"'.$type.'",';
		$rs.='"model":"'.$model.'",';
		$rs.='"msrp":"'.$this->msrp.'",';
		$rs.='"myear":"'.$this->year.'",';
		$rs.='"axel":"'.$axel.'",';
		
		$rs.='"production":"'.$this->production.'",';
		$rs.='"series":"'.$this->trim.'",';
		$rs.='"bodystyle":"'.$this->trim.'",';
		$rs.='"seatset":'.$this->getSeatSet($this->seats).',';
		
		$rs.='"cctodisp":"'.$capacity.'",';
		$rs.='"catcode":"'.$this->category.'",';
		$rs.='"enginecapacity":"'.$capacity.'",';
		$rs.='"seats":"'.$seats.'",';
		$rs.='"seatstodisp":"'.$seats.'",';
		$rs.='"fueltype":"'.$fueltype.'",';
		$rs.='"weight":"'.$this->weight.'",';

		$rs.='"cabin":"'.$cabin.'",';
		$rs.='"axel":"'.$atm_axel.'",';
		$rs.='"engine_power":"'.$engine_power.'",';
		
		$rs.='"photo":"'.$this->photo.'",';
		$rs.='"opsstat":0,';
		$rs.='"opstype":"'.$this->opstype.'",';
		$rs.='"opsstatinfo":""';
		$rs.='}';
		$this->log("Success");
		return $rs;
	}

	public function proceed_MOYDVANS(){
		proceed_MOYDVANS:
		$this->loadGlobals('eu');
		$refpage = "";
		//Get details level 1
		$this->vinprefix = substr($this->vin,0,8)."*".substr($this->vin, 9, 1);
		
		//Get the details level 1
		$res = $this->dbcon->execute("select * from moyd_vans where vdsplus='{$this->vdsplus}' and vinprefix='{$this->vinprefix}'");
		if($res[2]){ $res = $res[0]; $row = $this->dbcon->nextRow($res);
			$make			= $row['make'];
			$makecode		= $row['makecode'];
			$model			= $row['model'];
			$modelcode		= $row['modelcode'];
			$body 			= $row['body'];
			$bodycode		= $row['bodycode'];
			$capacity  		= $row['capacity'];
			$this->category = $row['category'];
			$fueltype  		= $row['fueltype'];
			$year			= $row['year'];

			$this->yearfrom		 = $row['modelfrom'];
			$this->yearto		 = $row['modelto'];
			$this->production = $this->yearfrom." - ".$this->yearto;
		}else{
			$this->log("Not in moyd_vans");
			return '{"opsstat":1,"opsstatinfo":"An error occur: Code = MAVNDET001 '.$this->vmiref.' '.$this->vdsplus.'"}';
		}

		//Get msrp and others
		$res=$this->dbcon->execute("select MIN(msrp) as msrp, capacity from europe_van_trims where makecode='{$makecode}' and modelcode='{$modelcode}' and bodycode='{$bodycode}' and year='{$year}' and fueltype='{$fueltype}' and capacity='{$capacity}'");
		if($res[2]){ $res = $res[0]; $row=$this->dbcon->nextRow($res); $this->msrp = $row["msrp"];
			//also get the other details there
			$res=$this->dbcon->execute("select * from europe_van_trims where makecode='{$makecode}' and modelcode='{$modelcode}' and bodycode='{$bodycode}' and year='{$year}' and fueltype='{$fueltype}' and capacity='{$capacity}' and msrp='{$this->msrp}'");
			if($res[2]){ $res = $res[0]; $row=$this->dbcon->nextRow($res);
				$this->trim 	= $row['trim'];
				$this->seats 	= $row['seats'];
				$this->photo 	= $row['photo'];
				$this->weight 	= $row['weight'];
				$refpage 	 	= $row['ref'];
			 }else{ $this->msrp = 0; }
		}else{
			$this->log("No MSRP");
			return '{"opsstat":1, "opsstatinfo":"Price not set !"}';
		}
		
		//Send output
		/*if(trim($this->photo)==""){
			$this->photo = base_url("nbase/vphotos/nophoto.jpg");
		}else{ $this->photo = base_url("nbase/vphotos/sprinters_n_vans/".$this->photo); }*/
		if(trim($_POST['seats'])==""){//should proceed to pick seats
			$this->opstype="p1_se";
		}else{//seats already picked
			$this->opstype="p2";
		}
		$rs='{';
		$rs.='"module":"'.$this->module.'",';
		$rs.='"formula":"B",';
		$rs.='"refpage":"'.$refpage.'",';
		$rs.='"dpath":1,';
		$rs.='"vin":"'.$this->vin.'",';
		$rs.='"make":"'.$make.'",';
		$rs.='"makecode":"'.$makecode.'",';
		$rs.='"model":"'.$model.'",';
		$rs.='"modelcode":"'.$modelcode.'",';
		$rs.='"msrp":"'.$this->msrp.'",';
		$rs.='"myear":"'.$year.'",';
		
		$rs.='"production":"'.$this->production.'",';
		$rs.='"body":"'.$body.'",';
		$rs.='"bodycode":"'.$bodycode.'",';
		$rs.='"bodystyle":"'.$body.'",';
		$rs.='"series":"'.$this->trim.'",';
		$rs.='"seatset":'.$this->getSeatSet($this->seats).',';
		
		$rs.='"cctodisp":"'.$capacity.'",';
		$rs.='"catcode":"'.$this->category.'",';
		$rs.='"enginecapacity":"'.$capacity.'",';
		$rs.='"fueltype":"'.$fueltype.'",';
		$rs.='"weight":"'.$this->weight.'",';
		
		$rs.='"photo":"'.$this->photo.'",';
		$rs.='"opsstat":0,';
		$rs.='"opstype":"'.$this->opstype.'",';
		$rs.='"opsstatinfo":""';
		$rs.='}';
		$this->log("Success");
		return $rs;
	}

	public function proceed_MAYDBENZVANS(){
		proceed_MAYDBENZVANS:
		$this->loadGlobals('eu'); $refpage = false; //$this->opstype = "sprY";

		$res = $this->dbcon->execute("select platform, yearfrom, yearto from mayd_benz_vans where vdsplus='{$this->vdsplus}' limit 1");
		if($res[2]){ $res = $res[0]; $row = $this->dbcon->nextRow($res);
			$this->platform = $row['platform'];
			$this->yearfrom = $row['yearfrom'];
			$this->yearto = $row['yearto'];
		}else{
			$this->log("Not in mayd_benz_vans");
			echo '{"opsstat":1,"opsstatinfo":"An error occur: Code = MABNVNDET001 '.$this->vmiref.' '.$this->vdsplus.'"}';
			return;
		}
		
		//Get year
		$v = $_POST['year'];
		if(!empty($v)){ $this->year = $v; }
		else{
			$res=$this->dbcon->execute("select year from yeartable_benz where platform='{$this->platform}' and '{$this->vin4}'>=vin4from and '{$this->vin4}'<=vin4to");
			if($res[2]){ $res = $res[0]; $row = $this->dbcon->nextRow($res); $this->year = trim($row['year']); }
			else{
				$rs='{';
				$rs.='"module":"'.$this->module.'",';
				$rs.='"opsstat":0,';
				$rs.='"opstype":"sprY",';
				$rs.='"yearfrom":'.$this->yearfrom.',';
				$rs.='"yearto":'.$this->yearto.',';
				$rs.='"opsstatinfo":"Select year in range"';
				$rs.='}';
				$this->log2("Select year in range ".$this->yearfrom." - ".$this->yearto);
				$_POST['year'] = $this->yearfrom; goto proceed_MAYDBENZVANS;
				//return $rs;
			}
		}
		$lowermatch = false;
		$res=$this->dbcon->execute("select distinct body from sprinter_trims where vdsplus='{$this->vdsplus}' and year='{$this->year}'");
		$bstyles='[]';

		if($res[2]){
			$nobs = $res[1];
			if($nobs==0){
				$this->log("Not in sprinter_trims");
				//echo $this->NOMOCO_SPRINTER();
			}
			elseif($nobs==1){//one body
				$res=$this->dbcon->execute("select * from sprinter_trims where vdsplus='{$this->vdsplus}' and year='{$this->year}'");
				if($res[2]){ $res = $res[0]; $row = $this->dbcon->nextRow($res);
					$this->make = $row['make'];
					$this->model = $row['model'];
					$this->category = $row['category'];
					$this->fueltype = $row['fuel'];
					$this->seats = $row['seats'];
					$this->vdsplus = $row['vdsplus'];
					$this->body = $row['body'];
					$this->capacity = $row['capacity'];
					$this->msrp = $row['msrp'];
					$this->photo = trim($row['photo']);
					$this->refcode = $row['page'];

					$this->weight = $row['weight'];
					$this->production = $row['vinfrom']." - ".$row['vinto'];
					$refpage = $row['page'];

					/*if($this->photo=="") $this->photo = base_url("nbase/vphotos/nophoto.jpg"); else $this->photo = base_url("nbase/vphotos/sprinters_n_vans/".$this->photo);*/
				}
				$bstyles='[]';
				if($this->seats=="") $this->opstype="p1_se"; else $this->opstype="p2";
			}else{//several bodies
				$res = $res[0];
				$jsarr='['; $k=-1;
				while($row=$this->dbcon->nextRow($res)){
					$this->body = $row['body'];
					//$rs = $this->dbcon->execute("select * from sprinter_trims where body='{$this->body}' and vdsplus='{$this->vdsplus}' and year='{$this->year}'");
					$rs = $this->dbcon->execute("select * from sprinter_trims where vdsplus='{$this->vdsplus}' and year='{$this->year}' limit 1");
					if($rs[2]){ $rs = $rs[0];
						$rw = $this->dbcon->nextRow($rs);
						$this->seats = $rw['seats'];
						
						$this->photo = trim($rw["photo"]);
						/*if($this->photo==""){
							$this->photo = base_url("nbase/vphotos/nophoto.jpg");
						}else{
							$this->photo = base_url("nbase/vphotos/sprinters_n_vans/".$this->photo);
						}*/
						$this->weight = $rw['weight'];
						$this->production = $rw['vinfrom']." - ".$rw['vinto'];
						$refpage = $rw['page'];

						$k++;
						$rs='{';
						$rs.='"photo":"'.$this->photo.'",';
						$rs.='"year":"'.$this->year.'",';
						$rs.='"make":"'.$rw["make"].'",';
						$rs.='"model":"'.$this->cleanUp($rw["model"]).'",';
						$rs.='"body":"'.$this->cleanUp($this->body).'",';
						$rs.='"enginecapacity":'.$rw["capacity"].',';
						$rs.='"fueltype":"'.$rw["fuel"].'",';
						$rs.='"seats":"'.$rw["seats"].'",';
						$rs.='"seatstodisp":"'.$rw["seats"].'",';
						$rs.='"msrp":"'.$rw["msrp"].'",';
						$rs.='"refcode":"'.$rw["page"].'",';
						$rs.='"category":"'.$rw["category"].'"';
						$rs.='}';
						
						$jsarr.=$rs;
						$jsarr.=',';
					}
				}
				$jsarr=$this->removeLastComma($jsarr); $jsarr.=']';
				$bstyles = $jsarr;
				$this->opstype="p2";
				//$this->opstype = "sprA";
			}
		}else{
			$this->log("Not in sprinter_trims");
			//echo $this->NOMOCO_SPRINTER();
			return '{"opsstat":1,"opsstatinfo":"An error occur: Code = SPRBODS001"}';
		}
		
		//Send output
		$rs='{';
		$rs.='"opstype":"'.$this->opstype.'",';
		$rs.='"vdsplus":"'.$this->vdsplus.'",';
		$rs.='"refpage":"'.$refpage.'",';
		$rs.='"module":"'.$this->module.'",';
		$rs.='"formula":"Z",';
		$rs.='"dpath":7,';
		$rs.='"vin":"'.$this->vin.'",';
		$rs.='"make":"'.$this->make.'",';
		$rs.='"makecode":"'.$this->vmiref.'",';
		$rs.='"model":"'.$this->model.'",';
		$rs.='"msrp":"'.$this->msrp.'",';
		$rs.='"myear":"'.$this->year.'",';
		
		$rs.='"production":"'.$this->production.'",';
		$rs.='"bstyles":'.$bstyles.',';
		$rs.='"body":"'.$this->body.'",';
		$rs.='"bodystyle":"'.$this->body.'",';
		$rs.='"series":"'.$this->body.'",';
		$rs.='"seats":"'.$this->seats.'",';
		$rs.='"seatstodisp":"'.$this->seats.'",';
		$rs.='"seatset":'.$this->getSeatSet($this->seats).',';
		$rs.='"capacityset":'.$this->getCapacitySet($this->capacity).',';
		
		$rs.='"cctodisp":"'.$this->capacity.'",';
		$rs.='"catcode":"'.$this->category.'",';
		$rs.='"enginecapacity":"'.$this->capacity.'",';
		$rs.='"fueltype":"'.$this->fueltype.'",';
		$rs.='"refcode":"'.$this->refcode.'",';

		$rs.='"weight":"'.$this->weight.'",';
		$rs.='"production":"'.$this->production.'",';
		
		$rs.='"photo":"'.$this->photo.'",';
		$rs.='"opsstat":0,';
		$rs.='"opstype":"'.$this->opstype.'",';
		$rs.='"opsstatinfo":""';
		$rs.='}';
		$this->log("Success");
		return $rs;
	}


	private function find_TRIMCAR($formula, $capacityIR, $opstype){
		//get all bodystyles
		$P = array(
			"region" 	=> "eu",
			"formula"	=> $formula,
			"make" 		=> $this->make,
			"makecode"	=> $this->makeref,
			"model" 	=> $this->model,
			"modellevelone" => $this->modellevelone,
			"modelcode" => $this->modellevelone,
			"year" 		=> $this->year,
			"bodycode" 	=> $this->bodycode,
			"body" 		=> $this->body,
			"fueltype"	=> $this->fueltype,
			"capacity" 	=> $capacityIR,
			"seats" 	=> $this->seats,
			"doors" 	=> $this->doors,
			"trimcode"	=> $this->trimcode,
			"transmission"	=> $this->transmission
			//"type" => "eu","axel" => "eu",
		);
		//$bssplus = $this->getvinselectable_trims($P);

		$rs='{';
		$rs.='"module":"'.$this->module.'",';
		$rs.='"formula":"'.$formula.'",';
		$rs.='"dpath":2,';
		$rs.='"vin":"'.$this->vin.'",';
		$rs.='"make":"'.$this->make.'",';
		$rs.='"makecode":"'.$this->makeref.'",';
		$rs.='"model":"'.$this->model.'",';
		$rs.='"modellevelone":"'.$this->modellevelone.'",';
		$rs.='"modelcode":"'.$this->modelcode.'",';
		
		$rs.='"myear":"'.$this->year.'",';
		$rs.='"fyear":"'.$this->year.'",';
		$rs.='"yearA":"'.$this->yearfrom.'",';
		$rs.='"yearB":"'.$this->yearto.'",';
		
		$rs.='"production":"'.$this->production.'",';
		$rs.='"body":"'.$this->body.'",';
		$rs.='"bodystyle":"'.$this->bodystyle.'",';
		$rs.='"bodystyle2":"'.$this->bodystyle.'",';
		$rs.='"bodycode":"'.$this->bodycode.'",';
		//$rs.='"bsscount":"'.$bssplus['count'].'",';
		//$rs.='"bss1json":'.$bssplus['bss1json'].',';
		$rs.='"doors":"'.$this->doors.'",';
		$rs.='"seats":'.$this->seats.',';
		$rs.='"seatstodisp":'.$this->seats.',';
		$rs.='"weight":'.$this->weight.',';
		
		$rs.='"cc":"'.$this->capacity.'",';
		$rs.='"ccToDisp":"'.$this->capacity.'",';
		$rs.='"catcode":"'.$this->category.'",';
		$rs.='"enginecapacity":"'.$capacityIR.'",';
		$rs.='"fueltype":"'.$this->fueltype.'",';
		$rs.='"natcode":"'.$this->natcode.'",';

		/*if($this->photo==""){ $this->photo = base_url("nbase/vphotos/nophoto.jpg"); }
		else{ $photo = base_url("vehicle_gallery/{$photo}"); }*/
		
		$rs.='"photo":"'.$this->photo.'",';
		$rs.='"opsstat":0,';
		$rs.='"opstype":"'.$opstype.'",';
		$rs.='"opsstatinfo":""';
		$rs.='}';
		$this->log("Success");
		//return $bssplus['load'].'~|~'.$rs;
		return '~|~'.$rs;
	}
	private function NOMOCO(){
		$rs='{';
		$rs.='"opstype":"nomoco",';
		$rs.='"make":"'.$this->make.'",';
		$rs.='"module":"'.$this->module.'",';
		$rs.='"model":"'.$this->model.'",';
		$rs.='"body":"'.$this->body.'",';
		$rs.='"bodystyle":"'.$this->bodystyle.'",';
		$rs.='"fueltype":"'.$this->fueltype.'",';
		$rs.='"enginecapacity":"'.$this->capacity.'",';
		$rs.='"myear":"'.$this->year.'",';
		$rs.='"opsstat":0';
		$rs.='}';
		return '~|~'.$rs;
	}
	private function NOMOCO_SPRINTER(){
		$rs='{"opsstat":1, "opsstatinfo":""}';
		$res = $this->dbcon->execute("select * from sprinter_trims where vdsplus='{$this->vdsplus}'");
		if($res[2]){
			$row = $this->dbcon->nextRow($res[0]);
			$rs='{';
			$rs.='"opstype":"nomoco",';
			$rs.='"module":"'.$this->module.'",';
			$rs.='"make":"'.$row['make'].'",';
			$rs.='"model":"'.$row['model'].'",';
			$rs.='"myear":"'.$this->year.'",';
			$rs.='"body":"'.$row['body'].'",';
			$rs.='"bodystyle":"'.$row['body'].'",';
			$rs.='"fueltype":"'.$row['fuel'].'",';
			$rs.='"enginecapacity":"'.$row['capacity'].'",';
			$rs.='"opsstat":0';
			$rs.='}';
		}
		return $rs;
	}

	private function log($info){
		$this->dbcon->silentExecute("update `{$this->vinsrc}` set `{$this->colname}`='{$info}' where vin='{$this->vin}'");
	}
	private function log2($info){
		$this->dbcon->silentExecute("update `{$this->vinsrc}` set `{$this->colname2}`='{$info}' where vin='{$this->vin}'");
	}
	public function removeLastComma($s){
		$slen=strlen($s);
		if(substr($s, $slen-1)==","){ $s=substr($s, 0, $slen-1); }
		return $s;
	}
	public function getCapacitySet($cc){
		$cc=str_replace("/","|",$cc);
		$jsarr='[';
		$ccArr=explode("|",$cc);
		for($i=0; $i<sizeof($ccArr); $i++){
			$tstr=trim($ccArr[$i]);
			if($tstr=="") continue;
			$jsarr.=$tstr.',';
		}
		$jsarr=$this->removeLastComma($jsarr);
		$jsarr.=']';
		return $jsarr;
	}
	public function getSeatSet($seats){
		$jsarr='[';
		$this->seatsArr=explode("|",$seats);
		for($i=0; $i<sizeof($this->seatsArr); $i++){
			$tstr=trim($this->seatsArr[$i]);
			if($tstr=="" || (strtolower($tstr)=="no data")) continue;
			$jsarr.=$tstr.',';
		}
		$jsarr=$this->removeLastComma($jsarr);
		$jsarr.=']';
		return $jsarr;
	}
	public function getSeatsAsOptions($seats){
		$jsarr='';
		$this->seatsArr=explode("|",$seats);
		for($i=0; $i<sizeof($this->seatsArr); $i++){
			$tstr=trim($this->seatsArr[$i]);
			if($tstr=="") continue;
			$jsarr.='<option>'.$tstr.'</option>';
		}
		return $jsarr;
	}
	public function getSeatAndCCRange($cat){
		$res=$this->dbcon->execute("select * from seat_ranges where category='{$cat}' limit 1");
		$jsarr1='['; $jsarr2='[';
		if($res[2]){ $res = $res[0];
			$row = $this->dbcon->nextRow($res);
			$jsarr1.=$row["seatfrom"].','.$row["seatto"]; $jsarr2.=$row["ccfrom"].','.$row["ccto"];
		}
		$jsarr1.=']'; $jsarr2.=']';
		return array($jsarr1, $jsarr2);
	}




	public function listToSQL($arr){
		$t=""; foreach($arr as $val){ $t.="'{$val}',"; } return $this->removeLastComma($t);
	}

	public function splitToSQL($col,$val,$ops="or"){
		$arr = explode(",",$val); $s="";
		for($i=0; $i<sizeof($arr); $i++){
			$t=trim($arr[$i]);
			if($t=="") continue;
			$s.="{$col}='".$t."' {$ops} ";
		}
		$s=substr($s,0,strlen($s)-4);
		return $s;
	}

	public function verbalizeSplitToSQL($col,$val,$ops="or"){
		$s=""; $n=0;
		if(trim($val)!=""){
			$arr = explode(",",$val); $s="";
			for($i=0; $i<sizeof($arr); $i++){
				$t=trim($arr[$i]); if($t==""){ continue; }else{ $n++; $s.=$t.", "; }
			}
			$s = substr($s,0,strlen($s)-2);
			if($n>1){
				$p = strrpos($s,",");
				$s = substr($s,0,$p)." {$ops}".substr($s,$p+1);
			}
			$s = "{$col} is {$s}"; 
		}
		return $s;
	}
	public function roundToVeryNearest($d){
		$str=number_format((double) $d,2,".",""); $pos=strpos($str,"."); $wholepart=substr($str,0,$pos); $fracpart=substr($str,($pos+1));
		$fracpartAI = (int) $fracpart;
		if($fracpartAI>=50){
			$str=(((int) $wholepart)+1)."";
		}else{
			$str=$wholepart;
		}
		return $str;
	}
	public function toOneDP($d){
		return number_format((double) $d,1,".",",");
	}
	public function toZeroDP($d){
		return number_format((double) $d,0,".",",");
	}
	
	public function rem($str){
		return strip_tags(trim($str));//mysql_real_escape_string(strip_tags(trim($str)));
	}
	
	public function format($d){
		return number_format((double) $d,2,".",",");
	}
	
	public function formatAD($d){
		$str=number_format((double) $d,3,".",""); $pos=strpos($str,"."); $wholepart=substr($str,0,$pos); $fracpart=substr($str,($pos+1));
		$lastchar=substr($fracpart,2);
		if($lastchar=="0"){
			$ret=$wholepart.".".substr($fracpart,0,2);
		}else{
			$ret=$wholepart.".".(((double) substr($fracpart,0,2)) + 1);
		}
		return (double) $ret;
	}
	
	public function formatRate($d){
		return number_format((double) $d,4,".",",");
	}
	public function formatPerc($d){
		return number_format((((double) $d)*100),0,".",",")."%";
	}
	public function formatPerc_P($d){
		return (((double) $d)*100)."%";
	}
}

////////////////////////// BEGIN OPS //////////////////////////
$audit = new Audit(trim($_POST['t_name']));

//MAIN LOOP
echo "MAPPING TEST<br />";
if($audit->dbcon->silentExecute("update `{$audit->vinsrc}` set `{$audit->colname}`=''"))
	echo "Column '{$audit->colname}' cleared<br />";
if($audit->dbcon->silentExecute("update `{$audit->vinsrc}` set `{$audit->colname2}`=''"))
	echo "Column '{$audit->colname2}' cleared<br />";
if($audit->dbcon->silentExecute("alter table `{$audit->vinsrc}` add `{$audit->colname}` varchar(32) not null"))
	echo "Column '{$audit->colname}' created<br />";
if($audit->dbcon->silentExecute("alter table `{$audit->vinsrc}` add `{$audit->colname2}` varchar(32) not null"))
	echo "Column '{$audit->colname2}' created<br />";

$ML_res = $audit->dbcon->execute("select id, vin from `{$audit->vinsrc}` group by vin order by id");
if($ML_res[2]){
	echo "<br /><b>LOOP STARTED</b><br />";
	while($ML_row = $audit->dbcon->nextRow($ML_res[0])){
		echo "<br />Now at {$ML_row['id']}";
		$_POST['vin'] = $ML_row['vin'];
		$audit->eustructureddecode();
	}
}else{
	echo "<br /><b style='color:red'>LOOP FAILED</b><br />";
}
echo "<hr />ENDED";
?>