// EBD JS Functions
// Copyright (c) 2006 bytepark GmbH (http://www.bytepark.de)


function checkQuality(field)
{
    var output = document.getElementById('pwQuality');
    var pass = field.value;
    var score = 0;
    var punctuations = '!@#$%^&*()~`_-+={}[];:"<>,./?\\\''.split('');
    var uppercases = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
    var lowercases = 'abcdefghijklmnopqrstuvwxyz'.split('');
    var hasPunctuation = false;
    var hasUppercase = false;
    var hasLowercase = false;
    var hasNumber = false;
    var membership = [];
    for (c in punctuations)
    {
        if (pass.indexOf(punctuations[c]) != -1)
        {
            score++;
            hasPunctuation = true;
            membership[pass.indexOf(punctuations[c])] = 'P';
        }
    }
    for (c in uppercases)
    {
        if (pass.indexOf(uppercases[c]) != -1)
        {
            score += 0.5;
            hasUppercase = true;
            membership[pass.indexOf(uppercases[c])] = 'U';
        }
    }
    for (c in lowercases)
    {
        if (pass.indexOf(lowercases[c]) != -1)
        {
            score += 0.5;
            hasLowercase = true;
            membership[pass.indexOf(lowercases[c])] = 'L';
        }
    }
    for (c=0;c<=9;c++)
    {
        if (pass.indexOf(c) != -1)
        {
            score += 0.5;
            hasNumber = true;
            membership[pass.indexOf(c)] = 'N';
        }
    }
    // calculate the "noisyness" of the password
    // each transition from one group of symbols to the next adds .25 to the score
    // NoI$yNeSs of that string is 4.25
    // noisyness of that string is .25
    var lastType = '';
    for (i = 0; i<pass.length; i++)
    {
        if (lastType != membership[i])
        {
            score += 0.25;
            lastType = membership[i];
        }
    }
    if (hasPunctuation) score++;
    if (hasNumber) score++;
    if (hasUppercase) score++;
    if (hasLowercase) score++;

    var q = '';
    if (score >10) score=10;
    if (score <= 10) q = '#00FF00';
    if (score < 8) q = '#80FF80';
    if (score < 7) q = '#FFFF00';
    if (score < 6) q = '#FFFF80';
    if (score < 5) q = '#FF8080';
    if (score < 4) q = '#FF0000';
    output.style.width = (score*25) + 'px';
    output.style.backgroundColor = q;
    output.innerHTML = score;
}