/**
 * Javascript functions used in dealerMap.ftl. The functions are highly coupled to the global 
 * variables defined in dealerMap.ftl. Hence these functions should be changed with care.
 * TODO If properly designed, similar functions can be used for dealerMap.ftl and drivingDirectionsMap.ftl
 * @author Anshuman Singh(c) Versata 2006
 */  
  
  function constructURL()
  {
    // The url for dealer map has following parameters
    // BaseURL: http://www.multimap.com/clients/gif.cgi
    // client:  dealerdirect
    // scale:   The scale values that comes along with map xml
    // iconX,iconY: The positions where the icons are to be placed
    // width:   The width of the desired image
    // height:  The height of the deasired image
    // panX:    If the image is to be displaced horizontally. The values can be ... -1, 0, 1 ...
    // panY:    If the image is to be displaced vertically. The values can be   ... -1, 0, 1 ...
    var errorDiv = document.getElementById("errorDiv");
    errorDiv.innerHTML = "";
    var mapURL = baseURL + '?icon=' + dealerIcon + '&amp;client='+clientVal+'&amp;scale='+scales[zoomLevel]+'&amp;x='+iconX+'&amp;y='+iconY+'&amp;width='+width+'&amp;height='+height+'&amp;panx='+panx+'&amp;pany='+pany;
    var mapImage = document.getElementById("dealerMapImage");
    mapImage.src = mapURL;
  }

  function moveUp()
  {
    pany+=1;
    constructURL();  
  }
  
  function moveDown()
  {
    pany-=1;
    constructURL();  
  }  

  function moveLeft()
  {
    panx-=1;
    constructURL();  
  }
  
  function moveRight()
  {
    panx+=1;
    constructURL();  
  }

  function zoomtolevel(level,position)
  {
    if(level < 1)
    {
      level = 1;
    }
    if(level > scales.length-1)
    {
      level = scales.length-1;
    }
    zoomLevel = level;
    constructURL();
  }

  function zoomOut()
  {
    if(zoomLevel == scales.length-1)
    {
      var errorDiv = document.getElementById("errorDiv");
      errorDiv.innerHTML = "Cannot zoom out any furthur";
      return;
    }
    zoomLevel++;
    constructURL();  
  }
  
  function zoomIn()
  {
    if(zoomLevel == 1)
    {
      var errorDiv = document.getElementById("errorDiv");
      errorDiv.innerHTML = "Cannot zoom in any furthur";
      return;
    }
    zoomLevel--;
    constructURL();  
  }
  
  function populateDealerLocation(street,city,state,postalCode)
  {
    document.forms['mmDrivingDirectionsForm']['dealerLocation.street1'].value=street;
    document.forms['mmDrivingDirectionsForm']['dealerLocation.city'].value=city;        
    document.forms['mmDrivingDirectionsForm']['dealerLocation.state'].value=state;
    document.forms['mmDrivingDirectionsForm']['dealerLocation.postalCode'].value=postalCode;       
  }
  
  function createPrintPage(dealerName)
  {
    var popUpWin = window.open('','_blank','height=500,width=450,menubar=0,titlebar=0,scrollbars=1');
    if(!popUpWin)
    {
      alert("You may be using a popup blocker. Please disable it to view print page.");
      return;
    }
    
    var printPage = '<html><head><link rel="stylesheet" type="text/css" media="all" href="' + vfsBase + '/apps/css/mapPrint.css"></head><body>';
    printPage += '<div class="dealerLocationPrint">';
    printPage += '<div class="dealerName">'+dealerName+'</div>';
    printPage += '<div class="dealerAddress">'+document.getElementById('dealerPrimaryAddress').innerHTML+'</div>';
    printPage += '<img class="dealerImage" src="'+document.getElementById('dealerMapImage').src+'"/>';
    printPage += '<div class="printIcon"><a href="javascript:void();" onclick="window.print();">Print</a></div>';
    printPage += '</div>';
    printPage += '</body></html>';
    popUpWin.document.open();
    popUpWin.focus();
    popUpWin.document.write(printPage);
    popUpWin.document.close();
  }
