// JavaScript Document
var NORMAL_STATE = 4;
var http = getHTTPObject();// We create the HTTP Object for Login
var phpbbhttp = getHTTPObject();// We create the HTTP Object for PHPBB
var myUserName;
var myPassWord;
var htmlTemp;

function PasswordPopup()
{
		window.open("login/accountinforequest.php","mywindowx","menubar=0,resizable=0, width=400, height=240");
}

// validateLogin method: validates a login request
function DoLoginOrLogout()
{
	var returnVal;
	
	loginstate = document.getElementById('loginstate').value;
	if (loginstate.indexOf('loggedin') == -1)
		alreadyloggedIn = false;
	else
		alreadyloggedIn = true;
	
	// Check if we are already logged in
	if (alreadyloggedIn)
	{
		// Do Logout
		// Disable Submit Button and rename to Logging In
		submitButton = document.getElementById('LoginButton');
		submitButton.disabled = true;
		submitButton.value = 'Logging Out...';

		// open the HTTP connection
		http.open('POST', 'http://www.artofping.com/logout.php', true);

		// Set Required HTTP Request Header
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		// Setup CallBack Function
		http.onreadystatechange = handleLogoutResponse;
	
		// Send POST Body
		http.send('LogoutMeBoy');
		
		myUserName = '';
		myPassWord = '';
		
		returnVal = false;
	}
	else
	{
		// get form form elements 'username' and 'password'
		username = document.getElementById('uname').value;
		password = document.getElementById('pword').value;
		submitButton = document.getElementById('LoginButton');
		
		postbody = 'psName=' + username + '&psPassword=' + password;
		myUserName = username;
		myPassWord = password;
		
		// ignore if either is empty
		if ((username != '') && (password  != '')) 
		{
			// Disable Submit Button and rename to Logging In
			submitButton.disabled = true;
			submitButton.value = 'Logging In...';
	
			// open the HTTP connection
			http.open('POST', 'http://www.artofping.com/loginresponse.php', true);
	
			// Set Required HTTP Request Header
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
			// Setup CallBack Function
			http.onreadystatechange = handleLoginResponse;
		
			// Send POST Body
  			http.send(postbody);
		}
		else
		{
			alert('No User/Pass Supplied');
			submitButton.value = 'Need U/P';
		}
		returnVal = false;
	}
	return returnVal;
}

// handleLoginResponse method: called when the login results are returned from the server
function handleLoginResponse()
{
	// Are we done?
	if (http.readyState == NORMAL_STATE) 
	{
		// Fetch the Result
		results = http.responseText;
		submitButton = document.getElementById('LoginButton');
		if (results.indexOf('Logged in') == -1)
		{
			alert('Error Logging In: Invalid username or password supplied');
			submitButton.value = 'Login';
			submitButton.disabled = false;
		}
		else
		{
			/// Login to the forums
			// Abort any current requests
			phpbbhttp.abort();
			
			// Setup CallBack Function
			phpbbhttp.onreadystatechange = HandlePHPBBLoginResponse;

			// open the HTTP connection
			phpbbhttp.open('POST', 'http://www.artofping.com/forum/login.php', true);
	
			// Set Required HTTP Request Header
			phpbbhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			phpbbhttp.setRequestHeader('Referer', 'http://www.artofping.com/forum/login.php?');
		
			// Construct POST Body
			postbody = 'username=' + myUserName + '&password=' + myPassWord + '&redirect=&login=Log+in';
			
			// Send POST Body
  			phpbbhttp.send(postbody);

			urlString = location.href;
			if ((urlString.indexOf('redirectlogin.php') != -1) || (urlString.indexOf('logout.php') != -1))
			{
				location.href = 'http://www.artofping.com/account-info.php';
			}
			else
			{
				// Get Login Box
				loginform = document.getElementById('loginbox');					
				
				// Dynamically Modify login box
				htmlTemp = '<div align = "center" style = "font-size: 12px; color: #FFFFFF;  width: 144px">Logged in as <br /></div><table width="100%" border="0"><td height="20"></td></table><div align = "center" style = "font-size: 12px; color: #800000; font-weight: 600; width: 144px; ">' + myUserName + '<br /><br /></div><table width="100%" border="0"><td height="4"></td></table><div align="center" style = "width: 144px; "><input type="hidden" id = "loginstate" value="loggedin" /><input type="submit" id = "LoginButton" name="LoginButton" value="Logout" /></div><table width="100%" border="0"><td height="';
				if (navigator.appVersion.indexOf("MSIE")!=-1)
				{
					loginform.innerHTML = htmlTemp + '36"></td></table>';
				}
				else
				{
					loginform.innerHTML = htmlTemp + '29"></td></table>';
				}
					
				
				// Get Submit Button All Over Again
				submitButton = document.getElementById('LoginButton');
				submitButton.value = 'Logout';
				submitButton.disabled = false;
			}
		}
	}
}

// handleLogoutResponse method: called when the logout results are returned from the server
function handleLogoutResponse()
{
	// Are we done?
	if (http.readyState == NORMAL_STATE) 
	{
		// Fetch the Result
		results = http.responseText;
		submitButton = document.getElementById('LoginButton');
		if (results.indexOf('logged out') == -1)
		{
			submitButton.value = 'LogoutX';
			submitButton.disabled = false;
		}
		else
		{
			/// Login to the forums
			// Abort any current requests
			phpbbhttp.abort();
			
			// Setup CallBack Function
			phpbbhttp.onreadystatechange = HandlePHPBBLoginResponse;

			// open the HTTP connection
			phpbbhttp.open('GET', 'http://www.artofping.com/forum/login.php?logout=true', true);
	
			// Set Required HTTP Request Header
			phpbbhttp.setRequestHeader('Referer', 'http://www.artofping.com/forum/index.php?');
		
			// Send POST Body
  			phpbbhttp.send('');

			urlString = location.href;
			if (urlString.indexOf('account-info.php') != -1)
			{
				location.href = 'logout.php';
			}
			else
			{
				// Get Login Box
				loginform = document.getElementById('loginbox');					
				
				// Dynamically Modify login box
				loginform.innerHTML = '<label>Username:</label><input name="uname" id= "uname" /> <label>Password:</label><input name="pword" id = "pword" type="password"/><input type="hidden" id = "loginstate" value="loggedout" /><input type="submit" id = "LoginButton" name="LoginButton" value="Login" /><div style="padding-left: 10px"><a href="javascript: DisplayRegistrationWindow()" style = "color:#FFFFFF; text-decoration: none; ">How do I register ?</a></div><div style="padding-left: 4px"><a href="javascript: PasswordPopup()" style="color:#FFFFFF; text-decoration: none; ">Forgot My Password</a></div>'; 
				
				// Get Submit Button All Over Again
				submitButton = document.getElementById('LoginButton');
				submitButton.value = 'Login';
				submitButton.disabled = false;
			}
		}
	}
}

function HandlePHPBBLoginResponse()
{
	// Are we done?
	if (phpbbhttp.readyState == NORMAL_STATE) 
	{
	}
}


function DisplayRegistrationWindow()
{
	window.open("registration.php","mywindow","menubar=0,resizable=0, width=400, height=70");
}
