import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.mar.FormatRS;

import org.apache.log4j.Logger;

import uk.ac.roe.wfau.DatabaseConnection;
import uk.ac.roe.wfau.DatabaseURL;
import uk.ac.roe.wfau.FormatLines;
import uk.ac.roe.wfau.HTMLSnippets;
import uk.ac.roe.wfau.OSASchema;
import uk.ac.roe.wfau.SQLMethods;
import uk.ac.roe.wfau.StringToRADec;
import uk.ac.roe.wfau.VDFSHTMLSchema;
import uk.ac.roe.wfau.VDFSSchema;
import uk.ac.roe.wfau.VDFSSession;
import uk.ac.roe.wfau.VSASchema;
import uk.ac.roe.wfau.WSAHTMLSchema;
import uk.ac.roe.wfau.WSASchema;
import uk.ac.roe.wfau.WSASession;
import uk.ac.roe.wfau.stack2string;

import com.javaexchange.dbConnectionBroker.DbConnectionBroker;

public class ImageList extends HttpServlet {
    //private DBConnectionManager connMgr; // previous connection pool
    private DbConnectionBroker myBroker;
    private static final int RUNNUM_UPPERLIMIT = 99999;
    private static int RunNumber = 0; // 'Unique' counter
    private static String tempPath;
    private static String progPath;
    private static String tempURL;
    private static Logger logger;
    private static String filePrefix;
    private static String selectBase;
    private static String selectAll;
    private static String fromBase;
    private static String whereBase;
    private static String orderByBase;
    private static String dateParameter;
    //private static File dir;
    private static String WSACGIDIRURL;
    private static String BASEURL;
    //private String user;
    //private String passwd;
   
    private static String [] VSASERVERS=null;
    private static String [] WSASERVERS=null;
    private static String [] VSACUSERVERS=null;
    private static String [] WSACUSERVERS=null;
    private static String [] WSANSSERVERS=null;
    
    private static String defaultUKIDSSDatabase;
    private static String defaultWorldDatabase;
    private String server;
    private String proprietyServer;
    private static int defaultRowsPerPage=100;
    private static int maxRowsPerPage=1000;
    private static int rowsToRunOn=500;
    private static String CLOSINGHTML=  "<script language=\"JavaScript\" type=\"text/javascript\">\n <!--\n swap(0)\n -->\n </script>\n </body></html>";

    private static synchronized int getNextRunNumber() {
        RunNumber++;

        if (RunNumber > RUNNUM_UPPERLIMIT) {
            RunNumber = 1;
        }
        ;

        return RunNumber;
    };

    public void init() throws ServletException {
        //super.init(conf);
        logger=Logger.getLogger("wsa.simple");
        logger.debug("ImageList,initailizing Imagelist servlet");
        // read in global properties
        Properties properties = new Properties();
        String path = getServletContext().getRealPath("/");
        try {
            
            properties.load(new FileInputStream(path
                    + "WEB-INF/WSA.properties"));

        } catch (IOException e) {
            logger.error("ImageList,Can't find properties file ");
            return;
        }
 
        server = properties.getProperty("server");
        proprietyServer=properties.getProperty("proprietyServer");
        BASEURL=properties.getProperty("BASEURL");
        
        WSASERVERS=FormatLines.commaSeparatedStringToStringArray(properties.getProperty("WSASERVERS").trim());
        VSASERVERS=FormatLines.commaSeparatedStringToStringArray(properties.getProperty("VSASERVERS").trim());
        WSACUSERVERS=FormatLines.commaSeparatedStringToStringArray(properties.getProperty("WSACUSERVERS").trim());
        VSACUSERVERS=FormatLines.commaSeparatedStringToStringArray(properties.getProperty("VSACUSERVERS").trim());
        WSANSSERVERS=FormatLines.commaSeparatedStringToStringArray(properties.getProperty("WSANSSERVERS").trim());
        /*
        for (int i=0; i<WSASERVERS.length;i ++) {
        logger.info(WSASERVERS[i]);
        }
        for (int i=0; i<VSASERVERS.length;i ++) {
            logger.info(VSASERVERS[i]);
            }
            */
      // user = properties.getProperty("user");
      //  passwd = properties.getProperty("passwd");
        defaultUKIDSSDatabase = properties.getProperty("defaultUKIDSSDatabase");
        defaultWorldDatabase = properties.getProperty("defaultWorldDatabase");
       // String poolLogfile = properties.getProperty("pool.logfile");
        tempPath = properties.getProperty("genericTempPath");
        tempURL = properties.getProperty("genericTempURL");
 
        progPath = properties.getProperty("progPath");
        
        filePrefix = properties.getProperty("filePrefix");
        selectBase = properties.getProperty("ImageListSelect"); 
        selectAll = properties.getProperty("ImageListSelectAll");
        fromBase = properties.getProperty("ImageListFrom");
        whereBase = properties.getProperty("ImageListWhere");
        orderByBase = properties.getProperty("ImageListOrderBy");
        //dateParameter=properties.getProperty("ImageListDateParameter");
        dateParameter="m.dateObs";
        WSACGIDIRURL=properties.getProperty("WSACGIDIRURL");
        if (WSACGIDIRURL == null) {
            WSACGIDIRURL=WSAHTMLSchema.baseCGIURL;
        }
       /* do without pool for now
        try {
            myBroker = new DbConnectionBroker(
                    "com.microsoft.jdbc.sqlserver.SQLServerDriver",
                    "jdbc:microsoft:sqlserver://" + server
                            + ":1433;DatabaseName=" + database, user, passwd,
                    2, 10, poolLogfile, 0.25, false, 60, 1);
        } catch (IOException e) {
            logger.error("DBConnectionBroker I/O error "+stack2string.getString(e));
        }
        */
    }

    static {

        try {
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // A class not found error means the SQL driver class could not be
            // found.
            // Which means that this driver would not be available until this is
            // remedied. This flag variable will signal this to the class!
            logger.error("ImageList,SQLRetrieverThread::static(): Could not load SQLServerDriver class! "+stack2string.getString(e));
          }
          
    }

    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        HttpSession session = req.getSession();
        boolean wgetFile = false;
        String archive="WSA";
        
        if (req.getParameter("archive") != null) {
            archive=req.getParameter("archive");
        }
        int archiveID=VDFSSchema.getArchiveID(archive);
        logger.info(archive);
        PrintWriter wgetImgWriter=null;
        PrintWriter wgetCatWriter=null;
        boolean loginboolean=false;
        

        try {
        //loginboolean=((Boolean)session.getAttribute("login")).booleanValue();
        loginboolean=VDFSSession.getLoginBoolean(session,archive);
        }
        catch (Exception e){
            loginboolean=false;
        }
        boolean nsboolean=false;
        try {
        nsboolean=((Boolean)session.getAttribute("nonsurvey")).booleanValue();
        
        }
        catch (Exception e){
            nsboolean=false;
            
        }
        logger.info(archive+" 1");
        res.setContentType("text/html");
        if (BASEURL==null) {
            BASEURL=VDFSHTMLSchema.getBaseURL(archiveID);
        }
        PrintWriter out = res.getWriter();
        out.print(HTMLSnippets.getStartPageWithTitleAndBody(archive +" ImageList","onLoad=\"swap(0);\"","",BASEURL+"imSwap.js"));
        out.flush();
        //out.println(DatabaseURL.getDatabaseURL(session,));
        int rID=getNextRunNumber();
        boolean openTime=false;
        boolean proceed=true;
        boolean showConf=false;
        boolean showCal=true;
        StringBuffer select=new StringBuffer(selectBase);
        StringBuffer from=new StringBuffer(fromBase);
        StringBuffer where=new StringBuffer(whereBase);
        StringBuffer order=new StringBuffer(orderByBase);
        
        if (archive.equalsIgnoreCase("vsa")) {
            select.append(",vistaRunNo,obsName");
            order.append(",vistaRunNo asc");
    
        }
        else if (archive.equalsIgnoreCase("osa")) {
            select.append(",vstRunNo");
            order.append(",vstRunNo asc");
    
        }
        else {
            select.append(",m.ukirtRunNo");
            order.append(",m.ukirtRunNo asc");
        }
        logger.info(archive+" 1a");
        String userSess=VDFSSession.getUser(session,archiveID,false);
        logger.info(archive+" 2a");
        String genUser=VDFSSession.getUser(session,archiveID,true);
        logger.info(archive+" 2b");
        String community=VDFSSession.getCommunity(session,archiveID);
        out.println("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><b>"+archive+" ImageList</b></td><td>&nbsp;&nbsp;&nbsp;</td>");
        out.println("<td><img name=\"imgMain\" src=\""+BASEURL+"static.gif\" border=\"1\"></td></table><p>");
        out.flush();
        if (!loginboolean){
            out.println("Not logged in: links will only be returned " +
            		"for frames that are publicly accessible<p>");
        }
        out.println("Archive Listing<p>Searching...<br>");
        logger.info(archive+" 2");
        String database=req.getParameter("database");
        if (req.getParameter("dlFile") != null && (req.getParameter("dlFile").equalsIgnoreCase("yes")|| req.getParameter("dlFile").equalsIgnoreCase("y"))) {
            wgetFile = true;
            logger.info("ImageList,Login,"+userSess+","+community+","+req.getRemoteAddr()+", wgetFile");
        }
        String userSelect=req.getParameter("userSelect");
        if (userSelect != null){
            if (userSelect.equals("all")){
                select=new StringBuffer(selectAll);
            }
        }
        
        String programmeID=null;
        if ((community.equalsIgnoreCase("nonSurvey") || VDFSSchema.isProprietary(community,archiveID) )&& VDFSSession.getLoginBoolean(session,archive)) {
            programmeID=VDFSSession.getProgID(session,archiveID);
            select.append(",object");
            
        } else {
        programmeID=req.getParameter("programmeID");
        }
        //out.print(programmeID);
        if (programmeID != null){
            if (programmeID.equalsIgnoreCase("openTime")){
                openTime=true;
                programmeID=req.getParameter("openTimeID");
            }
        }
        
        if (req.getParameter("showCal") !=null && req.getParameter("showCal").toLowerCase().startsWith("n")) {
            showCal=false;
        }
        
        if (req.getParameter("showConf") !=null && req.getParameter("showConf").equalsIgnoreCase("y")) {
            showConf=true;
        }
        String filterID=req.getParameter("filterID");
        String suppMinRA=req.getParameter("minRA");
        String suppMaxRA=req.getParameter("maxRA");
        String suppMinDec=req.getParameter("minDec");
        String suppMaxDec=req.getParameter("maxDec");
        String startYear=req.getParameter("startYear");
        String startMonth=req.getParameter("startMonth");
        String startDay=req.getParameter("startDay");
        String endYear=req.getParameter("endYear");
        String endMonth=req.getParameter("endMonth");
        String endDay=req.getParameter("endDay");
        String obsType=req.getParameter("obsType");
        String frameType=req.getParameter("frameType");
        String skipRow=req.getParameter("skipRow");
        String pCount=req.getParameter("pCount");
        String suppliedMFID=req.getParameter("mfid");
        String suppliedLMFID=req.getParameter("lmfid");
        String framesetID=req.getParameter("fsid");
        String publicOption=req.getParameter("po");
        String deprecated="0";
            if (req.getParameter("dep") !=null) {
                deprecated=req.getParameter("dep");
                if (!deprecated.equals("0")) {
                    select.insert(0,"m.deprecated,");           
            }
            }
            if (deprecated.equals("0")) {
                where.append(" and (M.deprecated=0 or M.deprecated=111) ");
            }
        String formatRA="hours";
        if (req.getParameter("formatRA") != null) {
        formatRA=req.getParameter("formatRA");
        }
        String formatDec="degrees";
            if (req.getParameter("formatDec") != null) {
                formatDec=req.getParameter("formatDec") ;
            }
        
        String pageRows=req.getParameter("rows");
        int rowsPerPage=defaultRowsPerPage;
        if (pageRows != null ){
            try{
            rowsPerPage=Math.min(maxRowsPerPage,Integer.valueOf(pageRows).intValue());
            }
            catch (NumberFormatException nfe){
                rowsPerPage=defaultRowsPerPage;
            }
        }
        
        if (database.equalsIgnoreCase("none")) {
            proceed=false;
            out.println("<b>Error: No valid database.</b><br>");
        }
        
        
        int skipRowInt=0;
        if (programmeID != null ){
            try{
                int iPID=Integer.valueOf(programmeID).intValue();
            if (iPID < 1000) {                       
            out.println("<b>Survey: </b>"+VDFSSchema.getSurveyName(Integer.valueOf(programmeID).intValue(),archiveID)+"<br>");
            }
            if (iPID == 1000) {                       
                out.println("<!-- opentime+uds --><b>Opentime: </b><br>");
                }
            
            if (!loginboolean && database.equalsIgnoreCase("ukidssdr8plus") && (iPID==102)){ // || iPID==105)) {
                proceed=false;
                out.println("<b>Warning: GPS not yet publically available in UKIDSSDR8PLUS, please use an earlier version.</b><br>");
            }
            
            if ((iPID > 1000 && ( !community.equalsIgnoreCase("nonsurvey") && !community.equalsIgnoreCase(VSASchema.PROPRIETARYCOMMUNITY) ) ) || (iPID < 1000 && community.equalsIgnoreCase("nonsurvey"))) {
                proceed=false;
                out.println("<b>Error: Invalid programmeID.</b><br>");
            }
            if (archiveID==VDFSSchema.VSAARCHIVEID) {
                if (iPID < 20000) {
                    where.append(" and (PF.programmeID="+programmeID);
                    if (showCal) {
                    where.append(" or PF.programmeID="+VSASchema.CALPROGRAMMEID);
                    }
                    where.append(")");
                }
                else {
                    where.append(" and (PF.programmeID="+programmeID+")");  
                }
            }
            else if (archiveID==VDFSSchema.OSAARCHIVEID) {
                if (iPID < 200) {
                    where.append(" and (PF.programmeID="+programmeID);
                    if (showCal) {
                    where.append(" or PF.programmeID="+OSASchema.CALPROGRAMMEID + " or PF.programmeID="+OSASchema.TECHPROGRAMMEID);
                    }
                    where.append(")");
                }
                else {
                    where.append(" and (PF.programmeID="+programmeID+")");  
                }
            }
            else {
            if (iPID != WSASchema.CALPROGRAMMEID) {
                if (community.equalsIgnoreCase("nonsurvey") && iPID==99999999) {
                    //out.print (userSess);
                    String proj="";
                    String [] possProj={"uukidss","userv","ucmp","u05a","u05b","u06a","u06b","u07a","u07b","u08a","u08b","u09a","u09b","u10a","u10b","u11a","u11b","u12a","u12b"};
                    for (int i=0; i<possProj.length; i++){
                        if (userSess.toLowerCase().indexOf(possProj[i]) > -1) {
                            proj="u/"+possProj[i].substring(1)+"/"+userSess.substring(possProj[i].length());
                            out.print(" <b>"+proj+"</b><br>");
                        }
                    }
                    where.append(" and m.project like '"+proj+"' "); 
                }
                else {
                    if (iPID != 1000) {
                    	//showcal
                        where.append(" and (PF.programmeID="+programmeID);
                        if (showCal) {
                        	where.append(" or PF.programmeID="+WSASchema.CALPROGRAMMEID);
                        }
                        where.append(" )");
                    }
                    else {
                        where.append(" and (PF.programmeID>"+programmeID+" or PF.programmeID="+WSASchema.UKIDSSUDSPROGRAMMEID+")");
                    }
                	}
                }
            else {
                where.append(" and (PF.programmeID="+WSASchema.CALPROGRAMMEID+" or PF.programmeID="+WSASchema.OTHERCALPROGRAMMEID+")"); 
            }
            }
            
            }
            
            catch (NumberFormatException nfe){
                if (programmeID.equalsIgnoreCase("all")) {
                    
                    out.println("<b>Survey/programme:</b> "+VDFSSchema.getAllSurveys(archiveID)+"<br>");
                    if (archiveID==VDFSSchema.VSAARCHIVEID && !database.equalsIgnoreCase(VSASchema.OPENDB)) {
                        where.append(" and (PF.programmeID < 1000 and PF.programmeID > 0) ");  
                        if (!showCal) {
                        	where.append(" and PF.programmeid != "+VSASchema.CALPROGRAMMEID+" ");
                        }
                    }
                    else if (archiveID==VDFSSchema.VSAARCHIVEID && database.equalsIgnoreCase(VSASchema.OPENDB)){
                    	where.append(" and (PF.programmeID > 1000 or PF.programmeid="+VSASchema.CALPROGRAMMEID+") "); 
                    	  if (!showCal) {
                          	where.append(" and PF.programmeid != "+VSASchema.CALPROGRAMMEID+" ");
                          }
                    }
                    else {
                        if (!loginboolean && database.equalsIgnoreCase("ukidssdr8plus")) {
                            where.append(" and (PF.programmeID < 1000 and PF.programmeID > 10 and PF.programmeID != 102 ) ");
                            out.print("(LAS, GCS, DXS and UDS only)<br>");
                        }else {
                    where.append(" and (PF.programmeID < 1000 and PF.programmeID > 10) ");
                        }
                    }
                } else {
                    proceed=false;
                    out.println("<b>Error: Invalid programmeID.</b><br>");
                    
                }
            }
        }
                /*
         * ?????
         */
        else {
            proceed=false;
            out.println("<b>Error: Invalid programmeID.</b><br>"); 
        }
        
        if (archiveID==VDFSSchema.WSAARCHIVEID && !loginboolean ) {
       	 where.append(" and (PF.programmeID != 107) "); 
       }
        logger.info("ImageList,Login,"+userSess+","+community+","+req.getRemoteAddr()+","+programmeID);
        if (suppliedMFID != null && !suppliedMFID.equals("")) {
            try {
                long lMFID=Long.valueOf(suppliedMFID.trim()).longValue();
                out.println("<b>MultiframeID: </b>"+suppliedMFID+"<br>");
                where.append(" and (M.multiframeID="+suppliedMFID+")");
            }
            catch (NumberFormatException nfe){
                proceed=false;
                out.println("<b>Error: The supplied multiframeID</b> "+suppliedMFID+" <b>is not a valid number.</b><br>");
            }
        }
        StringBuffer inClause=new StringBuffer("");
        if (suppliedLMFID != null && !suppliedLMFID.equals("") && !suppliedLMFID.equals(" ")) {
            String listmfid=suppliedLMFID.replaceAll("\r?\n"," ").replace(',',' '); // replace line feeds and commas
            String [] mfidArray=listmfid.trim().split("\\s+");
            
            boolean first=true;
            int numValid=0;
            for (int n=0; n < mfidArray.length;n++) {
                try {
                    Long.valueOf(mfidArray[n]);
                    numValid++;
                    if (!first) {
                        inClause.append(","+mfidArray[n]);
                    }
                    else {
                    inClause.append(mfidArray[n]);
                    first=false;
                    }
                }
                catch (NumberFormatException nfe) {
                    out.println("<br><b>Invalid MFID</b> "+mfidArray[n]);
                }
            }
            
            out.print("<br><b>Number of valid multiframeIDs</b> "+numValid+"<br>");
            if (numValid > 0 && numValid <= 100) {
                where.append(" and (m.multiframeid in ("+inClause+"))");
            }
            else {
                proceed = false;
                out.print("<br><b>Please only supply up to 100 multiframeIDs</b><br>");
            }
        }
       
        
        
        
        // frameset
        String mergeLog=null;
        long lPID=0;
        
        if (framesetID != null && !framesetID.equals("")) {
            try {
                long lFSID=Long.valueOf(framesetID.trim()).longValue();
                long maska=0xffff00000000L;
                long maskb=0x100000000L;
                lPID=(lFSID & maska)/maskb;
                
                if (community != null){
                if (community.equalsIgnoreCase("nonsurvey")) {
                    mergeLog=userSess+"mergelog";
                }
           
                else {
                    mergeLog=VDFSSchema.getMergeTableName((int)lPID,archive);
                    if (mergeLog == null) {
                        mergeLog=genUser+"mergelog";
                    }
                }
                }

                if (mergeLog == null) {
                    out.println("<b>Error: Unable to resolve mergelog name from framesetID.</b><br>");
                    proceed=false; 
                }
                
                
            }
            catch (NumberFormatException nfe){
                out.println("<b>Error: The supplied framesetID</b> "+framesetID+" <b>is not a valid number.</b><br>");
                proceed=false;
            }
            
            
        }
        int pCountInt=0;
        if (pCount != null){
            try {
            pCountInt=Integer.valueOf(pCount).intValue();
            }
            catch (Exception e) {
                pCountInt=0;
            }
        }
        if (skipRow != null){
            try {
            skipRowInt=Integer.valueOf(skipRow).intValue();
            }
            catch (Exception e) {
                skipRowInt=0;
            }
        }
        
        if (publicOption !=null && publicOption.equals("y")) {
            where.append(" and ( filename not like '%none%') ");
        }
        
        if (obsType != null){
            if (obsType.equalsIgnoreCase("object")) {
                where.append(" and ( "+
                        "m.obstype NOT LIKE 'BIAS%' AND m.frametype NOT LIKE 'BIAS%' AND "+
                        "m.obstype NOT LIKE 'DARK%' AND m.frametype NOT LIKE 'DARK%' AND "+
                        "m.obstype NOT LIKE 'SKY%' AND m.frametype NOT LIKE 'SKY%' AND "+
                        "m.obstype NOT LIKE 'FOCUS%' AND m.frametype NOT LIKE 'FOCUS%' AND "+
                        "m.obstype NOT LIKE 'CONFIDENCE%' AND m.frametype NOT LIKE 'CONFIDENCE%' AND "+
                        "m.obstype NOT LIKE '%FLAT%' AND m.frametype NOT LIKE '%FLAT%'"+
                        ") ");
            }
            else if (obsType.equalsIgnoreCase("bias")){
                where.append(" and ( m.obstype like 'BIAS%' or m.frametype like 'BIAS%') ");               
            }
            else if (obsType.equalsIgnoreCase("dark")){
                where.append(" and ( m.obstype like 'DARK%' or m.frametype like 'DARK%') ");               
            }
            else if (obsType.equalsIgnoreCase("flat")){
                where.append(" and ( m.obstype like '%FLAT%' or m.frametype like '%FLAT%') ");               
            }
            else if (obsType.equalsIgnoreCase("sky")){
                where.append(" and ( m.obstype like 'SKY%' or m.frametype like 'SKY%') ");               
            }
            else if (obsType.equalsIgnoreCase("focus")){
                where.append(" and ( m.obstype like 'FOCUS%' or m.frametype like 'FOCUS%') ");               
            }
            else if (obsType.equalsIgnoreCase("confidence")){
                where.append(" and ( m.obstype like 'CONFIDENCE%' or m.frametype like 'CONFIDENCE%') ");               
            }
            
        }
        
        if (frameType != null && !frameType.equalsIgnoreCase("all")){
                where.append(" and ( "+
                        "m.frametype LIKE '%"+frameType+"%') ");
            }
        
        
        
        if (filterID != null  && !filterID.equals("all")){
            out.println("<b>Waveband: </b>"+VDFSSchema.getFilterName(Integer.valueOf(filterID).intValue(),archiveID)+"<br>");
            where.append(" and (M.filterID="+filterID+")");
        }   
        
        
        double minRAValue=0.0;
        double maxRAValue=24.0;
        double minDecValue=-90.0;
        double maxDecValue=90.0;
        
        if (suppMinRA == null || suppMinRA.equals("")) {
            suppMinRA="0.0";
        }
        if (suppMaxRA == null || suppMaxRA.equals("")) {
            suppMaxRA="24.0";
        }
        if (suppMinDec == null || suppMinDec.equals("")) {
            suppMinDec="-90.0";
        }
        if (suppMaxDec == null || suppMaxDec.equals("")) {
            suppMaxDec="90.0";
        }
            if (formatRA.equalsIgnoreCase("degrees")){
                try {
                    minRAValue=Double.valueOf(suppMinRA).doubleValue()/15.0;
                    maxRAValue=Double.valueOf(suppMaxRA).doubleValue()/15.0;
                }
                catch (NumberFormatException nfe) {
                    out.println("<b>Error: check supplied minimum/maximum RA</b><br>");
                    proceed=false;  
                }
            }
            else if (formatRA.equalsIgnoreCase("sexagesimal")){
                try {
                minRAValue=StringToRADec.coordStringToDouble(suppMinRA,"RA",false)/15.0;
                }
                catch (NumberFormatException nfe) {
                    out.println("<b>Error: check supplied minimum RA, unable to parse sexagesimal string</b><br>");
                    proceed=false;  
                }
                try{
                maxRAValue=StringToRADec.coordStringToDouble(suppMaxRA,"RA",false)/15.0;
                
                }
                catch (NumberFormatException nfe) {
                    out.println("<b>Error: check supplied maximum RA, unable to parse sexagesimal string</b><br>");
                    proceed=false;  
                }
            }
            else {
                try {
                    minRAValue=Double.valueOf(suppMinRA).doubleValue();
                }
                    catch (NumberFormatException nfe) {
                        out.println("<b>Error: check supplied minimum RA</b><br>");
                        proceed=false;  
                    }
                    try{
                    maxRAValue=Double.valueOf(suppMaxRA).doubleValue();
                }
                catch (NumberFormatException nfe) {
                    out.println("<b>Error: check supplied maximum RA</b><br>");
                    proceed=false;  
                }
            }
        
        String minRA=String.valueOf(minRAValue);
        String maxRA=String.valueOf(maxRAValue);
        
        if (formatDec.equalsIgnoreCase("sexagesimal")){
            try {
                
                minDecValue=StringToRADec.coordStringToDouble(suppMinDec,"Dec",false);
            }
                catch (NumberFormatException nfe) {
                    out.println("<b>Error: check supplied minimum Dec, unable to parse sexagesimal string</b><br>");
                    proceed=false;  
                }
                try{
                maxDecValue=StringToRADec.coordStringToDouble(suppMaxDec,"Dec",false);
            }
            catch (NumberFormatException nfe) {
                out.println("<b>Error: check supplied maximum Dec, unable to parse sexagesimal string</b><br>");
                proceed=false;  
            }
        }
        
        else {
            try {
                minDecValue=Double.valueOf(suppMinDec).doubleValue();
            }
            catch (NumberFormatException nfe) {
                out.println("<b>Error: check supplied minimum Dec</b><br>");
                proceed=false;  
            }
            try {
                maxDecValue=Double.valueOf(suppMaxDec).doubleValue();
            }
            catch (NumberFormatException nfe) {
                out.println("<b>Error: check supplied minimum/maximum Dec</b><br>");
                proceed=false;  
            }
        }
        
        String minDec=String.valueOf(minDecValue);
        String maxDec=String.valueOf(maxDecValue);
        
        
        if (minRA != null || maxRA != null){
            
            float minra=0.0F;
            float maxra=24.0F;
            try {
            minra=Float.valueOf(minRA).floatValue();
            }            
            catch (Exception e){
                // user can't enter a number
                minRA=null;
            }
            try {
            maxra=Float.valueOf(maxRA).floatValue();
            }            
            catch (Exception e){
                // user can't enter a number
                maxRA=null;
            }
            if ((minRA ==null && maxRA == null) || (minRAValue==0.0 && maxRAValue==24.0)){
                // do nothing
            }
            else if (minRA == null && maxRA != null) {
                where.append(" and (m.rabase <= " +maxRA +")");
            } 
            else if (maxRA == null && minRA != null) {
                where.append(" and (m.rabase >= " +minRA +")");
            }
            else {    
            if (minra > maxra) {
                where.append(" and ((m.rabase >= " + minra +" and m.rabase <=24.0) or " +
                		"(m.rabase <= "+maxra+" ))");
            }
            else {
                where.append(" and (m.rabase >= "+minra+" and m.rabase <= "+maxra+") ");
            }
        }
            
            if (minra < 0.0 || maxra > 24.0 || minra > 24.0 || maxra < 0){
                out.println("<b>Warning: The supplied RAs should be between 0.0 and 24.0 hours</b><br>");
                proceed=false;
            }          
        }
        
        
        if (minDec != null || maxDec != null){
            float mindec=-90.0F;
            float maxdec=90.0F;
            try {
            mindec=Float.valueOf(minDec).floatValue();
            }
            catch (Exception e){
                // user can't enter a number
                minDec=null;
            }
            try {
            maxdec=Float.valueOf(maxDec).floatValue();
            }
            catch (Exception e){
                // user can't enter a number
                maxDec=null;
            }
            if ((minDec ==null && maxDec == null) || (minDecValue == -90.0 && maxDecValue==90.0)){
                // do nothing
            }
            else if (minDec == null && maxDec != null) {
                where.append(" and (m.decbase <= " +maxDec +")");
            } 
            else if (maxDec == null && minDec != null) {
                where.append(" and (m.decbase >= " +minDec +")");
            }
            else {
            where.append(" and (m.decbase >= " + minDec +" and m.decbase <= " + maxDec+" ) ");
            }
            
            if (mindec < -90.0 || maxdec > 90.0){
                out.println("<b>Warning: The supplied DECs should be between -90.0 and +90.0 degrees</b><br>");
                proceed=false;
            }          
                         
            if (mindec > maxdec){
                out.println("<b>Warning: min Dec &gt; max Dec.</b><br>");
                proceed=false;
            }
        }
        
        if (proceed) {
            out.println("<b>Minimum RA:</b> "+minRA+" hours <b>Maximum RA:</b> "+maxRA+" hours <br>");
            out.println("<b>Minimum Dec:</b> "+minDec+" degrees <b>Maximum Dec:</b> "+maxDec+" degrees <br>");     
        }
        int maxDay=31;
        Calendar cal = Calendar.getInstance();
        cal.clear();
        
        int year = 0;
        int month =0;
        int day =0;
        long startTime=0;
        long endTime=0;
        
        if (startYear != null && startMonth != null && startDay!=null) {
        year=Integer.valueOf(startYear).intValue();
        month = Integer.valueOf(startMonth).intValue();
        day = Integer.valueOf(startDay).intValue();
        if (day == 0) {
            day=1;
        }
        if (month == 0) {
            month=1;
        }
  
        
        if (year !=0 && month !=0 && day !=0){
        cal.set(year,month-1,1);
        maxDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        
        if (day > maxDay){
            cal.set(year,month-1,maxDay,0,0,0);
                    }
        else {
            cal.set(year,month-1,day,0,0,0);
        }
        startTime=cal.getTimeInMillis();
        String startSQLDate = DateFormat.getDateInstance(DateFormat.LONG).format(cal.getTime());
        where.append(" and ("+dateParameter+" >= '"+startSQLDate+"') ");
        out.println("<b>Start date: </b>" +startSQLDate+"<br>");
        }
        }
        
        
        cal.clear();
        if (endYear != null && endMonth != null && endDay!=null) {
         year = Integer.valueOf(endYear).intValue();
         month = Integer.valueOf(endMonth).intValue();
         day = Integer.valueOf(endDay).intValue();
         if (day == 0) {
             day=1;
         }
         if (month == 0) {
             month=1;
         }
        if (year !=0 && month !=0 && day !=0){
        cal.set(year,month-1,1);
        maxDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        
        if (day > maxDay){
            cal.set(year,month-1,maxDay,23,59,59);
                    }
        else {
            cal.set(year,month-1,day,23,59,59);
        }
        endTime=cal.getTimeInMillis();
        String endSQLDate = DateFormat.getDateInstance(DateFormat.LONG).format(cal.getTime());
        where.append(" and ("+dateParameter+" <= '23:59:59.9 "+endSQLDate+"') ");
        out.println("<b>End date: </b>" +endSQLDate+"<br>");
        }
        }
        if (startTime > endTime && endTime !=0 ){
            out.println("<b>Warning: start date is after end date.</b>");
            proceed=false;
        }
        if (!proceed) {
            out.println("<p><b>Query stopped: check errors/warnings above and try again.</b>");
            out.println(CLOSINGHTML);
            out.flush();
            return;
        }

               StringBuffer sql=new StringBuffer("");
        
               
        String UKIDSSDatabase=defaultUKIDSSDatabase.toString();
        String worldDatabase=defaultWorldDatabase.toString();
        if (database == null) {
            
            if (community.equalsIgnoreCase("nonSurvey")) {
              
                database = userSess;
            } else {
                if (loginboolean) {
                    
                    database = defaultUKIDSSDatabase.toString();
                } else {
                    
                    database = defaultWorldDatabase.toString();
                }
            }
        } else {
            
            UKIDSSDatabase = database;
            worldDatabase = database;
        }
        
        Connection con = null;
        ResultSet rs = null;
        Statement stmt = null;
        File wgetImgFile=null;
        File wgetCatFile=null;
 
        try {            
           // con = myBroker.getConnection();
            /*
            String durl=null;
            if (database.equalsIgnoreCase(WSASchema.rollingDB) && (loginboolean) && !community.equalsIgnoreCase("prerelease")) {
               durl=DatabaseURL.getDatabaseURL(proprietyServer,WSASchema.actualRollingDB,WSASchema.rollingDBUser,WSASchema.rollingDBPasswd,true);
               // durl=DatabaseURL.getDatabaseURL(WSASchema.rollingServer,"testRSC",WSASchema.rollingDBUser,WSASchema.rollingDBPasswd,true);
               logger.info("IMAGELIST DBURL nopre "+durl);
            }
            else {
            durl=DatabaseURL.getDatabaseURL(session,true,server,UKIDSSDatabase,worldDatabase);
            logger.info("IMAGELIST DBURL "+durl);
            }
            */
            String [] servers = VDFSSession.getServers(community,archive,WSASERVERS,WSACUSERVERS,VSASERVERS,VSACUSERVERS,WSANSSERVERS);
 
            out.println("<script language=\"JavaScript\" type=\"text/javascript\"> <!-- ");
            out.println("swap(1);");
            out.println("--> </script>");
            out.flush();
            try {
                Thread.sleep(1000);
            }
            catch (Exception e) {
                
            }
            //out.println("<br><b>Using database: ****WARNING QUERYING A RECENT COPY OF THE WSA (whilst the WSA is offline)****** </b> ");
            //out.println("<br><b>Using database:</b> "+DatabaseURL.getDatabaseNameFromURL(durl,true));
            out.println("<br><b>Using database:</b> "+database);
            out.flush();
            res.flushBuffer();
          //  String durl="jdbc:jtds:sqlserver://"+server+":1433/" + database+";user="+user+";password="+passwd;
            //String durl= "jdbc:microsoft:sqlserver://"+server+":1433;selectMethod=cursor;databaseName="+database+";user="+user+";password="+passwd+"";
            if (archiveID==VDFSSchema.VSAARCHIVEID && VSASchema.isProprietary(community) && 
                    (database.equalsIgnoreCase(VSASchema.rollingDB) || database.equalsIgnoreCase(VSASchema.PRERELEASEDATABASE))) {
                database=VSASchema.actualRollingDB;
              //  out.print(" ("+database+")");
            }
            if (archiveID==VDFSSchema.OSAARCHIVEID && OSASchema.isProprietary(community) && 
                    (database.equalsIgnoreCase(OSASchema.rollingDB) || database.equalsIgnoreCase(OSASchema.PRERELEASEDATABASE))) {
                database=OSASchema.actualRollingDB;
               // out.print(" ("+database+")");
            }
            //con = DriverManager.getConnection(durl);
            con=DatabaseConnection.getConnection(servers,database,archive,session);
            servers=null;
            if (con == null) {
                out.println("Can't get database connection");
                out.println(CLOSINGHTML);
                out.flush();
                return;
            }
            con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
            ResultSetMetaData md = null;
            try {
                //stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                stmt = con.createStatement();
                stmt.setQueryTimeout(1200);
                //stmt.executeUpdate("use wsa");

                //            rs = stmt.executeQuery("SELECT
                // MultiFrameDetector.extNum,MultiFrame.filename,CurrentAstrometry.*,
                // MultiframeDetector.axis1Length,MultiframeDetector.axis2Length,numaxes
                // FROM Multiframe INNER JOIN MultiframeDetector ON
                // Multiframe.multiframeID = MultiframeDetector.multiframeID
                // INNER JOIN CurrentAstrometry ON Multiframe.multiframeID =
                // CurrentAstrometry.multiframeID AND MultiframeDetector.extNum
                // = CurrentAstrometry.extNum");
                
                if (mergeLog != null) {
                    
                    rs = stmt.executeQuery(SQLMethods.getRequiredFiltersSQL()+" where programmeID= "+lPID);
                    StringBuffer framesetSQL=SQLMethods.getFramesetSQL(rs,mergeLog,"m",archiveID);
                    
                    if (framesetSQL==null){
                        out.print("<br><b>Unable to construct frameset SQL.</b><br>");
                        con.close();
                        out.println(CLOSINGHTML);
                        out.flush();
                        return;
                    }
                    
                    from.append(","+mergeLog);
                    where.append(" and framesetid="+framesetID+" and ("+framesetSQL+")");
                    
                    rs.close();	
                }
                
                if (showConf) {
                    select.append(",cmf.filename as confFileName,m.confID ");
                  
          
                }
                if ((database.equalsIgnoreCase(WSASchema.rollingDB) || database.equalsIgnoreCase(VSASchema.actualRollingDB) || database.equalsIgnoreCase(VSASchema.rollingDB) || community.equalsIgnoreCase("prerelease")) && !community.equalsIgnoreCase("nonsurvey")){
                    select.append(",cast (2.0*degrees(asin(sqrt(square(sin(radians(0.5*(m.decbase-m.decmoon)))) + cos(radians(m.decbase))*cos(radians (m.decmoon))* square(sin(radians(7.5*(m.rabase-m.ramoon))))))) as int) as moondist");
                }
                
                int top=skipRowInt+rowsToRunOn+1+rowsPerPage;
                sql.append("select distinct top "+top+" "+select+" from "+from+" where "+where+" "+order);
                StringBuffer newSql =new StringBuffer("");
                newSql.append("select "+select+" from (select distinct substring(compfile,1, len(compfile)-charindex('_',reverse(compfile))) as jpegBase, t.multiframeid from " +
                		"(select top "+top+" m.multiframeid from "+from+" where "+where+" "+order+
                		") as t,multiframedetector as mfd where mfd.multiframeid=t.multiframeid) as t1, multiframe as m, filter as F ");
                
                if (archive.equalsIgnoreCase(VDFSSchema.VSAARCHIVE)) {
                    newSql.append(",MultiframeEsoKeys as me ");
                }
                if (showConf) {
                    newSql.append(",multiframe as cmf ");
                }
                /*
                 *         from.append(",MultiframeEsoKeys as me");
            where.append(" and me.multiframeid=m.multiframeid");
                 */
                newSql.append("where t1.multiframeid=m.multiframeid and F.filterid=M.filterid ");
                if (archive.equalsIgnoreCase(VDFSSchema.VSAARCHIVE)) {
                    newSql.append(" and me.multiframeid=m.multiframeid ");
                }
                if (showConf) {
                    newSql.append(" and cmf.multiframeid=m.confid ");
                }
                newSql.append(order);
                
                logger.info("Imagelist SQL: "+newSql.toString());            
             //   logger.info("Imagelist SQL: "+sql.toString());
                rs = stmt.executeQuery(newSql.toString());
               
                md = rs.getMetaData();
               /* rs.afterLast();
                rs.last(); 
                rs.absolute(10);
*/
                
               // int records = rs.getRow(); 
               // out.println(records);
                FormatRS frs=new FormatRS();
                frs.setRSMD(md);
                int row = 0;
                int filenameColumn=0;
                int confFilenameColumn=0;
                int depColumn=-999;
                boolean setDepCol=false;
                int multiframeidColumn=0;
                int toGoColumn=0;
                int numDetColumn=0;
                int catnameColumn=0;
                int compFileColumn=0;
                int jpegBaseColumn=0;
                int numDetectorsColumn=0;
                String[] strArray;
                String tableName;
                String columnName;
                String startLetter;
                String glossLink;
                int resultsNumColumns = md.getColumnCount();
                strArray = new String[resultsNumColumns];
                out.println("<p><table><tr><td align=\"right\"><b>View</b> column link</td><td align=\"left\">shows jpeg images of multiframe in a new window plus links to download file(s)</td>");
                out.println("<tr><td align=\"right\"><b>Img</b> column link</td><td align=\"left\">download the <a href=\"" +
                		VDFSHTMLSchema.getBaseURL(archiveID)+"qa.html#compress\">RICE compressed</a> FITS image file. Use <b>View</b> column link to retrieve uncompressed images.</td>");
                out.println("<tr><td align=\"right\"><b>Cat</b> column link</td><td align=\"left\">download the FITS catalogue file.</td>");
                if (showConf)
                {                out.println("<tr><td align=\"right\"><b>Conf</b> column link</td><td align=\"left\">download the image confidence file.</td>");
                }
                out.println("</table>");
                if (wgetFile) {
                    out.println("Links to wget scripts will appear at the end of this page.<br>");
                }              
                out.println("<br>begin row "+(skipRowInt+1)+"<br><table border=\"1\"><tr bgcolor=\""+VDFSHTMLSchema.headRow+"\"><td align=\"center\">View</td>" +
                		"<td align=\"center\">Img</td><td align=\"center\">Cat</td>");
                if (showConf) {
                    out.println("<td align=\"center\">Conf</td>");
                }
                for (int column = 1; column <= resultsNumColumns; column++){
                    glossLink="";
                    try {
                    tableName=md.getTableName(column).toLowerCase();
                    //out.println("::"+tableName);
                    columnName=md.getColumnName(column).toLowerCase();
                    if (tableName != null && !tableName.equals("") && columnName != null && !columnName.equals("")){
                        startLetter=columnName.substring(0,1);
                        glossLink="<a href="+VDFSHTMLSchema.getBaseURL(archiveID)+"gloss_"+startLetter+".html#"+tableName+"_"
                        +columnName+">";
                    }
                    } catch (Exception e){
                        out.println(e);
                        
                    }
                    if (md.getColumnName(column).equalsIgnoreCase("filename")){
                        filenameColumn=column;
                    }
                    else if (md.getColumnName(column).equalsIgnoreCase("numDetectors")){
                        numDetectorsColumn=column;
                        out.println("<td align=\"middle\"><b>"+md.getColumnName(column)+"</b></td>");
   
                    }
                    else if (md.getColumnName(column).equalsIgnoreCase("toGo")){
                        toGoColumn=column;  
                    }
                    else if (md.getColumnName(column).equalsIgnoreCase("catname")){
                        catnameColumn=column;
                     }
                    else if (md.getColumnName(column).equalsIgnoreCase("compfile")){
                        compFileColumn=column;
                     }
                    else if (md.getColumnName(column).equalsIgnoreCase("jpegBase")){
                        jpegBaseColumn=column;
                     }
                    else if (md.getColumnName(column).equalsIgnoreCase("multiframeid")){
                        multiframeidColumn=column;
                        out.println("<td align=\"middle\"><b>"+md.getColumnName(column)+"</b></td>");
                     }
                    else if (md.getColumnName(column).equalsIgnoreCase("deprecated") && !setDepCol){
                        depColumn=column; // first one
                        setDepCol=true;
                        out.println("<td align=\"middle\"><b>"+md.getColumnName(column)+"</b></td>");
                     }
                    else if (md.getColumnName(column).equalsIgnoreCase("confFileName")){
                        confFilenameColumn=column;
                     }
                    else {    
                        
                    out.println("<td align=\"middle\"><b>"+glossLink+md.getColumnName(column)+"</a></b></td>");
                }
            }
                out.println("</tr>");
                 
                   String trimString="";
                   String parsedCatname="";
                   int skipped=0;
                  // rs.absolute(10);
 
 
                if (wgetFile) {
                    File dir=new File(tempPath+archive.toLowerCase()+"/tmp/ImageList");
                    Random random =  new Random();
                    int r1 = random.nextInt();
                    String hash1 = Integer.toHexString(r1);
                    
                    wgetImgFile = File.createTempFile(hash1, "_img.wget",
                            dir);
                    wgetCatFile = File.createTempFile(hash1, "_cat.wget",
                            dir);
                    
                    wgetImgWriter = new PrintWriter(new BufferedWriter(new FileWriter(wgetImgFile)));
                    wgetCatWriter = new PrintWriter(new BufferedWriter(new FileWriter(wgetCatFile)));
                    Enumeration enum =req.getParameterNames();
                    while (enum.hasMoreElements()) {
                        String key = (String)enum.nextElement();
                        String value = req.getParameter(key);
                        wgetImgWriter.println("# " + key + " = " + value);
                        wgetCatWriter.println("# " + key + " = " + value);
                    }
                }
                while (skipped < skipRowInt){
                rs.next();
                    // skip rows
                    //out.println("here");
                    skipped++;
                                          
                }
                           
                String filename=null;         
                row=0;           
                boolean stillResults=true;
                boolean fileDeleted=false;
                
                while (row < rowsPerPage && stillResults) {
                    
                    if (rs.next()) {
                    fileDeleted=false;
                   
                    filename=rs.getString(filenameColumn).trim();
                    if (filename.toLowerCase().startsWith("pixelfileno")) {
                        fileDeleted=true;
                        filename="pixelFileNoLongerAvailable";
                    }
                    else {
                    filename=filename.substring(filename.indexOf(":")+1,filename.length());
                    }
                    if (rs.getString(catnameColumn).matches("(?i).*empty_catalogue.*")){
                        parsedCatname="empty_catalogue.fits";
                    } else {
                        parsedCatname=rs.getString(catnameColumn);
                    }
                    
                  
                    row=row+1;
                    if (row % 2 == 0){
                        out.println("<tr bgcolor="+VDFSHTMLSchema.evenRow+">");
                    }
                    else {
                    out.println("<tr bgcolor="+VDFSHTMLSchema.oddRow+">");
                    }
                    strArray = frs.setRS(rs);
             
                    
                    if (row >=0 && !filename.equalsIgnoreCase("none") && filename!= null && filename.indexOf("NONE") < 0){
                        out.println("<td><a href=\""+WSACGIDIRURL+"display.cgi?file=" +
                        		filename+"&cat="+
                        		parsedCatname+"&comp="+rs.getString(jpegBaseColumn)+"&noExt="+ // compFileColumn
                        		rs.getString(numDetectorsColumn)+"&MFID="+
                        		rs.getString(multiframeidColumn)+"&rID="+rID+
                        		"\" target=display>view</a></td> ");
                        if (fileDeleted) {
                            out.println("<td>&nbsp;</td> ");
                        }else {
                        out.println("<td><a href=\""+WSACGIDIRURL+"fits_download.cgi?file="+filename+
                                "&MFID="+rs.getString(multiframeidColumn)+"&rID="+rID+ "\">FITS</a></td>");
                        }
                        if (wgetFile) {
                            wgetImgWriter.println("wget \""
                                    +WSACGIDIRURL+"fits_download.cgi?file="+filename+"&MFID="+rs.getString(multiframeidColumn)+"&rID="+rID
                                    +"\" -O "+filename.substring(filename.lastIndexOf('/')+1));
                            if (!parsedCatname.equalsIgnoreCase("empty_catalogue.fits")) {
                            wgetCatWriter.println("wget \""
                                    +WSACGIDIRURL+"fits_download.cgi?file="+parsedCatname.replaceAll("(?i)djoser:","")+"&MFID="+rs.getString(multiframeidColumn)+"&rID="+rID
                                    +"\" -O "+parsedCatname.substring(parsedCatname.lastIndexOf('/')+1));
                            }
                        }
                    
            
                    } else {
                    strArray[multiframeidColumn-1]="&nbsp;";
                    out.println("<td>&nbsp;</td> ");
                    out.println("<td>&nbsp;</td> ");
                    }
                    
                    if (parsedCatname.indexOf("none") >=0 || parsedCatname.indexOf("NONE") >=0 || parsedCatname.indexOf("empty") >=0 || parsedCatname.equals("")) {
                        out.println("<td>&nbsp;</td> ");
                    }
                    else {
                        out.println("<td><a href=\""+WSACGIDIRURL+"fits_download.cgi?file="+parsedCatname+
                                "&MFID="+rs.getString(multiframeidColumn)+"&rID="+rID+"\">FITS</a></td>");
                    }
                    
                    if (showConf) {
                        if (!rs.getString(confFilenameColumn).equalsIgnoreCase("NONE")) {
                        out.println("<td><a href=\""+WSACGIDIRURL+"fits_download.cgi?file="+rs.getString(confFilenameColumn)+
                                "&MFID="+rs.getString("confID")+"&rID="+rID+ "\">FITS</a></td>");
                        }
                        else {
                            out.println("<td>&nbsp;</td> ");
                        }
                    }
                    
                    for (int column = 0; column < resultsNumColumns; column++){
                        if (column+1==toGoColumn || column+1==catnameColumn || column+1==compFileColumn
                                || column+1==filenameColumn || column+1==jpegBaseColumn || column+1==confFilenameColumn){
                            // miss columns
                        } else {
                           
                            trimString=strArray[column].trim();
                            
                            if (!trimString.equals("") && trimString != null){
                                if (column+1!=depColumn) {
                    out.println("<td nowrap align=\"right\">"+trimString+"</td>");
                                } else {
                                    if (trimString.equalsIgnoreCase("0")) {
                                    out.println("<td nowrap align=\"right\">"+trimString+"</td>");
                                    }else {
                                        out.println("<td nowrap align=\"right\" bgcolor=\"#FF0000\">"+trimString+"</td>");   
                                    }
                                }
                            } else {
                                out.println("<td nowrap align=\"right\">&nbsp;</td>");
                            }
                    }
                    }
                    out.println("</tr>");
                }
                    else {
                        stillResults=false;
                    }
                    out.flush();
                    }// end of resultset loop
                
                if (row > 0){
                  
                    
                    out.println("</table><br> row(s) "+(skipped+1)+" to "+(skipped+row)+" displayed.<br>");
                }
                else {
                    out.println("</table><br>Zero rows returned by query.<br>");
                }
                ;
                Enumeration en=req.getParameterNames();
                StringBuffer URLSB=req.getRequestURL();
                URLSB.append("?");
                while (en.hasMoreElements()){
                    String name=(String)en.nextElement();
                    if (!name.equalsIgnoreCase("skipRow") && !name.equalsIgnoreCase("pCount") && !name.equalsIgnoreCase("lmfid")) {
                    URLSB.append(name+"=");
                    URLSB.append(URLEncoder.encode(req.getParameter(name),"UTF-8")+"&");
                    }   
                } 
                
                if (inClause.toString().trim() != "") {
                    URLSB.append("lmfid=");
                    URLSB.append(URLEncoder.encode(inClause.toString(),"UTF-8")+"&");
                }
                out.print("<pre>");
                pCountInt++;
                if (skipped > rowsPerPage) {
                    out.print("<a href=\""+URLSB+"skipRow=0&pCount="+pCountInt+"\">first</a>  ");
                }
                if (skipped > 0) {
                    out.print("<a href=\""+URLSB+"skipRow="+Math.max(0,(skipRowInt-rowsPerPage))+"&pCount="+pCountInt+"\"><--prev</a>  ");
                }
                int onCount=0;
                if (rs.next()) {
                    onCount=1;
                    out.print("<a href=\""+URLSB+"skipRow="+(skipRowInt+rowsPerPage)+"&pCount="+pCountInt+"\">next--></a>  ");
                    while (rs.next() && onCount < rowsToRunOn) {
                        onCount++;
                    }
                    if (onCount < rowsToRunOn) {
                        out.print("( "+onCount+" rows to go )");
                        
                    }
                    else {
                        out.print("( At least "+onCount+" rows to go )");
                    }
                }

                out.print("</pre>");
                
                if (wgetFile) {
                    wgetImgWriter.close();
                    wgetCatWriter.close();
                    wgetImgWriter.close();
                out.println("<br><a href=\""+tempURL+archive.toLowerCase()+"/tmp/ImageList/"+wgetImgFile.getName().substring(wgetImgFile.getName().lastIndexOf(File.pathSeparator)+1)
                        +"\">Link to image wget script</a> (can be used to download all image files on this page)" );
                out.println("<br><a href=\""+tempURL+archive.toLowerCase()+"/tmp/ImageList/"+wgetCatFile.getName().substring(wgetCatFile.getName().lastIndexOf(File.pathSeparator)+1)
                        +"\">Link to catalogue wget script</a> (can be used to download all catalogue files on this page)<p>" );
                }
                
                int goBack = 1+(skipRowInt/rowsPerPage);
                    out.print("<a href=\"javascript:history.go(-"+pCountInt+")\">Back to form</a> (uses Javascript)");
                   
                
             
            } catch (SQLException se) {
                out.println("<p>Error querying the database: "
                        + se.getErrorCode());

                if (se.getErrorCode() == 0) {
                    con.close();
                    con=null;
                }
            }

            catch (Exception e) {
                out.println("<p>Error " + e);
                StringWriter sw = new StringWriter();
                e.printStackTrace(new PrintWriter(sw));
                String stacktrace = sw.toString();
                logger.equals(stacktrace);

            }

 
        } catch (Exception e) {
            System.out.println("errorhere");
            System.out.println(e);
            //e.printStackTrace(out);
            out.print(e);
            if (con != null) {
                //System.out.println("con alive");
            }
        } finally {
  
            System.gc();
            //out.flush();
            //out.close();
            try {
                //Thread.sleep(6000);
                stmt.close();

            } catch (Exception e) {
                ;
            }
            if (con != null) {

                //	connMgr.freeConnection("idb", con);
               // myBroker.freeConnection(con);
             
                try {
                    con.close();
                    con=null;
                    stmt=null;
                    rs=null;
                }
                catch (Exception e) {
                    ;
                }    
            }
            if (wgetFile) {
  
               wgetImgWriter.close();
               wgetCatWriter.close();
            }
            out.println(CLOSINGHTML);
            out.flush();

        }

    }

    public void destroy() {
        //          connMgr.release();
        //  myBroker.destroy();
        logger=null;
        super.destroy();
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        // Data received by an HTTP POST is handled the same way
        // as data sent with a HTTP GET request... simply pass on to above...
        doGet(req, res);
    }; // END of doPost()

   
}



