/* Random Password Generator
 * by Ryan Kaskel 
 * License: Public Domain
*/

/* Settings */

var MIN_PW_LEN = 6;

/* Potential password characters */
var uppercase = new String("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
var lowercase = new String("abcdefghijklmnopqrstuvwxyz");
var numbers = new String("0123456789");
var punctuation = new String("~!#$%^&*()_+-=");

var set_password = function(pw_field, msg)
{
    pw_field.value = msg;
}

var display_error = function(pw_field, msg)
{
    set_password(pw_field, "error");
    alert(msg);
}

/* Generate a random number between two integers
    http://roshanbh.com.np/2008/09/get-random-number-range-two-numbers-javascript.html */
var rand_int_from_range = function(min, max, float_val)
{
    var rand_val = min + (Math.random() * (max - min));
    return typeof floatVal=='undefined'?Math.round(rand_val): rand_val.toFixed(float_val);
}

/*  does the actual password generation one character at a time */
var do_generate = function(chars, num_chars)
{
    var new_password = "";
    for (var i=0; i < num_chars; i++)
    {
        new_password = new_password.concat(
                            chars[rand_int_from_range(0,chars.length)]);
    }
    return new_password;
}
    

/* generate_password takes a form as an argument and expects that it will receive a checkbox and 
   a number which represents the length of the generated password. 

   The form should have three elements:
    1) Checkbox options called p_opts with at least one checked
    2) Text input called num_chars greater than MIN_PW_LEN
    3) Text input called random_password; passed to various functions to set the input text to the new password
    
    This function only validates the fields and then hands off the character set from which to randomly choose,
    the number of characters to generate, and the random_password text input object.
*/
var generate_password = function(in_form)
{  
    /* Check that all the necessary fields are filled */
    var char_option_checked = false;
    var num_chars = 0;
    var char_set = "";
    
    if (!"p_opts" in in_form || !"num_chars" in in_form || !"random_password" in in_form)
    {
        display_error(in_form.random_password, "Required field missing from form.")
        return false;
    }
    
    /* Check that the num_chars option is at least the size of MIN_PW_LEN */
    num_chars = parseInt(in_form.num_chars.value)
    if (!num_chars || num_chars < MIN_PW_LEN)
    {
        display_error(in_form.random_password, "The number of characters must be at least " + MIN_PW_LEN);
        return false;
    }
       
    /* Check to see that at least one password character set is selected
                and those chracter sets that are chosen are added to the master character set */
    var i = 0;
    while (i < in_form.p_opts.length)
    {
        if (in_form.p_opts[i].checked == true)
        {
            char_option_checked = true;
            if (in_form.p_opts[i].value == "uppercase")
                char_set = char_set.concat(uppercase);
            else if (in_form.p_opts[i].value == "lowercase")
                char_set = char_set.concat(lowercase);
            else if (in_form.p_opts[i].value == "numbers")
                char_set = char_set.concat(numbers);
            else
                char_set = char_set.concat(punctuation);
        }
        ++i;
    }
    if (!char_option_checked)
    {
        display_error(in_form.random_password, "You must select at least one set of characters.");
        return false;
    }
    
    /* Form is validated, now really generate the password and set the
               form input text random_password to the generated password */
    set_password(in_form.random_password,
            do_generate(char_set, num_chars));
    return true;
}



