import java.awt.*;
import java.net.Socket;
import java.io.*;
import java.awt.Frame;

public class saveDlg extends Frame {
TextField tf;

    public saveDlg() {
	super("Save");
	setLayout(null);
      	setFont(Env.font);
       	setBackground(Color.lightGray);
       	setForeground(Color.black);
        //Button save,cancel;
	tf = new TextField();
	tf.reshape(75,100,150,35);
	add(tf);
	int width=300, height=300;
	pack();
        reshape(getToolkit().getScreenSize().width/2-width/2, 
	getToolkit().getScreenSize().height/2-height/2,width,height); 
	show();
    }

    public void paint(Graphics g){
        g.drawString("Your name or pseudonym:", 50,75);
    }

    public boolean handleEvent(Event e){
		if(e.id == Event.KEY_PRESS){ 
				if(e.key == 10) return sendIt();
		}
		if(e.id == Event.ACTION_EVENT){
			if(e.target instanceof Button){
				if(((Button)e.target).getLabel()!="Cancel")
					return sendIt();
				this.dispose();
			    return true;
			}
		}
		if(e.id == Event.WINDOW_DESTROY) this.dispose();   
		return super.handleEvent(e);
	}
	
	public boolean sendIt(){
                DataOutputStream os=null;
                DataInputStream is=null;
		Socket sock = null;
		if(tf.getText()==null) return true;
		try{
		String name = tf.getText();
		name = name.trim().replace(' ','_').replace('&','_').replace(';','_'
                    ).replace('*','x').replace('$','S').replace('!','_'); 
		String GET = "GET /draw/cgi/saveClip.cgi?"+name;
                sock = new Socket(Env.host, 80);
                os = new DataOutputStream(sock.getOutputStream());
                is = new DataInputStream(sock.getInputStream());
                os.writeBytes(GET);
		os.flush();
		}catch(IOException io){
			System.out.println(io);
		}
		try{
			os.close();
		   	sock.close();
		   }catch(IOException ioe){
			System.out.println(ioe);
		}
		this.dispose();
		return true;
	}
}
