﻿// Shared Javascript functions
// checkValidation()  -> validates email entered

    function checkValidation ( addressField, language ) {
        if ( stringEmpty ( addressField.value ) )
            if (language == 'EN'){
            alert ( "Please enter email address" );
            }else{
            alert ("Vul je email adres in");
            }
        else if ( noAtSign ( addressField.value ) )
            if (language == 'EN'){
            alert ( "Please enter valid email address" );
            }else{
            alert("Vul een juist email adres in");
            }
        else if ( nothingBeforeAt ( addressField.value ) )
            if (language == 'EN'){
            alert ( "Please enter valid email address" );
            }else{
            alert("Vul een juist email adres in");
            }
        else if ( noLeftBracket ( addressField.value ) )
            if (language == 'EN'){
            alert ( "Please enter valid email address" );
            }else{
            alert("Vul een juist email adres in");
            }
        else if ( noRightBracket ( addressField.value ) )
            if (language == 'EN'){
            alert ( "Please enter valid email address" );
            }else{
            alert("Vul een juist email adres in");
            }
        else if ( noValidPeriod ( addressField.value ) )
            if (language == 'EN'){
            alert ( "Please enter valid email address" );
            }else{
            alert("Vul een juist email adres in");
            }
        else if ( noValidSuffix ( addressField.value ) )
            if (language == 'EN'){
            alert ( "Please enter valid email address" );
            }else{
            alert("Vul een juist email adres in");
            }
        else
            return (true);

        return ( false );
    }

    function stringEmpty ( address ) {
        // CHECK THAT THE STRING IS NOT EMPTY
        if ( address.length < 1 ) {
            return ( true );
        } else {
            return ( false );
        }
    }

    function noAtSign ( address ) {
        // CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING
        if ( address.indexOf ( '@', 0 ) == -1 ) {
            return ( true )
        } else {
            return ( false );
        }
    }

    function nothingBeforeAt ( address ) {
        // CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER
        if ( address.indexOf ( '@', 0 ) < 1 ) {
            return ( true )
        } else {
            return ( false );
        }
    }

    function noLeftBracket ( address ) {
        // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET
        if ( address.indexOf ( '[', 0 ) == -1 && address.charAt ( address.length - 1 ) == ']' ) {
            return ( true )
        } else {
            return ( false );
        }
    }

    function noRightBracket ( address ) {
        // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET
        if ( address.indexOf ( '[', 0 ) > -1 && address.charAt ( address.length - 1 ) != ']' ) {
            return ( true );
        } else {
            return ( false );
        }
    }

    function noValidPeriod ( address ) {
        // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
        if ( address.indexOf ( '@', 0 ) > 1 && address.charAt ( address.length - 1 ) == ']' )
            return ( false );

        // CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING
        if ( address.indexOf ( '.', 0 ) == -1 )
            return ( true );

        return ( false );
    }

    function noValidSuffix ( address ) {
        // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
        if ( address.indexOf ( '@', 0 ) > 1 && address.charAt ( address.length - 1 ) == ']' )
            return ( false );

        // CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD
        var len = address.length;
        var pos = address.lastIndexOf ( '.', len - 1 ) + 1;
        if ( ( len - pos ) < 2 || ( len - pos ) > 3 ) {
            return ( true );
        } else {
            return ( false );
        }
    }
    
    function validateFName(fName,language){
        if (stringEmpty(fName.value)) {
            if (language == 'EN'){
                alert('Please enter First name');
            }else{
                alert('Vul je voornaam in');
            }
        }
        else
            return (true);

        return ( false );
    }
    
        function validateLName(lName,language){
        if (stringEmpty(lName.value)) {
            if (language == 'EN'){
                alert('Please enter Last name');
            }else{
                alert('Vul je achternaam in');
            }
        }
        else
            return (true);

        return ( false );
    }

        function validateAddress(address,language){
        if (stringEmpty(address.value)) {
            if (language == 'EN'){
                alert('Please enter Address');
            }else{
                alert('Vul je adres in');
            }
        }
        else
            return (true);

        return ( false );
    }
    
        function validatePostCode(postcode,language){
        if (stringEmpty(postcode.value)) {
            if (language == 'EN') {
                alert('Please enter Post code');
            }else{
                alert('Vul je postcode in');
            }
        }
        else
            return (true);

        return ( false );
    }    
    
        function validateCity(city,language){
        if (stringEmpty(city.value)) {
            if (language == 'EN') {
                alert('Please enter City');
            }else{
                alert('Vul je woonplaats in');
            }
        }
        else
            return (true);

        return ( false );
    }

        function validateCountry(country,language){
        if (stringEmpty(country.value)) {
            if (language =='EN'){
                alert('Please select Country');
            }else{
                alert('Kies het juiste land')
            }
        }
        else
            return (true);

        return ( false );
    }
    
        function validatePhone(phone,language){
        if (stringEmpty(phone.value)) {
            alert('Please enter Telephone number');
        }
        else
            return (true);

        return ( false );
    }
