/////////////////////////////////////////////////////////////////////
//
var		g_oUser = null;
var		g_oVechile = null;
var		g_oGPSData = null;
var		g_oTrackObj	= null;


if( g_oUser == null ){
	g_oUser = new oUser();
}
if( g_oVechile == null ){
	g_oVechile = new oVechileArray();
} 
if( g_oGPSData == null ){
	g_oGPSData = new oGPSDataArray();
} 
if( g_oTrackObj == null ){
	g_oTrackObj = new oTrackObj;	
}
/////////////////////////////////////////////////////////////////////
function mapPoint(lng,lat){
	this.lng = lng;
	this.lat = lat;	
}

/////////////////////////////////////////////////////////////////////
function historyData(){
	this.condition=0;
	this.data;	
}
/////////////////////////////////////////////////////////////////////
//car mileage obj
function oMileage(){
    this.TEUID="";
    this.MILEAGE="";
}
    
/////////////////////////////////////////////////////////////////////
//
function oAckResult(){
	this.cmdType;
	this.cmdResult;	
}
/////////////////////////////////////////////////////////////////////
//user obj
function oUser(){
	this.isLogin = false;
	
	this.FLName = "";
	this.Username="";
	this.Password="";
	this.UserID="";
	this.Privilege=0;
	
	this.TelNo="";
	this.Email="";
	this.Coname="";
	this.Address="";
	this.Remark="";
	
	this.SubUser = new Array();
	// only license
	this.VechileList = new Array();
	// 
	this.validDate;
}

_p = oUser.prototype;

_p.test = function( ){
	var i,j = 0;
	this.FLName = "Supuser";
	this.Privilege=2;
	
	for(  i = 0; i <3; i++ ){
		this.SubUser[i] = new oUser();
		this.SubUser[i].FLName = "user"+i;
		
		for( j = 0; j < 8; j++ ){
			this.SubUser[i].VechileList[j] = "vechile"+j;
		}		
	}
		
	for( j = 0; j < 18; j++ ){
		this.VechileList[j] = "vechile"+j;
	}	
}

_p.putVechileToUser = function( user, vechile ){

	if( user == this.Username ){
		this.VechileList = vechile.split(",,");	
	}	
	else{
		for( var i = 0; i < this.SubUser.length; i++ ){
			if( user == this.SubUser[i].Username ){
				this.SubUser[i].VechileList = vechile.split(",,");
				break;	
			}				
		}	
	}
	return;
}

_p.addVechileToUser = function( user, vechile ){

	if( vechile.length < 0 ){
		return;	
	}
	
	if( user == this.Username ){
		this.VechileList.push( vechile );
	}	
	else{
		for( var i = 0; i < this.SubUser.length; i++ ){
			if( user == this.SubUser[i].Username ){
				this.SubUser[i].VechileList.push( vechile );
				break;	
			}				
		}	
	}

}
// remove vechile from user
// if user equal supper user, then remove this car from all user
_p.delVechileToUser = function( user, vechile ){
	if( vechile.length < 1 ){
		return;	
	}

	if( user == this.Username ){		
		this.removestrFromArray( this.VechileList, vechile );
		for( var i = 0; i < this.SubUser.length; i++ ){		
			this.removestrFromArray( this.SubUser[i].VechileList, vechile );
		}	
	}	
	else{
		for( var i = 0; i < this.SubUser.length; i++ ){
			if( user == this.SubUser[i].Username ){				
				this.removestrFromArray( this.SubUser[i].VechileList, vechile );
				break;	
			}				
		}	
	}
	
}

_p.removestrFromArray = function( arr, str ){
	
	for( var i = 0; i < arr.length; i++ ){
		if( arr[i] == str ){
			arr.splice( i, 1 );
			break;	
		}			
	}	
}

_p.IsUnGroupVechile = function ( obj ){
	
	for( var i = 0; i < this.SubUser.length; i++ ){
		
		var		vechileList = this.SubUser[i].VechileList;
		
		for( var j = 0; j < vechileList.length; j++ ){
			
			if( vechileList[j] == obj ){
				return true;	
			}			
		}		
	}
	return false;
	
}

// got the vechile that without parenet user
_p.getUnGroupVechile = function(){
	
	var			vechileArray = new Array();
	var			j = 0;
	
	for( var i = 0; i < this.VechileList.length; i++ ){
		
		if( this.IsUnGroupVechile( this.VechileList[i] ) == false ){
			vechileArray[j++] = this.VechileList[i];
		}
	}
	return vechileArray;
}

_p.IsLogin = function(){
	return this.isLogin;	
}

//////////////////////////////////////////////////////////////////////
//car obj
function oVechile(){
	
	this.License="";
	this.TEUID ="";
	this.TEType = 0;
	this.TENo;
	this.OwnerName;
	this.OwnerTel;
	this.OwnerAddr;
	this.Remark;
	this.bDisplay = false;	
}
_p = oVechile.prototype;

_p.setDisplay = function( flag ){
	this.bDisplay = flag;	
}
_p.isDisplay = function( ){
	return this.bDisplay;	
}

//////////////////////////////////////////////////////////////////////
function oVechileArray(){	
	this.objArray = new Array();	
}

_p = oVechileArray.prototype;

_p.getLen = function(  ){	
	return this.objArray.length;	
}
_p.addVechileInfo = function( License, TEUID, TEType,  bDisplay ){	
	
	var			obj = new oVechile();
	obj.TEUID = TEUID;
	obj.TEType = TEType;
	obj.License = License;
	obj.bDisplay = bDisplay;
	this.addVechileObj( obj );
}
_p.addVechileObj = function( obj ){	
	
	var			obj2;
	var			i = 0;
	
	//alert( "addVechileObjstart len" + this.getLen() );
	for( i = 0; i < this.getLen(); i++ ){
		obj2 = this.getVechile( i );		
		if( obj2.License == obj.License ){
			break;	
		}		
	}
	//alert( "addVechileObj" + i );
	this.objArray[i] = obj;	
	return this.objArray.length;		
}
_p.UpdateVechileObj = function ( obj ){
	for(var i = 0; i < this.getLen(); i++ ){
		if ( this.objArray[i].TEUID == obj.TEUID){
			if (obj.TEUID != null){
				this.objArray[i].TEUID = obj.TEUID;
			}
			if (obj.TEType != null){
				this.objArray[i].TEType = obj.TEType;
			}
			if (obj.License != null){
				this.objArray[i].License = obj.License;
			}
			if (obj.TENo != null){
				this.objArray[i].TENo = obj.TENo;
			}
			if (obj.OwnerName != null){
				this.objArray[i].OwnerName = obj.OwnerName;
			}
			if (obj.OwnerTel != null){
				this.objArray[i].OwnerTel = obj.OwnerTel;
			}
			if (obj.OwnerAddr != null){
				this.objArray[i].OwnerAddr = obj.OwnerAddr;
			}
			if (obj.Remark != null){
				this.objArray[i].Remark = obj.Remark;
			}
			break;
		}
	}
}
_p.delVechileObj = function ( CarTEUID ){
	for(var i = 0; i < this.getLen(); i++ ){
		if ( this.objArray[i].TEUID == CarTEUID){
			this.objArray.splice( i,1 );
			break;
		}
	}
}

_p.getVechile=function( index ){
	
	var			oVechile = null;
	if( typeof( index ) == "string" ){
		for( var i =0; i< this.getLen(); i++ ){
			if( this.objArray[i].License == index ){
				oVechile = 	this.objArray[i];
				break;
			}				
		}		
	} 
	else if( typeof(index) == "number" ){	
		if( index < this.getLen() ){
			oVechile = this.objArray[index];
		}	
	}
	return oVechile;
}

_p.getVechileByUID=function( index ){
	
	var			oVechile = null;
	for( var i =0; i< this.getLen(); i++ ){
		if( this.objArray[i].TEUID == index ){
			oVechile = 	this.objArray[i];
			break;
		}				
	}	
	return oVechile;
}
_p.getVechileLicenseByUID=function( index ){
	
	var			oVechile = null;
	for( var i =0; i< this.getLen(); i++ ){
		if( this.objArray[i].TEUID == index ){
			oVechile = 	this.objArray[i];
			break;
		}				
	}	
	if( oVechile != null ){
		return oVechile.License;
	}
	else{
		return null;
	}
}

_p.getVechileTEUIDByLicense=function( inputLicense ){
	
	var			oVechile = null;
	for( var i =0; i< this.getLen(); i++ ){
		
		if( this.objArray[i].License == inputLicense ){
			oVechile = 	this.objArray[i];
			break;
		}				
	}	
	if( oVechile != null ){
		return oVechile.TEUID;
	}
	else{
		//alert( inputLicense + " no TEUID!");
		return null;
	}
}

_p.getTENOByUID=function( index ){
	
	var			oVechile = null;
	for( var i =0; i< this.getLen(); i++ ){
		if( this.objArray[i].TEUID == index ){
			oVechile = 	this.objArray[i];
			break;
		}				
	}	
	if( oVechile != null ){
		return oVechile.TENo;
	}
	else{
		return null;
	}
}
_p.getTETypeByUID=function( index ){
	
	var			oVechile = null;
	for( var i =0; i< this.getLen(); i++ ){
		if( this.objArray[i].TEUID == index ){
			oVechile = 	this.objArray[i];
			break;
		}				
	}	
	if( oVechile != null ){
		//alert( oVechile.License );
		return oVechile.TEType;
	}
	else{
		return null;
	}
}
_p.setVechileDisplayEnalbe = function( TEUID, flag ){
	
	for( var i = 0; i < this.getLen(); i++ ){
		if( this.getVechile(i).TEUID == TEUID ){
			this.getVechile(i).bDisplay = flag;			
			break;	
		}		
	}	
}
_p.IsVechileDisplayEnalbe = function( TEUID ){
	
	var			flag = false;
	for( var i = 0; i < this.getLen(); i++ ){
		if( this.getVechile(i).TEUID == TEUID ){
			flag = this.getVechile(i).bDisplay;			
			break;	
		}		
	}	
	return flag;
}
///////////////////////////////////////////////////////////////////////
// GPSÊý¾Ý

function oGPSData(){
	
	this.hasUpdate = true;
	this.first = true;
	
	this.TEUID;
	this.VALID;
	this.TIME;
	this.LON;
	this.LAT;
	this.SPEED;
	this.DIRECTION;
	this.MILEAGE;
	this.A_URGENCY;
	this.A_CUSTOM1;
	this.A_CUSTOM2;
	this.A_CUSTOM3;
	this.A_CUSTOM4;
	this.A_POWEROFF;
	this.A_LOWPOWER;
	this.A_PARKOVERTIME;
	this.A_IN_AREA;
	this.A_OUT_AREA;
	this.A_IL_ACCON;
	this.A_IL_DOOROPEN;
	this.A_OVERSPEED;
	
}
function oGPSDataArray(){
	this.objArray = new Array();
}
_p = oGPSDataArray.prototype;

_p.getLen = function (){
	return this.objArray.length;	
}
_p.addGPSObj = function( obj ){	
	
	var			i = 0;	
	obj.hasUpdate = true;
	
	for( i = 0; i < this.getLen(); i++ ){
		if( this.getGPS( i ).TEUID == obj.TEUID ){
			obj.first = false;
			break;	
		}				
	}
	this.objArray[i] = obj;		
	return this.objArray.length;	
}
_p.addGPSInfo = function( 	
	TEUID,
	VALID,
	TIME,
	LON,
	LAT,
	SPEED,
	DIRECTION,
	MILEAGE,
	A_URGENCY,
	A_CUSTOM1,
	A_CUSTOM2,
	A_CUSTOM3,
	A_CUSTOM4,
	A_POWEROFF,
	A_LOWPOWER,
	A_PARKOVERTIME,
	A_IN_AREA,
	A_OUT_AREA,
	A_IL_ACCON,
	A_IL_DOOROPEN,
	A_OVERSPEED ){
			
	obj = new oGPSData();
	
	obj.TEUID=TEUID;
	obj.VALID=VALID;
	obj.TIME=TIME;
	obj.LON=LON;
	obj.LAT=LAT;
	obj.SPEED=SPEED;
	obj.DIRECTION=DIRECTION;
	obj.MILEAGE=MILEAGE;
	obj.A_URGENCY=A_URGENCY;
	obj.A_CUSTOM1=A_CUSTOM1;
	obj.A_CUSTOM2=A_CUSTOM2;
	obj.A_CUSTOM3=A_CUSTOM3;
	obj.A_CUSTOM4=A_CUSTOM4;
	obj.A_POWEROFF=A_POWEROFF;
	obj.A_LOWPOWER=A_LOWPOWER;
	obj.A_PARKOVERTIME=A_PARKOVERTIME;
	obj.A_IN_AREA=A_IN_AREA;
	obj.A_OUT_AREA=A_OUT_AREA;
	obj.A_IL_ACCON=A_IL_ACCON;
	obj.A_IL_DOOROPEN=A_IL_DOOROPEN;
	obj.A_OVERSPEED=A_OVERSPEED;
	
	_p.addGPSObj( obj );
}
_p.getGPS = function ( index ){
	
	var			oGPS = null;
	if( typeof( index ) == "string" ){
		for( var i =0; i< this.getLen(); i++ ){
			if( this.objArray[i].TEUID == index ){
				oGPS = 	this.objArray[i];
				break;
			}				
		}		
	} 
	else if( typeof(index) == "number" ){	
		if( index < this.getLen() ){
			oGPS = this.objArray[index];			
		}	
	}
	return oGPS;
}

_p.setUpdateFlag = function ( index, flag ){
	if( index < this.getLen() ){
		this.objArray[index].hasUpdate = flag;	
	}
}

///////////////////////////////////////////////////////////
// track obj
function oTrackObj( ){

	this.objArray = new Array();	
	this.keyptArray = new Array();
	this.keyptName = new Array();
	this.state = 0;		// 0:stop 1:play 2:pause		
	this.step = 0;
	this.valid = false; 	// 0/1 That is TRACK (can screen out other operations)
}

_p = oTrackObj.prototype;

_p.isTrackValid = function( ){
	return this.valid;	
}

// NG
// Start enter gps data
_p.start = function ( objArray, flag ){
	
	this.state = 0;
	this.step = 0;
	this.valid = true;
	this.objArray = objArray;
	
	var		len = objArray.length;
	
	if( len == 0 ){
		return 0;	
	}
	this.linename = getLicenseByTEUID(objArray[0].TEUID)+"_line";
	this.pointname = getLicenseByTEUID(objArray[0].TEUID)+" ";
	
	var		nSpace = 1;
	
	
	if( len > 10 ){		
		if( len % 10 == 0 ){
			nSpace =  Math.round(len/10);			
		}
		else{
			nSpace =  Math.round(len/9);				
		}
	}
	
	var			points = new Array();
	var			lineobj;
	var			j = 0;
	var			nMileage = 0;
	
	for( var i = 0; i < len; i++ ){
		if( i == 0 ){
			nMileage = 0;
		}
		else{
			nMileage += parseInt( this.objArray[i].MILEAGE );
		}
		if( ( i % nSpace )  == 0 || ( i == len - 1 ) ){
			this.keyptArray[j] = new oGPSData();
			for( var Member in this.objArray[i] ){
				this.keyptArray[j][Member] = this.objArray[i][Member];	
			}
			//this.keyptArray[j] = this.objArray[i];
			this.keyptArray[j].MILEAGE = nMileage;
			j++;			
		}		
		points.push( new mapPoint( this.objArray[i].LON, this.objArray[i].LAT ) );		
	}
	mapDrawLine( {name:this.linename, points:points});	
	mapBestView( points );
	
	
	if( flag == true ){
		var			name;
		var			vurl;
		
		len = this.keyptArray.length;
		
		for( i = 0; i < len; i++ ){
			if( i == 0 ){
				
				name = "start:";	
			}
			else if( i == len-1 ){
				name = "end:";	
			}
			else{
				name = i+"#:";
			}
			name += "\r\n" + this.keyptArray[i].MILEAGE/1000 +"km";
			
			if( i == 0 ){
				vurl = mapGetStartUrl(this.keyptArray[i]);
			}
			else if( i == len-1 ){
				vurl = mapGetEndUrl(this.keyptArray[i]);	
			}
			else{
				vurl = mapGetParkUrl(this.keyptArray[i]);
			}
			mapAddMarker( name, vurl, this.keyptArray[i].LON, this.keyptArray[i].LAT );	
			this.keyptName[i] = name;	
		}
	}
}

_p.play = function( ){
	this.state = 1;
}
_p.pause = function( ){
	this.state = 2;
}
_p.next = function( ){
	if( this.state == 1 ){
		this.step += 1;	
		if( this.step < this.objArray.length ){		
			//alert( this.step+"/"+this.objArray.length + this.objArray[this.step].LON +this.objArray[this.step].LAT );
			var			name;
			
			name 	= this.pointname 
					+ "\n "
					+ "(" + ( this.step + 1 ) + "/" + this.objArray.length + ")"
					+ "\n " 
					+ UTCtoLocal(this.objArray[this.step].TIME).toLocaleString()
					+ "\n "
					+ this.objArray[this.step].SPEED+"km/h";
			
			mapAddMarker2( 	name, 
							this.objArray[this.step].LON, 
							this.objArray[this.step].LAT, 
							this.objArray[this.step].SPEED, 
							this.objArray[this.step].DIRECTION, 
							this.objArray[this.step].A_URGENCY||this.objArray[this.step].A_CUSTOM1||this.objArray[this.step].A_CUSTOM2||this.objArray[this.step].A_CUSTOM3||this.objArray[this.step].A_CUSTOM4||this.objArray[this.step].A_POWEROFF||this.objArray[this.step].A_LOWPOWER||this.objArray[this.step].A_PARKOVERTIME||this.objArray[this.step].A_IN_AREA||this.objArray[this.step].A_OUT_AREA||this.objArray[this.step].A_IL_ACCON||this.objArray[this.step].A_IL_DOOROPEN||this.objArray[this.step].A_OVERSPEED );
		
			mapCenter( this.objArray[this.step].LON, this.objArray[this.step].LAT );
			return 1;
		}
		else{
			return 0;
		}
	}
	return 0;
}
_p.stop = function( ){
	this.state = 0;
	this.step  = 0;	
	mapDeleteMarker( this.pointname);
}
_p.end = function( ){
	this.state = 0;	
	this.valid = false;	
	
	for( var i = 0; i < this.keyptName.length; i++ ){
		mapDeleteMarker( this.keyptName[i] );			
	}
	this.objArray.splice(0,this.objArray.length );
	this.keyptArray.splice(0,this.keyptArray.length);
	this.keyptName.splice(0,this.keyptName.length);
	mapDeleteMarker( this.linename );
	mapDeleteMarker( this.pointname);
}

//alert( "obj.js end" );

