<?vsp 
--  
--  $Id: addressbook.vsp,v 1.2 2006/08/15 19:30:34 source Exp $
--  
--  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
--  project.
--  
--  Copyright (C) 1998-2006 OpenLink Software
--  
--  This project is free software; you can redistribute it and/or modify it
--  under the terms of the GNU General Public License as published by the
--  Free Software Foundation; only version 2 of the License, dated June 1991.
--  
--  This program is distributed in the hope that it will be useful, but
--  WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
--  General Public License for more details.
--  
--  You should have received a copy of the GNU General Public License along
--  with this program; if not, write to the Free Software Foundation, Inc.,
--  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--  
--  
 http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');

   declare _user_name, _login_ok varchar;
  declare _sid varchar;
  declare _err long varchar;
  declare _address, _addressnew, _addressold, _to_do, _add_ok, _update_ok, _delete_ok,_res_msg, _email_msg varchar;

  declare _err_msg varchar;
  declare _random1 integer;

  _random1 := rnd(9999999);
  _to_do := get_keyword('TO_DO', params);
  _addressold := get_keyword('ADDRESSOLD', params);
  _addressnew := get_keyword('ADDRESSNEW', params);
  _sid := '';
  _sid := get_keyword('SESS', params);
   _login_ok := 'NO';
        _add_ok := 'NO';
        _update_ok := 'NO';

 if (_sid   <> '')
 {
  if (exists (select * from wapsession where sid = _sid) )
  {
   _login_ok := 'YES';
   _user_name := (select _username from wapsession where sid = _sid);
   update wapsession set _time_last_active = datestring ( now() ) where  sid = _sid;

   if (_to_do = 'ADD')
   {
    if (not exists (select 1 from wapaddresses where _username = _user_name and _email_desc = _addressnew))
    {
     insert into wapaddresses (_username, _email_address, _email_desc) values (_user_name, _addressnew,_addressnew);
     _err_msg := '';
     _res_msg := 'Added';
     _email_msg := _addressnew;
     _add_ok := 'YES';
    }else{
      _err_msg := 'Email Address Already exists';
    }
   }

   if (_to_do = 'UPDATE')
   {
    if (not exists (select 1 from wapaddresses where _username = _user_name and _email_desc = _addressnew))
    {
     update wapaddresses set _email_desc = _addressnew, _email_address = _addressnew where _username = _user_name and _email_address = _addressold and _email_desc = _addressold;
     _err_msg := '';
     _res_msg := 'Updated';
     _email_msg := _addressnew;
     _update_ok := 'YES';
    }else{
      _err_msg := 'Email Address Already exists';
    }
   }

   if (_to_do = 'DELETE')
   {
    if (exists (select 1 from wapaddresses where _username = _user_name and _email_address = _addressold))
    {
     delete from wapaddresses where _username = _user_name and _email_address = _addressold;
     _err_msg := '';
     _res_msg := 'Deleted';
     _email_msg := _addressold;
     _delete_ok := 'YES';
    }else{
      _err_msg := 'Email Address Does Not Exist';
    }
   }
  }
  }else{
    _login_ok := 'NO';
  }

if ( (_login_ok = 'YES' ) and (_to_do = 'LIST' or _add_ok = 'YES' or _update_ok = 'YES' or _delete_ok = 'YES' ) )
{ http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">

<wml>
 <?vsp
 if (_add_ok = 'YES' or _update_ok = 'YES' or _delete_ok = 'YES')
 {
 ?>
 <card id="addok">
   <onevent type="onenterforward">
           <refresh>
    <setvar name="addressnew1" value=""/>
    <setvar name="addressold1" value=""/>
           </refresh>
    </onevent>
  <do type="accept" label="Mail Menu">
   <go href="checkmail.vsp?SESS=<?=_sid?>&amp;REF=<?=_random1?>"></go>
  </do>
  <do type="options" label="Option">
   <go href="#opt"/>
  </do>
  <p>
         Email Address:<br/>
         <?=_email_msg?> <?=_res_msg?>.
  </p>
    <p><a href="checkmail.vsp?SESS=<?=_sid?>&amp;REF=<?=_random1?>" title="Continue">Mail Menu</a></p>
 </card>
 <?vsp
 }
 ?>
<card id="main1">
  <onevent type="onenterforward">
          <refresh>
   <setvar name="addressnew1" value=""/>
   <setvar name="addressold1" value=""/>
          </refresh>
   </onevent>
 <do type="options" label="Option">
  <go href="#opt"/>
 </do>
 <p>Address Book:</p>
  <p mode="nowrap"><a href="#add" title="New">New Address</a></p>
 <?vsp
 if (exists(select * from wapaddresses where _username = _user_name))
 {
  for select * from wapaddresses where _username = _user_name do
  {
   http(sprintf('
   <p mode="nowrap"><a href="compose.vsp?SESS=%s&amp;REF=%d&amp;TO=%s" title="Mail to">%s</a></p>\n',
   _sid, _random1, _email_address, _email_address));
  }
 }
 ?>
</card>

<card id="opt">
   <onevent type="onenterforward">
          <refresh>
   <setvar name="addressnew1" value=""/>
   <setvar name="addressold1" value=""/>
          </refresh>
   </onevent>
 <do type="options" label="Exit">
  <go href="#main1"/>
 </do>
   <p mode="nowrap"><a href="#add">Add</a></p>
   <p mode="nowrap"><a href="addressbook.vsp?SESS=<?=_sid?>&amp;TO_DO=ERASE&amp;REF=<?vsp http(sprintf('%d',_random1));?>" title="Delete">Delete</a></p>
   <p mode="nowrap"><a href="addressbook.vsp?SESS=<?=_sid?>&amp;TO_DO=MODIFY1&amp;REF=<?vsp http(sprintf('%d',_random1));?>" title="Modify">Modify</a></p>
   <p mode="nowrap"><a href="checkmail.vsp?SESS=<?=_sid?>&amp;REF=<?vsp http(sprintf('%d',_random1));?>" title="Mail Menu">Mail Menu</a></p>
   <p mode="nowrap"><a href="#main1" title="Exit">Exit</a></p>
</card>

<card id="add">
   <onevent type="onenterforward">
          <refresh>
   <setvar name="addressnew1" value=""/>
   <setvar name="addressold1" value=""/>
          </refresh>
   </onevent>
 <do type="options" label="Options">
  <go href="#opt"/>
 </do>
    <p>Emai Address:<input name="addressnew1" value="" maxlength="30" /></p>
 <p mode="nowrap">
 Add Email Address: <br/>
 $(addressnew1) ?
 </p>
      <p><a href="addressbook.vsp?SESS=<?=_sid?>&amp;TO_DO=ADD&amp;ADDRESSNEW=$(addressnew1:e)" title="Yes">Yes</a></p>
      <p><a href="#main1" title="No">No</a></p>
</card>
</wml>
<?vsp
}

if ((_login_ok = 'YES' ) and( _to_do = 'ERASE' ) )
{ http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">

<wml>
<card id="main1">
 <do type="options" label="Options">
  <go href="#opt"/>
 </do>
   <?vsp
    if(exists(select * from wapaddresses where _username = _user_name ))
    {
     http(sprintf('<p>Delete Address</p>'));
   for select * from wapaddresses where _username = _user_name  do
   {
    http(sprintf('<p mode="nowrap"><a href="addressbook.vsp?SESS=%s&amp;TO_DO=DELETE&amp;ADDRESSOLD=%s" title="Delete">%s</a></p>',_sid, _email_address, _email_address));
                 }
               }else{
                http(sprintf('<p>There is no email address avaliable for deletion</p>'));
               }
    ?>
</card>

<card id="opt">
   <onevent type="onenterforward">
          <refresh>
   <setvar name="addressnew1" value=""/>
   <setvar name="addressold1" value=""/>
          </refresh>
   </onevent>
 <do type="options" label="Exit">
  <go href="#main1"/>
 </do>
   <p mode="nowrap"><a href="#add" title="Add">Add</a></p>
   <p mode="nowrap"><a href="#main1" title="Delete">Delete</a></p>
   <p mode="nowrap"><a href="addressbook.vsp?SESS=<?=_sid?>&amp;TO_DO=MODIFY1&amp;REF=<?vsp http(sprintf('%d',_random1));?>" title="Modify">Modify</a></p>
   <p mode="nowrap"><a href="checkmail.vsp?SESS=<?=_sid?>&amp;REF=<?vsp http(sprintf('%d',_random1));?>" title="Mail Menu">Mail Menu</a></p>
   <p mode="nowrap"><a href="#main1" title="Exit">Exit</a></p>
</card>
<card id="add">
   <onevent type="onenterforward">
          <refresh>
   <setvar name="addressnew1" value=""/>
   <setvar name="addressold1" value=""/>
          </refresh>
   </onevent>
 <do type="options" label="Options">
  <go href="#opt"/>
 </do>
    <p>Emai Address:<input name="addressnew1" value="" maxlength="30" /></p>
 <p mode="nowrap">
 Add Email Address: <br/>
 $(addressnew1) ?
 </p>
      <p><a href="addressbook.vsp?SESS=<?=_sid?>&amp;TO_DO=ADD&amp;ADDRESSNEW=$(addressnew1:e)" title="Yes">Yes</a></p>
      <p><a href="#main1" title="No">No</a></p>
</card>
</wml>
<?vsp
}

if ( (_login_ok = 'YES' ) and (_to_do = 'MODIFY1' ) )
{ http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">

<wml>
<card id="main1">
 <do type="options" label="Options">
  <go href="#opt"/>
 </do>
   <?vsp
    if(exists(select * from wapaddresses where _username = _user_name ))
    {
     http(sprintf('<p>Modify Address</p>'));
   for select * from wapaddresses where _username = _user_name  do
   {
    http(sprintf('<p mode="nowrap"><a href="addressbook.vsp?SESS=%s&amp;TO_DO=MODIFY2&amp;ADDRESSOLD=%s" title="Modify">%s</a></p>',_sid, _email_address, _email_address));
                 }
               }else{
                http(sprintf('<p>There is no email address avaliable for modification</p>'));
               }
    ?>
</card>

<card id="opt">
   <onevent type="onenterforward">
          <refresh>
   <setvar name="addressnew1" value=""/>
   <setvar name="addressold1" value=""/>
          </refresh>
   </onevent>
 <do type="options" label="Exit">
  <go href="#main1"/>
 </do>
   <p mode="nowrap"><a href="#add" title="Add">Add</a></p>
   <p mode="nowrap"><a href="addressbook.vsp?SESS=<?=_sid?>&amp;TO_DO=ERASE&amp;REF=<?vsp http(sprintf('%d',_random1));?>" title="Delete">Delete</a></p>
   <p mode="nowrap"><a href="#main1" title="Modify">Modify</a></p>
   <p mode="nowrap"><a href="checkmail.vsp?SESS=<?=_sid?>&amp;REF=<?vsp http(sprintf('%d',_random1));?>" title="Mail Menu">Mail Menu</a></p>
   <p mode="nowrap"><a href="#main1" title="Exit">Exit</a></p>
</card>
<card id="add">
   <onevent type="onenterforward">
          <refresh>
   <setvar name="addressnew1" value=""/>
   <setvar name="addressold1" value=""/>
          </refresh>
   </onevent>
 <do type="options" label="Options">
  <go href="#opt"/>
 </do>
    <p>Emai Address:<input name="addressnew1" value="" maxlength="30" /></p>
 <p mode="nowrap">
 Add Email Address: <br/>
 $(addressnew1) ?
 </p>
      <p><a href="addressbook.vsp?SESS=<?=_sid?>&amp;TO_DO=ADD&amp;ADDRESSNEW=$(addressnew1:e)" title="Yes">Yes</a></p>
      <p><a href="#main1" title="No">No</a></p>
</card>
</wml>
<?vsp
}

if ( (_login_ok = 'YES' ) and (_to_do = 'MODIFY2' ) )
{ http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">

<wml>
<card id="main1">
 <do type="options" label="Options">
  <go href="#opt"/>
 </do>
        <p>Modify <?=_addressold?> to</p>
        <p>New Emai Address:</p>
        <p><input name="addressnew1" value="" maxlength="30" /></p>
 <p mode="nowrap">
 Modify to: <br/>
 $(addressnew1) ?
 </p>
      <p><a href="addressbook.vsp?SESS=<?=_sid?>&amp;TO_DO=UPDATE&amp;ADDRESSOLD=<?=_addressold?>&amp;ADDRESSNEW=$(addressnew1:e)" title="Yes">Yes</a></p>
      <p><a href="#main1" title="No">No</a></p>
</card>

<card id="opt">
   <onevent type="onenterforward">
          <refresh>
   <setvar name="addressnew1" value=""/>
   <setvar name="addressold1" value=""/>
          </refresh>
   </onevent>
 <do type="options" label="Exit">
  <go href="#main1"/>
 </do>
   <p mode="nowrap"><a href="#add" title="Add">Add</a></p>
   <p mode="nowrap"><a href="addressbook.vsp?SESS=<?=_sid?>&amp;TO_DO=ERASE&amp;REF=<?vsp http(sprintf('%d',_random1));?>" title="Delete">Delete</a></p>
   <p mode="nowrap"><a href="addressbook.vsp?SESS=<?=_sid?>&amp;TO_DO=MODIFY1&amp;REF=<?vsp http(sprintf('%d',_random1));?>" title="Modify">Modify</a></p>
   <p mode="nowrap"><a href="checkmail.vsp?SESS=<?=_sid?>&amp;REF=<?vsp http(sprintf('%d',_random1));?>" title="Mail Menu">Mail Menu</a></p>
   <p mode="nowrap"><a href="#main1" title="Exit">Exit</a></p>
</card>
<card id="add">
   <onevent type="onenterforward">
          <refresh>
   <setvar name="addressnew1" value=""/>
   <setvar name="addressold1" value=""/>
          </refresh>
   </onevent>
 <do type="options" label="Options">
  <go href="#opt"/>
 </do>
    <p>Emai Address:<input name="addressnew1" value="" maxlength="30" /></p>
 <p mode="nowrap">
 Add Email Address: <br/>
 $(addressnew1) ?
 </p>
      <p><a href="addressbook.vsp?SESS=<?=_sid?>&amp;TO_DO=ADD&amp;ADDRESSNEW=$(addressnew1:e)" title="Yes">Yes</a></p>
      <p><a href="#main1" title="No">No</a></p>
</card>
</wml>
<?vsp
}
if(_login_ok = 'NO')
{ http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?>
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">


<wml>
  <card id="P1" title="Wrong" >
    <p mode="nowrap">
   Wrong Login
    </p>
  </card>
</wml>
<?vsp
}
?>

