function cookieMemberID(form)
{
	if (form.remember.checked)
	{
		id = form.customer_id.value;
		
		var days = 30; // time to keep cookie
		
		// check validity of member ID
		if (validMemberID(id))
		{
			var expiry = new Date();
			
			expiry.setTime(expiry.getTime() + (days * 86400000)); // days * [milliseconds in a day]
			
			document.cookie = 'lastMemberID='+id+'; expires='+expiry.toGMTString()+'; path=/';
		}
	}
	else
	{
		// expire cookie so that it will not populate
		var lastID = getCookie('lastMemberID');
		
		// if exists
		if (lastID != null)
		{
			// expire cookie so that it will not populate
			var expiry = new Date();
			
			expiry.setTime(expiry.getTime() - (1 * 86400000)); // 1 days * [milliseconds in a day]
			
			document.cookie = 'lastMemberID=; expires='+expiry.toGMTString()+'; path=/';
		}
	}
}

function validMemberID(sText)
{
	var regex = /[0-9]{10}/;
	
	return regex.test(sText);
}

/**
 * Auto-fill the member id with the cookied value
 */
function populateMemberID()
{
	// if login form exists
	if (document.loginForm)
	{
		var lastID = getCookie('lastMemberID');
		
		if (lastID != null)
		{
			// in case something else is already filling the field
			if (document.loginForm.customer_id.value == '' || document.loginForm.customer_id.value == 'Member Number')
				document.loginForm.customer_id.value = lastID;
			document.loginForm.remember.checked = true;
			if (document.altloginForm)
			{
				if (document.altloginForm.customer_id.value == '' || document.altloginForm.customer_id.value == 'Member Number')
					document.altloginForm.customer_id.value = lastID;
				document.altloginForm.remember.checked = true;
			}
		}
	}
}

// Retrieve the value of the cookie with the specified name.
function getCookie(sName)
{
	// cookies are separated by semicolons
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
		// a name/value pair (a crumb) is separated by an equal sign
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0]) 
			return unescape(aCrumb[1]);
	}
	
	// a cookie with the requested name does not exist
	return null;
}

/*  *******************************************************************************************************************
	Assigns event handlers to the password fields
		- mcconnelll @ 20080604 for mantis #9361
*/
function passwordTextHandler ( e ) {
	if (document.loginForm) {
//		document.loginForm.setAttribute("autocomplete", "off");
//		document.loginForm.autocomplete = "off";
		Event.observe(document.loginForm.customer_id,   'focus', function(e) { populateUseridText(e, document.loginForm.customer_id) });
		Event.observe(document.loginForm.customer_id,   'blur',  function(e) { populatePasswordText(e, document.loginForm.customer_pass) });
		Event.observe(document.loginForm.customer_pass, 'focus', function(e) { populatePasswordText(e, document.loginForm.customer_pass) });
		populatePasswordText( e, document.loginForm.customer_pass );
	}

	if (document.altloginForm) {
//		document.altloginForm.autocomplete = "off";
		Event.observe(document.altloginForm.customer_id,   'focus', function(e) { populateUseridText(e, document.altloginForm.customer_id) });
		Event.observe(document.altloginForm.customer_id,   'blur',  function(e) { populatePasswordText(e, document.altloginForm.customer_pass) });
		Event.observe(document.altloginForm.customer_pass, 'focus', function(e) { populatePasswordText(e, document.altloginForm.customer_pass) });
		populatePasswordText( e, document.altloginForm.customer_pass );
	}
}

function populateUseridText( e, passField ) {
	if ( e.type == "focus" ) {
		if (passField.value == 'Member Number') {
			passField.value = "";
		}
	}
	else { // ( e.type == "load" || e.type == "blur" || e.type == "update", etc... )
		// stick some code here to dynamically populate the field if you need to
	}
}

/*  *******************************************************************************************************************
	Event handler for adding the word 'Password' if the field is blank
		- mcconnelll @ 20080604 for mantis #9361
			- had to change to use a bg image because msie6 found other methods too confusing
*/
function populatePasswordText( e, passField ) {
	if ( e.type == "focus" ) {
		passField.removeClassName("blank");
	}
	else { // ( e.type == "load" || e.type == "blur" || e.type == "update", etc... )
 		if (passField.type == "password" && passField.value.match(/\S+/)) {
			passField.removeClassName("blank");	// clicking the id field can sometimes auto-populate password field
		}
		else {
			if (e.type != "blur") {
				passField.addClassName("blank");
			}
		}
	}
}


/*  *******************************************************************************************************************
	Window Loaded Observers
		Assign events once the window has loaded to trigger setup of objects
*/
if ( isW3CDOM && isPrototype() ) {
  	/* happens from top down */
	Event.observe(window, 'load', populateMemberID, false);
	Event.observe(window, 'load', passwordTextHandler, false);
}
