 
<jsp:useBean id="em" scope="session" class="EntrakMain"/>
<%@ page language="java" import=" java.util.*" errorPage="error.jsp"%>
<%!
    String userName ;
    String password ; 
    
    String timeScale;
    String environmentParameter ="";
    String resolution;
	String frequency;
	String fromHour=null;
	String toHour;
	String fromMinute;
	String toMinute;	
	String fromMonth="Mar";
	String toMonth="Mar";
	String fromYear="2003";
	String toYear="2003";
	String fromDay;
	String toDay;
	String eboxId =null;
	String ic_item;
	
	
	String hostOfDBMS;
	
    String[] months = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
                    "Oct", "Nov","Dec" } ;
	String[] mon_no={"01","02","03","04","05","06","07","08",
                    "09", "10","11","12"};				
    String[] hours = {"00","01","02","03","04","05","06","07","08",
                    "09", "10","11","12","13","14","15","16","17","18","19","20","21","22","23" } ;
   
    String[] days = {"01","02","03","04","05","06","07","08",
                    "09", "10","11","12","13","14","15","16",
					"17","18","19","20","21","22","23","24","25",
					"26","27","28","29","30","31" } ;
	String[] frequencyOptions={"Monthly","Daily","Hourly","Half Hourly","Minute" };
    String[] eboxes;	
%>

<html>
<head>
<title>RealTimeIndoorEnv</title>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</head>
 <% 
      eboxId=request.getParameter("eboxId");
 	  if( eboxId == null){
	        Calendar rightNow = Calendar.getInstance();
	        fromHour =hours[rightNow.get(Calendar.HOUR_OF_DAY)];
			fromDay = days[rightNow.get(Calendar.DAY_OF_MONTH) -2 ];
			fromMonth= mon_no[ rightNow.get(Calendar.MONTH)];
			fromYear = ""+ rightNow.get(Calendar.YEAR);
			
			toHour =hours[rightNow.get(Calendar.HOUR_OF_DAY) + 1];
			toDay = days[rightNow.get(Calendar.DAY_OF_MONTH) -1 ];
			toMonth= mon_no[ rightNow.get(Calendar.MONTH)];
			toYear = ""+ rightNow.get(Calendar.YEAR);
			frequency="Half Hourly";	 										
			//hostOfDBMS=em.hostOfDBMS;
		    //userName =em.username;
            //password =new String(em.password);			
			/**********************************************************
			 * set list of e-boxes available within an hour today     *
			 **********************************************************/
			/* String intervalTerm = "60 MINUTE";	       
            String command ="select  m_id from smarthome.monitored" +
                " where ( (NOW() - INTERVAL " + intervalTerm + ") AND date =CURRENT_DATE)" +
				" group by m_id order by m_id DESC ";            
			int rowCount=0; 
			Statement stmt = null;
            ResultSet rs =   null;
			try{              
               stmt  = em.con.createStatement();
               stmt.execute( command );
			   rowCount= stmt.getUpdateCount();
               rs= stmt.getResultSet();             
               stmt.close();
            }
            catch( SQLException e ) {
              String errorMessage = new String ( "SQLException: " + e.getMessage()  );
              System.out.println("SQLStatement.executeSQL(), " + errorMessage);
            }
            catch( Exception e ) {
              String errorMessage = new String ( " exception " + e.getMessage()  );
              System.out.println("SQLStatement.executeSql(), " + errorMessage);
            }
		    eboxes= new String[rowCount+1]; 
            int count=0;				 
		    System.out.println ("row count " + rowCount); 	       
            while (rs.next()){ 	     		
		      eboxes[count] = rs.getString("m_id");
		      System.out.println( "ebox " +eboxes[count]);
		      count++; 		 
            }
            eboxId=eboxes[0];
			*/
	  }
	  else{	     
	    frequency=request.getParameter("frequency");
		fromMonth=request.getParameter("fromMonth");
		fromYear=request.getParameter("fromYear");
		fromDay=request.getParameter("fromDay");
		fromHour=request.getParameter("fromHour");
		fromMinute=request.getParameter("fromMinute");
		toMonth=request.getParameter("toMonth");
		toYear=request.getParameter("toYear");
		toDay=request.getParameter("toDay");
		toHour=request.getParameter("toHour");
		toMinute=request.getParameter("toMinute");			
      }			   
	 
	  environmentParameter=request.getParameter("environmentParameter");
	   //ic_item=request.getParameter("ic_item");
	  ic_item=environmentParameter;	   
	  hostOfDBMS=em.hostOfDBMS;
      userName =em.username;
      password =new String(em.password);				 	
	%>	

 


<body BGCOLOR="#FFFFFF" TEXT="#000000" LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">

 
<table WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="100%" BORDERCOLOR="#000000">
  <tr> 
	<td WIDTH="60%" HEIGHT="129" ALIGN="CENTER" VALIGN="middle"> 
	
	  <form NAME="form1" METHOD="post" ACTION="">
		<table WIDTH="80%" BORDER="0" CELLPADDING="2" BGCOLOR="#000000" ALIGN="CENTER">
		  <tr VALIGN="middle" BGCOLOR="#999999"> 
			<td ALIGN="center"> 
			  <center>
			  </center>
			  <table WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="100%">
			  
			  	<tr> 
				  <td WIDTH="34%"><font COLOR="#FFFFFF" FACE="Arial, Helvetica, sans-serif" SIZE="2">e-box 
					id:</font></td>
				  <td WIDTH="66%"> 
					<input TYPE="text" NAME="eboxId" VALUE=<%=eboxId%> >
				  </td>
				</tr>
			  
				<tr> 
				  <td WIDTH="34%"><font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">Parameter:</font> 
				  </td>
				  <td WIDTH="66%">
				   <input TYPE="text" NAME= "environmentParameter"  VALUE= <%=environmentParameter%> ></td>
				   
				 
				</tr>
			 
			  </table>
			</td>
			<td ALIGN="center"> 
			  <center>
				<font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">Period:</font> 
			  </center>
			</td>
			<td ALIGN="center" VALIGN="middle"> 
			  <table WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="100%" ALIGN="RIGHT">
				<tr> 
				  <td ALIGN="center" VALIGN="middle" NOWRAP> 
					<p><font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">from: 
					       <select name="fromHour" size="1">
                            <%
                                for(int i=0;i < hours.length;i++) {
                                    if( hours[i].equals(fromHour)) {
                            %>
                                   <option selected><%=hours[i]%></option>
                            <%
                                    } //if(hours[i].equals(fromMonth))
                                    else {
                            %>
                                    <option><%=hours[i]%></option>
                            <%
                                    } //else
                                } //for(int i=0;i<months.length;i++)
                            %>

                   </select>
					
					  : 
					  <select NAME="fromMinute">
						<option VALUE="00" SELECTED>00</option>
						<option VALUE="15">15</option>
						<option VALUE="30">30</option>
						<option VALUE="45">45</option>
					  </select>
					  , </font><font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF"> 
					  
					     <select name="fromDay" size="1">
                            <%
                                for(int i=0;i<days.length;i++) {
                                    if(days[i].equals(fromDay)) {
                            %>
                                   <option selected><%=days[i]%></option>
                            <%
                                    } //if(days[i].equals(fromDay))
                                    else {
                            %>
                                    <option><%=days[i]%></option>
                            <%
                                    } //else
                                } //for(int i=0;i<months.length;i++)
                            %>

                   </select>
					   
					   <select name="fromMonth" size="1">
                            <%
                                for(int i=0;i<months.length;i++) {
                                    if(mon_no[i].equals(fromMonth)) {
                            %>
                                   <option value = <%= mon_no[i] %> selected><%=months[i]%></option>
                            <%
                                    } //if(months[i].equals(fromMonth))
                                    else {
                            %>
                                    <option value = <%= mon_no[i] %> ><%=months[i]%></option>
                            <%
                                    } //else
                                } //for(int i=0;i<months.length;i++)
                            %>
                   </select>
							  
					  <select NAME="fromYear">
						 
						<option VALUE="2002">2002</option>
						<option VALUE="2003" SELECTED>2003</option>
					  </select>
					  					  
					  
					  </font></p>
				  </td>
				</tr>
				<tr> 
				  <td ALIGN="center" VALIGN="middle" NOWRAP> 
					<p><font COLOR="#FFFFFF" FACE="Arial, Helvetica, sans-serif" SIZE="2">to:
				   <select name="toHour" size="1">
					   <%
                                for(int i=0;i < hours.length;i++) {
                                    if( hours[i].equals(toHour)) {
                            %>
                                   <option selected><%=hours[i]%></option>
                            <%
                                    } //if(hours[i].equals(fromMonth))
                                    else {
                            %>
                                    <option><%=hours[i]%></option>
                            <%
                                    } //else
                                } //for(int i=0;i<months.length;i++)
                            %>
                      </select>
					   : 
					  <select NAME="toMinute">
						<option VALUE="00" SELECTED><font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">00</font></option>
						<option VALUE="15"><font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">15</font></option>
						<option VALUE="30"><font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">30</font></option>
						<option VALUE="45"><font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">45</font></option>
					  </select>
					  </font> <font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF"> 
					  ,</font><font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF"> 
					 
					 
					   <select name="toDay" size="1">
                            <%
                                for(int i=0;i<days.length;i++) {
                                    if(days[i].equals(toDay)) {
                            %>
                                   <option selected><%=days[i]%></option>
                            <%
                                    } //if(days[i].equals(fromDay))
                                    else {
                            %>
                                    <option><%=days[i]%></option>
                            <%
                                    } //else
                                } //for(int i=0;i<months.length;i++)
                            %>

                   </select>
					   
					   					 					  
					     <select name="toMonth" size="1">
                            <%
                                for(int i=0;i<months.length;i++) {
                                    if(mon_no[i].equals(toMonth)) {
                            %>
                                   <option value = <%= mon_no[i] %> selected><%=months[i]%></option>
                            <%
                                    } //if(months[i].equals(fromMonth))
                                    else {
                            %>
                                    <option value = <%= mon_no[i] %> > <%=months[i]%></option>
                            <%
                                    } //else
                                } //for(int i=0;i<months.length;i++)
                            %>

                   </select>					  										  
					  
                   <select NAME="toYear">						 
						<option VALUE="2002">2002</option>
						<option VALUE="2003" selected>2003</option>
				  </select>
				  </font></p>
				  </td>
				</tr>
			  </table>
			</td>
			<td ALIGN="center"><font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">Frequency:</font></td>
			<td ALIGN="center"> 
			
			  <select NAME="frequency">
			     <%
                                for(int i=0; i < frequencyOptions.length; i++) {
                                    if(  frequencyOptions[i].equals( frequency )) {
                            %>
                                   <option selected><%=frequencyOptions[i]%></option>
                            <%
                                    } //if(months[i].equals(fromMonth))
                                    else {
                            %>
                                    <option><%=frequencyOptions[i]%></option>
                            <%
                                    } //else
                                } //for(int i=0;i<months.length;i++)
                   %>			  			  			  				
				
			  </select>
			 						
			  <!--select NAME="frequency">
				<option VALUE="Monthly">Monthly</option>
				<option VALUE="Daily" SELECTED>Daily</option>
				<option VALUE="Hourly">Hourly</option>
				<option VALUE="Half Hourly">Half Hourly</option>
				<option VALUE="Minute">Minutes</option>
				
			  </select-->
			</td>
			
			
			<!--td ALIGN="center" WIDTH="10"><font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF"> 
			  <input TYPE="image" BORDER="0" NAME="imageField" SRC="images/start.gif" WIDTH="64" HEIGHT="17">
			  </font></td-->
		  </tr>
		  
		 
			  
       <td ALIGN="center" WIDTH="10"><font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF"> 
			  <input TYPE="image" BORDER="0" NAME="imageField" SRC="images/start.gif" WIDTH="64" HEIGHT="17">
			  </font></td>		  
		</table>
	  </form>
	</td>
  </tr>
  <tr> 
	<td WIDTH="70%" ALIGN="CENTER" VALIGN="top" HEIGHT="516"> 
	  <center>
		<% 
		if (request.getParameter("eboxId")!=null){
		    	
	  %>
			<!--img SRC="LineChart123?environmentParameter=<%=environmentParameter%>&frequency=<%=frequency%>&fromHour=<%=fromHour%>&fromMinute=<%=fromMinute%>&fromDay=<%=fromDay%>&fromMonth=<%=fromMonth%>&fromYear=<%=fromYear%>&toHour=<%=toHour%>&toMinute=<%=toMinute%>&toDay=<%=toDay%>&toMonth=<%=toMonth%>&toYear=<%=toYear%>&eboxId=<%=eboxId%>&ic_item=<%=ic_item%>" WIDTH="500" HEIGHT="300" --> 
           <img SRC="LineChart123?environmentParameter=<%=environmentParameter%>&frequency=<%=frequency%>&fromHour=<%=fromHour%>&fromMinute=<%=fromMinute%>&fromDay=<%=fromDay%>&fromMonth=<%=fromMonth%>&fromYear=<%=fromYear%>&toHour=<%=toHour%>&toMinute=<%=toMinute%>&toDay=<%=toDay%>&toMonth=<%=toMonth%>&toYear=<%=toYear%>&eboxId=<%=eboxId%>&ic_item=<%=ic_item%>&userName=<%=userName%>&password=<%=password%>&hostOfDBMS=<%=hostOfDBMS%>" WIDTH="500" HEIGHT="300" > 					
      <%
		}
      %>
		</center>
		</table>
	  </body>
</html>
