//-------------------------------
function AdjustLineBoxDisplay()
{
//along the top  
  for (i=0;i<5;i++)
  {
  eval('cursorDiv_'+i+'0.style.left = mzjzLineBoxLeft+dx*i');
  eval('cursorDiv_'+i+'0.style.top = mzjzLineBoxTop');
  }
// along the bottom  
  for (i=0;i<5;i++)
  {
  eval('cursorDiv_'+i+'4.style.left = mzjzLineBoxLeft+dx*i');
  eval('cursorDiv_'+i+'4.style.top = eventClientY');
  }
//down the far side  
  for (i=1;i<4;i++)
  {
  eval('cursorDiv_4'+i+'.style.left = eventClientX');
  eval('cursorDiv_4'+i+'.style.top = mzjzLineBoxTop+dy*i');
  }
//down the near side  
  for (i=1;i<4;i++)
  {
  eval('cursorDiv_0'+i+'.style.left = mzjzLineBoxLeft');
  eval('cursorDiv_0'+i+'.style.top = mzjzLineBoxTop+dy*i');
  }
}  
//-------------------------------
function UpdateLineBoxDisplay(visibility)
{
      for (i=0;i<5;i++)eval('cursorDiv_'+i+'0').style.visibility=visibility;
      for (i=0;i<5;i++)eval('cursorDiv_'+i+'4').style.visibility=visibility;
      for (i=0;i<4;i++)eval('cursorDiv_4'+i).style.visibility=visibility;
      for (i=0;i<4;i++)eval('cursorDiv_0'+i).style.visibility=visibility;
}
//--------------------------------
function mapMouseDown() {

  var x =  eventClientX+document.body.scrollLeft;
  var y =  eventClientY+document.body.scrollTop;
  mzjsMouseClickX = x
  mzjsMouseClickY = y

  if (mzjzCreatingLineBox)
      {
      mzjzLineBoxRight = mzjsMouseClickX;
      mzjzLineBoxBottom = mzjsMouseClickY;

      UpdateLineBoxDisplay('hidden')
      blobDiv.style.left=mzjzLineBoxLeft;
      blobDiv.style.top=mzjzLineBoxTop;
      blobDiv.style.width=mzjzLineBoxRight-mzjzLineBoxLeft;
      blobDiv.style.height=mzjzLineBoxBottom-mzjzLineBoxTop;
      blobDiv.style.visibility='visible';

      
      mzjzCreatingLineBox=false;
      return;
      }

  if (mzjsNoAddSpotMode)
      {
      showCategoryBox();
      return;
      }

//--alert('mapMouseDown - calling serverside scripts')

  var systemTime= new Date() ;
  universalTime = systemTime.getTime();
      
  xSpotDiv.style.visibility = "visible";
  xSpotDiv.style.left = x-18 ;
  xSpotDiv.style.top = y-18 ;
  xSpotDiv.style.zIndex=100 ;

  borderRepositioningX = 11+50
  borderRepositioningY = 11+50
  
  x = x - borderRepositioningX
  y = y - borderRepositioningY

  
  if (x>360) x=370
  
if (eventCtrlKey)  
{
      addLinkToMap(x,y);
}
else
{
      addSpot(x,y);
}    
}

//-----------------------------------------------------------
function hideCatBox()
{
    linkMapDiv.style.visibility = 'hidden'
}
//-----------------------------------------------------------
function showCategoryBox()
{
//alert('showCategoryBox - used for click on the map')

    txt = showCategoryBoxTxt()
    
    document.all['linkMapDiv'].innerHTML = txt
    linkMapDiv.style.position = 'absolute';
    linkMapDiv.style.top =  ''+eventClientY;
    linkMapDiv.style.left = ''+eventClientX;
    linkMapDiv.style.visibility = 'visible'; 
    linkMapDiv.style.zIndex = 200;
}
//-----------------------------------------------------------
function startLineBox()
{
    
    mzjzLineBoxLeft = mzjsMouseClickX;
    mzjzLineBoxTop = mzjsMouseClickY;
    
    
    mzjzCreatingLineBox=true;
    
    UpdateLineBoxDisplay('visible');
    AdjustLineBoxDisplay();
    
    blobDiv.style.visibility='hidden';

    hideCatBox();
}
//-----------------------------------------------------------
function showCategoryBoxTxt()
{
//alert('showCategoryBoxTxt - Generate popup box from mzjsMapPopup')

    
    txt=''
    txt += '<table bgcolor="#cccccc" border="2" cellpadding=0 cellspacing=0>'
    txt += '<tr><td valign="top" ' + mzjsMapPopupTableStyle + '>'
    
    numOfRows=0
    for (i=0;i<mzjsMapPopup.length;i++)
        {
        listEntry=mzjsMapPopup[i]
        
        listEntryEnd=listEntry.length
        DollarPosition = listEntry.indexOf('$')
        if (DollarPosition >-1) 
            {
            NextDollarPosition = DollarPosition+listEntry.slice(DollarPosition+1,listEntryEnd).indexOf('$')
                if (NextDollarPosition >-1)
                    {
                    listEntry1=listEntry.slice(0,DollarPosition)
                    listEntry2=listEntry.slice(DollarPosition+1,NextDollarPosition+1)
                    listEntry3=listEntry.slice(NextDollarPosition+2,listEntryEnd)
                    listEntry=listEntry1 +eval(listEntry2)+listEntry3
                    }
            }
            
        if (((numOfRows+1-Math.floor((numOfRows+1)/10)*10)==1)&&(numOfRows>0))
            {txt += '</td><td valign="top" ' + mzjsMapPopupTableStyle + '>'}
        txt += listEntry + '<br/>'
        numOfRows++;
        }
    
    
    txt += '</td></tr>'
    txt += '</table>'
    
    return txt
}
//------------------------------------------------------
function dispNoPtsMsg() {
      txt= '<div ID="KEY">'
      txt=txt +  '<table  border=0  cellpadding=2  cellspacing=0><tr><td>'     
      txt=txt +  '<font face="arial"  color="black">'
      txt=txt +  'No spots on this map.</font>'     
      txt=txt +  '</td></tr><table></div>'

      document.all['keyDiv'].innerHTML = txt
      keyDiv.style.visibility = "visible"; 
      keyDiv.style.zIndex = 100;
      return;    
}
function addSpot(x,y)
{
      txt = newSpotDivTxt(x,y,MapIndexName)
      document.all['newSpotDiv'].innerHTML = txt
      newSpotDiv.style.position = 'absolute';
      newSpotDiv.style.top = "300px";
      newSpotDiv.style.left = "500px";
      newSpotDiv.style.visibility = "visible"; 
      newSpotDiv.style.zIndex = 200;
}

function addLinkToMap(x,y)
{
      txt = linkMapDivTxt(x,y,MapIndexName)
      document.all['linkMapDiv'].innerHTML = txt
      linkMapDiv.style.position = 'absolute';
      linkMapDiv.style.top = "150px";
      linkMapDiv.style.left = "500px";
      linkMapDiv.style.visibility = "visible"; 
      linkMapDiv.style.zIndex = 200;
}

function newSpotDivTxt(x,y,mapIndexName)
{
txt=''
txt += '<form name="newSpotForm" action="' +cgiDirectory+ 'addSpot.py" method="get">'
txt += '<table bgcolor="cccccc">'
txt += '<tr>'
txt += '<td style="background-color:\'ffffff\'" colspan="2">Add spot</td>'
txt += '<td><input type="button" style="background-color:\'cc8888\'; cursor:hand" value="Cancel" name= "next1" '
txt += 'onclick="javascript:newSpotDiv.style.visibility = \'hidden\';"></td>'
txt += '<td><input type="button" style="background-color:\'cc8888\'; cursor:hand" value="Save" name= "next2" '
txt += 'onclick="javascript:document.forms.newSpotForm.submit();"></td>'
txt += '</tr>'
txt += '<tr><td>'
txt += 'Name:</td><td colspan="3"><input type="text" size="40" value="" name="Name" id="Name">'
txt += '<tr><td>'
txt += 'Url:</td><td colspan="3"><input type="text" size="40" value="" name="Url" id="Url">'
txt += '<tr><td>'
txt += 'Class:</td><td colspan="3"><input type="text" size="20" value="General" name="Class" id="Class"'
txt += '>&nbsp;&nbsp;Group Leader?<input type="checkbox" value="yes" name="IsGroup" id="IsGroup">'
txt += '</td></tr>'
txt += '<tr><td>'
txt += '<input type="hidden" value="'+ x.toString() + '" name="X" id="X"'
txt += '><input type="hidden" value="'+ y.toString() + '" name="Y" id="Y"'
txt += '><input type="hidden" value="' +mzDataSet+ '"name="mzDataSet" id="mzDataSet"'
txt += '><input type="hidden" value="point" name="spotType" id="spotType"'
txt += '><input type="hidden" value="'+ mapIndexName + '" name="MapIndexName" id="MapIndexName"> '
txt += '</td></tr><tr><td>'
txt += 'Icon:</td><td colspan="3">'
txt += '<select name="Icon" id="Icon">'
txt += '<option>spotR.gif'
txt += '<option>spotB.gif'
txt += '<option>fiat.gif'
txt += '<option>sportscar.gif'
txt += '<option>Custom'
txt += '</select>'
txt += '</td></tr><tr><td>'
txt += 'Custom:</td><td colspan="3"><input type="text" size="40" value="" name="Custom" id="Custom"> '
txt += '</td>'
txt += '</tr>'
txt += '</table>'
txt += '</form>'
return txt
}

function linkMapDivTxt(x,y,mapIndexName)
{
txt=''
txt += '<form name="linkMapForm" action="' +cgiDirectory+ 'addSpot.py" method="get">'
txt += '<table bgcolor="cccccc">'
txt += '<tr>'
txt += '<td style="background-color:\'ffffff\'" colspan="2">Link new map to current map</td>'
txt += '<td><input type="button" style="background-color:\'cc8888\'; cursor:hand" value="Cancel" name= "next1" '
txt += 'onclick="javascript:linkMapDiv.style.visibility = \'hidden\';"></td>'
txt += '<td><input type="button" style="background-color:\'cc8888\'; cursor:hand" value="Save" name= "next2" '
txt += 'onclick="javascript:document.forms.linkMapForm.submit();"></td>'
txt += '</tr>'
txt += '<tr>'
txt += '<td>'
txt += 'Name:</td><td colspan="3"><textarea value="" name="Name" id="Name"></textarea>'
txt += '</td></tr><tr><td>'
txt += 'X: </td><td><input type="text" size="6" value="'+ x.toString() + '" name="X" id="X"> </td><td colspan="2">'
txt += 'Y: </td><td><input type="text" size="6" value="'+ y.toString() + '" name="Y" id="Y"> '
txt += '</td></tr><tr><td>'
txt += 'Leaf map:</td><td colspan="3"><input type="text" size="24" value="None" name="Url" id="Url" '
txt += '><input type="hidden" size="24"  value="' +mzDataSet+ '" name="mzDataSet" id="mzDataSet" '
txt += '><input type="hidden" value="leafMap" name="spotType" id="spotType"'
txt += '><input type="hidden" size="24"  value="' +MapIndexName+ '" name="MapIndexName" id="MapIndexName"> '
txt += '</td></tr><tr><td>'
txt += 'Icon:</td><td colspan="3">'
txt += '<select name="Icon" id="Icon">'
txt += '<option>spotG.gif'
txt += '<option>spotB.gif'
txt += '<option>fiat.gif'
txt += '<option>sportscar.gif'
txt += '<option>Custom'
txt += '</select>'
txt += '</td></tr><tr><td>'
txt += 'Custom:</td><td colspan="3"><input type="text" size="24" value="" name="Custom" id="Custom"> '
txt += '</td>'
txt += '</tr>'
txt += '</table>'
txt += '</form>'
return txt
}
