<%@ page language = "java"%>
<%@ page import = "java.util.*"%>
<%@ page import = "java.sql.*"%>
<%@ page import = "java.io.*"%>
<%@ page import = "java.net.*"%>

<link rel="stylesheet" href="css/mysite.css" type="text/css"/>
<script type="text/javascript" src="js/ajax.js"></script>

<%!

public static Vector<String> delimit(String toDelimit) {

  String temp ="";
  Vector<String> v = new Vector<String>();
  
  for (int i=0;i<toDelimit.length();i++) {
    if (toDelimit.charAt(i) != '|')
    temp+=toDelimit.charAt(i);
    else {
    v.add(temp);
    temp="";    
     }
    }
    
    for (int i=0;i<v.size();i++) {
    if (v.elementAt(i).equals(""))
    v.remove(i);
    }
    
    return v;
}
%>

<%

    String andor ="";
    String tag = "";
		Connection conn = null;
  
           try
           {
               String userName = "root";
               String password = "";
               String url = "jdbc:mysql://localhost/band";
               Class.forName ("com.mysql.jdbc.Driver").newInstance();
               conn = DriverManager.getConnection (url, userName, password);
               System.out.println ("Database connection established");
           
			
			
		
            if (request.getParameter("tag").equals("") || request.getParameter("andor").equals("")) {
            System.out.println("empty.."); 
            return;
            
            
            
        } else {
           tag = request.getParameter("tag");
           andor = request.getParameter("andor");
           System.out.println(tag+" "+andor);
           }
           
          Vector<String> v = delimit(tag);
           
/*******************************************/
           
      Statement s = conn.createStatement ();
      String query="select name from bands where id in";
      
			query+=" (select band_id from tagged where tag_id in (select id from tags where name = '"+v.elementAt(0)+"')) ";
			
			for (int i=1;i<v.size();i++) {
			query += (" "+andor+" id in ");
			query += " (select band_id from tagged where tag_id in (select id from tags where name = '"+v.elementAt(i)+"')) ";
			
			}
			
			query+="ORDER BY name";
			System.out.println(query);			
					
			s.executeQuery (query);
			ResultSet rs = s.getResultSet ();
              
              
       
      
      String bandname="";
      
      out.println("<div class=\"left50\">");
      
      out.println("<br><b>Bands tagged as: </b>");
      for (int i=0;i<v.size()-1;i++)
      out.println(v.elementAt(i)+" <i>"+andor+"</i> ");
      
      out.println(v.lastElement());
      
      out.println("<br><br>");
      out.println("<div class=\"bandtags\">");
		while (rs.next ())
		{
			bandname = rs.getString ("name");
			out.println("<div class=\"suggest_link\" onmouseover=\"javascript:suggestOver(this);\" onmouseout=\"javascript:suggestOut(this);\" onclick=\"javascript:getBand('"+bandname+"');enableForum();\"/>");
      out.println(bandname+"<br>");      
      out.println("</div>");      
		}
		
		out.println("</div>");
		out.println("</div>");
		rs.close ();
		s.close ();

		

   
   
           
	}                   catch (Exception e)
           {
               System.err.println ("database error");
               System.err.println (e);
           }
           
           finally
           {
               if (conn != null)
               {
                   try
                   {
                       conn.close ();
                       System.out.println ("Database connection terminated");
                   }
                   catch (Exception e) { /* ignore close errors */ }
               }
           }
           

%>



<div class="right50">

Add more tags:
<br><br>

<form id="moretags_form">
<%
System.out.println(andor);
out.println("<br><br> <input type=\"text\" onkeyup=\"moreTags('"+andor+"','"+tag+"')\" name=\"what\" id=\"what\" />");

%>
<div id="moretagssugg" class="search_suggest"></div>
</form>
</div>


