var xmlHttp
var xmlHttp2
var xmlHttp3
var etripdestdiv
var etripdestdiv2
var nr2
var nr

var confirmMsg  = 'Vil du';

function confirmLink(theLink, theSqlQuery)
{
    if (confirmMsg == '' || typeof(window.opera) != 'undefined') 
    {
    	return true;
    }

    var is_confirmed = confirm(confirmMsg + ' ' + theSqlQuery);
    
    return is_confirmed;
}

function ajax_action(parameters,destdiv)
{ 
	etripdestdiv=destdiv
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	var url="ajax.php"
	url=url+parameters+'&rnd=';
	xmlHttp.onreadystatechange=stateChangedprodinfo
	/*document.getElementById(etripdestdiv).innerHTML='<img src="/system/images/loading.gif" />&nbsp;E-trip beregner..';
	*/
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function stateChangedprodinfo() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById(etripdestdiv).innerHTML=xmlHttp.responseText; //'triploaddiv'
		// parent.mapframe.getElementById('mappageinfodiv').innerHTML=xmlHttp.responseText 
	} 
} 



function ajax_action2(parameters,destdiv)
{ 
	etripdestdiv2=destdiv
	xmlHttp3=GetXmlHttpObject()
	if (xmlHttp3==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	var url="ajax.php"
	url=url+parameters+'&rnd=';
	xmlHttp3.onreadystatechange=stateChangedprodinfo2
	/*document.getElementById("vehiclelist").innerHTML='<img src="images/loading.gif" />&nbsp;laster..';
	*/
	
	xmlHttp3.open("GET",url,true)
	xmlHttp3.send(null)
}

function showloaderanim()
{
	// document.getElementByID(\'bookcontentcontainer\').innerHTML=\'<img src=images/ajax-loader.gif>\';
	document.getElementById("bookcontentcontainer").innerHTML='<br><br><center><img src="/system/images/ajax-loader.gif"><br><br><b>Vennligst vent</b><br>ETRIP genererer kj&oslash;reboken slik at den inneholder enhetens siste bevegelser.<br>Generering kan ta opptil flere minutter.</center>';
}

function stateChangedprodinfo2() 
{ 
	if (xmlHttp3.readyState==4 || xmlHttp3.readyState=="complete")
	{ 
		document.getElementById('vehiclelist').innerHTML = xmlHttp3.responseText; //'triploaddiv'
		// parent.mapframe.getElementById('mappageinfodiv').innerHTML=xmlHttp.responseText 
	} 
} 


function ajax_fetchtripdata(parameters, thisnr)
{ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
	alert ("Browser does not support HTTP Request")
	return
	} 
	
	//  nr =  "ajax_container" + thisnr;
	 nr =  thisnr;
	/*document.getElementById(nr).innerHTML='<img src="/system/images/loading.gif" />&nbsp;E-trip beregner..';
	*/
	var url="ajax.php"
	url=url+parameters+"&divtag="+nr;
	xmlHttp.onreadystatechange=stateChangedtripinfo
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


function stateChangedtripinfo() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	document.getElementById(nr+'').innerHTML=xmlHttp.responseText;	
	} 
} 





function ajax_fetchtripdata_livemonitor(parameters)
{ 
	xmlHttp2=GetXmlHttpObject()
	if (xmlHttp2==null)
	{
	alert ("Browser does not support HTTP Request")
	return
	} 	
	
	nr2 =  'deb';
	/*document.getElementById(nr2).innerHTML='';*/
	var url="ajax.php"
	url=url+parameters+"&divtag="+nr2;
	xmlHttp2.onreadystatechange=stateChangedtripinfo2
	xmlHttp2.open("GET",url,true)
	xmlHttp2.send(null)

	// lage loop her, med kobling til hastighet-div på venstre marg live moni.
	// ................
}


function returnControl(controlName)
{
 return document.getElementByID(controlName);
}


/*
document.getElementById(thisdiv).innerHTML = '<span class=driving><img src="images/pix.gif" height=8 width=8>' + texts3[1] + '(' + textVal + ')</span>';
else
document.getElementById(thisdiv).innerHTML = '<span class=parked><img src="images/pix.gif" height=8 width=8>' + texts3[1] + '(' + textVal + ')</span>';
*/

function stateChangedtripinfo2() 
{ 
	if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete")
	{ 	
		// string returned is: sim_kmt_street$sim_kmt_street$sim_kmt_street$sim_kmt_street$ etc..	 
		var textVal = xmlHttp2.responseText;
		var texts = textVal.split("$");
		//alert(textVal);
		
		for ( i=0; i < texts.length; i++ )
		{		 
			var textVal2 = ( texts[i] );		 
			var texts3 = textVal2.split("_");
			
			if(texts3[0] > 0)
			{
				var thisdiv = 'speedinfo' + texts3[0];
				if(parseFloat(texts3[1]) > 3)
				document.getElementById(''+ thisdiv+'' ).innerHTML = '<img src="/system/icons/diode1.gif">';
//				document.getElementById('"'+thisdiv+'"').innerHTML = '<a href="javascript:;" onclick="newWindow(\'google.php?lat=' + texts3[3] +'&long=' + texts3[4] +'&txt=' + texts3[2] + '\',\'KART\',800,600,\'\')"><img src="/system/icons/diode1.gif" ></a>&nbsp;&nbsp;';
				else
					{
						// alert('div: '+ thisdiv+'');
				// 37.4419, -122.1419
				document.getElementById(''+ thisdiv+'' ).innerHTML = '<img src="/system/icons/diode0.gif">';
			}
//				document.getElementById('"'+thisdiv+'"').innerHTML = '<a href="javascript:;" onclick="newWindow(\'google.php?lat=' + texts3[3] +'&long=' + texts3[4] +'&txt=' + texts3[2] + '\',\'KART\',800,600,\'\')"><img src="/system/icons/diode0.gif" ></a>&nbsp;&nbsp;';
			}
			
			if(texts3[0] > 0)
			{
				var thisdiv = 'streetinfo' + texts3[0];
				document.getElementById(''+ thisdiv+'' ).innerHTML = texts3[2]  ;
			} 		
		}         	
	} 
} 

 

function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
	objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
	objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}



function showDiv(){
hideinfoDiv();
document.getElementById("kartdiv").style.display="block"
}

function hideDiv(){
document.getElementById("kartdiv").style.display="none"
showinfoDiv();
}

function showinfoDiv(){
document.getElementById("mappageinfodiv").style.display="block"
}

function hideinfoDiv(){
document.getElementById("mappageinfodiv").style.display="none"
}



function openPopup(url, height, width, pageTitle){
	var left = (GetScreenWidth() / 2) - (width / 2);
	var top = (GetScreenHeight() / 2) - (height / 2);
	var attrib = "height=" + height + ",width=" + width + ",left=" + left + ",top=" + top + ",resizable,scrollbars";
	window.open(url,"",attrib);
}

function GetWindowWidth() {

	var frameWidth;
	
	if (top.innerWidth)
	{
		frameWidth = top.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		frameWidth = document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		frameWidth = document.body.clientWidth;
	}
	
	return frameWidth;

}

function GetScreenWidth() {
	return self.screen.width;
}

function GetScreenHeight() {
	return self.screen.height;
}

function WindowOnload(f) {
	var prev = window.onload;
	window.onload = function() { if(prev) prev(); f(); }
}

function ShowHideDiv(id)
{
	var e = document.getElementById(id);	
	if (e.style.display == 'block' || e.style.display == '')
	{
		e.style.display = 'none'; 
	}
	else
	{
		e.style.display = 'block'; 
	}
}

/*
function HideDiv(id)
{
	var e = document.getElementById(id);	
	e.style.display = 'none'; 
}

function ShowDiv(id)
{
	var e = document.getElementById(id);	
	e.style.display = 'block';	
}
*/
function ShowDivByElement(e)
{
	e.style.display = 'block';
}







/*

timer functions 
Per Waagen

*/

var timerID = null;
var timerRunning = false;
function stopclock (){
        if(timerRunning)
                clearTimeout(timerID);
        timerRunning = false;
}

function startclock () {
        stopclock();
        showtime();
}

function showtime () 
{			
	/*
	ajax_action2('?cmd_updatevehiclelist=1','vehiclelist');		
	ajax_fetchtripdata('?cmd_multiplelocations=1','infowrapper');
	ajax_fetchtripdata_livemonitor('?cmd_getvehicledata=1');
/**/

	ajax_action2('?cmd_updatevehiclelist=1','vehiclelist');		
	ajax_fetchtripdata('?cmd_multiplelocations=1','infowrapper');
	ajax_fetchtripdata_livemonitor('?cmd_getvehicledata=1');

    timerID = setTimeout("showtime()",6000);
    timerRunning = true;
}
 


function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			document.myForm.time.value = ajaxRequest.responseText;
		}
	}
	ajaxRequest.open("GET", "!ajax.php", true);
	ajaxRequest.send(null); 
}    