﻿function fill_q(searchby, querystring) {
    var the_url;
    if (searchby == "Type") {
        the_url = "http://discovery.cor.gov/public/cvb/cvbweb.nsf/getRestBusinessList";
        $('search_by_label').innerHTML = "Select Type";
    }else{
         the_url = "http://discovery.cor.gov/public/cvb/cvbweb.nsf/getCuisineList";
         $('search_by_label').innerHTML = "Select Cuisine";
    }

    new Ajax.Request('request_proxy.aspx',
           {
               method: "get",
               parameters: {
                   url: the_url
               },
               onSuccess: function(transport) {
                   var op;
                   var resp = transport.responseText || "no response text";
                   
                   var obj = resp.evalJSON();
                   if (obj.response == "success") {

                       var list = Object.values(obj.cuisines);
                       /*
                       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;
                           }
                           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(searchby, querystring);
                   }
               }
           });
}

function get_results(searchby, querystring) {
    //var q = $F('q');
    var q = querystring.toLowerCase();
    var sb = searchby;
    //alert(sb);
    var the_url = "http://discovery.cor.gov/public/cvb/cvbweb.nsf/getRestaurantList?openagent";
    if (sb == "Cuisine" || sb == "All") {
        the_url += "&searchby=cuisine";
    }
    if (sb == "Type") {
        the_url += "&searchby=business";
    }
    if (q == null) {
        the_url += "&q=all";
    } else {
        the_url += "&q=" + $F('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.restaurant.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.restaurant[x].membershiptype != "B") {
                                  html += "<a href=\"dining_details.aspx?id=" + o.restaurant[x].id + "\" class=\"list_link\">";
                                  html += o.restaurant[x].name + "</a>";
                              } else {
                                  html += o.restaurant[x].name;
                              }
                              html += "</td>\r\n";
                              html += "<td valign=\"top\" class=\"list_text\" align=\"left\">" + o.restaurant[x].address + "<br />";
                              if (o.restaurant[x].address2 != "") {
                                  html += o.restaurant[x].address2 + "<br />";
                              }
                              html += o.restaurant[x].city + ", " + o.restaurant[x].state + " " + o.restaurant[x].zip +"</td>\r\n";
                              html += "<td valign=\"top\" class=\"list_text\" align=\"left\">" + o.restaurant[x].phone + "<br />";
                              if (o.restaurant[x].website != "") {
                                  if (o.restaurant[x].website.indexOf("http://") >= 0) {
                                      html += "<a href=\"" + o.restaurant[x].website + "\" target=\"_blank\" class=\"list_link\">Website</a>";
                                  } else {
                                      html += "<a href=\"http://" + o.restaurant[x].website + "\" target=\"_blank\" class=\"list_link\">Website</a>";
                                  }
                              }
                              html += "</td>\r\n";
                              html += "<td class=\"list_text\" valign=\"top\" align=\"left\">";
                              if (o.restaurant[x].couponurl != "") {
                                  html += "<a href=\"" + o.restaurant[x].couponurl + "\" target=\"_blank\" class=\"list_link\">Coupon</a>";
                              } else {
                                  html += "&nbsp;";
                              }
                              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);
                      }
                  }
              }
           );
}
