/**
 * XXXX
 * Inserte aquí la descripción del tipo.
 * Fecha de creación: (10/05/07)
 */    
package com.ejie.p06b.tags;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.ejie.p06b.datatype.model.P06BFCAGeneralDatatype;


public class P06BSelectTag extends TagSupport{
	private String name;
	   
	public void setName(String name){
        this.name = name;
    }
	
	private String obj;
	   
	public void setObj(String obj){
     this.obj = obj;
	}
	
	private String style;
	   
	public void setStyle(String style){
     this.style = style;
	}
	
	private String onchange;
	   
	public void setOnchange(String onchange){
		this.onchange = onchange;
	}
	
	private String valueToCompare;
	   
	public void setValueToCompare(String valueToCompare){
		this.valueToCompare = valueToCompare;
	}
	
	
	public int doStartTag() throws JspException {
	    try{
	    	String htmlPrint = "";
	    	HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
	    	ArrayList lista = null;
	    	if(request.getAttribute(obj)!=null)
	    		lista = (ArrayList)request.getAttribute(obj);
	    	else
	    		lista = new ArrayList();
	    	
	    	if(onchange==null)
	    		onchange="";
	    	
	    	if(valueToCompare!=null){
	    		if(lista.isEmpty()&&style==null){
		    		htmlPrint="<select style=\"width:180px;\" name=\""+name+"\" class=\"inputField\" size=\"1\" onchange=\""+onchange+"\">";
		    	}else{
		    		htmlPrint="<select style=\""+style+"\" name=\""+name+"\" class=\"inputField\" size=\"1\" onchange=\""+onchange+"\">";
		    	}
		    	if(!valueToCompare.equals("")){
		    		htmlPrint+="<option value=\"\"></option>";
		    	}else{
		    		htmlPrint+="<option SELECTED value=\"\"></option>";
		    	}
		    	P06BFCAGeneralDatatype elemento = null;
		    	for(int i=0;i<lista.size();i++){
	            	elemento = (P06BFCAGeneralDatatype)lista.get(i);
            		if(elemento.getCodigo().equals(valueToCompare)){
	            		htmlPrint+="<option SELECTED value=\""+elemento.getCodigo()+"\">"+elemento.getDescripcion()+"</option>";
	            	}else{
	            		htmlPrint+="<option value=\""+elemento.getCodigo()+"\">"+elemento.getDescripcion()+"</option>";
	            	}
	            }
				htmlPrint+="</select>"; 
	    	}else{
	    		if(lista.isEmpty()&&style==null){
		    		htmlPrint="<select style=\"width:180px;\" name=\""+name+"\" class=\"inputField\" size=\"1\" onchange=\""+onchange+"\">";
		    	}else{
		    		htmlPrint="<select style=\""+style+"\" name=\""+name+"\" class=\"inputField\" size=\"1\" onchange=\""+onchange+"\">";
		    	}
		    	if(request.getParameter(name)!=null){
		    		htmlPrint+="<option value=\"\"></option>";
		    	}else{
		    		htmlPrint+="<option SELECTED value=\"\"></option>";
		    	}
		    	P06BFCAGeneralDatatype elemento = null;
	            for(int i=0;i<lista.size();i++){
	            	elemento = (P06BFCAGeneralDatatype)lista.get(i);
	            	if(request.getParameter(name)!=null
	            			&&elemento.getCodigo().equals(request.getParameter(name))){
	            		htmlPrint+="<option SELECTED value=\""+elemento.getCodigo()+"\">"+elemento.getDescripcion()+"</option>";
	            	}else{
	            		htmlPrint+="<option value=\""+elemento.getCodigo()+"\">"+elemento.getDescripcion()+"</option>";
	            	}
	            }
				htmlPrint+="</select>"; 
	    	}
	    	pageContext.getOut().print(htmlPrint);
	    } catch (Exception e) {
	        throw new JspException ("Error: IOException" + 
			e.getMessage());
	    }
	    return SKIP_BODY;
	}
	
    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }
}
