//Javascript Document
var Global_Url_Param = "" ;
var CarIconsLoaded = new Array() ;
var GPS_MapAction = 0;
var GPS_CounterAjaxProcess = 0;

function ActivityRecorder(user, id)
{
  var d=new Date();
  var day = d.getDate();
  var mon = d.getMonth() + 1;
  var year= d.getFullYear();
  var hrs = d.getHours();
  var min = d.getMinutes();
  var sec = d.getSeconds();  
  day = (day<10) ? "0"+day : day;
  mon = (mon<10) ? "0"+mon : mon;  
  hrs = (hrs<10) ? "0"+hrs : hrs;
  min = (min<10) ? "0"+min : min;
  sec = (sec<10) ? "0"+sec : sec;
  var datetime =  year + "-" + mon + "-" + day + " " + hrs + ":" + min + ":" + sec;
  var UrlAdd = "D01=" + user + "&D02=" + id + "&D03=" + datetime;
  var Url = "WebServices/set_activity_recorder.cgi?" + UrlAdd + "&Ref=" + encodeURI(Math.round(Math.random(0,1)*100000000000)) ; 
  Global_Request('SetActivityRecorder',Url+SetGlobalUrlParam());
}

function GPS_GeneralLoadingStart()
{
	var MyID = "GPS_LoadingMsg_General";
	if (!document.getElementById(MyID))
	{
		var D = document.createElement('div') ;
		D.id = MyID ;
		D.name = MyID ;
		D.style.position = 'absolute';
		D.style.zIndex = 100000 ;
		D.style.backgroundColor = '#CCCCCC' ;
		D.style.color = 'blue' ;
		D.style.border = '1px solid black' ;
		D.style.padding = '5px' ;
		D.style.fontSize = '12' ;
		D.style.fontWeight = 'bold' ;
		document.body.appendChild(D);
	}
	var B = document.getElementById(MyID) ;
	B.innerHTML = "&nbsp; Loading.... &nbsp;" ;
	B.style.visibility = 'visible' ;
	B.style.display = '' ;
	var W = B.offsetWidth ;
	var H = B.offsetHeight ;
	var WW = (window.innerWidth)?window.innerWidth:document.body.offsetWidth
	var WH = (window.innerHeight)?window.innerHeight:document.body.offsetHeight
	B.style.left = Math.round(150);
	B.style.top  = Math.round(5);
	B.style.zIndex = 100000 ;
}

function GPS_GeneralLoadingStop(ID)
{
	var MyID = "GPS_LoadingMsg_General" ;
	if (document.getElementById(MyID)) 
	{
		var B = document.getElementById(MyID) ;
		B.innerHTML = '' ;
		B.style.zIndex = 0 ;
		B.style.visibility = 'hidden' ;
		B.style.display = 'none' ;
	}
}

function GPS_LoadingStart(ID,DataText)
{
	var MyID = "GPS_LoadingMsg_"+ID ;
	if (!document.getElementById(MyID))
	{
		var D = document.createElement('div') ;
		D.id = MyID ;
		D.name = MyID ;
		D.style.position = 'absolute';
		D.style.zIndex = 100000 ;
		D.style.backgroundColor = 'white' ;
		D.style.color = 'blue' ;
		D.style.border = '1px solid black' ;
		D.style.padding = '5px' ;
		D.style.fontSize = '16' ;
		D.style.fontWeight = 'bold' ;
		document.body.appendChild(D);
	}
	var B = document.getElementById(MyID) ;
	B.innerHTML = "&nbsp; "+DataText+" &nbsp;" ;
	B.style.visibility = 'visible' ;
	B.style.display = '' ;
	var W = B.offsetWidth ;
	var H = B.offsetHeight ;
	var WW = (window.innerWidth)?window.innerWidth:document.body.offsetWidth
	var WH = (window.innerHeight)?window.innerHeight:document.body.offsetHeight
	B.style.left = Math.round((WW-W)/2);
	B.style.top  = Math.round((WH-H)/2);
	B.style.zIndex = 100000 ;
}

function GPS_LoadingStop(ID) 
{
	var MyID = "GPS_LoadingMsg_"+ID ;
	if (document.getElementById(MyID)) {
		var B = document.getElementById(MyID) ;
		B.innerHTML = '' ;
		B.style.zIndex = 0 ;
		B.style.visibility = 'hidden' ;
		B.style.display = 'none' ;
	}
}

function EncodeURL(Str)
{
	Str = encodeURI(Str);
	Str = Str.replace(/\&/g,"%26");
	return Str ;
}

function VisibleContrast(Color)
{
	Color = Color.replace("#","");
	var Red = parseInt(Color.substr(0,2),16);
	var Green = parseInt(Color.substr(2,2),16);
	var Blue = parseInt(Color.substr(4,2),16);
	var cHalfBrightness = ((0.3 * 255.0) + (0.59 * 255.0) + (0.11 * 255.0)) / 2.0 ;
	var BrightNess = (0.3 * Red) + (0.59 * Green) + (0.11 * Blue);
	if (BrightNess > cHalfBrightness) 
	{
		return "0D4508";
	} else {
		return "E0F8DE";
	}
}

function LoadCarsIcons()
{
	for (var i=0; i<=360; i++)
	{
		CarIconsLoaded[i] = new Image ;
		CarIconsLoaded[i].src = "icons/car_icon_"+i+".gif" ;
	}
}

function CheckCarsIcons()
{
	for (var i=0; i<=360; i++)
	{
		if (!CarIconsLoaded[i].complete)
		{
			CarIconsLoaded[i].src = "icons/car_icon_"+i+".gif" ;
		}
	}
}

function in_polygon(X,Y,Vertices)
{
    var length = Vertices.length ;
	var out = 1 ;
	j = length - 2;
	for (var i=0; i<length-1; i+=2)
	{
		if ( ((Vertices[i+1]<=Y) && (Y<=Vertices[j+1])) || ((Vertices[j+1]<=Y) && (Y<=Vertices[i+1])))
		{
			var T = ((Vertices[j+1]-Vertices[i+1]) != 0)?(Vertices[j]-Vertices[i])*(Y-Vertices[i+1])/(Vertices[j+1]-Vertices[i+1]):0.0000000000000000001 ;
			if (X <= T+Vertices[i])
			{
				out = 1 - out;
			}
		}
		j = i;
	}
 	return out;
}

function VisibleContrast2(Color)
{
	Color = Color.replace("#","");
	var Red = parseInt(Color.substr(0,2),16);
	var Green = parseInt(Color.substr(2,2),16);
	var Blue = parseInt(Color.substr(4,2),16);
	var BrightNess = (0.3 * Red) + (0.59 * Green) + (0.11 * Blue);
	if (BrightNess > 25)
	{
		return "000000";
	} else {
		return "FFFFFF";
	}
}

function NewHistory(ID)
{
	this.ID = ID ;
	this.DateTime = '' ;
	this.Longitude = '' ;
	this.Latitude = '' ;
	this.RealLongitude = '' ;
	this.RealLatitude = '' ;
	this.RoadName = '' ;
	this.Speed = '' ;
	this.Heading = '' ;
	this.Altitude = '' ;
	this.Satellite = '' ;
	this.Report = '' ;
	this.Input = '' ;
	this.Output = '' ;
	this.Days = '' ;
	this.Times = '' ;
	this.Input9 = '' ;
	this.Input10 = '' ;
}

function GPS_NewVehicle(ID)
{
	this.ID = ID ;
	this.History = new Array () ;
	this.NewUpdate = false ;
	this.DateUpdate = '' ;
	this.Longitude = '' ;
	this.Latitude = '' ;
	this.RoadName = '' ;
	this.ClientID = '' ;
	this.Name = '' ;
	this.Color = '' ;
	this.Factor1 = '' ;
	this.ZeroValue1 = '' ;
	this.Unit1 = '' ;
	this.Factor2 = '' ;
	this.ZeroValue2 = '' ;
	this.Unit2 = '' ;
	this.BusinessCompany = '' ;
	this.Driver = '' ;
	this.BusinessTradeName = '' ;
	this.Mask = 0 ; 
}

function NewOffSet()
{
	this.left = '' ;
	this.top = '' ;
}

function AngleMirror(Angle)
{
	return 360-Angle+90 ;
}

function GetDistance(A,B)
{
	var Distance = "-1" ;
	if (GlobalCountry == "nz") {
		Distance = GetNZDistance(A,B) ;
	} else if (GlobalCountry=="tas") {
		Distance = GetTasDistance(A,B) ;
	} else if (GlobalCountry=="") {
		Distance = GetAustraliaDistance(A,B) ;
	}
	return Distance ;
}

function GetAustraliaDistance(A,B)
{
	var Distance = 0 ;
	if (!(A.Longitude == B.Longitude && A.Latitude == B.Latitude))
	{
		Distance = (20001.6/Math.PI)*Math.acos(Math.sin(Math.PI*(A.Latitude)/180)*Math.sin(Math.PI*(B.Latitude)/180)+Math.cos(Math.PI*(A.Latitude)/180)*
		Math.cos(Math.PI*(B.Latitude)/180)*Math.cos(((B.Longitude)-(A.Longitude))*Math.PI/180)) ;
	}
	return Distance ;
}

function GetNZDistance(A,B)
{
	var Distance = 0 ;
	if (!(A.Longitude == B.Longitude && A.Latitude == B.Latitude))
	{
		Distance = Math.sqrt(Math.pow((A.Longitude-B.Longitude),2) + Math.pow((A.Latitude-B.Latitude),2)) * 0.001 ;
	}
	return Distance ;
}

function GetTasDistance(A,B)
{
	var Distance = 0 ;
	if (!(A.Longitude == B.Longitude && A.Latitude == B.Latitude))
	{
		Distance = Math.sqrt(Math.pow((A.Longitude-B.Longitude),2) + Math.pow((A.Latitude-B.Latitude),2)) * 0.001 ;
	}
	return Distance ;
}

function ShowOffSet(ID)
{
	var left = 0 ;
	var top = 0 ;
	var NewID = document.getElementById(ID);
	left = 0 ;
	top = 0 ;
	while (NewID.offsetParent)
	{
		top += parseInt(NewID.offsetTop) ;
		left += parseInt(NewID.offsetLeft) ;
		NewID = NewID.offsetParent ;
	}
	var NewObj = new NewOffSet();
	NewObj.left = left ; 
	NewObj.top = top ; 
	return NewObj ;
}

function TrimSpace(D)
{
	if (D)
	{
		var Regex ;
		Regex = /\s*$/ ;
		D = D.replace(Regex,"");
		Regex = /^\s*/ ;
		D = D.replace(Regex,"");
	}
	return D ;
}

function TitleCase(D)
{
	return D.replace(/\b(\w)/g,function(str) { return str.toUpperCase(); })
}

function GetDeviceInformation(e,Img,M)
{
	var BVI ;
	var nn6=document.getElementById&&!document.all;
	if (document.getElementById('BodyVehicleInformation'))
	{
		BVI = document.getElementById('BodyVehicleInformation') ;
	} else {
		BVI = document.createElement('div') ;
		BVI.id = 'BodyVehicleInformation' ; 
		BVI.name = BVI.id ; 
		BVI.style.position = 'absolute' ; 
		BVI.style.backgroundColor = '#CCD6FE' ;
		BVI.style.border = '1px solid #0F1E58' ;
		BVI.onmouseout = function() {
			this.innerHTML = "" ;
		}
		document.body.appendChild(BVI);
	}
	
	if (Img != '')
	{
		var V = Img.V ;
		var i = V.History.length - 1 ;
		var Information = "<table id=VehicleInformationTable cellpadding=3 cellspacing=0 border=0>" ;
		Information += "<tr bgcolor=\"#4B5C9E\"><td style=\"border-bottom:1px solid #0F1E58;\" colspan=2><b style=\" color: yellow; letter-spacing:1;\">&#187; Vehicle Information<b></td></tr>" ;
		if (V.Longitude && V.Latitude)
		{
			var DTV = 0 ;
			DTV = V.Mask & 2 ;
			if (DTV == 2)
			{
				Information += (V.ID)?"<tr><td class=smallerbold>Device ID</td><td class=smaller>: &nbsp; "+V.ID+"</td></tr>":"" ;
			}
			Information += (V.Name)?"<tr><td class=smallerbold>Vehicle Name</td><td class=smaller>: &nbsp; "+V.Name+"</td></tr>":"" ;
			DTV = V.Mask & 4 ;
			if (DTV == 4)
			{
				Information += (V.BusinessCompany)?"<tr><td class=smallerbold>Company Name</td><td class=smaller>: &nbsp; "+V.BusinessCompany+"</td></tr>":"" ;
			}
			DTV = V.Mask & 8 ;
			//if (DTV == 8)
			//{
				Information += (V.Driver)?"<tr><td class=smallerbold>Driver</td><td class=smaller>: &nbsp; "+V.Driver+
				"</td></tr>":"<tr><td class=smallerbold>Driver</td><td class=smaller>: &nbsp; None</td></tr>" ;
			//}
			DTV = V.Mask & 16 ;
			if (DTV == 16)
			{
				Information += (V.BusinessTradeName)?"<tr><td class=smallerbold>Trade Name</td><td class=smaller>: &nbsp; "+V.BusinessTradeName+"</td></tr>":"" ;
			}
			Information += (V.History[i].DateTime)?"<tr><td class=smallerbold>Date & Time</td><td class=smaller>: &nbsp; "+V.History[i].DateTime+"</td></tr>":"" ;
			Information += (V.History[i].RoadName)?"<tr><td class=smallerbold>Road Name</td><td class=smaller>: &nbsp; "+V.History[i].RoadName+"</td></tr>":"" ;
			if (Get_Cookie('Goldcoast_GPS_Login_Rights') != 1) {
				Information += (V.History[i].Speed)?"<tr><td class=smallerbold>Speed</td><td class=smaller>: &nbsp; "+V.History[i].Speed+"</td></tr>":"" ;
			}
			Information += (V.History[i].Heading)?"<tr><td class=smallerbold>Heading</td><td class=smaller>: &nbsp; "+V.History[i].Heading+"</td></tr>":"" ;
			Information += (V.History[i].Altitude)?"<tr><td class=smallerbold>Altitude</td><td class=smaller>: &nbsp; "+V.History[i].Altitude+"</td></tr>":"" ;
			Information += (V.History[i].Satellite)?"<tr><td class=smallerbold>Satellite</td><td class=smaller>: &nbsp; "+V.History[i].Satellite+"</td></tr>":"" ;
			Information += (V.History[i].Report)?"<tr><td class=smallerbold>Report</td><td class=smaller>: &nbsp; "+V.History[i].Report+"</td></tr>":"" ;
			Information += (V.History[i].Input)?"<tr><td class=smallerbold>Input</td><td class=smaller>: &nbsp; "+V.History[i].Input+"</td></tr>":"" ;
			Information += (V.History[i].Output)?"<tr><td class=smallerbold>Output</td><td class=smaller>: &nbsp; "+V.History[i].Output+"</td></tr>":"" ;
			Information += (V.History[i].Input9)?"<tr><td class=smallerbold>Input9</td><td class=smaller>: &nbsp; "+V.History[i].Input9.toFixed(2)+" "+
			V.Unit1.replace("&#176;","")+"</td></tr>":"" ;
			Information += (V.History[i].Input10)?"<tr><td class=smallerbold>Input10</td><td class=smaller>: &nbsp; "+V.History[i].Input10.toFixed(2)+" "+V.Unit2.replace("&#176;","")+"</td></tr>":"" ;
			Information += (V.History[i].Distance>=0)?"<tr><td class=smallerbold>Distance</td><td class=smaller>: &nbsp; "+V.History[i].Distance.toFixed(2)+" KM</td></tr>":"" ;
			Information += "</table>" ;
			BVI.innerHTML = Information ;
			if (M=='mouse')
			{
				var MTB = document.getElementById('VehicleInformationTable') ;
				var MTL = document.getElementById('GM_Tiling_Div') ;
				var MTG = ShowOffSet('GenMapDiv') ;
				var SLeft = parseInt(Img.style.left) + parseInt(MTL.style.left) + parseInt(MTG.left) + Math.round(Img.width) + 5 ;
				var STop = parseInt(Img.style.top) + parseInt(MTL.style.top) + parseInt(MTG.top) + 5 ;

				if (SLeft+MTB.offsetWidth > (window.screen.width-20))
				{
					SLeft -= MTB.offsetWidth + Math.round(Img.width) + 10; 
				}
				if (STop+MTB.offsetHeight > (window.screen.height-180))
				{
					STop -= MTB.offsetHeight ; 
				}
				BVI.style.left = SLeft;
				BVI.style.top = STop;
			} else {
				var MTG = ShowOffSet('GenMapDiv') ;
				BVI.style.left = MTG.left;
				BVI.style.top = MTG.top;
			}
		}
	} else {
		BVI.innerHTML = "" ;
	}
}

function Global_RefreshLink() 
{
	var Global_MenuLink = new Array ();
	Global_MenuLink[0] = "LinkMap";
	Global_MenuLink[1] = "LinkReport";
	Global_MenuLink[2] = "LinkReplay";
	Global_MenuLink[2] = "LinkMessaging";	
	Global_MenuLink[3] = "LinkUser";
	Global_MenuLink[4] = "LinkGeofence";
	Global_MenuLink[5] = "LinkDownload";
	Global_MenuLink[6] = "LinkAdministrator" ;

	for (var i in Global_MenuLink)
	{
		if (document.getElementById(Global_MenuLink[i]))
		{
			var NL  = document.getElementById(Global_MenuLink[i]);
			NL.href = NL.href + "?Ref=" + Math.round(Math.random(0,1)*100000000000) ;
			var MyRight = Get_Cookie('Goldcoast_GPS_Login_Rights') ;
			if (MyRight == 1) 
			{
				if (i == 1 || i == 2 || i == 4 || i == 5 || i == 6)
				{
					NL.innerHTML = "" ;
				}				
			}
			if (MyRight != 64 && (i == 5 || i == 6) ) 
			{
				NL.innerHTML = "";
			}
		}
	}
	GetGlobalUrlParam();
}

function GetGlobalUrlParam()
{
	if (Get_Cookie('Goldcoast_GPS_Login_UserName'))
	{
		var tz = new Date()
		var OS = -(tz.getTimezoneOffset());
		var TZOS = "&TZOS=" + OS ;
		TZOS = "" ;
		Global_Url_Param = "&Q2479=" + encodeURI(Get_Cookie('Goldcoast_GPS_Login_UserName')) + TZOS ;
	}
	LoadCarsIcons();
}

function SetGlobalUrlParam()
{
	if (Get_Cookie('Goldcoast_GPS_Login_UserName'))
	{
		var tz = new Date()
		var OS = -(tz.getTimezoneOffset());
		var TZOS = "&TZOS=" + OS ;
		TZOS = "" ;
		return "&Q2479=" + encodeURI(Get_Cookie('Goldcoast_GPS_Login_UserName')) + TZOS ;
	} else {
		return "" ;
	}
}

function Global_CheckLogin()
{
	if (Get_Cookie('Goldcoast_GPS_Login_UserID')) 
	{
		location.href = "default.html" + "?Ref=" + Math.round(Math.random(0,1)*100000000000) ;
	}
}

function Global_CheckLogout() 
{
	if (!Get_Cookie('Goldcoast_GPS_Login_UserID'))
	{
		location.href = "index.html" + "?Ref=" + Math.round(Math.random(0,1)*100000000000) ;
	}
}

function Global_Logout()
{
	var UserName  = Get_Cookie('Goldcoast_GPS_Login_UserName');
	ActivityRecorder(UserName, '0');

	Set_Cookie( 'Goldcoast_GPS_Login_UserID', '', 0, '/', '', '' );
	Set_Cookie( 'Goldcoast_GPS_Login_UserName', '', 0, '/', '', '' );
	Set_Cookie( 'Goldcoast_GPS_Login_ClientID', '', 0, '/', '', '' );
	Set_Cookie( 'Goldcoast_GPS_Login_Rights', '', 0, '/', '', '' );
	location.href = "index.html" + "?Ref=" + Math.round(Math.random(0,1)*100000000000) ; ;
}

function Global_LoginSuccess(UserID,UserName,ClientID,Rights) 
{
	Set_Cookie( 'Goldcoast_GPS_Login_UserID', UserID, 10000, '/', '', '' );
	Set_Cookie( 'Goldcoast_GPS_Login_UserName', UserName, 10000, '/', '', '' );
	Set_Cookie( 'Goldcoast_GPS_Login_ClientID', ClientID, 10000, '/', '', '' );
	Set_Cookie( 'Goldcoast_GPS_Login_Rights', Rights, 10000, '/', '', '' );
	ActivityRecorder(UserName, '1');
	location.href = "default.html" + "?Ref=" + Math.round(Math.random(0,1)*100000000000) ; ;
}

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( name )
{
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function Global_FreeAjaxProcess()
{
  if (GPS_CounterAjaxProcess > 0) GPS_CounterAjaxProcess--;
  GPS_LoadingStop("DB");
}

function Global_CekInvalidAjaxProcess(kode)
{
  
}

function Global_RequestReady(ID,http_request)
{
 var txt = "";
 //try
 //{
	if (http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
			RequestDone(ID,http_request) ;
  		   if (GPS_CounterAjaxProcess > 0) GPS_CounterAjaxProcess--;
		} else {
			//alert('There was a problem with the data request. please refresh the page.');
		}
	}
	/*}catch(e) {
	  GPS_LoadingStart("DB", "Loading ajax failed with ID = " + ID + " when trying to receive data.<br> Please refresh page or trying to load the data again.");
	  setTimeout("Global_FreeAjaxProcess();", 3000);
	  RequestDone(ID,'');
    }*/	
}

function Global_Request(ID,url)
{
	var url_post = url;
	var url_send = null;
	if (url.indexOf('?')>0)
	{
		url_post = url.substring(0,url.indexOf('?'));
		url_send = url.substring(url.indexOf('?')+1);
	} else {
		url_post = url ;
		url_send = null ;
	}

	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType)
		{
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = function() { Global_RequestReady(ID,http_request) }
	
    GPS_CounterAjaxProcess++;
   //try
  //{
	if (url_send != null)
	{
		if (document.getElementById('XXX'))
		{
			var XXX = document.getElementById('XXX') ;
			XXX.innerHTML = url_post + "<br>" ;
			XXX.innerHTML += url_send ;
		}
		http_request.open('POST', url_post, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader("Content-length", url_send.length);
		http_request.setRequestHeader("Connection", "close");	
		http_request.send(url_send);
	} else {
		http_request.open('GET', url_post, true);
		http_request.send(url_send);
	}
  /*}catch(e) {
	  GPS_LoadingStart("DB", "Loading ajax failed with ID = " + ID + " when trying to sending data.<br> Please refresh page or trying to load the data again.");
	  setTimeout("Global_FreeAjaxProcess();", 3000);
	  RequestDone(ID,'');
  }*/
}


function Global_Request_Post(ID,url)
{
	var url_post = url;
	var url_send = null;
	if (url.indexOf('?')>0)
	{
		url_post = url.substring(0,url.indexOf('?'));
		url_send = url.substring(url.indexOf('?')+1);
	} else {
		url_post = url ;
		url_send = null ;
	}
	var http_request = false;

	if (window.XMLHttpRequest)
	{ // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType)
		{
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = function() { Global_RequestReady(ID,http_request) }
	
  GPS_CounterAjaxProcess++;
  try
  {
	if (url_send != null)
	{
		if (document.getElementById('XXX'))
		{
			var XXX = document.getElementById('XXX') ;
			XXX.innerHTML = url_post + "<br>" ;
			XXX.innerHTML += url_send ;
		}
		http_request.open('POST', url_post, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader("Content-length", url_send.length);
		http_request.setRequestHeader("Connection", "close");	
		http_request.send(url_send);
	} else {
		http_request.open('GET', url_post, true);
		http_request.send(url_send);
	}
  }catch(e) {
	  GPS_LoadingStart("DB", "Loading ajax failed with ID = " + ID + " when trying to sending data.<br> Please refresh page or trying to load the data again.");
	  setTimeout("Global_FreeAjaxProcess();", 3000);
	  RequestDone(ID,'');
  }
}

function CheckDatebyMonthYear(FormName,DForm,MForm,YForm)
{
	var SD = eval("document."+FormName+"."+DForm);
	var SM = eval("document."+FormName+"."+MForm);
	var SY = eval("document."+FormName+"."+YForm);

	var TDD = SD.options[SD.options.selectedIndex].value ;
	var TDM = SM.options[SM.options.selectedIndex].value ;
	var TDY = SY.options[SY.options.selectedIndex].value ;

	var LongDate = (TDM==1 || TDM==3 || TDM==5 || TDM==7 || TDM==8 || TDM==10 || TDM==12)?31:30;
	TDM = TDM * 1 ;	
	if (TDM == 2)
	{
		LongDate = (TDY%4==0)?29:28;
	}
	while (SD.options.value)
	{
	   SD.options[0] = null ;
	}
	var j = 0 ;
	for (var i=1;i<=LongDate ;i++ )
	{
		var ValI = i ;
		ValI = (ValI<10)?"0"+i:i;
		SD.options[j] = new Option(SD,SD,true,true,true) ;
	    SD.options[j].text = i ;
	    SD.options[j].value = ValI ;
		j++;
	}
	
	if (TDD <= LongDate)
	{
		SD.options[TDD-1].selected = true ;
	} else {
		SD.options[j-1].selected = true ;
	}

}


function GoCustomTimeNew(FormName,HForm,MForm,SForm,H,M,S) 
{
	var SH = eval("document."+FormName+"."+HForm);
	var SM = eval("document."+FormName+"."+MForm);
	var SS = eval("document."+FormName+"."+SForm);

	var Sel = 0 ;
	var j = 0 ;
	for (var i=0; i<=23; i++)
	{
		var ValI = i ;
		ValI = (ValI<10)?"0"+i:i;
		SH.options[j] = new Option(SH,SH,true,true,true) ;
	    SH.options[j].text = ValI ;
	    SH.options[j].value =  ValI ;
		if (i == H)
		{
			Sel = j ;
		}
		j++;
	}
	SH.options[Sel].selected = true ;

	Sel = 0 ;
	j = 0 ;
	for (var i=0; i<=59; i++)
	{
		var ValI = i ;
		ValI = (ValI<10)?"0"+i:i;
		SM.options[j] = new Option(SM,SM,true,true,true) ;
	    SM.options[j].text = ValI ;
	    SM.options[j].value =  ValI ;
		if (i == M)
		{
			Sel = j ;
		}
		j++;
	}
	SM.options[Sel].selected = true ;

	Sel = 0 ;
	j = 0 ;
	for (var i=0; i<=59; i++)
	{
		var ValI = i ;
		ValI = (ValI<10)?"0"+i:i;
		SS.options[j] = new Option(SS,SS,true,true,true) ;
	    SS.options[j].text = ValI ;
	    SS.options[j].value =  ValI ;
		if (i == S)
		{
			Sel = j ;
		}
		j++;
	}
	SS.options[Sel].selected = true ;
}

function GoCustomDateNew(FormName,DForm,MForm,YForm,DD,MM,YY)
{
	var SD = eval("document." + FormName + "."+DForm);
	var SM = eval("document." + FormName + "."+MForm);
	var SY = eval("document." + FormName + "."+YForm);
	if (DD == 0 || MM == 0 || YY == 0)
	{
		var a = new Date ;
		TDD = a.getDate() ;
		TMM = a.getMonth() + 1 ;
		TYY = a.getFullYear() ;
	} else {
		TDD = DD ;
		TMM = MM ;
		TYY = YY ;
	}
	var MonthName = new Array () ;
	MonthName[1] = "Jan" ;
	MonthName[2] = "Feb" ;
	MonthName[3] = "Mar" ;
	MonthName[4] = "Apr" ;
	MonthName[5] = "May" ;
	MonthName[6] = "Jun" ;
	MonthName[7] = "Jul" ;
	MonthName[8] = "Aug" ;
	MonthName[9] = "Sep" ;
	MonthName[10] = "Oct" ;
	MonthName[11] = "Nov" ;
	MonthName[12] = "Dec" ;
	var Sel = 0 ;
	var j = 0 ;
	for (var i=(TYY-5); i<=(TYY+5); i++ )
	{
		SY.options[j] = new Option(SY,SY,true,true,true) ;
	    SY.options[j].text = i ;
	    SY.options[j].value =  i ;
		if (i == TYY)
		{
			Sel = j ;
		}
		j++;
	}
	SY.options[Sel].selected = true ;

	j = 0 ;
	for (var i=1;i<=12 ;i++ )
	{
		var ValI = i ;
		ValI = (ValI<10)?"0"+i:i;
		SM.options[j] = new Option(SM,SM,true,true,true) ;
	    SM.options[j].text = MonthName[i] ;
	    SM.options[j].value = ValI ;
		if (i == TMM) 
		{
			Sel = j ;
		}
		j++;
	}
	SM.options[Sel].selected = true ;

	var LongDate = (TMM==1 || TMM==3 || TMM==5 || TMM==7 || TMM==8 || TMM==10 || TMM==12)?31:30;
	if (TMM == 2)
	{
		LongDate = (TYY%4==0)?29:28;
	}
	j = 0 ;
	for (var i=1;i<=LongDate ;i++ )
	{
		var ValI = i ;
		ValI = (ValI<10)?"0"+i:i;
		SD.options[j] = new Option(SD,SD,true,true,true) ;
	    SD.options[j].text = i ;
	    SD.options[j].value = ValI ;
		if (i == TDD) 
		{
			Sel = j ;
		}
		j++;
	}
	SD.options[Sel].selected = true ;
}

/*========================================================================================================================================================================
Start TAS Conversion
========================================================================================================================================================================*/
function Tas_NewPoint()
{
	this.X = 0 ;
	this.Y = 0 ;
	this.Height = 0;
}

function ConverToTasMeter(X,Y)
{
	var Ln = X ;
	var Lt = Y ;
	var CM = 147 ;
	var PHI = 3.14159265358979 ;
	var RadLat = (Lt/180)*PHI ;
	var RadLon = (Ln/180)*PHI ;
	var LnDif = Ln - CM ;
	var RadLonDif = (LnDif/180)*PHI ;
	var Elec = 0.00669438002290079;
	var Nu = 6378137/Math.sqrt(1-(Elec*Math.sin(RadLat)*Math.sin(RadLat))) ;
	var Rho = 6378137*(1-Elec)/(Math.sqrt(1-(Elec*Math.sin(RadLat)*Math.sin(RadLat)))*(1-(Elec*Math.sin(RadLat)*Math.sin(RadLat)))) ;
	var Psi = Nu/Rho ;
	var SinLat = Math.sin(RadLat) ;
	var CosLat = Math.cos(RadLat) ;
	var TanLat = Math.sin(RadLat)/Math.cos(RadLat);
	var A0 = 0.998324298444585 ;
	var A2 = 0.00251460707284482 ;
	var A4 = 0.0000026390466202309 ;
	var A6 = 0.00000000341804613677521 ;
	var TanLat_2 = TanLat * TanLat ;
	var TanLat_4 = TanLat_2 * TanLat_2 ; 
	var TanLat_6 = TanLat_4 * TanLat_2 ;
	var RadLonDif_2 = RadLonDif * RadLonDif ;
	var RadLonDif_3 = RadLonDif_2 * RadLonDif ;
	var RadLonDif_4 = RadLonDif_3 * RadLonDif ;
	var RadLonDif_5 = RadLonDif_4 * RadLonDif ;
	var RadLonDif_6 = RadLonDif_5 * RadLonDif ;
	var RadLonDif_7 = RadLonDif_6 * RadLonDif ;
	var RadLonDif_8 = RadLonDif_7 * RadLonDif ;
	var CosLat_2 = CosLat * CosLat;
	var CosLat_3 = CosLat_2 * CosLat;
	var CosLat_4 = CosLat_3 * CosLat;
	var CosLat_5 = CosLat_4 * CosLat;
	var CosLat_6 = CosLat_5 * CosLat;
	var CosLat_7 = CosLat_6 * CosLat;
	var Psi_2 = Psi * Psi ;
	var Psi_3 = Psi_2 * Psi ;
	var Psi_4 = Psi_3 * Psi ;
	var MeridienDistance = (6378137*RadLat*A0)+(-6378137*Math.sin(2*RadLat)*A2)+(6378137*Math.sin(4*RadLat)*A4)+(-6378137*Math.sin(6*RadLat)*A6);
	var DegX = (0.9996*((Nu*RadLonDif*CosLat)+(Nu*RadLonDif_3*CosLat_3*(Psi-TanLat_2)/6)+(Nu*RadLonDif_5*CosLat_5*(4*Psi_3*(1-6*TanLat_2)+Psi_2*(1+8*TanLat_2)-Psi*(2*TanLat_2)+TanLat_4)/120)+(Nu*RadLonDif_7*CosLat_7*(61-479*TanLat_2+179*TanLat_4-TanLat_6)/5040)))+(500000) ;
	var DegY = (0.9996*((MeridienDistance)+(Nu*SinLat*RadLonDif_2*CosLat/2)+(Nu*SinLat*RadLonDif_4*CosLat_3*(4*Psi_2+Psi-TanLat_2)/24)+(Nu*SinLat*RadLonDif_6*CosLat_5*(8*Psi_4*(11-24*TanLat_2)-28*Psi_3*(1-6*TanLat_2)+Psi_2*(1-32*TanLat_2)-Psi*(2*TanLat_2)+TanLat_4)/720)+(Nu*SinLat*RadLonDif_8*CosLat_7*(1385-3111*TanLat_2+543*TanLat_4-TanLat_6)/40320)))+(10000000);
	var MeterX = DegX-112;
	var MeterY = DegY-184;
	
	var NewConvert = new Tas_NewPoint() ;
	NewConvert.X = MeterX ;
	NewConvert.Y = MeterY ;

	return NewConvert ;
}


/*========================================================================================================================================================================
Start NZ Conversion
========================================================================================================================================================================*/
var a   = 6378388;
var e0  = 2510000;
var n0  = 6023150;
var lt0 = -41.0;
var ln0 = 173.0;

function Cartesian()
{
	this.x = 0 ;
	this.y = 0 ;
	this.z = 0 ;
}

function NZ_NewPoint()
{
	this.X = 0 ;
	this.Y = 0 ;
	this.Height = 0;
}

function ConverToNZMeter(X,Y) 
{
	var NewConvert = new NZ_NewPoint() ;
	var Tmp1 = new NZ_NewPoint() ;
	Tmp = NZGD2KtoNZGD49(X,Y);
	var Tmp2 = new NZ_NewPoint() ;
	Tmp2 = GeodToNZMG(Tmp.X,Tmp.Y);
	NewConvert.X = Tmp2.X ;
	NewConvert.Y = Tmp2.Y ;
	return NewConvert ;
}

function NZGD2KGeoToNZGD49Cartesian(_long, _lat, _height) 
{
	var a_nz    = 6378137 ;
	var f       = 1/298.257222101 ;
	var deg2rad = Math.PI/180 ;
	var _b = 6356752.314 ;
	var _e = 2*f - Math.pow(f,2) ;
	var _n = a_nz / Math.sqrt(1 - _e * Math.pow(Math.sin(_lat*deg2rad),2));
	var _XYZ = new Cartesian() ;
	_XYZ.x = (_n + _height) * Math.cos(_lat*deg2rad) * Math.cos(_long*deg2rad);
	_XYZ.y = (_n + _height) * Math.cos(_lat*deg2rad) * Math.sin(_long*deg2rad);
	_XYZ.z = (_n * (1 - _e) + _height) * Math.sin(_lat*deg2rad);
	return _XYZ ;
}

function NZGD49CartesianToNZGD49Geo(_x,_y,_z) 
{
	var _lat = 0 ;
	var _long = 0 ;
	var a_nz = 6378388;
	var f    = 1/297;
	var deg2rad = Math.PI/180;
	var rad2deg = 180/Math.PI;
	var _b = 6356911.946;
	var _e = 2*f - Math.pow(f,2);
	var _n = a_nz / Math.sqrt(1 - _e*Math.pow(Math.sin(_lat),2));
	var _p = Math.sqrt(Math.pow(_x,2)+Math.pow(_y,2));
	var _e2 = Math.pow((a_nz /_b),2)-1;
	var theta = Math.atan2(_z*a_nz,_p*_b);
	var P = new NZ_NewPoint() ;
	P.Y = Math.atan2(_z+(_e2*_b*Math.pow(Math.sin(theta),3)), _p-(_e*a*Math.pow(Math.cos(theta),3))) * rad2deg;
	P.X = Math.atan2(_y, _x) * rad2deg;
	return P ;
}

function NZGD2KtoNZGD49(X,Y) 
{
	var Tx  = 59.47;
	var Ty  = -5.04;
	var Tz  = 187.44;
	var Rx  = -0.47;
	var Ry  = 0.10;
	var Rz  = -1.024;
	var ds  = -4.5993;
	var k   = 0.000004848137;

	var _XYZ = NZGD2KGeoToNZGD49Cartesian(X,Y,0);
	var xyz2k = new Cartesian() ;
	var xyz49 = new Cartesian() ;
	var tmp = new Array(new Array(4),new Array(4),new Array(4),new Array(4));
	var itmp = new Array(new Array(4),new Array(4),new Array(4),new Array(4));

	xyz2k.x = (_XYZ.x - Tx) / (1 + ds*1e-6);
	xyz2k.y = (_XYZ.y - Ty) / (1 + ds*1e-6);
	xyz2k.z = (_XYZ.z - Tz) / (1 + ds*1e-6);

	tmp[1][1]  = 1.0;   tmp[1][2]  = k*Rz;  tmp[1][3]  = -k*Ry;
	tmp[2][1]  = -k*Rz; tmp[2][2]  = 1;     tmp[2][3]  = k*Rz;
	tmp[3][1]  = k*Ry;  tmp[3][2]  = -k*Rx; tmp[3][3]  = 1;
	
	_det = (tmp[1][1]*tmp[2][2]*tmp[3][3])+(tmp[1][2]*tmp[2][3]*tmp[3][1])+(tmp[1][3]*tmp[2][1]*tmp[3][2]) - (tmp[1][3]*tmp[2][2]*tmp[3][1])-(tmp[1][2]*tmp[2][1]*tmp[3][3])-(tmp[1][1]*tmp[3][2]*tmp[2][3]);

	itmp[1][1] = (Math.pow(-1,2)*(tmp[2][2]*tmp[3][3]-tmp[3][2]*tmp[2][3]))/_det;
	itmp[1][2] = (Math.pow(-1,3)*(tmp[1][2]*tmp[3][3]-tmp[3][1]*tmp[1][3]))/_det;
	itmp[1][3] = (Math.pow(-1,4)*(tmp[1][2]*tmp[2][3]-tmp[2][2]*tmp[1][3]))/_det;
	itmp[2][1] = (Math.pow(-1,3)*(tmp[2][1]*tmp[3][3]-tmp[3][1]*tmp[2][3]))/_det;
	itmp[2][2] = (Math.pow(-1,4)*(tmp[1][1]*tmp[3][3]-tmp[3][1]*tmp[1][3]))/_det;
	itmp[2][3] = (Math.pow(-1,5)*(tmp[1][1]*tmp[2][3]-tmp[2][1]*tmp[1][3]))/_det;
	itmp[3][1] = (Math.pow(-1,4)*(tmp[2][1]*tmp[3][2]-tmp[3][1]*tmp[2][3]))/_det;
	itmp[3][2] = (Math.pow(-1,5)*(tmp[1][1]*tmp[3][2]-tmp[3][1]*tmp[1][2]))/_det;
	itmp[3][3] = (Math.pow(-1,6)*(tmp[1][1]*tmp[2][2]-tmp[2][1]*tmp[1][2]))/_det;

	xyz49.x = (itmp[1][1]*xyz2k.x) + (itmp[1][2]*xyz2k.y) + (itmp[1][3]*xyz2k.z);
	xyz49.y = (itmp[2][1]*xyz2k.x) + (itmp[2][2]*xyz2k.y) + (itmp[2][3]*xyz2k.z);
	xyz49.z = (itmp[3][1]*xyz2k.x) + (itmp[3][2]*xyz2k.y) + (itmp[3][3]*xyz2k.z);	
	
	var Dt = NZGD49CartesianToNZGD49Geo(xyz49.x,xyz49.y,xyz49.z);
	return Dt ;
}

function _Complex()
{
	this._real  = 0;
	this._imag  = 0;
}

function ToComplex(_real,_imag) 
{
	var R = new _Complex() ; 
	R._real  = _real;
	R._imag  = _imag;
	return R ;
}

function c_add(c1,c2) 
{
	var R = new _Complex() ; 
	R._real = c1._real + c2._real;
	R._imag = c1._imag + c2._imag;
	return R ;
}

function c_mult(c1,c2) 
{
	var R = new _Complex() ; 
	R._real = c1._real * c2._real - c1._imag * c2._imag;
	R._imag = c1._real * c2._imag + c1._imag * c2._real;
	return R ;
}

function c_power(c1,exp) 
{
	var tmp = c1 ;
	for (var i=1;i<=exp-1;i++){ 
		tmp = c_mult(tmp,c1) ;
	}
	return tmp;
}

function GeodToNZMG(_long,_lat) 
{
	var An = [0,0.6399175073,-0.1358797613,0.063294409,-0.02526853,0.0117879,-0.0055161,0.0026906,-0.001333,0.00067,-0.00034];
	var BnR = [0,0.7557853228,0.249204646,-0.001541739,-0.10162907,-0.26623489,-0.6870983];
	var BnI = [0,0.0,0.003371507,0.041058560,0.01727609,-0.36249218,-1.1651967];
	var dLat  = (_lat - lt0) * 3600e-5;
	var dLong = (_long - ln0) * Math.PI/180;
	var dY    = 0;
	for (var i=1; i<An.length; i++)
	{
		dY  = dY + (An[i]*Math.pow(dLat,i));
	}

	var z = new _Complex();
	var theta = new _Complex();
	var Bn = new _Complex();
	theta = ToComplex(dY, dLong);
	for (var i=1; i<BnR.length;i++) 
	{
		Bn = ToComplex(BnR[i],BnI[i]);
		z =  c_add(z,c_mult(Bn, c_power(theta,i)));
	}

	var _east = (z._imag * a) + e0;
	var _north= (z._real * a) + n0;

	var Dt = new NZ_NewPoint() ;
	Dt.X = _east ;
	Dt.Y = _north ;
	return Dt ;
}

/*========================================================================================================================================================================
End NZ Conversion
========================================================================================================================================================================*/