﻿// TripInput.js
// Author: Nicola Zordan 2/12/2007 23:22 Nicola@Zordan.net

// To Improve Speed:
// - Add, Remove, Insert: do not recreate the html for all loocations, 
// -- only for the ones the ones necessary: 1 location add, remove, ...

//TripXML='TripNZ.xml';

    function onLoad0() {
      AddLocation();
      AddLocation();
    };

    function onLoad00()
    {
        // create the map in the 'map' div
        map = new GMap(document.getElementById("map"));
        //map.addControl(new GSmallMapControl());
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
	map.setMapType(G_HYBRID_TYPE);
        map.centerAndZoom(pCentre, nZoom);
        //map.centerAndZoom(pCentre, nZoom);


	//ShowLocations();
	//ShowLocationsPath();
	TripDisplay();

        // Open info window for a marker
        //showMarker("mHele");
    };

    var TripXMLdocument;
    var ShowXMLtripURL='Trip.htm?OpenerTripXML()';
    function ShowXMLtrip() {
	//var txt=document.all.InXMLtxt.value;
	var txt=GetElementId('InXMLtxt').value;
	//alert('show trip'+'\n'+txt);
	// validate xml
	// coonvert text input to xml
        var xml=String2XML(txt);
        if(xml.xml.length==0) {
          alert('ERROR: Invalid XML !');
	  return;
        }; 
	//alert('show trip'+'\n'+xml.xml);
	TripXMLdocument=xml;
	// show trip xml
	window.open(ShowXMLtripURL);
    };

/*
function String2XML (xmlText) {
  var xmlObj=new ActiveXObject('Microsoft.XMLDOM');
  xmlObj.async=false;
  xmlObj.loadXML(xmlText);
  return xmlObj;
};
*/

var Locations=[];
    // Locations[Locations.length]=["id","name","address","tel",longitude,latitude, "<a href='http://www.NicolaZordan.net' target='_blank'>www.NicolaZordan.net</a> 12a", "detailsURL", dateStart, timeStart, dateEnd, timeEnd, imageUrl, pushpin ];	
var TLocations=[];
    // Locations[Locations.length]=[index,"name","address","tel",longitude,latitude, "<a href='http://www.NicolaZordan.net' target='_blank'>www.NicolaZordan.net</a> 12a", "detailsURL", dateStart, timeStart, dateEnd, timeEnd, imageUrl ];	


var Connections=[];
    // Connections[Connections.length]=[index, name, text, (url), (transportType), (company), (id), (dateStart), (timeStart), (dateEnd), (timeEnd), (duration), (description), imageUrl, [points] ];

var TripData=[];
    // TripData=[ name, text, (url), (imageURL), (dateStart), (timeStart), (dateEnd), (timeEnd) ];



function CheckAddress(ix) {
  var lIx=Locations[ix];
  var address=lIx[2];
};

var LocationValidAddress=[];

var LocationValidId='LocationValid_';
function SetLocationValidAddress(ix,txt,status) {
  LocationValidAddress[ix]=txt;
  ShowLocationValidAddress(ix,status); 
};

function ShowLocationValidAddress(ix,status) {
  var show='';
  if (status!=null) {
    if (status==true) {
      show=show+'<font color="green">';
     } else {
      show=show+'<font color="red">';
    };
  };
  show=show+LocationValidAddress[ix];
  if (status!=null) {
   show=show+'</font>';
  };
  //document.all[LocationValidId+ix].innerHTML=show;
  SetElementHTML(LocationValidId+ix,show);
};

function NoLocationMessage(address,lat,lon) {
  //alert("Google maps API cannot Geolocate: \n"+address+"\nfix the address or use latitude longitude\n (lat="+lat+" lon="+lon+")");
  alert("Google maps API cannot Geolocate: \n"+address+"\nfix the address or use latitude longitude");
};


function CheckLocationAddress(ix,showMessage) {
  /*
  var id,name,address,tel,html,lat,lon, description;
  id=TLocations[ix][0];
  name=TLocations[ix][1];
  address=TLocations[ix][2];
  tel=TLocations[ix][3];
  lon=TLocations[ix][5];
  lat=TLocations[ix][4];
  description=TLocations[ix][6];
  if (description==null) description="";
  //-html="<div>"+LocationDescriptionHTML(ix)+"</div>";
  */
  var address=document.TripInput['LocationAddress_'+ix].value;
  if (address==null || address=='') {
    SetLocationValidAddress(ix,'No address',false,showMessage);
    //if (showMessage) NoLocationMessage(address);
    //alert('No address for location '+ix);
    return;
  };

  //LocationAddressValid[ix]='??';
  SetLocationValidAddress(ix,'??',null);
  geocoder.getLatLng(
    address,
    function(point) {
      var where=point;
      if (!point) {
        //LocationAddressValid[ix]='NO';
        SetLocationValidAddress(ix,'No GeoLocation',false,showMessage);
        if (showMessage) NoLocationMessage(address);
        /*
        LocationsUseLatLon[ix]=false;
 	if (lon==null || lat==null) {
	  lat=0;
	  lon=0;
          //alert("Google maps API cannot find: \n"+address+"\nusing lat="+lat+" lon="+lon);
          alert("Google maps API cannot find: \n"+address+"\nfix the address or use latitude longitude\n lat="+lat+" lon="+lon);
         } else {
	};
 	//where=new GPoint(lat,lon);
	//where=new GPoint(lon,lat);
	where=new GLatLng(lat,lon);
        */
       } else { 
        //LocationAddressValid[ix]='OK';
        SetLocationValidAddress(ix,'OK',true,showMessage);
        /*
        LocationsUseLatLon[ix]=true;
	lon=where.x;
    	lat=where.y;
        */
      };
      /*
	Locations[ix][4]=lon;
	Locations[ix][5]=lat;

	LocationsPathPoints[ix]=where;

	ShowLocationsPath(Locations.length);

        var iconName = "iconHome";
        var markerName = getMarkerIdent( id );
        //eval(markerName + "= new GMarker( point, " + iconName + ");");
        //-eval(markerName + "= new GMarker( where, " + iconName + ");");
	//--eval(markerName + "= new GMarker( where );");
        LocationsMarkers[ix]=new GMarker(where);
        //--eval(markerName + '.html = "' + html + '";');
        html="<div>"+LocationDescriptionHTML(ix)+"</div>";
	LocationsMarkers[ix].html=html;
        //--eval("GEvent.addListener(" + markerName +", 'click', function() { " + markerName + ".openInfoWindowHtml(" + markerName + ".html); });");
	//alert(ix+'. '+markerName+'\n'+LocationsMarkers[ix]);
	//+GEvent.addListener(LocationsMarkers[ix], 'click', eval('function() { LocationsMarkers['+ix+'].openInfoWindowHtml(LocationsMarkers['+ix+'].html); }') );
        //+alert('function() { LocationsMarkers['+ix+'].openInfoWindowHtml(LocationsMarkers['+ix+'].html); }'+'\n'+eval('function() { LocationsMarkers['+ix+'].openInfoWindowHtml(LocationsMarkers['+ix+'].html); }'));
	//GEvent.addListener(LocationsMarkers[ix], 'click', eval('function() { LocationsMarkers['+ix+'].openInfoWindowHtml(LocationsMarkers['+ix+'].html); }') );
	eval("GEvent.addListener(LocationsMarkers["+ix+"], 'click', function() { LocationsMarkers["+ix+"].openInfoWindowHtml(LocationsMarkers["+ix+"].html); } );" );
        //--eval("map.addOverlay(" + markerName + ");");
        map.addOverlay(LocationsMarkers[ix]);
       */
    }
  );
}

function CheckAllLocationsAddresses (showMessage) {
  for (var i=0;i<TLocations.length;i++) {
    CheckLocationAddress(i,showMessage);
  };
};



function GoogleMapAddress(ix) {
  /*
  var id,name,address,tel,html,lat,lon, description;
  id=TLocations[ix][0];
  name=TLocations[ix][1];
  address=TLocations[ix][2];
  tel=TLocations[ix][3];
  lon=TLocations[ix][4];
  lat=TLocations[ix][5];
  description=TLocations[ix][6];
  if (description==null) description="";
  //-html="<div>"+LocationDescriptionHTML(ix)+"</div>";
  */
  //var address=document.TripInput['LocationAddress_'+ix].value;
  var address=TLocations[ix][2];
  if (address==null || address=='') {
    //alert('No address for location '+ix);
    alert('No address for location '+(ix+1));
    return;
  };
  var LatLonURL='';
  /*
  if (lat!=null && lat!='' && lon!=null && lon!='') {
  //if (LocationsUseLatLon[ix]==false) {
  //if (lat!=null && LocationsUseLatLon[ix]==false) {
    LatLonURL='http://maps.google.com/maps?&ll='+lat+','+lon+'';
   } else {   
    //var addressMap=address;
    //addressMap=address.replace(/ /g,'%20');
    //LatLonURL='http://maps.google.com/maps?&q='+addressMap;
    LatLonURL='http://maps.google.com/maps?&q='+address;
    //alert(LatLonURL);
  };
  */
  /*
  LatLonURL='http://maps.google.com/maps?&q='+address;
  //LatLonURL='http://maps.google.com/maps?f=q&hl=en&q='+address+'&iwloc=addr';  
  //http://maps.google.com/maps?f=q&hl=en&q=719+38th+ave+#4,+San+Francisco,+CA+94121,+USA
  //http://maps.google.com/maps?f=q&hl=en&q=719+38th+Ave,+San+Francisco,+CA+94121&ie=UTF8&z=15&ll=37.776176,-122.498417&spn=0.016214,0.035834&om=1&iwloc=addr
  LatLonURL=URLnoBlanks(LatLonURL);
  var w=window.open(LatLonURL);
  */
  openGoogleMapAddress(address);  
};

function openGoogleMapAddress(address) {
  var googleURL='http://maps.google.com/maps?&q='+address;
  var url=URLnoBlanks(googleURL);
  var w=window.open(url);
};


function GoogleMapLatLon(ix) {
  // Bristol LatLon= -2.617235,51.470103
  /*
  var id,name,address,tel,html,lat,lon, description;
  id=TLocations[ix][0];
  name=TLocations[ix][1];
  address=TLocations[ix][2];
  tel=TLocations[ix][3];
  lon=TLocations[ix][4];
  lat=TLocations[ix][5];
  description=TLocations[ix][6];
  if (description==null) description="";
  //-html="<div>"+LocationDescriptionHTML(ix)+"</div>";
  */
  //var lat=document.TripInput['LocationLat_'+ix].value;
  //var lon=document.TripInput['LocationLon_'+ix].value;
  var lat=TLocations[ix][4];
  var lon=TLocations[ix][5];
  if (lat==null || lat=='' || lon==null || lon=='') {
    //alert('No address for location '+ix);
    alert('No Latitude and Longitude for location '+(ix+1));
    return;
  };
  var LatLonURL='';
  /*
  if (lat!=null && lat!='' && lon!=null && lon!='') {
  //if (LocationsUseLatLon[ix]==false) {
  //if (lat!=null && LocationsUseLatLon[ix]==false) {
    LatLonURL='http://maps.google.com/maps?&ll='+lat+','+lon+'';
    //LatLonURL='http://maps.google.com/maps?&ll='+lat+','+lon+'';
   } else {   
    //var addressMap=address;
    //addressMap=address.replace(/ /g,'%20');
    //LatLonURL='http://maps.google.com/maps?&q='+addressMap;
    LatLonURL='http://maps.google.com/maps?&q='+address;
    //alert(LatLonURL);
  };
  */
  /*
  //LatLonURL='http://maps.google.com/maps?&ll='+lat+','+lon+'';
  LatLonURL='http://maps.google.com/maps?&ll='+lon+','+lat+'';    
  LatLonURL=URLnoBlanks(LatLonURL);
  var w=window.open(LatLonURL);
  */
  openGoogleMapLatLon (lat,lon);
};

function openGoogleMapLatLon (lat,lon) {
  var googleURL='http://maps.google.com/maps?&ll='+lon+','+lat+'';    
  var url=URLnoBlanks(googleURL);
  var w=window.open(googleURL);
};


function Location2HTML (ix) {
    // Locations[Locations.length]=["id","name","address","tel",longitude,latitude, "<a href='http://www.NicolaZordan.net' target='_blank'>www.NicolaZordan.net</a> 12a", "detailsURL", dateStart, timeStart, dateEnd, timeEnd ];	
  //var l=Locations[ix];
  var l=TLocations[ix];
  //alert(l);
  var c=null;
  var lon='', lat='';
  var html='';
  html+='';
  //var html+='';
  html+='<tr>';

  html+='<td valign="top" align0="left">';
  html+=(ix+1)+'. ';
  html+='<input name="LocationSelector" type="radio" valueIx="'+l[0]+'" value="'+ix+'"/>';
  html+='<input name="LocationIndex_'+ix+'" type="hidden" value0="'+l[0]+'" value="'+ix+'"/>';
  html+='</td>';
  
  html+='<td valign="top" align="left">';
  html+='<input name="LocationName_'+ix+'" type="text" value="'+l[1]+'"/>';
  html+='</td>';

  html+='<td valign="top" align="left">';
  
  html+='<table>';  
  html+='<tr><td align="left"><b>Address</b></td><td align="left">';    
  html+='<input name="LocationAddress_'+ix+'" type="text" value="'+l[2]+'"/>';

  html+='<input type="button" value="map" onClick="openGoogleMapAddress(document.TripInput.LocationAddress_'+ix+'.value)" 0onClick="GoogleMapAddress('+ix+')" />';
  html+='<input type="button" value="check" onClick="CheckLocationAddress('+ix+',true)" />';
  html+=' <span id="'+LocationValidId+ix+'">-</span>';

  html+='</td></tr>';    
  html+='<tr><td align="left">Tel</td><td align="left">';    
  html+='<input name="LocationTel_'+ix+'" type="text" value="'+l[3]+'"/>';
  html+='</td></tr>';    

  //lat=l[5];
  lat=l[4];
  if (lat==null) lat='';
  html+='<tr><td align="left">Latitude</td><td align="left">';    
  //html+='<input name="LocationLat_'+ix+'" type="text" value="'+l[5]+'"/>';
  html+='<input name="LocationLat_'+ix+'" type="text" value="'+lat+'"/>';

  html+='<input type="button" value="map" onClick="openGoogleMapLatLon(document.TripInput.LocationLat_'+ix+'.value,document.TripInput.LocationLon_'+ix+'.value)" 0onClick="GoogleMapLatLon('+ix+')" />';

  html+='</td></tr>';    

  //lon=l[4];
  lon=l[5];
  if (lon==null) lon='';
  html+='<tr><td align="left">Longitude</td><td align="left">';    
  //html+='<input name="LocationLon_'+ix+'" type="text" value="'+l[4]+'"/>';
  html+='<input name="LocationLon_'+ix+'" type="text" value="'+lon+'"/>';

  html+='</td></tr>';    

  html+='<tr><td align="left">Description</td><td align="left">';    
  html+='<input name="LocationDescription_'+ix+'" type="text" value="'+l[6]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Detail URL</td><td align="left">';    
  html+='<input name="LocationURL_'+ix+'" type="text" value="'+l[7]+'"/>';
  html+='<input type="button" value="view" onClick="OpenPageField(\'LocationURL_'+ix+'\')" />';

  html+='</td></tr>';    
  html+='<tr><td align="left">Image URL</td><td align="left">';    
  html+='<input name="LocationImageURL_'+ix+'" type="text" value="'+l[12]+'"/>';
  html+='<input type="button" value="view" onClick="OpenPageField(\'LocationImageURL_'+ix+'\')" />';
  html+='</td></tr>';    
  if (l[8]==null) l[8]='';
  html+='<tr><td align="left">Date Start</td><td align="left">';    
  html+='<input name="LocationDateStart_'+ix+'" type="text" value="'+l[8]+'"/>';
  html+='</td></tr>';    
  if (l[9]==null) l[9]='';
  html+='<tr><td align="left">Time Start</td><td align="left">';    
  html+='<input name="LocationTimeStart_'+ix+'" type="text" value="'+l[9]+'"/>';
  html+='</td></tr>';    
  if (l[10]==null) l[10]='';
  html+='<tr><td align="left">Date End</td><td align="left">';    
  html+='<input name="LocationDateEnd_'+ix+'" type="text" value="'+l[10]+'"/>';
  html+='</td></tr>';    
  if (l[11]==null) l[11]='';
  html+='<tr><td align="left">Time End</td><td align="left">';    
  html+='<input name="LocationTimeEnd_'+ix+'" type="text" value="'+l[11]+'"/>';
  html+='</td></tr>';  
//-alert(l[13]);  
  if (l[13]==null) l[13]='';
  html+='<tr><td align="left">Push Pin</td><td align="left">';    
  html+='<input name="PushPinImage_'+ix+'" type="text" value="'+l[13]+'"/>';
  html+='</td></tr>';    
  html+='</table>';  
  
    // Connections[Connections.length]=[index, name, text, (url), (transportType), (company), (id), (dateStart), (timeStart), (dateEnd), (timeEnd), (duration), (description), [points] ];
  
  // alert(ix+'\n'+Locations.length);  
  //if (ix < Locations.length-1) {
  if (ix < TLocations.length-1) {
  c=Connections[ix];
  //c=Connections[TLocations[ix][0]];
   
   if (c!=null) {
//alert(ix+'\n'+Connections.length+'\n'+c);  
  
  html+='<table>';  
  html+='<tr><th colaspan="2"><i>Connection</i></th></tr>';    
  html+='<tr><td align="left">Name</td><td align="left">';    
  //html+='<input name="ConnectionIndex_'+ix+'" type="hidden" value0="'+c[0]+'" value="'+ix+'"/>';
  html+='<input name="ConnectionIndex_'+ix+'" type="hidden" value0="'+c[0]+'" value="'+(ix+1)+'"/>';
  html+='<input name="ConnectionName_'+ix+'" type="text" value="'+c[1]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Text</td><td align="left">';    
  html+='<input name="ConnectionText_'+ix+'" type="text" value="'+c[2]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Details URL</td><td align="left">';    
  html+='<input name="ConnectionURL_'+ix+'" type="text" value="'+c[3]+'"/>';
  html+='<input type="button" value="view" onClick="OpenPageField(\'ConnectionURL_'+ix+'\')" />';

  html+='</td></tr>';    
  html+='<tr><td align="left">Image URL</td><td align="left">';    
  html+='<input name="ConnectionImageURL_'+ix+'" type="text" value="'+c[13]+'"/>';
  html+='<input type="button" value="view" onClick="OpenPageField(\'ConnectionImageURL_'+ix+'\')" />';
  html+='</td></tr>';    
  html+='<tr><td align="left">Transport Type</td><td align="left">';    
  html+='<input name="ConnectionTransport_'+ix+'" type="text" value="'+c[4]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Company</td><td align="left">';    
  html+='<input name="ConnectionCompany_'+ix+'" type="text" value="'+c[5]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Trasport ID</td><td align="left">';    
  html+='<input name="ConnectionTransportID_'+ix+'" type="text" value="'+c[6]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Date Start</td><td align="left">';    
  html+='<input name="ConnectionDateStart_'+ix+'" type="text" value="'+c[7]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Time Start</td><td align="left">';    
  html+='<input name="ConnectionTimeStart_'+ix+'" type="text" value="'+c[8]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Date End</td><td align="left">';    
  html+='<input name="ConnectionDateEnd_'+ix+'" type="text" value="'+c[9]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Time End</td><td align="left">';    
  html+='<input name="ConnectionTimeEnd_'+ix+'" type="text" value="'+c[10]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Duration</td><td align="left">';    
  html+='<input name="ConnectionDuration_'+ix+'" type="text" value="'+c[11]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Description</td><td align="left">';    
  html+='<input name="ConnectionDescription_'+ix+'" type="text" value="'+c[12]+'"/>';
  html+='</td></tr>';    
  html+='</table>';  
   }
  };
  
  html+='</td>';
  
  html+='</tr>';

  return html;
};

function Locations2HTML () {
  html='';
  html+='<table>';
  /*
  html+='<tr>';
  html+='<td colspan="3" align="center">';
  html+='Index: '+LocationsIndexHTML();
  html+='</td>';
  html+='</tr>';
  */
  html+='<tr>';
  html+='<th>';
  html+='selector';
  html+='</th>';
  html+='<th>';
  html+='Location';
  html+='</th>';
  html+='<th>';
  html+='Details';
  html+='</th>';
  html+='</tr>';
  //for (var i=0;i<Locations.length;i++) {
  for (var i=0;i<TLocations.length;i++) {
    html+=Location2HTML(i);
// SLOW
    window.status='Location: '+i;
  };
  html+='</table>';
  return html;
};

function LocationsShow0 () {
  var html='';
  html+=Locations2HTML();
  document.all.LocationsData.innerHTML=html;
  //document.all.StopsCount.innerHTML=Locations.length;
  document.all.StopsCount.innerHTML=TLocations.length;
};

function LocationsShow2 () {
  var html='';
  html+=Locations2HTML();
  //document.all.LocationsData.innerHTML=html;
  SetElementHTML('LocationsData',html);
  html=LocationsListSelector2HTML();
  //document.all.LocationsSelector.innerHTML=html;
  SetElementHTML('LocationsSelector',html);
  //document.all.StopsCount.innerHTML=Locations.length;
  //document.all.StopsCount.innerHTML=TLocations.length;
  SetElementHTML('StopsCount',TLocations.length);
};

function LocationsShow3 () {
  var html='';
  html+=Locations2HTML();
  //document.all.LocationsData.innerHTML=html;
  SetElementHTML('LocationsData',html);
  html=LocationsListSelector2HTML();
  //document.all.LocationsSelector.innerHTML=html;
  SetElementHTML('LocationsSelector',html);
  //document.all.StopsCount.innerHTML=Locations.length;
  //document.all.StopsCount.innerHTML=TLocations.length;
  SetElementHTML('StopsCount',TLocations.length);
  if (map!=null) ShowTripOnMap(null);
};

function LocationsShow () {
alert('before '+TLocations.length);
//alert('after');

  var html='';
  html+=Locations2HTML();
  //document.all.LocationsData.innerHTML=html;
  SetElementHTML('LocationsData',html);
  html=LocationsListSelector2HTML();
  //document.all.LocationsSelector.innerHTML=html;
  SetElementHTML('LocationsSelector',html);
  //document.all.StopsCount.innerHTML=Locations.length;
  //document.all.StopsCount.innerHTML=TLocations.length;
  SetElementHTML('StopsCount',TLocations.length);
  if (map!=null) ShowTripOnMap();
//alert('before '+TLocations.length);
alert('after');

};


function HTML2TripData0 () {
  //
    // TripData=[ name, text, (url), (imageURL), (dateStart), (timeStart), (dateEnd), (timeEnd) ];
  TripData=['','','','', '','','','' ];
  var name, text, url, imageUrl, dateStart, timeStart, dateEnd, timeEnd;
  name=document.TripInput.TripName.value;
  text=document.TripInput.TripDescription.value;
  url=document.TripInput.TripURL.value;
  imageUrl=document.TripInput.TripImageURL.value;
  dateStart=document.TripInput.TripDateStart.value;
  timeStart=document.TripInput.TripTimeStart.value;
  dateEnd=document.TripInput.TripDateEnd.value;
  timeEnd=document.TripInput.TripTimeEnd.value;
  
  TripData=[ name, text, url, imageUrl, dateStart, timeStart, dateEnd, timeEnd ];
  
  //alert(TripData);

  return TripData;  
}; 


function HTML2Location (ix) {
    // Locations[Locations.length]=["id","name","address","tel",longitude,latitude, "<a href='http://www.NicolaZordan.net' target='_blank'>www.NicolaZordan.net</a> 12a", "detailsURL", dateStart, timeStart, dateEnd, timeEnd ];	
// [[index, name, address, tel, (lat), (long), text, (url), (dateStart), (timeStart), (dateEnd), (timeEnd)]]
  //var l=Locations[ix];
  var id, index,name,address,tel,lon,lat,text,url,dateStart,timeStart,dateEnd,timeEnd,imageUrl,pushpinImage;
  var ixi=ix+1;
//alert(ix);
  id=document.TripInput['LocationIndex_'+ix].value;
  name=document.TripInput['LocationName_'+ix].value;
  address=document.TripInput['LocationAddress_'+ix].value;
  tel=document.TripInput['LocationTel_'+ix].value;
  //lon=document.TripInput['LocationLon_'+ix].value;
  lat=document.TripInput['LocationLat_'+ix].value;
  lon=document.TripInput['LocationLon_'+ix].value;
  text=document.TripInput['LocationDescription_'+ix].value;
  url=document.TripInput['LocationURL_'+ix].value;
  dateStart=document.TripInput['LocationDateStart_'+ix].value;
  timeStart=document.TripInput['LocationTimeStart_'+ix].value;
  dateEnd=document.TripInput['LocationDateEnd_'+ix].value;
  timeEnd=document.TripInput['LocationTimeEnd_'+ix].value;
  imageUrl=document.TripInput['LocationImageURL_'+ix].value;
  pushpinImage=document.TripInput['PushPinImage_'+ix].value;
  //var l=[ ixi, name, address, tel, lat, lon, text, url, dateStart, timeStart, dateEnd, timeEnd];
  //var l=[ markerId(ixi), name, address, tel, lon, lat, text, url, dateStart, timeStart, dateEnd, timeEnd];
  //-var l=[ markerId(ixi), name, address, tel, lon, lat, text, url, dateStart, timeStart, dateEnd, timeEnd];
  var l=[ ixi, name, address, tel, lat, lon, text, url, dateStart, timeStart, dateEnd, timeEnd,imageUrl,pushpinImage];
  //-var l=[ markerId(ixi), name, address, tel, lat, lon, text, url, dateStart, timeStart, dateEnd, timeEnd];
  //alert('Loc '+ix+'\n'+l);
  return l;  
}; 

function HTML2Locations () {
 //alert(TLocations.length);
 //return;
  //
  //alert(TLocations);
  for (var i=0;i<TLocations.length;i++) {
    TLocations[i]=HTML2Location(i);    
  };
  //alert(TLocations);
  //alert(TLocations);
  return TLocations;
}; 

function HTML2Connection (ix) {
  if (document.TripInput['ConnectionIndex_'+ix]==undefined) return null;
    // Connections[Connections.length]=[index, name, text, (url), (transportType), (company), (id), (dateStart), (timeStart), (dateEnd), (timeEnd), (duration), (description), [points] ];
  //var c=Connections[ix];
  var transportId, index,name,text,url,transport,company,type, dateStart,timeStart,dateEnd,timeEnd, duration, description,imageUrl;
  var ixi=ix+1;
  index=document.TripInput['ConnectionIndex_'+ix].value;
  name=document.TripInput['ConnectionName_'+ix].value;
  text=document.TripInput['ConnectionText_'+ix].value;
  company=document.TripInput['ConnectionCompany_'+ix].value;
  transport=document.TripInput['ConnectionTransport_'+ix].value;
  duration=document.TripInput['ConnectionDuration_'+ix].value;
  transportID=document.TripInput['ConnectionTransportID_'+ix].value;
  description=document.TripInput['ConnectionDescription_'+ix].value;
  url=document.TripInput['ConnectionURL_'+ix].value;
  dateStart=document.TripInput['ConnectionDateStart_'+ix].value;
  timeStart=document.TripInput['ConnectionTimeStart_'+ix].value;
  dateEnd=document.TripInput['ConnectionDateEnd_'+ix].value;
  timeEnd=document.TripInput['ConnectionTimeEnd_'+ix].value;
  imageUrl=document.TripInput['ConnectionImageURL_'+ix].value;
    // Connections[Connections.length]=[index, name, text, (url), (transportType), (company), (id), (dateStart), (timeStart), (dateEnd), (timeEnd), (duration), (description), [points] ];
  var c=[ index, name, text, url, transport, company, transportID, dateStart, timeStart, dateEnd, timeEnd, duration, description, imageUrl, ];
  //alert('Conn '+ix+'\n'+c);
  return c;  
}; 

function HTML2Connections () {
  //
  //Connections=[];
  var tConn=[];
  for (var i=0;i<TLocations.length-1;i++) {
    tConn[i]=HTML2Connection(i);    
  };
  Connections=tConn;
  return Connections;
}; 

function HTML2Trip0 () {
  HTML2TripData();
  HTML2Locations();
  HTML2Connections();
  HTML2Areas();
};


function TripData2HTMLset0 () {
  //
    // TripData=[ name, text, (url), (imageURL), (dateStart), (timeStart), (dateEnd), (timeEnd) ];
  //TripData=['','','','', '','','','' ];
  var name, text, url, imageUrl, dateStart, timeStart, dateEnd, timeEnd;
  name=TripData[0];
  text=TripData[1];
  url=TripData[2];
  imageUrl=TripData[3];
  dateStart=TripData[4];
  timeStart=TripData[5];
  dateEnd=TripData[6];
  timeEnd=TripData[7];
  //
  document.TripInput.TripName.value=name;
  document.TripInput.TripDescription.value=text;
  document.TripInput.TripURL.value=url;
  document.TripInput.TripImageURL.value-imageUrl;
  document.TripInput.TripDateStart.value=dateStart;
  document.TripInput.TripTimeStart.value=timeStart;
  document.TripInput.TripDateEnd.value=dateEnd;
  document.TripInput.TripTimeEnd.value=timeEnd;
  //
  return TripData;  
}; 


function GetSelector () {
  var ix=null
  //document.TripInput.LocationIndex[0].checked
  //if (Locations.length<1) {
  if (TLocations.length<1) {
    return ix;
  };
  //if (Locations.length<2) {
  if (TLocations.length<2) {
    if (document.TripInput.LocationSelector.checked==true) {
      ix=0;
    };
    //alert(ix);
    return ix;
  };
  for (var i=0;i<TLocations.length;i++) {
    if (document.TripInput.LocationSelector[i].checked==true) {
      ix=i;
      break;
    };
  };
  return ix;
};

function GetSelectorEdit () {
  var ix=null
  //document.TripInput.LocationIndex[0].checked
  //if (Locations.length<1) {
  if (TLocations.length<1) {
    return ix;
  };
  //if (Locations.length<2) {
  if (TLocations.length<2) {
    if (document.TripInput.LocationSelectorEditRadio.checked==true) {
      ix=0;
    };
    //alert(ix);
    return ix;
  };
  for (var i=0;i<TLocations.length+1;i++) {
    if (document.TripInput.LocationSelectorEditRadio[i].checked==true) {
      ix=i;
      break;
    };
  };
  return ix;
};


var SavedConnections=null;
var ShowConnectionsInInput=false;
function ShowHideConnections() {
  HTML2Trip();
  var oldShowConnections=ShowConnectionsInInput;
  //ShowConnectionsInInput=document.all.ConnectionsInput.checked;
  ShowConnectionsInInput=GetElementId('ConnectionsInput').checked;
  ShowHideConnectionStatus();
  if (!ShowConnectionsInInput) {
    SavedConnections=Connections;
    Connections=[];
   } else {
    //Connections=SavedConnections;
    var c;
    Connections=[];
    for (var i=0;i<TLocations.length-1;i++) {
      c=NewConnection;
      c[0]=TLocations[i][0];
      if (SavedConnections!=null && SavedConnections[i]!=null) {
        c=SavedConnections[i];
      };
      Connections[i]=c;
    };
  }; 
  LocationsShow();
  
};


function ShowHideConnectionStatus0 () {
  if (ShowConnectionsInInput) {
    document.all.ConnectionsInput.checked=true;   
   } else {
    document.all.ConnectionsInput.checked=false;   
  };
};

//var EmptyTripXML='/EmptyTripXML.xml';
var EmptyTripXML='./EmptyTripXML.xml';
function CreateEmptyTrip () {
  var name=document.TripInput.TripName.value;
  if (EmptyTripXML==null) {
      AddLocationEdit();
      DoZoom=true;
      AddLocationEdit();
      SelectedLocationEdit(0);
  };
  if (EmptyTripXML!=null) {
      TripXMLdocument=null;
      GetTripXML(EmptyTripXML);
//alert(TripXMLdocument.xml);
      //GetTripXML('Garda.xml');
      var xmlTxt=XML2string(TripXMLdocument);
//alert(xmlTxt);
      LoadTripXML2InputEdit(null,xmlTxt);
  };
  document.TripInput.TripName.value=name;
};

function ClearTrip() {
  TLocations=[];
  Connections=[];
  CreateEmptyTrip();
};



function FocusOnLocation(ix) {
  if (ix <0 || ix>TLocations.length) return null;
  var field=document.TripInput['LocationName_'+ix];
  //alert(field)
  //window.status='location added';
  //-field.focus();
  return field;
};


function LocationsIndexHTML () {
  var html='';
  html+='';
  for (var i=0;i<TLocations.length;i++) {
    html+='<input type="button" value="'+(i+1)+'" onClick="FocusOnLocation('+i+')" />'
  };
  return html;
};


// SLOW ---///--- var LocationHTML=[]; // Store HTML for each Location, without recreating it every time

//var NewLocation=[0,'name0','','',0,0,'', '', '','','','' ];
//var NewLocation=[0,'name0','','', '','' ,'', '', '','','','' ];
var NewLocation=[0,'','','','','', '', '', '', '', '', '', '', ''];	
var NewConnection=[0,'','','','','','', '','','','', '', '','' ];
function AddLocation (loc,con) {
  HTML2Trip();
  //var newIndex=Locations.length, newConn=0, Index=newIndex+1;
  var newIndex=TLocations.length, newConn=0, index=newIndex+1;
  //Locations[newIndex]=[newIndex,'name'+(Index),'','',0,0,'', '', '','','','' ];
  //alert(TLocations);
  //TLocations[newIndex]=[index,'name'+(index),'','',0,0,'', '', '','','','' ];
  var l=loc;
  if (l==null) l=NewLocation.slice(0);
  TLocations[newIndex]=l;
  TLocations[newIndex][0]=index;
  if (TLocations[newIndex][1]==null || TLocations[newIndex][1]=='')
    TLocations[newIndex][1]='name '+(index);
  //alert(TLocations);
  if (newIndex>0 && ShowConnectionsInInput) {
    newConn=newIndex-1;
    // Connections[Connections.length]=[index, name, text, (url), (transportType), (company), (id), (dateStart), (timeStart), (dateEnd), (timeEnd), (duration), (description), [points] ];
    //-Connections[newConn]=['location_'+newConn,'','','','','','', '','','','', '', '' ];
    //--Connections[newConn]=[index,'','','','','','', '','','','', '', '' ];
    //Connections[newConn]=[index,'','','','','','', '','','','', '', '' ];
    //Connections[newConn]=[index,'','','','','','', '','','','', '', '' ];
    var c=con;
    if (c==null) c=NewConnection.slice(0);
    Connections[newConn]=c;
    Connections[newConn][0]=index;
  };
  LocationsShow();
  FocusOnLocation(TLocations.length-1);
};

function AddLocationEdit () {
  AddLocation();
  SelectedLocationEdit(TLocations.length-1);
};

function InsertLocation(ix,loc, con) {
//alert('insert'+'\n'+ix+'\n'+loc+'\n'+con);
  if (ix>=TLocations.length) {
    AddLocation(loc,con);
    return;
  };
  HTML2Trip();
  //var ix=0;
  //var Location2Remove=GetSelector();
  var LocationIndex;
  if (ix==null) {
    LocationIndex=GetSelector();  
   } else {
    LocationIndex=ix;  
  };
  //alert(Location2Remove+'\n');
  if (LocationIndex==null) {
    alert('Select the location to remove');
    return;
  };
  var locTmp=TLocations;
  var connTmp=Connections;
  TLocations=[];
  Connections=[];
  var ii=0;
  for (var i=0;i<locTmp.length;i++) {
    if (i==LocationIndex) {
      var l=loc;
      if (l==null) l=NewLocation.slice(0);
      TLocations[ii]=l;
      TLocations[ii][0]=ii+1;
      if (TLocations[ii][1]==null || TLocations[ii][1]=='')
        TLocations[ii][1]='name'+(locTmp.length+1);
      if (i<locTmp.length-1 && ShowConnectionsInInput) {
        var c=con;
        if (c==null) c=NewConnection.slice(0);
        Connections[ii]=c;
        //alert(ix+'\n'+locTmp.length+'\n'+Connections.length+'\n'+Connections[ix]);
        Connections[ii][0]=TLocations[ii][0];
      };
      ii++;
    };
    //ix=TLocations.length;
    TLocations[ii]=locTmp[i];
    TLocations[ii][0]=ix+1;
    if (i<locTmp.length-1 && ShowConnectionsInInput) {
      Connections[ii]=connTmp[i];
      //alert(ix+'\n'+locTmp.length+'\n'+Connections.length+'\n'+Connections[ix]);
      Connections[ii][0]=TLocations[ii][0];
    };
    ii++;
  };
  LocationsShow();
  FocusOnLocation(ix);
};

function InsertEditLocation(ix) {
  InsertLocation(ix);
  SelectedLocationEdit(ix);
};

function ReverseTrip() {
  HTML2Trip();
/*
  var LocationIndex;
  if (ix==null) {
    LocationIndex=GetSelector();  
   } else {
    LocationIndex=ix;  
  };
*/
  var locTmp=TLocations;
  var connTmp=Connections;
  TLocations=[];
  Connections=[];
  var ix=0,ii=0;
  for (var i=locTmp.length-1;i>=0;i--) {    
    ix=ii;
    TLocations[ii]=locTmp[i];
    TLocations[ii][0]=ix+1;
    if (ii<locTmp.length-1 && ShowConnectionsInInput) {
      //if (i<locTmp.length-1 && ShowConnectionsInInput) {
      Connections[ii]=connTmp[i];
      if (Connections[ii]==null) Connections[ii]=NewConnection.slice(0);
      Connections[ii][0]=TLocations[ii][0];
    };
    ii++;
  };
  LocationsShow();
  //ShowTripOnMap();
  //FocusOnLocation(ix);
  SelectedLocationEdit(0);
};



function RemoveLocation (ix) {
  HTML2Trip();
  //var ix=0;
  //var Location2Remove=GetSelector();
  var LocationIndex;
  if (ix==null) {
    LocationIndex=GetSelector();  
   } else {
    LocationIndex=ix;  
  };
  //alert(Location2Remove+'\n');
  if (LocationIndex==null) {
    alert('Select the location to remove');
    return;
  };
  var locTmp=TLocations;
  var connTmp=Connections;
  TLocations=[];
  Connections=[];
  for (var i=0;i<locTmp.length;i++) {
    if (i==LocationIndex) continue;
    ix=TLocations.length;
    TLocations[ix]=locTmp[i];
    TLocations[ix][0]=ix+1;
    if (i<locTmp.length-1 && ShowConnectionsInInput) {
      Connections[ix]=connTmp[i];
      //alert(ix+'\n'+locTmp.length+'\n'+Connections.length+'\n'+Connections[ix]);
      Connections[ix][0]=TLocations[ix][0];
    };
  };
  LocationsShow();
  FocusOnLocation(ix);
};

function RemoveLocationEdit (ix) {
  if (TLocations.length<=2) {
    alert('A trip cannot have less than 2 locations\nYou can edit the locations');
    return;
  };
  RemoveLocation(ix);
  var ixs=ix;
  if (TLocations.length<1) return;
  if (ixs>TLocations.length-1) ixs=TLocations.length-1;
  SelectedLocationEdit(ixs);
};


function SwapLocations(from,to) {
  var lTmp=TLocations[to];
  TLocations[to]=TLocations[from];
  TLocations[to][0]=(from+1);
  TLocations[from]=lTmp;
  TLocations[from][0]=(to+1);
  // Connections
  if (ShowConnectionsInInput && Connections!=null && Connections.length>0) {
    var cTmp=Connections[to];
    if (to<(TLocations.length-1)) {
      if (from<(TLocations.length-1)) {
        Connections[to]=Connections[from];
       } else { 
        Connections[to]=NewConnection;
      };
        Connections[to][0]=(from+1);
     } else {
      //Connections[to]=null;
    };
    if (from<(TLocations.length-1)) {
      if (to<(TLocations.length-1)) {
        Connections[from]=cTmp;
       } else { 
        Connections[from]=NewConnection;
      };
        Connections[from][0]=(to+1);
     } else {
      //Connections[from]=null;
    };
  };
};


function MoveUpLocation(ix) {
  //alert('Not yet implemented');
  //return;
  HTML2Trip();
  //var ix=0;
  //var LocationIndex=GetSelector();
  var LocationIndex;
  if (ix==null) {
    LocationIndex=GetSelector();  
   } else {
    LocationIndex=ix;  
  };
  //alert(LocationIndex+'\n');
  if (LocationIndex==null) {
    alert('Select the location to move up');
    return;
  };
  if (LocationIndex==0) {
    alert('Cannot move before first');
    return;
  };
  var from=LocationIndex;
  var to=LocationIndex-1;
  SwapLocations(from,to);
 
  LocationsShow();
};




function MoveDownLocation(ix) {
  //alert('Not yet implemented');
  //return;
  HTML2Trip();
  //var ix=0;
  //var LocationIndex=GetSelector();
  var LocationIndex;
  if (ix==null) {
    LocationIndex=GetSelector();  
   } else {
    LocationIndex=ix;  
  };
  //alert(LocationIndex+'\n');
  if (LocationIndex==null) {
    alert('Select the location to move down');
    return;
  };
  if (LocationIndex>=(TLocations.length-1)) {
    alert('Cannot move after last');
    return;
  };
  var from=LocationIndex;
  var to=LocationIndex+1;
  SwapLocations(from,to);
 
  LocationsShow();
};


function ShowLocations () {
  HTML2Locations();
  LocationsShow();
};


function GoogleMapLocationIx(ix) {
  var l=TLocations[ix];
  if (l==null) {
    alert('No location '+ix);
    return;
  };
  var latI=4, lonI=5;
  if ((l[latI]!=null && l[latI]!='' ) && (l[lonI]!=null && l[lonI]!='')) {
  //if (l[latI]!=null && l[lonI]!=null) {
    GoogleMapLatLon(ix);
   } else {
    GoogleMapAddress(ix);
  };
};


function LocationsListSelector2HTML () {
  var html='';
  var LocationName='';
  //html+='';
  var showingIx=null;
  var editIx=document.TripInput.LocationSelector_Edit;
  if (editIx!=null) {
    showingIx=editIx.value;
  };
  html+='<ol>';
  for (var i=0;i<TLocations.length;i++) {
    LocationName=TLocations[i][1];
// SLOW
    window.status='Locations Selector: '+i;
    html+='<li>';
    //html+='<a href="SelectedLocation('+i+')">'+LocationName+'</a>';
    
    //html+='<input type="button" value="'+(i+1)+'" onClick="SelectedLocation('+i+')" />';
    //- html+='<input type="button" value="i" onClick="SelectedLocationEdit('+i+')" style="{font-family: webdings}" />';
    html+='<input type="radio" name="LocationSelectorEditRadio" value="'+i+'" onClick="SelectedLocationEdit('+i+')" style="{font-family: webdings}" ';
    if (i==showingIx) html+=' checked ';
    html+=' />';

    html+='<input type="button" value="þ" value0="û" onClick="GoogleMapLocationIx('+i+')" style="{font-family: webdings}" />';

    html+=' ';
    
    html+='<input type="button" value="4" onClick="InsertEditLocation('+i+')" style="{font-family: webdings}" />';
    //html+='<input type="button" value="q" onClick="ChangeLocation('+i+')" style="{font-family: webdings}" />';

    html+='<input type="button" value="5" onClick="MoveUpLocation('+i+')" style="{font-family: webdings}" ';
    if (i==0) html+=' disabled ';
    html+=' />';
    html+='<input type="button" value="6" onClick="MoveDownLocation('+i+')" style="{font-family: webdings}" ';
    if (i==TLocations.length-1) html+=' disabled ';
    html+=' />';
    
    //html+='<input type="button" value="r" onClick="RemoveLocationEdit('+i+')" style="{font-family: webdings}" />';
    html+='<input type="button" value="r" onClick="RemoveLocationEdit('+i+')" style="{font-family: webdings}" '
    //if (i<2) html+=' disabled ';
    html+=' />';
    
    html+=' ';
    
    //html+='<a href="SelectedLocation('+i+')">'+LocationName+'</a>';
    if (map!=null) {
      //-html+='<a href="javascript:SelectedLocationEdit('+i+');showLocationDetails('+i+')">';
      html+='<a href="javascript:SelectedLocationEdit('+i+');showLocationDetails('+i+')" ';
      //-if (LocationsPathPoints[i]==InvalidGeoPoint) 
      //-  html+='<font color="red">';
      if (LocationsPathPoints[i]==InvalidGeoPoint) 
        html+='style="color:red"';
      html+=' >';
    };
    html+=LocationName;
    if (map!=null) {
      //-if (LocationsPathPoints[i]==InvalidGeoPoint) 
      //-  html+='</font>';
      html+='</a>';
    };

/*
    if (map!=null) {
      // TODO: Highlight imvalid points
      if (LocationsPathPoints[i]==InvalidGeoPoint) 
        html+=' <font face="Webdings" color="red">x</font> ';
    };
*/    
    if (Connections!=null && Connections.length>0 && Connections[i]!=null && Connections[i][1]!=null && Connections[i][1]!='') {
      html+=' ~ ';
      html+=Connections[i][1];
    };

    
    html+='</li>';
  };
  // Add Location
    html+='<li>';
    html+='<input type="radio" name="LocationSelectorEditRadio" value="'+TLocations.length+'" onClick="SelectedLocationEdit('+TLocations.length+')" style="{font-family: webdings}" ';
    if (showingIx>=TLocations.length) html+=' checked ';
    html+=' />';
    html+='<input type="button" value="Add" onclick="AddLocationEdit()" />';
    html+='</li>';


  html+='</ol>';
  return html;
}

function SelectedLocation(ix) {
 //alert('Selected location:'+ix+'\n'+TLocations[ix]);
 document.TripInput.LocationSelector[ix].checked=true;
 document.TripInput.LocationSelector[ix].focus();
 //document.all.LocationEdit.innerHTML=Location2HTML4Edit(ix);
 //document.TripInput.LocationSelector_Edit.focus(); 
 //document.TripInput.LocationName_Edit.focus(); 
};

function SelectedLocationEdit(ix) {
 //alert('Selected location:'+ix+'\n'+TLocations[ix]);
 //document.TripInput.LocationSelector[ix].checked=true;
 //document.TripInput.LocationSelector[ix].focus();
 //-document.all.LocationEdit.innerHTML=Location2HTML4Edit(ix);
 SetElementHTML('LocationEdit',Location2HTML4Edit(ix));
 //alert(document.TripInput.LocationSelectorEditRadio.length);
 if (document.TripInput.LocationSelectorEditRadio.length>=ix) {
   document.TripInput.LocationSelectorEditRadio[ix].checked=true;
 };
 //-LocationsShow();
 //document.TripInput.LocationSelector_Edit.focus(); 
 //if (document.TripInput.LocationName_Edit==null) return;
//alert(document.TripInput.LocationName_Edit.focus);
 //if (document.TripInput.LocationName_Edit!=null && 
 //    document.TripInput.LocationName_Edit.style.visibility=='visible')  
   document.TripInput.LocationName_Edit.focus(); 
};


function Location2HTML4Edit (ixE) {
    // Locations[Locations.length]=["id","name","address","tel",longitude,latitude, "<a href='http://www.NicolaZordan.net' target='_blank'>www.NicolaZordan.net</a> 12a", "detailsURL", dateStart, timeStart, dateEnd, timeEnd ];	
  //var l=Locations[ix];
  var l=TLocations[ixE];
  //if (l==null) return '';
  if (l==null) l=NewLocation.slice(0);
  var ix='Edit';
  //alert(l);
  var c=null;
  var lon='', lat='';
  var html='';
  html+='';
  //var html+='';
  html+='<table>';
  
  html+='<tr>';
  html+='<th>';
  html+='edit';
  html+='</th>';
  html+='<th>';
  html+='Location';
  html+='</th>';

  //html+='<th>';
  //html+='Details';
  //html+='</th>';
  if (map==null) {
    html+='<th>';
    html+='Details';
    html+='</th>';
  };
  html+='</tr>';

  html+='<tr>';

  html+='<td valign="top" align0="left">';
  //html+=(ixE+1)+'. ';
  //html+='<input name="LocationSelector_Edit" type="radio" valueIx="'+l[0]+'" value="'+ixE+'"/>';
  //html+='<input name="LocationSelector_Edit" type="checkbox" valueIx="'+l[0]+'" value="'+ixE+'"/>';

  html+='<select name="LocationSelector_Edit" >';
  //html+='<option value="">new</option>';
  for (var i=0;i<TLocations.length+1;i++) {
  //for (var i=0;i<TLocations.length;i++) {
    html+='<option value="'+i+'" ';
    if (i==ixE) {
      html+=' selected ';
    };
    html+=' >';
    if (i<TLocations.length)
      html+=(i+1)
     else
      html+='*';
    html+='</option>';
  };
  html+='</select>';
  
  html+='<input name="LocationIndex_'+ix+'" type="hidden" value0="'+l[0]+'" value="'+ixE+'"/>';
  html+='</td>';
  
  html+='<td valign="top" align="left">';
  html+='<input name="LocationName_'+ix+'" type="text" value="'+l[1]+'"/>';

  //html+='<br/>';
  if (map==null) {
      html+='<br/>';
  };
  html+='<input type="button" value="save" onClick="SaveEditLocation()" />';
  //html+='<input type="button" value="add"  onClick="AddEditLocation()" />';
  //html+='<input type="button" value="add" onClick="SaveAddEditLocation()" />';
  html+='<input type="button" value="insert" onClick="SaveInsertEditLocation()" />';
  
  html+='</td>';

  if (map!=null) {
      //html+='</tr><tr>';
      html+='</tr></table><table><tr>';
  };
 
  
  html+='<td valign="top" align="left">';
  
  html+='<table>';  
  html+='<tr><td align="left"><b>Address</b></td><td align="left">';    
  html+='<input name="LocationAddress_'+ix+'" type="text" value="'+l[2]+'"/>';

  html+='<input type="button" value="map" onClick="openGoogleMapAddress(document.TripInput.LocationAddress_'+ix+'.value)" 0onClick="GoogleMapAddress(\''+ix+'\')" />';
  html+='<input type="button" value="check" onClick="CheckLocationAddress(\''+ix+'\',true)" />';
  html+=' <span id="'+LocationValidId+ix+'">-</span>';

  //lat=l[5];
  lat=l[4];
  if (lat==null) lat='';
  lon=l[5];
  if (lon==null) lon='';

  html+='<tr id="LatLonShowHide"';
  if (lat!='' || lon!='') html+=' style="display: none" ';    
  html+='>';
  html+='<td></td><td><small>';
  html+='<a href="javaScript:{HideShowDiv(\'Lat\');HideShowDiv(\'Lon\');HideShowDiv(\'LatLonShowHide\');undefined};">Enter Latitude and Longitude</a>';
  html+='</small></td>';
  html+='</tr>';

  html+='</tr>';

  //-html+='<tr><td align="left">Latitude</td><td align="left">';    
  html+='<tr id="Lat"';
  if (lat=='' && lon=='') html+=' style="display: none" ';    
  html+='>';    
  html+='<td align="left">Latitude</td><td align="left">';    
  //html+='<input name="LocationLat_'+ix+'" type="text" value="'+l[5]+'"/>';
  html+='<input name="LocationLat_'+ix+'" type="text" value="'+lat+'"/>';

  html+='<input type="button" value="map" onClick="openGoogleMapLatLon(document.TripInput.LocationLat_'+ix+'.value,document.TripInput.LocationLon_'+ix+'.value)" 0onClick="GoogleMapLatLon(\''+ix+'\')" />';

  html+='</td></tr>';    

  //lon=l[4];
  //lon=l[5];
  //if (lon==null) lon='';
  //-html+='<tr><td align="left">Longitude</td><td align="left">';    
  html+='<tr id="Lon"';    
  if (lat=='' && lon=='') html+=' style="display: none" ';    
  html+='>';    
  html+='<td align="left">Longitude</td><td align="left">';    
  //html+='<input name="LocationLon_'+ix+'" type="text" value="'+l[4]+'"/>';
  html+='<input name="LocationLon_'+ix+'" type="text" value="'+lon+'"/>';

  if (map!=null) {
    html+='<input type="button" value="From Map" onClick="ChangeLocationEditPointFromMap()" />';
  };
  html+='<input type="button" value="clear" onClick="document.TripInput.LocationLon_'+ix+'.value=\'\';document.TripInput.LocationLat_'+ix+'.value=\'\';" />';

  html+='</td></tr>';    

  //html+='<tr><td align="left">Description</td><td align="left">';    
  //html+='<input name="LocationDescription_'+ix+'" type="text" value="'+l[6]+'"/>';
  //html+='</td></tr>';    
///
  html+='<tr><td align="left" colspan="2">Description<br/>';
  html+='<textarea name="LocationDescription_'+ix+'" rows="5" cols="44">'+l[6]+'</textarea>';
  html+='</td></tr>';    

  html+='</td></tr>';    
  html+='<tr><td align="left">Tel</td><td align="left">';    
  html+='<input name="LocationTel_'+ix+'" type="text" value="'+l[3]+'"/>';
  html+='</td></tr>';    


  html+='<tr><td align="left">Detail URL</td><td align="left">';    
  html+='<input name="LocationURL_'+ix+'" type="text" value="'+l[7]+'"/>';
  html+='<input type="button" value="view" onClick="OpenPageField(\'LocationURL_'+ix+'\')" />';
  html+='</td></tr>';    
  html+='<tr><td align="left">Image URL</td><td align="left">';    
  html+='<input name="LocationImageURL_'+ix+'" type="text" value="'+l[12]+'"/>';
  html+='<input type="button" value="view" onClick="OpenPageField(\'LocationImageURL_'+ix+'\')" />';
  html+='</td></tr>';    
  html+='<tr><td align="left">Date Start</td><td align="left">';    
  html+='<input name="LocationDateStart_'+ix+'" type="text" value="'+l[8]+'"/>';
  html+='<button onClick="CallNAZCalendarFocus(document.TripInput[\'LocationDateStart_'+ix+'\'])"><img src="CalendarDate.gif" alt="Pick a date from the calendar" /></button>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Time Start</td><td align="left">';    
  html+='<input name="LocationTimeStart_'+ix+'" type="text" value="'+l[9]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Date End</td><td align="left">';    
  html+='<input name="LocationDateEnd_'+ix+'" type="text" value="'+l[10]+'"/>';
  html+='<button onClick="CallNAZCalendarFocus(document.TripInput[\'LocationDateEnd_'+ix+'\'])"><img src="CalendarDate.gif" alt="Pick a date from the calendar" /></button>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Time End</td><td align="left">';    
  html+='<input name="LocationTimeEnd_'+ix+'" type="text" value="'+l[11]+'"/>';
  html+='</td></tr>';    

  html+='<tr><td align="left">Push Pin</td><td align="left">';    
  html+='<input name="PushPinImage_'+ix+'" type="text" value="'+l[13]+'"/>';
  html+='</td></tr>';    

  html+='</table>';  
  
    // Connections[Connections.length]=[index, name, text, (url), (transportType), (company), (id), (dateStart), (timeStart), (dateEnd), (timeEnd), (duration), (description), [points] ];
  
  // alert(ix+'\n'+Locations.length);  
  //if (ix < Locations.length-1) {
  if (ixE < TLocations.length-1) {
  c=Connections[ixE];
  //c=Connections[TLocations[ix][0]];
   
   if (c!=null) {
//alert(ix+'\n'+Connections.length+'\n'+c);  
  
  html+='<table>';  
  html+='<tr><th colaspan="2"><i>Connection</i></th></tr>';    
  html+='<tr><td align="left">Name</td><td align="left">';    
  //html+='<input name="ConnectionIndex_'+ix+'" type="hidden" value0="'+c[0]+'" value="'+ix+'"/>';
  html+='<input name="ConnectionIndex_'+ix+'" type="hidden" value0="'+c[0]+'" value="'+(ix+1)+'"/>';
  html+='<input name="ConnectionName_'+ix+'" type="text" value="'+c[1]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Text</td><td align="left">';    
  html+='<input name="ConnectionText_'+ix+'" type="text" value="'+c[2]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Details URL</td><td align="left">';    
  html+='<input name="ConnectionURL_'+ix+'" type="text" value="'+c[3]+'"/>';
  html+='<input type="button" value="view" onClick="OpenPageField(\'ConnectionURL_'+ix+'\')" />';
  html+='</td></tr>';    
  html+='<tr><td align="left">Image URL</td><td align="left">';    
  html+='<input name="ConnectionImageURL_'+ix+'" type="text" value="'+c[13]+'"/>';
  html+='<input type="button" value="view" onClick="OpenPageField(\'ConnectionImageURL_'+ix+'\')" />';
  html+='</td></tr>';    
  html+='<tr><td align="left">Transport Type</td><td align="left">';    
  html+='<input name="ConnectionTransport_'+ix+'" type="text" value="'+c[4]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Company</td><td align="left">';    
  html+='<input name="ConnectionCompany_'+ix+'" type="text" value="'+c[5]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Trasport ID</td><td align="left">';    
  html+='<input name="ConnectionTransportID_'+ix+'" type="text" value="'+c[6]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Date Start</td><td align="left">';    
  html+='<input name="ConnectionDateStart_'+ix+'" type="text" value="'+c[7]+'"/>';
  html+='<button onClick="CallNAZCalendarFocus(document.TripInput[\'ConnectionDateStart_'+ix+'\'])"><img src="CalendarDate.gif" alt="Pick a date from the calendar" /></button>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Time Start</td><td align="left">';    
  html+='<input name="ConnectionTimeStart_'+ix+'" type="text" value="'+c[8]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Date End</td><td align="left">';    
  html+='<input name="ConnectionDateEnd_'+ix+'" type="text" value="'+c[9]+'"/>';
  html+='<button onClick="CallNAZCalendarFocus(document.TripInput[\'ConnectionDateEnd_'+ix+'\'])"><img src="CalendarDate.gif" alt="Pick a date from the calendar" /></button>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Time End</td><td align="left">';    
  html+='<input name="ConnectionTimeEnd_'+ix+'" type="text" value="'+c[10]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Duration</td><td align="left">';    
  html+='<input name="ConnectionDuration_'+ix+'" type="text" value="'+c[11]+'"/>';
  html+='</td></tr>';    
  //html+='<tr><td align="left">Description</td><td align="left">';    
  //html+='<input name="ConnectionDescription_'+ix+'" type="text" value="'+c[12]+'"/>';
  //html+='</td></tr>';    
  html+='<tr><td align="left" colspan="2">Description<br/>';
  html+='<textarea name="ConnectionDescription_'+ix+'" rows="5" cols="44">'+c[12]+'</textarea>';
  html+='</td></tr>';    
  html+='</table>';  
   }
  };
  
  html+='</td>';
  
  html+='</tr>';
  
  html+='</table>';

  return html;
};


function SaveEditLocation () {
  var ixN=document.TripInput.LocationSelector_Edit.selectedIndex;
  var ix='Edit';
  var connectionsInput=ShowConnectionsInInput;
  var l=HTML2Location(ix);
  var c=null;
  if (connectionsInput) {
    c=HTML2Connection(ix);
  };
  TLocations[ixN]=l;
  if (connectionsInput) {
    Connections[ixN]=c;
  };  
  LocationsShow();
  if (ixN>=TLocations.length-1) 
    SelectedLocationEdit(ixN);
};

function SaveAddEditLocation() {
  //var ixN=document.TripInput.LocationSelector_Edit.selectedIndex;
  var ixN=TLocations.length;
  var ix='Edit';
  var connectionsInput=ShowConnectionsInInput;
  var l=HTML2Location(ix);
  l[0]=ixN+1;
  var c=null;
  if (connectionsInput) {
    c=HTML2Connection(ix);
    c[0]=l[0];
  };
  //AddLocationEdit();
  AddLocation();
  TLocations[ixN]=l;
  if (connectionsInput) {
    Connections[ixN]=c;
  };  
  LocationsShow();
  SelectedLocationEdit(ixN);
};

function SaveInsertEditLocation() {
  var ixN=document.TripInput.LocationSelector_Edit.selectedIndex;
  //var ixN=GetSelectorEdit();
  //var ixN=TLocations.length;
  var ix='Edit';
  var connectionsInput=ShowConnectionsInInput;
  var l=HTML2Location(ix);
  l[0]=ixN+1;
  var c=null;
  if (connectionsInput) {
    c=HTML2Connection(ix);
    c[0]=l[0];
  };
/*
  InsertLocation(ixN);
  TLocations[ixN]=l;
  if (connectionsInput) {
    Connections[ixN]=c;
  };  
  LocationsShow();
*/
  InsertLocation(ixN,l,c);
  SelectedLocationEdit(ixN+1);
};

//---

var TripXMLdocument;




function Trip2XMLtext() {
  var html='';
  html+='';
  
    // Locations[Locations.length]=["id","name","address","tel",longitude,latitude, "<a href='http://www.NicolaZordan.net' target='_blank'>www.NicolaZordan.net</a> 12a", "detailsURL", dateStart, timeStart, dateEnd, timeEnd ];	
    // TLocations[Locations.length]=[index,"name","address","tel",longitude,latitude, "<a href='http://www.NicolaZordan.net' target='_blank'>www.NicolaZordan.net</a> 12a", "detailsURL", dateStart, timeStart, dateEnd, timeEnd ];	
    // Connections[Connections.length]=[index, name, text, (url), (transportType), (company), (id), (dateStart), (timeStart), (dateEnd), (timeEnd), (duration), (description), [points] ];
    // TripData=[ name, text, (url), (imageURL), (dateStart), (timeStart), (dateEnd), (timeEnd) ];

  var TripName, TripText,TripURL,TripImageURL,TripDateStart,TripTimeStart,TripDateEnd,TripTimeEnd;
  TripName=TripData[0];
  TripText=TripData[1];
  TripURL=TripData[2];
  TripImageURL=TripData[3];
  TripDateStart=TripData[4];
  TripTimeStart=TripData[5];
  TripDateEnd=TripData[6];
  TripTimeEnd=TripData[7];  

  html+='<?xml version="1.0" encoding="UTF-8"?>'+'\n';
//<!-- <?xml-stylesheet type="text/xsl" href="Trip2Locations.xsl" ?> -->
//<!-- <?xml-stylesheet type="text/xsl" href="Trip2Connections.xsl" ?> -->
//<!-- <?xml-stylesheet type="text/xsl" href="Trip2Trip.xsl" ?>  -->
  html+='<!-- Author: Nicola Zordan 2/8/2007 11:11 Nicola@Zordan.net -->'+'\n';
  html+='<Trip Name="'+TripName+'" dateStart="'+TripDateStart+'" timeStart="'+TripTimeStart+'" dateEnd="'+TripDateEnd+'" timeEnd="'+TripTimeEnd+'">'+'\n';
  html+='  <Description><![CDATA['+TripText+']]></Description>'+'\n';
  html+='  <DetailsURL><![CDATA['+TripURL+']]></DetailsURL>'+'\n';
  html+='  <ImageURL><![CDATA['+TripImageURL+']]></ImageURL>'+'\n';
  html+='  <Locations>'+'\n';
  var l, lIx,lName,lAddress,lTel,lLon,lLat,lText,lUrl,lDateStart, lTimeStart,lDateEnd, lTimeEnd,imageUrl,pushpinImage;
  for (var i=0;i<TLocations.length;i++) {
    l=TLocations[i];
    lIx=l[0];
    lName=l[1];
    lAddress=l[2];
    lTel=l[3];
    //lLon=l[4];
    //lLat=l[5];
    lLon=l[5];
    lLat=l[4];
//alert(lLon+'\n'+lLat);
    //lLat=l[4];
    //lLon=l[5];
    lText=l[6];
    lUrl=l[7];
    lDateStart=l[8];
    lTimeStart=l[9];
    lDateEnd=l[10];
    lTimeEnd=l[11];
    imageUrl=l[12];
    pushpinImage=l[13];
//alert(pushpinImage);
    //html+='    <Location index="'+lIx+'" latitude="'+lLat+'" longitude="'+lLon+'" Name="'+lName+'" dateStart="'+lDateStart+'" timeStart="'+lTimeStart+'" dateEnd="'+lDateEnd+'" timeEnd="'+lTimeEnd+'" >'+'\n';
    //html+='    <Location index="'+(i+1)+'" latitude="'+lLat+'" longitude="'+lLon+'" Name="'+lName+'" dateStart="'+lDateStart+'" timeStart="'+lTimeStart+'" dateEnd="'+lDateEnd+'" timeEnd="'+lTimeEnd+'" >'+'\n';
    html+='    <Location index="'+(i+1)+'" ';
    if (lLon!='' && lLat!='' && lLon!=null && lLat!=null) {
    //if (lLon!='' && lLat!='') {
//alert('bis\n'+lLon+'\n'+lLat);
      html+=' latitude="'+lLat+'" longitude="'+lLon+'" ';
    };
    if (pushpinImage!=null && pushpinImage!='') {
      html+=' pushpinImage="'+pushpinImage+'" ';
    };
    html+=' Name="'+lName+'" dateStart="'+lDateStart+'" timeStart="'+lTimeStart+'" dateEnd="'+lDateEnd+'" timeEnd="'+lTimeEnd+'" >'+'\n';
    html+='       <Description><![CDATA['+lText+']]></Description>'+'\n';
    html+='       <Address>'+lAddress+'</Address>'+'\n';
    html+='       <Tel>'+lTel+'</Tel>'+'\n';
    html+='       <DetailsURL><![CDATA['+lUrl+']]></DetailsURL>'+'\n';
    html+='       <ImageURL><![CDATA['+imageUrl+']]></ImageURL>'+'\n';
    //html+='       <Notes/>'+'\n';
    html+='    </Location>'+'\n';
  };
  html+='  </Locations>'+'\n';
  if (Connections!=null && Connections.length>0) {
  html+='  <Connections>'+'\n';
    // Connections[Connections.length]=[index, name, text, (url), (transportType), (company), (id), (dateStart), (timeStart), (dateEnd), (timeEnd), (duration), (description), [points] ] 
  var c, cIx,cName,cText,cUrl,cTransport,cCompany,cTransportID,cDateStart, cTimeStart,cDateEnd, cTimeEnd, cDuration,cDescription,cImageUrl;
  for (var i=0;i<TLocations.length;i++) {
    c=Connections[i];
    if (c==null || c.length==0) continue;
    cIx=c[0];
    cName=c[1];
    cText=c[2];
    cUrl=c[3];
    cTransport=c[4];
    cCompany=c[5];
    cTransportID=c[6];
    cDateStart=c[7];
    cTimeStart=c[8];
    cDateEnd=c[9];
    cTimeEnd=c[10];
    cDuration=c[11];
    cDescription=c[12];
    cImageUrl=c[13];
    if (cName=='' && cUrl=='' && cText=='' && cTransport=='') continue;
    //html+='    <Connection indexStartLocation="'+cIx+'" Name="'+cName+'">'+'\n';
    html+='    <Connection indexStartLocation="'+(i+1)+'" Name="'+cName+'">'+'\n';
    html+='       <Description><![CDATA['+cText+']]></Description>'+'\n';
    html+='       <Transportation transportType="'+cTransport+'" transportCompany="'+cCompany+'" transportID="'+cTransportID+'" dateStart="'+cDateStart+'" timeStart="'+cTimeStart+'" dateEnd="'+cDateEnd+'" timeEnd="'+cTimeEnd+'" duration="'+cDuration+'">'+'\n';
    html+='       <Description><![CDATA['+cDescription+']]></Description>'+'\n';
    html+='       </Transportation>'+'\n';
    html+='       <DetailsURL><![CDATA['+cUrl+']]></DetailsURL>'+'\n';
    html+='       <ImageURL><![CDATA['+cImageUrl+']]></ImageURL>'+'\n';
    //html+='       <Notes/>'+'\n';
    html+='       </Connection>'+'\n';
  };
  html+='  </Connections>'+'\n';
  };
  // Areas
//alert(Areas.length);
  if (Areas!=null && Areas.length>0) {
  html+='  <Areas>'+'\n';
    // Areas[Areas.length]=["id","name","tel","description", "detailsURL", dateStart, timeStart, dateEnd, timeEnd, image, pushpinImage, color, [[ix, "address",longitude,latitude, pointDescription], ...], opacity ];	
  var aId,aName,aTel,aDescription,aDetailsURL, aDateStart, aTimeStart, aDateEnd, aTimeEnd, aImage, aPushpinImage, aColor, aPoints, aOpacity, aPointsJS;	
  var a;
  for (var i=0;i<Areas.length;i++) {
    a=Areas[i];
    if (a==null || a.length==0) continue;
    aId=a[0];
    aName=a[1];
    aTel=a[2];
    aDescription=a[3];
    aURL=a[4];
    aDateStart=a[5];
    aTimeStart=a[6];
    aDateEnd=a[7];
    aTimeEnd=a[8];
    aImageURL=a[9];
    aPushPinImageURL=a[10];
    aColor=a[11];
    aPoints=a[12];
    aPointsJS=AreaPoints2PointsJS(a);
    aOpacity=a[13];
    //html+='    <Area index="'+(i+1)+'" Name="'+aName+'" color="'+aColor+'" opacity="'+aOpacity+'" >'+'\n';
    html+='    <Area index="'+(i+1)+'" ';
    if (aName!=null && aName!='') html+=' Name="'+aName+'" ';
    if (aColor!=null && aColor!='') html+=' color="'+aColor+'" ';
    if (aOpacity!=null && aOpacity!='') html+=' opacity="'+aOpacity+'" ';
    if (aPushPinImageURL!=null && aOpacity!='') html+=' PushPinImageURL="'+aPushPinImageURL+'" ';
    if (aDateStart!=null && aDateStart!='') html+=' DateStart="'+aDateStart+'" ';
    if (aTimeStart!=null && aTimeStart!='') html+=' TimeStart="'+aTimeStart+'" ';
    if (aDateEnd  !=null && aDateEnd  !='') html+=' DateEnd  ="'+aDateEnd  +'" ';
    if (aTimeEnd  !=null && aTimeEnd  !='') html+=' TimeEnd  ="'+aTimeEnd  +'" ';
    html+=' >'+'\n';
    html+='       <PointsList>'+aPointsJS+'</PointsList>'+'\n';
    //html+='       <Description><![CDATA['+aDescription+']]></Description>'+'\n';
    html+='       <Tel>'+aTel+'</Tel>'+'\n';
    html+='       <Description><![CDATA['+aDescription+']]></Description>'+'\n';
    html+='       <DetailsURL><![CDATA['+aURL+']]></DetailsURL>'+'\n';
    html+='       <ImageURL><![CDATA['+aImageURL+']]></ImageURL>'+'\n';
    //html+='       <Notes/>'+'\n';
    html+='       </Area>'+'\n';
  };
  html+='  </Areas>'+'\n';
  };

  html+='  </Trip>'+'\n';
//alert(html);
  return html; 
};



function GetTripXML4input () {
  HTML2Trip();
  var xmlTXT='';
  xmlTXT=Trip2XMLtext();
  //alert(xmlTXT);
  var xml=String2XML(xmlTXT);
  TripXMLdocument=xml;
  return TripXMLdocument;
};

var ShowXMLtripURL='Trip.htm?OpenerTripXML()';
function ShowInputTrip() {
  if (TLocations.length<2) {
    alert('Must have at least 2 locations');
    return;
  };
  HTML2Trip();
  var xml=GetTripXML4input();
  // open new window with trip
  var w=window.open(ShowXMLtripURL);
};

//var ShowXMLtripTXT='TripInputXMLtext.htm?OpenerTripXML()';
var ShowXMLtripTXT='TripInputXMLtextShow.htm?OpenerTripXML()';
function ShowInputTripXML() {
  if (TLocations.length<2) {
    alert('Must have at least 2 locations');
    return;
  };
  HTML2Trip();
  var xml=GetTripXML4input();
  // open new window with trip
  var w=window.open(ShowXMLtripTXT);
};

function ShowTripXMLtext () {
    HTML2Trip();
    var txt=Trip2XMLtext();
    alert(txt);
};

function LoadTripXML2Input (urlIn,xmlTextIn) {
  //alert('Not yet implemented');
  //var xml=LoadXML();
  //var tripXML=document.TripInput.TripXML.value;
  var tripXML=urlIn;
  if (tripXML==null)
    tripXML=document.TripInput.TripXML.value;
//alert(xmlTextIn);

  var locations=[];
  var connections=[];
  var tripData=[];
  // Display Locations
  
  //var xml=GetTripXML(tripXML)
  var xml;
  //alert('['+xmlTextIn+'] '+(xmlTextIn==null));
  if (xmlTextIn==null) {
    xml=GetTripXML(tripXML);
//alert('tripXML:'+tripXML+'\nxml:\n'+xml);
   } else { 
    xml=String2XML(xmlTextIn);
  };
  //alert(xml.xml);

  //alert(xml.xml);
  TripXMLdocument=null;
  //alert(TripLocationsXML(tripXML));
  var tl=null, tc=null, td=null;
  //tl=TripLocationsXML(tripXML);
  tl=TripLocationsXMLdoc(xml);
//alert('a.\n'+tl+'\n\n-[1][13].\n'+tl[1][13]);
//alert('a.\n'+tl+'\n\nb.\n'+locations);

  if (tl==null) {
    tl=[NewLocation.concat([]),NewLocation.concat([]),];
    //tl[0]=NewLocation.concat([]);
    tl[0][0]=1;
    tl[0][1]='location 1';
    //tl[1]=NewLocation.concat([]);
    tl[1][0]=2;
    tl[1][1]='location 2';
    //alert(tl);
  };
  //tc=TripConnectionsXML(tripXML);
  tc=TripConnectionsXMLdoc(xml);
  if (tc==null) {
    tc=[];
  };
  //td=TripTripXML(tripXML);
//alert('in\n'+xml);
  td=TripTripXMLdoc(xml);
  if (td==null) {
    td=NewTrip.concat([]);;
    td[0]='no trip';
  };
  
//alert('a.\n'+tl);
  locations=TripLocations2Locations(tl);
//alert('a.\n'+tl+'\n\nb.\n'+locations);
  //alert(locations);
  connections=Trip2Connections(tc);
  tripData=Trip2Trip(td);

  /*
  if (connections!=null && connections.length>0) {
    ShowConnectionsInInput=true;
   } else {
    ShowConnectionsInInput=false;
  };
  */
  
  ShowConnectionsInInput=(connections!=null && connections.length>0);
  ShowHideConnectionStatus();

  TLocations=[];
  Connections=[];
  for (var i=0;i<locations.length;i++) {
    TLocations[i]=locations[i];
    TLocations[i][0]=(i+1);
//alert(i+'. ['+TLocations[i][13]+']');
    if (i<locations.length-1 && ShowConnectionsInInput) {
      Connections[i]=NewConnection;
      Connections[i][0]=(i+1);
    };
  };

  if (connections!=null && connections.length>0) {
    var j;
    for (var i in connections) {
      //alert(connections[i]);
      j=connections[i][0];
      j--; // map the connection zero based
      Connections[j]=connections[i];
    };
  };

  TripXMLdocument=null;

  TripData=tripData;
  //alert(TripData);
  //
  TripData2HTMLset(); 
  // Display Connections
  LocationsShow(); 

  // Areas
  var as=TripAreasXMLdoc(xml);
  if (as==null) {
    as=[];
  };
  var areas=Trip2Areas(as);
  Areas=areas;
  AreasShow();
  if (Areas!=null && Areas.length>0) HideShowAreas('block');
};


function LoadTripXML2InputEdit (url,xmlTxt) {
  //DoZoom=true;
//alert('a');
  LoadTripXML2Input(url,xmlTxt);
//alert('b');
  //DoZoom=true;
  SelectedLocationEdit(0);
//alert('c');
};



// map

var TripInputMainAddress='Boca Raton, FL';
var TripInputMainAddressZoom=6;
function ShowMainAddress(address,mode) {
  if (address==null) { 
    address=document.getElementById("MainAddress").value;
   } else {
    document.getElementById("MainAddress").value=address;
  };
  var point=SetCentre(address,mode);
  if (point==null) return;
};

function SetCentre(address,mode) {
  //alert(address);
  var lat=null,lon=null, pCentre=null;
  geocoder.getLatLng(
    address,
    function(point) {
      var where=point;
      //LocationsUseLatLon[ix]=true;
      if (!point) {
        //LocationsUseLatLon[ix]=false;
 	if (lon==null || lat==null) {
	  lat=0;
	  lon=0;
  	  pCentre='NOPOINT';
	  window.status='Zone Main Address: '+'NO MAP: '+address;
  	  //ShowLocationsPath(Locations.length);
  	  //-SetZoneCenter(zoneIx,pointsCount);
          alert('Cannot locate:\n'+address);
           return;
         } else {
	};
	where=new GLatLng(lat,lon);
       } else { 
        //LocationsUseLatLon[ix]=true;
	lon=where.x;
    	lat=where.y;
      };

	//pCentre=where;
        //alert('Zones Main address'+' '+' : '+where.x+' , '+where.y+' : '+address);
        window.status='address'+' '+' : '+where.x+' , '+where.y+' : '+address;
        //map.setCenter(where);
        map.centerAndZoom(where, TripInputMainAddressZoom);

      if (mode=='ADD') {
        //AddAddressPoint(address,address,null);
        AddAddressPoint(address,'',null);
       } else if (mode=='INSERT') {
        //InsertAddressPoint(address,address,null);
        InsertAddressPoint(address,'',null);
      };
  
    }
  );
}

function ShowTripOnMap() {
  HTML2Trip();
  // remove markers
  // remove path
  // remove border
  map.clearOverlays();
//alert(TLocations);
//alert(TLocations.length);
/*
  for (var i=0;i<LocationsMarkers.length;i++) {
    map.removeOverlay(LocationsMarkers[i]);
  };
  map.removeOverlay(LocationsPath);
  map.removeOverlay(BorderPath);
*/
  LocationsPathPoints=[];
  LocationsMarkers=[];
  // recreate markers
  // recreate path
  // recreate border  
  Locations=TLocations;
  //DoZoom=zoom;
  ShowLocationsOnMap();
  //ShowLocations();
  //if (zoom!=null) ZoomToTrip();
  ShowAreas();
};


function ShowLocationsOnMap() {  
//alert('ShowLocationsOnMap');
//window.status='ShowLoctaionsOnMap '+;
        // ShowLocations from Trip.js
	//-ShowLocationsList();
        ShowLocationPathDrawn=false;
        showLocation(0);
/*
	for (var i=0;i<Locations.length;i++) {
	  //alert([Locations[i][0], Locations[i][4],Locations[i][5], "<div><b>"+Locations[i][1]+"</b><br/>"+Locations[i][2]+"<br/>Tel: "+Locations[i][3]+"</div>"]);
          //addMarker(Locations[i][0], Locations[i][4],Locations[i][5], "<div><b>"+Locations[i][1]+"</b><br/>"+Locations[i][2]+"<br/>Tel: "+Locations[i][3]+"</div>");           
	  //showAddress(Locations[i][0],Locations[i][2],"<div><b>"+Locations[i][1]+"</b><br/>"+Locations[i][2]+"<br/>Tel: "+Locations[i][3]+"</div>",Locations[i][4],Locations[i][5]);
	  showLocation(i);
	  //AddLocation(i);
        };
*/
	//ShowLocationsList();
        //if (zoom!=null) ZoomToTrip();
};

var PointFromMapCount=0;
var PointFromMapMode=null; // EDIT, INSERT, ADD, DISPLAY
function PointFromMap(overlay,point) {
  //var GetPoint=GetElementId('GetPointFromMap').checked;
  //if (!GetPoint) return;
  if (overlay) return;  
  //alert('point from map\n'+point);
  if (PointFromMapMode==null || PointFromMapMode=='' ) {
    // No mode: do nothing
    //alert('GeoPosition:\n'+point);
   } else if (PointFromMapMode=='EDIT') {
    SetLatLonEditFromMap(point);
    SetPointFromMapMode(null);
   } else if (PointFromMapMode=='INSERT') {
    PointFromMapCount++;
    InsertPointFromMap(point,PointFromMapCount);
   } else if (PointFromMapMode=='ADD') {
    PointFromMapCount++;
    AddPointFromMap(point,PointFromMapCount);
   } else if (PointFromMapMode=='DISPLAY') {
    alert('GeoPosition:\n'+point);
   } else {
    alert('Unknown Mode '+point+'\n'+PointFromMapMode);
  };
  return;
};

var ModeDisplayDiv='GetPointFromMapMode';
function SetPointFromMapMode (mode) {
  PointFromMapMode=mode;
  var text=mode;
  if (PointFromMapMode==null || PointFromMapMode=='' ) {
    // No mode: do nothing
    text='';
   } else if (PointFromMapMode=='EDIT') {
   } else if (PointFromMapMode=='INSERT') {
   } else if (PointFromMapMode=='ADD') {
   } else if (PointFromMapMode=='DISPLAY') {
   } else {
    text='?';
    alert('Unknown Mode '+point+'\n'+PointFromMapMode);
  };
  if (text!='') text='['+text+']';
  document.getElementById(ModeDisplayDiv).innerHTML=text;
  return;
};


function ChangeLocationPointFromMap(ix) {
  alert('get point from map for location:'+ix);
};

function ChangeLocationEditPointFromMap() {
  if (PointFromMapMode=='EDIT') {
    SetPointFromMapMode(null);
    return;
  };
  var ixN=document.TripInput.LocationSelector_Edit.selectedIndex;
  var ix='Edit';
  //alert('get point from map for location:'+ix+' '+ixN);
  SetPointFromMapMode('EDIT');
  alert('On the map\nClick on the point you want to use for the location \nto cancel click the button again');
};

function AddPointFromMap (point,n) {
  //AddAddressPoint('MapAdd '+n,null,point);
  //AddAddressPoint('Map '+n,null,point);
  AddAddressPoint('Map '+(TLocations.length+1),null,point);
  SelectedLocationEdit(TLocations.length-1);
};

function InsertPointFromMap (point,n) {
  //alert('Insert: '+n+'\n'+point);
  //InsertAddressPoint('MapInsert '+n,null,point);
  //InsertAddressPoint('Map '+n,null,point);
  InsertAddressPoint('Map '+(TLocations.length+1),null,point);
  //SelectedLocationEdit(TLocations.length-1);
};


function AddAddressPoint(name,address,point) {
  //AddLocation();
  //var ix=TLocations.length;
  var l=NewLocation.slice(0);
  l[1]=name;
  if (address!=null) { 
    l[2]=address;
  }
  if (point!=null) {
    l[4]=point.x;
    l[5]=point.y;
  };
  AddLocation(l,null);
};

function InsertAddressPoint(name,address,point) {
  //AddLocation();
  //var ix=document.TripInput.LocationSelector_Edit.selectedIndex;
  var ix=GetSelectorEdit();
  var l=NewLocation.slice(0);
  l[1]=name;
  if (address!=null) { 
    l[2]=address;
  }
  if (point!=null) {
    l[4]=point.x;
    l[5]=point.y;
  };
/*
  if (ix<TLocations.length)   
    InsertLocation(ix,l,null)
   else
    AddLocation(l,null);
*/
  InsertLocation(ix,l,null)
  //ShowLocations();
  SelectedLocationEdit(ix+1);
};

function SetLatLonEditFromMap(point) {
  document.TripInput.LocationLat_Edit.value=point.x;
  document.TripInput.LocationLon_Edit.value=point.y;
  SaveEditLocation();
};

function AddPointsFromMap() {
  var b=document.getElementById('AddPointsFromMapButton');
  if (PointFromMapMode=='ADD') {
    SetPointFromMapMode(null);
    b.value='Add Points';
    return;
  };
  PointFromMapCount=0;
  SetPointFromMapMode('ADD');
  b.value='END Add';
};

function InsertPointsFromMap() {
  var b=document.getElementById('InsertPointsFromMapButton');
  if (PointFromMapMode=='INSERT') {
    SetPointFromMapMode(null);
    b.value='Insert Points';
    return;
  };
  PointFromMapCount=0;
  SetPointFromMapMode('INSERT');
  b.value='END Insert';
  alert('On the map\nClick on the points you want to use for locations \nto end click the button again');
};

// Override Trip

var InvalidGeoPointMessage=null;

/*
function zoomToPolyLineTrip(points) {};
zoomToPolyLineTrip=zoomToPolyLine;
*/

/*no
var DoZoom=null;
function zoomToPolyLine(pl) {
  if (DoZoom!=null) {
    zoomToPolyLineTrip(pl);
    DoZoom=null;
  };
  return pl;
};

function zoomToPolyLineTrip(pl) {
//alert('pl:'+pl.length+'\n'+pl);
  if (pl.length<1) return pl; // if no points do not zoom
  var bounds = new GLatLngBounds();
  //var bounds = new GBounds();
  for (var i=0;i<pl.length;i++) {
    //alert(i+'. \n'+pl[i]+'\n'+bounds);
    bounds.extend(pl[i]); 
  };  
  //alert(bounds);
  map.setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds)); 
  return pl;
};



function ZoomToTrip() {
  //zoomToPolyLine(LocationsPathPoints);
  DoZoom=true;
  //zoomToPolyLineTrip(LocationsPathPoints);
  var points=LocationsPathPointsValid();
//alert(points);
  zoomToPolyLine(points);
};
*/

function ShowLocationsList() {
  var html=LocationsListSelector2HTML();
  //document.all.LocationsSelector.innerHTML=html;
  SetElementHTML('LocationsSelector',html);
  //SetElementHTML('StopsCount',TLocations.length);
};

// add: edit, from map
function LocationDescriptionHTML (ix) {
  var html="";
  //if (Locations[ix]==null) return html;
  var id,name,address,tel,html,lat,lon, description, detailsURL,imageUrl;
  id=Locations[ix][0];
  name=Locations[ix][1];
  address=Locations[ix][2];
  tel=Locations[ix][3];
  lon=Locations[ix][4];
  lat=Locations[ix][5];
  //alert(Locations[ix]+'\n'+lon+'\n'+lat);
  description=Locations[ix][6];
  detailsURL=Locations[ix][7];
  dateStart=Locations[ix][8];
  timeStart=Locations[ix][9];
  dateEnd=Locations[ix][10];
  timeEnd=Locations[ix][11];
  imageUrl=Locations[ix][12];
  //description=Locations[ix][6];
  // http://maps.google.com/maps?&ll=35.962446,-77.817535
  var LatLonURL='';
  if (lat!=null && lat!='' && lon!=null && lon!='') {
  //if (LocationsUseLatLon[ix]==false) {
  //if (lat!=null && LocationsUseLatLon[ix]==false) {
    LatLonURL='http://maps.google.com/maps?&ll='+lat+','+lon+'';
   } else {
    //var addressMap=address;
    //addressMap=address.replace(/ /g,'%20');
    //LatLonURL='http://maps.google.com/maps?&q='+addressMap;
    //LatLonURL='http://maps.google.com/maps?&q='+address;
    LatLonURL='http://maps.google.com/maps?f=q&hl=en&q='+address;
    if (address=='') LatLonURL+=name;
    //alert(LatLonURL);
  };
  LatLonURL=URLnoBlanks(LatLonURL);
  //alert(LatLonURL);
  //var LatLonIcon='LNPRidlûüýþw~ë';
  var LatLonText='i';
  var LatLonHTML='<a href='+LatLonURL+' target="_blank"><font face="webdings" style="{text-decoration: none;}">'+LatLonText+'</font></a>';
  //var LatLonTextShow='l';
  //LatLonHTML+='<a href="javascript:alert(\'Latitude, Longitude:\\n'+lat+', '+lon+'\')"><font face="webdings" style="{text-decoration: none;}">'+LatLonTextShow+'</font></a>';
  if (lat!=null && lat!='' && lon!=null && lon!='') {
  //if (LocationsUseLatLon[ix]==false) {
    LatLonHTML+=' <br/>['+'<a href='+LatLonURL+' target="_blank" style="text-decoration:none;">'+lat+', '+lon+'</a>'+']';
  }

  if (description==null) description="";
  //html="<div><b>"+name+"</b><br/>"+address+"<br/>Tel: "+tel+"<br/><a href='http://www.NicolaZordan.net' target='_blank'>www.NicolaZordan.net</a></div>";
  html="";
  html+='<table 0border="1"><tr><td><big>';
//alert(imageUrl+'\n'+Locations[ix]);
  if (imageUrl!=null && imageUrl.length>0) {
    html+='<img src="'+imageUrl+'" height="100" border="0" align="left" style="{margin-right: 2px;}" />';
    //html+='</td><td>';
  };
  if (detailsURL!=null && detailsURL.length>0) {
    //html+='<br/><a href="'+detailsURL+'" target="_blank">details</a>';
    html+='<a href="'+detailsURL+'" target="_blank">';
  };
  html+="<b>"+name+"</b>";
  if (detailsURL!=null && detailsURL.length>0) {
    //html+='<br/><a href="'+detailsURL+'" target="_blank">details</a>';
    html+='</a>';
  };
  if (dateStart!=null && dateStart.length>0) {
    html+=' '+dateStart;
  };
  if (timeStart!=null && timeStart.length>0) {
    html+=' '+timeStart;
  };
  if (dateEnd!=null && dateEnd.length>0) {
    html+=' - '+dateEnd;
  };
  if (timeEnd!=null && timeEnd.length>0) {
    html+=' '+timeEnd;
  };
  html+=" "+LatLonHTML;

  //html+="<br/>"+address;
  if (address!=null && address.length>0) {
    html+="<br/>"+address;
  };
  if (tel!=null && tel.length>0) {
    html+="<br/>Tel: "+tel;
  };
//alert(ix);
  if (description!=null && description.length>0) {
    //html+="<br/>"+description;
//alert(description);
    html+="<br/>"+description.replace(/\n/g,'<br/>\n');
  };
  //if (detailsURL!=null && detailsURL.length>0) {
  //  html+='<br/><a href="'+detailsURL+'" target="_blank">details</a>';
  //};
  if (Connections[ix]!=null) {
    //html+='<br/>'+ConnectionDescriptionHTML(ix);
    //html+='<br/>'+'<table><tr><td>'+ConnectionDescriptionHTML(ix)+'</td></tr></table>';
    html+='</big></td></tr><tr><td><big>';
    html+=ConnectionDescriptionHTML(ix);
  };
  //html+="<div><b>"+name+"</b><br/>"+address+"<br/>Tel: "+tel+"<br/><a href='http://www.NicolaZordan.net' target='_blank'>www.NicolaZordan.net</a></div>";

  html+='<br/>'+EditLinks(ix);

  html+='</big></td></tr></table>';
  html+="";
  html="<small>"+html+"</small>";
  return html;
};

function EditLinks(ix) {
  var html='';
  //html+='<a href="javascript:SelectedLocationEdit('+ix+');document.TripInput[\'LocationName_'+ix+'\'].focus();">edit</a>';
  html+='<a href="javascript:SelectedLocationEdit('+ix+');">edit</a>';
  html+=' - ';
  html+='<a href="javascript:SelectedLocationEdit('+ix+');ChangeLocationEditPointFromMap();">from map</a>';
  html+=' - ';
  html+='<a href="javascript:RemoveLocation('+ix+');">remove</a>';
  html+=' - ';
  html+='<a href="javascript:MoveDownLocation('+ix+');">move down</a>';
  html+=' - ';
  html+='<a href="javascript:MoveUpLocation('+ix+');">move up</a>';
  return html;
};

//--

var host=window.opener;
var hostUser='';
var hostFilePath='';
var hostFileName='';
var hostText='';

function GetDataFromHost() {
  if (host==null || host.ActionForm==null) {
    if (host!=null) {
      if (host.PushpinStartStop!=undefined) 
        PushpinStartStop=host.PushpinStartStop;
      if (host.ShowTripPath!=undefined) 
        ShowTripPath=host.ShowTripPath;
      if (host.ShowTripBorder!=undefined) 
        ShowTripBorder=host.ShowTripBorder;
    };
    return;
  };
  hostUser=host.ActionForm.xmlUser.value;
  hostFilePath=host.ActionForm.xmlFilePath.value;
  hostFileName=host.ActionForm.xmlFileName.value;
  //openerText=host.ActionForm.xmlText.value;
};

function SetDataFromHost () {
  document.TripInput.xmlUser.value=hostUser;
  document.TripInput.xmlFilePath.value=hostFilePath;
  document.TripInput.xmlFileName.value=hostFileName;
  document.TripInput.TripName.value=hostFileName;
  if (host==null || host.ActionForm==null) {
    document.TripInput.SaveTripButton.disabled=true;
    document.TripInput.TripPath.disabled=true;
  };
};

//GetDataFromHost();

function TripSaveHost() {
  var name=document.TripInput.TripName.value;
  document.TripInput.xmlFileName.value=name;
  var path=document.TripInput.TripPath[document.TripInput.TripPath.selectedIndex].value;
  document.TripInput.xmlFilePath.value=path;

  var xmlFileName=document.TripInput.xmlFileName.value;
  if (xmlFileName=='') {
    alert('Missing Trip name');
    document.TripInput.xmlFileName.focus();
    return;
  };
  var xmlFilePath=document.TripInput.xmlFilePath.value;
  //var xmlFilePath=document.TripInput.xmlFileName.value;

  HTML2Trip();
  var xml=GetTripXML4input();
  //- open new window with trip
  //-var w=window.open(ShowXMLtripTXT);
  var xmlText=xml.xml;
  //alert(xmlText);
  //if (!confirm(xmlText)) return;
  if (host==null || host.TripSave==null || (''+host.TripSave=='undefined')) {
    alert('Cannot save the trip: '+name);
    w=window.open(ShowXMLtripTXT);
    return;
  };
  //if (!confirm('Confirm Save ?')) return;
  host.focus();
  host.TripSave(xmlFilePath,xmlFileName,xmlText);
  //alert('closing');
  window.close();
};

function OpenPageField(fieldName) {
  if (fieldName==null || fieldName=='') {
    alert('No field name for URL');
    //return null;
    return;
  };
  var url=document.TripInput[fieldName].value;
  if (url==null || url=='') {
    alert('No URL');
    //return nulll
    return;
  };
  var w=window.open(url);
  //return w;
};

//--

  var FolderCurrent='/';
  var Folders=[FolderCurrent];
  
  function GetFolders() {
    if (window.opener!=null)
      if (window.opener.UserFolders!=null)
        Folders=window.opener.UserFolders;
    if (window.opener!=null)
      if (window.opener.ActionForm!=null)
        if (window.opener.ActionForm.xmlFilePath!=null)
          FolderCurrent=window.opener.ActionForm.xmlFilePath.value;
  };

  function SelectFoldersHTML(name,currentFolder,foldersIn) {
    var folders=foldersIn;
    if (folders==null) folders=Folders;
    var html='';
    html+='<select name="'+name+'">';
    for (var i=0;i<folders.length;i++) {
      html+='<option value="'+folders[i]+'" ';
      if (currentFolder==folders[i]) html+=' selected ';
      html+='>'+folders[i]+'</option>';
    };
    html+='</select>';
    return html
  };

//--
// Areas
    // Areas[Areas.length]=["id","name","tel","description", "detailsURL", dateStart, timeStart, dateEnd, timeEnd, image, pushpinImage, [Location, Location, ...],opacity ];	

var NewArea=[0,"area name","","", "", '', '', '', '', '', '', [], '' ];

function AreasListSelector2HTML () {
  var html='';
  var AreaName='';
  var AreaColor='';
  //html+='';
  var showingIx=null;
  var editIx=document.TripInput.AreaSelector_Edit;
  if (editIx!=null) {
    showingIx=editIx.value;
  };

  html+='<ol type="A">';
  //html+='Areas';
  var radioIx=0;
  for (var i=0;i<Areas.length;i++) {
    AreaName=Areas[i][1];
    AreaColor=Areas[i][11];
// SLOW
    radioIx=i+(Locations.length+1);
    window.status='Area Selector: '+i;
    html+='<li>';
    //html+='<input type="radio" name="AreaSelectorEditRadio" value="'+i+'" onClick="SelectedAreaEdit('+i+')" style="{font-family: webdings}" ';
    html+='<input type="radio" name="LocationSelectorEditRadio" value="'+radioIx+'" 0onClick="SelectedAreaEdit('+i+')" style="{font-family: webdings}" ';
    if (i==showingIx) html+=' checked ';
    html+=' />';

    html+='<input type="button" value="q" onClick="SelectedAreaEdit('+i+')" 0value="þ" 0value="û" 0onClick="GoogleMapAreaIx('+i+')" style="{font-family: webdings}" />';

    html+=' ';
    
    //html+='<input type="button" value="4" onClick="InsertEditArea('+i+')" style="{font-family: webdings}" />';

    html+='<input type="button" value="5" onClick="MoveUpArea('+i+')" style="{font-family: webdings}" ';
    if (i==0) html+=' disabled ';
    html+=' />';
    html+='<input type="button" value="6" onClick="MoveDownArea('+i+')" style="{font-family: webdings}" ';
    if (i==Areas.length-1) html+=' disabled ';
    html+=' />';
    
    html+='<input type="button" value="r" onClick="RemoveAreaEdit('+i+')" style="{font-family: webdings}" '
    html+=' />';
    
    html+=' ';

    html+=AreaColorHTML(i)+' ';
    
    if (map!=null) {
      //html+='<a href="javascript:SelectedAreaEdit('+i+');showAreaDetails('+i+')" ';
      html+='<a href="javascript:showAreaDetails('+i+')" ';
      if (Areas[i][12]==null || Areas[i][12]=='') 
      //if (Areas[i][12]==InvalidGeoPoint) 
        html+='style="color:red"';
      html+=' >';
    };

    html+=AreaName;

    if (map!=null) {
      //-if (LocationsPathPoints[i]==InvalidGeoPoint) 
      //-  html+='</font>';
      html+='</a>';
    };

    html+='</li>';
  };
  // Add Area
    html+='<li>';
    html+='<input type="radio" name="AreaSelectorEditRadio" value="'+Areas.length+'" onClick="SelectedAreaEdit('+Areas.length+')" style="{font-family: webdings}" ';
    if (showingIx>=Areas.length) html+=' checked ';
    html+=' />';
    html+='<input type="button" value="Add" onclick="AddAreaEdit()" />';
    html+='</li>';


  html+='</ol>';
  return html;
}

var AreaEditURL='AreaInput.htm?IX=';
function SelectedAreaEdit (ix) {
  var url=AreaEditURL+ix;
  var w=window.open(url);
};

function AddAreaEdit (ix) {
  SelectedAreaEdit(Areas.length);
};

function UpdateAreaIx(ix, name, points, color, opacity,  tel,description,url,dateStart,timeStart,dateEnd,timeEnd,image,pushpin) {
    // Areas[Areas.length]=["id","name","tel","description", "detailsURL", dateStart, timeStart, dateEnd, timeEnd, image, pushpinImage, [Location, Location, ...], opacity ];	
  var a=NewArea.slice(0);
  a[0]=ix;
  a[1]=name;
/*
  a[2]=tel;
  a[3]=description;
  a[4]=url;
  a[5]=dateStart;
  a[6]=timeStart;
  a[7]=dateEnd;
  a[8]=timeEnd;
  a[9]=image;
  a[10]=pushpin;
*/
  a[11]=color;
  //a[12]=points;
  var aPoints=[];
  if (points!=null) aPoints=eval(points);
  a[12]=PointsJS2AreaPoints(aPoints);
  a[13]=opacity;
  Areas[ix]=a;
  AreasShow();
  return a;
};

function GoogleMapAreaIx (ix) {
  alert('Not yet implemented');
};

function InsertEditArea (ix) {
  alert('Not yet implemented');
};


function SwapAreas(from,to) {
  var aTmp=Areas[to];
  Areas[to]=Areas[from];
  Areas[to][0]=(from+1);
  Areas[from]=aTmp;
  Areas[from][0]=(to+1);
};

function MoveUpArea (ix) {
  //HTML2Trip();
  var AreaIndex;
  if (ix==null) {
    AreaIndex=GetAreaSelector();  
   } else {
    AreaIndex=ix;  
  };
  if (AreaIndex==null) {
    alert('Select the Area to move up');
    return;
  };
  if (AreaIndex==0) {
    alert('Cannot move before first');
    return;
  };
  var from=AreaIndex;
  var to=AreaIndex-1;
  SwapAreas(from,to);
 
  AreasShow();
};

function MoveDownArea (ix) {
  //HTML2Trip();
  var AreaIndex;
  if (ix==null) {
    AreaIndex=GetAreaSelector();  
   } else {
    AreaIndex=ix;  
  };
  if (AreaIndex==null) {
    alert('Select the Area to move down');
    return;
  };
  if (AreaIndex>=(Areas.length-1)) {
    alert('Cannot move after last');
    return;
  };
  var from=AreaIndex;
  var to=AreaIndex+1;
  SwapAreas(from,to);
  AreasShow();
};

function RemoveAreaEdit (ix) {
  //HTML2Trip();
  var AreaIndex;
  if (ix==null) {
    AreaIndex=GetAreaSelector();  
   } else {
    AreaIndex=ix;  
  };
  //alert(Location2Remove+'\n');
  if (AreaIndex==null) {
    alert('Select the Area to remove');
    return;
  };
  var areasTmp=Areas;
  Areas=[];
  for (var i=0;i<areasTmp.length;i++) {
    if (i==AreaIndex) continue;
    ix=Areas.length;
    Areas[ix]=areasTmp[i];
    Areas[ix][0]=ix+1;
  };
  AreasShow();
  //FocusOnArea(ix);
};

function FocusOnArea(ix) {
  alert('Not yet implemented');
};


function Areas2HTML () {
  html='';
  html+='Areas';
  html+='<table>';
  /*
  html+='<tr>';
  html+='<td colspan="3" align="center">';
  html+='Index: '+AreasIndexHTML();
  html+='</td>';
  html+='</tr>';
  */
  html+='<tr>';
  html+='<th>';
  html+='selector';
  html+='</th>';
  html+='<th>';
  html+='Location';
  html+='</th>';
  html+='<th>';
  html+='Details';
  html+='</th>';
  html+='</tr>';
  for (var i=0;i<Areas.length;i++) {
    html+=Area2HTML(i);
// SLOW
    window.status='Area: '+i;
  };
  html+='</table>';
  return html;
};


function AreaPoints2PointsJS (a) {
  var jsA='';
  jsA+='[ ';
  var pA=a[12];
  for (var i=0;i<pA.length;i++) {
    if (i>0) jsA+=', ';
    jsA+='['+pA[i][2]+', '+pA[i][3]+']';
  };
  jsA+=' ]';
  return jsA;
};


function PointsJS2AreaPoints (js) {
  var jsA=eval(js);
  var aPoints=[];
  var p,aP;
  var pId,address,pointsListArray,lat,lon,pDescription;
  for (var i=0;i<jsA.length;i++) {
    p=jsA[i];
        pId=(i+1);
        address=null;
        lat=p[0];
        lon=p[1];
        pDescription=null;
    aP=[pId,address,lat,lon,pDescription];
    aPoints[aPoints.length]=aP;
  };
  jsA+=' ]';
  return aPoints;
};


//var AreaValidId='LocationValid_';
function Area2HTML (ix) {
    // Areas[Areas.length]=["id","name","tel","description", "detailsURL", dateStart, timeStart, dateEnd, timeEnd, image, pushpinImage, color, [[ix, "address",longitude,latitude, pointDescription], ...], opacity ];	
  //var l=Locations[ix];
  var a=Areas[ix];
  //alert(l);
  var lon='', lat='';
  var html='';
  html+='';
  //var html+='';
  html+='<tr>';

  html+='<td valign="top" align0="left">';
  html+=(ix+1)+'. ';
  html+='<input name="AreaSelector" type="radio" valueIx="'+a[0]+'" value="'+ix+'"/>';
  html+='<input name="AreaIndex_'+ix+'" type="hidden" value0="'+a[0]+'" value="'+ix+'"/>';
  html+='</td>';
  
  html+='<td valign="top" align="left">';
  html+='<input name="AreaName_'+ix+'" type="text" value="'+a[1]+'"/>';
  html+='</td>';

  html+='<td valign="top" align="left">';
  
  html+='<table>';  
  html+='<tr><td align="left"><b>Points</b></td><td align="left">';    
  var points=AreaPoints2PointsJS(a);
  html+='<input name="AreaPoints_'+ix+'" type="text" value="'+points+'"/>';

  html+='<input type="button" value="map" onClick="openGoogleMapAddress(document.TripInput.LocationAddress_'+ix+'.value)" 0onClick="GoogleMapAddress('+ix+')" />';
  //html+='<input type="button" value="check" onClick="CheckAreaPoints('+ix+',true)" />';
  //html+=' <span id="'+AreaValidId+ix+'">-</span>';

  html+='</td></tr>';    
//-alert(l[13]);  
  if (a[2]==null) a[2]='';
  html+='<tr><td align="left">Color</td><td align="left">';    
  html+='<input name="AreaColor_'+ix+'" type="text" value="'+a[11]+'"/>';
  html+='</td></tr>';    
  html+='<tr><td align="left">Push Pin</td><td align="left">';    
  html+='<input name="AreaPushPinImage_'+ix+'" type="text" value="'+a[10]+'"/>';
  html+='</td></tr>';    
  html+='</table>';  
  
  
  html+='</td>';
  
  html+='</tr>';

  return html;
};

function AreasShow0 () {
  var html='';
  html+=Areas2HTML();
  SetElementHTML('AreasData',html);
  html=AreasListSelector2HTML();
  SetElementHTML('AreasSelector',html);
  //document.all.StopsCount.innerHTML=Locations.length;
  //document.all.StopsCount.innerHTML=TLocations.length;
  //-SetElementHTML('AreasCount',TLocations.length);
  if (map!=null) ShowTripOnMap(null);
};

function HTML2Area (ix) {
    // Areas[Areas.length]=["id","name","tel","description", "detailsURL", dateStart, timeStart, dateEnd, timeEnd, image, pushpinImage, color, [[ix, "address",longitude,latitude, pointDescription], ...], opacity ];	
  //var a=Areas[ix];
  var id, name,tel,description,url,dateStart,timeStart,dateEnd,timeEnd,imageUrl,pushpinImage, color, pointsJS, opacity;
  var ixi=ix+1;
//alert(ix);
  id=document.TripInput['AreaIndex_'+ix].value;
  name=document.TripInput['AreaName_'+ix].value;
  tel=document.TripInput['AreaTel_'+ix].value;
  //lon=document.TripInput['LocationLon_'+ix].value;
  lat=document.TripInput['AreaLat_'+ix].value;
  lon=document.TripInput['AreaLon_'+ix].value;
  description=document.TripInput['AreaDescription_'+ix].value;
  url=document.TripInput['AreaURL_'+ix].value;
  dateStart=document.TripInput['AreaDateStart_'+ix].value;
  timeStart=document.TripInput['AreaTimeStart_'+ix].value;
  dateEnd=document.TripInput['AreaDateEnd_'+ix].value;
  timeEnd=document.TripInput['AreaTimeEnd_'+ix].value;
  imageUrl=document.TripInput['AreaImageURL_'+ix].value;
  pushpinImage=document.TripInput['AreaPushPinImage_'+ix].value;
  color=document.TripInput['AreaColor_'+ix].value;
  pointsJS=document.TripInput['AreaPointsJS_'+ix].value;
  opacity=document.TripInput['AreaOpacity_'+ix].value;
  var a=[ ixi, name, tel, description, url, dateStart, timeStart, dateEnd, timeEnd,imageUrl,pushpinImage];
  //-var l=[ markerId(ixi), name, address, tel, lat, lon, text, url, dateStart, timeStart, dateEnd, timeEnd];
  //alert('Loc '+ix+'\n'+l);
  return l;  
}; 

function HTML2Locations () {
 //alert(TLocations.length);
 //return;
  //
  //alert(TLocations);
  for (var i=0;i<TLocations.length;i++) {
    TLocations[i]=HTML2Location(i);    
  };
  //alert(TLocations);
  //alert(TLocations);
  return TLocations;
}; 

function GetURLFromWindow (fieldName,formName,winIn) {
  var win=winIn;
  if (win==null) win=window.open();
  URLFromWindow(fieldName,formName,win);
  return win;
};

var URLFromWindowWin=null;
function URLFromWindow (field,form, winIn) {
  var win=winIn;  
//alert('['+field+']\n['+form+']\n['+win+']');
  if (win==null) win=URLFromWindowWin;
  if (win==null) return;

  if (win.closed) return;
  if (field==null || form==null) return;
  URLFromWindowWin=win;
  //var url=win.URL;
  var url=win.location;
  var d=document;
  var f=document[form];
  var i=document[form][field];
  var v=document[form][field].value;
//alert(d+'\n'+f+'\n'+i+'\n\n['+v+']');
  
  document[form][field].value=url;
  //setTimeout('URLFormWindow("'+field+'","'+form+'")',1000/5); 
//alert('URLFormWindow("'+field+'","'+form+'")');
  setTimeout('URLFromWindow("'+field+'","'+form+'")',1000/5); 
}; 


