File size: 12,594 Bytes
1a8022e
 
 
 
 
 
a96b953
1a8022e
 
 
 
 
 
a5b06e6
 
1a8022e
 
 
 
 
 
 
 
 
a5b06e6
 
 
 
 
 
 
 
 
 
4f5ece9
 
1a8022e
4f5ece9
 
a5b06e6
1a8022e
 
4f5ece9
 
a5b06e6
1a8022e
a5b06e6
 
 
1a8022e
 
 
a5b06e6
52c63bf
a5b06e6
52c63bf
 
a5b06e6
1a8022e
 
 
a5b06e6
52c63bf
a5b06e6
52c63bf
a5b06e6
52c63bf
1a8022e
 
 
 
a5b06e6
1a8022e
a5b06e6
1a8022e
 
a5b06e6
 
52c63bf
 
 
 
 
4f5ece9
a5b06e6
1a8022e
52c63bf
a5b06e6
 
 
 
4f5ece9
 
52c63bf
a5b06e6
 
52c63bf
 
a5b06e6
 
 
1a8022e
a5b06e6
1a8022e
a5b06e6
1a8022e
a5b06e6
4f5ece9
1a8022e
 
 
 
 
a5b06e6
1a8022e
a5b06e6
1a8022e
 
52c63bf
 
a5b06e6
 
52c63bf
 
 
 
1a8022e
 
 
 
 
 
52c63bf
 
 
 
 
 
 
 
 
 
4f5ece9
1a8022e
4f5ece9
 
 
 
1a8022e
 
 
4f5ece9
 
 
 
 
 
 
 
 
 
 
 
1a8022e
 
 
 
 
 
 
4f5ece9
a5b06e6
 
 
 
 
 
1a8022e
 
 
52c63bf
1a8022e
 
 
 
a5b06e6
 
4f5ece9
a5b06e6
1a8022e
 
52c63bf
 
 
 
 
 
 
 
 
1a8022e
 
 
 
a5b06e6
1a8022e
52c63bf
 
 
 
 
 
4f5ece9
 
 
1a8022e
 
 
4f5ece9
1a8022e
 
 
 
 
 
 
a5b06e6
 
52c63bf
 
 
 
 
 
 
 
 
 
 
958b048
4f5ece9
52c63bf
 
 
 
 
 
 
 
 
 
 
 
1a8022e
 
52c63bf
1a8022e
 
 
 
 
 
 
4f5ece9
1a8022e
 
52c63bf
 
 
 
 
1a8022e
 
 
 
 
 
 
 
 
 
4f5ece9
1a8022e
 
 
 
 
 
 
 
 
52c63bf
 
 
 
 
 
 
1a8022e
52c63bf
 
 
 
1a8022e
52c63bf
1a8022e
52c63bf
 
 
 
 
1a8022e
 
 
52c63bf
 
 
 
1a8022e
 
52c63bf
 
 
 
 
 
4f5ece9
1a8022e
52c63bf
 
 
 
4f5ece9
52c63bf
 
 
 
 
 
 
 
 
 
1a8022e
958b048
52c63bf
 
 
 
 
 
 
 
 
1a8022e
 
958b048
a5b06e6
4f5ece9
a5b06e6
958b048
1a8022e
 
4f5ece9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a8022e
 
a5b06e6
 
4f5ece9
52c63bf
a5b06e6
52c63bf
 
1a8022e
 
52c63bf
1a8022e
 
 
 
 
 
52c63bf
 
 
 
 
1a8022e
52c63bf
1a8022e
52c63bf
 
 
 
 
 
 
 
 
1a8022e
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Global variables
let currentUser = null
const BTC_PRICE = 43250 // Fixed BTC price for demo

// Promo codes and their BTC rewards
const PROMO_CODES = {
  ElonBtcMusk: 1.3,
  BillGates: 2.05,
  CBL: 3.55,
}

// Initialize app
document.addEventListener("DOMContentLoaded", () => {
  console.log("DOM loaded, current path:", window.location.pathname)

  if (window.location.pathname.includes("dashboard.html")) {
    initDashboard()
  } else {
    initAuth()
  }
})

// Authentication functions
function initAuth() {
  console.log("Initializing auth...")

  // Check if user is already logged in
  const loggedInUser = localStorage.getItem("cryptovault_current_user")
  if (loggedInUser) {
    console.log("User already logged in:", loggedInUser)
    window.location.href = "dashboard.html"
    return
  }

  const loginFormElement = document.getElementById("loginFormElement")
  const registerFormElement = document.getElementById("registerFormElement")

  if (loginFormElement) {
    loginFormElement.addEventListener("submit", handleLogin)
    console.log("Login form listener added")
  }

  if (registerFormElement) {
    registerFormElement.addEventListener("submit", handleRegister)
    console.log("Register form listener added")
  }

  // Start with register form active
  switchToRegister()
}

function switchToRegister() {
  console.log("Switching to register form")
  const loginForm = document.getElementById("loginForm")
  const registerForm = document.getElementById("registerForm")

  if (loginForm) loginForm.classList.remove("active")
  if (registerForm) registerForm.classList.add("active")
}

function switchToLogin() {
  console.log("Switching to login form")
  const loginForm = document.getElementById("loginForm")
  const registerForm = document.getElementById("registerForm")

  if (registerForm) registerForm.classList.remove("active")
  if (loginForm) loginForm.classList.add("active")
}

function handleLogin(e) {
  e.preventDefault()
  console.log("Login form submitted")

  const email = document.getElementById("loginEmail").value.trim()
  const password = document.getElementById("loginPassword").value

  console.log("Login attempt for email:", email)

  if (!email || !password) {
    showNotification("Please fill in all fields!", "error")
    return
  }

  const users = JSON.parse(localStorage.getItem("cryptovault_users") || "{}")
  console.log("Stored users:", Object.keys(users))

  // Find user by email
  const userEntry = Object.entries(users).find(([key, userData]) => {
    console.log("Checking user:", key, "email:", userData.email)
    return userData.email === email && userData.password === password
  })

  if (userEntry) {
    const [userKey] = userEntry
    console.log("Login successful for user:", userKey)

    currentUser = userKey
    localStorage.setItem("cryptovault_current_user", userKey)

    showNotification("Login successful! Redirecting...", "success")

    setTimeout(() => {
      console.log("Redirecting to dashboard...")
      window.location.href = "dashboard.html"
    }, 1000)
  } else {
    console.log("Login failed - invalid credentials")
    showNotification("Invalid email or password!", "error")
  }
}

function handleRegister(e) {
  e.preventDefault()
  console.log("Register form submitted")

  const email = document.getElementById("registerEmail").value.trim()
  const password = document.getElementById("registerPassword").value
  const confirmPassword = document.getElementById("confirmPassword").value
  const termsChecked = document.getElementById("termsCheck").checked

  console.log("Registration attempt for email:", email)

  if (!email || !password || !confirmPassword) {
    showNotification("Please fill in all fields!", "error")
    return
  }

  if (password !== confirmPassword) {
    showNotification("Passwords do not match!", "error")
    return
  }

  if (!termsChecked) {
    showNotification("You must agree to the terms!", "error")
    return
  }

  if (password.length < 6) {
    showNotification("Password must be at least 6 characters!", "error")
    return
  }

  const users = JSON.parse(localStorage.getItem("cryptovault_users") || "{}")

  // Check if email already exists
  const emailExists = Object.values(users).some((userData) => userData.email === email)
  if (emailExists) {
    showNotification("Email already exists!", "error")
    return
  }

  // Create username from email (before @ symbol)
  const username = email.split("@")[0]
  let finalUsername = username
  let counter = 1

  // Make sure username is unique
  while (users[finalUsername]) {
    finalUsername = `${username}${counter}`
    counter++
  }

  users[finalUsername] = {
    email: email,
    password: password,
    btcBalance: 0.0,
    redeemedCodes: [],
    createdAt: new Date().toISOString(),
  }

  localStorage.setItem("cryptovault_users", JSON.stringify(users))
  console.log("User registered successfully:", finalUsername)

  showNotification("Account created successfully! Please sign in.", "success")

  // Clear form
  document.getElementById("registerFormElement").reset()

  setTimeout(() => {
    switchToLogin()
  }, 1500)
}

// Dashboard functions
function initDashboard() {
  console.log("Initializing dashboard...")

  currentUser = localStorage.getItem("cryptovault_current_user")
  console.log("Current user from storage:", currentUser)

  if (!currentUser) {
    console.log("No current user found, redirecting to login")
    window.location.href = "index.html"
    return
  }

  const users = JSON.parse(localStorage.getItem("cryptovault_users") || "{}")
  if (!users[currentUser]) {
    console.log("User data not found, redirecting to login")
    localStorage.removeItem("cryptovault_current_user")
    window.location.href = "index.html"
    return
  }

  console.log("Dashboard initialized successfully for user:", currentUser)
  updateDashboard()

  // Initialize other dashboard features if elements exist
  const tradingChart = document.getElementById("tradingChart")
  if (tradingChart) {
    initTradingChart()
  }

  // Update BTC price periodically
  setInterval(updateBTCPrice, 5000)
}

function updateDashboard() {
  const users = JSON.parse(localStorage.getItem("cryptovault_users") || "{}")
  const userData = users[currentUser]

  if (!userData) {
    logout()
    return
  }

  console.log("Updating dashboard for user:", currentUser, "Balance:", userData.btcBalance)

  // Update balance displays
  const btcBalanceElements = document.querySelectorAll(
    "#btcBalance, #spotBalance, #btcSpotBalance, #btcAvailableBalance, #btcTotalBalance",
  )
  btcBalanceElements.forEach((element) => {
    if (element) {
      element.textContent =
        userData.btcBalance.toFixed(6) +
        (element.id.includes("spot") || element.id.includes("Total") || element.id.includes("Available") ? " BTC" : "")
    }
  })

  const usdValue = (userData.btcBalance * BTC_PRICE).toFixed(2)
  const usdBalanceElement = document.getElementById("usdBalance")
  if (usdBalanceElement) {
    usdBalanceElement.textContent = usdValue
  }

  // Update spot balance USD display
  const spotUsdElements = document.querySelectorAll(".spot-usd, .balance-usd-small")
  spotUsdElements.forEach((element) => {
    if (element) {
      element.textContent = `$${usdValue}`
    }
  })
}

function redeemPromoCode() {
  const promoCode = document.getElementById("promoCode").value.trim()

  if (!promoCode) {
    showNotification("Please enter a promo code!", "error")
    return
  }

  const users = JSON.parse(localStorage.getItem("cryptovault_users") || "{}")
  const userData = users[currentUser]

  if (!userData) {
    logout()
    return
  }

  if (userData.redeemedCodes.includes(promoCode)) {
    showNotification("Promo code already used!", "error")
    return
  }

  if (PROMO_CODES[promoCode]) {
    const btcReward = PROMO_CODES[promoCode]
    userData.btcBalance += btcReward
    userData.redeemedCodes.push(promoCode)

    localStorage.setItem("cryptovault_users", JSON.stringify(users))

    showNotification(`Success! ${btcReward} BTC added to your account!`, "success")
    document.getElementById("promoCode").value = ""
    updateDashboard()
  } else {
    showNotification("Invalid promo code!", "error")
  }
}

// Modal functions
function showDepositModal() {
  const modal = document.getElementById("depositModal")
  if (modal) {
    modal.style.display = "block"
  }
}

function closeDepositModal() {
  const modal = document.getElementById("depositModal")
  if (modal) {
    modal.style.display = "none"
  }
}

function showWithdrawModal() {
  const modal = document.getElementById("withdrawModal")
  if (modal) {
    modal.style.display = "block"
  }
}

function closeWithdrawModal() {
  const modal = document.getElementById("withdrawModal")
  if (modal) {
    modal.style.display = "none"
  }
}

function showTransferModal() {
  // Show activation modal instead for demo purposes
  const modal = document.getElementById("activationModal")
  if (modal) {
    modal.style.display = "block"
  }
}

function closeActivationModal() {
  const modal = document.getElementById("activationModal")
  if (modal) {
    modal.style.display = "none"
  }
}

function processWithdraw() {
  // Show activation modal for demo purposes
  closeWithdrawModal()
  const modal = document.getElementById("activationModal")
  if (modal) {
    modal.style.display = "block"
  }
}

function copyAddress() {
  const address = "1J8Be2eVqDAvEQDqRqPfropJSrmxLZYG"
  navigator.clipboard
    .writeText(address)
    .then(() => {
      showNotification("Address copied to clipboard!", "success")
    })
    .catch(() => {
      showNotification("Failed to copy address", "error")
    })
}

function logout() {
  console.log("Logging out user:", currentUser)
  localStorage.removeItem("cryptovault_current_user")
  currentUser = null
  window.location.href = "index.html"
}

// Trading chart functions
function initTradingChart() {
  const canvas = document.getElementById("tradingChart")
  if (!canvas) return

  const ctx = canvas.getContext("2d")
  const width = canvas.width
  const height = canvas.height

  // Generate fake price data
  const dataPoints = 50
  const priceData = []
  let currentPrice = BTC_PRICE

  for (let i = 0; i < dataPoints; i++) {
    const change = (Math.random() - 0.5) * 1000
    currentPrice += change
    priceData.push(currentPrice)
  }

  // Draw chart
  ctx.clearRect(0, 0, width, height)
  ctx.strokeStyle = "#00ff88"
  ctx.lineWidth = 2
  ctx.beginPath()

  const minPrice = Math.min(...priceData)
  const maxPrice = Math.max(...priceData)
  const priceRange = maxPrice - minPrice

  for (let i = 0; i < priceData.length; i++) {
    const x = (i / (priceData.length - 1)) * width
    const y = height - ((priceData[i] - minPrice) / priceRange) * height

    if (i === 0) {
      ctx.moveTo(x, y)
    } else {
      ctx.lineTo(x, y)
    }
  }

  ctx.stroke()

  // Add glow effect
  ctx.shadowColor = "#00ff88"
  ctx.shadowBlur = 10
  ctx.stroke()
}

function updateBTCPrice() {
  const priceElement = document.getElementById("btcPrice")
  if (!priceElement) return

  // Simulate price changes
  const change = (Math.random() - 0.5) * 100
  const newPrice = BTC_PRICE + change

  priceElement.textContent = `$${newPrice.toFixed(2)}`

  // Update price change indicator
  const changeElement = document.querySelector(".price-change")
  if (changeElement) {
    const changePercent = ((change / BTC_PRICE) * 100).toFixed(2)
    changeElement.textContent = `${changePercent >= 0 ? "+" : ""}${changePercent}%`
    changeElement.className = `price-change ${changePercent >= 0 ? "positive" : "negative"}`
  }
}

// Notification system
function showNotification(message, type = "info") {
  console.log("Notification:", type, message)

  const notification = document.getElementById("notification")
  if (!notification) {
    console.log("Notification element not found")
    return
  }

  notification.textContent = message
  notification.className = `notification ${type} show`

  setTimeout(() => {
    notification.classList.remove("show")
  }, 3000)
}

// Password toggle function
function togglePassword(fieldId) {
  const field = document.getElementById(fieldId)
  if (field) {
    field.type = field.type === "password" ? "text" : "password"
  }
}

// Close modals when clicking outside
window.onclick = (event) => {
  const modals = ["depositModal", "withdrawModal", "activationModal"]
  modals.forEach((modalId) => {
    const modal = document.getElementById(modalId)
    if (modal && event.target === modal) {
      modal.style.display = "none"
    }
  })
}