// JavaScript Document

function loginClick()
{
	instructionContainer = document.getElementById('instructionSet');
	if (instructionContainer)
	{
		instructionContainer.innerHTML = '<img src="adminThemes/themes/default/images/loadingAnimation2.gif" /> <strong>Processing your log in ...</strong>';
	}

	// Disable the login button
	document.getElementById('submitButton').disabled = true;
	document.getElementById('webAdminLoginPassword').disabled = true;
	document.getElementById('webAdminLoginEmail').disabled = true;

	this.xml = new XMLWriter();	
	this.xml.BeginNode("loginInformation");

	this.xml.BeginNode('webAdminLoginEmail');	
	this.xml.WriteString(document.getElementById('webAdminLoginEmail').value);
	this.xml.EndNode();
	this.xml.BeginNode('webAdminLoginPassword');	
	
	this.xml.WriteString(document.getElementById('webAdminLoginPassword').value);
	this.xml.EndNode();

	this.xml.EndNode();
	
	//Get the XHR object
	this.ro = new xmlHttpConnection();

	//Assign the script...
	this.script = 'index.php?&action=ajax&ajaxAction=login';
	
	//Specify the response handler...
	this.handler = function (xmlDocument)
		{
			var success = xmlDocument.getElementsByTagName('success')[0];
			if (success.firstChild.nodeValue == 'true')
			{
				//alert ('were good...');
				window.location = 'index.php?area=home';
			}
			else
			{
				window.location = 'index.php?failedLoginAttempt=1';
			}
		};

	//Connect to the remote script, and process the request.		
	this.ro.connect(this.script, "POST", this.xml.ToString(), this.handler);
	
}


function resetPassword()
{
	/* First get the password */
	var confirmText = '<p>Please enter your email address. We will generate a new random password, and send it to you via email.</p><p><strong>Email Address</strong><br /><input type="text" id="newPasswordEmail" style="width: 250px;" /></p>';

	var myMessage = newConfirm('Confirm Email', confirmText, "submitResetPassword()", "");
	document.getElementById('newPasswordEmail').focus();

}

function submitResetPassword()
{
	instructionContainer = document.getElementById('instructionSet');
	if (instructionContainer)
	{
		instructionContainer.innerHTML = '<img src="adminThemes/themes/default/images/loadingAnimation2.gif" /> <strong>Resetting your password ...</strong>';
	}

	// Disable the login button
	document.getElementById('submitButton').disabled = true;
	document.getElementById('webAdminLoginPassword').disabled = true;
	document.getElementById('webAdminLoginEmail').disabled = true;


	var emailAccount = document.getElementById('newPasswordEmail').value;
	

	// If the email is blank, then return an error
	if (emailAccount == '')
	{		
		var msg = alert('Sorry, you must enter a valid email to reset your password');
	}
	else
	{
		this.xml = new XMLWriter();	
		this.xml.BeginNode("emailInformation");

		this.xml.BeginNode('emailAccount');	
		this.xml.WriteString(emailAccount);
		this.xml.EndNode();

		this.xml.EndNode();
		
		//Get the XHR object
		this.ro = new xmlHttpConnection();

		//Assign the script...
		this.script = 'index.php?&action=ajax&ajaxAction=resetPassword';
		
		//Specify the response handler...
		this.handler = function (xmlDocument)
			{
				var success = xmlDocument.getElementsByTagName('success')[0];
				if (success.firstChild.nodeValue == 'true')
				{
					//alert ('were good...');
					var msg = newAlert('Success', 'A new password has been emailed to you. Please check your email and spam filters');
				}
				else
				{
					var msg = newAlert('Error', 'Sorry, we could not find an account under that email address. Please try again.');
				}

				instructionContainer = document.getElementById('instructionSet');
				if (instructionContainer)
				{
					instructionContainer.innerHTML = '';
				}

				document.getElementById('submitButton').disabled = false;
				document.getElementById('webAdminLoginPassword').disabled = false;
				document.getElementById('webAdminLoginEmail').value = emailAccount;
				document.getElementById('webAdminLoginEmail').disabled = false;
				document.getElementById('webAdminLoginPassword').focus();

			};

		//Connect to the remote script, and process the request.		
		this.ro.connect(this.script, "POST", this.xml.ToString(), this.handler);

	}

	return false;
}	
