<%@ page language="java" import="java.util.*,java.sql.*,java.net.*,java.awt.*,java.awt.image.*,javax.swing.*,java.io.*"%>


<html>
<head>
<title>Energy Environment Information System</title>


<SCRIPT LANGUAGE="JavaScript">


    //create a global variable so the window object can be accessed elsewhere


    function createWindow(url) {

        var winNum = self.opener.winNum;
        var imgWin = self.opener.imgWin[winNum];

        //if the window.name isn't set, create the window
        if(imgWin == null) {
            var targetWin = "ImageWindow_"+winNum.toString();
            imgWin = window.open(url,targetWin,'status=yes,top=20,left=20,dependent=yes,scrollbars=yes,resizable=no,width=400,height=400,menubar=no');
            //imgWin.location.replace(url);
            imgWin.location.reload();

            self.opener.imgWin[winNum] = imgWin;
            winNum++;
            self.opener.winNum = winNum;
        }//
        else {
            if(imgWin.name != null) {
                imgWin.focus();
            }
        }//else

    }//function createWindow(url)


    function loadFrame(url,target) {
       
	      
		  var  newWin=window.open(url,target); 		    
		    newWin.location.replace(url);
					
        //newWin.location.reload();

    } //function loadFrame(url,target)


</SCRIPT>



<script language="JavaScript">
    function underConstruction( ) {
        window.alert("This option is now under construction : " );
    } //function underConstruction()
</script>

<script language="JavaScript">
    function message( message ) {
        window.alert( message);
    } //function underConstruction()
</script>

<script language="JavaScript">

    function runApp(url) {

        var newWin = window.open(url,"any");
        newWin.focus();

    } //function runApp(url)

</script>




</head>


<body>
<h3>Consumption Profile</h3>
<!--
<p>
    <A HREF="#" TARGET="" onClick="underConstruction();">
        About EEIS
    </A>
    |
    <A HREF="#" TARGET="" onClick="runApp('http://www.esru.strath.ac.uk/');">
        ESRU
    </A>
    |
    <A HREF="#" TARGET="" onClick="underConstruction();">
        Energy Links
    </A>
    |
    <A HREF="#" TARGET="" onClick="underConstruction();">
        Contact
    </A>
</p>

-->






<%!
    String mapURL="http://localhost:8000/smarthome/maps/europe/europe.html";
    String[]  fuelTypeStrings = { "0 : Total","1 : Electricity(kWh)", "2 : Gas(kWh)", "3 : Oil"} ;
    String[]  fuelTypeCode   = {"0","1","2","3" };
    String [] scopeNameSet = {"living_1"};


    String[] years = {"1994","1995","1996","1997","1998","1999","2000"};
    String fuelType = "1";
    String scope = "living_1";//(  building.ref_ref=\'SUL1\' ) ";
    String resultType = "";
    String fromMonth = "";
    String toMonth = "";
    String fromYear = "";
    String toYear = "";

    String[] months = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
                    "Oct", "Nov","Dec" } ;

%>

    <jsp:useBean id="em" scope="session" class="EntrakMain"/>
    <jsp:useBean id="mcpm" scope="session" class="MainControlPanel_Map"/>
    <jsp:useBean id="epm" scope="session" class="EnergyProfileManager"/>	

<%

        String actionButtonName = request.getParameter("action");

        try {
            
            mcpm.linkDatabase(new URL(mapURL));
			mcpm.setDatabase( em.database );
			
			System.out.println("JSPEnergyProfile..., em.database " + em.database);
            System.out.println("JSPEnergyProfile..., urllEntrakHome: " + mcpm.getUrlEntrakHome());
			System.out.println("JSPEnergyProfile..., mcpm.urlScope: " + mcpm.urlScope);
			
            ScopeFileManager sfm = new ScopeFileManager(mcpm.urlScope);
            sfm.readScopeFileWC();

            epm.setConnection(mcpm.getConnection());
            epm.setDatabase(mcpm.getDatabase());
			// System.out.println("JSP_EPM, epm.database: " + mcpm.getDatabase());
			
            epm.setUrlScope(mcpm.urlScope);
            epm.setUrlEntrakHome(mcpm.getUrlEntrakHome());

            System.out.println("JSP_EPM, epm.urlEntrakHome: " + epm.getUrlEntrakHome());
            System.out.println("JSP_EPM, mcpm.urlEntrakHome: " + mcpm.getUrlEntrakHome());

            String clName = ClassLoaderName.getName(getClassLoader().toString());
			System.out.println("JSPEnergyPro....jsp, " + clName );
			
            epm.setClassLoaderName(clName);    //This is important for Web client!
            
		    //System.out.println("EPM.jsp, " + epm.getClassLoaderName());
            epm.setup(); 
			

            fuelTypeStrings = epm.getFuelTypeStrings();
            fuelTypeCode = epm.getFuelTypeCode();
            scopeNameSet = epm.scopeNameSet;
            years = epm.getYears();

            String [] yearsArray = epm.getYears();

            if(yearsArray != null)
                years = yearsArray;

        } //try

        catch(MalformedURLException mue) {
              // System.out.println( mue.getMessage() );
        } //catch(MalformedURLException mue)
%>






<%


    if(actionButtonName != null) {


%>
<!--
        <p>The button you clicked is <%=actionButtonName%>.</p>
-->
<%

        fuelType = request.getParameter("fuelType");
		
        epm.fuelTypePane.itemCombo.setSelectedItem(fuelType);
        int fuel = epm.fuelTypePane.itemCombo.getSelectedIndex();
        String fuelString = String.valueOf(fuel);

        scope = request.getParameter("scope");
        epm.selectSetPane.itemCombo.setSelectedItem(scope);

        String[] fuelTypeStrings = epm.getFuelTypeStrings();
        String itemTitle = new String( fuelTypeStrings[ fuel ] +" in " + " \'" +
                     (String) epm.selectSetPane.itemCombo.getSelectedItem() + "\' ");


        resultType = request.getParameter("resultType");

        if(resultType.equals("yearly")) {
            epm.resultTypePane.itemRadio[0].doClick();
            //epm.resultTypePane.itemRadio[0].setSelected(true);
            //epm.resultTypePane.itemRadio[1].setSelected(false);
        } //if(resultType.equals("yearly"))

        else {
            epm.resultTypePane.itemRadio[1].doClick();
            //epm.resultTypePane.itemRadio[1].setSelected(true);
            //epm.resultTypePane.itemRadio[0].setSelected(false);
        } //else

        fromMonth = request.getParameter("fromMonth");
        toMonth = request.getParameter("toMonth");
        fromYear = request.getParameter("fromYear");
        toYear = request.getParameter("toYear");

        epm.fromPane.combo[0].setSelectedItem(fromMonth);
        epm.toPane.combo[0].setSelectedItem(toMonth);
        epm.fromPane.combo[1].setSelectedItem(fromYear);
        epm.toPane.combo[1].setSelectedItem(toYear);



%>
<!--
        <p>
            fuelType = <%=fuelType%><br>
            fuel = <%=fuel%><br>
            epm.fuelType = <%=(String)epm.fuelTypePane.itemCombo.getSelectedItem()%><br>
            scope = <%=scope%><br>
            epm.scope = <%=(String)epm.selectSetPane.itemCombo.getSelectedItem()%><br>
            itemTitle = <%=itemTitle%><br>
            resultType = <%=resultType%><br>
            epm.resultTypePane.itemRadio[0].isSelected() = <%=epm.resultTypePane.itemRadio[0].isSelected()%><br>
            epm.resultTypePane.itemRadio[1].isSelected() = <%=epm.resultTypePane.itemRadio[1].isSelected()%><br>
            epm.fromPane.combo[0].getSelectedItem() = <%=epm.fromPane.combo[0].getSelectedItem()%><br>
            epm.fromPane.combo[1].getSelectedItem() = <%=epm.fromPane.combo[1].getSelectedItem()%><br>
            epm.toPane.combo[0].getSelectedItem() = <%=epm.toPane.combo[0].getSelectedItem()%><br>
            epm.toPane.combo[1].getSelectedItem() = <%=epm.toPane.combo[1].getSelectedItem()%><br>
        </p>
-->
<%

        if (actionButtonName.equals("Profile")) {

            System.out.println("JSPEnergyProfileManager.jsp, actionButtonName = Profile");
            epm.setFuelWC(fuel);
            epm.setItemTitleWC(itemTitle);
%>
         itemTitle :  <%=itemTitle%>
            <script>     		  
                loadFrame("drawImg.jsp","imgFrame");
            </script>

<%

        } //if (actionButtonName.equals("Profile"))


        else if (actionButtonName.equals("Individual Trend")) {

            boolean success = epm.processForLineChart(fuel,itemTitle);
            if(success) {

                JFrame frame = epm.getChartJFrame();

                if(frame!=null) {
                    File f = new File("c:\\j2sdkee1.2.1\\public_html\\smarthome\\energy_managers\\frameObject");
					
                    if(f.exists())
                        f.delete();
					System.out.println(" jsp energyprogfile : update 191:00");
					
                    ObjectReadWrite.write(f,frame);
					
%>
                    itemTitle :  <%=itemTitle%>
                    <script>					     					    
                        loadFrame("showApplet.jsp","imgFrame");
                    </script>
					
<%


                }//if(frame!=null)

                else {

                    System.out.println("JSP_EPM, frame = null");

                } //

            } //if(success)

            else {

                System.out.println("JSP_EPM, success = false");

            } //


        }//else if (actionButtonName.equals("Individual Trend"))


        else if (actionButtonName.equals("Table")) {

            boolean success = epm.processForTable(itemTitle);

            if(success) {

                MyTableModel tm = epm.getTm();

                if(tm!=null) {

                   File f = new File("c:\\j2sdkee1.2.1\\public_html\\smarthome\\energy_managers\\tableObject");
				   
                    if(f.exists())
                        f.delete();
                    
					ObjectReadWrite.write( f,tm);	
					
                    System.out.println("EPM, written to tableObject");

%>
                    <script>
                        loadFrame("showApplet2.jsp","imgFrame");
                    </script>

<%


                }//if(table!=null)

                else {

                    System.out.println("JSP_EPM, Table, table = null");

                } //

            } //if(success)

            else {

                System.out.println("JSP_EPM, Table, success = false");

            } //


        }//else if (actionButtonName.equals("Table"))


        else { //actions for other buttons goes here

%>
            <script>
                loadFrame("blank.jsp","imgFrame");				
            </script>

<%

        } //else



    } //if(actionButtonName != null)


    else {


%>
        <script>
            loadFrame("blank.jsp","imgFrame");
            //underConstruction( " actionButtonName == null" )	;		
        </script>

<%

    } //else



%>




<!--
<p>
        mapURL : <%=mapURL%>
</p>
-->

    <table width="100%" border="0" VSPACE="5" HSPACE="5" CELLPADDING="5">
        <tr>
            <td width="50%" BGCOLOR="#C0C0C0">Database</td>
            <td width="50%" BGCOLOR="#80FFFF"><%=mcpm.getDatabase()%></td>
        </tr>
        <tr>
            <td width="50%" BGCOLOR="#C0C0C0">Name of map</td>
            <td width="50%" BGCOLOR="#80FFFF"><%=mcpm.getNameOfMap()%></td>
        </tr>
        <tr>
            <td width="50%" BGCOLOR="#C0C0C0">District</td>
            <td width="50%" BGCOLOR="#80FFFF"><%=mcpm.getDistrict()%></td>
        </tr>
    </table>


<%

        Connection c = mcpm.getConnection();
        String us = mcpm.urlScope;
        String ueHome = mcpm.getUrlEntrakHome();

        if(c == null) {
%>
            <p>
                Connection c is null.
            </p>
<%

        } //if(c == null)


        if(us == null) {
%>
            <p>
                String us is null.
            </p>
<%

        } //if(us == null)


        if(ueHome == null) {
%>
            <p>
                String ueHome is null.
            </p>
<%

        } //if(ueHome == null)


%>














<form method="post">

    <table width="100%" border="0" VSPACE="5" HSPACE="5" CELLPADDING="5">

        <tr>
            <td width="40%" BGCOLOR="#C0C0C0">Scope:</td>
            <td width="60%" BGCOLOR="#80FFFF">
                <select name="scope" size="1">
                    <%
                        for(int i=0;i<scopeNameSet.length;i++) {

                            if(scopeNameSet[i].equals(scope)) {
                    %>
                                <option selected><%=scopeNameSet[i]%></option>
                    <%
                            } //if(scopeNameSet[i].equals(scope))

                            else {
                    %>
                                <option><%=scopeNameSet[i]%></option>
                    <%
                            } //else

                        } //for(int i=0;i<scopeNameSet.length;i++)
                    %>
                </select>
            </td>
        </tr>

        <tr>
            <td width="40%" BGCOLOR="#C0C0C0">Fuel type:</td>
            <td width="60%" BGCOLOR="#80FFFF">
                <select name="fuelType" size="1">
                    <%
                        for(int i=0;i<fuelTypeStrings.length;i++) {
                            if(fuelTypeStrings[i].equals(fuelType)) {

                    %>
                                <option selected><%=fuelTypeStrings[i]%></option>
                    <%

                            } //if(fuelTypeStrings[i].equals(fuelType))

                            else {
                    %>
                                <option><%=fuelTypeStrings[i]%></option>
                    <%
                            } //else
                        } //for(int i=0;i<fuelTypeStrings.length;i++)
                    %>

                </select>
            </td>
        </tr>

        <tr>
            <td width="40%" BGCOLOR="#C0C0C0">Result type:</td>
            <td width="60%" BGCOLOR="#80FFFF">
                <%
                    if(resultType.equals("yearly")) {
                %>
                        <input type="radio" name="resultType" value="yearly" CHECKED> Yearly
                        <input type="radio" name="resultType" value="monthly"> Monthly
                <%
                    } //if(resultType.equals("yearly"))

                    else {
                %>
                        <input type="radio" name="resultType" value="yearly"> Yearly
                        <input type="radio" name="resultType" value="monthly" checked> Monthly
                <%
                    } //else
                %>
            </td>
        </tr>


        <tr>


            <td width="40%" BGCOLOR="#C0C0C0">
                Duration:
            </td>
            <td width="60%" BGCOLOR="#80FFFF">



            <table>

                <tr>
                    <td>


                        From:

                        <select name="fromMonth" size="1">

                            <%
                                for(int i=0;i<months.length;i++) {

                                    if(months[i].equals(fromMonth)) {
                            %>
                                        <option selected><%=months[i]%></option>

                            <%
                                    } //if(months[i].equals(fromMonth))
                                    else {
                            %>
                                        <option><%=months[i]%></option>

                            <%
                                    } //else
                                } //for(int i=0;i<months.length;i++)
                            %>

                        </select>

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


                    </td>
                </tr>
                <tr>

                    <td>


                        To:

                        <select name="toMonth" size="1">
                            <%
                                for(int i=0;i<months.length;i++) {

                                    if(months[i].equals(toMonth)) {
                            %>
                                        <option selected><%=months[i]%></option>

                            <%
                                    } //if(months[i].equals(toMonth))
                                    else {
                            %>
                                        <option><%=months[i]%></option>

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


                    </td>

                </tr>

            </table>




            </td>
        </tr>
    </table>


    <table width="100%" border="0" VSPACE="5" HSPACE="5" CELLPADDING="5">
        <tr>
            <td width="100%" BGCOLOR="#C0C0C0">
                <input TYPE="submit" VALUE="Profile" NAME="action">
                <input TYPE="submit" VALUE="Individual Trend" NAME="action">
                <input TYPE="submit" VALUE="Table" NAME="action">
            </td>
        <tr>

    </table>

</form>








</body>

</html>





