:- ['/usr/local/lib/prolog/html.pl'].

main :- 
  html_get_form_input(Info),
  html_get_value(Info,mood,Mood0),
  html_get_value(Info,potatoes,Potatoes),
  html_get_value(Info,pizza,Pizza),
  html_get_value(Info,coke,Coke),
  html_get_value(Info,pass,Password),
  html_get_value(Info,text,Text),
  html_get_value(Info,menu1,Number),
  findall(Color,member(menu2=Color,Info),Colors),
  compute_reply(Mood0,Potatoes,Pizza,Coke,Password,Text,Number,Colors,Reply),
  default(Mood0,happy,Mood),
  output_html([
    form_reply,
    start,
    title('Sample Form generated from Prolog'),
    heading(1,'Sample HTML form'),
    Reply,
    --,
    heading(2,'This is a form:'),
    form('http://www.clip.dia.fi.upm.es/cgi-bin/html_forms'),
    'Please select mood:',\\,
    radio(mood,happy,Mood),nl,
    image('http://www.clip.dia.fi.upm.es/images/smile.happy.gif',
      [align=middle]),
    nl,
    radio(mood,sad,Mood),nl,
    image('http://www.clip.dia.fi.upm.es/images/smile.sad.gif',
      [align=middle]),
    $,
    'What ',strong(do),' you want?',\\,
    checkbox(potatoes,Potatoes), 'Potato(e)s',\\,
    checkbox(pizza,Pizza), 'Pizza',\\,
    checkbox(coke,Coke), 'Coke',
    --,
    'Please write a password:',nl,
    input(password,[name=pass,size=9,maxlength=8]),$,
    textarea(text,[rows=5,cols=20],'Write here something'),$,
    'You can choose here:', menu(menu1,[],[one,$two,three]),$,
    'Also here:', menu(menu2,[multiple],[$red,$green,blue]),$,
    input(submit,[value='Send values']),nl,
    input(reset,[value='Reset values']),
    end_form,
    --,
    address('clip@dia.fi.upm.es'),
    end]).

compute_reply('',_,_,_,_,_,_,_,'') :- !.
compute_reply(Mood,Potatoes,Pizza,Coke,Passwd0,Text0,Number,Colors0,Reply) :-
  compute_food(Potatoes,Pizza,Coke,Food),
  compute_text(Text0,Text),
  compute_password(Passwd0,Passwd),
  compute_colors(Colors0,Colors),
  Reply = [
    --,
    heading(2,'Submitted data:'),
    'You are ',strong(Mood),'.',\\,
    'You want ',strong(Food),\\,
    'The password is ',tt(verbatim(Passwd)),'.',\\,
    'In the text area you wrote: ',tt(verbatim(Text)),$,
    'You chose number ',tt(Number),' and color(s):',
    preformatted(Colors)].

compute_food('','','','no food.').
compute_food(on,'','','potatoes.').
compute_food('',on,'','pizza.').
compute_food('','',on,'coke.').
compute_food(on,on,'','potatoes and pizza.').
compute_food('',on,on,'pizza with coke.').
compute_food(on,'',on,'potatoes with coke.').
compute_food(on,on,on,'potatoes and pizza with coke.').

compute_text(T0,T) :-
  html_empty_value(T0) -> T = '{Nothing}' ; T = T0.

compute_password(P0,P) :-
  P0 = true -> P = '{none}'; P = P0.

compute_colors(C0,C) :-
  C0 = [] -> C = [none] ; C = C0.

member(X,[X|_]).
member(X,[_|Xs]) :- member(X,Xs).

:- save_cgi_bin(html_forms,main).
