var langue;

function ValidateCP(source, value) {
    // get control that called this
    var ControlName = source.controltovalidate;
    // get custom validator name
    //var ControlValidatorName = source.id;

    var control = document.getElementById("postalcodesHiddenField");
    var validInput = control.value;

    //var controlToValidate = document.getElementById("WhereTextBox");

    var whereTextBox;
    var whatTextBox;
    var whoTextBox;

    var phoneTextBox;

    var searchControl;

    if (ControlName == 'WhereTextBox') {
        whereTextBox = document.getElementById('WhereTextBox');
        whatTextBox = document.getElementById('WhatTextBox');
        whoTextBox = document.getElementById('WhoTextBox');

        searchControl = document.getElementById("searchLinkButton");
    }
    else {
        whereTextBox = document.getElementById('advancedWhereTextBox');
        whatTextBox = document.getElementById('advancedWhatTextBox');
        whoTextBox = document.getElementById('advancedWhoTextBox');

        phoneTextBox = document.getElementById('phoneTextBox');

        searchControl = document.getElementById("advancedsearchLinkButton");
    }

    /*alert(whereTextBox.value);
    alert(whatTextBox.value);
    alert(whoTextBox.value);*/

    var reg = new RegExp('[ ]');
    var array = whereTextBox.value.split(reg);
    var where = array[0];

    var errorMessage;

    //alert(searchControl.innerText);

    if (searchControl.innerText == 'Rechercher') {
        langue = 'FR';
        errorMessageWrongCP = 'Veuillez choisir un code postal dans la liste deroulante !';
        errorMessageNoCP = 'Veuillez introduire un code postal !';
    }
    else {
        langue = 'NL';
        errorMessageWrongCP = 'Gelieve een postcode uit de lijst te kiezen !';
        errorMessageNoCP = 'Gelieve een postcode in te geven !';
    }

    //alert(controlToValidate.value);
    //alert(result);

    if (whereTextBox.value.length == 0 && whatTextBox.value.length == 0 && whoTextBox.value.length == 0) {
        value.IsValid = true;
        return true;
    }

    if (whereTextBox.value.length == 0 && whatTextBox.value.length > 0) {
        if (phoneTextBox != null) {
            if (phoneTextBox.value.length > 0) {
                value.IsValid = true;
                return true;
            }
            else {
                alert(errorMessageNoCP);
                value.IsValid = false;
                return false;
            }
        }
        else {
            alert(errorMessageNoCP);
            value.IsValid = false;
            return false;
        }
    }

    if (whereTextBox.value.length == 0 && whoTextBox.value.length > 0) {
        if (phoneTextBox != null) {
            if (phoneTextBox.value.length > 0) {
                value.IsValid = true;
                return true;
            }
            else {
                alert(errorMessageNoCP);
                value.IsValid = false;
                return false;
            }
        }
        else {
            alert(errorMessageNoCP);
            value.IsValid = false;
            return false;
        }
    }
    
    var result = validInput.search(where.toUpperCase());

    if (result > 0) {
        var count = where.length;
        if (count < 3) {
            alert(errorMessageWrongCP);
            value.IsValid = false;
            return false;
        }
        else {
            value.IsValid = true;
            return true;
        }
    }

    if (result < 0) {
        if (phoneTextBox != null && phoneTextBox.value.length > 0) {
            value.IsValid = true;
            return true;
        }
        else {
            alert(errorMessageWrongCP);
            value.IsValid = false;
            return false;
        }
    }
}

function ValidateMyCP(source, value) {

    var ControlName = source.controltovalidate;
    var cp = document.getElementById(ControlName).value;

    var control = document.getElementById("postalcodesHiddenField");
    var validInput = control.value;
    
    //Pour la validation JSON
    //var validInput = eval(control.value);

    var phoneTextBox;

    var searchControl;

    if (ControlName == 'WhereTextBox') {
        searchControl = document.getElementById("searchLinkButton");
    }
    else {
        phoneTextBox = document.getElementById('phoneTextBox');

        searchControl = document.getElementById("advancedsearchLinkButton");
    }

    var reg = new RegExp('[ ]');
    var array = cp.split(reg);
    var where = array[0];

    var errorMessageWrongCP;
    var errorMessageNoCP;

    //alert(searchControl.innerText);

    if (searchControl.innerText == 'Rechercher') {
        errorMessageWrongCP = 'Veuillez choisir un code postal dans la liste deroulante !';
        errorMessageNoCP = 'Veuillez introduire un code postal !';
    }
    else {
        errorMessageWrongCP = 'Gelieve een postcode uit de lijst te kiezen !';
        errorMessageNoCP = 'Gelieve een postcode in te geven !';
    }

    var result = validInput.search(where.toUpperCase());

    if (result > 0) {
        var count = where.length;
        if (count < 3) {
            alert(errorMessageWrongCP);
            value.IsValid = false;
            return false;
        }
        else {
            value.IsValid = true;
            return true;
        }
    }

    if (result <= 0) {
        if (phoneTextBox != null && phoneTextBox.value.length > 0) {
            value.IsValid = true;
            return true;
        }
        else {
            alert(errorMessageWrongCP);
            value.IsValid = false;
            return false;
        }
    }

    /*
    //Exemple de validation via JSON
    
    var i;

    var found = false;

    if (where.length > 0) {
    for (i = 0; i < validInput.length; i++) {
    if (validInput[i].CP == where.toUpperCase() || validInput[i].VILLEFR == where.toUpperCase() || validInput[i].VILLENL == where.toUpperCase()) {
    found = true;
    }
    }
    }

    if (found == true) {
    value.IsValid = true;
    return true;
    }
    else {
    alert(errorMessageWrongCP);
    value.IsValid = false;
    return false;
    }*/
    
}
