﻿var xclient;

function LoginSubmit(tForm)
{
	var inNVC = new NameValueCollection();
	inNVC.Set("UserName", Tag("username").value);
	inNVC.Set("Password", Tag("password").value);
	inNVC.Set("Action", "Login");
	
	function Login_Submit_Success()
	{
		var check = xclient.GetResponseText()
		//alert(check.indexOf("Redirect:"));
		
		switch(check)
		{
			case "USER DOES NOT EXIST":
				Tag("body").innerHTML = "User does not exist.";
				break;
			
			case "PASSWORD INVALID":
				Tag("body").innerHTML = "Password invalid";
				break;
				
			default: //this is a successful login
				if(check.indexOf("Redirect:") != -1)
				{
					//alert(check.substring(check.indexOf(":")+1));
					window.location.href = check.substring(check.indexOf(":")+1);
				}
				else
				window.location.href = window.location.pathname;
				break;
		}
	}
	
	xclient = new XmlClient(Login_Submit_Success, Login_Submit_Failure);
	xclient.SendNVC(inNVC);
}

function LogOut()
{
	var inNVC = new NameValueCollection();
	inNVC.Set("Action", "Logout");
	
	function Logout_Submit_Success()
	{
		window.location.href = window.location.pathname;
	}
	
	xclient = new XmlClient(Logout_Submit_Success, Login_Submit_Failure);
	xclient.SendNVC(inNVC);
}

function Login_Submit_Failure() 
{ 
	var newWindow = Show( "about:blank", "_blank", 500, 500 );
	newWindow.document.write( xclient.GetResponseText() );
	newWindow.document.close(); 
}