/*
 * Created on 18-May-2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package uk.ac.roe.wfau;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import com.oreilly.servlet.MultipartRequest;
//import com.sun.rsasign.o;

/**
 * @author mar
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class VSAJsp extends WSAJsp {
    public final static String selected="selected";
    public final static String checked="checked";
    public final static String [] MONTHSOFYEAR={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
    public final static String [] LISTDRIVENAPERARRAY={"aperMag3","aperMag4","aperMag6"};
    public final static String [] LISTDRIVENFILTERARRAY={"Y","J_1","H","K"};
    public final static String LISTDRIVENBASESELECT="sourceID, framesetID, RA, Dec";
    
    public static String getOnChange(String [] params) {
        StringBuffer extra=new StringBuffer("");
        
        if (params !=null) {
            for (int i=0;i<params.length;i++) {
                extra.append("+'&"+params[i]+"='+this.form."+params[i]+".value");
            }
        }
            return extra.toString();
    }
    public static String getParamValue (HttpServletRequest request,String param){
        return getParamValue (request, param, "");
    }
    
    public static String getQueryString(HttpServletRequest request, String omit) {
        if (request.getQueryString() == null) {
            return "";
        }
        if (omit != null) {
        StringBuffer sb=new StringBuffer(request.getQueryString());
        int strt=sb.indexOf(omit+"=");
        int end=sb.indexOf("&",strt);
        if (strt >= 0 && end > strt) {
            return sb.delete(strt,end).toString();
        }
        else if (strt >= 0 && end <= strt) {
            return sb.delete(strt,sb.length()).toString();
        }
        else {
            return request.getQueryString(); 
        }
        }
        else {
        return request.getQueryString();
        }
    }
    
    public static String getParamValue (HttpServletRequest request,String param, String none) {
        if (request.getParameter(param)!= null) {
            return request.getParameter(URLEncoder.encode(param));
            
        }
        else {
            return none;
        }
    }
    public static String getParamChecked (HttpServletRequest request,String param, String value){
        return getParamChecked (request,param,value,false);
    }
    public static String getParamChecked (HttpServletRequest request,String param, String value,boolean isDefault) {
        if (request.getParameter(param)!= null) {
            if (request.getParameter(param).equalsIgnoreCase(value)) {
            return checked;
            }
            else {
                return "";   
            }
        }
        else {
            if (isDefault) {
                return checked;
            }
            else {
            return "";
            }
        }
    }
    
    public static String getParamSelected (HttpServletRequest request,String param, String value, boolean defaultSel) {
        if (request.getParameter(param)!= null) {
            if (request.getParameter(param).equalsIgnoreCase(value)) {
            return selected;
            }
            else {
                if (defaultSel) {
                    return selected;
                }
                else {
                return "";
                }
            }
        }
        else {
            if (defaultSel) {
                return selected;
            }
            else {
            return "";
            }
        }
    }
    public static String getParamSelected (HttpServletRequest request,String param, String value) {
        return getParamSelected(request,param,value,false);
        /*
        if (request.getParameter(param)!= null) {
            if (request.getParameter(param).equalsIgnoreCase(value)) {
            return selected;
            }
            else {
                return "";   
            }
        }
        else {
            return "";
        }*/
    }
    public static void printDBList(JspWriter out, HttpSession session, int progID, HttpServletRequest request){
        printDBList(out,session,progID,request,"");
    }
    
    public static String printDBList(JspWriter out, HttpSession session, int progID, HttpServletRequest request, String paramURL) {
        return printDBList(out, session,progID, request,paramURL,false) ;
    }

   public static String printDBList(JspWriter out, HttpSession session, int progID, HttpServletRequest request, String paramURL,boolean doRollingDB) {
        return printDBList(out, session,progID, request,paramURL,doRollingDB,false) ;
    }
    
    public static String printDBList(JspWriter out, HttpSession session, int progID, HttpServletRequest request, String paramURL,boolean doRollingDB, String [] params,int strtP){
        return printDBList(out, session,progID, request,paramURL,doRollingDB,params,strtP,true);
    }
    public static String printDBList(JspWriter out, HttpSession session, int progID, HttpServletRequest request, String paramURL,boolean doRollingDB, String [] params,int strtP,boolean includeExtraDB){
        return printDBList(out, session,progID,request,paramURL,doRollingDB,params,strtP,includeExtraDB,false);
    }
    
    public static String printDBList(JspWriter out, HttpSession session, int progID, HttpServletRequest request, String paramURL,boolean doRollingDB, String [] params,int strtP,boolean includeExtraDB,boolean includeMoreDB){
        return printDBList(out, session,progID, request,paramURL,doRollingDB,params,strtP,includeExtraDB,includeMoreDB,-1);
    }
    public static String printDBList(JspWriter out, HttpSession session, int progID, HttpServletRequest request, String paramURL,boolean doRollingDB, String [] params,int strtP,boolean includeExtraDB,boolean includeMoreDB,int minRelease) {
        String [] DBs=VSASession.getDB(session,progID,doRollingDB,includeExtraDB,includeMoreDB);
        
        String [] equivDBs=null;
        if (DBs!=null && DBs.length >= 1) {
            equivDBs= new String [DBs.length];
            for (int d=0; d<DBs.length;d++) {
                equivDBs[d]="";
            }
            String [] wDBs=VSASchema.getWorldDBs(progID);
            String [] eDBs=VSASchema.getEquivWorldDBs(progID);
            if (wDBs!=null && eDBs !=null) {
            for (int d=0; d<DBs.length;d++) {
                for (int w=0; w<wDBs.length;w++) {
                    if (DBs[d].equalsIgnoreCase(wDBs[w])) {
                        if (!eDBs[w].equals(".")){
                        equivDBs[d]="("+eDBs[w]+")";
                        }
                    }
                }
            }   
            }
        }
            
        String thisDB=null;
        boolean vsaShown=false;
        StringBuffer extra=new StringBuffer("");
        
        if (params !=null) {
            for (int i=strtP;i<params.length;i++) {
                extra.append("+'&"+params[i]+"='+this.form."+params[i]+".value");
            }
            //extra="+'&ra='+this.form.ra.value+'&dec='+this.form.dec.value";
        }
        try {
            /*out.print(VSASession.getSessionClass(session));
            out.print("::"+DBs[0]);
            */
        out.print("<select name=\"database\" ");
        if (paramURL !=null) {
           out.print(" onchange=\"location = '"+paramURL+"' + this.options[this.selectedIndex ].value"+extra+";\"");
//           out.print(" onchange=\"location = '"+paramURL+"' + this.options[this.selectedIndex ].value+'&ra='+this.form.ra.value+'&dec='+this.form.dec.value;\"");
        }
        out.print(">");
        if (DBs==null) {
            out.print("<option value=\"none\">Select programme/survey");
        }
        else if (DBs.length==0) {
            out.print("<option value=\"none\">No database available");
        }
        else {
            thisDB=DBs[0];
            
                if (request.getParameter("database") != null && !request.getParameter("database").equals("")) {
                    thisDB=request.getParameter("database");
                }
        for (int i=0; i < DBs.length; i++ ) {
            
                 if (WSASchema.getVersionNoOfRelease(DBs[i]) >= minRelease) {
                out.print("<option value=\""+DBs[i]+"\"");         
               if (thisDB.equalsIgnoreCase(DBs[i])) {
                  out.print(" selected");
               }   
               out.print(">"+DBs[i]+" "+equivDBs[i]); 
               if (DBs[i].equalsIgnoreCase(VSASchema.PRERELEASEDATABASE)) {
            	   out.print(" (QC)**");
            	   vsaShown=true;
               }
                 }
         }
        }
        out.print("</select>");
        if (vsaShown) {
            out.print("&nbsp; &nbsp;  <a href=\""+VSAHTMLSchema.baseURL+"theVSADatabase.html\">notes on VSA (QC) database</a>");
        }
        }
        catch (IOException ioe) {
          // do nothing  
        }
        return thisDB;
    }
    public static void printDBList(JspWriter out, HttpSession session, MultipartRequest request){
        printDBList(out,session,request,"");
    }
    
    public static String printDBList(JspWriter out, HttpSession session, MultipartRequest request, String paramURL) {
        return printDBList(out, session, request,paramURL,false) ;
    }

    public static String printDBList(JspWriter out, HttpSession session, MultipartRequest request, String paramURL,boolean doRollingDB) {
        return printDBList(out, session, request,paramURL,doRollingDB,false) ;
    }
    public static String printDBList(JspWriter out, HttpSession session, MultipartRequest request, String paramURL,boolean doRollingDB, String [] params,int strtP){
        return printDBList(out, session, request,paramURL,doRollingDB,params,strtP,true);
    }
    public static String printDBList(JspWriter out, HttpSession session, MultipartRequest request, String paramURL,boolean doRollingDB, String [] params,int strtP,boolean includeExtraDB) {
        String [] DBs=WSASession.getDB(session,doRollingDB,includeExtraDB);
        String thisDB=DBs[0];

        StringBuffer extra=new StringBuffer("");
        
        if (params !=null) {
            for (int i=strtP;i<params.length;i++) {
                extra.append("+'&"+params[i]+"='+this.form."+params[i]+".value");
            }
            //extra="+'&ra='+this.form.ra.value+'&dec='+this.form.dec.value";
        }
        try {
        if (request.getParameter("database") != null) {
            thisDB=request.getParameter("database");
        }
        out.print("<select name=\"database\" ");
        if (paramURL !=null) {
           out.print(" onchange=\"location = '"+paramURL+"' + this.options[this.selectedIndex ].value"+extra+";\"");
//           out.print(" onchange=\"location = '"+paramURL+"' + this.options[this.selectedIndex ].value+'&ra='+this.form.ra.value+'&dec='+this.form.dec.value;\"");
        }
        out.print(">");
        for (int i=0; i < DBs.length; i++ ) {
            
                out.print("<option value=\""+DBs[i]+"\"");         
               if (thisDB.equalsIgnoreCase(DBs[i])) {
                  out.print(" selected");
               }   
               out.print(">"+DBs[i]); 
              
         } 
        out.print("</select>");

        }
        catch (IOException ioe) {
          // do nothing  
        }
        return thisDB;
    }
        
    public static String printDBList(JspWriter out, HttpSession session, int progID, HttpServletRequest request, String paramURL,boolean doRollingDB,boolean doRADec) {
    String [] DBs=WSASession.getDB(session,doRollingDB);
    String thisDB=DBs[0];

    String extra="";
    if (doRADec) {
        extra="+'&ra='+this.form.ra.value+'&dec='+this.form.dec.value";
    }
    try {
    if (request.getParameter("database") != null) {
        thisDB=request.getParameter("database");
    }
    out.print("<select name=\"database\" ");
    if (paramURL !=null) {
       out.print(" onchange=\"location = '"+paramURL+"' + this.options[this.selectedIndex ].value"+extra+";\"");
//       out.print(" onchange=\"location = '"+paramURL+"' + this.options[this.selectedIndex ].value+'&ra='+this.form.ra.value+'&dec='+this.form.dec.value;\"");
    }
    out.print(">");
    for (int i=0; i < DBs.length; i++ ) {
        
            out.print("<option value=\""+DBs[i]+"\"");         
           if (thisDB.equalsIgnoreCase(DBs[i])) {
              out.print(" selected");
           }   
           out.print(">"+DBs[i]); 
          
     } 
    out.print("</select>");

    }
    catch (IOException ioe) {
      // do nothing  
    }
    return thisDB;
}
    
    public static String printDBList(JspWriter out, HttpSession session, MultipartRequest request, String paramURL,boolean doRollingDB,boolean doRADec) {
        String [] DBs=WSASession.getDB(session,doRollingDB);
        String thisDB=DBs[0];

        String extra="";
        if (doRADec) {
            extra="+'&ra='+this.form.ra.value+'&dec='+this.form.dec.value";
        }
        try {
        if (request.getParameter("database") != null) {
            thisDB=request.getParameter("database");
        }
        out.print("<select name=\"database\" ");
        if (paramURL !=null) {
           out.print(" onchange=\"location = '"+paramURL+"' + this.options[this.selectedIndex ].value"+extra+";\"");
//           out.print(" onchange=\"location = '"+paramURL+"' + this.options[this.selectedIndex ].value+'&ra='+this.form.ra.value+'&dec='+this.form.dec.value;\"");
        }
        out.print(">");
        for (int i=0; i < DBs.length; i++ ) {
            
                out.print("<option value=\""+DBs[i]+"\"");         
               if (thisDB.equalsIgnoreCase(DBs[i])) {
                  out.print(" selected");
               }   
               out.print(">"+DBs[i]); 
              
         } 
        out.print("</select>");

        }
        catch (IOException ioe) {
          // do nothing  
        }
        return thisDB;
    }

public static String  printVSurveyList(JspWriter out, HttpSession session, String database, String paramURL,boolean all) {
    return printVSurveyList( out, session, database, paramURL, all, false,null,false);
}
public static String printVSurveyList(JspWriter out, HttpSession session, String database, String paramURL,boolean all, boolean topNull, HttpServletRequest request) {
    return printVSurveyList( out, session, database, paramURL, all, topNull,request,false);
}
public static String printVSurveyList(JspWriter out, HttpSession session, String database, String paramURL,boolean all, boolean topNull,HttpServletRequest request,boolean sourceOnly) {
    return printVSurveyList( out, session, database, paramURL, all, topNull,request,sourceOnly,false);
}

public static String printVSurveyList(JspWriter out, HttpSession session, String database, String paramURL,boolean all, boolean topNull,HttpServletRequest request,boolean sourceOnly,String [] params,int strtP) {
    return printVSurveyList( out, session, database, paramURL, all, topNull,request,sourceOnly,params,strtP,false);
}

public static String printVSurveyList(JspWriter out, HttpSession session, String database, String paramURL,boolean all, boolean topNull,HttpServletRequest request,boolean sourceOnly,String [] params,int strtP, boolean standards) {
    return printVSurveyList(out, session,database,paramURL,all,topNull,request,sourceOnly,params,strtP,standards,null);
}

public static String printVSurveyList(JspWriter out, HttpSession session, String database, String paramURL,boolean all, boolean topNull,HttpServletRequest request,boolean sourceOnly,String [] params,int strtP, boolean standards, int [] progs) {
    String thisProgID="-9999";
    
    StringBuffer extra=new StringBuffer("");
    
    if (params !=null) {
        for (int i=strtP;i<params.length;i++) {
//            extra.append("+'&"+params[i]+"='+escape(this.form."+params[i]+".value)");
            extra.append("+'&"+params[i]+"='+this.form."+params[i]+".value");
        }
        //extra="+'&ra='+this.form.ra.value+'&dec='+this.form.dec.value";
    }

    try {
        if (request != null && request.getParameter("programmeID") != null) {
            thisProgID=request.getParameter("programmeID");
        }
    out.print("<select name=\"programmeID\" ");
    if (request != null && !paramURL.equals("")) {
        out.print("onchange=\"location = '"+paramURL+"' + this.options[this.selectedIndex ].value"+extra+";\"");
        //out.print("onchange=\"location = '"+paramURL+"' + this.options[this.selectedIndex ].value+'&ra='+this.form.ra.value+'&dec='+this.form.dec.value;\"");
    }
    out.print(">");
    if (topNull && !database.equalsIgnoreCase(WSASchema.PTSDB) && !database.equalsIgnoreCase(WSASchema.CALDB)) {
        out.println("<option value=\"none\" >Choose survey from list");
        }

    if (all && VSASession.getLoginBoolean(session) && !database.equalsIgnoreCase(WSASchema.PTSDB) && !database.equalsIgnoreCase(WSASchema.CALDB)) {
        out.println("<option value=\"all\" ");
        if (thisProgID.equals("all")) {
            out.println("selected");
        }
        out.println(">All VISTA surveys");
        }
 
  
//    int [] progs;
    if (progs == null ) {
    if (database !=null && database.equalsIgnoreCase(WSASchema.PTSDB)) {
        progs=new int [1];
        progs[0]=WSASchema.PTSPROGRAMMEID;
    }
    else if (database !=null && database.equalsIgnoreCase(WSASchema.CALDB)) {
            progs=new int [1];
            progs[0]=WSASchema.CALPROGRAMMEID; 
    }
    else {
    progs=VSASession.getProgs(session);
    }
    }
    boolean show=true;
    String isSelected="";
    for (int i=0; i < progs.length; i++ ) { 
        
        isSelected="";
        show=true;
        try {
        if (Integer.valueOf(thisProgID).intValue() == progs[i]) {
            isSelected=" selected";
        }
        }
        catch (NumberFormatException nfe) {
            
        }
        /*
        if (sourceOnly && WSASchema.getSourceTableName(progs[i]) == null) {
            show=false;
        }
        */
        if (show){
        if (database !=null) {
  
        out.println("<option value=\""+progs[i]+"\""+isSelected+">"+VSASchema.getSurveyName(progs[i]));
        }
        
        }
    }
    if (all && !VSASession.getLoginBoolean(session)) {
        out.println("<option value=\"all\" ");
        if (thisProgID.equals("all")) {
            out.println("selected");
        }
        out.println(">All VISTA PI projects");
        }
    
    /*
    if (standards && database != null && database.equalsIgnoreCase(WSASchema.PRERELEASEDATABASE)) {
        String isSelected="";
        try {
            if (Integer.valueOf(thisProgID).intValue() == WSASchema.CALPROGRAMMEID) {
                isSelected=" selected";
            }
            }
            catch (NumberFormatException nfe) {
                
            }  
            out.print("<option value=\""+WSASchema.CALPROGRAMMEID+"\""+isSelected+">"+WSASchema.getSurveyName(WSASchema.CALPROGRAMMEID)); 
    }
    */
    out.print("</select>"); 
    
    }
    catch (IOException ioe) {
        // do nothing  
      }
   return thisProgID; 
}








public static String printVSurveyList(JspWriter out, HttpSession session, String database, String paramURL,boolean all, boolean topNull,HttpServletRequest request,boolean sourceOnly,boolean doRADec) {
    String thisProgID="-9999";
    String extra="";
    
    if (doRADec) {
        extra="+'&ra='+this.form.ra.value+'&dec='+this.form.dec.value";
    }  
    try {
        if (request != null && request.getParameter("programmeID") != null) {
            thisProgID=request.getParameter("programmeID");
        }
    out.print("<select name=\"programmeID\" ");
    if (request != null && !paramURL.equals("")) {
        out.print("onchange=\"location = '"+paramURL+"' + this.options[this.selectedIndex ].value"+extra+";\"");
        //out.print("onchange=\"location = '"+paramURL+"' + this.options[this.selectedIndex ].value+'&ra='+this.form.ra.value+'&dec='+this.form.dec.value;\"");
    }
    out.print(">");
    if (topNull && !database.equalsIgnoreCase(WSASchema.PTSDB) && !database.equalsIgnoreCase(WSASchema.CALDB)) {
        out.println("<option value=\"null\">choose survey from list");
        }
    if (all && VSASession.getLoginBoolean(session) && !database.equalsIgnoreCase(WSASchema.PTSDB) && !database.equalsIgnoreCase(WSASchema.CALDB)) {
    out.println("<option value=\"all\" selected>All UKIDSS surveys");
    }
    int [] progs;
    if (database !=null && database.equalsIgnoreCase(WSASchema.PTSDB)) {
        progs=new int [1];
        progs[0]=WSASchema.PTSPROGRAMMEID;
    }
    else if (database !=null && database.equalsIgnoreCase(WSASchema.CALDB)) {
            progs=new int [1];
            progs[0]=WSASchema.CALPROGRAMMEID; 
    }
    else {
    progs=VSASession.getProgs(session);
    }
    boolean show=true;
    for (int i=0; i < progs.length; i++ ) { 
        
        String selected="";
        show=true;
        try {
        if (Integer.valueOf(thisProgID).intValue() == progs[i]) {
            selected=" selected";
        }
        }
        catch (NumberFormatException nfe) {
            
        }
        if (sourceOnly && WSASchema.getSourceTableName(progs[i]) == null) {
            show=false;
        }
        if (show){
        if (database !=null) {
        if (WSASchema.isProgInDB(database,progs[i])) {
            
            out.print("<option value=\""+progs[i]+"\""+selected+">"+WSASchema.getSurveyName(progs[i])); 
        }
        }
        else {
        out.print("<option value=\""+progs[i]+"\">"+WSASchema.getSurveyName(progs[i]));
        }
        
        }
    }
    
    out.print("</select>"); 
    
    }
    catch (IOException ioe) {
        // do nothing  
      }
   return thisProgID;
    
}


}
