/**
 * @file login.js
 * @author Stephen Wright
 * @author Marc Matthysse
 * @date Nov 2006
 */
 
	
 	// have the inputs of the login form listen for enter key press to submit
	Event.observe(window, 'load', function () {		
		if (true || SystemCanRunDesignLinx())
		{		
			//$('userLogin').focus();
			$$('#loginForm input').each(function(el){
				Event.observe(el, 'keypress', function (e) { 
					if (e.keyCode == 13) submitLoginForm ();
				});
			});
			getme();
		}
		else
		{
			var errMsg 
				= "<div>"
				+ "Currently the 3Dream "
				+ "3D room planner utilizes a web control that only works in "
				+ "<b>Internet Explorer versions 6 and up. </b>"
				+ "</div>";
			$('login_form').innerHTML = errMsg;
		}
	});

	// Populate form with cookie values
	function getme()
	{
		//alert('getme');
		
		if ($('userLogin') == null)
			return;

		//alert('userLogin found in page');
		
		if (GetCookie('username') != null)
		{
			//alert('username found in cookie');
			$('userLogin').value 		= GetCookie('username');
			$('userPassword').value 	= GetCookie('password');
			$('rememberUser').checked 	= 'true' == GetCookie('save');
		}
		//alert('checked: ' + $('rememberUser').checked);
	}

	// Save login credentials to cookie
	function saveme()
	{	
		//alert('saveme');
		var user = $F('userLogin');
		var pass = $F('userPassword');		
		
		if (0 == user.length || 0 == pass.length)
		{
			alert('You must enter a username/password.');
			return false;
		}
		
		if ($('rememberUser').checked)
		{
			expdate = new Date();
			expdate.setTime(expdate.getTime()+(365 * 24 * 60 * 60 * 1000));
			SetCookie('username', user, expdate, '/', null, true);
			SetCookie('password', pass, expdate, '/', null, true);
			SetCookie('save', 'true', expdate, '/');
		}
		else
		{
			DeleteCookie('username', '/');
			DeleteCookie('password', '/');
			DeleteCookie('save', '/');
		}	
	}
		
	function forgotPassword ()
	{
		location.href = CONTEXT_PATH 
			+ '/jsp/public/account/manage/forgotPassword.jsp?userEmail='
			+ $F('userLogin');
		return false;
	}
	
	function submitLoginForm ()
	{
		if(saveme()!=false)
			document.getElementById('login_form').submit();
	}
	
	