﻿function validateLocationForm() {
    this.messages = [];
    if (document.getElementById('pc').value == '') {
        this.messages.push('Le code postal est un champ requis.');
    }

    if (!(isPostCode(document.getElementById('pc').value))) {
        this.messages.push('Le code postal entré est invalide');
    }

    if (this.messages.length > 0) {
        var alerts = 'S\'il vous plaît réexaminer les champs qui suivre:\n\n';
        this.messages.each(function(item, index) {
            alerts += item + '\n';
        });
        alert(alerts);
        return false;
    }
    return true;
}

function isPostCode(entry) {
    strlen = entry.length;// if (strlen !== 6) { return false; }
    entry = entry.toUpperCase();
    if (' '.indexOf(entry.charAt(3)) < 0) {
        if ('ABCEGHJKLMNPRSTVXY'.indexOf(entry.charAt(0)) < 0) { return false; }
        if ('0123456789'.indexOf(entry.charAt(1)) < 0) { return false; }
        if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(2)) < 0) { return false; }
        if ('0123456789'.indexOf(entry.charAt(3)) < 0) { return false; }
        if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(4)) < 0) { return false; }
        if ('0123456789'.indexOf(entry.charAt(5)) < 0) { return false; }
        return true;
    }
    else {
        if ('ABCEGHJKLMNPRSTVXY'.indexOf(entry.charAt(0)) < 0) { return false; }
        if ('0123456789'.indexOf(entry.charAt(1)) < 0) { return false; }
        if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(2)) < 0) { return false; }
        if (' '.indexOf(entry.charAt(3)) < 0) { return false; }
        if ('0123456789'.indexOf(entry.charAt(4)) < 0) { return false; }
        if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(5)) < 0) { return false; }
        if ('0123456789'.indexOf(entry.charAt(6)) < 0) { return false; }
        return true;
    }
}

