

loginUserEnter = function(event,ourform)
{
	switch (BrowserDetect.browser)
		{
		case "Explorer":
			if (window.event && window.event.keyCode == 13)
				{
					loginUser();
				}
   			else
				{
					return true;
				}
		break
		
		default:
			if (event && event.which == 13)
				{
					loginUser();
				}
    		else
				{
					return true;
				}
		break
		}

}

loginUser = function()
{
	showProcessor('login-logout');
	var username = getControlValue('txtUsername');
	var password = getControlValue('txtLoginPassword');
	
	var url = location.href;
	engLogin.loginUser('txtUsername', username, 'txtLoginPassword', password, callback_loginUser);
}

callback_loginUser = function(res)
{
	var results = res.value.Tables[0];
	if (results.Rows[0].result == "True")
	{
	/*
		if (document.getElementById("chkRememberMe").checked)
			{
			newCookie('a', document.getElementById("txtUsername").value, 0)
			newCookie('b', document.getElementById("txtLoginPassword").value, 0)
			}
	*/		
		var controls = res.value.Tables[1];
		for (var i=0; i<controls.Rows.length; i++)
		{
			clearInputField(controls.Rows[i].controlID);
			setControlErrorMessage(controls.Rows[i].controlID,'');
		}
		switch (results.Rows[0].errMessage)
		{
			case "valid":			
			window.setTimeout("hideLoginPanel();", 750);
			break
		}
	}
	else
	{
		switch (results.Rows[0].errMessage)
		{
			case "invalid":
				hideProcessor('login-logout');
				alert('Incorrect username/password combination.');
			break
			case "nomatch":
				hideProcessor('login-logout');
				alert('User does not exist.');
			break
			case "unverified":
				hideProcessor('login-logout');
				alert('You must verify your email address by clicking the link provided by Obami.');
			break
		}
		var controls = res.value.Tables[1];
		for (var i=0; i<controls.Rows.length; i++)
		{
			if (controls.Rows[i].valid == "True")
			{
				setControlErrorMessage(controls.Rows[i].controlID,'');
			}
			else
			{
				setControlErrorMessage(controls.Rows[i].controlID,controls.Rows[i].errMessage);
			}
		}
	}
}

hideLoginPanel = function()
{
	window.location = "/index.aspx";
	//window.location = "http://www.obami.com/index.aspx";
}

callback_redirect = function(res)
{
	window.location = window.location;
}

logoutUser = function()
{
	showProcessor('login-logout');
	engLogin.logout(callback_logoutUser);
}

callback_logoutUser = function(res)
{
	//window.location = "http://www.obami.com/index.aspx";
	window.location = "/index.aspx"; 
}

sendPassword = function()
{
	var email = getControlValue('txtForgotPassword');
	showFeedback('reminderActionContainer');	
	engLogin.sendPasswordReminder('txtForgotPassword', email, callback_sendPassword);
}

callback_sendPassword = function(res)
{
	if (res.error!=null){alert(res.error)};
	
	var results = res.value.Tables[0];
	if (results.Rows[0].result == "True")
	{
		var controls = res.value.Tables[1];
		for (var i=0; i<controls.Rows.length; i++)
		{
			clearInputField(controls.Rows[i].controlID);
			resetControlErrorMessage(controls.Rows[i].controlID,'');
		}
		switch (results.Rows[0].errMessage)
		{
			case "sent":

			feedbackValid('Sending','Sent');
			window.setTimeout('hidePasswordReminder();', 4000);
			
			break
			case "nomatch":
				feedbackInvalid('reminderActionContainer');
				setSpanValue('txtForgotPasswordErr', 'Sorry, there is no user with this email address on Obami');
				document.getElementById("txtForgotPasswordErr").style.display = "block";
				window.setTimeout(function(){processValidationResponse(controls)}, 2001);
			break
			case "invalid":
				feedbackInvalid('reminderActionContainer');
				setSpanValue('txtForgotPasswordErr', 'Sorry, there is no user with this email address on Obami');
				document.getElementById("txtForgotPasswordErr").style.display = "block";
				window.setTimeout(function(){processValidationResponse(controls)}, 2001);
			break
		}
	}
	else
	{
		var controls = res.value.Tables[1];
		
		switch (results.Rows[0].errMessage)
		{
		
			case "nomatch":
				feedbackInvalid('reminderActionContainer');
				setSpanValue('txtForgotPasswordErr', 'Sorry, there is no user with this email address on Obami');
				document.getElementById("txtForgotPasswordErr").style.display = "block";
				window.setTimeout(function(){processValidationResponse(controls)}, 2001);
			break
			case "invalid":
				feedbackInvalid('reminderActionContainer');
				window.setTimeout(function(){processValidationResponse(controls)}, 2001);
			break
		}
	
	}
}

hidePasswordReminder = function()
{
	cleanupFeedback('reminderActionContainer');
	
	document.getElementById("txtForgotPasswordErr").style.display = "none";
	setControlValue('txtForgotPassword','');
	closeModalWindow('passwordReminder');
}

