// here we define global variable
var ajaxdestination="";

function getdata(what,where) { // get data from source (what)
 try {
   xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
  		new ActiveXObject("Microsoft.XMLHTTP");
 }
 catch (e) { /* do nothing */ }

 document.getElementById(where).innerHTML ="<center><img src='loading.gif'></center>";
// we are defining the destination DIV id, must be stored in global variable (ajaxdestination)
 ajaxdestination=where;
 xmlhttp.onreadystatechange = triggered; // when request finished, call the function to put result to destination DIV
 xmlhttp.open("GET", what);
 xmlhttp.send(null);
  return false;
}

function triggered() { // put data returned by requested URL to selected DIV
  if (xmlhttp.readyState == 4) if (xmlhttp.status == 200)
    document.getElementById(ajaxdestination).innerHTML =xmlhttp.responseText;
}


function hide(elem){document.getElementById(elem).style.display = 'none';}
function show(elem){document.getElementById(elem).style.display = 'block';}

// Nasconde/Visualizza un elemento definito come 'id' nel codice html
function hideElement(name)
{
    if (document.getElementById(name).style.display == "")
    {
        document.getElementById(name).style.display="none";
    }
    else
    {
        document.getElementById(name).style.display="";
    }
}

function showDetails(myid, prefixImage, prefixShow, prefixLink)
{
  var theImage = document.getElementById(prefixImage+myid);
  theImage.src = '/pics/common/minus.png';

  var theRow = document.getElementById(prefixShow+myid);
  theRow.style.display = 'inline';

  var theLink = document.getElementById(prefixLink+myid);
  theLink.onclick = function() {hideDetails(myid,prefixImage,prefixShow,prefixLink)};
}

function hideDetails(myid, prefixImage, prefixShow, prefixLink)
{
  var theImage = document.getElementById(prefixImage+myid);
  theImage.src = '/pics/common/plus.png';

  var theRow = document.getElementById(prefixShow+myid);
  theRow.style.display = 'none';

  var theLink = document.getElementById(prefixLink+myid);
  theLink.onclick = function() {showDetails(myid,prefixImage,prefixShow,prefixLink)};
}

function show(myid) { var theId = document.getElementById(myid); theId.style.display = 'inline'; }
function checkAll(field)  {if (field == undefined) return; for (var i=0; i<field.length; i++) field[i].checked = true;}
function uncheckAll(field){if (field == undefined) return; for (var i=0; i<field.length; i++) field[i].checked = false;}
function invertAll(field) {if (field == undefined) return; for (var i=0; i<field.length; i++) field[i].checked = !field[i].checked;}

function NewWindow(url,name,width,height,center,toolbar,status,menubar,scrollbars,resizable,top,left)
{
  var winl = 0;
  var wint = 0;
  if (center == 'yes')
  {
    winl = (screen.width-width)/2;
    wint = (screen.height-height)/2;
  }
  else
  {
    wint = top;
    winl = left;
  }
  var settings ='height='+height+',';
  settings +='width='+width+',';
  settings +='top='+wint+',';
  settings +='left='+winl+',';
  settings +='scrollbars='+scrollbars+',';
  settings +='toolbar='+toolbar+',';
  settings +='status='+status+',';
  settings +='menubar='+menubar+',';
  settings +='scrollbars='+scrollbars+',';
  settings +='resizable='+resizable;
  var win = window.open(url,name,settings);
  if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}

function rolloverImage(imgName, imgPath){var theImage = document.getElementById(imgName);
  theImage.src = imgPath;}
function rollover(imgName, imgPath, textID, textLabel)
{
  var theImage = document.getElementById(imgName);
  theImage.src = imgPath;
  var theText = document.getElementById(textID);
  theText.innerHTML = textLabel;
}

function MM_swapImgRestore(){var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;}
function MM_preloadImages(){var d=document;if(d.images){if(!d.MM_p) d.MM_p=new Array();var i,j=d.MM_p.length,a=MM_preloadImages.arguments;for(i=0; i<a.length; i++) if (a[i].indexOf("#")!=0){d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}}
function MM_findObj(n, d)
{var p,i,x;if(!d)d=document;
if((p=n.indexOf("?"))>0&&parent.frames.length){d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all)x=d.all[n];for (i=0;!x&&i<d.forms.length;i++)x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++)x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById)x=d.getElementById(n); return x;}
function MM_swapImage()
{var i,j=0,x,a=MM_swapImage.arguments;document.MM_sr=new Array;
for(i=0;i<(a.length-2);i+=3)if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x;if(!x.oSrc) x.oSrc=x.src;x.src=a[i+2];}}

function NewWindow(url,name,width,height,center,toolbar,status,menubar,scrollbars,resizable,top,left)
{
  var winl = 0;
  var wint = 0;
  if (center == 'yes')
  {
    winl = (screen.width-width)/2;
    wint = (screen.height-height)/2;
  }
  else
  {
    wint = top;
    winl = left;
  }

  var settings ='height='+height+',';
  settings +='width='+width+',';
  settings +='top='+wint+',';
  settings +='left='+winl+',';
  settings +='scrollbars='+scrollbars+',';
  settings +='toolbar='+toolbar+',';
  settings +='status='+status+',';
  settings +='menubar='+menubar+',';
  settings +='scrollbars='+scrollbars+',';
  settings +='resizable='+resizable;
  var win = window.open(url,name,settings);
  if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}

function uncheck(check1, check2, check3, check4, check5, check6)
{
    var theInput;
    if (check1 == undefined) return;
    theInput = document.getElementById(check1); theInput.checked = false;
    if (check2 == undefined) return;
    theInput = document.getElementById(check2); theInput.checked = false;
    if (check3 == undefined) return;
    theInput = document.getElementById(check3); theInput.checked = false;
    if (check4 == undefined) return;
    theInput = document.getElementById(check4); theInput.checked = false;
    if (check5 == undefined) return;
    theInput = document.getElementById(check5); theInput.checked = false;
    if (check6 == undefined) return;
    theInput = document.getElementById(check6); theInput.checked = false;
}

function disableInput(inputName, value)
{
	var theId = document.getElementById(inputName);
	if (theId != null)
	{
		theId.disabled = value;
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


/** codice per visualizzare o nascondere delle colonne di tabelle
*
*
* */
var showMode = 'table-cell';
if (document.all) showMode='block';
function toggleColumn(colName)
{
	var cells=document.getElementsByName(colName);
	for(var j=0;j<cells.length;j++){if(cells[j].style.display=='none')cells[j].style.display=showMode;else cells[j].style.display='none';}
}
function showColumn(colName)
{
	var cells=document.getElementsByName(colName);
	for(var j=0;j<cells.length;j++)cells[j].style.display=showMode;
}
function hideColumn(colName)
{
	var cells=document.getElementsByName(colName);
	for(var j=0;j<cells.length;j++)cells[j].style.display='none';
}

function showVehiclesTrips(whereToShow)
{
  var namelist = "";
  with(document.copVehicles)
  {
    for(var i = 0; i < trips.length; i++)
    {
      if(trips[i].checked)
      {
        namelist += trips[i].value + ",";
      }
    }
  }
  getdata("/jsp/trips/includeList.jsp?vehicles=" + namelist, whereToShow);
  return false;
}