﻿var GroupTravel = function ($) {

    var validation = {

        /**
         *
         */
        NotSelectedWithEmpty: function (elm) {
            return (elm.selectedIndex < 1);
        },

        /**
         *
         */
        NotFilled: function (elm) {
            return !validation.isFilled(elm);
        },

        /**
         *
         */
        isFilled: function (elm) {
            return (elm.value != null && elm.value.length > 0);
        },

        /**
         *
         */
        BadEmail: function (field) {
            var str = field.value;
            var re = /^.+@.+\..+$/i;
            return !str.match(re);
        },

        /**
         *
         */
        BadPhone: function (invoer) {
            var str = validation.trim(invoer.value);
            var re = /^[0-9.-]{15}|[0-9.-]{14}|[0-9.-]{13}|[0-9.-]{12}|[0-9.-]{11}|[0-9.-]{10}|[0-9.-]{9}|[0-9]{8}$/;
            return (str.search(re) == -1);
        },

        /**
         *
         */
        BadZipCode: function (invoer) {
            var str = validation.trim(invoer.value).toUpperCase();
            document.getElementById("_postcode").value = str;
            var returnwaarde=true;
            var re = /\d{4}\w{2}/i;
            if (str.match(re)) { 
                if((str.substring(4,5)>='a' && str.substring(4,5)<='z' || str.substring(4,5)>='A' && str.substring(4,5)<='Z') &&
           (str.substring(5,6)>='a' && str.substring(5,6)<='z' || str.substring(5,6)>='A' && str.substring(5,6)<='Z')) { 
                    returnwaarde=false;
                }
            } 
            return returnwaarde;
        },

        /**
         *
         */
        trim: function (str) {
            return str.split(" ").join("");
        }
    };
    
    var priv = {
    
        /**
         *
         * @private
         */
        Check: function (field){
            var valid = true;
            switch (field.name){
                case "achternaam":
                    var filter = /[a-z A-Z]+$/;
                    document.getElementById("emptyAchternaam").style.display = "none";
                    document.getElementById("noRegularExpressionAchternaam").style.display = "none";
                    document.getElementById("li-error-emptyAchternaam").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.NotFilled(field)) {
                        document.getElementById("li-error-emptyAchternaam").style.display = "block";
                        document.getElementById("emptyAchternaam").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }else if (!filter.test(field.value)) {
                        document.getElementById("li-error-emptyAchternaam").style.display = "block";
                        document.getElementById("noRegularExpressionAchternaam").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }
                    break;
                case "straatnaam":
                    document.getElementById("emptyStraatnaam").style.display = "none";
                    document.getElementById("li-error-emptyStraatnaam").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.NotFilled(field)) {
                        document.getElementById("li-error-emptyStraatnaam").style.display = "block";
                        document.getElementById("emptyStraatnaam").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }	
                    break;	
                case "huisnummer":
                    document.getElementById("emptyHuisnummer").style.display = "none";
                    document.getElementById("li-error-huisnummer").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.NotFilled(field)) {
                        document.getElementById("li-error-huisnummer").style.display = "block";
                        document.getElementById("emptyHuisnummer").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }else if (isNaN(parseInt(field.value))){
                        document.getElementById("li-error-huisnummer").style.display = "block";
                        document.getElementById("naNHuisnummer").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }
                    break;
                case "postcode":
                    document.getElementById("emptyPostcode").style.display = "none";
                    document.getElementById("li-error-emptyPostcode").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.NotFilled(field)) {
                        document.getElementById("li-error-emptyPostcode").style.display = "block";
                        document.getElementById("emptyPostcode").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }else if (document.formulier.land.value == "NL") {
                        if (validation.BadZipCode(field)) {
                            document.getElementById("li-error-emptyPostcode").style.display = "block";
                            document.getElementById("emptyPostcode").style.display = "block";
                            field.style.borderColor = "#f60";
                            field.focus();
                            valid = false;
                        }
                    }
                    break;
                case "woonplaats":
                    document.getElementById("emptyWoonplaats").style.display = "none";
                    document.getElementById("li-error-emptyWoonplaats").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.NotFilled(field)) {
                        document.getElementById("li-error-emptyWoonplaats").style.display = "block";
                        document.getElementById("emptyWoonplaats").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }
                    break;
                case "telefoonPrive":
                    document.getElementById("emptyTelPrive").style.display = "none";
                    document.getElementById("noRegularExpressionTelPrive").style.display = "none";
                    document.getElementById("li-error-telPrive").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.NotFilled(field)) {
                        document.getElementById("li-error-telPrive").style.display = "block";
                        document.getElementById("emptyTelPrive").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;			
                    }else if (validation.BadPhone(field)) {
                        document.getElementById("li-error-telPrive").style.display = "block";
                        document.getElementById("noRegularExpressionTelPrive").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }
                    break;
                case "telMobiel":
                    document.getElementById("noRegularExpressionTelMobiel").style.display = "none";
                    document.getElementById("li-error-telMobiel").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.isFilled(field)) {
                        if (validation.BadPhone(field)) {
                            document.getElementById("li-error-telMobiel").style.display = "block";
                            document.getElementById("noRegularExpressionTelMobiel").style.display = "block";
                            field.style.borderColor = "#f60";
                            field.focus();
                            valid = false;
                        }
                    }
                    break;
                case "emailAdres":
                    document.getElementById("emptyEmail").style.display = "none";
                    document.getElementById("li-error-email").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.NotFilled(field)) {
                        document.getElementById("li-error-email").style.display = "block";
                        document.getElementById("emptyEmail").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }else if (validation.BadEmail(field)) {
                        document.getElementById("li-error-email").style.display = "block";
                        document.getElementById("noRegularExpressionEmail").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }
                    break;
                case "aantal_deelnemers":
                    document.getElementById("invalidCharParti").style.display = "none";
                    document.getElementById("li-error-invalidCharParti").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.NotFilled(field)) {
                        document.getElementById("li-error-invalidCharParti").style.display = "block";
                        document.getElementById("invalidCharParti").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }else if (isNaN(parseInt(field.value))){
                        document.getElementById("li-error-invalidCharParti").style.display = "block";
                        document.getElementById("invalidCharParti").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }
                    break;
                case "aantal_volwassenen":
                    document.getElementById("empty_adult").style.display = "none";
                    document.getElementById("li-error-empty_adult").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.NotFilled(field)) {
                        document.getElementById("li-error-empty_adult").style.display = "block";
                        document.getElementById("empty_adult").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }else if (isNaN(parseInt(field.value))){
                        document.getElementById("li-error-empty_adult").style.display = "block";
                        document.getElementById("empty_adult").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }
                    break;
                case "voorkeur1":
                    document.getElementById("emptypref1").style.display = "none";
                    document.getElementById("li-error-emptypref1").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.NotFilled(field)) {
                        document.getElementById("li-error-emptypref1").style.display = "block";
                        document.getElementById("emptypref1").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }
                    break;
                case "verblijf":
                    document.getElementById("empty_acco").style.display = "none";
                    document.getElementById("li-error-empty_acco").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.NotSelectedWithEmpty(field)) {
                        document.getElementById("li-error-empty_acco").style.display = "block";
                        document.getElementById("empty_acco").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }
                    break;
                case "meal":
                    document.getElementById("empty_meal").style.display = "none";
                    document.getElementById("li-error-empty_meal").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.NotSelectedWithEmpty(field)) {
                        document.getElementById("li-error-empty_meal").style.display = "block";
                        document.getElementById("empty_meal").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }
                    break;
                case "klasse":
                    document.getElementById("empty_style").style.display = "none";
                    document.getElementById("li-error-empty_style").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.NotSelectedWithEmpty(field)) {
                        document.getElementById("li-error-empty_style").style.display = "block";
                        document.getElementById("empty_style").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }
                    break;		
                case "vervoer":
                    document.getElementById("empty_vervoer").style.display = "none";
                    document.getElementById("li-error-empty_vervoer").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.NotSelectedWithEmpty(field)) {
                        document.getElementById("li-error-empty_vervoer").style.display = "block";
                        document.getElementById("empty_vervoer").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }
                    break;
                case "aantal_nachten":
                    document.getElementById("empty_nachten").style.display = "none";
                    document.getElementById("li-error-empty_nachten").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.NotSelectedWithEmpty(field)) {
                        document.getElementById("li-error-empty_nachten").style.display = "block";
                        document.getElementById("empty_nachten").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }
                    break;
                case "depdatum_dag":
                    document.getElementById("WrongDate").style.display = "none";
                    document.getElementById("li-error-WrongDate").style.display = "none";
                    field.style.borderColor = "";
                    document.formulier.depdatum_maand.style.borderColor = "";
                    document.formulier.depdatum_jaar.style.borderColor = "";
                    if (validation.NotSelectedWithEmpty(field)){
                        document.getElementById("li-error-WrongDate").style.display = "block";
                        document.getElementById("WrongDate").style.display = "block";
                        field.style.borderColor = "#f60";
                        document.formulier.depdatum_dag.focus();
                        valid = false;				
                    } else if (validation.NotSelectedWithEmpty(document.formulier.depdatum_maand)){
                        document.getElementById("li-error-WrongDate").style.display = "block";
                        document.getElementById("WrongDate").style.display = "block";
                        document.formulier.depdatum_maand.style.borderColor = "#f60";
                        document.formulier.depdatum_maand.focus();
                        valid = false;
                    } else if (validation.NotSelectedWithEmpty(document.formulier.depdatum_jaar)){
                        document.getElementById("li-error-WrongDate").style.display = "block";
                        document.getElementById("WrongDate").style.display = "block";
                        document.formulier.depdatum_jaar.style.borderColor = "#f60";
                        document.formulier.depdatum_jaar.focus();
                        valid = false;
                    } else {
                        document.formulier.vertrekdatum.value=document.forms[0].depdatum_dag.options[document.forms[0].depdatum_dag.selectedIndex].value + "-" + document.forms[0].depdatum_maand.options[document.forms[0].depdatum_maand.selectedIndex].value + "-" + document.forms[0].depdatum_jaar.options[document.forms[0].depdatum_jaar.selectedIndex].text;
                    }
                    break;
                case "budget":
                    document.getElementById("emptyBudget").style.display = "none";
                    document.getElementById("li-error-budget").style.display = "none";
                    field.style.borderColor = "";
                    if (validation.NotFilled(field)) {
                        document.getElementById("li-error-budget").style.display = "block";
                        document.getElementById("emptyBudget").style.display = "block";
                        field.style.borderColor = "#f60";
                        field.focus();
                        valid = false;
                    }
                    break;
                default:
                    valid = true;
                    break;
            }	
            return valid;
        },

        /**
         *
         * @private
         */
        checkForm: function () {
            var isFullValid = true;
            var elements = document.formulier.elements.length;	
            var idx = 0;
            
            while (idx < elements) {
                if (!priv.Check(document.formulier.elements[idx])) {
                    isFullValid = false;
                }
                idx++;
            }
            return isFullValid;
        },
        
        /**
         * Get a string combining the values of several form fields, to fill the 'budget_inclusief' hidden input field
         * @param {String} form The form
         * @return {String} The values of the fields, combined into a string.
         * @private
         */
        getBudgetInclusive: function (form) {
            var str = "";
            
            // Get the values of the checkboxes by reading out their labels. Only checkboxes 3 - 7 are interesting
            for (var i = 3; i <= 7; i++) {
                if (form["chkbox" + i].checked == true) {
                    str = str + $(form["chkbox" + i]).next("label").text() + ", ";
                }
            }

            // Add value of 'overig' text field
            if (validation.isFilled(form.overig)){
                str = str + form.overig.value;
            }
            
            return str;
        },
        
        /**
         * Get the full name of the person who filled out the form.
         * The full name combines the values of the title, first letter, middle name, and lastname fields
         * for as far as they have been filled out.
         * @param {String} form The form containing the fields to process
         * @return {String} The full name
         * @private
         */
        getFromName: function (form) {
            var name = "";

            if ( validation.NotSelectedWithEmpty(form.aanhef) ){
                name = form.aanhef.value;
            }
            if ( validation.isFilled(form.voorletters) ){
                name += " " + form.voorletters.value;
            }
            if ( validation.isFilled(form.tussenvoegsel) ){
                name += " " + form.tussenvoegsel.value;
            }
            
            return name + " " + form.achternaam.value;
        }
    };

    return {
    
        /**
         * Check the supplied input field for validity.
         * @param {HTMLElement} input The input field
         * @return {Boolean} True if the field is valid
         */
        Check: function (input) {
            return priv.Check(input);
        },

        /**
         * Submit the form if it is valid. Fill any hidden input fields that need to be filled
         */
        submitForm: function () {
            // Only process a valid form
            if (!priv.checkForm()) {
                return false;
            }
            
            // Fill hidden inputs 'budget_inclusief' and 'fromName'
            document.formulier.budget_inclusief.value = priv.getBudgetInclusive(document.formulier);
            document.formulier.fromName.value = priv.getFromName(document.formulier);

            // submit
            document.formulier.submit();	
        }
    };
}(jQuery);
