﻿//************************************************************************************* 
// File     :   aram_membershipCreateUser.js
// Version  :   1.1.0.1
// Requires :   prototype.js
// Author(s):   Rusty Swayne (rss), Kyle Weems (ksw)
// Origin   :   mindfly.com
// Created  :   August 14, 2007 (rss)
// Modified :   September 18, 2007 (ksw)
// Purpose  :   Enhance registration process with on-the-fly status reports on validity of registration. 
//*************************************************************************************

// Version History (only for changes of at least 0.0.1 in version number)
// ===============
// Version 1.0   (08/14/2007) - Basic functionality.
// Version 1.1   (08/27/2007) - Changed script so that instead of inserting values to an unordered list, it adds them to labels placed 'inline' in the control.

//=============================================================================================
// verifyUsernameUnique()
//=============================================================================================
//  Intended to be used to inform user if the username they are attempting to register already exists.
function verifyUsernameUnique(username) 
{
    // Step 1: Make sure the username is at least 5 characters long.
    if(username.length >= 5) 
    {
        // Step 2: Check if that username is already in use.
        if(usernameExists(BLL.MembershipAjaxUtility.UsernameExists(username))=='true')
        {
            // If so, then display a message stating so.
            $('createUserUsernameHelp').innerHTML = '<span class="confirmNo">Sorry, that name already exists.</span>';        
            $('createUserUsernameHelp').style.color = 'red';
        } 
        // If Step 2 is false, then display a message saying name is available.
        else
        {
            $('createUserUsernameHelp').innerHTML = '<span class="confirmYes">That name is available!</span>';
            $('createUserUsernameHelp').style.color = 'green';
        }
    } 
    // If Step 1 was false, then give a message stating username is too short.
    else 
    {
        $('createUserUsernameHelp').innerHTML = '<span class="confirmNo">5 character minimum</span>';
        $('createUserUsernameHelp').style.color = 'red';
	}
} // end of function verifyUsernameUnique()


//=============================================================================================
// verifyEmail()
//=============================================================================================
// When called, checks the input box 'txtRegisterEmail' for a value. If it has none, display no text.
// If it does have a value, see if the text matches a valid e-mail format. If it doesn't, let them know.
// If it does, acknowledge email address looks good.
function verifyEmail()
{
    // Create a regular expression used to check the Email address format.
	re = /\w+\@\w+(\.\w{2,6})$/
	// Get the Email input box.
	var em = document.getElementsByClassName('txtRegisterEmail');
	var regEmail = em[0];
	// Step 1: Check if Email box is empty.
	if(regEmail.value =='')
	{
	    // If so, then don't display the helper message.
	    $('lblCreateEmailHelp').style.display   = 'none';
	    $('lblCreateEmailHelp').innerHTML       = '&nbsp;';
	}
	// If Step 1 is false...
	else
	{
	    // ...then Step 2: Check if the text in the box is a valid Email format.
	    if(re.test(regEmail.value))
	    {
	        // If so, display a message saying the Email address looks correct.
		    $('lblCreateEmailHelp').innerHTML       = '<span class="confirmYes">E-mail address looks good!</span>';
		    $('lblCreateEmailHelp').style.color     = 'green';
		    $('lblCreateEmailHelp').style.display   = '';
	    }
	    // If Step 2 is false...
	    else
	    {
	        // ...then display a message saying that a valid Email address is needed.   
		    $('lblCreateEmailHelp').innerHTML       = '<span class="confirmNo">Please enter a valid Email address.</span>';
		    $('lblCreateEmailHelp').style.color     = 'red';
		    $('lblCreateEmailHelp').style.display   = '';
	    }
	}
} // end of function verifyEmail()


//=============================================================================================
// verifyBirthday()
//=============================================================================================
// Confirms if the birthdate entered is a valid date between 01/01/1900 and 12/12/2099 that matches
// the mm/dd/yyyy format.
function verifyBirthday()
{
    // Get the Birthdate input box
    var bd = document.getElementsByClassName('txtRegisterBirthday');
    var regBirthday = bd[0];
    // Create a regular expression used to confirm the date is in the mm/dd/yyyy format.
    re = /(([0][1]|[0][3]|[0][5]|[0][7-8]|[1][0]|[1][2])\/([0][1-9]|[1-2][0-9]|[3][0-1])\/([1][9][0-9][0-9]|[2][0][0-9][0-9])|([0][4]|[0][6]|[0][9]|[1][1])\/([0][1-9]|[1-2][0-9]|[3][0])\/([1][9][0-9][0-9]|[2][0][0-9][0-9])|([0][2])\/([0][1-9]|[1-2][0-8])\/([1][9][0-9][0-9]|[2][0][0-9][0-9]))$/
    // Step 1: Check if the birthdate box is empty. 
    if(regBirthday.value == '')
    {
        // If it is, then don't display the helper message.
        $('lblCreateBirthdayHelp').style.display    = 'none';
        $('lblCreateBirthdayHelp').innerHTML        = '&nbsp;';
    }
    // If Step 1 is false...
    else
    {
        // ... then Step 2: Check if the text in the box matches the regular expression.
        if(re.test(regBirthday.value))
        {
            // If it does, then display a message confirming that the birthdate is correct.
	        $('lblCreateBirthdayHelp').innerHTML        = '<span class="confirmYes">Thanks for entering birthday!</span>';
	        $('lblCreateBirthdayHelp').style.color      = 'green';
	        $('lblCreateBirthdayHelp').style.display    = '';
	        $('lblCreateEmailHelp').style.display       = '';
        }
        // If Step 2 is false...
        else
        {
            // ... then display a message asking that the correct format is used.
	        $('lblCreateBirthdayHelp').innerHTML        = '<span class="confirmNo">Please use mm/dd/yyyy format.</span>';
	        $('lblCreateBirthdayHelp').style.color      = 'red';
	        $('lblCreateBirthdayHelp').style.display    = '';
	        $('lblCreateEmailHelp').style.display       = '';
        }
    }
} // end of function verifyBirthday


//=============================================================================================
// verifyPassword()
//=============================================================================================
// Confirms that the Confirmation Password box matches the value of the Password box.
function verifyPassword()
{
    // Get the password input box and the confirm password input box.
    var pw              = document.getElementsByClassName('txtRegisterPassword');
    var pwc             = document.getElementsByClassName('txtRegisterPasswordConfirm');
    var password        = pw[0];
    var passwordConfirm = pwc[0];
    // Step 1: Check if the password box is empty.
    if(password.value == '')
    {
        // If so, then don't display a helper message.
        $('lblCreatePasswordHelp').style.display    = 'none';
        $('lblCreatePasswordHelp').innerHTML        = '&nbsp;';      
    }
    // If Step 1 is false...
    else
    {
        // ...then Step 2: Check if the confirm password box is empty.
        if(passwordConfirm.value == '')
        {
            // If it is, then display a message reminding them to confirm the password.
            $('lblCreatePasswordHelp').innerHTML        = "<span class='confirmNo'>Don't forget to confirm the password.</span>";
            $('lblCreatePasswordHelp').style.color      = 'red';
            $('lblCreatePasswordHelp').style.display    = '';
        }
        // If Step 2 is false...
        else
        {
            // ...then Step 3: Check if the password and password confirm boxes match each other.
            if(password.value == passwordConfirm.value)
            {
                // If they do, then display a message thanking them for confirming.
                $('lblCreatePasswordHelp').innerHTML        = '<span class="confirmYes">Thanks for confirming the password!</span>';
                $('lblCreatePasswordHelp').style.color      = 'green';
                $('lblCreatePasswordHelp').style.display    = '';
            }
            // If Step 3 is false...
            else
            {
                // ...then display a message reminding them that the confirmation password needs to match the password.
                $('lblCreatePasswordHelp').innerHTML        = '<span class="confirmNo">Make sure your confirmation matches the password.</span>';
                $('lblCreatePasswordHelp').style.color      = 'red';
                $('lblCreatePasswordHelp').style.display    = '';
            }
            
        }
    }
} // end of function verifyPassword()


//=============================================================================================
// usernameExists()
//=============================================================================================
// adds help text to span with id createUserUsernameHelp
function usernameExists(res) 
{
    var exists = res.value;
    if(exists) { return('true');  }
    else       { return('false'); }
} // end of function usernameExists()


//=============================================================================================
// showHideState()
//=============================================================================================
function showHideState(country) 
{
    var label = $('labelRegisterState');
    if(country == 'US')
        label.style.display = '';
    else
        label.style.display = 'none';
} // end of function showHideState()


//=============================================================================================
// hideLoginHelpers()
//=============================================================================================
// Called on page load to pre-set the login aids.
function hideLoginHelpers()
{
    // Ensure email help message is empty and invisible.
    if($('lblCreateEmailHelp'))
    {   $('lblCreateEmailHelp').innerHTML           = '&nbsp;'; 
        $('lblCreateEmailHelp').style.display       = 'none';
    }
    // Ensure birthday help message is invisible.
    if($('lblCreateBirthdayHelp')){ $('lblCreateBirthdayHelp').style.display = 'none'; }
    // Ensure password help message is invisible.
    if($('lblCreatePasswordHelp')){ $('lblCreatePasswordHelp').style.display = ''; }
} // end of function hideLoginHelpers()


// This is called when page loads, activating function hideLoginHelpers()
addLoadEvent(hideLoginHelpers);

