﻿function testPassword(element) {
    var password = element.text;
    var inputField = $("#password");
    inputField.val(password);

    checkPassword(password);
}

function evaluateStatus(parentselector, selector, status) {
    var row = $(selector);
    var element = $(selector + " .status");

    // remove old class
    element.removeClass("failed");
    element.removeClass("passed");
    element.removeClass("exceeded");

    var p = new PasswordMeter();
    if (status == p.STATUS.FAILED) {
        element.addClass("failed");
    }
    else if (status == p.STATUS.PASSED) {
        element.addClass("passed");
    }
    else if (status == p.STATUS.EXCEEDED) {
        element.addClass("exceeded");
    }

    // get old status and save new, highlight when changed
    var old = element.data("status");
    if (old != status) {
        row.effect("highlight", { color: "#ffff00" }, 800).addClass("lastChanged");
        element.data("status", status);
    }
}

function isBonusOrPenalty(element, rating, pm) {
    // remove old class

    if (rating < 0) {
        // set classes
        // TODO
        return element.html(pm.float2str(rating));
    }
    else if (rating > 0) {
        // set classes
        // TODO
        return element.html("+" + pm.float2str(rating));
    }
    else {
        // set classes
        // TODO
        return element.html("0");
    }
}

function assignData(selector, data, pm) {
    evaluateStatus("#passwordCriterias table", selector, data.status);
    $(selector + " .count").html(pm.int2str(data.count));
    isBonusOrPenalty($(selector + " .rating"), data.rating, pm);

    $(selector + " .minimum").html(pm.int2str(data.minimum));
    $(selector + " .bonus").html(pm.int2str(data.bonus));
    $(selector + " .penalty").html(pm.int2str(data.penalty));
}

/**
* Reset the state of the display area
*/
function cleanState() {
    // clean the last state marker
    $("#passwordCriterias table tr").removeClass("lastChanged");
}

function checkPassword() {
    // get the input field
    var passwordField = $("#password");

    // get the value, basically the password
    var password = passwordField.val();

    var oldStatus = passwordField.data("prePassword");
    if (oldStatus == password) {
        // no change, leave and do nothing
        return;
    }
    passwordField.data("prePassword", password);

    var pm = new PasswordMeter();
    pm.checkPassword(password);

    var strength = document.getElementById("strength"); //.setAttribute(width, pm.int2str(pm.Score.adjusted));
    strength.style.width = (pm.int2str(pm.Score.adjusted) + "%");
    //$("#bar")(pm.int2str(pm.Score.adjusted));
    $("#score").html(pm.int2str(pm.Score.adjusted) + "%");
    $("#originalscore").html(pm.float2str(pm.Score.count));
    $("#beforeRedundancyScore").html(pm.float2str(pm.Score.beforeRedundancy));
    $("#redundancy").html(pm.float2str(pm.Redundancy.value));

    // clean the last state marker
    cleanState();

    // assign PasswordLength information
    assignData("#PasswordLength", pm.PasswordLength, pm);

    // assign nBasicRequirementsMet information
    assignData("#BasicRequirements", pm.BasicRequirements, pm);

    // assign RecommendedLength information
    assignData("#RecommendedPasswordLength", pm.RecommendedPasswordLength, pm);

    // assign LowercaseLetters information
    assignData("#LowercaseLetters", pm.LowercaseLetters, pm);

    // assign UppercaseLetters information
    assignData("#UppercaseLetters", pm.UppercaseLetters, pm);

    // assign Numerics information
    assignData("#Numerics", pm.Numerics, pm);

    // assign Symbols information
    assignData("#Symbols", pm.Symbols, pm);

    // assign MiddleNumerics information
    assignData("#MiddleNumerics", pm.MiddleNumerics, pm);

    // assign MiddleSymbols information
    assignData("#MiddleSymbols", pm.MiddleSymbols, pm);

    // assign SequentialLetters information
    assignData("#SequentialLetters", pm.SequentialLetters, pm);

    // assign SequentialNumerics information
    assignData("#SequentialNumerics", pm.SequentialNumerics, pm);

    // assign KeyboardPatterns information
    assignData("#KeyboardPatterns", pm.KeyboardPatterns, pm);

    // assign RepeatedSequences information
    assignData("#RepeatedSequences", pm.RepeatedSequences, pm);

    // assign MirroredSequences information
    assignData("#MirroredSequences", pm.MirroredSequences, pm);

    // set complexity
    var complexity = pm.Complexity.value;
    var indicator = $("#complexity");

    if (complexity == pm.COMPLEXITY.VERYWEAK) {
        indicator.html("Очень легкий");
    }
    else if (complexity == pm.COMPLEXITY.WEAK) {
        indicator.html("Легкий");
    }
    else if (complexity == pm.COMPLEXITY.GOOD) {
        indicator.html("Нормальный");
    }
    else if (complexity == pm.COMPLEXITY.STRONG) {
        indicator.html("Сложный");
    }
    else if (complexity == pm.COMPLEXITY.VERYSTRONG) {
        indicator.html("Очень сложный");
    }
}

function togglePasswordMask(command) {
    var showPassword = $("#showPassword");
    var hidePassword = $("#hidePassword");

    var myPasswords = $("#myPasswords");

    if (command == "Show") {
        showPassword.hide();
        hidePassword.show();
        myPasswords.slideDown(1000);
    }
    else {
        showPassword.show();
        hidePassword.hide();
        myPasswords.slideUp(1000);
    }

    // save value
    var password = $("#password");
    var passwordHidden = $("#passwordHidden");

    passwordHidden.val(password.val());

    password.attr("id", "passwordTemp");
    passwordHidden.attr("id", "password");
    password.attr("id", "passwordHidden");
}

function storePassword() {
    // get my passwords area
    var myPasswords = $("#myPasswords");
    var myPasswordsArea = $("#myPasswords p.data");

    var password = $("#password");
    var currentPassword = password.val();

    // create element and add
    myPasswordsArea.append("<a href=\"#\" onclick=\"testPassword(this); return false;\">" + currentPassword + "<\/a> ");

    // display if hidden
    myPasswords.slideDown(1000);
}

$(document).ready(
		function() {
		    var password = $("#password").val();

		    // this trick cleans the display area on reload
		    checkPassword(password);
		    cleanState();
		    checkPassword(password);

		    // insert the tool tips
		    $("div.hover").cluetip(
				{
				    fx: { open: "fadeIn" },
				    local: true,
				    hideLocal: true,
				    attribute: 'rel',    // the attribute to be used for fetching the clueTip's body content
				    titleAttribute: 'title',  // the attribute to be used for fetching the clueTip's title
				    positionBy: 'auto' // next to the mouse
				    // splitTitle: '|' // use the invoking element's title attribute to populate the clueTip...
				    // ...and split the contents into separate divs where there is a "|"
				});
		}
	); 
