Spaces:
Running
Running
File size: 8,182 Bytes
38738e2 b68eb43 feae401 38738e2 b68eb43 feae401 b68eb43 38738e2 b68eb43 feae401 b68eb43 feae401 b68eb43 38738e2 b68eb43 b7d6890 38738e2 b7d6890 38738e2 b68eb43 feae401 b68eb43 feae401 b68eb43 38738e2 b68eb43 38738e2 feae401 b68eb43 38738e2 b68eb43 38738e2 |
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 |
document.addEventListener('DOMContentLoaded', function() {
// DOM Elements
const amountButtons = document.querySelectorAll('.amount-btn');
const currencyButtons = document.querySelectorAll('.currency-btn');
const form = document.getElementById('checkoutForm');
const userNameInput = document.getElementById('userName');
const walletAddressInput = document.getElementById('walletAddress');
const walletFeedback = document.getElementById('walletFeedback');
const proceedButton = document.getElementById('proceedButton');
const simulatedReturnBox = document.getElementById('simulatedReturnBox');
const returnAmountEl = document.getElementById('returnAmount');
const totalAmountEl = document.getElementById('totalAmount');
const selectedAmountInput = document.getElementById('selectedAmount');
const selectedCurrencyInput = document.getElementById('selectedCurrency');
// Preview Elements
const previewAmount = document.getElementById('previewAmount');
const previewCurrency = document.getElementById('previewCurrency');
const previewWalletType = document.getElementById('previewWalletType');
const currencyNote = document.getElementById('currencyNote');
// State
let selectedAmount = null;
let selectedCurrency = null;
let simulatedReturn = null;
// Amount Selection
amountButtons.forEach(button => {
button.addEventListener('click', () => {
amountButtons.forEach(btn => btn.classList.remove('active'));
button.classList.add('active');
selectedAmount = parseInt(button.dataset.value);
selectedAmountInput.value = selectedAmount;
updatePreview();
validateForm();
// Handle simulated return for $50
if(selectedAmount === 50) {
const percentage = Math.floor(Math.random() * 21) + 20; // 20-40
simulatedReturn = (50 * percentage / 100).toFixed(2);
const totalAmount = (50 + parseFloat(simulatedReturn)).toFixed(2);
returnAmountEl.textContent = `Bonus return: ${simulatedReturn} at ${percentage}%`;
totalAmountEl.textContent = `Total amount: ${totalAmount}`;
simulatedReturnBox.classList.remove('hidden');
} else {
simulatedReturnBox.classList.add('hidden');
}
});
});
// Currency Selection
currencyButtons.forEach(button => {
button.addEventListener('click', () => {
currencyButtons.forEach(btn => btn.classList.remove('active'));
button.classList.add('active');
selectedCurrency = button.dataset.currency;
selectedCurrencyInput.value = selectedCurrency;
updatePreview();
validateForm();
updateCurrencyNote();
});
});
// Input Validation
userNameInput.addEventListener('input', validateForm);
walletAddressInput.addEventListener('input', validateWalletAddress);
// Form submission
form.addEventListener('submit', function(e) {
e.preventDefault();
if (validateForm()) {
// Redirect to confirmation page with query parameters
window.location.href = `confirmation.html?amount=${selectedAmount}¤cy=${selectedCurrency}`;
}
});
// Validation Functions
function validateWalletAddress() {
const address = walletAddressInput.value.trim();
let isValid = false;
let message = '';
if(!selectedCurrency) {
walletFeedback.textContent = 'Please select a currency first';
walletFeedback.className = 'mt-2 text-sm text-yellow-400';
walletAddressInput.classList.remove('valid', 'invalid');
return false;
}
switch(selectedCurrency) {
case 'USDT':
case 'BNB':
// ^0x[a-fA-F0-9]{40}$
const bep20Regex = /^0x[a-fA-F0-9]{40}$/;
isValid = bep20Regex.test(address);
message = isValid ? 'Valid BEP20 address' : 'Invalid BEP20 address (must start with 0x and be 42 characters)';
break;
case 'BTC':
// ^(bc1[ac-hj-np-z02-9]{11,71}|[13][a-km-zA-HJ-NP-Z1-9]{25,34})$
const btcRegex = /^(bc1[ac-hj-np-z02-9]{11,71}|[13][a-km-zA-HJ-NP-Z1-9]{25,34})$/;
isValid = btcRegex.test(address);
message = isValid ? 'Valid Bitcoin address' : 'Invalid Bitcoin address format';
break;
case 'SOL':
// ^[1-9A-HJ-NP-Za-km-z]{32,44}$
const solRegex = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
isValid = solRegex.test(address);
message = isValid ? 'Valid Solana address' : 'Invalid Solana address format';
break;
}
// Update UI
walletFeedback.textContent = message;
walletFeedback.className = `mt-2 text-sm ${isValid ? 'text-green-400' : 'text-red-400'}`;
walletAddressInput.classList.toggle('valid', isValid);
walletAddressInput.classList.toggle('invalid', !isValid && address !== '');
validateForm(); // Re-validate form
return isValid;
}
function validateForm() {
const isAmountSelected = selectedAmount !== null;
const isCurrencySelected = selectedCurrency !== null;
const isNameFilled = userNameInput.value.trim() !== '';
const isWalletValid = selectedCurrency ? validateWalletAddress() : true; // Allow form to be valid even without wallet validation initially
const isFormValid = isAmountSelected && isCurrencySelected && isNameFilled && (isWalletValid || walletAddressInput.value.trim() === '');
// Update proceed button state
proceedButton.disabled = !isFormValid;
if (isFormValid) {
proceedButton.classList.remove('opacity-50', 'pointer-events-none');
proceedButton.classList.add('hover:opacity-100');
} else {
proceedButton.classList.add('opacity-50', 'pointer-events-none');
proceedButton.classList.remove('hover:opacity-100');
}
return isFormValid;
}
// Re-validate when wallet address is empty
walletAddressInput.addEventListener('blur', function() {
if (this.value.trim() === '') {
walletFeedback.textContent = '';
walletFeedback.className = 'mt-2 text-sm';
this.classList.remove('valid', 'invalid');
validateForm();
}
});
// UI Update Functions
function updatePreview() {
previewAmount.textContent = selectedAmount ? `${selectedAmount}` : '$0';
previewCurrency.textContent = selectedCurrency || '-';
if(selectedCurrency) {
previewWalletType.textContent = getWalletTypeName(selectedCurrency);
} else {
previewWalletType.textContent = '-';
}
}
function updateCurrencyNote() {
let note = '';
switch(selectedCurrency) {
case 'USDT':
note = 'BEP20 addresses start with 0x... (42 chars).';
break;
case 'BNB':
note = 'BEP20 addresses start with 0x... (42 chars).';
break;
case 'BTC':
note = 'Only mainnet formats 1/3/bc1 are accepted in this demo.';
break;
case 'SOL':
note = 'Base58 address; avoid ambiguous characters.';
break;
default:
note = 'Select a currency to see format requirements.';
}
currencyNote.textContent = note;
}
function getWalletTypeName(currency) {
switch(currency) {
case 'USDT': return 'BEP20';
case 'BNB': return 'BEP20';
case 'BTC': return 'Bitcoin';
case 'SOL': return 'Solana';
default: return '-';
}
}
// Initialize
updatePreview();
updateCurrencyNote();
});
|