var bLoggedIn=0;
var ie=document.all
var ns6=document.getElementById&&!document.all

var varDvrChild=null;
var varVhiChild=null;
var GPSForm=null;
var actGrpObj=null;	
var actGrpObj1=null;	
var dropmenuobj=null;
var whichFocus=null;
function OnPageClick(iPageNo)
{

	document.Fleetform.PageNo.value=iPageNo;
	document.Fleetform.submit();
}
function OnMenu(action)
	{
		if(bLoggedIn!=1){
			alert("You have not logged in");
			return;
		}
		if(action=="Me_CreateTrip"){
			if(document.Fleetform.m_RegNo!=null){
				//document.Fleetform.FleetID.value="";
				document.Fleetform.m_RegNo.value="";
				document.Fleetform.TripID.value="";
			}
		}
		if(document.Fleetform.PerPage)document.Fleetform.PerPage.value="15";
		document.Fleetform.PageNo.value="1";
		document.Fleetform.ActionTaken.value=action;	
		document.Fleetform.submit();	
	}
function OnProfileSearch()
{
	document.Fleetform.Data2.value="";
	document.Fleetform.submit();
}
function OnFocus(obj)
{
	  	whichFocus=obj;  
}
function OnUserFocus(obj)
{
	  	whichFocus=obj;  
}
function OnPassFocus(obj)
{
	  	whichFocus=obj;  
}
	function OnProfileFocus(obj)
	{
	  	whichFocus=obj;  
	}	
function alertkey(e)
{
		if( !e ) {
			//if the browser did not pass the event information to the
			//function, we will have to obtain it from the event register
			if( window.event ) {
			  //Internet Explorer
			  	e = window.event;
			}else {
			  //total failure, we have no way of referencing the event
			  	return;
			}
	  	}
	
		if(e.keyCode==27){
			if(ActMap!=null){
				ActMap.style.display="none";	
			}
			if(map!=null){
				delete map;
				map=null;
			}
			hideled();
			return false;			
		}
		if (event.keyCode == 13){
			if(whichFocus!=null){
				if(whichFocus.name=="Profile"){
					document.Fleetform.Username.focus();
					document.Fleetform.Username.select();
					return false;						
				}				
				if(whichFocus.name=="Username"){
					document.Fleetform.Password.focus();
					document.Fleetform.Password.select();
					return false;						
				}
				if(whichFocus.name=="Password"){			
					return true;
				}
			}
		}
		
		if(e.keyCode==131111){
			if(whichFocus!=null){
				//UpdateCtField(whichFocus.name);
			}
			//return false;
		}

}

function TL()
{
		if(bLoggedIn){
			bLoggedIn=false;
			document.Fleetform.Username.value="";
			document.Fleetform.Password.value="";
		}
		document.Fleetform.ActionTaken.value="Login";
		document.Fleetform.submit();
}
	function OnMenuPM(str)
	{
		document.Fleetform.ActionTaken.value="Me_ControlPanel";
		document.Fleetform.ControlPanel.value=str;
		document.Fleetform.submit();	
	}	


	function OnMenuX(action)
	{
		alert("Yet to release");
	}	
	function Logout()
	{
		if(bLoggedIn){
			bLoggedIn=false;
			document.Fleetform.Username.value="";
			document.Fleetform.Password.value="";
			document.Fleetform.ActionTaken.value="";
			document.Fleetform.ControlPanel.value="";			
		}
		delete_cookie ( "user" );

		//document.cookie = "user=;";
		document.Fleetform.submit();
	}
function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}
	
function OnLoad1()
{
//	if(document.Fleetform.ListLdd!=null){
		FillAdd();
//	}
}

	function OnLocation(i,lat,lng){
		var point = new GLatLng(lat/600000,lng/600000);
        geocoder.getLocations(	point, function(response){
			if (!response || response.Status.code != 200) {
				//document.getElementById("Loc_"+i).innerHTML=response.Status.code;
				document.getElementById("Loc_"+i).innerHTML="Unknown";
			}else{
				place = response.Placemark[0];
				document.getElementById("Loc_"+i).innerHTML=place.address;
				//alert(		place.address);
				//point = new GLatLng(place.Point.coordinates[1],
				//place.Point.coordinates[0]);
			}
	  	});
    }
function FillAdd()
{
	var i=0;
	while(1){
		if(document.getElementById("Loc_"+i)){
			OnLocation(i,document.getElementById("Lat_"+i).value,document.getElementById("Lng_"+i).value);
		}else{
			break;
		}
		i++;
	}
}
function ClkClient(id)
{
	hidetip(this);
	var bOpenNew=false;
	if(varDvrChild == null){
		bOpenNew=true;
	}else{
		if(varDvrChild.closed){
			bOpenNew=true;
		}else{
			document.Clientform.Profile.value=document.Fleetform.Profile.value;
			document.Clientform.Username.value=document.Fleetform.Username.value;
			document.Clientform.id_dvr.value=id;
//			varDvrChild.ParentRequest();		
			document.Clientform.submit();
			//varDvrChild.close();
			varDvrChild.focus();
			//varDvrChild=null;
		}
	}
	if(bOpenNew){
		document.Clientform.Profile.value=document.Fleetform.Profile.value;
		document.Clientform.Username.value=document.Fleetform.Username.value;			
		document.Clientform.id_dvr.value=id;		
		varDvrChild = window.open('','ClientformView','resizable=no,left=50,top=100,screenX=0,screenY=100,scrollbars=yes,height=375,width=800');
		var a = window.setTimeout("document.Clientform.submit();",1000); 								
	}
}
	function ClkGlb()
	{
		var bOpenNew=false;
		if(GPSForm == null){
			bOpenNew=true;
		}else{
			if(GPSForm.closed){
				bOpenNew=true;
			}else{
				GPSForm.focus();
			}
		}
		if(bOpenNew){
			document.GPSForm.Profile.value=document.Fleetform.Profile.value;
			document.GPSForm.Username.value=document.Fleetform.Username.value;			
			document.GPSForm.Password.value=document.Fleetform.Password.value;							
			document.GPSForm.ActionTaken.value="Login";
			
			GPSForm = window.open('','GPSFormView');
			var a = window.setTimeout("document.GPSForm.submit();",1000); 								
		}
	}
	
function ClkGlbPt(unitid)
	{
		var bOpenNew=false;
		if(GPSForm == null){
			bOpenNew=true;
		}else{
			if(GPSForm.closed){
				bOpenNew=true;
			}else{
//				document.GPSForm.UnitFoc.value=unitid;
				GPSForm.ParentRequest(unitid);
//				document.GPSForm.submit();
				GPSForm.focus();
			}
		}
		if(bOpenNew){
			document.GPSForm.Profile.value=document.Fleetform.Profile.value;
			document.GPSForm.Username.value=document.Fleetform.Username.value;			
			document.GPSForm.Password.value=document.Fleetform.Password.value;							
			document.GPSForm.ActionTaken.value="Login";
//			alert(document.Fleetform.Password.value);
			document.GPSForm.QryData.value=unitid+"^";
			document.GPSForm.UnitFoc.value=unitid;
			GPSForm = window.open('','GPSFormView');
			var a = window.setTimeout("document.GPSForm.submit();",1000); 								
		}
	}

function GetPureRegNo(reg)
{
	var iLen=reg.length;
	var newStr="";
	var iMode=4;
	reg=reg.toLowerCase();
	for(i=0;i<iLen;i++){
		if( (reg.charAt(i)>='0') && (reg.charAt(i)<='9') ){
			if((iMode!=0)&&(i!=0))newStr+=' ';		
			newStr+=reg.charAt(i);
			iMode=0;
		}
		if( (reg.charAt(i)>='a') && (reg.charAt(i)<='z') ){
			if((iMode!=1)&&(i!=0))newStr+=' ';					
			newStr+=reg.charAt(i);
			iMode=1;
		}		
	}
	 newStr=newStr.toUpperCase();
	return newStr;
}

function UpdatePrField(TblName,sFldName)
{
	var val;
	val=document.getElementById(sFldName).value;
	if(sFldName=="m_RegNo"){
		val=GetPureRegNo(document.getElementById(sFldName).value);
		document.getElementById(sFldName).value=val;
	}
  	qstr="";
  	qstr = qstr + 'Profile=' + document.getElementById("Profile").value;	
   	qstr = qstr + '&FieldName=' + sFldName;      	
  	qstr = qstr + '&FieldValue=' + escape(val);
   	qstr = qstr + '&TblName=' + TblName;
    qstr = qstr + '&m_id=' + document.getElementById("id_dvr").value;
   	qstr = qstr + '&LastReqAction=update'; 
	xmlhttpPost("../inc/update.asp",qstr,"ActBox",sFldName)
}
function OnValueChange(tbl,obj,sSec,sField)
{
	whichFocus=obj;
	if(sSec!="1"){
		alert("Sorry. You have no permission to do this");
		return;
	}
	document.getElementById("Sv_" + sField).innerHTML ="<img src='../Images/SaveIcon.gif' width='16' height='16' onClick='UpdatePrField(\""+tbl+"\",\"" + sField + "\");'>";
	document.getElementById("Sv_" + sField).style.visibility='visible';
}	
function xmlhttpPost(strURL,sFrmData,sTarget,sTargetImg) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	document.getElementById(sTarget).innerHTML="<p><br><br><br>Please wait...<br><br><br></p>";
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			document.getElementById(sTarget).innerHTML = self.xmlHttpReq.responseText;
			if(self.xmlHttpReq.responseText.indexOf("Done")>0){
				document.getElementById("Sv_" +sTargetImg).style.visibility='hidden';
			}else{
				//alert("Sorry. Unable to save");
			}
		}
	}
	self.xmlHttpReq.send(sFrmData);
}	
function onkey(tbl,e)
{
		if( !e ) {
			//if the browser did not pass the event information to the
			//function, we will have to obtain it from the event register
			if( window.event ) {
			  //Internet Explorer
			  	e = window.event;
			}else {
			  //total failure, we have no way of referencing the event
			  	return;
			}
	  	}
	
		if(e.keyCode==27){
			blur();
			window.parent.document.focus();			
		}
		if(e.keyCode==13){
			if(whichFocus != null){	// && (whichFocus.name=="LogNotes")){
				UpdatePrField(tbl,whichFocus.name);
				return false;
			}
		}
}
function OnSelVehiX(obj,name,unitid)
{
	actGrpObj.value=name;

	//document.Fleetform("RegNo_"+unitid).value=name;
	//document.getElementById("Ct_m_RegNo").innerHTML ="<img src='Images/SaveIcon.gif' width='16' height='16' onClick='UpdateCtField(\"" + "Vehicle" + "\");'>";	
	hideled();
	//document.Fleetform.submit();
}
function hideled(){
	if( (dropmenuobj!=null) && (dropmenuobj!='undefined')){
		dropmenuobj.style.visibility="hidden"
	}
}

function OnClickSave(unitid)
{
	fldval=GetPureRegNo(document.getElementById('RegNo_'+unitid).value);
	document.getElementById('RegNo_'+unitid).value=fldval;
  	qstr="";
   	qstr = qstr + 'FieldName=m_RegNo';
  	qstr = qstr + '&FieldValue=' + escape(fldval);
   	qstr = qstr + '&TblName=tbl_profile_device';
   	qstr = qstr + '&UnitID='+unitid;
   	qstr = qstr + '&LastReqAction=SaveDevice'; 
	xmlhttpPost("inc/update.asp",qstr,"ActBox");
	OnClickEdit('black',true,unitid);
}
function ClkVehicle(regno)
{
	hidetip(this);
	var bOpenNew=false;
	if(varDvrChild == null){
		bOpenNew=true;
	}else{
		if(varDvrChild.closed){
			bOpenNew=true;
		}else{
			document.Vehicleform.Profile.value=document.Fleetform.Profile.value;
			document.Vehicleform.Username.value=document.Fleetform.Username.value;
			document.Vehicleform.regno.value=regno;			
//			varDvrChild.ParentRequest();		
			document.Vehicleform.submit();
			//varDvrChild.close();
			varDvrChild.focus();
			//varDvrChild=null;
		}
	}
	if(bOpenNew){
		document.Vehicleform.Profile.value=document.Fleetform.Profile.value;
		document.Vehicleform.Username.value=document.Fleetform.Username.value;			
		document.Vehicleform.regno.value=regno;					
		varDvrChild = window.open('','VehicleformView','resizable=no,left=50,top=100,screenX=0,screenY=100,scrollbars=yes,height=375,width=800');
		var a = window.setTimeout("document.Vehicleform.submit();",1000); 								
	}
}

function ClkDvr(id)
{
	hidetip(this);
	var bOpenNew=false;
	if(varDvrChild == null){
		bOpenNew=true;
	}else{
		if(varDvrChild.closed){
			bOpenNew=true;
		}else{
			document.Driverform.Profile.value=document.Fleetform.Profile.value;
			document.Driverform.Username.value=document.Fleetform.Username.value;
			document.Driverform.id_dvr.value=id;
//			varDvrChild.ParentRequest();		
			document.Driverform.submit();
			//varDvrChild.close();
			varDvrChild.focus();
			//varDvrChild=null;
		}
	}
	if(bOpenNew){
		document.Driverform.Profile.value=document.Fleetform.Profile.value;
		document.Driverform.Username.value=document.Fleetform.Username.value;			
		document.Driverform.id_dvr.value=id;		
		varDvrChild = window.open('','DriverformView','resizable=no,left=50,top=100,screenX=0,screenY=100,scrollbars=yes,height=500,width=800');
		var a = window.setTimeout("document.Driverform.submit();",1000); 								
	}
}
function OnClickHistory(sUnitID,regno)
	{
		var bOpenNew=false;
		if(SIMchildHistory == null){
			bOpenNew=true;
		}else{
			if(SIMchildHistory.closed){
				bOpenNew=true;
			}else{		
				//mychild.close();
				SIMchildList.focus();
				//mychild=null;
			}
		}
		if(bOpenNew){
			document.UnitHistoryForm.Profile.value = document.Fleetform.Profile.value;		
			document.UnitHistoryForm.Username.value = document.Fleetform.Username.value;
			document.UnitHistoryForm.Password.value = document.Fleetform.Password.value;
			document.UnitHistoryForm.RegNo.value = regno;			
			document.UnitHistoryForm.UnitID.value = sUnitID;
			SIMchildHistory = window.open('','MyUnitWinHistory','resizable=yes,left=0,top=100,screenX=0,screenY=100,scrollbars=yes,height=400,width=800');	
			var a = window.setTimeout("document.UnitHistoryForm.submit();",1000); 								
		}
	}
	function OnClickEdit(col,edit,unitID)
	{
		var sTemp;
		document.Fleetform.UnitIDEntered.value = unitID;
		document.getElementById("RegNo_" + unitID).readOnly=edit;
		document.getElementById("RegNo_" + unitID).style.color=col;
	}
function OnValueChangeX(obj,sSec,unitid)
{
	whichFocus=obj;
	if(sSec!="1"){
		alert("Sorry. You have no permission to do this");
		return;
	}
	document.getElementById("Sv_" + unitid).innerHTML ="<img src='Images/SaveIcon.gif' width='16' height='16' onClick='UpdateDvField(\"" + unitid + "\");'>";
	document.getElementById("Sv_" +unitid).style.visibility='visible';
}	
function UpdateDvField(unitid)
{
	//tbl_profile_device
	var xmlHttpReq = false;
	var self = this;
	
  	qstr="";
  	qstr = qstr + 'Profile=' + document.getElementById("Profile").value;	
   	qstr = qstr + '&FieldName=m_RegNo';
  	qstr = qstr + '&FieldValue=' + escape(document.getElementById("Reg_"+unitid).value);
  	qstr = qstr + '&unitid=' + escape(unitid);	
   	qstr = qstr + '&LastReqAction=updateDivMgr'; 

	// Mozilla/Safari
	document.getElementById("DvMgr").innerHTML="<p><br><br><br>Please wait...<br><br><br></p>";
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', "inc/update.asp", true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			document.getElementById("DvMgr").innerHTML = self.xmlHttpReq.responseText;
			document.getElementById("Sv_" +unitid).style.visibility='hidden';
			if(self.xmlHttpReq.responseText.indexOf("Done")==-1){
				//document.getElementById("Reg_"+unitid).value="";
			}					
		}else{
			//document.getElementById("Reg_"+unitid).value="";
		}

	}
	self.xmlHttpReq.send(qstr);
}	
function ClkX(unitid)
{
	OnValueChangeX("",'1',unitid);
	document.getElementById("Reg_" +unitid).value="";
}	

