446c060c8b63d93a13194106fd5598b77210e75c
[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                 var stores={
40                         'admin': db_admin_store
41                 }
42                 idb.init('stafforg',stafforg.dbcb,1,stores);
43
44                 // ui will be initialized in db callback
45         },
46         dbcb: function() {
47                 stafforg.activate_subapp('start');
48                 $('.startbtn').click(function(event) {
49                         var sapp=event.target.id.replace(/^sel/,'');
50                         cl("clicked "+event.target.id+", starting "+sapp);
51                         stafforg.event_action(sapp,'start');
52                 });
53         },
54         activate_subapp: function(sapp) {
55                 $('.startbtn').each(function() {
56                         var sappname=this.id.replace(/^sel/,'');
57                         var sappid='#'+sappname;
58                         $(sappid).css('height','100%');
59                         if(sappname==sapp)
60                                 $(sappid).css('display','block');
61                         else
62                                 $(sappid).css('display','none');
63                 });
64                 if(sapp=='start') {
65                         $('#start').css('height','100%');
66                         $('#start').css('display','block');
67                 }
68                 else {
69                         $('#start').css('display','none');
70                 }
71                 vert_align_text('.head');
72                 switch(sapp) {
73                 case 'admin':
74                         stafforg.admin_init();
75                         break;
76                 }
77         },
78         event_action: function(sapp,type) {
79                 switch(type) {
80                 case 'start':
81                         stafforg.activate_subapp(sapp);
82                         break;
83                 }
84         },
85         admin_init: function() {
86                 idb.get_item_by_key('admin',1,function(item) {
87                         if(item.passwd==undefined) {
88                                 $('#adminset').css('display','block');
89                                 $('#adminauth').css('display','none');
90                                 $('#adminmain').css('display','none');
91                                 $('#adminpw1').keyup(function() {
92                                         stafforg.checkpw();
93                                 });
94                                 $('#adminpw2').keyup(function() {
95                                         stafforg.checkpw();
96                                 });
97                         }
98                         else {
99                         }
100                 });
101         },
102         checkpw: function() {
103                 var pw1=$('#adminpw1').val();
104                 var pw2=$('#adminpw2').val();
105
106                 if(pw1=='') {
107                         $('#adminpw1').css('border-color','red');
108                         return
109                 }
110                 else {
111                         $('#adminpw1').css('border-color','black');
112                 }
113
114                 if(pw2=='') {
115                         $('#adminpw2').css('border-color','grey');
116                         return
117                 }
118
119                 if(pw1!=pw2) {
120                         $('#adminpw2').css('border-color','red');
121                         return
122                 }
123
124                 if(pw1==pw2) {
125                         alert("yep, i will store this password!");
126                 }
127         }
128 };
129
130 $(document).ready(function() {
131         if('cordova' in window) {
132                 stafforg.init();
133         }
134         else {
135                 stafforg.startup();
136         }
137 });
138