// Nicola Zordan: www.NicolaZordan.com 10:21 4/25/2007


function MapContacts(xmlText) {
  return MapContactsXmlTxt(xmlText);
};

function MapContactsXmlText(xmlText,cList) {
  var w=window.open('about:blank');
  w.document.write(MapHTML(xmlText,cList));
  w.document.close();
};

function MapContactsXml(xml) {
  return MapContactsXmlText(xml.xml);
};

// [[name,address,tel,description,company],]
function MapContactsArray(contactsArray) {
  var xml='', cList='';

  if (contactsArray.length < 1) {
    alert('No contacts to be mapped');
    return;
  };

  cList=cList+'<ol>';

  var contactCount = 0
  var contact, name,address,tel,description,compnay;

  for (var i=0;i<contactsArray.length;i++) {
      contact=contactsArray[i];
      name = contact[0];
      address = contact[1];
      tel = contact[2];
      description = contact[3];
      company = contact[4];
      cList = cList + '<li>' + name + '</li>' + '\n';
      
      contactCount = contactCount + 1;
  };

  cList=cList+'</ol>';

  var xml=MapContactsArray2XMLtext(contactsArray);

  return MapContactsXmlText(xml,cList);
};




function MapHTML(xmlTxt,cListHTML) {
  var MappingServerPage = 'http://www.MappedContacts.com/MapContacts.asp';

  var cList=cListHTML;
  if (cList==null) cList='<ol>List of contacts</ol>';
 
  // create the IE html
  var html = '';
  html = html + '<html>' + '\n';
  html = html + '<head>' + '\n';
  html = html + '<title>' + '\n';
  html = html + 'MappedContacts.com' + '\n';
  html = html + '</title>' + '\n';
  html = html + '</head>' + '\n';
  html = html + '<body onload="GetMap()" >' + '\n';
  
  html = html + '<h1><center>' + '\n';
  html = html + 'Contacts Map' + '\n';
  html = html + '</center></h1>' + '\n';
  
  html = html + '<form name="contactsXML" action="' + MappingServerPage + '" method="POST" >' + '\n';
 
  html = html + '<center>' + '\n';
  html = html + '<input type="submit" value="show map" 0onClick="ShowMap()" />' + '\n';
  html = html + '<table><tr><td align="left">' + '\n';
  html = html + cList + '\n';
  html = html + '</td></tr></table>' + '\n';
  html = html + '</center>' + '\n';
  
  html = html + '<script language="JavaScript">' + '\n';
  html = html + '<!--//' + '\n';
  html = html + '\n';
  
  html = html + 'function GetMap() {' + '\n';
  html = html + '  document.contactsXML.submit();' + '\n';
  html = html + '};' + '\n';
  
  html = html + '\n';
  
  html = html + 'var ZoneName="Contacts";' + '\n';
  html = html + 'var ZoneMembers=[];' + '\n';
  html = html + 'var ZoneEMails=[];' + '\n';
  html = html + 'var ZoneTeamLeadersIx=[];' + '\n';
  html = html + 'var ZoneMeetingPlaces=[];' + '\n';
  
  html = html + '\n';
  html = html + 'var MapPage="http://MappedContacts.com/MapContacts.htm";  ' + '\n';
  html = html + 'var TripXMLdocument;  ' + '\n';
  html = html + 'function ShowMap () {' + '\n';
  html = html + '  //alert("show map");' + '\n';
  html = html + '  var xmlTxt=document.all.xmlTextArea.value;' + '\n';
  html = html + '  ' + '\n';
  html = html + '  var xmlObj=new ActiveXObject("Microsoft.XMLDOM");' + '\n';
  html = html + '  xmlObj.async=false;' + '\n';
  html = html + '  xmlObj.loadXML(xmlTxt);' + '\n';
  html = html + '  ' + '\n';
  html = html + '  TripXMLdocument=xmlObj;' + '\n';
  html = html + '  ' + '\n';
  html = html + '  window.open(MapPage);' + '\n';
  html = html + '  ' + '\n';
  html = html + '  //alert(xmlTxt);' + '\n';
  html = html + '  ' + '\n';
  html = html + '\n';
  html = html + '};' + '\n';
  html = html + '\n';
  html = html + '//-->' + '\n';
  html = html + '</script>' + '\n';
  
  html = html + '<div style="{visibility: hidden; display: none}">' + '\n';
  html = html + '<textarea id="xmlTextArea" name="xmlText" >' + '\n';
  html = html + xmlTxt + '\n';
  html = html + '</textarea>' + '\n';
  html = html + '</div>' + '\n';
  
  html = html + '</form>' + '\n';
  
  
  html = html + '</body>' + '\n';
  html = html + '</html>' + '\n';
  
  return html;
};


function MapContactsXMLtext2Array(xmlText) {
  var xml=String2XML(xmlText);
  return MapContactsXML2Array(xml);
};


// [[name,address,tel,description,company],]
function MapContactsXML2Array(xml) {
  var contacts=[];

  var lXML=xml.getElementsByTagName('Location');

  var l,id,name,address,tel, lat,lon, desc, detUrl, ds,ts,de,te,notes;
  for (i=0;i<lXML.length;i++) {
    l=lXML[i];
    id=l.getAttribute('index');
    name=l.getAttribute('Name');
    address=getElementByTagName(l,'Address');
    tel=getElementByTagName(l,'Tel');
    lat=l.getAttribute('latitude');
    lon=l.getAttribute('longitude');
    notes=getElementByTagName(l,'Notes');
    desc=getElementByTagName(l,'Description');
    detUrl=getElementByTagName(l,'DetailsURL');
    ds=l.getAttribute('dateStart');
    ts=l.getAttribute('timeStart')
    de=l.getAttribute('dateEnd');
    te=l.getAttribute('timeEnd');

    //locations[locations.length]=[id,name,address,tel, lat,lon, desc, detUrl, ds,ts,de,te];    
    contacts[contacts.length]=[name,address,tel, desc];    
//alert(i+'. '+locations[locations.length-1]);
  }
  //locations[locations.length]=null;    

  return contacts;  
};

// [[name,address,tel,description,company],]
function MapContactsXML2ArrayJS(xml) {
  var cJS='';
  var contacts=MapContactsXML2Array(xml);
  var contact;
  cJS=cJS+'[ ';
  for (i=0;i<contacts.length;i++) {
    if (i>0) cJS=cJS+', ';
    contact=contacts[i];
    cJS=cJS+'["'+contact[0]+'", "'+contact[1]+'", "'+contact[2]+'", "'+contact[3]+'", "'+contact[4]+'"]';
  };
  cJS=cJS+' ]';
  return cJS;
};

function getElementByTagName(l,name) {
  var r='';
  var es=l.getElementsByTagName(name);
  if (es==null || es.length<1) return r;
  var e=es[0];
  r='';
  if (e.text != null) r=r+e.text;
  if (e.textContent != null) r=r+e.textContent;
//alert(r);
  return r;
};


function MapContactsCSV2Array(csvTextIn) {
  var a=[];
  var csvText=csvTextIn.replace(/\r/g,'');
  var csv=csvText.split('\n');
  var contactCount = 0
  var contact, name,address,city,state,zip,country,arrayContact, tel, email, description,company,note;
  for (var i=0;i<csv.length;i++) {
    contact=csv[i].split(',');
    if (contact.length<1) continue;
    name = contact[0];
    address = contact[1];
    city = contact[2];
    if (city==null) city='';
    state = contact[3];
    if (state==null) state='';
    zip = contact[4];
    if (zip==null) zip='';
    country = contact[5];
    if (country==null) country='';

    tel = contact[6];
    if (tel==null) tel='';
    email = contact[7];
    if (email==null) email='';
    note = contact[8];
    if (note==null) note='';

    description='';
    if (email!='') description=description+'<a href="mailto:'+email+'">'+email+'</a>';
    if (note!='') {
      if (description!='') description=description+'<br/>';
      description=description+note;
    };
    company = contact[8];
    if (company==null) company='';


    if (name==null) continue;
    if (address==null) continue;
    arrayContact=[
      name,
      address+', '+city+', '+state+' '+zip+', '+country,
      tel,
      description,
      company
    ];
    a[a.length]=arrayContact;
    contactCount++;
  };
  return a;
};


function MapContactsArray2XMLtext(contacts, titleIn, descriptionIn) {
  var contactsArray=contacts;
  var title=titleIn;
  //if (title==null) title='Trip Contacts';
  if (title==null) title='Contacts list';
  var description=descriptionIn;
  if (description==null) description='';
  if (title==null) title='Trip Contacts list';
  var xml='';

  xml = xml + '<?xml version="1.0" encoding="UTF-8"?>' + '\n';
  xml = xml + '<!-- <?xml-stylesheet type="text/xsl" href="Trip2Locations.xsl" ?> -->' + '\n';
  xml = xml + '<!-- <?xml-stylesheet type="text/xsl" href="Trip2Connections.xsl" ?> -->' + '\n';
  xml = xml + '<!-- <?xml-stylesheet type="text/xsl" href="Trip2Trip.xsl" ?>  -->' + '\n';
  xml = xml + '<!-- Author: Nicola Zordan 2/8/2007 11:11 Nicola@Zordan.net -->' + '\n';
  xml = xml + '<Trip Name="MappedContacts.com' + ': '+ title + '" ';
  xml = xml + ' >' + '\n';
  xml = xml + '   <Description><![CDATA['+description+']]></Description>' + '\n';
  xml = xml + '   <DetailsURL>http://www.MappedContacts.com</DetailsURL>' + '\n';
  xml = xml + '   <ImageURL></ImageURL>' + '\n';
  xml = xml + '   <Locations>' + '\n';
  
  var contactCount = 0
  var contact, name,address,tel,description,compnay;
    
  for (var i=0;i<contactsArray.length;i++) {
      contact=contactsArray[i];
      name = contact[0];
      address = contact[1];
      tel = contact[2];
      description = contact[3];
      company = contact[4];
      //cList = cList + '<li>' + name + '</li>' + '\n';
      
      xml = xml + '     <Location ';
      xml = xml + ' index="' + (i + 1) + '" ';
      xml = xml + ' Name="' + name + '" ';
      xml = xml + ' >' + '\n';
      
      xml = xml + '       <Description>';
      xml = xml + '<![CDATA[';
      xml = xml + description;
      xml = xml + ']]>';
      xml = xml + '</Description>' + '\n';
      xml = xml + '       <Address>' + address + '</Address>' + '\n';
      xml = xml + '       <Tel>' + tel + '</Tel>' + '\n';
      
      xml = xml + '     </Location>' + '\n';
      contactCount = contactCount + 1;
  };
   
  xml = xml + '   </Locations>' + '\n';
  xml = xml + '</Trip>' + '\n';

  return xml;  
};


// Override (Trip Override)

/*
var ZonePageTitle='';
function LocationsListHTML (showExtra) {
        var title=ZonePageTitle;
        if (title!='') title=title+' ';
        title=title+ZoneName;
        document.title=title;
	var html="";
	// zoom back to trip
	html=html+"<center>";
	//html=html+'<input type="button" value="Show Trip" onClick="ZoomToTrip()" />';
	html=html+'<input type="button" value="Re-Zoom" onClick="ZoomToTrip()" />';
	html=html+"</center>";
	html=html+"<br/>";
	//
	html=html+"<ol>";
	for (var i=0;i<Locations.length;i++) {
  	  html=html+"<li>";
          if (i==Locations.length-ZoneMeetingPlaces.length-1+1)
            html=html+"<b>";
          html=html+LocationListHTML(i,showExtra);
          if (i==Locations.length-ZoneMeetingPlaces.length-1+1)
            html=html+"</b>";
          //html=html+"<br/>";
          if (i==Locations.length-ZoneMeetingPlaces.length-1)
            html=html+"<br/><br/>";
	  html=html+"</li>";
        };
	html=html+"</ol>";

	html=html+"<center>";

/--*
        //-no-GetZoneRadiosTxt();
        if(ZoneRadiosTxt.length>0)
  	  html=html+'<a href="javascript:ShowRadiosMessage()"><font face="webdings" style="{text-decoration:none}">~ </font></a><a href="javascript:ShowRadiosWindow()">radios members list</a> <br/>';
*--/
	//html=html+EMailsHTML('<font face="webdings" style="{text-decoration:none}">i </font>eMail members');
	//-no-html=html+EMailsHTML('<font face="webdings" style="{text-decoration:none}">i </font>');
	//-no-html=html+'<a href="javascript:ShowEmailsWindow()">eMail members list</a>';

        //-no-html=html+'<br/><a href="ZoneDisasterPlan.htm" target="_blank">Standard Disaster Plan</a>';

	html=html+"</center>";

        //html='<small><small>'+html+'</small></small>';
        html='<small>'+html+'</small>';

	return html;
};
*/

var TripBorderColor='#FFFF00';
var ZoneBoundaries=null;
var ZonesPageMailEmail=null;
var ZonePageTitle='';
var MembersCountText='Listed';
