// Login Functions
var default_alert_html = "";
function login(form,postTo)
{
	if ( document.getElementById('u_username').value == "" || document.getElementById('u_password').value == "")
	{
		msg = '<span class="error">Error:</span><br> Please enter a valid username and password, both fields are required.</p>';
		showLoginAlert(msg);
	}
	else
	{
		default_alert_html = document.getElementById('loginAlert').innerHTML;
		html = '<table align="center" cellpadding="0" cellspacing="0" style="border: 1px solid #CCCCCC; background-color: #FFFFFF;"><tr><td align="center" style="padding: 5px 25px 5px 25px;"><table border="0" cellspacing="0" cellpadding="0"><tr><td>&nbsp;<img src="images/icons/loading.gif" width="32" height="32" name="loading_img" id="loading_img"></td><td>&nbsp;&nbsp;&nbsp;</td><td style="font-size: 15px;">Processing</td></tr></table></td></tr></table>';
		document.getElementById('loginAlert').className = "visible";
		document.getElementById('loginAlert').innerHTML = html;

		param = buildFormQuery(form);
		makeAjaxPostRequest(postTo, param, loginHandler);
		document.getElementById('u_username').value = "";
		document.getElementById('u_password').value = "";
	}
}

function loginHandler(result)
{
	if(result == "Success")
	{
		msg = '<span class="success">Successful login:</span><br> You have successfully logged in.<br> If you are not automatically redirected <a href="home.php"><font color=#009900>click&nbsp;here</font></a>';
		showLoginAlert(msg);
		window.setTimeout('window.location.reload();', 1000);
	}
	
	if(result == "Error: 2")
	{
		msg = '<span class="error">Error: 2</span><br>Your account is inactive';
		showLoginAlert(msg);
	}
	
	if(result == "Error: 1")
	{
		msg = '<span class="error">Error: 1</span><br>Invalid username or password. Please try again with a valid username and password';
		showLoginAlert(msg);
	}
}

function redirect()
{
	window.open('home.php', 'home', 'toolbar=0, menubar=0, resizable=1');
	window.close();
}

function setFocus() 
{
	if ( document.getElementById('loginForm') )
	{
		document.getElementById('loginForm').u_username.select();
		document.getElementById('loginForm').u_username.focus();
	}
	else
	{
		window.setTimeout('setFocus()',10);
	}
}

function setFocusEmail() 
{
	if ( document.getElementById('userMail') )
	{
		document.getElementById('userMail').u_email.select();
		document.getElementById('userMail').u_email.focus();
	}
	else
	{
		window.setTimeout('setFocusEmail()',10);
	}
}

function resetPasswordHandler(result)
{
	if(result == "NoUser")
	{
		al = '<span class="error">Error:</span> Email address not found.';
	}
	else
	{
		al = 'An email has been sent to your email address to allow you to reset your password.';
		ajaxpage('tpl/aj.login.htm', 'alertArea', true, 250, 'Sending');
		setFocus();
	}
	showLoginAlert(al);
}

function resetPassword(form,postTo)
{
	if ( checkEmail(document.getElementById('u_email').value) == true )
	{
		param = buildFormQuery(form);
		makeAjaxRequest(postTo + '?' + param, resetPasswordHandler);
	}
	else
	{
		alert('Please enter a valid email address');
		document.getElementById('u_email').value = "";
		document.getElementById('u_email').select();
		document.getElementById('u_email').focus();
	}
}

function checkEmail(email)
{
	var x = email;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) 
		return true;
	else 
		return false;
}

function showLoginAlert(msg)
{
	if( default_alert_html != "" )
		document.getElementById('loginAlert').innerHTML = default_alert_html;
	
	if( document.getElementById('loginAlert').className == "visible")
	{		
		hideLoginAlert();
		window.setTimeout("showLoginAlert('"+msg+"')", 250);
	}
	else
	{
		document.getElementById('loginAlert').className = "visible";
		document.getElementById('loginAlertText').innerHTML = msg;
	}
}

function hideLoginAlert()
{
	document.getElementById('loginAlert').className = "hidden";
}
