passwd verification
[outofuni/stafforg.git] / app / www / js / index.js
1 /*
2  * stafforg: crew organizaion app
3  * author: hackbard@hackdaworld.org
4  *
5  */
6
7 /*
8  * Licensed to the Apache Software Foundation (ASF) under one
9  * or more contributor license agreements.  See the NOTICE file
10  * distributed with this work for additional information
11  * regarding copyright ownership.  The ASF licenses this file
12  * to you under the Apache License, Version 2.0 (the
13  * "License"); you may not use this file except in compliance
14  * with the License.  You may obtain a copy of the License at
15  *
16  * http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing,
19  * software distributed under the License is distributed on an
20  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21  * KIND, either express or implied.  See the License for the
22  * specific language governing permissions and limitations
23  * under the License.
24  */
25
26 var stafforg = {
27         init: function() {
28                 document.addEventListener('deviceready',this.startup,false);
29                 window.addEventListener('orientationchange',function() {
30                         setTimeout(function() {
31                                 cl("orientation change ...");
32                         },200);
33                 });
34         },
35         startup: function() {
36                 cl("starting stafforg app ...");
37
38                 // idb
39                 //idb.name='stafforg';
40                 //idb.del();
41                 //return;
42                 var stores={
43                         'admin': db_admin_store
44                 }
45                 idb.init('stafforg',stafforg.dbcb,1,stores);
46
47                 // ui will be initialized in db callback
48         },
49         dbcb: function() {
50                 stafforg.activate_subapp('start');
51                 $('.startbtn').click(function(event) {
52                         var sapp=event.target.id.replace(/^sel/,'');
53                         cl("clicked "+event.target.id+", starting "+sapp);
54                         stafforg.event_action(sapp,'start');
55                 });
56         },
57         activate_subapp: function(sapp) {
58                 $('.startbtn').each(function() {
59                         var sappname=this.id.replace(/^sel/,'');
60                         var sappid='#'+sappname;
61                         $(sappid).css('height','100%');
62                         if(sappname==sapp)
63                                 $(sappid).css('display','block');
64                         else
65                                 $(sappid).css('display','none');
66                 });
67                 if(sapp=='start') {
68                         $('#start').css('height','100%');
69                         $('#start').css('display','block');
70                 }
71                 else {
72                         $('#start').css('display','none');
73                 }
74                 vert_align_text('.head');
75                 switch(sapp) {
76                 case 'admin':
77                         stafforg.admin_init();
78                         break;
79                 }
80         },
81         event_action: function(sapp,type) {
82                 switch(type) {
83                 case 'start':
84                         stafforg.activate_subapp(sapp);
85                         break;
86                 }
87         },
88         admin_init: function() {
89                 idb.get_item_by_key('admin',1,function(item) {
90                         if(item.passwd==undefined) {
91                                 $('#adminset').css('display','block');
92                                 $('#adminauth').css('display','none');
93                                 $('#adminmain').css('display','none');
94                                 $('#adminpw1').keyup(function() {
95                                         stafforg.checkpw();
96                                 });
97                                 $('#adminpw2').keyup(function() {
98                                         stafforg.checkpw();
99                                 });
100                         }
101                         else {
102                                 $('#adminset').css('display','none');
103                                 $('#adminauth').css('display','block');
104                                 $('#adminmain').css('display','none');
105                                 $('#adminpw').each(function() {
106                                         cl("yep ...");
107                                 });
108                                 $('#adminpw').keyup(function() {
109                                         stafforg.verifypw(item.passwd);
110                                 });
111                         }
112                 });
113         },
114         checkpw: function() {
115                 var pw1=$('#adminpw1').val();
116                 var pw2=$('#adminpw2').val();
117
118                 if(pw1=='') {
119                         $('#adminpw1').css('border-color','red');
120                         return;
121                 }
122                 else {
123                         $('#adminpw1').css('border-color','black');
124                 }
125
126                 if(pw2=='') {
127                         $('#adminpw2').css('border-color','grey');
128                         return;
129                 }
130
131                 if(pw1!=pw2) {
132                         $('#adminpw2').css('border-color','red');
133                         return;
134                 }
135
136                 if(pw1==pw2) {
137                         var nitem={
138                                 passwd: pw1
139                         };
140                         idb.update_store_item('admin',1,nitem,function() {
141                                 cl("password '"+nitem.passwd+"' set!");
142                                 setTimeout(function() {
143                                         stafforg.admin_init();
144                                 },200);
145                         });
146                         return;
147                 }
148         },
149         verifypw: function(passwd) {
150                 cl("verify called!");
151                 if($('#adminpw').val()!=passwd) {
152                         $('#adminpw').css('border-color','red');
153                 }
154                 else {
155                         alert("Thanks!");
156                 }
157                 return;
158         }
159 };
160
161 $(document).ready(function() {
162         if('cordova' in window) {
163                 stafforg.init();
164         }
165         else {
166                 stafforg.startup();
167         }
168 });
169