public
Description: Memo is a shoppinglist application for Pocket PC and other devices that can run javascript.
Homepage: http://github.com/joakimk/memo/wikis/memo
Clone URL: git://github.com/joakimk/memo.git
Click here to lend your support to: memo and make a donation at www.pledgie.com !
joakimk (author)
Thu Jun 26 11:06:02 -0700 2008
commit  40340dcd798a704c10c78044560c71bcdad9c60c
tree    7d218c23349c28a8f88dc71b0dd94e45e7510c3d
parent  99cfa5995aae395297187c0d25c8cb9d4f17d49f
memo / memo.js
100755 207 lines (171 sloc) 4.708 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) 2008 Joakim K.
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from
// the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must
// not claim that you wrote the original software. If you use this
// software in a product, an acknowledgment in the product documentation
// would be appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
 
have = new Array();
need = new Array();
 
function e(id)
{
  return document.getElementById(id);
}
 
function display(id, visible)
{
  e(id).style.display = visible ? '' : 'none';
}
 
function add_item()
{
  display('item_form', true);
}
 
function delete_item()
{
  display('item_delete', true);
  add_items_to_delete_from(need);
  add_items_to_delete_from(have);
}
 
function close_delete_item()
{
  display('item_delete', false);
  e('item_delete_list').innerHTML = '';
}
 
function delete_item_by_name(name)
{
  if(is_in_array(have, name))
    have = exclude_value_from_array(have, name)
  else
    need = exclude_value_from_array(need, name)
    
  save_state();
  close_delete_item();
  e('need').innerHTML = '';
  e('have').innerHTML = '';
  show_needs();
  show_haves();
}
 
function is_in_array(array, name)
{
  for(var i = 0; i < array.length; i++)
  {
    if(array[i] == name)
      return true;
  }
  
  return false;
}
 
function add_items_to_delete_from(list)
{
  for(var i = 0; i < list.length; i++)
  {
    e('item_delete_list').innerHTML += create_list_item('delete_item_by_name', list[i], "Delete");
  }
}
 
function cancel_item()
{
  display('item_form', false);
}
 
function save_item()
{
  display('item_form', false);
 
  var name = e('item_name').value;
  add_have(name);
}
 
function add_have(name)
{
  have.push(name);
  need = exclude_value_from_array(need, name);
  save_state();
  
  e('need').innerHTML = '';
  show_needs();
  show_have(name);
}
 
function add_need(name)
{
  need.push(name);
  have = exclude_value_from_array(have, name);
  save_state();
  
  e('have').innerHTML = '';
  show_haves();
  show_need(name);
}
 
function show_have(name)
{
  e('have').innerHTML += create_list_item('add_need', name, "Need");
}
 
function show_need(name)
{
  e('need').innerHTML += create_list_item('add_have', name, "Have");
}
 
function create_list_item(onclick_method, item_text, button_text)
{
  return '<div class="item"><a href="#" onclick="' + onclick_method + '(\'' + item_text + '\'); return false;" class="button">' + button_text + '</a><div class="text"> ' + item_text + ' </div></div>';
}
 
function exclude_value_from_array(old_array, excluded_value)
{
  new_array = new Array();
  for(var i = 0; i < old_array.length; i++)
  {
    if(old_array[i] != excluded_value)
      new_array.push(old_array[i]);
  }
  
  return new_array;
}
 
function save_state()
{
  save('memo_have', have.join(','));
  save('memo_need', need.join(','));
}
 
function save(name, value)
{
  var date = new Date();
  var days = 500000;
  date.setTime(date.getTime() + days*24*60*60*1000);
  document.cookie = name + "=" + value + "; expires=" + date.toGMTString();
}
 
function load(name)
{
  var pairs = document.cookie.split(';');
  for(var i = 0; i < pairs.length; i++)
  {
    var pair = pairs[i].split('=');
        
    if(pair[0] == ' ' + name || pair[0] == name)
      return pair[1];
  }
}
 
function load_data()
{
  var memo_have = load('memo_have');
  var memo_need = load('memo_need');
 
  if(memo_have != undefined && memo_have != '')
    have = memo_have.split(',');
      
  if(memo_need != undefined && memo_need != '')
    need = memo_need.split(',');
    
  show_haves();
  show_needs();
  
  display('loading', false);
  display('content', true);
}
 
function show_needs()
{
  for(var i = 0; i < need.length; i++)
  {
    show_need(need[i]);
  }
}
 
function show_haves()
{
  for(var i = 0; i < have.length; i++)
  {
    show_have(have[i]);
  }
}
 
// Only way I found to hook into "DOM loaded" on Windows Mobile 5.0 IE.
setTimeout("load_data()", 500);