forked from guy-peer/mybudgetmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewItem.js
executable file
·186 lines (142 loc) · 5.57 KB
/
NewItem.js
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
$(document).ready(function() {
var categoryToSubCategory = {};
$(".container1").hide();
$(".container2").hide();
$("#Ready").click(function () {
$("#Ready").slideUp(400);
$("#Custom").slideUp(400);
$(".container1").slideDown(500);
});
$("#Custom").click(function () {
$("#Ready").slideUp(400);
$("#Custom").slideUp(400);
$(".container2").slideDown(500);
});
$("#Back1").click(function () {
$(".container1").slideUp(400);
$(".container2").slideUp(400);
$("#Ready").slideDown(500);
$("#Custom").slideDown(500);
});
$("#Back2").click(function () {
$(".container1").slideUp(400);
$(".container2").slideUp(400);
$("#Ready").slideDown(400);
$("#Custom").slideDown(400);
});
var Item = Parse.Object.extend("Cost_Items");
var query = new Parse.Query(Item);
query.find({
success: function (results) {
for (var i = 0 ; i < results.length ; i++) {
var category = results[i].get("Category");
var subCategory = results[i].get("SubCategory");
if (categoryToSubCategory.hasOwnProperty(category)) {
var existingSubCategories = categoryToSubCategory[category];
if (subCategory && $.inArray(subCategory, existingSubCategories) < 0){
existingSubCategories.push(subCategory);
}
}
else{
if (subCategory) {
categoryToSubCategory[category] = [subCategory];
}
}
}
$("#savenewitem1").click(function () {
var Amount = $("#Amount1").val();
var insertCostItem = true;
if (!isAmountInDailyBudget(Amount)) {
insertCostItem = confirm("You are about to go over your daily budget. Continue anyway?")
}
if (!isAmountInInMonthlyBudget(Amount)) {
insertCostItem = confirm("You are about to go over your monthly budget. Continue anyway?")
}
if (insertCostItem) {
var Category = $("#Category1").val();
var Subcats = $("#SubCats1").val();
var user = Parse.User.current();
var newItem = new Item();
newItem.set("Category", Category);
newItem.set("SubCategory", Subcats);
newItem.set("Amount", Amount);
newItem.set("user", user);
newItem.save({
success: function () {
document.location = "MainPage.html";
}
});
}
});
$("#savenewitem2").click(function () {
var Category2 = $("#Category2").val();
var Subcats2 = $("#SubCats2").val();
var Amount2 = $("#Amount2").val();
var user2 = Parse.User.current();
var newItem = new Item();
newItem.set("Category", Category2);
newItem.set("SubCategory", Subcats2);
newItem.set("Amount", Amount2);
newItem.set("user", user2);
newItem.save({
success: function () {
document.location = "MainPage.html";
}
});
})
$(document).trigger('dataLoaded');
}
});
function isAmountInDailyBudget(amount) {
if (commonObj.dailyBudget - commonObj.totalAmountSpentToday - amount >= 0) {
return true;
}
return false;
}
function isAmountInInMonthlyBudget(amount) {
if (commonObj.monthlyBudget - commonObj.totalAmountSpentThisMonth - amount >= 0) {
return true;
}
return false;
}
$("#logoutDeskMenu").click(logOut);
$("#userlogout").click(logOut);
$("#logoutSlideMenu").click(logOut);
function logOut(){
Parse.User.logOut();
location="Welcom.html";
}
$(document).on("dataLoaded", function() {
var keys = Object.keys(categoryToSubCategory);
for (var i = 0 ; i < keys.length ; i++){
var opt = document.createElement('option');
opt.value = keys[i];
opt.textContent = keys[i];
$("#Category1").append(opt);
}
$("#Category1").on('change', function () {
list(categoryToSubCategory[$(this).val()])
})
});
function list(array_list) {
$("#SubCats1").html("");
var pleaseSelectOption = document.createElement('option');
pleaseSelectOption.value = '';
pleaseSelectOption.textContent = '-- Please Select --';
$("#SubCats1").append(pleaseSelectOption);
$(array_list).each(function (i) {
var opt = document.createElement('option');
opt.value = this.toString();
opt.textContent = this.toString();
$("#SubCats1").append(opt);
});
}
var MSM = $("#mobileMenuButton");
MSM.click(function(event) {
$("#sideMenu").slideDown(400);
});
var CMSM = $("#closeMobileMenuButton");
CMSM.click(function(event) {
$("#sideMenu").slideUp(200);
});
});