package e4s.tutorial;

import e4s.html.*;
import e4s.html.input.extended.*;
import e4s.servlet.*;
import e4s.application.sysmodule.*;

import java.io.*;
import java.util.*;
import java.text.*;


/**
 * Pick up a value (e.g. a date from a calendar) while editing a form.
 *
 * {@tutorial Example_Pickup}
 */
public class Example_Pickup extends E4ModuleImplementation
{   
   public static E4Method        startForm = null;
   public static E4MethodPickup pickupMusic = null;
   public static E4Method        viewParameters = null;
   
   
   public void startForm( HTML html )
   {
      BODY body = html.BODY();

      FORM form = body.FORM("TEST");

      // Define a TEXTFIELD and do your own pickup selection
      TEXTFIELD name = form.TEXTFIELD(new E4InputFieldName("MUSIC"),"Your favorite Music",30);
      name.setValue("Rolling Stones");
      name.definePickupList(pickupMusic); 
      form.P();
      
      // Define a Java Script which can be used for notification on value changes
      E4JavaScript jsNotify = body.createScript();
      jsNotify.appendln("function " + jsNotify.getName() + "()");
      jsNotify.appendln("{");
      jsNotify.appendln("   alert('the field value has been changed');");
      jsNotify.appendln("}");
      
      // Define a TEXTFIELD and use standard color selection
      TEXTFIELD color = form.TEXTFIELD(new E4InputFieldName("COLOR"),"Your favorite Color",7);
      color.setValue("#FF0000");
      color.definePickupList(E4ModuleColorPickup.initSelection,jsNotify); 
      form.P();
      
      // Note, that a DATEFIELD automatically gets a pickup for a Calendar
      DATEFIELD next_holiday = form.DATEFIELD(new E4InputFieldName("HOLIDAY"),"My next holiday");
      next_holiday.noTimeFormat();
      form.P();

      // You can avoid this Clendar selection
      DATEFIELD birthday = form.DATEFIELD(new E4InputFieldName("BIRTHDAY"),"Your birthday",false);
   }   
   
   public void pickupMusic( HTML html, E4CgiParams params )
   {
      html.setTitle("Please select");
      
      String default_value = params.get(E4MethodPickup._PARAM_INIT_VALUE);
      
      A view_parameters = html.A(viewParameters);
      view_parameters.print("[view the parameters]");

      String music[] = { "WHO", "REM", "Rolling Stones", "Beatles", "Doors", "Kinks", "Velvet Underground", "Blondie", "Sex Pistols", "PIL" };
      
      TABLE table = html.TABLE();
      table.setBgColor(E4Color.GRAY(10));
      table.setBorder();
      table.setCellspacing(0,0);
      
      for( int i = 0; i < music.length; i++ )
      {
         TD td = table.TR().TD();
         A_ReturnValue returnvalue = td.A_ReturnValue(music[i],params);
         returnvalue.println(music[i]);
         
         if ((default_value != null) && default_value.trim().equalsIgnoreCase(music[i]))
            td.setBgColor(E4Color.YELLOW);
      }  
      
      html.P();
      
      A close = html.A(E4EventHandlerA.closeWindow());
      close.println("[Cancel]");
   }  

   public void viewParameters( HTML html, E4CgiParams p )
   {
      TABLE t = html.TABLE();
      p.toTable(t);
      
      A back = html.A(startForm);
      back.print("[start again]");
   }  
}