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


<html>
<head>

<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);
		//updated from newWin.location.reload();

    } //function loadFrame(url,target)

</SCRIPT>









</head>


<body>

<h3>Classification</h3>
<%!
    String mapURL="http://peta-esru.mecheng.strath.ac.uk: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 = {"No scope defined"};
    String[] years = {"2000"};
    String fuelType = "";
    String scope = "";
    //String resultType = "";
    String dataType = "totalConsumption";
    String [] dataTypeCheck = {"Total consumption (kWh)", "Consumption per area (kWh/m2)",
                           "Consumption per heated area (kWh/m2)"};

    String [] dataTypeValues = {"totalConsumption","consumptionPerArea","consumptionPerHeatedArea"};

    String [] numbers = {"1","2","3","4","5","6","7","8","9",
                    "10", "11","12" } ;
    String classNo = "5";
    String rangeOption = "";
    String inputRangeFrom = "";
    String inputRangeTo = "";

    String fromMonth = "";
    String toMonth = "";
    String fromYear = "";
    String toYear = "";

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

    String periodTitle = "";
    String title = "";


%>

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

<%

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

        try {
           
            mcpm.linkDatabase(new URL(mapURL));
            mcpm.setDatabase( em.database );
			System.out.println("JSP_CPM, OK here mapURL" + mapURL);
         
            ScopeFileManager sfm = new ScopeFileManager(mcpm.urlScope);
			
            sfm.readScopeFileWC();

            cpm.setConnection(mcpm.getConnection());
            cpm.setDatabase(mcpm.getDatabase());
			
            cpm.setUrlScope(mcpm.urlScope);
            cpm.setUrlEntrakHome(mcpm.getUrlEntrakHome());

            System.out.println("JSP_CPM, cpm.urlEntrakHome: " + cpm.getUrlEntrakHome());
            System.out.println("JSP_CPM, mcpm.urlEntrakHome: " + mcpm.getUrlEntrakHome());

            String cl = ClassLoaderName.getName(getClassLoader().toString());
            //System.out.println("CPM.jsp, CLName : " + cl);
			// cpm.setClassLoaderName(getClassLoader().toString());    //This is important for Web client!

            cpm.setClassLoaderName(cl);    //This is important for Web client!

            cpm.setup();
            System.out.println("CPM.jsp, " + cpm.getClassLoaderName());

            fuelTypeStrings = cpm.getFuelTypeStrings();
            fuelTypeCode = cpm.getFuelTypeCode();
            scopeNameSet = cpm.scopeNameSet;
	

            String [] yearsArray = cpm.getYears();

            if(yearsArray != null)
                years = yearsArray;

        } //try

        catch(MalformedURLException mue) {
                 System.out.println(" Error malformURL exception " );
        } //catch(MalformedURLException mue)
%>









<%


    if(actionButtonName != null) {


%>

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

<%

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

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



        dataType = request.getParameter("dataType");

        if(dataType.equals("totalConsumption")) {
            cpm.resultTypePane.itemRadio[0].doClick();
        } //if(dataType.equals("totalConsumption"))

        else if(dataType.equals("consumptionPerArea")) {
            cpm.resultTypePane.itemRadio[1].doClick();
        } //else if(dataType.equals("consumptionPerArea"))

        else if(dataType.equals("consumptionPerHeatedArea")) {
            cpm.resultTypePane.itemRadio[2].doClick();
        } //else if(dataType.equals("consumptionPerHeatedArea"))

        else {
            cpm.resultTypePane.itemRadio[0].doClick();
        } //else


        classNo = request.getParameter("classNo");
        rangeOption = request.getParameter("rangeOption");


        cpm.numberOfClassPane.itemField.setText(classNo);
        //cpm.numberOfClassPane.itemField.setText("5");


        if(rangeOption.equals("custom")) {

            cpm.autoButton.setSelected(false);
            cpm.customButton.setSelected(true);

            cpm.inputRangePane.textField[  0 ].setEnabled(false);
            cpm.inputRangePane.textField[  1 ].setEnabled(false);

            inputRangeFrom = request.getParameter("inputRangeFrom");
            inputRangeTo = request.getParameter("inputRangeTo");

            cpm.inputRangePane.textField[0].setText(inputRangeFrom);
            cpm.inputRangePane.textField[1].setText(inputRangeTo);

        } //if(rangeOption.equals("custom"))

        else {

            cpm.autoButton.setSelected(true);
            cpm.customButton.setSelected(false);

            cpm.inputRangePane.textField[  0 ].setEnabled(false);
            cpm.inputRangePane.textField[  1 ].setEnabled(false);

        } //else



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

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


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


         periodTitle = new String( (String) cpm.fromPane.combo[ 0 ].getSelectedItem() +
                ", " +  (String) cpm.fromPane.combo[ 1 ].getSelectedItem() + "-" +
                 (String) cpm.toPane.combo[ 0 ].getSelectedItem() +  ", " +
                 (String) cpm.toPane.combo[ 1 ].getSelectedItem() );

         title = new String( itemTitle +  ": Classification " +
              cpm.resultTypePane.itemRadio[ 0 ].getText()  +
              periodTitle   );



%>
<!--
        <p>
            fuelType = <%=fuelType%><br>
            fuel = <%=fuel%><br>
            cpm.fuelType = <%=(String)cpm.fuelTypePane.itemCombo.getSelectedItem()%><br>
            scope = <%=scope%><br>
            cpm.scope = <%=(String)cpm.selectSetPane.itemCombo.getSelectedItem()%><br>
            itemTitle = <%=itemTitle%><br>
            dataType = <%=dataType%><br>
            cpm.resultTypePane.itemRadio[0].isSelected() = <%=cpm.resultTypePane.itemRadio[0].isSelected()%><br>
            cpm.resultTypePane.itemRadio[1].isSelected() = <%=cpm.resultTypePane.itemRadio[1].isSelected()%><br>
            cpm.resultTypePane.itemRadio[2].isSelected() = <%=cpm.resultTypePane.itemRadio[2].isSelected()%><br>
            classNo = <%=classNo%><br>
            rangeOption = <%=rangeOption%><br>

            cpm.autoButton.isSelected() = <%=cpm.autoButton.isSelected()%><br>
            cpm.customButton.isSelected() = <%=cpm.customButton.isSelected()%><br>

            cpm.fromPane.combo[0].getSelectedItem() = <%=cpm.fromPane.combo[0].getSelectedItem()%><br>
            cpm.fromPane.combo[1].getSelectedItem() = <%=cpm.fromPane.combo[1].getSelectedItem()%><br>
            cpm.toPane.combo[0].getSelectedItem() = <%=cpm.toPane.combo[0].getSelectedItem()%><br>
            cpm.toPane.combo[1].getSelectedItem() = <%=cpm.toPane.combo[1].getSelectedItem()%><br>
        </p>
-->
<%

        if (actionButtonName.equals("Draw Chart")) {

            System.out.println("CPM.jsp, actionButtonName = Draw Chart");

            cpm.setFuelWC(fuel);
            cpm.setItemTitleWC(itemTitle);


%>
            <script>
                loadFrame("drawPieImg.jsp","imgFrame_c");
            </script>

<%

        } //if (actionButtonName.equals("Draw Chart"))



        else { //actions for other buttons goes here


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

<%

        } //else



    } //if(actionButtonName != null)


    else {


%>
        <script>
            loadFrame("blank.jsp","imgFrame_c");
        </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">Country</td>
            <!--td width="50%" BGCOLOR="#80FFFF"><%=mcpm.getDistrict()%></td-->
            <td width="50%" BGCOLOR="#80FFFF">Scotland</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">Data type:</td>
            <td width="60%" BGCOLOR="#80FFFF">
                <%
                    for(int i=0;i<dataTypeCheck.length;i++) {
                        if(dataType.equals(dataTypeValues[i])) {

                            %>
                                <input type="radio" name="dataType" value="<%=dataTypeValues[i]%>" CHECKED>
                                    <%=dataTypeCheck[i]%>
                                </input><br>
                            <%

                        }//if(dataType.equals(dataTypeCheck[i]))

                        else {

                            %>
                                <input type="radio" name="dataType" value="<%=dataTypeValues[i]%>">
                                    <%=dataTypeCheck[i]%>
                                </input><br>
                            <%

                        } //else

                    }//for(int i=0;i<dataTypeCheck.length;i++)

                %>

            </td>
        </tr>

        <tr>
            <td>
                Class no
                <select name="classNo" size="1">
                    <%
                        for(int i=0;i<numbers.length;i++) {

                            if(classNo.equals(String.valueOf(numbers[i]))) {

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

                            } //if(classNo.equals(String.valueOf(numbers[i])))

                            else {

                    %>
                                <option><%=numbers[i]%></option>
                    <%

                            } //else
                        } //for(int i=0;i<numbers.length;i++)

                    %>
                
                </select>
            </td>
            <td>
                Range option<br>
                <%
                    if(rangeOption.equals("custom")) {
                %>
                        <input type="radio" name="rangeOption" value="auto" >Auto</input>
                        <input type="radio" name="rangeOption" value="custom" checked>Custom</input>
                <%
                    } //if(rangeOption.equals("custom"))

                    else {
                %>
                        <input type="radio" name="rangeOption" value="auto" checked>Auto</input>
                        <input type="radio" name="rangeOption" value="custom">Custom</input>
                <%
                    } //else
                %>
            </td>
        </tr>

        <tr>
            <td>
                Input Range
            </td>
            <td>
                From: <input type="text" name="inputRangeFrom"><br>To: <input type="text" name="inputRangeTo">
            </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="Draw Chart" NAME="action">
            </td>
        <tr>

    </table>

</form>








</body>

</html>





