﻿function fill_q(querystring) {
    var the_url;
    the_url = "http://discovery.cor.gov/public/cvb/cvbweb.nsf/getOtherBusinessTypesByIndustry?openagent&q=Shopping";
    $('search_by_label').innerHTML = "Select Type";


    new Ajax.Request('request_proxy.aspx',
           {
               method: "get",
               parameters: {
                   url: the_url
               },
               onSuccess: function(transport) {
                   var op;
                   var resp = transport.responseText || "no response text";
                   resp = resp.replace("[", "{");
                   resp = resp.replace("]", "}");
                   var obj = resp.evalJSON();
                   if (obj.response == "success") {

                       var list = Object.values(obj.otherbusinesstypes);
                       /*
                       op = document.createElement('option');
                       op.text = "All";
                       op.value = "all";
        
                       try {
                       $('q').add(op, null);
                       } catch (ex) {
                       $('q').add(op);
                       }
                       */
                       var li = -1;
                       var si = 0;
                       list.each(function(s) {
                           li++;
                           if (li == 0 && querystring == "") {
                               si = li;
                               querystring = s;
                           }
                           if (s.toLowerCase() == querystring.toLowerCase()) {
                               si = li;
                           }
                           op = document.createElement('option');
                           op.text = s;
                           op.value = s;
                           try {
                               $('q').add(op, null);
                           } catch (ex) {
                               $('q').add(op);
                           }
                       }

                       );

                       $('q').selectedIndex = si;
                       get_results(querystring);
                   }
               }
           });
}

function get_results(querystring) {
    var q = querystring;
    var the_url = "http://discovery.cor.gov/public/cvb/cvbweb.nsf/getOtherListByBusinessType?openagent&searchby=Shopping&q=" + q;

    $('TheList').innerHTML = "<img src=\"images/ajax-loader.gif\" />";
    new Ajax.Request('request_proxy.aspx',
              {
                  method: "get",
                  parameters: { url: the_url },
                  onSuccess: function(transport) {
                      var r = transport.responseText;
                      var o = r.evalJSON();
                      if (o.response == "success") {
                          $('TheList').innerHTML = "";
                          var max = o.other.length;
                          var x = 0;
                          var html = "<table width=\"100%\" class=\"list_table\">";
                          html += "<tr><td colspan=\"5\" class=\"list_line\" align=\"left\">&nbsp;</td></tr>";

                          var clr = "#EFEFEF";
                          while (x < max) {
                              if (clr == "#EFEFEF") {
                                  clr = "#FFFFFF";
                              } else {
                                  clr = "#EFEFEF";
                              }

                              html += "<tr class=\"list_row\" bgcolor=\"" + clr + "\">\r\n";
                              html += "<td valign=\"top\" class=\"list_text\" align=\"left\">";
                              if (o.other[x].membershiptype != "B") {
                                  html += "<a href=\"shopping_details.aspx?id=" + o.other[x].id + "\" class=\"list_link\">";
                                  html += o.other[x].name + "</a>";
                              } else {
                                  html += o.other[x].name;
                              }
                              html += "</td>\r\n";
                              html += "<td valign=\"top\" class=\"list_text\" align=\"left\">" + o.other[x].address; 
                              html += "<br />" + o.other[x].city + ", " + o.other[x].state + " " + o.other[x].zipcode +"</td>\r\n";
                              html += "<td valign=\"top\" class=\"list_text\" align=\"left\">" + o.other[x].phone + "<br />";
                              if (o.other[x].website != "") {
                                  if (o.other[x].website.indexOf("http://") >= 0) {
                                      html += "<a href=\"" + o.other[x].website + "\" target=\"_blank\" class=\"list_link\">Website</a>";
                                  } else {
                                      html += "<a href=\"http://" + o.other[x].website + "\" target=\"_blank\" class=\"list_link\">Website</a>";
                                  }
                              }
                              html += "</td>\r\n";

                              html += "</tr>\r\n";
                              html += "<tr bgcolor=\"" + clr + "\"><td colspan=\"5\" class=\"list_line\" align=\"left\">&nbsp;</td></tr>";

                              x++;

                          };

                          html += "</table>\r\n";


                          $('TheList').innerHTML = html;
                      } else {
                          alert(o.response);
                      }
                  }
              }
           );
}
