/*
 * Created on Feb 17, 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package gov.va.med.esr.ui.taglib.support;

import net.mlw.vlh.web.tag.support.*;

import java.util.Map;

import net.mlw.vlh.ValueListInfo;
import net.mlw.vlh.web.tag.TableInfo;

import gov.va.med.fw.ui.valuelist.SelectableSearchValueListAdapter;


/**
 * @author DNS   CHENB
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class HtmlDisplay508CheckBoxSortingProvider extends HtmlDisplayCheckBoxSortingProvider {

	public static final String CHECKBOX_PARAM = "CheckboxName508";
    public static final String SELECTED_REQUEST_ATTRIBUTE_KEY = SelectableSearchValueListAdapter.SELECTED_REQUEST_ATTRIBUTE_KEY;

    
	public static String addScript(Object fieldName)
    {
        return "javascript:SelectedPaging(document, \'" + fieldName + "\', \'";
    }

	/**
	 * 
	 */
	public HtmlDisplay508CheckBoxSortingProvider() {
		super();
		// TODO Auto-generated constructor stub
	}
    /**
     * Formats the text to be displayed as the header by wraping it in a link if
     * sorting is enabled. Alt (hint) is localized, please define in your
     * property file for messages the property "sorting"
     * 
     * @param columnInfo The ColumnInfo.
     * @param tableInfo The TableInfo.
     * @param info The ValueListInfo.
     * @return The formated HTML.
     */
    public String getHeaderLabel(ColumnInfo columnInfo, TableInfo tableInfo,
            ValueListInfo info, Map includeParameters)
    {
    	includeParameters.remove(SELECTED_REQUEST_ATTRIBUTE_KEY);

        StringBuffer sb = new StringBuffer().append(
    		super.getHeaderLabel(columnInfo,
    		tableInfo, info, includeParameters));
        
        //insert javascript and document 
        if (includeParameters.containsKey(CHECKBOX_PARAM) 
        	&& sb != null && sb.indexOf("href=") > 0)
        {
        	//checkboxName=selected
        	int urlPos = sb.indexOf(tableInfo.getUrl());
        	if (urlPos >= 0)
        		sb.insert(urlPos, addScript(includeParameters.get(CHECKBOX_PARAM)));
        	
        	//insert script end ')
        	int endPos = sb.indexOf("\">");
        	if (endPos >= 0)
        		sb.insert(endPos, "\')");
        }
        
        return sb.toString();
    }
}
