﻿/*
    JavaScript File: javascript/IsPasswordOK.js
    Written by Bob Archer, 2008.
    
    These functions are used to validate a user's password, that is, are they entering a password that
    meets the minimum password requirements. This is used in real time, it is call on the KeyUp event
    as the user is entering their new password and when changing their password.
    
    There are also functions used in conjunction with choosing a username that indicate, in real time,
    if the string they have entered is available as a username.

*/

    
function ValidatePasswordTrigger(source)
{
    if ( source.value.length > 0 ) {
        setTimeout("ValidatePassword('" + source.id + "');", 1);
    } else {
        $get('IsPasswordOK').innerHTML = '<span style="color: red;">Password required.</span>';
    }
}

function ValidatePassword(id)
{
    var output;
    var re = new RegExp();
    //var yes = '<img src="../images/icons/add.gif" alt="OK" width="16" height="16" />';
    //var no = '<img src="../images/icons/cancel.gif" alt="Not OK" width="16" height="16" />';
    
    var password = document.getElementById(id).value;
    
    re = /[^0-9a-zA-Z\/\.\*\+\?\|\(\)\[\]\{\}\\~`!@#$%^&-_=:;"'<,>]/g;
    if ( re.test(password) )
    {
        password = password.replace(re, "");
        document.getElementById(id).value = password;
        alert("Character(s) found that are disallowed in the password. Disallowed characters include spaces, tabs and carriage returns.");
    }

    // Test for length
    //output = "Length OK: ";            
   // output += ( password.length >= 5 ) ? yes : no;
    
    var test1 = new Boolean();
    test1 = ( password.length >= 5 );
    
    // Test for letters
    // REMMED re = /[a-zA-Z]/;
    //output += ', Letters: ' + (( re.test(password) ) ? yes : no);
    
//    var test2 = new Boolean();
//    test2 = ( re.test(password) ) ? true : false;

    // Test for numbers            
    // REMMED re = /[0-9]/;
    //output += ', Numbers: ' + (( re.test(password) ) ? yes : no);

    // REMMED var test3 = new Boolean();
    // REMMED test3 = ( re.test(password) ) ? true : false;

    
    // - Bob, 3/08
    // Finding special characters was challenging.
    // I found it works best to seperate the otherwise long list of choices.
    // When the last 2 regex's were put together, at least in certain combination,
    //  it was matching any digit. I never figured this out but splitting them seems to somehow fix it.
    
//    var hasSymbol = new Boolean(false);
//    re = /[\/\\\.\*\+\?\|\(\)\[\]\{\}]/;
//    if ( re.test(password) ) { hasSymbol = true; }
//    re = /[_=:;"'<,>~`]/;
//    if ( re.test(password) ) {  hasSymbol = true; }
//    re = /[!@#$%&^-]/;
//    if ( re.test(password) ) {  hasSymbol = true; }

    //output += ', Symbols: ' + ( (hasSymbol === true) ? yes : no);

//    var test4 = new Boolean();
//    test4 = ( hasSymbol === true ) ? true : false;

    /*
    // Since I was outputting the password in development I wanted to replace harmful html.
    // However, since the password is not outputted now then there is no need to replace.
    // I build the output variable myself. It doesn't contain user input.
    output = output.replace(/&/g, "&amp;");  // do amp first
    output = output.replace(/</g, "&lt;");
    output = output.replace(/>/g, "&gt;");
    output = output.replace(/"/g, "&quot;");
    output = output.replace(/'/g, "&apos;");
    */

    // if (test1 && test2 && test3 && test4) {
    if ( test1 ) {
        output = '<span style="color: green; font-weight: bold;">OK</span>';
    }
    else {
        output = '<span style="color: red;">Does not meet requirements</span>';
    }

    $get('IsPasswordOK').innerHTML = output;
}




function ValidateUserNameTrigger(source)
{

    var username = source.value

    // Make sure username only has certain characters.
    var re = new RegExp();
    re = /[^0-9a-zA-Z\.\-\+@_]/g;
    if ( re.test(username) )
    {
        username = username.replace(re, "");
        $get(source.id).value = username;
        alert("Character(s) found that are disallowed in the username. Allowed characters are letters, numbers, the at sign(@), the period(.), the dash(-) and the underscore(_).");
    }

    var usernameCheckerTimer;
    
    $get('IsUserNameOK').innerHTML = "";

    clearTimeout(usernameCheckerTimer);
    if (username.length > 0 && username.length < 3) {
        $get('IsUserNameOK').innerHTML = "<span style='color: Red;'>Must be more than 2 characters</span>";
    }
    else if (username.length > 0) {
        $get('IsUserNameOK').innerHTML = "<span style='color: #ccc;'>checking...</span>";
        usernameCheckerTimer = setTimeout("checkUsernameUsage('" + username + "');", 1);
    }
}

function checkUsernameUsage(UserName) 
{
    $.ajax({
        url: "../../WebMethods.aspx/IsUserNameTaken",
        type: "POST",
        data: "{ UserName: '" + UserName + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            // True Means the name was found so it is indeed unavailable.
            if (msg.d == true)
                $get('IsUserNameOK').innerHTML = "<span style='color: Red;'>Unavailable</span>";
            else
                $get('IsUserNameOK').innerHTML = "<span style='color: Green; font-weight: bold;'>Available</span>";
            
        }
    });
}

