/* ***********************************************************************************

This file contains the functions specific to the search results template as well as IdentifyIcon
Should always be included after the common file as well as the insert script tag.

**************************************************************************************** */

//function used to display the search results
//does not start or begin table, only displays results as row
//searchResults - the location collection containing the results of the search
function mq_display_searchResults(searchResults) {
   if(!mq_ParamExists(searchResults))
      return;

   for(var count=0;count<searchResults.count;count++) {
      var loc = searchResults.getAt[count];

      document.write("<tr>");
         document.write("<td><img src=\"images/spacer.gif\" width=\"100\" height=\"10\"></td>");
         document.write("<td valign=\"top\"><img src=\"images/numbers/401_" + (loc.number) + ".gif\" width=\"20\" height=\"20\" alt=\"" + (loc.number) + "\" border=\"0\" /></td>");
        // document.write("<td width=\"216\">");
        //    mq_display_map_image_tag(loc.map,"");
        // document.write("</td>");
        // document.write("<td width=\"10\">&nbsp;</td>");
         document.write("<td valign=\"top\">");
            mq_display_address_location(loc,generateLocMapRQ(loc));
            if (mq_ParamExists(loc.distance)) {
               document.write("<td valign=\"top\" style=\"padding-left:40px;\">" + loc.distance.value + (loc.distance.units == "mi" ? " miles" : " kilometers") +  "<br><a href=\"" + generateLocMapRQ(loc) + "\" class=\"mqAnchor\">View map</a><br>");
			   document.write("<a href=\"directions.asp?destName=" + loc.name + "&destrecordId=" + loc.recordId +  "&destaddress=" + loc.address + "&destcity=" + loc.city + "&deststateProvince=" + loc.stateProvince + "&destpostalCode=" + loc.postalCode + "\" class=\"mqAnchor\">Get Directions</a></td>");
			}
            else
               document.write("<td>&nbsp;</td>");

         document.write("</tr>");

         if(count < eval(searchResults.count)-1) {
            document.write("<tr>");
               document.write("<td>&nbsp;</td>");
               document.write("<td colspan=\"3\"><br/>");
                  //document.write("<img src=\"images/rule.gif\" width=\"600\" height=\"1\" alt=\"\" border=\"0\"><br>");
               document.write("</td>");
            document.write("</tr>");
         }//end if(count < eval(searchResults.count)-1) {
      }//end for(var count=0;count<searchResults.count;count++) {
}//end function mq_display_searchResults(searchResults) {

//creates the url required to transform a location into a locmap request.
function generateLocMapRQ(loc) {
   var url = "?transaction=locmap";

   url+=createMapNV(loc.map);

   url+="&pheight=344&pwidth=574";

   url+= createLocNV(loc,"",-1);

   return url;
}

//creates name value pairs from a map object.
//NOTE: does not use height and width leaves caller to add these.
function createMapNV(map) {
   var url="";

   if (!mq_ParamExists(map))
      return url;

   if(mq_ParamExists(map.latitude) && map.latitude.length > 0)
   {
      url+="&mapLatitude="+map.latitude;
   }
   if(mq_ParamExists(map.longitude) && map.longitude.length > 0)
   {
      url+="&mapLongitude="+map.longitude;
   }
   if(mq_ParamExists(map.zoomLevel) && map.zoomLevel.length > 0)
   {
      url+="&zoomLevel="+map.zoomLevel;
   }
   if(mq_ParamExists(map.mapStyle) && map.mapStyle.length > 0)
   {
      url+="&mapStyle="+map.mapStyle;
   }

   return url;
}

//function to url_encode equal and ampersands
function urlEncoder(t)   {
   if(!mq_ParamExists(t)){return "";}
   t = t.replace("&", "%26");
   t = t.replace("=", "%3d");
   t = t.replace("#", "%23");
   return t;
}


//function used to turn a location into Name value pairs
function createLocNV(loc, prefix, num) {
   var url = "";
   var s;

   if(mq_ParamExists(loc) != true)
      return url;

   if(mq_ParamExists(loc.name) && loc.name.length > 0)
   {
      url+="&";
      if (prefix != "")
         url+=prefix;

      url+="name";

      if (num > -1)
         url+=num;
       s=loc.name;
       s = s.replace("&#39","'");
       s = s.replace("&#039;","'");
      url+="=" + urlEncoder(s);
   }
   if(mq_ParamExists(loc.address) && loc.address.length > 0)
   {
      url+="&";
      if (prefix != "")
         url+=prefix;

      url+="address";

      if (num > -1)
         url+=num;

      url+="=" + urlEncoder(loc.address);
   }
   if(mq_ParamExists(loc.city) && loc.city.length > 0)
   {
      url+="&";
      if (prefix != "")
         url+=prefix;

      url+="city";

      if (num > -1)
         url+=num;

      url+="=" + urlEncoder(loc.city);
   }
   if(mq_ParamExists(loc.stateProvince) && loc.stateProvince.length > 0)
   {
      url+="&";
      if (prefix != "")
         url+=prefix;

      url+="stateProvince";

      if (num > -1)
         url+=num;

      url+="=" + loc.stateProvince;
   }
   if(mq_ParamExists(loc.country) && loc.country.length > 0)
   {
      url+="&";
      if (prefix != "")
         url+=prefix;

      url+="country";

      if (num > -1)
         url+=num;

      url+="=" + loc.country;
   }
   if(mq_ParamExists(loc.latitude) && loc.latitude.length > 0)
   {
      url+="&";
      if (prefix != "")
         url+=prefix;

      url+="latitude";

      if (num > -1)
         url+=num;

      url+="=" + loc.latitude;
   }
   if(mq_ParamExists(loc.longitude) && loc.longitude.length > 0)
   {
      url+="&";
      if (prefix != "")
         url+=prefix;

      url+="longitude";

      if (num > -1)
         url+=num;

      url+="=" + loc.longitude;
   }
   /*
   if(mq_ParamExists(loc.iconId) && loc.iconId.length > 0)
   {
      url+="&";
      if (prefix != "")
         url+=prefix;

      url+="iconid";

      if (num > -1)
         url+=num;

      url+="=" + loc.iconId;
   }
   */
   if((mq_ParamExists(loc.userFields)) && (mq_ParamExists(loc.userFields.user1)) && (loc.userFields.user1.length > 0))
   {
      url+="&";
      if (prefix != "")
         url+=prefix;

      url+="user1";

      if (prefix == "poi_")
         url+="_";

      if (num > -1)
         url+=num;

      url+="=" + loc.userFields.user1;

   }

   return url;
}//end function createLocNV(loc, prefix, num) {

//function used to output next and previous links
function mq_output_next_prev(av) {
   if(!mq_ParamExists(av))
      return;
   document.write("<td width=\"20\"><img src=\"images/spacer.gif\" width=\"20\" height=\"5\"></td>");
   document.write("<td colspan=\"2\">");

document.write ("<div style=\"width:720px;\">");
   document.write ("<div class=\"prevLink\" >&nbsp; ");

   if(mq_ParamExists(av.prevData)) {
      document.write ("<a class=\"mqAnchor\" href=\"\" id=\"mqPrev\" onClick=\"addRevertState('" + av.prevData + "','mqPrev');\">&lt;&nbsp;Previous</a>");
   }

   document.write (" </div>");
   document.write ("<div class=\"backToTop\"> ");

      document.write (" [ <a class=\"mqAnchor\" href=\"#identifier\">Back to Top</a> ] ");

   document.write (" </div>");
   document.write ("<div class=\"nextLink\"> ");

   if(mq_ParamExists(av.nextData)) {
      document.write("<a href=\"\" class=\"mqAnchor\" id=\"mqNext\" onClick=\"addRevertState('" + av.nextData + "','mqNext',av);\" >Next&nbsp;&gt;</a>");
   }

   document.write (" &nbsp;</div>");
   document.write ("<div style=\"clear: both\"> </div>");
   document.write ("</div>");
   document.write("</td>");

}//end function mq_output_next_prev(av) {
function addRevertState(url,direction,av){
 url = url+"&revertState="+getElementById("revertState").value+getElementById("mqSearchUrl").value;
 url = url + "&mapSessionId="+getElementById("mapSessionId").value;
 url = mqurlencode(url);
 url = "template=map_search&url=" + url;
 getElementById(direction).href="?"+url;

}
