const deduction = 0.8; const annual = 12; const info = 'facturando mensualmente'; const incrementBoarding = { starter: 50, professional: 500, enterprise: 1000 } const defaultBoarding = { starter: 200, professional: 500, enterprise: 2000 } const originalPriceForBoarding = { starter: 75, professional: 375, enterprise: 250 }; const priceWithDeductionForBoarding = { starter: 60, professional: 300, enterprise: 200 }; const newValues = { starter: 200, professional: 500, enterprise: 2000 }; let showPrice = {} /* typePaymentInPage = true has deduction */ let typePaymentInPage = true; /* Prices showed */ const originalPrices = []; const pricesWithDeduction = []; const allPrice = document.getElementsByClassName('officialPrice'); const allIncreases = document.getElementsByClassName('increasesNumber'); const allBilling = document.getElementsByClassName('billing'); for(let i = 0; i < allPrice.length; i++){ let getPrice = Number(allPrice[i].textContent.replace('$','').replace(',','')); originalPrices.push(getPrice); pricesWithDeduction.push(parseInt(getPrice * deduction)); } /* Boarding */ const starterPack = document.getElementById('starterPack').value; const professionalPack = document.getElementById('professionalPack').value; const enterprisePack = document.getElementById('enterprisePack').value; /* Change values */ const starterPaymentR = document.getElementsByName('starterPayment'); const professionalPaymentR = document.getElementsByName('professionalPayment'); const enterprisePaymentR = document.getElementsByName('enterprisePayment'); /* Page init set annual values */ const setDefaultValue = () => { starterPaymentR[1].checked = true; professionalPaymentR[1].checked = true; enterprisePaymentR[1].checked = true; showPriceInPage(true); changeMontlyPrice(); setAnotherValue('annualPayment'); } /* Show change prices in page */ const showPriceInPage = ( hasDeduction ) => { showPrice = hasDeduction ? pricesWithDeduction : originalPrices; for(let i = 0; i < allPrice.length; i++){ allPrice[i].textContent = "$" + showPrice[i].toLocaleString('en-US'); } } /* Set values with deduction or not */ const setAnotherValue = ( typePayment ) => { switch(typePayment){ case 'montlyPayment': showPriceInPage(false); starterPaymentR[0].checked = true; professionalPaymentR[0].checked = true; enterprisePaymentR[0].checked = true; showPrice = originalPrices; typePaymentInPage = false; changeMontlyPrice(); break; case 'annualPayment': showPriceInPage(true); starterPaymentR[1].checked = true; professionalPaymentR[1].checked = true; enterprisePaymentR[1].checked = true; showPrice = pricesWithDeduction; typePaymentInPage = true; changeMontlyPrice(); break; } setNewValue(newValues.starter,'starterPack'); setNewValue(newValues.professional,'professionalPack'); setNewValue(newValues.enterprise,'enterprisePack'); } /* Change montly price */ const changeMontlyPrice = () => { if(typePaymentInPage){ allIncreases[0].textContent = priceWithDeductionForBoarding.starter; allIncreases[1].textContent = priceWithDeductionForBoarding.professional; allIncreases[2].textContent = priceWithDeductionForBoarding.enterprise; } else { allIncreases[0].textContent = originalPriceForBoarding.starter; allBilling[0].textContent = info; allIncreases[1].textContent = originalPriceForBoarding.professional; allBilling[1].textContent = info; allIncreases[2].textContent = originalPriceForBoarding.enterprise; allBilling[2].textContent = info; } } /* Set new value in Onboarding */ const setNewValue = ( newValue, selectedType ) => { let cash = ''; let value = 0; let newPrice = typePaymentInPage ? pricesWithDeduction : originalPrices; let purchase = typePaymentInPage ? priceWithDeductionForBoarding : originalPriceForBoarding; switch(selectedType){ case 'starterPack': newValues.starter = newValue; value = (newValues.starter - defaultBoarding.starter) / incrementBoarding.starter; value = value >= 0 ? value : 0; cash = newPrice[0] + (value * purchase.starter); allPrice[0].textContent = cash.toLocaleString('en-US'); setInfo(cash, 0); break; case 'professionalPack': newValues.professional = newValue; value = (newValues.professional - defaultBoarding.professional) / incrementBoarding.professional; value = value >= 0 ? value : 0; cash = newPrice[1] + (value * purchase.professional); allPrice[1].textContent = cash.toLocaleString('en-US'); setInfo(cash, 1); break; case 'enterprisePack': newValues.enterprise = newValue; value = (newValues.enterprise - defaultBoarding.enterprise) / incrementBoarding.enterprise; value = value >= 0 ? value : 0; cash = newPrice[2] + (value * purchase.enterprise); allPrice[2].textContent = cash.toLocaleString('en-US'); setInfo(cash, 2); break; } } /* Set info */ const setInfo = (cash, i) => { if(typePaymentInPage){ allBilling[i].textContent = 'factura de $' + (cash * annual).toLocaleString('en-US') + '/año'; } } setDefaultValue();