Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
8.4 kB
4
Indexable
Never
window.onload = function(){
    if(document.body.contains(document.querySelector('.form-tool__range'))){
        rangeInput()
    }

    if(document.body.contains(document.querySelector('.form-tool__contact'))){
        passForm()
    }
}

function rangeInput() {
    document.querySelector('.form-tool__slider input[type="range"]').addEventListener('change', function() {
        document.querySelector('.form-tool__number input').value = this.value
    })
    
    document.querySelector('.form-tool__number input').addEventListener('change', function() {
        document.querySelector('.form-tool__slider input[type="range"]').value = this.value
    })
}

function passForm() {
    document.querySelector('.form-tool__contact .form-tool__submit').addEventListener('click', function() {
        let selectedValue = JSON.parse(document.querySelector('select[name="location"]').value)
        let country = selectedValue.country
        let continent = selectedValue.continent
        let specialPrice = selectedValue.price
        
        let rooms = document.querySelector('.form-tool__range .form-tool__number input').value
        let name = document.querySelector('.form-tool__contact input[name="name"]').value
        let email = document.querySelector('.form-tool__contact input[name="email"]').value
        let offer = ''
        let price

        if (document.querySelector('.form-tool__left input[name="offer"]').checked == true) {
            offer = document.querySelector('.form-tool__left input[name="offer"]:checked').value
        }

        if(offer != '' && country != '' && name != '' && email != '') {
            if(document.querySelector('.form-tool__contact input[name="getintouch"]').checked == false) {
                document.querySelector('.form-tool__contact').innerHTML += '<p>Please check at least one of the consent boxes!</p>'
            } else {
                if(document.querySelector('.form-tool__contact input[name="useemail"]').checked == true) {
                    document.querySelector('.form-tool input[name="form-offer-2"]').value = 'You have also consented to your email being used for marketing purposes such as newsletters.'
                }

                if(["Europe", "Asia", "Africa", "Middle East"].includes(continent)) {
                    if(rooms < 15) {
                        switch (offer) {
                            case "essential":
                                price = '1';
                                break;
                            case "connected":
                                price = '2';
                                break;
                            case "complete":
                                price = '3'
                                break;
                            default:
                                break;
                        }
                    } else if (rooms < 30) {
                        switch (offer) {
                            case "essential":
                                price = '4';
                                break;
                            case "connected":
                                price = '5';
                                break;
                            case "complete":
                                price = '6'
                                break;
                            default:
                                break;
                        }
                    } else if (rooms < 60) {
                        switch (offer) {
                            case "essential":
                                price = '7';
                                break;
                            case "connected":
                                price = '8';
                                break;
                            case "complete":
                                price = '9'
                                break;
                            default:
                                break;
                        }
                    } else {
                        switch (offer) {
                            case "essential":
                                price = '10';

                                if(specialPrice == 1) {
                                    price = '13'
                                }

                                break;
                            case "connected":
                                price = '11';

                                if(specialPrice == 1) {
                                    price = '14'
                                }
                                break;
                            case "complete":
                                price = '12'

                                if(specialPrice == 1) {
                                    price = '15'
                                }
                                break;
                            default:
                                break;
                        }
                    }
                } else {
                    if(rooms < 15) {
                        switch (offer) {
                            case "essential":
                                price = '16';
                                break;
                            case "connected":
                                price = '17';
                                break;
                            case "complete":
                                price = '18'
                                break;
                            default:
                                break;
                        }
                    } else if (rooms < 30) {
                        switch (offer) {
                            case "essential":
                                price = '19';
                                break;
                            case "connected":
                                price = '20';
                                break;
                            case "complete":
                                price = '21'
                                break;
                            default:
                                break;
                        }
                    } else if (rooms < 60) {
                        switch (offer) {
                            case "essential":
                                price = '22';
                                break;
                            case "connected":
                                price = '23';
                                break;
                            case "complete":
                                price = '24'
                                break;
                            default:
                                break;
                        }
                    } else {
                        switch (offer) {
                            case "essential":
                                price = '25';

                                if(specialPrice == 1) {
                                    price = '28'
                                }

                                break;
                            case "connected":
                                price = '26';

                                if(specialPrice == 1) {
                                    price = '29'
                                }
                                break;
                            case "complete":
                                price = '27'

                                if(specialPrice == 1) {
                                    price = '30'
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }

                document.querySelector('.form-tool input[name="form-name"]').value = name
                document.querySelector('.form-tool input[name="form-email"]').value = email
                document.querySelector('.form-tool input[name="form-offer"]').value = price
            }
        } else {
            document.querySelector('.form-tool__contact').innerHTML += '<p>Please fill all the fields!</p>'
        }
    })
}