/* ***********************************************************************************

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,loc1) {
   if(!mq_ParamExists(searchResults))
      return;
      var loc2 = searchResults.getAt[0];
      document.write("<tr><td colspan=\"6\" style=\"padding-left:23px\">");


     if (mq_ParamExists(loc2.searchFields))  
{
        document.write("<span class=\"address\"><b>Search Criteria (Program Services):&nbsp;</b></span>");
}


     else
{
     if (mq_ParamExists(loc2.textFields))
{
     if ((loc2.textFields.text8 == 1) || (loc2.textFields.text60 == 1) ||
         (loc2.textFields.text61 == 1) || (loc2.textFields.text62 == 1))

     document.write("<span class=\"address\"><b>Search Criteria (Program Services):&nbsp;</b></span>");

//	 if (loc2.textFields.text63 == 1) document.write("<span class=\"address\"><b>Search Criteria (Program Services):&nbsp;</b></span>");
}
}
     if (mq_ParamExists(loc2.searchFields))  {
      if (loc2.searchFields.search1 == 1) document.write("<span class=\"address\">Substance Abuse Treatment Programs;&nbsp;</span>");
      if (loc2.searchFields.search2 == 1) document.write("<span class=\"address\">Support Groups;&nbsp;</span>");
      if (loc2.searchFields.search3 == 1) document.write("<span class=\"address\">Conflict Resolution/Mediation;&nbsp;</span>");
      if (loc2.searchFields.search4 == 1) document.write("<span class=\"address\">Housing Programs;&nbsp;</span>");
      if (loc2.searchFields.search5 == 1) document.write("<span class=\"address\">Parole and Probation Offices;&nbsp;</span>");
      if (loc2.searchFields.search6 == 1) document.write("<span class=\"address\">Primary/Secondary Public Schools;&nbsp;</span>");
      if (loc2.searchFields.search7 == 1) document.write("<span class=\"address\">Victim Services;&nbsp;</span>");
      if (loc2.searchFields.search8 == 1) document.write("<span class=\"address\">Legal Assistance;&nbsp;</span>");
      if (loc2.searchFields.search9 == 1) document.write("<span class=\"address\">Job Readiness/Adult Education;&nbsp;</span>");
      if (loc2.searchFields.search10 == 1) document.write("<span class=\"address\">Substance Abuse Prevention/Recovery Programs;&nbsp;	      </span>");
      if (loc2.searchFields.search12 == 1) document.write("<span class=\"address\">Health Services;&nbsp;</span>");
      if (loc2.searchFields.search13 == 1) document.write("<span class=\"address\">Mental Health Services;&nbsp;</span>");
      if (loc2.searchFields.search14 == 1) document.write("<span class=\"address\">Family Assistance Programs;&nbsp;</span>");
      if (loc2.searchFields.search15 == 1) document.write("<span class=\"address\">Food Assistance Programs;&nbsp;</span>");
      if (loc2.searchFields.search16 == 1) document.write("<span class=\"address\">Senior Citizen Resources;&nbsp;</span>");
      if (loc2.searchFields.search17 == 1) document.write("<span class=\"address\">Needle Exchange Programs;&nbsp;</span>");
      if (loc2.searchFields.search18 == 1) document.write("<span class=\"address\">Disability Services;&nbsp;</span>");
      if (loc2.searchFields.search19 == 1) document.write("<span class=\"address\">HIV/STD Testing Sites;&nbsp;</span>");
      if (loc2.searchFields.search20 == 1) document.write("<span class=\"address\">Other HIV/AIDS Related Care Services;&nbsp;</span>");
   }

     if (mq_ParamExists(loc2.textFields))  
{
      if (loc2.textFields.text8 == 1) document.write("<span class=\"address\">Buprenorphine Certified Physicians;&nbsp;</span>");
	  if (loc2.textFields.text60 == 1) document.write("<span class=\"address\">Department of Juvenile Services Offices;&nbsp;</span>");
	  if (loc2.textFields.text61 == 1) document.write("<span class=\"address\">Public Internet Access Sites;&nbsp;</span>");
	  if (loc2.textFields.text62 == 1) document.write("<span class=\"address\">Parks and Recreational Sites;&nbsp;</span>");
}




   if (mq_ParamExists(loc2.textFields)) {
      if ((loc2.textFields.text1 == 1) || (loc2.textFields.text2 == 1) ||
          (loc2.textFields.text3 == 1) || (loc2.textFields.text4 == 1) ||
          (loc2.textFields.text5 == 1) || (loc2.textFields.text6 == 1) ||
          (loc2.textFields.text7 == 1) || (loc2.textFields.text8 == 1) ||
          (loc2.textFields.text9 == 1) || (loc2.textFields.text10 == 1) ||
          (loc2.textFields.text11 == 1) || (loc2.textFields.text12 == 1) ||
          (loc2.textFields.text13 == 1) || (loc2.textFields.text14 == 1) ||
          (loc2.textFields.text15 == 1) || (loc2.textFields.text16 == 1) ||
          (loc2.textFields.text17 == 1) || (loc2.textFields.text18 == 1) ||
          (loc2.textFields.text19 == 1) || (loc2.textFields.text20 == 1) ||
          (loc2.textFields.text21 == 1) || (loc2.textFields.text22 == 1) ||
          (loc2.textFields.text23 == 1) || (loc2.textFields.text24 == 1) ||
          (loc2.textFields.text25 == 1) || (loc2.textFields.text26 == 1) ||
          (loc2.textFields.text27 == 1) || (loc2.textFields.text28 == 1) ||
          (loc2.textFields.text31 == 1) || (loc2.textFields.text32 == 1) ||
          (loc2.textFields.text33 == 1) || (loc2.textFields.text34 == 1) ||
          (loc2.textFields.text35 == 1) || (loc2.textFields.text36 == 1) ||
          (loc2.textFields.text37 == 1) || (loc2.textFields.text38 == 1) ||
          (loc2.textFields.text39 == 1) || (loc2.textFields.text40 == 1) ||
          (loc2.textFields.text41 == 1) || (loc2.textFields.text42 == 1) ||
          (loc2.textFields.text43 == 1) || (loc2.textFields.text44 == 1) ||
          (loc2.textFields.text45 == 1) || (loc2.textFields.text46 == 1) ||
          (loc2.textFields.text47 == 1) || (loc2.textFields.text48 == 1) ||
          (loc2.textFields.text49 == 1) || (loc2.textFields.text50 == 1) ||
          (loc2.textFields.text51 == 1) || (loc2.textFields.text52 == 1) ||
          (loc2.textFields.text54 == 1) || (loc2.textFields.text55 == 1) ||
          (loc2.textFields.text56 == 1) || (loc2.textFields.text57 == 1) ||
          (loc2.textFields.text58 == 1) || (loc2.textFields.text59 == 1))


      document.write("<br/><br/><span class=\"address\"><b>Other Search Criteria:&nbsp;</b></span>");

      if (loc2.textFields.text1 == 1) document.write("<span class=\"address\">Detox-Outpatient;&nbsp;</span>");
      if (loc2.textFields.text2 == 1) document.write("<span class=\"address\">Detox-Inpatient;&nbsp;</span>");
      if (loc2.textFields.text3 == 1) document.write("<span class=\"address\">Intensive/Standard Outpatient;&nbsp;</span>");
      if (loc2.textFields.text4 == 1) document.write("<span class=\"address\">Low Intensity Residential (Halfway House);&nbsp;</span>");
      if (loc2.textFields.text5 == 1) document.write("<span class=\"address\">Short term Residential;&nbsp;</span>");
      if (loc2.textFields.text6 == 1) document.write("<span class=\"address\">Long term Residential;&nbsp;");
      if (loc2.textFields.text7 == 1) document.write("<span class=\"address\">Methadone Assisted;&nbsp;</span>");
      if (loc2.textFields.text8 == 1) document.write("<span class=\"address\">Buprenorphine Assisted;&nbsp;</span>");
      if (loc2.textFields.text9 == 1) document.write("<span class=\"address\">No Fee;&nbsp;</span>");
      if (loc2.textFields.text10 == 1) document.write("<span class=\"address\">Sliding Scale;&nbsp;</span>");
      if (loc2.textFields.text11 == 1) document.write("<span class=\"address\">Medicaid;&nbsp;</span>");
      if (loc2.textFields.text12 == 1) document.write("<span class=\"address\">Medicare;&nbsp;</span>");
      if (loc2.textFields.text13 == 1) document.write("<span class=\"address\">Private Insurance;&nbsp;</span>");
      if (loc2.textFields.text14 == 1) document.write("<span class=\"address\">Females Only;&nbsp;</span>");
      if (loc2.textFields.text15 == 1) document.write("<span class=\"address\">Males Only;&nbsp;</span>");
      if (loc2.textFields.text16 == 1) document.write("<span class=\"address\">Children/Youth;&nbsp;</span>");
      if (loc2.textFields.text17 == 1) document.write("<span class=\"address\">Adults;&nbsp;</span>");
      if (loc2.textFields.text18 == 1) document.write("<span class=\"address\">Families;&nbsp;</span>");
      if (loc2.textFields.text19 == 1) document.write("<span class=\"address\">Women and Children;&nbsp;</span>");
      if (loc2.textFields.text20 == 1) document.write("<span class=\"address\">Homeless;&nbsp;</span>");
      if (loc2.textFields.text21 == 1) document.write("<span class=\"address\">Clients with HIV/AIDS;&nbsp;</span>");
      if (loc2.textFields.text22 == 1) document.write("<span class=\"address\">Clients with Mental Illness;&nbsp;</span>");
      if (loc2.textFields.text23 == 1) document.write("<span class=\"address\">Clients with Developmental Disabilities;&nbsp;</span>");
      if (loc2.textFields.text24 == 1) document.write("<span class=\"address\">LGBT Programs;&nbsp;</span>");
      if (loc2.textFields.text25 == 1) document.write("<span class=\"address\">Multi-lingual Services;&nbsp;</span>");
      if (loc2.textFields.text26 == 1) document.write("<span class=\"address\">On-site Child Care;&nbsp;</span>");
      if (loc2.textFields.text27 == 1) document.write("<span class=\"address\">Public Transportation Accessible;&nbsp;</span>");
      if (loc2.textFields.text28 == 1) document.write("<span class=\"address\">Ex-Offenders;&nbsp;</span>");
      if (loc2.textFields.text31 == 1) document.write("<span class=\"address\">Sex Offenders;&nbsp;</span>");
      if (loc2.textFields.text32 == 1) document.write("<span class=\"address\">Women with Adolescent Males;&nbsp;</span>");
      if (loc2.textFields.text33 == 1) document.write("<span class=\"address\">Men with Children;&nbsp;</span>");
      if (loc2.textFields.text34 == 1) document.write("<span class=\"address\">Clients with Dual Diagnosis;&nbsp;</span>");
      if (loc2.textFields.text35 == 1) document.write("<span class=\"address\">Clients with Substance Abuse Issues;&nbsp;</span>");
      if (loc2.textFields.text36 == 1) document.write("<span class=\"address\">Maryland Medical Assistance (i.e. MHIP, PAC);&nbsp;</span>");
      if (loc2.textFields.text37 == 1) document.write("<span class=\"address\">Veterans Programs;&nbsp;</span>");
      if (loc2.textFields.text38 == 1) document.write("<span class=\"address\">Programs for Immigrants;&nbsp;</span>");
      if (loc2.textFields.text39 == 1) document.write("<span class=\"address\">Disabled Accessible;&nbsp;</span>");
      if (loc2.textFields.text40 == 1) document.write("<span class=\"address\">Anonymous Testing;</span>");
      if (loc2.textFields.text41 == 1) document.write("<span class=\"address\">Rapid Testing;&nbsp;</span>");
      if (loc2.textFields.text42 == 1) document.write("<span class=\"address\">Oral Testing;&nbsp;</span>");
      if (loc2.textFields.text43 == 1) document.write("<span class=\"address\">Testing for Other STDs;&nbsp;</span>");
      if (loc2.textFields.text44 == 1) document.write("<span class=\"address\">Child Victimization;&nbsp;</span>");
      if (loc2.textFields.text45 == 1) document.write("<span class=\"address\">Elder Abuse;&nbsp;</span>");
      if (loc2.textFields.text46 == 1) document.write("<span class=\"address\">Dating/Domestic Violence;&nbsp;</span>");
      if (loc2.textFields.text47 == 1) document.write("<span class=\"address\">Sex Crimes (i.e. Sexual Assault, Harassment);&nbsp;</span>");
      if (loc2.textFields.text48 == 1) document.write("<span class=\"address\">Attempted Murder/Homicide;&nbsp;</span>");
      if (loc2.textFields.text49 == 1) document.write("<span class=\"address\">Robbery/Burglary;&nbsp;</span>");
      if (loc2.textFields.text50 == 1) document.write("<span class=\"address\">Assault;&nbsp;</span>");
      if (loc2.textFields.text51 == 1) document.write("<span class=\"address\">Other Violent Crime;&nbsp;</span>");
      if (loc2.textFields.text52 == 1) document.write("<span class=\"address\">Non Violent Crime;&nbsp;</span>");
      if (loc2.textFields.text54 == 1) document.write("<span class=\"address\">Programs for Male Victims;&nbsp;</span>");
      if (loc2.textFields.text55 == 1) document.write("<span class=\"address\">Drop-in Center;&nbsp;</span>");
      if (loc2.textFields.text56 == 1) document.write("<span class=\"address\">Emergency/Overnight Shelter;&nbsp;</span>");
      if (loc2.textFields.text57 == 1) document.write("<span class=\"address\">Transitional Housing;&nbsp;</span>");
      if (loc2.textFields.text58 == 1) document.write("<span class=\"address\">Supportive Permanent Housing;&nbsp;</span>");
      if (loc2.textFields.text59 == 1) document.write("<span class=\"address\">Inclement Weather Shelter;&nbsp;</span>");


   }
      document.write("<br/><br/><br/><br/></td></tr>");

//      document.write("<br/><br/>");
   for(var count=0;count<searchResults.count;count++) {
      var loc = searchResults.getAt[count];
      document.write("<tr valign=\"top\">");
         document.write("<td><img src=\"images/spacer.gif\" width=\"10\" height=\"10\"></td>");
         document.write("<td valign=\"top\"><img src=\"images/numbers/403_" + (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 width=\"350px\" valign=\"top\">");
            mq_display_address_location(loc,generateLocMapRQ(loc),loc1);
            if (mq_ParamExists(loc.distance))
               document.write("<td valign=\"top\" style=\"padding-left:0px;font-family:arial;font-size:90%;\">" + loc.distance.value + (loc.distance.units == "mi" ? " miles" : " kilometers") +  "</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=\"5\"><br/>");
            document.write("<td>&nbsp;</td>");
            document.write("</tr>");
            document.write("<tr>");
               document.write("<td>&nbsp;</td>");
               document.write("<td colspan=\"5\"><br/>");
                  document.write("<img src=\"images/rule.gif\" width=\"700\" height=\"1\" alt=\"\" border=\"0\"><br>");
                  document.write("<br/>");
               document.write("</td>");
            document.write("</tr>");
            document.write("<tr>");
            document.write("<td>&nbsp;</td>");
            document.write("<td colspan=\"5\"><br/>");
            document.write("<td>&nbsp;</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&iconId=34";

   url+=createMapNV(loc.map);

   url+="&pheight=324&pwidth=504";

   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");
   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;

   if(mq_ParamExists(av.prevData)) {
   if(mq_ParamExists(av.nextData)) {
      document.write("<td width=\"10\">");
      document.write ("&nbsp;");
      document.write("</td>");
      document.write("<td width=\"610\">");
      document.write ("<a class=\"body\" href=\"\" id=\"mqPrev\" onClick=\"addRevertState('" + av.prevData + "','mqPrev');\">&lt;&nbsp;Previous</a>");
      document.write("</td>");
      document.write("<td width=\"50\" align=\"left\">");
      document.write("<a class=\"body\" href=\"\"  id=\"mqNext\" onClick=\"addRevertState('" + av.nextData + "','mqNext',av);\" >Next&nbsp;&gt;</a>");
      document.write("</td>");
}
}

   if(mq_ParamExists(av.prevData)) {
   if(!mq_ParamExists(av.nextData)) {
      document.write("<td width=\"10\">");
      document.write ("&nbsp;");
      document.write("</td>");
      document.write("<td width=\"610\">");
      document.write ("<a class=\"body\" href=\"\" id=\"mqPrev\" onClick=\"addRevertState('" + av.prevData + "','mqPrev');\">&lt;&nbsp;Previous</a>");
      document.write("</td>");
      document.write("<td width=\"100\" align=\"left\">");
      document.write ("<a class=\"body\" href=\"#identifier\">Back to Top</a>");
      document.write("</td>");
}
}

   if(mq_ParamExists(av.nextData)) {
   if(!mq_ParamExists(av.prevData)) {
      document.write("<td width=\"10\">");
      document.write ("&nbsp;");
      document.write("</td>");
      document.write("<td width=\"630\">");
      document.write ("<a  class=\"body\" href=\"#identifier\">Back to Top</a>");
      document.write("</td>");
      document.write("<td width=\"50\" align=\"left\">");
      document.write("<a class=\"body\" href=\"\"  id=\"mqNext\" onClick=\"addRevertState('" + av.nextData + "','mqNext',av);\" >Next&nbsp;&gt;</a>");
      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;

}
