/**
 * The windows onload event. Handles setting event
 * listeners for the password input field.
*/

function windowLoad() {
    var pass = document.getElementById("password");
    pass.addEventListener("keypress", checkPassword, false);
    pass.focus();
}

/**
 * Handles checking the password.
*/

function checkPassword(e) {
	
    // The password value
    var password = document.getElementById("password").value;
    
    // No point carrying on if password is less than 6 characters, the minimum weak length
    if(password.length < 6)
    {
        // Return status to default
        var d = document.getElementById("result");
        d.innerHTML = "Weak";
        d.className = "weak";
        return false;
    }
    
    // The paramaters to send.
    var id = Math.floor(Math.random() * 1000);
    var parameters = "password=" + password + "&id=" + id;

    // Prepare and send the request using our ajax request object
    xmlHttp.open("GET", "../lib/strength_check.php?" + parameters, true);
    xmlHttp.onreadystatechange = handleResponse;
    xmlHttp.send(parameters);
}

/**
 * Handles the response.
 */

function handleResponse() {

    if(xmlHttp.readyState == 4)
    {
        // Evaluate the response text so we can get it as an object
        var json = eval("(" + xmlHttp.responseText + ")");
		
        // Add the strength to the result division
        var d = document.getElementById("result");
        d.innerHTML = json.strength;
        d.className = json.className;
    }
}

window.addEventListener("load", windowLoad, false);
